src/xchacha20poly1305.c

filter: Statementstmt 100% 210 / 210 Branchbran 100% 50 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 260 / 260 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/symprim.c ↑ Summary xs/aead.xs »
1 #include "symprim/symprim.h"
2 #include "symprim/xchacha20poly1305.h"
3 #include "symprim/wipe.h"
4  
5 #include <string.h>
6  
7 16621768 static uint32_t load32(const uint8_t *p)
8 {
9 16621768 return (uint32_t)p[0] | ((uint32_t)p[1] << 8)
10 16621768 | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
11 }
12  
13 8465968 static void store32(uint8_t *p, uint32_t v)
14 {
15 8465968 p[0] = (uint8_t)v;
16 8465968 p[1] = (uint8_t)(v >> 8);
17 8465968 p[2] = (uint8_t)(v >> 16);
18 8465968 p[3] = (uint8_t)(v >> 24);
19 8465968 }
20  
21 3636 static void store64(uint8_t *p, uint64_t v)
22 {
23 int i;
24 32724 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 29088 3636 32724
25 29088 p[i] = (uint8_t)(v >> (8 * i));
26 3636 }
27  
28 /* ---- ChaCha20 ---------------------------------------------------------- */
29  
30 #define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
31  
32 #define QUARTER(a, b, c, d) \
33 do { \
34 a += b; d ^= a; d = ROTL(d, 16); \
35 c += d; b ^= c; b = ROTL(b, 12); \
36 a += b; d ^= a; d = ROTL(d, 8); \
37 c += d; b ^= c; b = ROTL(b, 7); \
38 } while (0)
39  
40 528653 static void rounds(uint32_t x[16])
41 {
42 int i;
43 5815183 ▶ for (i = 0; i < 10; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 10; i++) { 5286530 528653 5815183
44 5286530 QUARTER(x[0], x[4], x[8], x[12]);
45 5286530 QUARTER(x[1], x[5], x[9], x[13]);
46 5286530 QUARTER(x[2], x[6], x[10], x[14]);
47 5286530 QUARTER(x[3], x[7], x[11], x[15]);
48 5286530 QUARTER(x[0], x[5], x[10], x[15]);
49 5286530 QUARTER(x[1], x[6], x[11], x[12]);
50 5286530 QUARTER(x[2], x[7], x[8], x[13]);
51 5286530 QUARTER(x[3], x[4], x[9], x[14]);
52 }
53 528653 }
54  
55 /* "expand 32-byte k", then the key, then four words the caller fills. */
56 3964 static void key_setup(uint32_t state[16], const uint8_t key[32])
57 {
58 int i;
59 3964 state[0] = 0x61707865;
60 3964 state[1] = 0x3320646e;
61 3964 state[2] = 0x79622d32;
62 3964 state[3] = 0x6b206574;
63 35676 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 31712 3964 35676
64 31712 state[4 + i] = load32(key + 4 * i);
65 3964 }
66  
67 526894 static void block(uint8_t out[64], const uint32_t state[16])
68 {
69 uint32_t x[16];
70 int i;
71  
72 526894 memcpy(x, state, sizeof x);
73 526894 rounds(x);
74 8957198 ▶ for (i = 0; i < 16; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) 8430304 526894 8957198
75 8430304 store32(out + 4 * i, x[i] + state[i]);
76 526894 symprim_wipe(x, sizeof x);
77 526894 }
78  
79 /* The counter is 32 bits and is never allowed to wrap into the nonce: a
80 * length that would need more blocks than are left is refused. */
81 2207 int symprim_chacha20(uint8_t *out, const uint8_t *in, size_t len,
82 const uint8_t key[32], const uint8_t nonce[12], uint32_t counter)
83 {
84 uint32_t state[16];
85 uint8_t stream[64];
86 /* Counted so that no length wraps: adding 63 first would make a length
87 * within 63 of the largest one look like less than a block. */
88 2207 uint64_t blocks = (uint64_t)len / 64 + ((uint64_t)len % 64 != 0);
89 size_t i, n;
90  
91 2207 ▶ if (blocks > UINT64_C(0x100000000) - counter)
Branch: 1 branch
BranchTrueFalseTotal
if (blocks > UINT64_C(0x100000000) - counter) 2 2205 2207
92 2 return SYMPRIM_E_ARGUMENT;
93  
94 2205 key_setup(state, key);
95 2205 state[12] = counter;
96 2205 state[13] = load32(nonce);
97 2205 state[14] = load32(nonce + 4);
98 2205 state[15] = load32(nonce + 8);
99  
100 529099 ▶ while (len) {
Branch: 1 branch
BranchTrueFalseTotal
while (len) { 526894 2205 529099
101 526894 block(stream, state);
102 526894 state[12]++;
103 526894 n = len < 64 ? len : 64;
104 34176167 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 33649273 526894 34176167
105 33649273 out[i] = in[i] ^ stream[i];
106 526894 out += n;
107 526894 in += n;
108 526894 len -= n;
109 }
110 2205 symprim_wipe(state, sizeof state);
111 2205 symprim_wipe(stream, sizeof stream);
112 2205 return SYMPRIM_OK;
113 }
114  
115 /* The rounds with no final addition: the first and last rows are the key
116 * XChaCha20 then runs ChaCha20 under. */
117 1759 void symprim_hchacha20(uint8_t out[32], const uint8_t key[32], const uint8_t nonce[16])
118 {
119 uint32_t x[16];
120 int i;
121  
122 1759 key_setup(x, key);
123 8795 ▶ for (i = 0; i < 4; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) 7036 1759 8795
124 7036 x[12 + i] = load32(nonce + 4 * i);
125 1759 rounds(x);
126 8795 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 7036 1759 8795
127 7036 store32(out + 4 * i, x[i]);
128 7036 store32(out + 16 + 4 * i, x[12 + i]);
129 }
130 1759 symprim_wipe(x, sizeof x);
131 1759 }
132  
133 /* ---- Poly1305 ---------------------------------------------------------- */
134  
135 /* Five limbs of 26 bits, so a product of two fits 64 bits with room for the
136 * carries and no 128 bit type is needed. */
137  
138 #define LIMB 0x3ffffff
139  
140 5400 void symprim_poly1305_init(symprim_poly1305_state *s, const uint8_t key[32])
141 {
142 int i;
143  
144 5400 s->r[0] = load32(key) & 0x3ffffff;
145 5400 s->r[1] = (load32(key + 3) >> 2) & 0x3ffff03;
146 5400 s->r[2] = (load32(key + 6) >> 4) & 0x3ffc0ff;
147 5400 s->r[3] = (load32(key + 9) >> 6) & 0x3f03fff;
148 5400 s->r[4] = (load32(key + 12) >> 8) & 0x00fffff;
149 32400 ▶ for (i = 0; i < 5; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) 27000 5400 32400
150 27000 s->h[i] = 0;
151 27000 ▶ for (i = 0; i < 4; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) 21600 5400 27000
152 21600 s->pad[i] = load32(key + 16 + 4 * i);
153 5400 s->buflen = 0;
154 5400 }
155  
156 /* One 16 byte block. high is the 2^128 bit every full block carries, and 0
157 * for the padded last one, which carries its own 1 byte. */
158 3305561 static void absorb(symprim_poly1305_state *s, const uint8_t *m, uint32_t high)
159 {
160 3305561 uint32_t r0 = s->r[0], r1 = s->r[1], r2 = s->r[2], r3 = s->r[3], r4 = s->r[4];
161 3305561 uint32_t s1 = r1 * 5, s2 = r2 * 5, s3 = r3 * 5, s4 = r4 * 5;
162 3305561 uint32_t h0 = s->h[0], h1 = s->h[1], h2 = s->h[2], h3 = s->h[3], h4 = s->h[4];
163 uint64_t d0, d1, d2, d3, d4;
164 uint32_t c;
165  
166 3305561 h0 += load32(m) & LIMB;
167 3305561 h1 += (load32(m + 3) >> 2) & LIMB;
168 3305561 h2 += (load32(m + 6) >> 4) & LIMB;
169 3305561 h3 += (load32(m + 9) >> 6) & LIMB;
170 3305561 h4 += (load32(m + 12) >> 8) | high;
171  
172 3305561 d0 = (uint64_t)h0 * r0 + (uint64_t)h1 * s4 + (uint64_t)h2 * s3 + (uint64_t)h3 * s2 + (uint64_t)h4 * s1;
173 3305561 d1 = (uint64_t)h0 * r1 + (uint64_t)h1 * r0 + (uint64_t)h2 * s4 + (uint64_t)h3 * s3 + (uint64_t)h4 * s2;
174 3305561 d2 = (uint64_t)h0 * r2 + (uint64_t)h1 * r1 + (uint64_t)h2 * r0 + (uint64_t)h3 * s4 + (uint64_t)h4 * s3;
175 3305561 d3 = (uint64_t)h0 * r3 + (uint64_t)h1 * r2 + (uint64_t)h2 * r1 + (uint64_t)h3 * r0 + (uint64_t)h4 * s4;
176 3305561 d4 = (uint64_t)h0 * r4 + (uint64_t)h1 * r3 + (uint64_t)h2 * r2 + (uint64_t)h3 * r1 + (uint64_t)h4 * r0;
177  
178 3305561 c = (uint32_t)(d0 >> 26); h0 = (uint32_t)d0 & LIMB; d1 += c;
179 3305561 c = (uint32_t)(d1 >> 26); h1 = (uint32_t)d1 & LIMB; d2 += c;
180 3305561 c = (uint32_t)(d2 >> 26); h2 = (uint32_t)d2 & LIMB; d3 += c;
181 3305561 c = (uint32_t)(d3 >> 26); h3 = (uint32_t)d3 & LIMB; d4 += c;
182 3305561 c = (uint32_t)(d4 >> 26); h4 = (uint32_t)d4 & LIMB; h0 += c * 5;
183 3305561 c = h0 >> 26; h0 &= LIMB; h1 += c;
184  
185 3305561 s->h[0] = h0; s->h[1] = h1; s->h[2] = h2; s->h[3] = h3; s->h[4] = h4;
186 3305561 }
187  
188 15975 void symprim_poly1305_update(symprim_poly1305_state *s, const uint8_t *in, size_t len)
189 {
190 size_t room;
191  
192 15975 ▶ if (s->buflen) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->buflen) { 6802 9173 15975
193 6802 room = 16 - s->buflen;
194 6802 ▶ if (room > len)
Branch: 1 branch
BranchTrueFalseTotal
if (room > len) 103 6699 6802
195 103 room = len;
196 6802 memcpy(s->buf + s->buflen, in, room);
197 6802 s->buflen += room;
198 6802 in += room;
199 6802 len -= room;
200 6802 ▶ if (s->buflen < 16)
Branch: 1 branch
BranchTrueFalseTotal
if (s->buflen < 16) 103 6699 6802
201 103 return;
202 6699 absorb(s, s->buf, 1UL << 24);
203 6699 s->buflen = 0;
204 }
205 3312916 ▶ while (len >= 16) {
Branch: 1 branch
BranchTrueFalseTotal
while (len >= 16) { 3297044 15872 3312916
206 3297044 absorb(s, in, 1UL << 24);
207 3297044 in += 16;
208 3297044 len -= 16;
209 }
210 15872 ▶ if (len) {
Branch: 1 branch
BranchTrueFalseTotal
if (len) { 8517 7355 15872
211 8517 memcpy(s->buf, in, len);
212 8517 s->buflen = len;
213 }
214 }
215  
216 5398 void symprim_poly1305_final(symprim_poly1305_state *s, uint8_t tag[16])
217 {
218 uint32_t h0, h1, h2, h3, h4, g0, g1, g2, g3, g4, c, mask;
219 uint64_t f;
220  
221 5398 ▶ if (s->buflen) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->buflen) { 1818 3580 5398
222 1818 s->buf[s->buflen++] = 1;
223 15192 ▶ while (s->buflen < 16)
Branch: 1 branch
BranchTrueFalseTotal
while (s->buflen < 16) 13374 1818 15192
224 13374 s->buf[s->buflen++] = 0;
225 1818 absorb(s, s->buf, 0);
226 }
227 5398 h0 = s->h[0]; h1 = s->h[1]; h2 = s->h[2]; h3 = s->h[3]; h4 = s->h[4];
228  
229 5398 c = h1 >> 26; h1 &= LIMB; h2 += c;
230 5398 c = h2 >> 26; h2 &= LIMB; h3 += c;
231 5398 c = h3 >> 26; h3 &= LIMB; h4 += c;
232 5398 c = h4 >> 26; h4 &= LIMB; h0 += c * 5;
233 5398 c = h0 >> 26; h0 &= LIMB; h1 += c;
234  
235 /* h is below 2^130 but may still be p or more. g is h - p. Which one is
236 * kept is chosen by a mask, not a branch: the choice depends on the key. */
237 5398 g0 = h0 + 5; c = g0 >> 26; g0 &= LIMB;
238 5398 g1 = h1 + c; c = g1 >> 26; g1 &= LIMB;
239 5398 g2 = h2 + c; c = g2 >> 26; g2 &= LIMB;
240 5398 g3 = h3 + c; c = g3 >> 26; g3 &= LIMB;
241 5398 g4 = h4 + c - (1UL << 26);
242  
243 5398 mask = (g4 >> 31) - 1;
244 5398 g0 &= mask; g1 &= mask; g2 &= mask; g3 &= mask; g4 &= mask;
245 5398 mask = ~mask;
246 5398 h0 = (h0 & mask) | g0;
247 5398 h1 = (h1 & mask) | g1;
248 5398 h2 = (h2 & mask) | g2;
249 5398 h3 = (h3 & mask) | g3;
250 5398 h4 = (h4 & mask) | g4;
251  
252 5398 h0 = h0 | (h1 << 26);
253 5398 h1 = (h1 >> 6) | (h2 << 20);
254 5398 h2 = (h2 >> 12) | (h3 << 14);
255 5398 h3 = (h3 >> 18) | (h4 << 8);
256  
257 5398 f = (uint64_t)h0 + s->pad[0]; store32(tag, (uint32_t)f);
258 5398 f = (uint64_t)h1 + s->pad[1] + (f >> 32); store32(tag + 4, (uint32_t)f);
259 5398 f = (uint64_t)h2 + s->pad[2] + (f >> 32); store32(tag + 8, (uint32_t)f);
260 5398 f = (uint64_t)h3 + s->pad[3] + (f >> 32); store32(tag + 12, (uint32_t)f);
261  
262 5398 symprim_wipe(s, sizeof *s);
263 5398 }
264  
265 /* ---- The AEAD of RFC 8439 --------------------------------------------- */
266  
267 /* The tag covers the associated data and the ciphertext, each padded with
268 * zeros to 16 bytes, then both lengths. The Poly1305 key is the first 32
269 * bytes of keystream block 0, so encryption starts at block 1. */
270 1818 static void aead_tag(uint8_t tag[16], const uint8_t *cipher, size_t len,
271 const uint8_t *ad, size_t ad_len,
272 const uint8_t nonce[12], const uint8_t key[32])
273 {
274 static const uint8_t zeros[16] = { 0 };
275 symprim_poly1305_state mac;
276 uint8_t one_time[32], lengths[16];
277  
278 1818 memset(one_time, 0, sizeof one_time);
279 1818 symprim_chacha20(one_time, one_time, sizeof one_time, key, nonce, 0);
280 1818 symprim_poly1305_init(&mac, one_time);
281 1818 ▶ if (ad_len)
Branch: 1 branch
BranchTrueFalseTotal
if (ad_len) 1745 73 1818
282 1745 symprim_poly1305_update(&mac, ad, ad_len);
283 1818 ▶ if (ad_len % 16)
Branch: 1 branch
BranchTrueFalseTotal
if (ad_len % 16) 1714 104 1818
284 1714 symprim_poly1305_update(&mac, zeros, 16 - ad_len % 16);
285 1818 ▶ if (len)
Branch: 1 branch
BranchTrueFalseTotal
if (len) 1788 30 1818
286 1788 symprim_poly1305_update(&mac, cipher, len);
287 1818 ▶ if (len % 16)
Branch: 1 branch
BranchTrueFalseTotal
if (len % 16) 1753 65 1818
288 1753 symprim_poly1305_update(&mac, zeros, 16 - len % 16);
289 1818 store64(lengths, (uint64_t)ad_len);
290 1818 store64(lengths + 8, (uint64_t)len);
291 1818 symprim_poly1305_update(&mac, lengths, sizeof lengths);
292 1818 symprim_poly1305_final(&mac, tag);
293 1818 symprim_wipe(one_time, sizeof one_time);
294 1818 }
295  
296 /* One nonce carries 2**32 - 1 blocks of 64 bytes, the counter starting at 1.
297 * Compared as bytes, so that no length wraps on its way to being blocks. */
298 1826 static int fits(size_t len)
299 {
300 1826 return (uint64_t)len <= UINT64_C(0xFFFFFFFF) * 64;
301 }
302  
303 267 int symprim_chacha20poly1305_seal(uint8_t *out, uint8_t tag[16],
304 const uint8_t *in, size_t len,
305 const uint8_t *ad, size_t ad_len,
306 const uint8_t nonce[12], const uint8_t key[32])
307 {
308 267 ▶ if (!fits(len))
Branch: 1 branch
BranchTrueFalseTotal
if (!fits(len)) 4 263 267
309 4 return SYMPRIM_E_ARGUMENT;
310 263 symprim_chacha20(out, in, len, key, nonce, 1);
311 263 aead_tag(tag, out, len, ad, ad_len, nonce, key);
312 263 return SYMPRIM_OK;
313 }
314  
315 1559 int symprim_chacha20poly1305_open(uint8_t *out,
316 const uint8_t *in, size_t len, const uint8_t tag[16],
317 const uint8_t *ad, size_t ad_len,
318 const uint8_t nonce[12], const uint8_t key[32])
319 {
320 uint8_t want[16];
321 int same;
322  
323 1559 ▶ if (!fits(len))
Branch: 1 branch
BranchTrueFalseTotal
if (!fits(len)) 4 1555 1559
324 4 return SYMPRIM_E_ARGUMENT;
325 1555 aead_tag(want, in, len, ad, ad_len, nonce, key);
326 1555 same = symprim_equal(want, tag, sizeof want);
327 1555 symprim_wipe(want, sizeof want);
328 1555 ▶ if (!same)
Branch: 1 branch
BranchTrueFalseTotal
if (!same) 1486 69 1555
329 1486 return SYMPRIM_E_AUTH;
330 69 symprim_chacha20(out, in, len, key, nonce, 1);
331 69 return SYMPRIM_OK;
332 }
333  
334 /* ---- XChaCha20-Poly1305 ------------------------------------------------ */
335  
336 /* The first 16 nonce bytes and the key make a subkey. The last 8, behind
337 * four zero bytes, are the nonce the RFC 8439 AEAD runs with under it. */
338 1715 static void extend(uint8_t subkey[32], uint8_t short_nonce[12],
339 const uint8_t nonce[24], const uint8_t key[32])
340 {
341 1715 symprim_hchacha20(subkey, key, nonce);
342 1715 memset(short_nonce, 0, 4);
343 1715 memcpy(short_nonce + 4, nonce + 16, 8);
344 1715 }
345  
346 156 int symprim_aead_seal(uint8_t *out, uint8_t tag[SYMPRIM_AEAD_TAG],
347 const uint8_t *in, size_t len,
348 const uint8_t *ad, size_t ad_len,
349 const uint8_t nonce[SYMPRIM_AEAD_NONCE],
350 const uint8_t key[SYMPRIM_AEAD_KEY])
351 {
352 uint8_t subkey[32], short_nonce[12];
353 int code;
354  
355 156 extend(subkey, short_nonce, nonce, key);
356 156 code = symprim_chacha20poly1305_seal(out, tag, in, len, ad, ad_len, short_nonce, subkey);
357 156 symprim_wipe(subkey, sizeof subkey);
358 156 return code;
359 }
360  
361 1559 int symprim_aead_open(uint8_t *out,
362 const uint8_t *in, size_t len,
363 const uint8_t tag[SYMPRIM_AEAD_TAG],
364 const uint8_t *ad, size_t ad_len,
365 const uint8_t nonce[SYMPRIM_AEAD_NONCE],
366 const uint8_t key[SYMPRIM_AEAD_KEY])
367 {
368 uint8_t subkey[32], short_nonce[12];
369 int code;
370  
371 1559 extend(subkey, short_nonce, nonce, key);
372 1559 code = symprim_chacha20poly1305_open(out, in, len, tag, ad, ad_len, short_nonce, subkey);
373 1559 symprim_wipe(subkey, sizeof subkey);
374 1559 return code;
375 }
« src/symprim.c ↑ Summary xs/aead.xs »