1261287Sdes/* 2261287Sdes * Public Domain poly1305 from Andrew Moon 3261287Sdes * poly1305-donna-unrolled.c from https://github.com/floodyberry/poly1305-donna 4261287Sdes */ 5261287Sdes 6261287Sdes/* $OpenBSD: poly1305.c,v 1.3 2013/12/19 22:57:13 djm Exp $ */ 7261287Sdes 8261287Sdes#include "includes.h" 9261287Sdes 10261287Sdes#include <sys/types.h> 11261287Sdes#ifdef HAVE_STDINT_H 12261287Sdes# include <stdint.h> 13261287Sdes#endif 14261287Sdes 15261287Sdes#include "poly1305.h" 16261287Sdes 17261287Sdes#define mul32x32_64(a,b) ((uint64_t)(a) * (b)) 18261287Sdes 19261287Sdes#define U8TO32_LE(p) \ 20261287Sdes (((uint32_t)((p)[0])) | \ 21261287Sdes ((uint32_t)((p)[1]) << 8) | \ 22261287Sdes ((uint32_t)((p)[2]) << 16) | \ 23261287Sdes ((uint32_t)((p)[3]) << 24)) 24261287Sdes 25261287Sdes#define U32TO8_LE(p, v) \ 26261287Sdes do { \ 27261287Sdes (p)[0] = (uint8_t)((v)); \ 28261287Sdes (p)[1] = (uint8_t)((v) >> 8); \ 29261287Sdes (p)[2] = (uint8_t)((v) >> 16); \ 30261287Sdes (p)[3] = (uint8_t)((v) >> 24); \ 31261287Sdes } while (0) 32261287Sdes 33261287Sdesvoid 34261287Sdespoly1305_auth(unsigned char out[POLY1305_TAGLEN], const unsigned char *m, size_t inlen, const unsigned char key[POLY1305_KEYLEN]) { 35261287Sdes uint32_t t0,t1,t2,t3; 36261287Sdes uint32_t h0,h1,h2,h3,h4; 37261287Sdes uint32_t r0,r1,r2,r3,r4; 38261287Sdes uint32_t s1,s2,s3,s4; 39261287Sdes uint32_t b, nb; 40261287Sdes size_t j; 41261287Sdes uint64_t t[5]; 42261287Sdes uint64_t f0,f1,f2,f3; 43261287Sdes uint32_t g0,g1,g2,g3,g4; 44261287Sdes uint64_t c; 45261287Sdes unsigned char mp[16]; 46261287Sdes 47261287Sdes /* clamp key */ 48261287Sdes t0 = U8TO32_LE(key+0); 49261287Sdes t1 = U8TO32_LE(key+4); 50261287Sdes t2 = U8TO32_LE(key+8); 51261287Sdes t3 = U8TO32_LE(key+12); 52261287Sdes 53261287Sdes /* precompute multipliers */ 54261287Sdes r0 = t0 & 0x3ffffff; t0 >>= 26; t0 |= t1 << 6; 55261287Sdes r1 = t0 & 0x3ffff03; t1 >>= 20; t1 |= t2 << 12; 56261287Sdes r2 = t1 & 0x3ffc0ff; t2 >>= 14; t2 |= t3 << 18; 57261287Sdes r3 = t2 & 0x3f03fff; t3 >>= 8; 58261287Sdes r4 = t3 & 0x00fffff; 59261287Sdes 60261287Sdes s1 = r1 * 5; 61261287Sdes s2 = r2 * 5; 62261287Sdes s3 = r3 * 5; 63261287Sdes s4 = r4 * 5; 64261287Sdes 65261287Sdes /* init state */ 66261287Sdes h0 = 0; 67261287Sdes h1 = 0; 68261287Sdes h2 = 0; 69261287Sdes h3 = 0; 70261287Sdes h4 = 0; 71261287Sdes 72261287Sdes /* full blocks */ 73261287Sdes if (inlen < 16) goto poly1305_donna_atmost15bytes; 74261287Sdespoly1305_donna_16bytes: 75261287Sdes m += 16; 76261287Sdes inlen -= 16; 77261287Sdes 78261287Sdes t0 = U8TO32_LE(m-16); 79261287Sdes t1 = U8TO32_LE(m-12); 80261287Sdes t2 = U8TO32_LE(m-8); 81261287Sdes t3 = U8TO32_LE(m-4); 82261287Sdes 83261287Sdes h0 += t0 & 0x3ffffff; 84261287Sdes h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff; 85261287Sdes h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff; 86261287Sdes h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff; 87261287Sdes h4 += (t3 >> 8) | (1 << 24); 88261287Sdes 89261287Sdes 90261287Sdespoly1305_donna_mul: 91261287Sdes t[0] = mul32x32_64(h0,r0) + mul32x32_64(h1,s4) + mul32x32_64(h2,s3) + mul32x32_64(h3,s2) + mul32x32_64(h4,s1); 92261287Sdes t[1] = mul32x32_64(h0,r1) + mul32x32_64(h1,r0) + mul32x32_64(h2,s4) + mul32x32_64(h3,s3) + mul32x32_64(h4,s2); 93261287Sdes t[2] = mul32x32_64(h0,r2) + mul32x32_64(h1,r1) + mul32x32_64(h2,r0) + mul32x32_64(h3,s4) + mul32x32_64(h4,s3); 94261287Sdes t[3] = mul32x32_64(h0,r3) + mul32x32_64(h1,r2) + mul32x32_64(h2,r1) + mul32x32_64(h3,r0) + mul32x32_64(h4,s4); 95261287Sdes t[4] = mul32x32_64(h0,r4) + mul32x32_64(h1,r3) + mul32x32_64(h2,r2) + mul32x32_64(h3,r1) + mul32x32_64(h4,r0); 96261287Sdes 97261287Sdes h0 = (uint32_t)t[0] & 0x3ffffff; c = (t[0] >> 26); 98261287Sdes t[1] += c; h1 = (uint32_t)t[1] & 0x3ffffff; b = (uint32_t)(t[1] >> 26); 99261287Sdes t[2] += b; h2 = (uint32_t)t[2] & 0x3ffffff; b = (uint32_t)(t[2] >> 26); 100261287Sdes t[3] += b; h3 = (uint32_t)t[3] & 0x3ffffff; b = (uint32_t)(t[3] >> 26); 101261287Sdes t[4] += b; h4 = (uint32_t)t[4] & 0x3ffffff; b = (uint32_t)(t[4] >> 26); 102261287Sdes h0 += b * 5; 103261287Sdes 104261287Sdes if (inlen >= 16) goto poly1305_donna_16bytes; 105261287Sdes 106261287Sdes /* final bytes */ 107261287Sdespoly1305_donna_atmost15bytes: 108261287Sdes if (!inlen) goto poly1305_donna_finish; 109261287Sdes 110261287Sdes for (j = 0; j < inlen; j++) mp[j] = m[j]; 111261287Sdes mp[j++] = 1; 112261287Sdes for (; j < 16; j++) mp[j] = 0; 113261287Sdes inlen = 0; 114261287Sdes 115261287Sdes t0 = U8TO32_LE(mp+0); 116261287Sdes t1 = U8TO32_LE(mp+4); 117261287Sdes t2 = U8TO32_LE(mp+8); 118261287Sdes t3 = U8TO32_LE(mp+12); 119261287Sdes 120261287Sdes h0 += t0 & 0x3ffffff; 121261287Sdes h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff; 122261287Sdes h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff; 123261287Sdes h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff; 124261287Sdes h4 += (t3 >> 8); 125261287Sdes 126261287Sdes goto poly1305_donna_mul; 127261287Sdes 128261287Sdespoly1305_donna_finish: 129261287Sdes b = h0 >> 26; h0 = h0 & 0x3ffffff; 130261287Sdes h1 += b; b = h1 >> 26; h1 = h1 & 0x3ffffff; 131261287Sdes h2 += b; b = h2 >> 26; h2 = h2 & 0x3ffffff; 132261287Sdes h3 += b; b = h3 >> 26; h3 = h3 & 0x3ffffff; 133261287Sdes h4 += b; b = h4 >> 26; h4 = h4 & 0x3ffffff; 134261287Sdes h0 += b * 5; b = h0 >> 26; h0 = h0 & 0x3ffffff; 135261287Sdes h1 += b; 136261287Sdes 137261287Sdes g0 = h0 + 5; b = g0 >> 26; g0 &= 0x3ffffff; 138261287Sdes g1 = h1 + b; b = g1 >> 26; g1 &= 0x3ffffff; 139261287Sdes g2 = h2 + b; b = g2 >> 26; g2 &= 0x3ffffff; 140261287Sdes g3 = h3 + b; b = g3 >> 26; g3 &= 0x3ffffff; 141261287Sdes g4 = h4 + b - (1 << 26); 142261287Sdes 143261287Sdes b = (g4 >> 31) - 1; 144261287Sdes nb = ~b; 145261287Sdes h0 = (h0 & nb) | (g0 & b); 146261287Sdes h1 = (h1 & nb) | (g1 & b); 147261287Sdes h2 = (h2 & nb) | (g2 & b); 148261287Sdes h3 = (h3 & nb) | (g3 & b); 149261287Sdes h4 = (h4 & nb) | (g4 & b); 150261287Sdes 151261287Sdes f0 = ((h0 ) | (h1 << 26)) + (uint64_t)U8TO32_LE(&key[16]); 152261287Sdes f1 = ((h1 >> 6) | (h2 << 20)) + (uint64_t)U8TO32_LE(&key[20]); 153261287Sdes f2 = ((h2 >> 12) | (h3 << 14)) + (uint64_t)U8TO32_LE(&key[24]); 154261287Sdes f3 = ((h3 >> 18) | (h4 << 8)) + (uint64_t)U8TO32_LE(&key[28]); 155261287Sdes 156261287Sdes U32TO8_LE(&out[ 0], f0); f1 += (f0 >> 32); 157261287Sdes U32TO8_LE(&out[ 4], f1); f2 += (f1 >> 32); 158261287Sdes U32TO8_LE(&out[ 8], f2); f3 += (f2 >> 32); 159261287Sdes U32TO8_LE(&out[12], f3); 160261287Sdes} 161