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