Copyright consolidation 05/10
[openssl.git] / crypto / poly1305 / poly1305.c
1 /*
2  * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "internal/poly1305.h"
14
15 typedef void (*poly1305_blocks_f) (void *ctx, const unsigned char *inp,
16                                    size_t len, unsigned int padbit);
17 typedef void (*poly1305_emit_f) (void *ctx, unsigned char mac[16],
18                                  const unsigned int nonce[4]);
19
20 struct poly1305_context {
21     double opaque[24];  /* large enough to hold internal state, declared
22                          * 'double' to ensure at least 64-bit invariant
23                          * alignment across all platforms and
24                          * configurations */
25     unsigned int nonce[4];
26     unsigned char data[POLY1305_BLOCK_SIZE];
27     size_t num;
28     struct {
29         poly1305_blocks_f blocks;
30         poly1305_emit_f emit;
31     } func;
32 };
33
34 size_t Poly1305_ctx_size ()
35 {
36     return sizeof(struct poly1305_context);
37 }
38
39 /* pick 32-bit unsigned integer in little endian order */
40 static unsigned int U8TOU32(const unsigned char *p)
41 {
42     return (((unsigned int)(p[0] & 0xff)) |
43             ((unsigned int)(p[1] & 0xff) << 8) |
44             ((unsigned int)(p[2] & 0xff) << 16) |
45             ((unsigned int)(p[3] & 0xff) << 24));
46 }
47
48 /*
49  * Implementations can be classified by amount of significant bits in
50  * words making up the multi-precision value, or in other words radix
51  * or base of numerical representation, e.g. base 2^64, base 2^32,
52  * base 2^26. Complementary characteristic is how wide is the result of
53  * multiplication of pair of digits, e.g. it would take 128 bits to
54  * accommodate multiplication result in base 2^64 case. These are used
55  * interchangeably. To describe implementation that is. But interface
56  * is designed to isolate this so that low-level primitives implemented
57  * in assembly can be self-contained/self-coherent.
58  */
59 #ifndef POLY1305_ASM
60 /*
61  * Even though there is __int128 reference implementation targeting
62  * 64-bit platforms provided below, it's not obvious that it's optimal
63  * choice for every one of them. Depending on instruction set overall
64  * amount of instructions can be comparable to one in __int64
65  * implementation. Amount of multiplication instructions would be lower,
66  * but not necessarily overall. And in out-of-order execution context,
67  * it is the latter that can be crucial...
68  *
69  * On related note. Poly1305 author, D. J. Bernstein, discusses and
70  * provides floating-point implementations of the algorithm in question.
71  * It made a lot of sense by the time of introduction, because most
72  * then-modern processors didn't have pipelined integer multiplier.
73  * [Not to mention that some had non-constant timing for integer
74  * multiplications.] Floating-point instructions on the other hand could
75  * be issued every cycle, which allowed to achieve better performance.
76  * Nowadays, with SIMD and/or out-or-order execution, shared or
77  * even emulated FPU, it's more complicated, and floating-point
78  * implementation is not necessarily optimal choice in every situation,
79  * rather contrary...
80  *
81  *                                              <appro@openssl.org>
82  */
83
84 typedef unsigned int u32;
85
86 /*
87  * poly1305_blocks processes a multiple of POLY1305_BLOCK_SIZE blocks
88  * of |inp| no longer than |len|. Behaviour for |len| not divisible by
89  * block size is unspecified in general case, even though in reference
90  * implementation the trailing chunk is simply ignored. Per algorithm
91  * specification, every input block, complete or last partial, is to be
92  * padded with a bit past most significant byte. The latter kind is then
93  * padded with zeros till block size. This last partial block padding
94  * is caller(*)'s responsibility, and because of this the last partial
95  * block is always processed with separate call with |len| set to
96  * POLY1305_BLOCK_SIZE and |padbit| to 0. In all other cases |padbit|
97  * should be set to 1 to perform implicit padding with 128th bit.
98  * poly1305_blocks does not actually check for this constraint though,
99  * it's caller(*)'s responsibility to comply.
100  *
101  * (*)  In the context "caller" is not application code, but higher
102  *      level Poly1305_* from this very module, so that quirks are
103  *      handled locally.
104  */
105 static void
106 poly1305_blocks(void *ctx, const unsigned char *inp, size_t len, u32 padbit);
107
108 /*
109  * Type-agnostic "rip-off" from constant_time_locl.h
110  */
111 # define CONSTANT_TIME_CARRY(a,b) ( \
112          (a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1) \
113          )
114
115 # if !defined(PEDANTIC) && \
116      (defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16) && \
117      (defined(__SIZEOF_LONG__) && __SIZEOF_LONG__==8)
118
119 typedef unsigned long u64;
120 typedef unsigned __int128 u128;
121
122 typedef struct {
123     u64 h[3];
124     u64 r[2];
125 } poly1305_internal;
126
127 /* pick 32-bit unsigned integer in little endian order */
128 static u64 U8TOU64(const unsigned char *p)
129 {
130     return (((u64)(p[0] & 0xff)) |
131             ((u64)(p[1] & 0xff) << 8) |
132             ((u64)(p[2] & 0xff) << 16) |
133             ((u64)(p[3] & 0xff) << 24) |
134             ((u64)(p[4] & 0xff) << 32) |
135             ((u64)(p[5] & 0xff) << 40) |
136             ((u64)(p[6] & 0xff) << 48) |
137             ((u64)(p[7] & 0xff) << 56));
138 }
139
140 /* store a 32-bit unsigned integer in little endian */
141 static void U64TO8(unsigned char *p, u64 v)
142 {
143     p[0] = (unsigned char)((v) & 0xff);
144     p[1] = (unsigned char)((v >> 8) & 0xff);
145     p[2] = (unsigned char)((v >> 16) & 0xff);
146     p[3] = (unsigned char)((v >> 24) & 0xff);
147     p[4] = (unsigned char)((v >> 32) & 0xff);
148     p[5] = (unsigned char)((v >> 40) & 0xff);
149     p[6] = (unsigned char)((v >> 48) & 0xff);
150     p[7] = (unsigned char)((v >> 56) & 0xff);
151 }
152
153 static void poly1305_init(void *ctx, const unsigned char key[16])
154 {
155     poly1305_internal *st = (poly1305_internal *) ctx;
156
157     /* h = 0 */
158     st->h[0] = 0;
159     st->h[1] = 0;
160     st->h[2] = 0;
161
162     /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
163     st->r[0] = U8TOU64(&key[0]) & 0x0ffffffc0fffffff;
164     st->r[1] = U8TOU64(&key[8]) & 0x0ffffffc0ffffffc;
165 }
166
167 static void
168 poly1305_blocks(void *ctx, const unsigned char *inp, size_t len, u32 padbit)
169 {
170     poly1305_internal *st = (poly1305_internal *)ctx;
171     u64 r0, r1;
172     u64 s1;
173     u64 h0, h1, h2, c;
174     u128 d0, d1;
175
176     r0 = st->r[0];
177     r1 = st->r[1];
178
179     s1 = r1 + (r1 >> 2);
180
181     h0 = st->h[0];
182     h1 = st->h[1];
183     h2 = st->h[2];
184
185     while (len >= POLY1305_BLOCK_SIZE) {
186         /* h += m[i] */
187         h0 = (u64)(d0 = (u128)h0 + U8TOU64(inp + 0));
188         h1 = (u64)(d1 = (u128)h1 + (d0 >> 64) + U8TOU64(inp + 8));
189         /*
190          * padbit can be zero only when original len was
191          * POLY1306_BLOCK_SIZE, but we don't check
192          */
193         h2 += (u64)(d1 >> 64) + padbit;
194
195         /* h *= r "%" p, where "%" stands for "partial remainder" */
196         d0 = ((u128)h0 * r0) +
197              ((u128)h1 * s1);
198         d1 = ((u128)h0 * r1) +
199              ((u128)h1 * r0) +
200              (h2 * s1);
201         h2 = (h2 * r0);
202
203         /* last reduction step: */
204         /* a) h2:h0 = h2<<128 + d1<<64 + d0 */
205         h0 = (u64)d0;
206         h1 = (u64)(d1 += d0 >> 64);
207         h2 += (u64)(d1 >> 64);
208         /* b) (h2:h0 += (h2:h0>>130) * 5) %= 2^130 */
209         c = (h2 >> 2) + (h2 & ~3UL);
210         h2 &= 3;
211         h0 += c;
212         h1 += (c = CONSTANT_TIME_CARRY(h0,c));
213         h2 += CONSTANT_TIME_CARRY(h1,c);
214         /*
215          * Occasional overflows to 3rd bit of h2 are taken care of
216          * "naturally". If after this point we end up at the top of
217          * this loop, then the overflow bit will be accounted for
218          * in next iteration. If we end up in poly1305_emit, then
219          * comparison to modulus below will still count as "carry
220          * into 131st bit", so that properly reduced value will be
221          * picked in conditional move.
222          */
223
224         inp += POLY1305_BLOCK_SIZE;
225         len -= POLY1305_BLOCK_SIZE;
226     }
227
228     st->h[0] = h0;
229     st->h[1] = h1;
230     st->h[2] = h2;
231 }
232
233 static void poly1305_emit(void *ctx, unsigned char mac[16],
234                           const u32 nonce[4])
235 {
236     poly1305_internal *st = (poly1305_internal *) ctx;
237     u64 h0, h1, h2;
238     u64 g0, g1, g2;
239     u128 t;
240     u64 mask;
241
242     h0 = st->h[0];
243     h1 = st->h[1];
244     h2 = st->h[2];
245
246     /* compare to modulus by computing h + -p */
247     g0 = (u64)(t = (u128)h0 + 5);
248     g1 = (u64)(t = (u128)h1 + (t >> 64));
249     g2 = h2 + (u64)(t >> 64);
250
251     /* if there was carry into 131st bit, h1:h0 = g1:g0 */
252     mask = 0 - (g2 >> 2);
253     g0 &= mask;
254     g1 &= mask;
255     mask = ~mask;
256     h0 = (h0 & mask) | g0;
257     h1 = (h1 & mask) | g1;
258
259     /* mac = (h + nonce) % (2^128) */
260     h0 = (u64)(t = (u128)h0 + nonce[0] + ((u64)nonce[1]<<32));
261     h1 = (u64)(t = (u128)h1 + nonce[2] + ((u64)nonce[3]<<32) + (t >> 64));
262
263     U64TO8(mac + 0, h0);
264     U64TO8(mac + 8, h1);
265 }
266
267 # else
268
269 #  if defined(_WIN32) && !defined(__MINGW32__)
270 typedef unsigned __int64 u64;
271 #  elif defined(__arch64__)
272 typedef unsigned long u64;
273 #  else
274 typedef unsigned long long u64;
275 #  endif
276
277 typedef struct {
278     u32 h[5];
279     u32 r[4];
280 } poly1305_internal;
281
282 /* store a 32-bit unsigned integer in little endian */
283 static void U32TO8(unsigned char *p, unsigned int v)
284 {
285     p[0] = (unsigned char)((v) & 0xff);
286     p[1] = (unsigned char)((v >> 8) & 0xff);
287     p[2] = (unsigned char)((v >> 16) & 0xff);
288     p[3] = (unsigned char)((v >> 24) & 0xff);
289 }
290
291 static void poly1305_init(void *ctx, const unsigned char key[16])
292 {
293     poly1305_internal *st = (poly1305_internal *) ctx;
294
295     /* h = 0 */
296     st->h[0] = 0;
297     st->h[1] = 0;
298     st->h[2] = 0;
299     st->h[3] = 0;
300     st->h[4] = 0;
301
302     /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
303     st->r[0] = U8TOU32(&key[0]) & 0x0fffffff;
304     st->r[1] = U8TOU32(&key[4]) & 0x0ffffffc;
305     st->r[2] = U8TOU32(&key[8]) & 0x0ffffffc;
306     st->r[3] = U8TOU32(&key[12]) & 0x0ffffffc;
307 }
308
309 static void
310 poly1305_blocks(void *ctx, const unsigned char *inp, size_t len, u32 padbit)
311 {
312     poly1305_internal *st = (poly1305_internal *)ctx;
313     u32 r0, r1, r2, r3;
314     u32 s1, s2, s3;
315     u32 h0, h1, h2, h3, h4, c;
316     u64 d0, d1, d2, d3;
317
318     r0 = st->r[0];
319     r1 = st->r[1];
320     r2 = st->r[2];
321     r3 = st->r[3];
322
323     s1 = r1 + (r1 >> 2);
324     s2 = r2 + (r2 >> 2);
325     s3 = r3 + (r3 >> 2);
326
327     h0 = st->h[0];
328     h1 = st->h[1];
329     h2 = st->h[2];
330     h3 = st->h[3];
331     h4 = st->h[4];
332
333     while (len >= POLY1305_BLOCK_SIZE) {
334         /* h += m[i] */
335         h0 = (u32)(d0 = (u64)h0 + U8TOU32(inp + 0));
336         h1 = (u32)(d1 = (u64)h1 + (d0 >> 32) + U8TOU32(inp + 4));
337         h2 = (u32)(d2 = (u64)h2 + (d1 >> 32) + U8TOU32(inp + 8));
338         h3 = (u32)(d3 = (u64)h3 + (d2 >> 32) + U8TOU32(inp + 12));
339         h4 += (u32)(d3 >> 32) + padbit;
340
341         /* h *= r "%" p, where "%" stands for "partial remainder" */
342         d0 = ((u64)h0 * r0) +
343              ((u64)h1 * s3) +
344              ((u64)h2 * s2) +
345              ((u64)h3 * s1);
346         d1 = ((u64)h0 * r1) +
347              ((u64)h1 * r0) +
348              ((u64)h2 * s3) +
349              ((u64)h3 * s2) +
350              (h4 * s1);
351         d2 = ((u64)h0 * r2) +
352              ((u64)h1 * r1) +
353              ((u64)h2 * r0) +
354              ((u64)h3 * s3) +
355              (h4 * s2);
356         d3 = ((u64)h0 * r3) +
357              ((u64)h1 * r2) +
358              ((u64)h2 * r1) +
359              ((u64)h3 * r0) +
360              (h4 * s3);
361         h4 = (h4 * r0);
362
363         /* last reduction step: */
364         /* a) h4:h0 = h4<<128 + d3<<96 + d2<<64 + d1<<32 + d0 */
365         h0 = (u32)d0;
366         h1 = (u32)(d1 += d0 >> 32);
367         h2 = (u32)(d2 += d1 >> 32);
368         h3 = (u32)(d3 += d2 >> 32);
369         h4 += (u32)(d3 >> 32);
370         /* b) (h4:h0 += (h4:h0>>130) * 5) %= 2^130 */
371         c = (h4 >> 2) + (h4 & ~3U);
372         h4 &= 3;
373         h0 += c;
374         h1 += (c = CONSTANT_TIME_CARRY(h0,c));
375         h2 += (c = CONSTANT_TIME_CARRY(h1,c));
376         h3 += (c = CONSTANT_TIME_CARRY(h2,c));
377         h4 += CONSTANT_TIME_CARRY(h3,c);
378         /*
379          * Occasional overflows to 3rd bit of h4 are taken care of
380          * "naturally". If after this point we end up at the top of
381          * this loop, then the overflow bit will be accounted for
382          * in next iteration. If we end up in poly1305_emit, then
383          * comparison to modulus below will still count as "carry
384          * into 131st bit", so that properly reduced value will be
385          * picked in conditional move.
386          */
387
388         inp += POLY1305_BLOCK_SIZE;
389         len -= POLY1305_BLOCK_SIZE;
390     }
391
392     st->h[0] = h0;
393     st->h[1] = h1;
394     st->h[2] = h2;
395     st->h[3] = h3;
396     st->h[4] = h4;
397 }
398
399 static void poly1305_emit(void *ctx, unsigned char mac[16],
400                           const u32 nonce[4])
401 {
402     poly1305_internal *st = (poly1305_internal *) ctx;
403     u32 h0, h1, h2, h3, h4;
404     u32 g0, g1, g2, g3, g4;
405     u64 t;
406     u32 mask;
407
408     h0 = st->h[0];
409     h1 = st->h[1];
410     h2 = st->h[2];
411     h3 = st->h[3];
412     h4 = st->h[4];
413
414     /* compare to modulus by computing h + -p */
415     g0 = (u32)(t = (u64)h0 + 5);
416     g1 = (u32)(t = (u64)h1 + (t >> 32));
417     g2 = (u32)(t = (u64)h2 + (t >> 32));
418     g3 = (u32)(t = (u64)h3 + (t >> 32));
419     g4 = h4 + (u32)(t >> 32);
420
421     /* if there was carry into 131st bit, h3:h0 = g3:g0 */
422     mask = 0 - (g4 >> 2);
423     g0 &= mask;
424     g1 &= mask;
425     g2 &= mask;
426     g3 &= mask;
427     mask = ~mask;
428     h0 = (h0 & mask) | g0;
429     h1 = (h1 & mask) | g1;
430     h2 = (h2 & mask) | g2;
431     h3 = (h3 & mask) | g3;
432
433     /* mac = (h + nonce) % (2^128) */
434     h0 = (u32)(t = (u64)h0 + nonce[0]);
435     h1 = (u32)(t = (u64)h1 + (t >> 32) + nonce[1]);
436     h2 = (u32)(t = (u64)h2 + (t >> 32) + nonce[2]);
437     h3 = (u32)(t = (u64)h3 + (t >> 32) + nonce[3]);
438
439     U32TO8(mac + 0, h0);
440     U32TO8(mac + 4, h1);
441     U32TO8(mac + 8, h2);
442     U32TO8(mac + 12, h3);
443 }
444 # endif
445 #else
446 int poly1305_init(void *ctx, const unsigned char key[16], void *func);
447 void poly1305_blocks(void *ctx, const unsigned char *inp, size_t len,
448                      unsigned int padbit);
449 void poly1305_emit(void *ctx, unsigned char mac[16],
450                    const unsigned int nonce[4]);
451 #endif
452
453 void Poly1305_Init(POLY1305 *ctx, const unsigned char key[32])
454 {
455     ctx->nonce[0] = U8TOU32(&key[16]);
456     ctx->nonce[1] = U8TOU32(&key[20]);
457     ctx->nonce[2] = U8TOU32(&key[24]);
458     ctx->nonce[3] = U8TOU32(&key[28]);
459
460 #ifndef POLY1305_ASM
461     poly1305_init(ctx->opaque, key);
462 #else
463     /*
464      * Unlike reference poly1305_init assembly counterpart is expected
465      * to return a value: non-zero if it initializes ctx->func, and zero
466      * otherwise. Latter is to simplify assembly in cases when there no
467      * multiple code paths to switch between.
468      */
469     if (!poly1305_init(ctx->opaque, key, &ctx->func)) {
470         ctx->func.blocks = poly1305_blocks;
471         ctx->func.emit = poly1305_emit;
472     }
473 #endif
474
475     ctx->num = 0;
476
477 }
478
479 #ifdef POLY1305_ASM
480 /*
481  * This "eclipses" poly1305_blocks and poly1305_emit, but it's
482  * conscious choice imposed by -Wshadow compiler warnings.
483  */
484 # define poly1305_blocks (*poly1305_blocks_p)
485 # define poly1305_emit   (*poly1305_emit_p)
486 #endif
487
488 void Poly1305_Update(POLY1305 *ctx, const unsigned char *inp, size_t len)
489 {
490 #ifdef POLY1305_ASM
491     /*
492      * As documented, poly1305_blocks is never called with input
493      * longer than single block and padbit argument set to 0. This
494      * property is fluently used in assembly modules to optimize
495      * padbit handling on loop boundary.
496      */
497     poly1305_blocks_f poly1305_blocks_p = ctx->func.blocks;
498 #endif
499     size_t rem, num;
500
501     if ((num = ctx->num)) {
502         rem = POLY1305_BLOCK_SIZE - num;
503         if (len >= rem) {
504             memcpy(ctx->data + num, inp, rem);
505             poly1305_blocks(ctx->opaque, ctx->data, POLY1305_BLOCK_SIZE, 1);
506             inp += rem;
507             len -= rem;
508         } else {
509             /* Still not enough data to process a block. */
510             memcpy(ctx->data + num, inp, len);
511             ctx->num = num + len;
512             return;
513         }
514     }
515
516     rem = len % POLY1305_BLOCK_SIZE;
517     len -= rem;
518
519     if (len >= POLY1305_BLOCK_SIZE) {
520         poly1305_blocks(ctx->opaque, inp, len, 1);
521         inp += len;
522     }
523
524     if (rem)
525         memcpy(ctx->data, inp, rem);
526
527     ctx->num = rem;
528 }
529
530 void Poly1305_Final(POLY1305 *ctx, unsigned char mac[16])
531 {
532 #ifdef POLY1305_ASM
533     poly1305_blocks_f poly1305_blocks_p = ctx->func.blocks;
534     poly1305_emit_f poly1305_emit_p = ctx->func.emit;
535 #endif
536     size_t num;
537
538     if ((num = ctx->num)) {
539         ctx->data[num++] = 1;   /* pad bit */
540         while (num < POLY1305_BLOCK_SIZE)
541             ctx->data[num++] = 0;
542         poly1305_blocks(ctx->opaque, ctx->data, POLY1305_BLOCK_SIZE, 0);
543     }
544
545     poly1305_emit(ctx->opaque, mac, ctx->nonce);
546
547     /* zero out the state */
548     memset(ctx, 0, sizeof(*ctx));
549 }
550
551 #ifdef SELFTEST
552 #include <stdio.h>
553
554 struct poly1305_test {
555     const char *inputhex;
556     const char *keyhex;
557     const char *outhex;
558 };
559
560 static const struct poly1305_test poly1305_tests[] = {
561     /*
562      * RFC7539
563      */
564     {
565      "43727970746f6772617068696320466f72756d2052657365617263682047726f"
566      "7570",
567      "85d6be7857556d337f4452fe42d506a8""0103808afb0db2fd4abff6af4149f51b",
568      "a8061dc1305136c6c22b8baf0c0127a9"
569     },
570     /*
571      * test vectors from "The Poly1305-AES message-authentication code"
572      */
573     {
574      "f3f6",
575      "851fc40c3467ac0be05cc20404f3f700""580b3b0f9447bb1e69d095b5928b6dbc",
576      "f4c633c3044fc145f84f335cb81953de"
577     },
578     {
579      "",
580      "a0f3080000f46400d0c7e9076c834403""dd3fab2251f11ac759f0887129cc2ee7",
581      "dd3fab2251f11ac759f0887129cc2ee7"
582     },
583     {
584      "663cea190ffb83d89593f3f476b6bc24d7e679107ea26adb8caf6652d0656136",
585      "48443d0bb0d21109c89a100b5ce2c208""83149c69b561dd88298a1798b10716ef",
586      "0ee1c16bb73f0f4fd19881753c01cdbe"
587     },
588     {
589      "ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0"
590      "990c62e48b8018b2c3e4a0fa3134cb67fa83e158c994d961c4cb21095c1bf9",
591      "12976a08c4426d0ce8a82407c4f48207""80f8c20aa71202d1e29179cbcb555a57",
592      "5154ad0d2cb26e01274fc51148491f1b"
593     },
594     /*
595      * self-generated vectors exercise "significant" lengths, such that
596      * are handled by different code paths
597      */
598     {
599      "ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0"
600      "990c62e48b8018b2c3e4a0fa3134cb67fa83e158c994d961c4cb21095c1bf9af",
601      "12976a08c4426d0ce8a82407c4f48207""80f8c20aa71202d1e29179cbcb555a57",
602      "812059a5da198637cac7c4a631bee466"
603     },
604     {
605      "ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0"
606      "990c62e48b8018b2c3e4a0fa3134cb67",
607      "12976a08c4426d0ce8a82407c4f48207""80f8c20aa71202d1e29179cbcb555a57",
608      "5b88d7f6228b11e2e28579a5c0c1f761"
609     },
610     {
611      "ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0"
612      "990c62e48b8018b2c3e4a0fa3134cb67fa83e158c994d961c4cb21095c1bf9af"
613      "663cea190ffb83d89593f3f476b6bc24d7e679107ea26adb8caf6652d0656136",
614      "12976a08c4426d0ce8a82407c4f48207""80f8c20aa71202d1e29179cbcb555a57",
615      "bbb613b2b6d753ba07395b916aaece15"
616     },
617     {
618      "ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0"
619      "990c62e48b8018b2c3e4a0fa3134cb67fa83e158c994d961c4cb21095c1bf9af"
620      "48443d0bb0d21109c89a100b5ce2c20883149c69b561dd88298a1798b10716ef"
621      "663cea190ffb83d89593f3f476b6bc24",
622      "12976a08c4426d0ce8a82407c4f48207""80f8c20aa71202d1e29179cbcb555a57",
623      "c794d7057d1778c4bbee0a39b3d97342"
624     },
625     {
626      "ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0"
627      "990c62e48b8018b2c3e4a0fa3134cb67fa83e158c994d961c4cb21095c1bf9af"
628      "48443d0bb0d21109c89a100b5ce2c20883149c69b561dd88298a1798b10716ef"
629      "663cea190ffb83d89593f3f476b6bc24d7e679107ea26adb8caf6652d0656136",
630      "12976a08c4426d0ce8a82407c4f48207""80f8c20aa71202d1e29179cbcb555a57",
631      "ffbcb9b371423152d7fca5ad042fbaa9"
632     },
633     {
634      "ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0"
635      "990c62e48b8018b2c3e4a0fa3134cb67fa83e158c994d961c4cb21095c1bf9af"
636      "48443d0bb0d21109c89a100b5ce2c20883149c69b561dd88298a1798b10716ef"
637      "663cea190ffb83d89593f3f476b6bc24d7e679107ea26adb8caf6652d0656136"
638      "812059a5da198637cac7c4a631bee466",
639      "12976a08c4426d0ce8a82407c4f48207""80f8c20aa71202d1e29179cbcb555a57",
640      "069ed6b8ef0f207b3e243bb1019fe632"
641     },
642     {
643      "ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0"
644      "990c62e48b8018b2c3e4a0fa3134cb67fa83e158c994d961c4cb21095c1bf9af"
645      "48443d0bb0d21109c89a100b5ce2c20883149c69b561dd88298a1798b10716ef"
646      "663cea190ffb83d89593f3f476b6bc24d7e679107ea26adb8caf6652d0656136"
647      "812059a5da198637cac7c4a631bee4665b88d7f6228b11e2e28579a5c0c1f761",
648      "12976a08c4426d0ce8a82407c4f48207""80f8c20aa71202d1e29179cbcb555a57",
649      "cca339d9a45fa2368c2c68b3a4179133"
650     },
651     {
652      "ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0"
653      "990c62e48b8018b2c3e4a0fa3134cb67fa83e158c994d961c4cb21095c1bf9af"
654      "48443d0bb0d21109c89a100b5ce2c20883149c69b561dd88298a1798b10716ef"
655      "663cea190ffb83d89593f3f476b6bc24d7e679107ea26adb8caf6652d0656136"
656      "812059a5da198637cac7c4a631bee4665b88d7f6228b11e2e28579a5c0c1f761"
657      "ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0"
658      "990c62e48b8018b2c3e4a0fa3134cb67fa83e158c994d961c4cb21095c1bf9af"
659      "48443d0bb0d21109c89a100b5ce2c20883149c69b561dd88298a1798b10716ef"
660      "663cea190ffb83d89593f3f476b6bc24d7e679107ea26adb8caf6652d0656136",
661      "12976a08c4426d0ce8a82407c4f48207""80f8c20aa71202d1e29179cbcb555a57",
662      "53f6e828a2f0fe0ee815bf0bd5841a34"
663     },
664     {
665      "ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0"
666      "990c62e48b8018b2c3e4a0fa3134cb67fa83e158c994d961c4cb21095c1bf9af"
667      "48443d0bb0d21109c89a100b5ce2c20883149c69b561dd88298a1798b10716ef"
668      "663cea190ffb83d89593f3f476b6bc24d7e679107ea26adb8caf6652d0656136"
669      "812059a5da198637cac7c4a631bee4665b88d7f6228b11e2e28579a5c0c1f761"
670      "ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0"
671      "990c62e48b8018b2c3e4a0fa3134cb67fa83e158c994d961c4cb21095c1bf9af"
672      "48443d0bb0d21109c89a100b5ce2c20883149c69b561dd88298a1798b10716ef"
673      "663cea190ffb83d89593f3f476b6bc24d7e679107ea26adb8caf6652d0656136"
674      "812059a5da198637cac7c4a631bee4665b88d7f6228b11e2e28579a5c0c1f761",
675      "12976a08c4426d0ce8a82407c4f48207""80f8c20aa71202d1e29179cbcb555a57",
676      "b846d44e9bbd53cedffbfbb6b7fa4933"
677     },
678     /*
679      * 4th power of the key spills to 131th bit in SIMD key setup
680      */
681     {
682      "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
683      "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
684      "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
685      "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
686      "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
687      "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
688      "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
689      "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
690      "ad628107e8351d0f2c231a05dc4a4106""00000000000000000000000000000000",
691      "07145a4c02fe5fa32036de68fabe9066"
692     },
693     {
694     /*
695      * poly1305_ieee754.c failed this in final stage
696      */
697      "842364e156336c0998b933a6237726180d9e3fdcbde4cd5d17080fc3beb49614"
698      "d7122c037463ff104d73f19c12704628d417c4c54a3fe30d3c3d7714382d43b0"
699      "382a50a5dee54be844b076e8df88201a1cd43b90eb21643fa96f39b518aa8340"
700      "c942ff3c31baf7c9bdbf0f31ae3fa096bf8c63030609829fe72e179824890bc8"
701      "e08c315c1cce2a83144dbbff09f74e3efc770b54d0984a8f19b14719e6363564"
702      "1d6b1eedf63efbf080e1783d32445412114c20de0b837a0dfa33d6b82825fff4"
703      "4c9a70ea54ce47f07df698e6b03323b53079364a5fc3e9dd034392bdde86dccd"
704      "da94321c5e44060489336cb65bf3989c36f7282c2f5d2b882c171e74",
705      "95d5c005503e510d8cd0aa072c4a4d06""6eabc52d11653df47fbf63ab198bcc26",
706      "f248312e578d9d58f8b7bb4d19105431"
707     },
708     /*
709      * AVX2 in poly1305-x86.pl failed this with 176+32 split
710      */
711     {
712     "248ac31085b6c2adaaa38259a0d7192c5c35d1bb4ef39ad94c38d1c82479e2dd"
713     "2159a077024b0589bc8a20101b506f0a1ad0bbab76e83a83f1b94be6beae74e8"
714     "74cab692c5963a75436b776121ec9f62399a3e66b2d22707dae81933b6277f3c"
715     "8516bcbe26dbbd86f373103d7cf4cad1888c952118fbfbd0d7b4bedc4ae4936a"
716     "ff91157e7aa47c54442ea78d6ac251d324a0fbe49d89cc3521b66d16e9c66a37"
717     "09894e4eb0a4eedc4ae19468e66b81f2"
718     "71351b1d921ea551047abcc6b87a901fde7db79fa1818c11336dbc07244a40eb",
719     "000102030405060708090a0b0c0d0e0f""00000000000000000000000000000000",
720     "bc939bc5281480fa99c6d68c258ec42f"
721     },
722     /*
723      * test vectors from Google
724      */
725     {
726      "",
727      "c8afaac331ee372cd6082de134943b17""4710130e9f6fea8d72293850a667d86c",
728      "4710130e9f6fea8d72293850a667d86c",
729     },
730     {
731      "48656c6c6f20776f726c6421",
732      "746869732069732033322d6279746520""6b657920666f7220506f6c7931333035",
733      "a6f745008f81c916a20dcc74eef2b2f0"
734     },
735     {
736      "0000000000000000000000000000000000000000000000000000000000000000",
737      "746869732069732033322d6279746520""6b657920666f7220506f6c7931333035",
738      "49ec78090e481ec6c26b33b91ccc0307"
739     },
740     {
741      "89dab80b7717c1db5db437860a3f70218e93e1b8f461fb677f16f35f6f87e2a9"
742      "1c99bc3a47ace47640cc95c345be5ecca5a3523c35cc01893af0b64a62033427"
743      "0372ec12482d1b1e363561698a578b359803495bb4e2ef1930b17a5190b580f1"
744      "41300df30adbeca28f6427a8bc1a999fd51c554a017d095d8c3e3127daf9f595",
745      "2d773be37adb1e4d683bf0075e79c4ee""037918535a7f99ccb7040fb5f5f43aea",
746      "c85d15ed44c378d6b00e23064c7bcd51"
747     },
748     {
749      "000000000000000b1703030200000000"
750      "06db1f1f368d696a810a349c0c714c9a5e7850c2407d721acded95e018d7a852"
751      "66a6e1289cdb4aeb18da5ac8a2b0026d24a59ad485227f3eaedbb2e7e35e1c66"
752      "cd60f9abf716dcc9ac42682dd7dab287a7024c4eefc321cc0574e16793e37cec"
753      "03c5bda42b54c114a80b57af26416c7be742005e20855c73e21dc8e2edc9d435"
754      "cb6f6059280011c270b71570051c1c9b3052126620bc1e2730fa066c7a509d53"
755      "c60e5ae1b40aa6e39e49669228c90eecb4a50db32a50bc49e90b4f4b359a1dfd"
756      "11749cd3867fcf2fb7bb6cd4738f6a4ad6f7ca5058f7618845af9f020f6c3b96"
757      "7b8f4cd4a91e2813b507ae66f2d35c18284f7292186062e10fd5510d18775351"
758      "ef334e7634ab4743f5b68f49adcab384d3fd75f7390f4006ef2a295c8c7a076a"
759      "d54546cd25d2107fbe1436c840924aaebe5b370893cd63d1325b8616fc481088"
760      "6bc152c53221b6df373119393255ee72bcaa880174f1717f9184fa91646f17a2"
761      "4ac55d16bfddca9581a92eda479201f0edbf633600d6066d1ab36d5d2415d713"
762      "51bbcd608a25108d25641992c1f26c531cf9f90203bc4cc19f5927d834b0a471"
763      "16d3884bbb164b8ec883d1ac832e56b3918a98601a08d171881541d594db399c"
764      "6ae6151221745aec814c45b0b05b565436fd6f137aa10a0c0b643761dbd6f9a9"
765      "dcb99b1a6e690854ce0769cde39761d82fcdec15f0d92d7d8e94ade8eb83fbe0",
766      "99e5822dd4173c995e3dae0ddefb9774""3fde3b080134b39f76e9bf8d0e88d546",
767      "2637408fe13086ea73f971e3425e2820"
768     },
769     /*
770      * test vectors from Hanno Böck
771      */
772     {
773      "cccccccccccccccccccccccccccccccccccccccccccccccccc80cccccccccccc"
774      "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccecccccc"
775      "ccccccccccccccccccccccccccccccc5cccccccccccccccccccccccccccccccc"
776      "cccccccccce3cccccccccccccccccccccccccccccccccccccccccccccccccccc"
777      "ccccccccaccccccccccccccccccccce6cccccccccc000000afcccccccccccccc"
778      "ccccfffffff50000000000000000000000000000000000000000000000000000"
779      "00ffffffe7000000000000000000000000000000000000000000000000000000"
780      "0000000000000000000000000000000000000000000000000000719205a8521d"
781      "fc",
782      "7f1b0264000000000000000000000000""0000000000000000cccccccccccccccc",
783      "8559b876eceed66eb37798c0457baff9"
784     },
785     {
786      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000"
787      "00000000800264",
788      "e0001600000000000000000000000000""0000aaaaaaaaaaaaaaaaaaaaaaaaaaaa",
789      "00bd1258978e205444c9aaaa82006fed"
790     },
791     {
792      "02fc",
793      "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c""0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
794      "06120c0c0c0c0c0c0c0c0c0c0c0c0c0c"
795     },
796     {
797      "7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b"
798      "7b7b7b7b7b7b7a7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b"
799      "7b7b5c7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b"
800      "7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b6e7b007b7b7b7b7b7b7b7b7b"
801      "7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7a7b7b7b7b7b7b7b7b7b7b7b7b"
802      "7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b5c7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b"
803      "7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b"
804      "7b6e7b001300000000b300000000000000000000000000000000000000000000"
805      "f20000000000000000000000000000000000002000efff000900000000000000"
806      "0000000000100000000009000000640000000000000000000000001300000000"
807      "b300000000000000000000000000000000000000000000f20000000000000000"
808      "000000000000000000002000efff00090000000000000000007a000010000000"
809      "000900000064000000000000000000000000000000000000000000000000fc",
810      "00ff0000000000000000000000000000""00000000001e00000000000000007b7b",
811      "33205bbf9e9f8f7212ab9e2ab9b7e4a5"
812     },
813     {
814      "7777777777777777777777777777777777777777777777777777777777777777"
815      "7777777777777777777777777777777777777777777777777777777777777777"
816      "777777777777777777777777ffffffe9e9acacacacacacacacacacac0000acac"
817      "ec0100acacac2caca2acacacacacacacacacacac64f2",
818      "0000007f0000007f0100002000000000""0000cf77777777777777777777777777",
819      "02ee7c8c546ddeb1a467e4c3981158b9"
820     },
821     /*
822      * test vectors from Andrew Moon
823      */
824     {   /* nacl */
825      "8e993b9f48681273c29650ba32fc76ce48332ea7164d96a4476fb8c531a1186a"
826      "c0dfc17c98dce87b4da7f011ec48c97271d2c20f9b928fe2270d6fb863d51738"
827      "b48eeee314a7cc8ab932164548e526ae90224368517acfeabd6bb3732bc0e9da"
828      "99832b61ca01b6de56244a9e88d5f9b37973f622a43d14a6599b1f654cb45a74"
829      "e355a5",
830      "eea6a7251c1e72916d11c2cb214d3c25""2539121d8e234e652d651fa4c8cff880",
831      "f3ffc7703f9400e52a7dfb4b3d3305d9"
832     },
833     {   /* wrap 2^130-5 */
834      "ffffffffffffffffffffffffffffffff",
835      "02000000000000000000000000000000""00000000000000000000000000000000",
836      "03000000000000000000000000000000"
837     },
838     {   /* wrap 2^128 */
839      "02000000000000000000000000000000",
840      "02000000000000000000000000000000""ffffffffffffffffffffffffffffffff",
841      "03000000000000000000000000000000"
842     },
843     {   /* limb carry */
844      "fffffffffffffffffffffffffffffffff0ffffffffffffffffffffffffffffff"
845      "11000000000000000000000000000000",
846      "01000000000000000000000000000000""00000000000000000000000000000000",
847      "05000000000000000000000000000000"
848     },
849     {   /* 2^130-5 */
850      "fffffffffffffffffffffffffffffffffbfefefefefefefefefefefefefefefe"
851      "01010101010101010101010101010101",
852      "01000000000000000000000000000000""00000000000000000000000000000000",
853      "00000000000000000000000000000000"
854     },
855     {   /* 2^130-6 */
856      "fdffffffffffffffffffffffffffffff",
857      "02000000000000000000000000000000""00000000000000000000000000000000",
858      "faffffffffffffffffffffffffffffff"
859     },
860     {   /* 5*H+L reduction intermediate */
861      "e33594d7505e43b900000000000000003394d7505e4379cd0100000000000000"
862      "0000000000000000000000000000000001000000000000000000000000000000",
863      "01000000000000000400000000000000""00000000000000000000000000000000",
864      "14000000000000005500000000000000"
865     },
866     {   /* 5*H+L reduction final */
867      "e33594d7505e43b900000000000000003394d7505e4379cd0100000000000000"
868      "00000000000000000000000000000000",
869      "01000000000000000400000000000000""00000000000000000000000000000000",
870      "13000000000000000000000000000000"
871     }
872 };
873
874 static unsigned char hex_digit(char h)
875 {
876     int i = OPENSSL_hexchar2int(h);
877
878     if (i < 0)
879         abort();
880     return i;
881 }
882
883 static void hex_decode(unsigned char *out, const char *hex)
884 {
885     size_t j = 0;
886
887     while (*hex != 0) {
888         unsigned char v = hex_digit(*hex++);
889         v <<= 4;
890         v |= hex_digit(*hex++);
891         out[j++] = v;
892     }
893 }
894
895 static void hexdump(unsigned char *a, size_t len)
896 {
897     size_t i;
898
899     for (i = 0; i < len; i++)
900         printf("%02x", a[i]);
901 }
902
903 int main()
904 {
905     static const unsigned num_tests =
906         sizeof(poly1305_tests) / sizeof(struct poly1305_test);
907     unsigned i;
908     unsigned char key[32], out[16], expected[16];
909     POLY1305 poly1305;
910
911     for (i = 0; i < num_tests; i++) {
912         const struct poly1305_test *test = &poly1305_tests[i];
913         unsigned char *in;
914         size_t inlen = strlen(test->inputhex);
915
916         if (strlen(test->keyhex) != sizeof(key) * 2 ||
917             strlen(test->outhex) != sizeof(out) * 2 || (inlen & 1) == 1)
918             return 1;
919
920         inlen /= 2;
921
922         hex_decode(key, test->keyhex);
923         hex_decode(expected, test->outhex);
924
925         in = malloc(inlen);
926
927         hex_decode(in, test->inputhex);
928
929         Poly1305_Init(&poly1305, key);
930         Poly1305_Update(&poly1305, in, inlen);
931         Poly1305_Final(&poly1305, out);
932
933         if (memcmp(out, expected, sizeof(expected)) != 0) {
934             printf("Poly1305 test #%d failed.\n", i);
935             printf("got:      ");
936             hexdump(out, sizeof(out));
937             printf("\nexpected: ");
938             hexdump(expected, sizeof(expected));
939             printf("\n");
940             return 1;
941         }
942
943         if (inlen > 16) {
944             Poly1305_Init(&poly1305, key);
945             Poly1305_Update(&poly1305, in, 1);
946             Poly1305_Update(&poly1305, in+1, inlen-1);
947             Poly1305_Final(&poly1305, out);
948
949             if (memcmp(out, expected, sizeof(expected)) != 0) {
950                 printf("Poly1305 test #%d/1+(N-1) failed.\n", i);
951                 printf("got:      ");
952                 hexdump(out, sizeof(out));
953                 printf("\nexpected: ");
954                 hexdump(expected, sizeof(expected));
955                 printf("\n");
956                 return 1;
957             }
958         }
959
960         if (inlen > 32) {
961             size_t half = inlen / 2;
962
963             Poly1305_Init(&poly1305, key);
964             Poly1305_Update(&poly1305, in, half);
965             Poly1305_Update(&poly1305, in+half, inlen-half);
966             Poly1305_Final(&poly1305, out);
967
968             if (memcmp(out, expected, sizeof(expected)) != 0) {
969                 printf("Poly1305 test #%d/2 failed.\n", i);
970                 printf("got:      ");
971                 hexdump(out, sizeof(out));
972                 printf("\nexpected: ");
973                 hexdump(expected, sizeof(expected));
974                 printf("\n");
975                 return 1;
976             }
977
978             for (half = 16; half < inlen; half += 16) {
979                 Poly1305_Init(&poly1305, key);
980                 Poly1305_Update(&poly1305, in, half);
981                 Poly1305_Update(&poly1305, in+half, inlen-half);
982                 Poly1305_Final(&poly1305, out);
983
984                 if (memcmp(out, expected, sizeof(expected)) != 0) {
985                     printf("Poly1305 test #%d/%d+%d failed.\n",
986                                            i, half, inlen-half);
987                     printf("got:      ");
988                     hexdump(out, sizeof(out));
989                     printf("\nexpected: ");
990                     hexdump(expected, sizeof(expected));
991                     printf("\n");
992                     return 1;
993                 }
994             }
995         }
996
997         free(in);
998     }
999
1000     printf("PASS\n");
1001
1002 # ifdef OPENSSL_CPUID_OBJ
1003     {
1004         unsigned char buf[8192];
1005         unsigned long long stopwatch;
1006         unsigned long long OPENSSL_rdtsc();
1007
1008         memset (buf,0x55,sizeof(buf));
1009         memset (key,0xAA,sizeof(key));
1010
1011         Poly1305_Init(&poly1305, key);
1012
1013         for (i=0;i<100000;i++)
1014             Poly1305_Update(&poly1305,buf,sizeof(buf));
1015
1016         stopwatch = OPENSSL_rdtsc();
1017         for (i=0;i<10000;i++)
1018             Poly1305_Update(&poly1305,buf,sizeof(buf));
1019         stopwatch = OPENSSL_rdtsc() - stopwatch;
1020
1021         printf("%g\n",stopwatch/(double)(i*sizeof(buf)));
1022
1023         stopwatch = OPENSSL_rdtsc();
1024         for (i=0;i<10000;i++) {
1025             Poly1305_Init(&poly1305, key);
1026             Poly1305_Update(&poly1305,buf,16);
1027             Poly1305_Final(&poly1305,buf);
1028         }
1029         stopwatch = OPENSSL_rdtsc() - stopwatch;
1030
1031         printf("%g\n",stopwatch/(double)(i));
1032     }
1033 # endif
1034     return 0;
1035 }
1036 #endif