]> pd.if.org Git - zpackage/blob - crypto/chacha.c
remove stray debug fprintf
[zpackage] / crypto / chacha.c
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "chacha.h"
6
7 typedef unsigned char u8;
8 typedef unsigned int u32;
9
10 typedef struct chacha_ctx chacha_ctx;
11
12 #define U8C(v) (v##U)
13 #define U32C(v) (v##U)
14
15 #define U8V(v) ((u8)(v) & U8C(0xFF))
16 #define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF))
17
18 #define ROTL32(v, n) \
19   (U32V((v) << (n)) | ((v) >> (32 - (n))))
20
21 #define U8TO32_LITTLE(p) \
22   (((u32)((p)[0])) | \
23    ((u32)((p)[1]) <<  8) | \
24    ((u32)((p)[2]) << 16) | \
25    ((u32)((p)[3]) << 24))
26
27 #define U32TO8_LITTLE(p, v) \
28   do { \
29     (p)[0] = U8V((v)); \
30     (p)[1] = U8V((v) >>  8); \
31     (p)[2] = U8V((v) >> 16); \
32     (p)[3] = U8V((v) >> 24); \
33   } while (0)
34
35 #define ROTATE(v,c) (ROTL32(v,c))
36 #define XOR(v,w) ((v) ^ (w))
37 #define PLUS(v,w) (U32V((v) + (w)))
38 #define PLUSONE(v) (PLUS((v),1))
39
40 #define QUARTERROUND(a,b,c,d) \
41   a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
42   c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
43   a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
44   c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
45
46 static const char sigma[] = "expand 32-byte k";
47 static const char tau[] = "expand 16-byte k";
48
49 void chacha_keysetup(chacha_ctx * x, const u8 * k, u32 kbits) {
50         const char *constants;
51
52         x->input[4] = U8TO32_LITTLE(k + 0);
53         x->input[5] = U8TO32_LITTLE(k + 4);
54         x->input[6] = U8TO32_LITTLE(k + 8);
55         x->input[7] = U8TO32_LITTLE(k + 12);
56         if (kbits == 256) {     /* recommended */
57                 k += 16;
58                 constants = sigma;
59         } else {                /* kbits == 128 */
60                 constants = tau;
61         }
62         x->input[8] = U8TO32_LITTLE(k + 0);
63         x->input[9] = U8TO32_LITTLE(k + 4);
64         x->input[10] = U8TO32_LITTLE(k + 8);
65         x->input[11] = U8TO32_LITTLE(k + 12);
66         x->input[0] = U8TO32_LITTLE(constants + 0);
67         x->input[1] = U8TO32_LITTLE(constants + 4);
68         x->input[2] = U8TO32_LITTLE(constants + 8);
69         x->input[3] = U8TO32_LITTLE(constants + 12);
70 }
71
72 void chacha_key(chacha_ctx * x, u8 * k) {
73         U32TO8_LITTLE(k, x->input[4]);
74         U32TO8_LITTLE(k + 4, x->input[5]);
75         U32TO8_LITTLE(k + 8, x->input[6]);
76         U32TO8_LITTLE(k + 12, x->input[7]);
77
78         U32TO8_LITTLE(k + 16, x->input[8]);
79         U32TO8_LITTLE(k + 20, x->input[9]);
80         U32TO8_LITTLE(k + 24, x->input[10]);
81         U32TO8_LITTLE(k + 28, x->input[11]);
82 }
83
84 void chacha_nonce(chacha_ctx * x, u8 * nonce) {
85         U32TO8_LITTLE(nonce + 0, x->input[13]);
86         U32TO8_LITTLE(nonce + 4, x->input[14]);
87         U32TO8_LITTLE(nonce + 8, x->input[15]);
88 }
89
90 void chacha_ivsetup(chacha_ctx * x, const u8 * iv, const u8 * counter) {
91         x->input[12] =
92             counter == NULL ? 0 : U8TO32_LITTLE(counter + 0);
93         x->input[13] =
94             counter == NULL ? 0 : U8TO32_LITTLE(counter + 4);
95         if (iv) {
96                 x->input[14] = U8TO32_LITTLE(iv + 0);
97                 x->input[15] = U8TO32_LITTLE(iv + 4);
98         }
99 }
100
101 void chacha_ivsetup_96bitnonce(chacha_ctx * x, const u8 * iv,
102                                const u8 * counter) {
103         x->input[12] =
104             counter == NULL ? 0 : U8TO32_LITTLE(counter + 0);
105         if (iv) {
106                 x->input[13] = U8TO32_LITTLE(iv + 0);
107                 x->input[14] = U8TO32_LITTLE(iv + 4);
108                 x->input[15] = U8TO32_LITTLE(iv + 8);
109         }
110 }
111
112 void chacha_ivupdate(chacha_ctx * x, const uint8_t * iv,
113                      const uint8_t * aad, const uint8_t * counter) {
114         x->input[12] =
115             counter == NULL ? 0 : U8TO32_LITTLE(counter + 0);
116         x->input[13] = U8TO32_LITTLE(iv + 0);
117         x->input[14] =
118             U8TO32_LITTLE(iv +
119                                        4) ^
120             U8TO32_LITTLE(aad);
121         x->input[15] =
122             U8TO32_LITTLE(iv +
123                                        8) ^ U8TO32_LITTLE(aad
124                                                                        +
125                                                                        4);
126 }
127
128 void chacha_encrypt_bytes(chacha_ctx * x, const u8 * m, u8 * c, u32 bytes) {
129         u32 i;
130         u32 x0, x1, x2, x3, x4, x5, x6, x7;
131         u32 x8, x9, x10, x11, x12, x13, x14, x15;
132         u32 j0, j1, j2, j3, j4, j5, j6, j7;
133         u32 j8, j9, j10, j11, j12, j13, j14, j15;
134         u8 *ctarget = NULL;
135         u8 tmp[64];
136
137         if (!bytes) {
138                 return;
139         }
140
141         j0 = x->input[0];
142         j1 = x->input[1];
143         j2 = x->input[2];
144         j3 = x->input[3];
145         j4 = x->input[4];
146         j5 = x->input[5];
147         j6 = x->input[6];
148         j7 = x->input[7];
149         j8 = x->input[8];
150         j9 = x->input[9];
151         j10 = x->input[10];
152         j11 = x->input[11];
153         j12 = x->input[12];
154         j13 = x->input[13];
155         j14 = x->input[14];
156         j15 = x->input[15];
157
158         for (;;) {
159                 if (bytes < 64) {
160                         for (i = 0; i < bytes; ++i) {
161                                 tmp[i] = m[i];
162                         }
163                         m = tmp;
164                         ctarget = c;
165                         c = tmp;
166                 }
167                 x0 = j0;
168                 x1 = j1;
169                 x2 = j2;
170                 x3 = j3;
171                 x4 = j4;
172                 x5 = j5;
173                 x6 = j6;
174                 x7 = j7;
175                 x8 = j8;
176                 x9 = j9;
177                 x10 = j10;
178                 x11 = j11;
179                 x12 = j12;
180                 x13 = j13;
181                 x14 = j14;
182                 x15 = j15;
183                 for (i = 20; i > 0; i -= 2) {
184                         QUARTERROUND(x0, x4, x8, x12)
185                             QUARTERROUND(x1, x5, x9, x13)
186                             QUARTERROUND(x2, x6, x10, x14)
187                             QUARTERROUND(x3, x7, x11, x15)
188                             QUARTERROUND(x0, x5, x10, x15)
189                             QUARTERROUND(x1, x6, x11, x12)
190                             QUARTERROUND(x2, x7, x8, x13)
191                             QUARTERROUND(x3, x4, x9, x14)
192                 }
193                 x0 = PLUS(x0, j0);
194                 x1 = PLUS(x1, j1);
195                 x2 = PLUS(x2, j2);
196                 x3 = PLUS(x3, j3);
197                 x4 = PLUS(x4, j4);
198                 x5 = PLUS(x5, j5);
199                 x6 = PLUS(x6, j6);
200                 x7 = PLUS(x7, j7);
201                 x8 = PLUS(x8, j8);
202                 x9 = PLUS(x9, j9);
203                 x10 = PLUS(x10, j10);
204                 x11 = PLUS(x11, j11);
205                 x12 = PLUS(x12, j12);
206                 x13 = PLUS(x13, j13);
207                 x14 = PLUS(x14, j14);
208                 x15 = PLUS(x15, j15);
209
210                 if (bytes < 64) {
211                         U32TO8_LITTLE(x->ks + 0, x0);
212                         U32TO8_LITTLE(x->ks + 4, x1);
213                         U32TO8_LITTLE(x->ks + 8, x2);
214                         U32TO8_LITTLE(x->ks + 12, x3);
215                         U32TO8_LITTLE(x->ks + 16, x4);
216                         U32TO8_LITTLE(x->ks + 20, x5);
217                         U32TO8_LITTLE(x->ks + 24, x6);
218                         U32TO8_LITTLE(x->ks + 28, x7);
219                         U32TO8_LITTLE(x->ks + 32, x8);
220                         U32TO8_LITTLE(x->ks + 36, x9);
221                         U32TO8_LITTLE(x->ks + 40, x10);
222                         U32TO8_LITTLE(x->ks + 44, x11);
223                         U32TO8_LITTLE(x->ks + 48, x12);
224                         U32TO8_LITTLE(x->ks + 52, x13);
225                         U32TO8_LITTLE(x->ks + 56, x14);
226                         U32TO8_LITTLE(x->ks + 60, x15);
227                 }
228
229                 x0 = XOR(x0, U8TO32_LITTLE(m + 0));
230                 x1 = XOR(x1, U8TO32_LITTLE(m + 4));
231                 x2 = XOR(x2, U8TO32_LITTLE(m + 8));
232                 x3 = XOR(x3, U8TO32_LITTLE(m + 12));
233                 x4 = XOR(x4, U8TO32_LITTLE(m + 16));
234                 x5 = XOR(x5, U8TO32_LITTLE(m + 20));
235                 x6 = XOR(x6, U8TO32_LITTLE(m + 24));
236                 x7 = XOR(x7, U8TO32_LITTLE(m + 28));
237                 x8 = XOR(x8, U8TO32_LITTLE(m + 32));
238                 x9 = XOR(x9, U8TO32_LITTLE(m + 36));
239                 x10 = XOR(x10, U8TO32_LITTLE(m + 40));
240                 x11 = XOR(x11, U8TO32_LITTLE(m + 44));
241                 x12 = XOR(x12, U8TO32_LITTLE(m + 48));
242                 x13 = XOR(x13, U8TO32_LITTLE(m + 52));
243                 x14 = XOR(x14, U8TO32_LITTLE(m + 56));
244                 x15 = XOR(x15, U8TO32_LITTLE(m + 60));
245
246                 j12 = PLUSONE(j12);
247                 if (!j12) {
248                         j13 = PLUSONE(j13);
249                         /*
250                          * Stopping at 2^70 bytes per nonce is the user's
251                          * responsibility.
252                          */
253                 }
254
255                 U32TO8_LITTLE(c + 0, x0);
256                 U32TO8_LITTLE(c + 4, x1);
257                 U32TO8_LITTLE(c + 8, x2);
258                 U32TO8_LITTLE(c + 12, x3);
259                 U32TO8_LITTLE(c + 16, x4);
260                 U32TO8_LITTLE(c + 20, x5);
261                 U32TO8_LITTLE(c + 24, x6);
262                 U32TO8_LITTLE(c + 28, x7);
263                 U32TO8_LITTLE(c + 32, x8);
264                 U32TO8_LITTLE(c + 36, x9);
265                 U32TO8_LITTLE(c + 40, x10);
266                 U32TO8_LITTLE(c + 44, x11);
267                 U32TO8_LITTLE(c + 48, x12);
268                 U32TO8_LITTLE(c + 52, x13);
269                 U32TO8_LITTLE(c + 56, x14);
270                 U32TO8_LITTLE(c + 60, x15);
271
272                 if (bytes <= 64) {
273                         if (bytes < 64) {
274                                 for (i = 0; i < bytes; ++i)
275                                         ctarget[i] = c[i];
276                         }
277                         x->input[12] = j12;
278                         x->input[13] = j13;
279                         x->unused = 64 - bytes;
280                         return;
281                 }
282                 bytes -= 64;
283                 c += 64;
284                 m += 64;
285         }
286 }
287
288 void chacha20_block(chacha_ctx * x, unsigned char *c, int len) {
289         int i;
290
291         unsigned int state[16];
292         for (i = 0; i < 16; i++)
293                 state[i] = x->input[i];
294         for (i = 20; i > 0; i -= 2) {
295                 QUARTERROUND(state[0], state[4], state[8], state[12])
296                     QUARTERROUND(state[1], state[5], state[9], state[13])
297                     QUARTERROUND(state[2], state[6], state[10], state[14])
298                     QUARTERROUND(state[3], state[7], state[11], state[15])
299                     QUARTERROUND(state[0], state[5], state[10], state[15])
300                     QUARTERROUND(state[1], state[6], state[11], state[12])
301                     QUARTERROUND(state[2], state[7], state[8], state[13])
302                     QUARTERROUND(state[3], state[4], state[9], state[14])
303         }
304
305         for (i = 0; i < 16; i++)
306                 x->input[i] = PLUS(x->input[i], state[i]);
307
308         for (i = 0; i < len; i += 4) {
309                 U32TO8_LITTLE(c + i, x->input[i / 4]);
310         }
311 }
312
313 int poly1305_generate_key(unsigned char *key256, unsigned char *nonce,
314                           unsigned int noncelen, unsigned char *poly_key,
315                           unsigned int counter) {
316         struct chacha_ctx ctx;
317         uint64_t ctr;
318         memset(&ctx, 0, sizeof(ctx));
319         chacha_keysetup(&ctx, key256, 256);
320         switch (noncelen) {
321         case 8:
322                 ctr = counter;
323                 chacha_ivsetup(&ctx, nonce, (unsigned char *) &ctr);
324                 break;
325         case 12:
326                 chacha_ivsetup_96bitnonce(&ctx, nonce,
327                                           (unsigned char *) &counter);
328                 break;
329         default:
330                 return -1;
331         }
332         chacha20_block(&ctx, poly_key, POLY1305_KEYLEN);
333         return 0;
334 }
335
336 /* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in
337  * little endian */
338 static unsigned long U8TO32(const unsigned char *p) {
339         return
340             (((unsigned long) (p[0] & 0xff)) |
341              ((unsigned long) (p[1] & 0xff) << 8) |
342              ((unsigned long) (p[2] & 0xff) << 16) |
343              ((unsigned long) (p[3] & 0xff) << 24));
344 }
345
346 /* store a 32 bit unsigned integer as four 8 bit unsigned integers in little
347  * endian */
348 static void U32TO8(unsigned char *p, unsigned long v) {
349         p[0] = (v) & 0xff;
350         p[1] = (v >> 8) & 0xff;
351         p[2] = (v >> 16) & 0xff;
352         p[3] = (v >> 24) & 0xff;
353 }
354
355 void tls_poly1305_init(struct poly1305_context *ctx,
356                                 const unsigned char key[32]) {
357         struct poly1305_context *st = (struct poly1305_context *) ctx;
358
359         /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
360         st->r[0] = (U8TO32(&key[0])) & 0x3ffffff;
361         st->r[1] = (U8TO32(&key[3]) >> 2) & 0x3ffff03;
362         st->r[2] = (U8TO32(&key[6]) >> 4) & 0x3ffc0ff;
363         st->r[3] = (U8TO32(&key[9]) >> 6) & 0x3f03fff;
364         st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff;
365
366         /* h = 0 */
367         st->h[0] = 0;
368         st->h[1] = 0;
369         st->h[2] = 0;
370         st->h[3] = 0;
371         st->h[4] = 0;
372
373         /* save pad for later */
374         st->pad[0] = U8TO32(&key[16]);
375         st->pad[1] = U8TO32(&key[20]);
376         st->pad[2] = U8TO32(&key[24]);
377         st->pad[3] = U8TO32(&key[28]);
378
379         st->leftover = 0;
380         st->final = 0;
381 }
382
383 void tls_poly1305_blocks(struct poly1305_context *st, const unsigned char *m,
384                 size_t bytes) {
385         const unsigned long hibit = (st->final) ? 0 : (1UL << 24);      /* 1 << 128 */
386         unsigned long r0, r1, r2, r3, r4;
387         unsigned long s1, s2, s3, s4;
388         unsigned long h0, h1, h2, h3, h4;
389         unsigned long long d0, d1, d2, d3, d4;
390         unsigned long c;
391
392         r0 = st->r[0];
393         r1 = st->r[1];
394         r2 = st->r[2];
395         r3 = st->r[3];
396         r4 = st->r[4];
397
398         s1 = r1 * 5;
399         s2 = r2 * 5;
400         s3 = r3 * 5;
401         s4 = r4 * 5;
402
403         h0 = st->h[0];
404         h1 = st->h[1];
405         h2 = st->h[2];
406         h3 = st->h[3];
407         h4 = st->h[4];
408
409         while (bytes >= POLY1305_BLOCK_SIZE) {
410                 /* h += m[i] */
411                 h0 += (U8TO32(m + 0)) & 0x3ffffff;
412                 h1 += (U8TO32(m + 3) >> 2) & 0x3ffffff;
413                 h2 += (U8TO32(m + 6) >> 4) & 0x3ffffff;
414                 h3 += (U8TO32(m + 9) >> 6) & 0x3ffffff;
415                 h4 += (U8TO32(m + 12) >> 8) | hibit;
416
417                 /* h *= r */
418                 d0 = ((unsigned long long) h0 * r0) +
419                     ((unsigned long long) h1 * s4) +
420                     ((unsigned long long) h2 * s3) +
421                     ((unsigned long long) h3 * s2) +
422                     ((unsigned long long) h4 * s1);
423                 d1 = ((unsigned long long) h0 * r1) +
424                     ((unsigned long long) h1 * r0) +
425                     ((unsigned long long) h2 * s4) +
426                     ((unsigned long long) h3 * s3) +
427                     ((unsigned long long) h4 * s2);
428                 d2 = ((unsigned long long) h0 * r2) +
429                     ((unsigned long long) h1 * r1) +
430                     ((unsigned long long) h2 * r0) +
431                     ((unsigned long long) h3 * s4) +
432                     ((unsigned long long) h4 * s3);
433                 d3 = ((unsigned long long) h0 * r3) +
434                     ((unsigned long long) h1 * r2) +
435                     ((unsigned long long) h2 * r1) +
436                     ((unsigned long long) h3 * r0) +
437                     ((unsigned long long) h4 * s4);
438                 d4 = ((unsigned long long) h0 * r4) +
439                     ((unsigned long long) h1 * r3) +
440                     ((unsigned long long) h2 * r2) +
441                     ((unsigned long long) h3 * r1) +
442                     ((unsigned long long) h4 * r0);
443
444                 /* (partial) h %= p */
445                 c = (unsigned long) (d0 >> 26);
446                 h0 = (unsigned long) d0 & 0x3ffffff;
447                 d1 += c;
448                 c = (unsigned long) (d1 >> 26);
449                 h1 = (unsigned long) d1 & 0x3ffffff;
450                 d2 += c;
451                 c = (unsigned long) (d2 >> 26);
452                 h2 = (unsigned long) d2 & 0x3ffffff;
453                 d3 += c;
454                 c = (unsigned long) (d3 >> 26);
455                 h3 = (unsigned long) d3 & 0x3ffffff;
456                 d4 += c;
457                 c = (unsigned long) (d4 >> 26);
458                 h4 = (unsigned long) d4 & 0x3ffffff;
459                 h0 += c * 5;
460                 c = (h0 >> 26);
461                 h0 = h0 & 0x3ffffff;
462                 h1 += c;
463
464                 m += POLY1305_BLOCK_SIZE;
465                 bytes -= POLY1305_BLOCK_SIZE;
466         }
467
468         st->h[0] = h0;
469         st->h[1] = h1;
470         st->h[2] = h2;
471         st->h[3] = h3;
472         st->h[4] = h4;
473 }
474
475 void tls_poly1305_finish(struct poly1305_context *ctx, unsigned char mac[16]) {
476         size_t i;
477         struct poly1305_context *st = (struct poly1305_context *) ctx;
478         unsigned long h0, h1, h2, h3, h4, c;
479         unsigned long g0, g1, g2, g3, g4;
480         unsigned long long f;
481         unsigned long mask;
482
483         /* process the remaining block */
484         if (st->leftover) {
485                 i = st->leftover;
486                 st->buffer[i++] = 1;
487                 for (; i < POLY1305_BLOCK_SIZE; i++)
488                         st->buffer[i] = 0;
489                 st->final = 1;
490                 tls_poly1305_blocks(st, st->buffer, POLY1305_BLOCK_SIZE);
491         }
492
493         /* fully carry h */
494         h0 = st->h[0];
495         h1 = st->h[1];
496         h2 = st->h[2];
497         h3 = st->h[3];
498         h4 = st->h[4];
499
500         c = h1 >> 26;
501         h1 = h1 & 0x3ffffff;
502         h2 += c;
503         c = h2 >> 26;
504         h2 = h2 & 0x3ffffff;
505         h3 += c;
506         c = h3 >> 26;
507         h3 = h3 & 0x3ffffff;
508         h4 += c;
509         c = h4 >> 26;
510         h4 = h4 & 0x3ffffff;
511         h0 += c * 5;
512         c = h0 >> 26;
513         h0 = h0 & 0x3ffffff;
514         h1 += c;
515
516         /* compute h + -p */
517         g0 = h0 + 5;
518         c = g0 >> 26;
519         g0 &= 0x3ffffff;
520         g1 = h1 + c;
521         c = g1 >> 26;
522         g1 &= 0x3ffffff;
523         g2 = h2 + c;
524         c = g2 >> 26;
525         g2 &= 0x3ffffff;
526         g3 = h3 + c;
527         c = g3 >> 26;
528         g3 &= 0x3ffffff;
529         g4 = h4 + c - (1UL << 26);
530
531         /* select h if h < p, or h + -p if h >= p */
532         mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1;
533         g0 &= mask;
534         g1 &= mask;
535         g2 &= mask;
536         g3 &= mask;
537         g4 &= mask;
538         mask = ~mask;
539         h0 = (h0 & mask) | g0;
540         h1 = (h1 & mask) | g1;
541         h2 = (h2 & mask) | g2;
542         h3 = (h3 & mask) | g3;
543         h4 = (h4 & mask) | g4;
544
545         /* h = h % (2^128) */
546         h0 = ((h0) | (h1 << 26)) & 0xffffffff;
547         h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;
548         h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
549         h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;
550
551         /* mac = (h + pad) % (2^128) */
552         f = (unsigned long long) h0 + st->pad[0];
553         h0 = (unsigned long) f;
554         f = (unsigned long long) h1 + st->pad[1] + (f >> 32);
555         h1 = (unsigned long) f;
556         f = (unsigned long long) h2 + st->pad[2] + (f >> 32);
557         h2 = (unsigned long) f;
558         f = (unsigned long long) h3 + st->pad[3] + (f >> 32);
559         h3 = (unsigned long) f;
560
561         U32TO8(mac + 0, h0);
562         U32TO8(mac + 4, h1);
563         U32TO8(mac + 8, h2);
564         U32TO8(mac + 12, h3);
565
566         /* zero out the state */
567         st->h[0] = 0;
568         st->h[1] = 0;
569         st->h[2] = 0;
570         st->h[3] = 0;
571         st->h[4] = 0;
572         st->r[0] = 0;
573         st->r[1] = 0;
574         st->r[2] = 0;
575         st->r[3] = 0;
576         st->r[4] = 0;
577         st->pad[0] = 0;
578         st->pad[1] = 0;
579         st->pad[2] = 0;
580         st->pad[3] = 0;
581 }
582
583 void tls_poly1305_update(struct poly1305_context *ctx, const unsigned char *m,
584                 size_t bytes) {
585         struct poly1305_context *st = (struct poly1305_context *) ctx;
586         size_t i;
587         /* handle leftover */
588         if (st->leftover) {
589                 size_t want = (POLY1305_BLOCK_SIZE - st->leftover);
590                 if (want > bytes)
591                         want = bytes;
592                 for (i = 0; i < want; i++)
593                         st->buffer[st->leftover + i] = m[i];
594                 bytes -= want;
595                 m += want;
596                 st->leftover += want;
597                 if (st->leftover < POLY1305_BLOCK_SIZE)
598                         return;
599                 tls_poly1305_blocks(st, st->buffer, POLY1305_BLOCK_SIZE);
600                 st->leftover = 0;
601         }
602
603         /* process full blocks */
604         if (bytes >= POLY1305_BLOCK_SIZE) {
605                 size_t want = (bytes & ~(POLY1305_BLOCK_SIZE - 1));
606                 tls_poly1305_blocks(st, m, want);
607                 m += want;
608                 bytes -= want;
609         }
610
611         /* store leftover */
612         if (bytes) {
613                 for (i = 0; i < bytes; i++)
614                         st->buffer[st->leftover + i] = m[i];
615                 st->leftover += bytes;
616         }
617 }
618
619 int poly1305_verify(const unsigned char mac1[16],
620                     const unsigned char mac2[16]) {
621         size_t i;
622         unsigned int dif = 0;
623         for (i = 0; i < 16; i++)
624                 dif |= (mac1[i] ^ mac2[i]);
625         dif = (dif - 1) >> ((sizeof(unsigned int) * 8) - 1);
626         return (dif & 1);
627 }
628
629 void chacha20_poly1305_key(struct chacha_ctx *ctx,
630                            unsigned char *poly1305_key) {
631         unsigned char key[32];
632         unsigned char nonce[12];
633         chacha_key(ctx, key);
634         chacha_nonce(ctx, nonce);
635         poly1305_generate_key(key, nonce, sizeof(nonce), poly1305_key, 0);
636 }
637
638 static unsigned char zeropad[] =
639     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
640
641 int chacha20_poly1305_aead(struct chacha_ctx *ctx, unsigned char *pt,
642                            unsigned int len, unsigned char *aad,
643                            unsigned int aad_len, unsigned char *poly_key,
644                            unsigned char *out) {
645         if (aad_len > POLY1305_MAX_AAD) {
646                 return -1;
647         }
648
649         unsigned int counter = 1;
650         chacha_ivsetup_96bitnonce(ctx, NULL, (unsigned char *) &counter);
651         chacha_encrypt_bytes(ctx, pt, out, len);
652
653         struct poly1305_context aead_ctx;
654         tls_poly1305_init(&aead_ctx, poly_key);
655         tls_poly1305_update(&aead_ctx, aad, aad_len);
656         int rem = aad_len % 16;
657         if (rem) {
658                 tls_poly1305_update(&aead_ctx, zeropad, 16 - rem);
659         }
660         tls_poly1305_update(&aead_ctx, out, len);
661         rem = len % 16;
662         if (rem) {
663                 tls_poly1305_update(&aead_ctx, zeropad, 16 - rem);
664         }
665
666         unsigned char trail[16];
667         U32TO8(&trail[0], aad_len);
668         *(int *) &trail[4] = 0;
669         U32TO8(&trail[8], len);
670         *(int *) &trail[12] = 0;
671
672         tls_poly1305_update(&aead_ctx, trail, 16);
673         tls_poly1305_finish(&aead_ctx, out + len);
674
675         return len + POLY1305_TAGLEN;
676 }