andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* This implementation of poly1305 is by Andrew Moon andre@0: * (https://github.com/floodyberry/poly1305-donna) and released as public andre@0: * domain. */ andre@0: andre@0: #include andre@0: #include andre@0: andre@0: #include "poly1305.h" andre@0: andre@0: #if defined(NSS_X86) || defined(NSS_X64) andre@0: /* We can assume little-endian. */ andre@0: static uint32_t U8TO32_LE(const unsigned char *m) { andre@0: uint32_t r; andre@0: memcpy(&r, m, sizeof(r)); andre@0: return r; andre@0: } andre@0: andre@0: static void U32TO8_LE(unsigned char *m, uint32_t v) { andre@0: memcpy(m, &v, sizeof(v)); andre@0: } andre@0: #else andre@0: static uint32_t U8TO32_LE(const unsigned char *m) { andre@0: return (uint32_t)m[0] | andre@0: (uint32_t)m[1] << 8 | andre@0: (uint32_t)m[2] << 16 | andre@0: (uint32_t)m[3] << 24; andre@0: } andre@0: andre@0: static void U32TO8_LE(unsigned char *m, uint32_t v) { andre@0: m[0] = v; andre@0: m[1] = v >> 8; andre@0: m[2] = v >> 16; andre@0: m[3] = v >> 24; andre@0: } andre@0: #endif andre@0: andre@0: static uint64_t andre@0: mul32x32_64(uint32_t a, uint32_t b) { andre@0: return (uint64_t)a * b; andre@0: } andre@0: andre@0: struct poly1305_state_st { andre@0: uint32_t r0,r1,r2,r3,r4; andre@0: uint32_t s1,s2,s3,s4; andre@0: uint32_t h0,h1,h2,h3,h4; andre@0: unsigned char buf[16]; andre@0: unsigned int buf_used; andre@0: unsigned char key[16]; andre@0: }; andre@0: andre@0: /* update updates |state| given some amount of input data. This function may andre@0: * only be called with a |len| that is not a multiple of 16 at the end of the andre@0: * data. Otherwise the input must be buffered into 16 byte blocks. */ andre@0: static void update(struct poly1305_state_st *state, const unsigned char *in, andre@0: size_t len) { andre@0: uint32_t t0,t1,t2,t3; andre@0: uint64_t t[5]; andre@0: uint32_t b; andre@0: uint64_t c; andre@0: size_t j; andre@0: unsigned char mp[16]; andre@0: andre@0: if (len < 16) andre@0: goto poly1305_donna_atmost15bytes; andre@0: andre@0: poly1305_donna_16bytes: andre@0: t0 = U8TO32_LE(in); andre@0: t1 = U8TO32_LE(in+4); andre@0: t2 = U8TO32_LE(in+8); andre@0: t3 = U8TO32_LE(in+12); andre@0: andre@0: in += 16; andre@0: len -= 16; andre@0: andre@0: state->h0 += t0 & 0x3ffffff; andre@0: state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff; andre@0: state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff; andre@0: state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff; andre@0: state->h4 += (t3 >> 8) | (1 << 24); andre@0: andre@0: poly1305_donna_mul: andre@0: t[0] = mul32x32_64(state->h0,state->r0) + andre@0: mul32x32_64(state->h1,state->s4) + andre@0: mul32x32_64(state->h2,state->s3) + andre@0: mul32x32_64(state->h3,state->s2) + andre@0: mul32x32_64(state->h4,state->s1); andre@0: t[1] = mul32x32_64(state->h0,state->r1) + andre@0: mul32x32_64(state->h1,state->r0) + andre@0: mul32x32_64(state->h2,state->s4) + andre@0: mul32x32_64(state->h3,state->s3) + andre@0: mul32x32_64(state->h4,state->s2); andre@0: t[2] = mul32x32_64(state->h0,state->r2) + andre@0: mul32x32_64(state->h1,state->r1) + andre@0: mul32x32_64(state->h2,state->r0) + andre@0: mul32x32_64(state->h3,state->s4) + andre@0: mul32x32_64(state->h4,state->s3); andre@0: t[3] = mul32x32_64(state->h0,state->r3) + andre@0: mul32x32_64(state->h1,state->r2) + andre@0: mul32x32_64(state->h2,state->r1) + andre@0: mul32x32_64(state->h3,state->r0) + andre@0: mul32x32_64(state->h4,state->s4); andre@0: t[4] = mul32x32_64(state->h0,state->r4) + andre@0: mul32x32_64(state->h1,state->r3) + andre@0: mul32x32_64(state->h2,state->r2) + andre@0: mul32x32_64(state->h3,state->r1) + andre@0: mul32x32_64(state->h4,state->r0); andre@0: andre@0: state->h0 = (uint32_t)t[0] & 0x3ffffff; c = (t[0] >> 26); andre@0: t[1] += c; state->h1 = (uint32_t)t[1] & 0x3ffffff; b = (uint32_t)(t[1] >> 26); andre@0: t[2] += b; state->h2 = (uint32_t)t[2] & 0x3ffffff; b = (uint32_t)(t[2] >> 26); andre@0: t[3] += b; state->h3 = (uint32_t)t[3] & 0x3ffffff; b = (uint32_t)(t[3] >> 26); andre@0: t[4] += b; state->h4 = (uint32_t)t[4] & 0x3ffffff; b = (uint32_t)(t[4] >> 26); andre@0: state->h0 += b * 5; andre@0: andre@0: if (len >= 16) andre@0: goto poly1305_donna_16bytes; andre@0: andre@0: /* final bytes */ andre@0: poly1305_donna_atmost15bytes: andre@0: if (!len) andre@0: return; andre@0: andre@0: for (j = 0; j < len; j++) andre@0: mp[j] = in[j]; andre@0: mp[j++] = 1; andre@0: for (; j < 16; j++) andre@0: mp[j] = 0; andre@0: len = 0; andre@0: andre@0: t0 = U8TO32_LE(mp+0); andre@0: t1 = U8TO32_LE(mp+4); andre@0: t2 = U8TO32_LE(mp+8); andre@0: t3 = U8TO32_LE(mp+12); andre@0: andre@0: state->h0 += t0 & 0x3ffffff; andre@0: state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff; andre@0: state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff; andre@0: state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff; andre@0: state->h4 += (t3 >> 8); andre@0: andre@0: goto poly1305_donna_mul; andre@0: } andre@0: andre@0: void Poly1305Init(poly1305_state *statep, const unsigned char key[32]) { andre@0: struct poly1305_state_st *state = (struct poly1305_state_st*) statep; andre@0: uint32_t t0,t1,t2,t3; andre@0: andre@0: t0 = U8TO32_LE(key+0); andre@0: t1 = U8TO32_LE(key+4); andre@0: t2 = U8TO32_LE(key+8); andre@0: t3 = U8TO32_LE(key+12); andre@0: andre@0: /* precompute multipliers */ andre@0: state->r0 = t0 & 0x3ffffff; t0 >>= 26; t0 |= t1 << 6; andre@0: state->r1 = t0 & 0x3ffff03; t1 >>= 20; t1 |= t2 << 12; andre@0: state->r2 = t1 & 0x3ffc0ff; t2 >>= 14; t2 |= t3 << 18; andre@0: state->r3 = t2 & 0x3f03fff; t3 >>= 8; andre@0: state->r4 = t3 & 0x00fffff; andre@0: andre@0: state->s1 = state->r1 * 5; andre@0: state->s2 = state->r2 * 5; andre@0: state->s3 = state->r3 * 5; andre@0: state->s4 = state->r4 * 5; andre@0: andre@0: /* init state */ andre@0: state->h0 = 0; andre@0: state->h1 = 0; andre@0: state->h2 = 0; andre@0: state->h3 = 0; andre@0: state->h4 = 0; andre@0: andre@0: state->buf_used = 0; andre@0: memcpy(state->key, key + 16, sizeof(state->key)); andre@0: } andre@0: andre@0: void Poly1305Update(poly1305_state *statep, const unsigned char *in, andre@0: size_t in_len) { andre@0: unsigned int i; andre@0: struct poly1305_state_st *state = (struct poly1305_state_st*) statep; andre@0: andre@0: if (state->buf_used) { andre@0: unsigned int todo = 16 - state->buf_used; andre@0: if (todo > in_len) andre@0: todo = in_len; andre@0: for (i = 0; i < todo; i++) andre@0: state->buf[state->buf_used + i] = in[i]; andre@0: state->buf_used += todo; andre@0: in_len -= todo; andre@0: in += todo; andre@0: andre@0: if (state->buf_used == 16) { andre@0: update(state, state->buf, 16); andre@0: state->buf_used = 0; andre@0: } andre@0: } andre@0: andre@0: if (in_len >= 16) { andre@0: size_t todo = in_len & ~0xf; andre@0: update(state, in, todo); andre@0: in += todo; andre@0: in_len &= 0xf; andre@0: } andre@0: andre@0: if (in_len) { andre@0: for (i = 0; i < in_len; i++) andre@0: state->buf[i] = in[i]; andre@0: state->buf_used = in_len; andre@0: } andre@0: } andre@0: andre@0: void Poly1305Finish(poly1305_state *statep, unsigned char mac[16]) { andre@0: struct poly1305_state_st *state = (struct poly1305_state_st*) statep; andre@0: uint64_t f0,f1,f2,f3; andre@0: uint32_t g0,g1,g2,g3,g4; andre@0: uint32_t b, nb; andre@0: andre@0: if (state->buf_used) andre@0: update(state, state->buf, state->buf_used); andre@0: andre@0: b = state->h0 >> 26; state->h0 = state->h0 & 0x3ffffff; andre@0: state->h1 += b; b = state->h1 >> 26; state->h1 = state->h1 & 0x3ffffff; andre@0: state->h2 += b; b = state->h2 >> 26; state->h2 = state->h2 & 0x3ffffff; andre@0: state->h3 += b; b = state->h3 >> 26; state->h3 = state->h3 & 0x3ffffff; andre@0: state->h4 += b; b = state->h4 >> 26; state->h4 = state->h4 & 0x3ffffff; andre@0: state->h0 += b * 5; andre@0: andre@0: g0 = state->h0 + 5; b = g0 >> 26; g0 &= 0x3ffffff; andre@0: g1 = state->h1 + b; b = g1 >> 26; g1 &= 0x3ffffff; andre@0: g2 = state->h2 + b; b = g2 >> 26; g2 &= 0x3ffffff; andre@0: g3 = state->h3 + b; b = g3 >> 26; g3 &= 0x3ffffff; andre@0: g4 = state->h4 + b - (1 << 26); andre@0: andre@0: b = (g4 >> 31) - 1; andre@0: nb = ~b; andre@0: state->h0 = (state->h0 & nb) | (g0 & b); andre@0: state->h1 = (state->h1 & nb) | (g1 & b); andre@0: state->h2 = (state->h2 & nb) | (g2 & b); andre@0: state->h3 = (state->h3 & nb) | (g3 & b); andre@0: state->h4 = (state->h4 & nb) | (g4 & b); andre@0: andre@0: f0 = ((state->h0 ) | (state->h1 << 26)) + (uint64_t)U8TO32_LE(&state->key[0]); andre@0: f1 = ((state->h1 >> 6) | (state->h2 << 20)) + (uint64_t)U8TO32_LE(&state->key[4]); andre@0: f2 = ((state->h2 >> 12) | (state->h3 << 14)) + (uint64_t)U8TO32_LE(&state->key[8]); andre@0: f3 = ((state->h3 >> 18) | (state->h4 << 8)) + (uint64_t)U8TO32_LE(&state->key[12]); andre@0: andre@0: U32TO8_LE(&mac[ 0], f0); f1 += (f0 >> 32); andre@0: U32TO8_LE(&mac[ 4], f1); f2 += (f1 >> 32); andre@0: U32TO8_LE(&mac[ 8], f2); f3 += (f2 >> 32); andre@0: U32TO8_LE(&mac[12], f3); andre@0: }