srl_siphash.h

filter: Statementstmt 81.0% 64 / 79 Branchbran 75.0% 21 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.4% 85 / 107 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« ptable.h ↑ Summary blib/lib/Sereal/Decoder/Constants.pm »
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 690 srl_siphash_rotl64(srl_u64 x, unsigned int b)
29 {
30 690 return (x << b) | (x >> (64 - b));
31 }
32  
33 SRL_STATIC_INLINE srl_u64
34 40 srl_siphash_load_le64(const U8 *p)
35 {
36 40 return ((srl_u64)p[0]) | ((srl_u64)p[1] << 8) |
37 40 ((srl_u64)p[2] << 16) | ((srl_u64)p[3] << 24) |
38 40 ((srl_u64)p[4] << 32) | ((srl_u64)p[5] << 40) |
39 40 ((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 35 srl_siphash_compress(srl_siphash_ctx *ctx, srl_u64 m)
55 {
56 unsigned int i;
57 35 ctx->v3 ^= m;
58 110 ▶ for (i = 0; i < ctx->compression_rounds; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->compression_rounds; i++) 75 35 110
59 75 SRL_SIPHASH_ROUND(ctx);
60 35 ctx->v0 ^= m;
61 35 }
62  
63 SRL_STATIC_INLINE void
64 5 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 5 srl_u64 k0 = srl_siphash_load_le64(key);
69 5 srl_u64 k1 = srl_siphash_load_le64(key + 8);
70 5 ctx->v0 = ((srl_u64)0x736f6d6570736575ULL) ^ k0;
71 5 ctx->v1 = ((srl_u64)0x646f72616e646f6dULL) ^ k1;
72 5 ctx->v2 = ((srl_u64)0x6c7967656e657261ULL) ^ k0;
73 5 ctx->v3 = ((srl_u64)0x7465646279746573ULL) ^ k1;
74 5 ▶ if (is_128_bit)
Branch: 1 branch
BranchTrueFalseTotal
if (is_128_bit) 4 1 5
75 4 ctx->v1 ^= (srl_u64)0xee;
76 5 ctx->total = 0;
77 5 ctx->tail_len = 0;
78 5 ctx->compression_rounds = compression_rounds;
79 5 ctx->finalization_rounds = finalization_rounds;
80 5 ctx->is_128_bit = (U8)(is_128_bit != 0);
81 5 }
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 5 srl_siphash_update(srl_siphash_ctx *ctx, const U8 *data, size_t len)
109 {
110 5 size_t i = 0;
111 5 ctx->total += (srl_u64)len;
112  
113 5 ▶ if (ctx->tail_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->tail_len) { 0 5 5
114 0 ▶ while (i < len && ctx->tail_len < 8)
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && ctx->tail_len < 8) 0 0 0
while (i < len && ctx->tail_len < 8) 0 0 0
115 0 ctx->tail[ctx->tail_len++] = data[i++];
116 0 ▶ if (ctx->tail_len == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->tail_len == 8) { 0 0 0
117 0 srl_siphash_compress(ctx, srl_siphash_load_le64(ctx->tail));
118 0 ctx->tail_len = 0;
119 }
120 }
121  
122 35 ▶ while (len - i >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
while (len - i >= 8) { 30 5 35
123 30 srl_siphash_compress(ctx, srl_siphash_load_le64(data + i));
124 30 i += 8;
125 }
126 19 ▶ while (i < len)
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) 14 5 19
127 14 ctx->tail[ctx->tail_len++] = data[i++];
128 5 }
129  
130 SRL_STATIC_INLINE srl_u64
131 1 srl_siphash_final64(srl_siphash_ctx *ctx)
132 {
133 1 srl_u64 b = (ctx->total & (srl_u64)0xff) << 56;
134 unsigned int i;
135 5 ▶ for (i = 0; i < ctx->tail_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->tail_len; i++) 4 1 5
136 4 b |= (srl_u64)ctx->tail[i] << (8 * i);
137 1 srl_siphash_compress(ctx, b);
138 1 ctx->v2 ^= (srl_u64)0xff;
139 5 ▶ for (i = 0; i < ctx->finalization_rounds; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->finalization_rounds; i++) 4 1 5
140 4 SRL_SIPHASH_ROUND(ctx);
141 1 return ctx->v0 ^ ctx->v1 ^ ctx->v2 ^ ctx->v3;
142 }
143  
144 SRL_STATIC_INLINE void
145 4 srl_siphash_final128(srl_siphash_ctx *ctx, U8 out[16])
146 {
147 4 srl_u64 b = (ctx->total & (srl_u64)0xff) << 56;
148 srl_u64 first, second;
149 unsigned int i;
150 14 ▶ for (i = 0; i < ctx->tail_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->tail_len; i++) 10 4 14
151 10 b |= (srl_u64)ctx->tail[i] << (8 * i);
152 4 srl_siphash_compress(ctx, b);
153 4 ctx->v2 ^= (srl_u64)0xee;
154 22 ▶ for (i = 0; i < ctx->finalization_rounds; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->finalization_rounds; i++) 18 4 22
155 18 SRL_SIPHASH_ROUND(ctx);
156 4 first = ctx->v0 ^ ctx->v1 ^ ctx->v2 ^ ctx->v3;
157 4 ctx->v1 ^= (srl_u64)0xdd;
158 22 ▶ for (i = 0; i < ctx->finalization_rounds; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->finalization_rounds; i++) 18 4 22
159 18 SRL_SIPHASH_ROUND(ctx);
160 4 second = ctx->v0 ^ ctx->v1 ^ ctx->v2 ^ ctx->v3;
161 36 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 32 4 36
162 32 out[i] = (U8)(first >> (8 * i));
163 32 out[i + 8] = (U8)(second >> (8 * i));
164 }
165 4 }
166  
167 SRL_STATIC_INLINE void
168 0 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 0 srl_siphash_init_rounds(&ctx, initial_key, 2, 4, 1);
178 0 srl_siphash_update(&ctx, first_prefix, sizeof(first_prefix) - 1);
179 0 srl_siphash_update(&ctx, opt_key, opt_key_len);
180 0 srl_siphash_final128(&ctx, intermediate);
181  
182 0 srl_siphash_init_rounds(&ctx, intermediate, 2, 4, 1);
183 0 srl_siphash_update(&ctx, second_prefix, sizeof(second_prefix) - 1);
184 0 srl_siphash_update(&ctx, key, key_len);
185 0 srl_siphash_final128(&ctx, out);
186 0 }
187  
188 #undef SRL_SIPHASH_ROUND
189  
190 #endif /* SRL_SIPHASH_H */
« ptable.h ↑ Summary blib/lib/Sereal/Decoder/Constants.pm »