| 1 |
|
| /* |
| 2 |
|
| * Small, portable SipHash implementation shared by the Encoder and Decoder. |
| 3 |
|
| * |
| 4 |
|
| * Version 0 is SipHash-2-4-64, version 1 is SipHash-2-4-128, and version 2 |
| 5 |
|
| * is SipHash-3-6-128. |
| 6 |
|
| * The 128-bit output follows the SipHash-128 initialization/finalization |
| 7 |
|
| * convention (v1 ^= 0xee, then v2 ^= 0xee and v1 ^= 0xdd). |
| 8 |
|
| */ |
| 9 |
|
| #ifndef SRL_SIPHASH_H |
| 10 |
|
| #define SRL_SIPHASH_H |
| 11 |
|
| |
| 12 |
|
| #include <stddef.h> |
| 13 |
|
| |
| 14 |
|
| /* Older Perl headers expose U64 only to the interpreter itself. */ |
| 15 |
|
| typedef U64TYPE srl_u64; |
| 16 |
|
| |
| 17 |
|
| typedef struct { |
| 18 |
|
| srl_u64 v0, v1, v2, v3; |
| 19 |
|
| srl_u64 total; |
| 20 |
|
| U8 tail[8]; |
| 21 |
|
| U8 tail_len; |
| 22 |
|
| U8 compression_rounds; |
| 23 |
|
| U8 finalization_rounds; |
| 24 |
|
| U8 is_128_bit; |
| 25 |
|
| } srl_siphash_ctx; |
| 26 |
|
| |
| 27 |
|
| SRL_STATIC_INLINE srl_u64 |
| 28 |
1212 |
| srl_siphash_rotl64(srl_u64 x, unsigned int b) |
| 29 |
|
| { |
| 30 |
1212 |
| return (x << b) | (x >> (64 - b)); |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
|
| SRL_STATIC_INLINE srl_u64 |
| 34 |
65 |
| srl_siphash_load_le64(const U8 *p) |
| 35 |
|
| { |
| 36 |
65 |
| return ((srl_u64)p[0]) | ((srl_u64)p[1] << 8) | |
| 37 |
65 |
| ((srl_u64)p[2] << 16) | ((srl_u64)p[3] << 24) | |
| 38 |
65 |
| ((srl_u64)p[4] << 32) | ((srl_u64)p[5] << 40) | |
| 39 |
65 |
| ((srl_u64)p[6] << 48) | ((srl_u64)p[7] << 56); |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
|
| #define SRL_SIPHASH_ROUND(ctx) do { \ |
| 43 |
|
| (ctx)->v0 += (ctx)->v1; (ctx)->v1 = srl_siphash_rotl64((ctx)->v1, 13); \ |
| 44 |
|
| (ctx)->v1 ^= (ctx)->v0; (ctx)->v0 = srl_siphash_rotl64((ctx)->v0, 32); \ |
| 45 |
|
| (ctx)->v2 += (ctx)->v3; (ctx)->v3 = srl_siphash_rotl64((ctx)->v3, 16); \ |
| 46 |
|
| (ctx)->v3 ^= (ctx)->v2; \ |
| 47 |
|
| (ctx)->v0 += (ctx)->v3; (ctx)->v3 = srl_siphash_rotl64((ctx)->v3, 21); \ |
| 48 |
|
| (ctx)->v3 ^= (ctx)->v0; \ |
| 49 |
|
| (ctx)->v2 += (ctx)->v1; (ctx)->v1 = srl_siphash_rotl64((ctx)->v1, 17); \ |
| 50 |
|
| (ctx)->v1 ^= (ctx)->v2; (ctx)->v2 = srl_siphash_rotl64((ctx)->v2, 32); \ |
| 51 |
|
| } while (0) |
| 52 |
|
| |
| 53 |
|
| SRL_STATIC_INLINE void |
| 54 |
53 |
| srl_siphash_compress(srl_siphash_ctx *ctx, srl_u64 m) |
| 55 |
|
| { |
| 56 |
|
| unsigned int i; |
| 57 |
53 |
| ctx->v3 ^= m; |
| 58 |
159 |
▶ | for (i = 0; i < ctx->compression_rounds; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ctx->compression_rounds; i++) |
106 |
53 |
159 |
|
| 59 |
106 |
| SRL_SIPHASH_ROUND(ctx); |
| 60 |
53 |
| ctx->v0 ^= m; |
| 61 |
53 |
| } |
| 62 |
|
| |
| 63 |
|
| SRL_STATIC_INLINE void |
| 64 |
12 |
| srl_siphash_init_rounds(srl_siphash_ctx *ctx, const U8 key[16], |
| 65 |
|
| U8 compression_rounds, U8 finalization_rounds, |
| 66 |
|
| int is_128_bit) |
| 67 |
|
| { |
| 68 |
12 |
| srl_u64 k0 = srl_siphash_load_le64(key); |
| 69 |
12 |
| srl_u64 k1 = srl_siphash_load_le64(key + 8); |
| 70 |
12 |
| ctx->v0 = ((srl_u64)0x736f6d6570736575ULL) ^ k0; |
| 71 |
12 |
| ctx->v1 = ((srl_u64)0x646f72616e646f6dULL) ^ k1; |
| 72 |
12 |
| ctx->v2 = ((srl_u64)0x6c7967656e657261ULL) ^ k0; |
| 73 |
12 |
| ctx->v3 = ((srl_u64)0x7465646279746573ULL) ^ k1; |
| 74 |
12 |
▶ | if (is_128_bit) |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_128_bit) |
12 |
0 |
12 |
|
| 75 |
12 |
| ctx->v1 ^= (srl_u64)0xee; |
| 76 |
12 |
| ctx->total = 0; |
| 77 |
12 |
| ctx->tail_len = 0; |
| 78 |
12 |
| ctx->compression_rounds = compression_rounds; |
| 79 |
12 |
| ctx->finalization_rounds = finalization_rounds; |
| 80 |
12 |
| ctx->is_128_bit = (U8)(is_128_bit != 0); |
| 81 |
12 |
| } |
| 82 |
|
| |
| 83 |
|
| SRL_STATIC_INLINE void |
| 84 |
|
| srl_siphash_init(srl_siphash_ctx *ctx, const U8 key[16], U8 version) |
| 85 |
|
| { |
| 86 |
|
| if (version == SRL_CHECKSUM_VERSION_SIPHASH_3_6_128) |
| 87 |
|
| srl_siphash_init_rounds(ctx, key, 3, 6, 1); |
| 88 |
|
| else if (version == SRL_CHECKSUM_VERSION_SIPHASH_2_4_128) |
| 89 |
|
| srl_siphash_init_rounds(ctx, key, 2, 4, 1); |
| 90 |
|
| else |
| 91 |
|
| srl_siphash_init_rounds(ctx, key, 2, 4, 0); |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| /* Zero means an unsupported version; callers must validate before indexing |
| 95 |
|
| * key slots or using a tag length. */ |
| 96 |
|
| SRL_STATIC_INLINE size_t |
| 97 |
|
| srl_checksum_tag_length(U8 version) |
| 98 |
|
| { |
| 99 |
|
| if (version == SRL_CHECKSUM_VERSION_SIPHASH_2_4_64) |
| 100 |
|
| return 8; |
| 101 |
|
| if (version == SRL_CHECKSUM_VERSION_SIPHASH_2_4_128 |
| 102 |
|
| || version == SRL_CHECKSUM_VERSION_SIPHASH_3_6_128) |
| 103 |
|
| return 16; |
| 104 |
|
| return 0; |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
|
| SRL_STATIC_INLINE void |
| 108 |
24 |
| srl_siphash_update(srl_siphash_ctx *ctx, const U8 *data, size_t len) |
| 109 |
|
| { |
| 110 |
24 |
| size_t i = 0; |
| 111 |
24 |
| ctx->total += (srl_u64)len; |
| 112 |
|
| |
| 113 |
24 |
▶ | if (ctx->tail_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tail_len) { |
6 |
18 |
24 |
|
| 114 |
11 |
▶ | while (i < len && ctx->tail_len < 8) |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < len && ctx->tail_len < 8) |
10 |
1 |
11 |
| while (i < len && ctx->tail_len < 8) |
5 |
5 |
10 |
|
| 115 |
5 |
| ctx->tail[ctx->tail_len++] = data[i++]; |
| 116 |
6 |
▶ | if (ctx->tail_len == 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tail_len == 8) { |
5 |
1 |
6 |
|
| 117 |
5 |
| srl_siphash_compress(ctx, srl_siphash_load_le64(ctx->tail)); |
| 118 |
5 |
| ctx->tail_len = 0; |
| 119 |
|
| } |
| 120 |
|
| } |
| 121 |
|
| |
| 122 |
60 |
▶ | while (len - i >= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len - i >= 8) { |
36 |
24 |
60 |
|
| 123 |
36 |
| srl_siphash_compress(ctx, srl_siphash_load_le64(data + i)); |
| 124 |
36 |
| i += 8; |
| 125 |
|
| } |
| 126 |
80 |
▶ | while (i < len) |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < len) |
56 |
24 |
80 |
|
| 127 |
56 |
| ctx->tail[ctx->tail_len++] = data[i++]; |
| 128 |
24 |
| } |
| 129 |
|
| |
| 130 |
|
| SRL_STATIC_INLINE srl_u64 |
| 131 |
0 |
| srl_siphash_final64(srl_siphash_ctx *ctx) |
| 132 |
|
| { |
| 133 |
0 |
| srl_u64 b = (ctx->total & (srl_u64)0xff) << 56; |
| 134 |
|
| unsigned int i; |
| 135 |
0 |
▶ | for (i = 0; i < ctx->tail_len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ctx->tail_len; i++) |
0 |
0 |
0 |
|
| 136 |
0 |
| b |= (srl_u64)ctx->tail[i] << (8 * i); |
| 137 |
0 |
| srl_siphash_compress(ctx, b); |
| 138 |
0 |
| ctx->v2 ^= (srl_u64)0xff; |
| 139 |
0 |
▶ | for (i = 0; i < ctx->finalization_rounds; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ctx->finalization_rounds; i++) |
0 |
0 |
0 |
|
| 140 |
0 |
| SRL_SIPHASH_ROUND(ctx); |
| 141 |
0 |
| return ctx->v0 ^ ctx->v1 ^ ctx->v2 ^ ctx->v3; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
|
| SRL_STATIC_INLINE void |
| 145 |
12 |
| srl_siphash_final128(srl_siphash_ctx *ctx, U8 out[16]) |
| 146 |
|
| { |
| 147 |
12 |
| srl_u64 b = (ctx->total & (srl_u64)0xff) << 56; |
| 148 |
|
| srl_u64 first, second; |
| 149 |
|
| unsigned int i; |
| 150 |
33 |
▶ | for (i = 0; i < ctx->tail_len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ctx->tail_len; i++) |
21 |
12 |
33 |
|
| 151 |
21 |
| b |= (srl_u64)ctx->tail[i] << (8 * i); |
| 152 |
12 |
| srl_siphash_compress(ctx, b); |
| 153 |
12 |
| ctx->v2 ^= (srl_u64)0xee; |
| 154 |
60 |
▶ | for (i = 0; i < ctx->finalization_rounds; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ctx->finalization_rounds; i++) |
48 |
12 |
60 |
|
| 155 |
48 |
| SRL_SIPHASH_ROUND(ctx); |
| 156 |
12 |
| first = ctx->v0 ^ ctx->v1 ^ ctx->v2 ^ ctx->v3; |
| 157 |
12 |
| ctx->v1 ^= (srl_u64)0xdd; |
| 158 |
60 |
▶ | for (i = 0; i < ctx->finalization_rounds; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ctx->finalization_rounds; i++) |
48 |
12 |
60 |
|
| 159 |
48 |
| SRL_SIPHASH_ROUND(ctx); |
| 160 |
12 |
| second = ctx->v0 ^ ctx->v1 ^ ctx->v2 ^ ctx->v3; |
| 161 |
108 |
▶ | for (i = 0; i < 8; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) { |
96 |
12 |
108 |
|
| 162 |
96 |
| out[i] = (U8)(first >> (8 * i)); |
| 163 |
96 |
| out[i + 8] = (U8)(second >> (8 * i)); |
| 164 |
|
| } |
| 165 |
12 |
| } |
| 166 |
|
| |
| 167 |
|
| SRL_STATIC_INLINE void |
| 168 |
6 |
| srl_siphash_prepare_key(const U8 *key, size_t key_len, |
| 169 |
|
| const U8 *opt_key, size_t opt_key_len, U8 out[16]) |
| 170 |
|
| { |
| 171 |
|
| static const U8 initial_key[] = "Call me Ishmael."; |
| 172 |
|
| static const U8 first_prefix[] = "Out damned spot!"; |
| 173 |
|
| static const U8 second_prefix[] = "Luke, I am your father."; |
| 174 |
|
| U8 intermediate[16]; |
| 175 |
|
| srl_siphash_ctx ctx; |
| 176 |
|
| |
| 177 |
6 |
| srl_siphash_init_rounds(&ctx, initial_key, 2, 4, 1); |
| 178 |
6 |
| srl_siphash_update(&ctx, first_prefix, sizeof(first_prefix) - 1); |
| 179 |
6 |
| srl_siphash_update(&ctx, opt_key, opt_key_len); |
| 180 |
6 |
| srl_siphash_final128(&ctx, intermediate); |
| 181 |
|
| |
| 182 |
6 |
| srl_siphash_init_rounds(&ctx, intermediate, 2, 4, 1); |
| 183 |
6 |
| srl_siphash_update(&ctx, second_prefix, sizeof(second_prefix) - 1); |
| 184 |
6 |
| srl_siphash_update(&ctx, key, key_len); |
| 185 |
6 |
| srl_siphash_final128(&ctx, out); |
| 186 |
6 |
| } |
| 187 |
|
| |
| 188 |
|
| #undef SRL_SIPHASH_ROUND |
| 189 |
|
| |
| 190 |
|
| #endif /* SRL_SIPHASH_H */ |