Home | History | Annotate | Line # | Download | only in dist
poly1305.c revision 1.5.12.2
      1  1.5.12.2  snj /*
      2  1.5.12.2  snj  * Public Domain poly1305 from Andrew Moon
      3  1.5.12.2  snj  * poly1305-donna-unrolled.c from https://github.com/floodyberry/poly1305-donna
      4  1.5.12.2  snj  */
      5  1.5.12.2  snj 
      6  1.5.12.2  snj /* $OpenBSD: poly1305.c,v 1.3 2013/12/19 22:57:13 djm Exp $ */
      7  1.5.12.2  snj #include "includes.h"
      8  1.5.12.2  snj __RCSID("$NetBSD: poly1305.c,v 1.5.12.2 2017/08/15 05:27:52 snj Exp $");
      9  1.5.12.2  snj 
     10  1.5.12.2  snj #include <sys/types.h>
     11  1.5.12.2  snj #include <stdint.h>
     12  1.5.12.2  snj 
     13  1.5.12.2  snj #include "poly1305.h"
     14  1.5.12.2  snj 
     15  1.5.12.2  snj #define mul32x32_64(a,b) ((uint64_t)(a) * (b))
     16  1.5.12.2  snj 
     17  1.5.12.2  snj #define U8TO32_LE(p) \
     18  1.5.12.2  snj 	(((uint32_t)((p)[0])) | \
     19  1.5.12.2  snj 	 ((uint32_t)((p)[1]) <<  8) | \
     20  1.5.12.2  snj 	 ((uint32_t)((p)[2]) << 16) | \
     21  1.5.12.2  snj 	 ((uint32_t)((p)[3]) << 24))
     22  1.5.12.2  snj 
     23  1.5.12.2  snj #define U32TO8_LE(p, v) \
     24  1.5.12.2  snj 	do { \
     25  1.5.12.2  snj 		(p)[0] = (uint8_t)((v)); \
     26  1.5.12.2  snj 		(p)[1] = (uint8_t)((v) >>  8); \
     27  1.5.12.2  snj 		(p)[2] = (uint8_t)((v) >> 16); \
     28  1.5.12.2  snj 		(p)[3] = (uint8_t)((v) >> 24); \
     29  1.5.12.2  snj 	} while (0)
     30  1.5.12.2  snj 
     31  1.5.12.2  snj void
     32  1.5.12.2  snj poly1305_auth(unsigned char out[POLY1305_TAGLEN], const unsigned char *m, size_t inlen, const unsigned char key[POLY1305_KEYLEN]) {
     33  1.5.12.2  snj 	uint32_t t0,t1,t2,t3;
     34  1.5.12.2  snj 	uint32_t h0,h1,h2,h3,h4;
     35  1.5.12.2  snj 	uint32_t r0,r1,r2,r3,r4;
     36  1.5.12.2  snj 	uint32_t s1,s2,s3,s4;
     37  1.5.12.2  snj 	uint32_t b, nb;
     38  1.5.12.2  snj 	size_t j;
     39  1.5.12.2  snj 	uint64_t t[5];
     40  1.5.12.2  snj 	uint64_t f0,f1,f2,f3;
     41  1.5.12.2  snj 	uint32_t g0,g1,g2,g3,g4;
     42  1.5.12.2  snj 	uint64_t c;
     43  1.5.12.2  snj 	unsigned char mp[16];
     44  1.5.12.2  snj 
     45  1.5.12.2  snj 	/* clamp key */
     46  1.5.12.2  snj 	t0 = U8TO32_LE(key+0);
     47  1.5.12.2  snj 	t1 = U8TO32_LE(key+4);
     48  1.5.12.2  snj 	t2 = U8TO32_LE(key+8);
     49  1.5.12.2  snj 	t3 = U8TO32_LE(key+12);
     50  1.5.12.2  snj 
     51  1.5.12.2  snj 	/* precompute multipliers */
     52  1.5.12.2  snj 	r0 = t0 & 0x3ffffff; t0 >>= 26; t0 |= t1 << 6;
     53  1.5.12.2  snj 	r1 = t0 & 0x3ffff03; t1 >>= 20; t1 |= t2 << 12;
     54  1.5.12.2  snj 	r2 = t1 & 0x3ffc0ff; t2 >>= 14; t2 |= t3 << 18;
     55  1.5.12.2  snj 	r3 = t2 & 0x3f03fff; t3 >>= 8;
     56  1.5.12.2  snj 	r4 = t3 & 0x00fffff;
     57  1.5.12.2  snj 
     58  1.5.12.2  snj 	s1 = r1 * 5;
     59  1.5.12.2  snj 	s2 = r2 * 5;
     60  1.5.12.2  snj 	s3 = r3 * 5;
     61  1.5.12.2  snj 	s4 = r4 * 5;
     62  1.5.12.2  snj 
     63  1.5.12.2  snj 	/* init state */
     64  1.5.12.2  snj 	h0 = 0;
     65  1.5.12.2  snj 	h1 = 0;
     66  1.5.12.2  snj 	h2 = 0;
     67  1.5.12.2  snj 	h3 = 0;
     68  1.5.12.2  snj 	h4 = 0;
     69  1.5.12.2  snj 
     70  1.5.12.2  snj 	/* full blocks */
     71  1.5.12.2  snj 	if (inlen < 16) goto poly1305_donna_atmost15bytes;
     72  1.5.12.2  snj poly1305_donna_16bytes:
     73  1.5.12.2  snj 	m += 16;
     74  1.5.12.2  snj 	inlen -= 16;
     75  1.5.12.2  snj 
     76  1.5.12.2  snj 	t0 = U8TO32_LE(m-16);
     77  1.5.12.2  snj 	t1 = U8TO32_LE(m-12);
     78  1.5.12.2  snj 	t2 = U8TO32_LE(m-8);
     79  1.5.12.2  snj 	t3 = U8TO32_LE(m-4);
     80  1.5.12.2  snj 
     81  1.5.12.2  snj 	h0 += t0 & 0x3ffffff;
     82  1.5.12.2  snj 	h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff;
     83  1.5.12.2  snj 	h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff;
     84  1.5.12.2  snj 	h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff;
     85  1.5.12.2  snj 	h4 += (t3 >> 8) | (1 << 24);
     86  1.5.12.2  snj 
     87  1.5.12.2  snj 
     88  1.5.12.2  snj poly1305_donna_mul:
     89  1.5.12.2  snj 	t[0]  = mul32x32_64(h0,r0) + mul32x32_64(h1,s4) + mul32x32_64(h2,s3) + mul32x32_64(h3,s2) + mul32x32_64(h4,s1);
     90  1.5.12.2  snj 	t[1]  = mul32x32_64(h0,r1) + mul32x32_64(h1,r0) + mul32x32_64(h2,s4) + mul32x32_64(h3,s3) + mul32x32_64(h4,s2);
     91  1.5.12.2  snj 	t[2]  = mul32x32_64(h0,r2) + mul32x32_64(h1,r1) + mul32x32_64(h2,r0) + mul32x32_64(h3,s4) + mul32x32_64(h4,s3);
     92  1.5.12.2  snj 	t[3]  = mul32x32_64(h0,r3) + mul32x32_64(h1,r2) + mul32x32_64(h2,r1) + mul32x32_64(h3,r0) + mul32x32_64(h4,s4);
     93  1.5.12.2  snj 	t[4]  = mul32x32_64(h0,r4) + mul32x32_64(h1,r3) + mul32x32_64(h2,r2) + mul32x32_64(h3,r1) + mul32x32_64(h4,r0);
     94  1.5.12.2  snj 
     95  1.5.12.2  snj 	                h0 = (uint32_t)t[0] & 0x3ffffff; c =           (t[0] >> 26);
     96  1.5.12.2  snj 	t[1] += c;      h1 = (uint32_t)t[1] & 0x3ffffff; b = (uint32_t)(t[1] >> 26);
     97  1.5.12.2  snj 	t[2] += b;      h2 = (uint32_t)t[2] & 0x3ffffff; b = (uint32_t)(t[2] >> 26);
     98  1.5.12.2  snj 	t[3] += b;      h3 = (uint32_t)t[3] & 0x3ffffff; b = (uint32_t)(t[3] >> 26);
     99  1.5.12.2  snj 	t[4] += b;      h4 = (uint32_t)t[4] & 0x3ffffff; b = (uint32_t)(t[4] >> 26);
    100  1.5.12.2  snj 	h0 += b * 5;
    101  1.5.12.2  snj 
    102  1.5.12.2  snj 	if (inlen >= 16) goto poly1305_donna_16bytes;
    103  1.5.12.2  snj 
    104  1.5.12.2  snj 	/* final bytes */
    105  1.5.12.2  snj poly1305_donna_atmost15bytes:
    106  1.5.12.2  snj 	if (!inlen) goto poly1305_donna_finish;
    107  1.5.12.2  snj 
    108  1.5.12.2  snj 	for (j = 0; j < inlen; j++) mp[j] = m[j];
    109  1.5.12.2  snj 	mp[j++] = 1;
    110  1.5.12.2  snj 	for (; j < 16; j++)	mp[j] = 0;
    111  1.5.12.2  snj 	inlen = 0;
    112  1.5.12.2  snj 
    113  1.5.12.2  snj 	t0 = U8TO32_LE(mp+0);
    114  1.5.12.2  snj 	t1 = U8TO32_LE(mp+4);
    115  1.5.12.2  snj 	t2 = U8TO32_LE(mp+8);
    116  1.5.12.2  snj 	t3 = U8TO32_LE(mp+12);
    117  1.5.12.2  snj 
    118  1.5.12.2  snj 	h0 += t0 & 0x3ffffff;
    119  1.5.12.2  snj 	h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff;
    120  1.5.12.2  snj 	h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff;
    121  1.5.12.2  snj 	h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff;
    122  1.5.12.2  snj 	h4 += (t3 >> 8);
    123  1.5.12.2  snj 
    124  1.5.12.2  snj 	goto poly1305_donna_mul;
    125  1.5.12.2  snj 
    126  1.5.12.2  snj poly1305_donna_finish:
    127  1.5.12.2  snj 	             b = h0 >> 26; h0 = h0 & 0x3ffffff;
    128  1.5.12.2  snj 	h1 +=     b; b = h1 >> 26; h1 = h1 & 0x3ffffff;
    129  1.5.12.2  snj 	h2 +=     b; b = h2 >> 26; h2 = h2 & 0x3ffffff;
    130  1.5.12.2  snj 	h3 +=     b; b = h3 >> 26; h3 = h3 & 0x3ffffff;
    131  1.5.12.2  snj 	h4 +=     b; b = h4 >> 26; h4 = h4 & 0x3ffffff;
    132  1.5.12.2  snj 	h0 += b * 5; b = h0 >> 26; h0 = h0 & 0x3ffffff;
    133  1.5.12.2  snj 	h1 +=     b;
    134  1.5.12.2  snj 
    135  1.5.12.2  snj 	g0 = h0 + 5; b = g0 >> 26; g0 &= 0x3ffffff;
    136  1.5.12.2  snj 	g1 = h1 + b; b = g1 >> 26; g1 &= 0x3ffffff;
    137  1.5.12.2  snj 	g2 = h2 + b; b = g2 >> 26; g2 &= 0x3ffffff;
    138  1.5.12.2  snj 	g3 = h3 + b; b = g3 >> 26; g3 &= 0x3ffffff;
    139  1.5.12.2  snj 	g4 = h4 + b - (1 << 26);
    140  1.5.12.2  snj 
    141  1.5.12.2  snj 	b = (g4 >> 31) - 1;
    142  1.5.12.2  snj 	nb = ~b;
    143  1.5.12.2  snj 	h0 = (h0 & nb) | (g0 & b);
    144  1.5.12.2  snj 	h1 = (h1 & nb) | (g1 & b);
    145  1.5.12.2  snj 	h2 = (h2 & nb) | (g2 & b);
    146  1.5.12.2  snj 	h3 = (h3 & nb) | (g3 & b);
    147  1.5.12.2  snj 	h4 = (h4 & nb) | (g4 & b);
    148  1.5.12.2  snj 
    149  1.5.12.2  snj 	f0 = ((h0      ) | (h1 << 26)) + (uint64_t)U8TO32_LE(&key[16]);
    150  1.5.12.2  snj 	f1 = ((h1 >>  6) | (h2 << 20)) + (uint64_t)U8TO32_LE(&key[20]);
    151  1.5.12.2  snj 	f2 = ((h2 >> 12) | (h3 << 14)) + (uint64_t)U8TO32_LE(&key[24]);
    152  1.5.12.2  snj 	f3 = ((h3 >> 18) | (h4 <<  8)) + (uint64_t)U8TO32_LE(&key[28]);
    153  1.5.12.2  snj 
    154  1.5.12.2  snj 	U32TO8_LE(&out[ 0], f0); f1 += (f0 >> 32);
    155  1.5.12.2  snj 	U32TO8_LE(&out[ 4], f1); f2 += (f1 >> 32);
    156  1.5.12.2  snj 	U32TO8_LE(&out[ 8], f2); f3 += (f2 >> 32);
    157  1.5.12.2  snj 	U32TO8_LE(&out[12], f3);
    158  1.5.12.2  snj }
    159