andre@0: diff -r c3565a90b8c4 lib/freebl/blapi.h andre@0: --- a/lib/freebl/blapi.h Fri Jan 03 20:59:10 2014 +0100 andre@0: +++ b/lib/freebl/blapi.h Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -986,6 +986,38 @@ andre@0: unsigned int *outputLen, unsigned int maxOutputLen, andre@0: const unsigned char *input, unsigned int inputLen); andre@0: andre@0: +/******************************************/ andre@0: +/* andre@0: +** ChaCha20+Poly1305 AEAD andre@0: +*/ andre@0: + andre@0: +extern SECStatus andre@0: +ChaCha20Poly1305_InitContext(ChaCha20Poly1305Context *ctx, andre@0: + const unsigned char *key, unsigned int keyLen, andre@0: + unsigned int tagLen); andre@0: + andre@0: +extern ChaCha20Poly1305Context * andre@0: +ChaCha20Poly1305_CreateContext(const unsigned char *key, unsigned int keyLen, andre@0: + unsigned int tagLen); andre@0: + andre@0: +extern void andre@0: +ChaCha20Poly1305_DestroyContext(ChaCha20Poly1305Context *ctx, PRBool freeit); andre@0: + andre@0: +extern SECStatus andre@0: +ChaCha20Poly1305_Seal(const ChaCha20Poly1305Context *ctx, andre@0: + unsigned char *output, unsigned int *outputLen, andre@0: + unsigned int maxOutputLen, andre@0: + const unsigned char *input, unsigned int inputLen, andre@0: + const unsigned char *nonce, unsigned int nonceLen, andre@0: + const unsigned char *ad, unsigned int adLen); andre@0: + andre@0: +extern SECStatus andre@0: +ChaCha20Poly1305_Open(const ChaCha20Poly1305Context *ctx, andre@0: + unsigned char *output, unsigned int *outputLen, andre@0: + unsigned int maxOutputLen, andre@0: + const unsigned char *input, unsigned int inputLen, andre@0: + const unsigned char *nonce, unsigned int nonceLen, andre@0: + const unsigned char *ad, unsigned int adLen); andre@0: andre@0: /******************************************/ andre@0: /* andre@0: diff -r c3565a90b8c4 lib/freebl/blapit.h andre@0: --- a/lib/freebl/blapit.h Fri Jan 03 20:59:10 2014 +0100 andre@0: +++ b/lib/freebl/blapit.h Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -222,6 +222,7 @@ andre@0: struct SHA512ContextStr ; andre@0: struct AESKeyWrapContextStr ; andre@0: struct SEEDContextStr ; andre@0: +struct ChaCha20Poly1305ContextStr; andre@0: andre@0: typedef struct DESContextStr DESContext; andre@0: typedef struct RC2ContextStr RC2Context; andre@0: @@ -240,6 +241,7 @@ andre@0: typedef struct SHA512ContextStr SHA384Context; andre@0: typedef struct AESKeyWrapContextStr AESKeyWrapContext; andre@0: typedef struct SEEDContextStr SEEDContext; andre@0: +typedef struct ChaCha20Poly1305ContextStr ChaCha20Poly1305Context; andre@0: andre@0: /*************************************************************************** andre@0: ** RSA Public and Private Key structures andre@0: diff -r c3565a90b8c4 lib/freebl/chacha20/chacha20.c andre@0: --- /dev/null Thu Jan 01 00:00:00 1970 +0000 andre@0: +++ b/lib/freebl/chacha20/chacha20.c Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -0,0 +1,108 @@ 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: +/* Adopted from the public domain code in NaCl by djb. */ andre@0: + andre@0: +#include andre@0: +#include andre@0: + andre@0: +#include "prtypes.h" andre@0: +#include "chacha20.h" andre@0: + andre@0: +#define ROTL32(v, n) (((v) << (n)) | ((v) >> (32 - (n)))) andre@0: +#define ROTATE(v, c) ROTL32((v), (c)) andre@0: +#define XOR(v, w) ((v) ^ (w)) andre@0: +#define PLUS(x, y) ((x) + (y)) andre@0: + andre@0: +#define U32TO8_LITTLE(p, v) \ andre@0: + { (p)[0] = ((v) ) & 0xff; (p)[1] = ((v) >> 8) & 0xff; \ andre@0: + (p)[2] = ((v) >> 16) & 0xff; (p)[3] = ((v) >> 24) & 0xff; } andre@0: +#define U8TO32_LITTLE(p) \ andre@0: + (((PRUint32)((p)[0]) ) | ((PRUint32)((p)[1]) << 8) | \ andre@0: + ((PRUint32)((p)[2]) << 16) | ((PRUint32)((p)[3]) << 24) ) andre@0: + andre@0: +#define QUARTERROUND(a,b,c,d) \ andre@0: + x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \ andre@0: + x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \ andre@0: + x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \ andre@0: + x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7); andre@0: + andre@0: +static void ChaChaCore(unsigned char output[64], const PRUint32 input[16], andre@0: + int num_rounds) { andre@0: + PRUint32 x[16]; andre@0: + int i; andre@0: + andre@0: + memcpy(x, input, sizeof(PRUint32) * 16); andre@0: + for (i = num_rounds; i > 0; i -= 2) { andre@0: + QUARTERROUND( 0, 4, 8,12) andre@0: + QUARTERROUND( 1, 5, 9,13) andre@0: + QUARTERROUND( 2, 6,10,14) andre@0: + QUARTERROUND( 3, 7,11,15) andre@0: + QUARTERROUND( 0, 5,10,15) andre@0: + QUARTERROUND( 1, 6,11,12) andre@0: + QUARTERROUND( 2, 7, 8,13) andre@0: + QUARTERROUND( 3, 4, 9,14) andre@0: + } andre@0: + andre@0: + for (i = 0; i < 16; ++i) { andre@0: + x[i] = PLUS(x[i], input[i]); andre@0: + } andre@0: + for (i = 0; i < 16; ++i) { andre@0: + U32TO8_LITTLE(output + 4 * i, x[i]); andre@0: + } andre@0: +} andre@0: + andre@0: +static const unsigned char sigma[16] = "expand 32-byte k"; andre@0: + andre@0: +void ChaCha20XOR(unsigned char *out, const unsigned char *in, unsigned int inLen, andre@0: + const unsigned char key[32], const unsigned char nonce[8], andre@0: + uint64_t counter) { andre@0: + unsigned char block[64]; andre@0: + PRUint32 input[16]; andre@0: + unsigned int u; andre@0: + unsigned int i; andre@0: + andre@0: + input[4] = U8TO32_LITTLE(key + 0); andre@0: + input[5] = U8TO32_LITTLE(key + 4); andre@0: + input[6] = U8TO32_LITTLE(key + 8); andre@0: + input[7] = U8TO32_LITTLE(key + 12); andre@0: + andre@0: + input[8] = U8TO32_LITTLE(key + 16); andre@0: + input[9] = U8TO32_LITTLE(key + 20); andre@0: + input[10] = U8TO32_LITTLE(key + 24); andre@0: + input[11] = U8TO32_LITTLE(key + 28); andre@0: + andre@0: + input[0] = U8TO32_LITTLE(sigma + 0); andre@0: + input[1] = U8TO32_LITTLE(sigma + 4); andre@0: + input[2] = U8TO32_LITTLE(sigma + 8); andre@0: + input[3] = U8TO32_LITTLE(sigma + 12); andre@0: + andre@0: + input[12] = counter; andre@0: + input[13] = counter >> 32; andre@0: + input[14] = U8TO32_LITTLE(nonce + 0); andre@0: + input[15] = U8TO32_LITTLE(nonce + 4); andre@0: + andre@0: + while (inLen >= 64) { andre@0: + ChaChaCore(block, input, 20); andre@0: + for (i = 0; i < 64; i++) { andre@0: + out[i] = in[i] ^ block[i]; andre@0: + } andre@0: + andre@0: + input[12]++; andre@0: + if (input[12] == 0) { andre@0: + input[13]++; andre@0: + } andre@0: + andre@0: + inLen -= 64; andre@0: + in += 64; andre@0: + out += 64; andre@0: + } andre@0: + andre@0: + if (inLen > 0) { andre@0: + ChaChaCore(block, input, 20); andre@0: + for (i = 0; i < inLen; i++) { andre@0: + out[i] = in[i] ^ block[i]; andre@0: + } andre@0: + } andre@0: +} andre@0: diff -r c3565a90b8c4 lib/freebl/chacha20/chacha20.h andre@0: --- /dev/null Thu Jan 01 00:00:00 1970 +0000 andre@0: +++ b/lib/freebl/chacha20/chacha20.h Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -0,0 +1,22 @@ andre@0: +/* andre@0: + * chacha20.h - header file for ChaCha20 implementation. andre@0: + * 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: +#ifndef FREEBL_CHACHA20_H_ andre@0: +#define FREEBL_CHACHA20_H_ andre@0: + andre@0: +#include andre@0: + andre@0: +/* ChaCha20XOR encrypts |inLen| bytes from |in| with the given key and andre@0: + * nonce and writes the result to |out|, which may be equal to |in|. The andre@0: + * initial block counter is specified by |counter|. */ andre@0: +extern void ChaCha20XOR(unsigned char *out, andre@0: + const unsigned char *in, unsigned int inLen, andre@0: + const unsigned char key[32], andre@0: + const unsigned char nonce[8], andre@0: + uint64_t counter); andre@0: + andre@0: +#endif /* FREEBL_CHACHA20_H_ */ andre@0: diff -r c3565a90b8c4 lib/freebl/chacha20/chacha20_vec.c andre@0: --- /dev/null Thu Jan 01 00:00:00 1970 +0000 andre@0: +++ b/lib/freebl/chacha20/chacha20_vec.c Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -0,0 +1,281 @@ 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 is by Ted Krovetz and was submitted to SUPERCOP and andre@0: + * marked as public domain. It was been altered to allow for non-aligned inputs andre@0: + * and to allow the block counter to be passed in specifically. */ andre@0: + andre@0: +#include andre@0: + andre@0: +#include "chacha20.h" andre@0: + andre@0: +#ifndef CHACHA_RNDS andre@0: +#define CHACHA_RNDS 20 /* 8 (high speed), 20 (conservative), 12 (middle) */ andre@0: +#endif andre@0: + andre@0: +/* Architecture-neutral way to specify 16-byte vector of ints */ andre@0: +typedef unsigned vec __attribute__ ((vector_size (16))); andre@0: + andre@0: +/* This implementation is designed for Neon, SSE and AltiVec machines. The andre@0: + * following specify how to do certain vector operations efficiently on andre@0: + * each architecture, using intrinsics. andre@0: + * This implementation supports parallel processing of multiple blocks, andre@0: + * including potentially using general-purpose registers. andre@0: + */ andre@0: +#if __ARM_NEON__ andre@0: +#include andre@0: +#define GPR_TOO 1 andre@0: +#define VBPI 2 andre@0: +#define ONE (vec)vsetq_lane_u32(1,vdupq_n_u32(0),0) andre@0: +#define LOAD(m) (vec)(*((vec*)(m))) andre@0: +#define STORE(m,r) (*((vec*)(m))) = (r) andre@0: +#define ROTV1(x) (vec)vextq_u32((uint32x4_t)x,(uint32x4_t)x,1) andre@0: +#define ROTV2(x) (vec)vextq_u32((uint32x4_t)x,(uint32x4_t)x,2) andre@0: +#define ROTV3(x) (vec)vextq_u32((uint32x4_t)x,(uint32x4_t)x,3) andre@0: +#define ROTW16(x) (vec)vrev32q_u16((uint16x8_t)x) andre@0: +#if __clang__ andre@0: +#define ROTW7(x) (x << ((vec){ 7, 7, 7, 7})) ^ (x >> ((vec){25,25,25,25})) andre@0: +#define ROTW8(x) (x << ((vec){ 8, 8, 8, 8})) ^ (x >> ((vec){24,24,24,24})) andre@0: +#define ROTW12(x) (x << ((vec){12,12,12,12})) ^ (x >> ((vec){20,20,20,20})) andre@0: +#else andre@0: +#define ROTW7(x) (vec)vsriq_n_u32(vshlq_n_u32((uint32x4_t)x,7),(uint32x4_t)x,25) andre@0: +#define ROTW8(x) (vec)vsriq_n_u32(vshlq_n_u32((uint32x4_t)x,8),(uint32x4_t)x,24) andre@0: +#define ROTW12(x) (vec)vsriq_n_u32(vshlq_n_u32((uint32x4_t)x,12),(uint32x4_t)x,20) andre@0: +#endif andre@0: +#elif __SSE2__ andre@0: +#include andre@0: +#define GPR_TOO 0 andre@0: +#if __clang__ andre@0: +#define VBPI 4 andre@0: +#else andre@0: +#define VBPI 3 andre@0: +#endif andre@0: +#define ONE (vec)_mm_set_epi32(0,0,0,1) andre@0: +#define LOAD(m) (vec)_mm_loadu_si128((__m128i*)(m)) andre@0: +#define STORE(m,r) _mm_storeu_si128((__m128i*)(m), (__m128i) (r)) andre@0: +#define ROTV1(x) (vec)_mm_shuffle_epi32((__m128i)x,_MM_SHUFFLE(0,3,2,1)) andre@0: +#define ROTV2(x) (vec)_mm_shuffle_epi32((__m128i)x,_MM_SHUFFLE(1,0,3,2)) andre@0: +#define ROTV3(x) (vec)_mm_shuffle_epi32((__m128i)x,_MM_SHUFFLE(2,1,0,3)) andre@0: +#define ROTW7(x) (vec)(_mm_slli_epi32((__m128i)x, 7) ^ _mm_srli_epi32((__m128i)x,25)) andre@0: +#define ROTW12(x) (vec)(_mm_slli_epi32((__m128i)x,12) ^ _mm_srli_epi32((__m128i)x,20)) andre@0: +#if __SSSE3__ andre@0: +#include andre@0: +#define ROTW8(x) (vec)_mm_shuffle_epi8((__m128i)x,_mm_set_epi8(14,13,12,15,10,9,8,11,6,5,4,7,2,1,0,3)) andre@0: +#define ROTW16(x) (vec)_mm_shuffle_epi8((__m128i)x,_mm_set_epi8(13,12,15,14,9,8,11,10,5,4,7,6,1,0,3,2)) andre@0: +#else andre@0: +#define ROTW8(x) (vec)(_mm_slli_epi32((__m128i)x, 8) ^ _mm_srli_epi32((__m128i)x,24)) andre@0: +#define ROTW16(x) (vec)(_mm_slli_epi32((__m128i)x,16) ^ _mm_srli_epi32((__m128i)x,16)) andre@0: +#endif andre@0: +#else andre@0: +#error -- Implementation supports only machines with neon or SSE2 andre@0: +#endif andre@0: + andre@0: +#ifndef REVV_BE andre@0: +#define REVV_BE(x) (x) andre@0: +#endif andre@0: + andre@0: +#ifndef REVW_BE andre@0: +#define REVW_BE(x) (x) andre@0: +#endif andre@0: + andre@0: +#define BPI (VBPI + GPR_TOO) /* Blocks computed per loop iteration */ andre@0: + andre@0: +#define DQROUND_VECTORS(a,b,c,d) \ andre@0: + a += b; d ^= a; d = ROTW16(d); \ andre@0: + c += d; b ^= c; b = ROTW12(b); \ andre@0: + a += b; d ^= a; d = ROTW8(d); \ andre@0: + c += d; b ^= c; b = ROTW7(b); \ andre@0: + b = ROTV1(b); c = ROTV2(c); d = ROTV3(d); \ andre@0: + a += b; d ^= a; d = ROTW16(d); \ andre@0: + c += d; b ^= c; b = ROTW12(b); \ andre@0: + a += b; d ^= a; d = ROTW8(d); \ andre@0: + c += d; b ^= c; b = ROTW7(b); \ andre@0: + b = ROTV3(b); c = ROTV2(c); d = ROTV1(d); andre@0: + andre@0: +#define QROUND_WORDS(a,b,c,d) \ andre@0: + a = a+b; d ^= a; d = d<<16 | d>>16; \ andre@0: + c = c+d; b ^= c; b = b<<12 | b>>20; \ andre@0: + a = a+b; d ^= a; d = d<< 8 | d>>24; \ andre@0: + c = c+d; b ^= c; b = b<< 7 | b>>25; andre@0: + andre@0: +#define WRITE_XOR(in, op, d, v0, v1, v2, v3) \ andre@0: + STORE(op + d + 0, LOAD(in + d + 0) ^ REVV_BE(v0)); \ andre@0: + STORE(op + d + 4, LOAD(in + d + 4) ^ REVV_BE(v1)); \ andre@0: + STORE(op + d + 8, LOAD(in + d + 8) ^ REVV_BE(v2)); \ andre@0: + STORE(op + d +12, LOAD(in + d +12) ^ REVV_BE(v3)); andre@0: + andre@0: +void ChaCha20XOR( andre@0: + unsigned char *out, andre@0: + const unsigned char *in, andre@0: + unsigned int inlen, andre@0: + const unsigned char key[32], andre@0: + const unsigned char nonce[8], andre@0: + uint64_t counter) andre@0: +{ andre@0: + unsigned iters, i, *op=(unsigned *)out, *ip=(unsigned *)in, *kp; andre@0: +#if defined(__ARM_NEON__) andre@0: + unsigned *np; andre@0: +#endif andre@0: + vec s0, s1, s2, s3; andre@0: +#if !defined(__ARM_NEON__) && !defined(__SSE2__) andre@0: + __attribute__ ((aligned (16))) unsigned key[8], nonce[4]; andre@0: +#endif andre@0: + __attribute__ ((aligned (16))) unsigned chacha_const[] = andre@0: + {0x61707865,0x3320646E,0x79622D32,0x6B206574}; andre@0: +#if defined(__ARM_NEON__) || defined(__SSE2__) andre@0: + kp = (unsigned *)key; andre@0: +#else andre@0: + ((vec *)key)[0] = REVV_BE(((vec *)key)[0]); andre@0: + ((vec *)key)[1] = REVV_BE(((vec *)key)[1]); andre@0: + nonce[0] = REVW_BE(((unsigned *)nonce)[0]); andre@0: + nonce[1] = REVW_BE(((unsigned *)nonce)[1]); andre@0: + nonce[2] = REVW_BE(((unsigned *)nonce)[2]); andre@0: + nonce[3] = REVW_BE(((unsigned *)nonce)[3]); andre@0: + kp = (unsigned *)key; andre@0: + np = (unsigned *)nonce; andre@0: +#endif andre@0: +#if defined(__ARM_NEON__) andre@0: + np = (unsigned*) nonce; andre@0: +#endif andre@0: + s0 = LOAD(chacha_const); andre@0: + s1 = LOAD(&((vec*)kp)[0]); andre@0: + s2 = LOAD(&((vec*)kp)[1]); andre@0: + s3 = (vec) { andre@0: + counter & 0xffffffff, andre@0: + counter >> 32, andre@0: + ((uint32_t*)nonce)[0], andre@0: + ((uint32_t*)nonce)[1] andre@0: + }; andre@0: + andre@0: + for (iters = 0; iters < inlen/(BPI*64); iters++) { andre@0: +#if GPR_TOO andre@0: + register unsigned x0, x1, x2, x3, x4, x5, x6, x7, x8, andre@0: + x9, x10, x11, x12, x13, x14, x15; andre@0: +#endif andre@0: +#if VBPI > 2 andre@0: + vec v8,v9,v10,v11; andre@0: +#endif andre@0: +#if VBPI > 3 andre@0: + vec v12,v13,v14,v15; andre@0: +#endif andre@0: + andre@0: + vec v0,v1,v2,v3,v4,v5,v6,v7; andre@0: + v4 = v0 = s0; v5 = v1 = s1; v6 = v2 = s2; v3 = s3; andre@0: + v7 = v3 + ONE; andre@0: +#if VBPI > 2 andre@0: + v8 = v4; v9 = v5; v10 = v6; andre@0: + v11 = v7 + ONE; andre@0: +#endif andre@0: +#if VBPI > 3 andre@0: + v12 = v8; v13 = v9; v14 = v10; andre@0: + v15 = v11 + ONE; andre@0: +#endif andre@0: +#if GPR_TOO andre@0: + x0 = chacha_const[0]; x1 = chacha_const[1]; andre@0: + x2 = chacha_const[2]; x3 = chacha_const[3]; andre@0: + x4 = kp[0]; x5 = kp[1]; x6 = kp[2]; x7 = kp[3]; andre@0: + x8 = kp[4]; x9 = kp[5]; x10 = kp[6]; x11 = kp[7]; andre@0: + x12 = (counter & 0xffffffff)+BPI*iters+(BPI-1); x13 = counter >> 32; andre@0: + x14 = np[0]; x15 = np[1]; andre@0: +#endif andre@0: + for (i = CHACHA_RNDS/2; i; i--) { andre@0: + DQROUND_VECTORS(v0,v1,v2,v3) andre@0: + DQROUND_VECTORS(v4,v5,v6,v7) andre@0: +#if VBPI > 2 andre@0: + DQROUND_VECTORS(v8,v9,v10,v11) andre@0: +#endif andre@0: +#if VBPI > 3 andre@0: + DQROUND_VECTORS(v12,v13,v14,v15) andre@0: +#endif andre@0: +#if GPR_TOO andre@0: + QROUND_WORDS( x0, x4, x8,x12) andre@0: + QROUND_WORDS( x1, x5, x9,x13) andre@0: + QROUND_WORDS( x2, x6,x10,x14) andre@0: + QROUND_WORDS( x3, x7,x11,x15) andre@0: + QROUND_WORDS( x0, x5,x10,x15) andre@0: + QROUND_WORDS( x1, x6,x11,x12) andre@0: + QROUND_WORDS( x2, x7, x8,x13) andre@0: + QROUND_WORDS( x3, x4, x9,x14) andre@0: +#endif andre@0: + } andre@0: + andre@0: + WRITE_XOR(ip, op, 0, v0+s0, v1+s1, v2+s2, v3+s3) andre@0: + s3 += ONE; andre@0: + WRITE_XOR(ip, op, 16, v4+s0, v5+s1, v6+s2, v7+s3) andre@0: + s3 += ONE; andre@0: +#if VBPI > 2 andre@0: + WRITE_XOR(ip, op, 32, v8+s0, v9+s1, v10+s2, v11+s3) andre@0: + s3 += ONE; andre@0: +#endif andre@0: +#if VBPI > 3 andre@0: + WRITE_XOR(ip, op, 48, v12+s0, v13+s1, v14+s2, v15+s3) andre@0: + s3 += ONE; andre@0: +#endif andre@0: + ip += VBPI*16; andre@0: + op += VBPI*16; andre@0: +#if GPR_TOO andre@0: + op[0] = REVW_BE(REVW_BE(ip[0]) ^ (x0 + chacha_const[0])); andre@0: + op[1] = REVW_BE(REVW_BE(ip[1]) ^ (x1 + chacha_const[1])); andre@0: + op[2] = REVW_BE(REVW_BE(ip[2]) ^ (x2 + chacha_const[2])); andre@0: + op[3] = REVW_BE(REVW_BE(ip[3]) ^ (x3 + chacha_const[3])); andre@0: + op[4] = REVW_BE(REVW_BE(ip[4]) ^ (x4 + kp[0])); andre@0: + op[5] = REVW_BE(REVW_BE(ip[5]) ^ (x5 + kp[1])); andre@0: + op[6] = REVW_BE(REVW_BE(ip[6]) ^ (x6 + kp[2])); andre@0: + op[7] = REVW_BE(REVW_BE(ip[7]) ^ (x7 + kp[3])); andre@0: + op[8] = REVW_BE(REVW_BE(ip[8]) ^ (x8 + kp[4])); andre@0: + op[9] = REVW_BE(REVW_BE(ip[9]) ^ (x9 + kp[5])); andre@0: + op[10] = REVW_BE(REVW_BE(ip[10]) ^ (x10 + kp[6])); andre@0: + op[11] = REVW_BE(REVW_BE(ip[11]) ^ (x11 + kp[7])); andre@0: + op[12] = REVW_BE(REVW_BE(ip[12]) ^ (x12 + (counter & 0xffffffff)+BPI*iters+(BPI-1))); andre@0: + op[13] = REVW_BE(REVW_BE(ip[13]) ^ (x13 + (counter >> 32))); andre@0: + op[14] = REVW_BE(REVW_BE(ip[14]) ^ (x14 + np[0])); andre@0: + op[15] = REVW_BE(REVW_BE(ip[15]) ^ (x15 + np[1])); andre@0: + s3 += ONE; andre@0: + ip += 16; andre@0: + op += 16; andre@0: +#endif andre@0: + } andre@0: + andre@0: + for (iters = inlen%(BPI*64)/64; iters != 0; iters--) { andre@0: + vec v0 = s0, v1 = s1, v2 = s2, v3 = s3; andre@0: + for (i = CHACHA_RNDS/2; i; i--) { andre@0: + DQROUND_VECTORS(v0,v1,v2,v3); andre@0: + } andre@0: + WRITE_XOR(ip, op, 0, v0+s0, v1+s1, v2+s2, v3+s3) andre@0: + s3 += ONE; andre@0: + ip += 16; andre@0: + op += 16; andre@0: + } andre@0: + andre@0: + inlen = inlen % 64; andre@0: + if (inlen) { andre@0: + __attribute__ ((aligned (16))) vec buf[4]; andre@0: + vec v0,v1,v2,v3; andre@0: + v0 = s0; v1 = s1; v2 = s2; v3 = s3; andre@0: + for (i = CHACHA_RNDS/2; i; i--) { andre@0: + DQROUND_VECTORS(v0,v1,v2,v3); andre@0: + } andre@0: + andre@0: + if (inlen >= 16) { andre@0: + STORE(op + 0, LOAD(ip + 0) ^ REVV_BE(v0 + s0)); andre@0: + if (inlen >= 32) { andre@0: + STORE(op + 4, LOAD(ip + 4) ^ REVV_BE(v1 + s1)); andre@0: + if (inlen >= 48) { andre@0: + STORE(op + 8, LOAD(ip + 8) ^ REVV_BE(v2 + s2)); andre@0: + buf[3] = REVV_BE(v3 + s3); andre@0: + } else { andre@0: + buf[2] = REVV_BE(v2 + s2); andre@0: + } andre@0: + } else { andre@0: + buf[1] = REVV_BE(v1 + s1); andre@0: + } andre@0: + } else { andre@0: + buf[0] = REVV_BE(v0 + s0); andre@0: + } andre@0: + andre@0: + for (i=inlen & ~15; i andre@0: +#include andre@0: + andre@0: +#include "seccomon.h" andre@0: +#include "secerr.h" andre@0: +#include "blapit.h" andre@0: +#include "poly1305/poly1305.h" andre@0: +#include "chacha20/chacha20.h" andre@0: +#include "chacha20poly1305.h" andre@0: + andre@0: +/* Poly1305Do writes the Poly1305 authenticator of the given additional data andre@0: + * and ciphertext to |out|. */ andre@0: +static void andre@0: +Poly1305Do(unsigned char *out, andre@0: + const unsigned char *ad, unsigned int adLen, andre@0: + const unsigned char *ciphertext, unsigned int ciphertextLen, andre@0: + const unsigned char key[32]) andre@0: +{ andre@0: + poly1305_state state; andre@0: + unsigned int j; andre@0: + unsigned char lengthBytes[8]; andre@0: + unsigned int i; andre@0: + andre@0: + Poly1305Init(&state, key); andre@0: + j = adLen; andre@0: + for (i = 0; i < sizeof(lengthBytes); i++) { andre@0: + lengthBytes[i] = j; andre@0: + j >>= 8; andre@0: + } andre@0: + Poly1305Update(&state, ad, adLen); andre@0: + Poly1305Update(&state, lengthBytes, sizeof(lengthBytes)); andre@0: + j = ciphertextLen; andre@0: + for (i = 0; i < sizeof(lengthBytes); i++) { andre@0: + lengthBytes[i] = j; andre@0: + j >>= 8; andre@0: + } andre@0: + Poly1305Update(&state, ciphertext, ciphertextLen); andre@0: + Poly1305Update(&state, lengthBytes, sizeof(lengthBytes)); andre@0: + Poly1305Finish(&state, out); andre@0: +} andre@0: + andre@0: +SECStatus andre@0: +ChaCha20Poly1305_InitContext(ChaCha20Poly1305Context *ctx, andre@0: + const unsigned char *key, unsigned int keyLen, andre@0: + unsigned int tagLen) andre@0: +{ andre@0: + if (keyLen != 32) { andre@0: + PORT_SetError(SEC_ERROR_BAD_KEY); andre@0: + return SECFailure; andre@0: + } andre@0: + if (tagLen == 0 || tagLen > 16) { andre@0: + PORT_SetError(SEC_ERROR_INPUT_LEN); andre@0: + return SECFailure; andre@0: + } andre@0: + andre@0: + memcpy(ctx->key, key, sizeof(ctx->key)); andre@0: + ctx->tagLen = tagLen; andre@0: + andre@0: + return SECSuccess; andre@0: +} andre@0: + andre@0: +ChaCha20Poly1305Context * andre@0: +ChaCha20Poly1305_CreateContext(const unsigned char *key, unsigned int keyLen, andre@0: + unsigned int tagLen) andre@0: +{ andre@0: + ChaCha20Poly1305Context *ctx; andre@0: + andre@0: + ctx = PORT_New(ChaCha20Poly1305Context); andre@0: + if (ctx == NULL) { andre@0: + return NULL; andre@0: + } andre@0: + andre@0: + if (ChaCha20Poly1305_InitContext(ctx, key, keyLen, tagLen) != SECSuccess) { andre@0: + PORT_Free(ctx); andre@0: + ctx = NULL; andre@0: + } andre@0: + andre@0: + return ctx; andre@0: +} andre@0: + andre@0: +void andre@0: +ChaCha20Poly1305_DestroyContext(ChaCha20Poly1305Context *ctx, PRBool freeit) andre@0: +{ andre@0: + memset(ctx, 0, sizeof(*ctx)); andre@0: + if (freeit) { andre@0: + PORT_Free(ctx); andre@0: + } andre@0: +} andre@0: + andre@0: +SECStatus andre@0: +ChaCha20Poly1305_Seal(const ChaCha20Poly1305Context *ctx, andre@0: + unsigned char *output, unsigned int *outputLen, andre@0: + unsigned int maxOutputLen, andre@0: + const unsigned char *input, unsigned int inputLen, andre@0: + const unsigned char *nonce, unsigned int nonceLen, andre@0: + const unsigned char *ad, unsigned int adLen) andre@0: +{ andre@0: + unsigned char block[64]; andre@0: + unsigned char tag[16]; andre@0: + andre@0: + if (nonceLen != 8) { andre@0: + PORT_SetError(SEC_ERROR_INPUT_LEN); andre@0: + return SECFailure; andre@0: + } andre@0: + *outputLen = inputLen + ctx->tagLen; andre@0: + if (maxOutputLen < *outputLen) { andre@0: + PORT_SetError(SEC_ERROR_OUTPUT_LEN); andre@0: + return SECFailure; andre@0: + } andre@0: + andre@0: + memset(block, 0, sizeof(block)); andre@0: + // Generate a block of keystream. The first 32 bytes will be the poly1305 andre@0: + // key. The remainder of the block is discarded. andre@0: + ChaCha20XOR(block, block, sizeof(block), ctx->key, nonce, 0); andre@0: + ChaCha20XOR(output, input, inputLen, ctx->key, nonce, 1); andre@0: + andre@0: + Poly1305Do(tag, ad, adLen, output, inputLen, block); andre@0: + memcpy(output + inputLen, tag, ctx->tagLen); andre@0: + andre@0: + return SECSuccess; andre@0: +} andre@0: + andre@0: +SECStatus andre@0: +ChaCha20Poly1305_Open(const ChaCha20Poly1305Context *ctx, andre@0: + unsigned char *output, unsigned int *outputLen, andre@0: + unsigned int maxOutputLen, andre@0: + const unsigned char *input, unsigned int inputLen, andre@0: + const unsigned char *nonce, unsigned int nonceLen, andre@0: + const unsigned char *ad, unsigned int adLen) andre@0: +{ andre@0: + unsigned char block[64]; andre@0: + unsigned char tag[16]; andre@0: + andre@0: + if (nonceLen != 8) { andre@0: + PORT_SetError(SEC_ERROR_INPUT_LEN); andre@0: + return SECFailure; andre@0: + } andre@0: + if (inputLen < ctx->tagLen) { andre@0: + PORT_SetError(SEC_ERROR_INPUT_LEN); andre@0: + return SECFailure; andre@0: + } andre@0: + *outputLen = inputLen - ctx->tagLen; andre@0: + if (maxOutputLen < *outputLen) { andre@0: + PORT_SetError(SEC_ERROR_OUTPUT_LEN); andre@0: + return SECFailure; andre@0: + } andre@0: + andre@0: + memset(block, 0, sizeof(block)); andre@0: + // Generate a block of keystream. The first 32 bytes will be the poly1305 andre@0: + // key. The remainder of the block is discarded. andre@0: + ChaCha20XOR(block, block, sizeof(block), ctx->key, nonce, 0); andre@0: + Poly1305Do(tag, ad, adLen, input, inputLen - ctx->tagLen, block); andre@0: + if (NSS_SecureMemcmp(tag, &input[inputLen - ctx->tagLen], ctx->tagLen) != 0) { andre@0: + PORT_SetError(SEC_ERROR_BAD_DATA); andre@0: + return SECFailure; andre@0: + } andre@0: + andre@0: + ChaCha20XOR(output, input, inputLen - ctx->tagLen, ctx->key, nonce, 1); andre@0: + andre@0: + return SECSuccess; andre@0: +} andre@0: diff -r c3565a90b8c4 lib/freebl/chacha20poly1305.h andre@0: --- /dev/null Thu Jan 01 00:00:00 1970 +0000 andre@0: +++ b/lib/freebl/chacha20poly1305.h Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -0,0 +1,15 @@ 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: +#ifndef _CHACHA20_POLY1305_H_ andre@0: +#define _CHACHA20_POLY1305_H_ 1 andre@0: + andre@0: +/* ChaCha20Poly1305ContextStr saves the key and tag length for a andre@0: + * ChaCha20+Poly1305 AEAD operation. */ andre@0: +struct ChaCha20Poly1305ContextStr { andre@0: + unsigned char key[32]; andre@0: + unsigned char tagLen; andre@0: +}; andre@0: + andre@0: +#endif /* _CHACHA20_POLY1305_H_ */ andre@0: diff -r c3565a90b8c4 lib/freebl/poly1305/poly1305-donna-x64-sse2-incremental-source.c andre@0: --- /dev/null Thu Jan 01 00:00:00 1970 +0000 andre@0: +++ b/lib/freebl/poly1305/poly1305-donna-x64-sse2-incremental-source.c Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -0,0 +1,623 @@ 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. It implements SIMD vectorization based on the algorithm described in andre@0: + * http://cr.yp.to/papers.html#neoncrypto. Unrolled to 2 powers, i.e. 64 byte andre@0: + * block size. */ andre@0: + andre@0: +#include andre@0: +#include andre@0: + andre@0: +#include "poly1305.h" andre@0: + andre@0: +#define ALIGN(x) __attribute__((aligned(x))) andre@0: +#define INLINE inline andre@0: +#define U8TO64_LE(m) (*(uint64_t*)(m)) andre@0: +#define U8TO32_LE(m) (*(uint32_t*)(m)) andre@0: +#define U64TO8_LE(m,v) (*(uint64_t*)(m)) = v andre@0: + andre@0: +typedef __m128i xmmi; andre@0: +typedef unsigned __int128 uint128_t; andre@0: + andre@0: +static const uint32_t ALIGN(16) poly1305_x64_sse2_message_mask[4] = {(1 << 26) - 1, 0, (1 << 26) - 1, 0}; andre@0: +static const uint32_t ALIGN(16) poly1305_x64_sse2_5[4] = {5, 0, 5, 0}; andre@0: +static const uint32_t ALIGN(16) poly1305_x64_sse2_1shl128[4] = {(1 << 24), 0, (1 << 24), 0}; andre@0: + andre@0: +static uint128_t INLINE andre@0: +add128(uint128_t a, uint128_t b) { andre@0: + return a + b; andre@0: +} andre@0: + andre@0: +static uint128_t INLINE andre@0: +add128_64(uint128_t a, uint64_t b) { andre@0: + return a + b; andre@0: +} andre@0: + andre@0: +static uint128_t INLINE andre@0: +mul64x64_128(uint64_t a, uint64_t b) { andre@0: + return (uint128_t)a * b; andre@0: +} andre@0: + andre@0: +static uint64_t INLINE andre@0: +lo128(uint128_t a) { andre@0: + return (uint64_t)a; andre@0: +} andre@0: + andre@0: +static uint64_t INLINE andre@0: +shr128(uint128_t v, const int shift) { andre@0: + return (uint64_t)(v >> shift); andre@0: +} andre@0: + andre@0: +static uint64_t INLINE andre@0: +shr128_pair(uint64_t hi, uint64_t lo, const int shift) { andre@0: + return (uint64_t)((((uint128_t)hi << 64) | lo) >> shift); andre@0: +} andre@0: + andre@0: +typedef struct poly1305_power_t { andre@0: + union { andre@0: + xmmi v; andre@0: + uint64_t u[2]; andre@0: + uint32_t d[4]; andre@0: + } R20,R21,R22,R23,R24,S21,S22,S23,S24; andre@0: +} poly1305_power; andre@0: + andre@0: +typedef struct poly1305_state_internal_t { andre@0: + poly1305_power P[2]; /* 288 bytes, top 32 bit halves unused = 144 bytes of free storage */ andre@0: + union { andre@0: + xmmi H[5]; /* 80 bytes */ andre@0: + uint64_t HH[10]; andre@0: + }; andre@0: + /* uint64_t r0,r1,r2; [24 bytes] */ andre@0: + /* uint64_t pad0,pad1; [16 bytes] */ andre@0: + uint64_t started; /* 8 bytes */ andre@0: + uint64_t leftover; /* 8 bytes */ andre@0: + uint8_t buffer[64]; /* 64 bytes */ andre@0: +} poly1305_state_internal; /* 448 bytes total + 63 bytes for alignment = 511 bytes raw */ andre@0: + andre@0: +static poly1305_state_internal INLINE andre@0: +*poly1305_aligned_state(poly1305_state *state) { andre@0: + return (poly1305_state_internal *)(((uint64_t)state + 63) & ~63); andre@0: +} andre@0: + andre@0: +/* copy 0-63 bytes */ andre@0: +static void INLINE andre@0: +poly1305_block_copy(uint8_t *dst, const uint8_t *src, size_t bytes) { andre@0: + size_t offset = src - dst; andre@0: + if (bytes & 32) { andre@0: + _mm_storeu_si128((xmmi *)(dst + 0), _mm_loadu_si128((xmmi *)(dst + offset + 0))); andre@0: + _mm_storeu_si128((xmmi *)(dst + 16), _mm_loadu_si128((xmmi *)(dst + offset + 16))); andre@0: + dst += 32; andre@0: + } andre@0: + if (bytes & 16) { _mm_storeu_si128((xmmi *)dst, _mm_loadu_si128((xmmi *)(dst + offset))); dst += 16; } andre@0: + if (bytes & 8) { *(uint64_t *)dst = *(uint64_t *)(dst + offset); dst += 8; } andre@0: + if (bytes & 4) { *(uint32_t *)dst = *(uint32_t *)(dst + offset); dst += 4; } andre@0: + if (bytes & 2) { *(uint16_t *)dst = *(uint16_t *)(dst + offset); dst += 2; } andre@0: + if (bytes & 1) { *( uint8_t *)dst = *( uint8_t *)(dst + offset); } andre@0: +} andre@0: + andre@0: +/* zero 0-15 bytes */ andre@0: +static void INLINE andre@0: +poly1305_block_zero(uint8_t *dst, size_t bytes) { andre@0: + if (bytes & 8) { *(uint64_t *)dst = 0; dst += 8; } andre@0: + if (bytes & 4) { *(uint32_t *)dst = 0; dst += 4; } andre@0: + if (bytes & 2) { *(uint16_t *)dst = 0; dst += 2; } andre@0: + if (bytes & 1) { *( uint8_t *)dst = 0; } andre@0: +} andre@0: + andre@0: +static size_t INLINE andre@0: +poly1305_min(size_t a, size_t b) { andre@0: + return (a < b) ? a : b; andre@0: +} andre@0: + andre@0: +void andre@0: +Poly1305Init(poly1305_state *state, const unsigned char key[32]) { andre@0: + poly1305_state_internal *st = poly1305_aligned_state(state); andre@0: + poly1305_power *p; andre@0: + uint64_t r0,r1,r2; andre@0: + uint64_t t0,t1; andre@0: + andre@0: + /* clamp key */ andre@0: + t0 = U8TO64_LE(key + 0); andre@0: + t1 = U8TO64_LE(key + 8); andre@0: + r0 = t0 & 0xffc0fffffff; t0 >>= 44; t0 |= t1 << 20; andre@0: + r1 = t0 & 0xfffffc0ffff; t1 >>= 24; andre@0: + r2 = t1 & 0x00ffffffc0f; andre@0: + andre@0: + /* store r in un-used space of st->P[1] */ andre@0: + p = &st->P[1]; andre@0: + p->R20.d[1] = (uint32_t)(r0 ); andre@0: + p->R20.d[3] = (uint32_t)(r0 >> 32); andre@0: + p->R21.d[1] = (uint32_t)(r1 ); andre@0: + p->R21.d[3] = (uint32_t)(r1 >> 32); andre@0: + p->R22.d[1] = (uint32_t)(r2 ); andre@0: + p->R22.d[3] = (uint32_t)(r2 >> 32); andre@0: + andre@0: + /* store pad */ andre@0: + p->R23.d[1] = U8TO32_LE(key + 16); andre@0: + p->R23.d[3] = U8TO32_LE(key + 20); andre@0: + p->R24.d[1] = U8TO32_LE(key + 24); andre@0: + p->R24.d[3] = U8TO32_LE(key + 28); andre@0: + andre@0: + /* H = 0 */ andre@0: + st->H[0] = _mm_setzero_si128(); andre@0: + st->H[1] = _mm_setzero_si128(); andre@0: + st->H[2] = _mm_setzero_si128(); andre@0: + st->H[3] = _mm_setzero_si128(); andre@0: + st->H[4] = _mm_setzero_si128(); andre@0: + andre@0: + st->started = 0; andre@0: + st->leftover = 0; andre@0: +} andre@0: + andre@0: +static void andre@0: +poly1305_first_block(poly1305_state_internal *st, const uint8_t *m) { andre@0: + const xmmi MMASK = _mm_load_si128((xmmi *)poly1305_x64_sse2_message_mask); andre@0: + const xmmi FIVE = _mm_load_si128((xmmi*)poly1305_x64_sse2_5); andre@0: + const xmmi HIBIT = _mm_load_si128((xmmi*)poly1305_x64_sse2_1shl128); andre@0: + xmmi T5,T6; andre@0: + poly1305_power *p; andre@0: + uint128_t d[3]; andre@0: + uint64_t r0,r1,r2; andre@0: + uint64_t r20,r21,r22,s22; andre@0: + uint64_t pad0,pad1; andre@0: + uint64_t c; andre@0: + uint64_t i; andre@0: + andre@0: + /* pull out stored info */ andre@0: + p = &st->P[1]; andre@0: + andre@0: + r0 = ((uint64_t)p->R20.d[3] << 32) | (uint64_t)p->R20.d[1]; andre@0: + r1 = ((uint64_t)p->R21.d[3] << 32) | (uint64_t)p->R21.d[1]; andre@0: + r2 = ((uint64_t)p->R22.d[3] << 32) | (uint64_t)p->R22.d[1]; andre@0: + pad0 = ((uint64_t)p->R23.d[3] << 32) | (uint64_t)p->R23.d[1]; andre@0: + pad1 = ((uint64_t)p->R24.d[3] << 32) | (uint64_t)p->R24.d[1]; andre@0: + andre@0: + /* compute powers r^2,r^4 */ andre@0: + r20 = r0; andre@0: + r21 = r1; andre@0: + r22 = r2; andre@0: + for (i = 0; i < 2; i++) { andre@0: + s22 = r22 * (5 << 2); andre@0: + andre@0: + d[0] = add128(mul64x64_128(r20, r20), mul64x64_128(r21 * 2, s22)); andre@0: + d[1] = add128(mul64x64_128(r22, s22), mul64x64_128(r20 * 2, r21)); andre@0: + d[2] = add128(mul64x64_128(r21, r21), mul64x64_128(r22 * 2, r20)); andre@0: + andre@0: + r20 = lo128(d[0]) & 0xfffffffffff; c = shr128(d[0], 44); andre@0: + d[1] = add128_64(d[1], c); r21 = lo128(d[1]) & 0xfffffffffff; c = shr128(d[1], 44); andre@0: + d[2] = add128_64(d[2], c); r22 = lo128(d[2]) & 0x3ffffffffff; c = shr128(d[2], 42); andre@0: + r20 += c * 5; c = (r20 >> 44); r20 = r20 & 0xfffffffffff; andre@0: + r21 += c; andre@0: + andre@0: + p->R20.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)( r20 ) & 0x3ffffff), _MM_SHUFFLE(1,0,1,0)); andre@0: + p->R21.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)((r20 >> 26) | (r21 << 18)) & 0x3ffffff), _MM_SHUFFLE(1,0,1,0)); andre@0: + p->R22.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)((r21 >> 8) ) & 0x3ffffff), _MM_SHUFFLE(1,0,1,0)); andre@0: + p->R23.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)((r21 >> 34) | (r22 << 10)) & 0x3ffffff), _MM_SHUFFLE(1,0,1,0)); andre@0: + p->R24.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)((r22 >> 16) ) ), _MM_SHUFFLE(1,0,1,0)); andre@0: + p->S21.v = _mm_mul_epu32(p->R21.v, FIVE); andre@0: + p->S22.v = _mm_mul_epu32(p->R22.v, FIVE); andre@0: + p->S23.v = _mm_mul_epu32(p->R23.v, FIVE); andre@0: + p->S24.v = _mm_mul_epu32(p->R24.v, FIVE); andre@0: + p--; andre@0: + } andre@0: + andre@0: + /* put saved info back */ andre@0: + p = &st->P[1]; andre@0: + p->R20.d[1] = (uint32_t)(r0 ); andre@0: + p->R20.d[3] = (uint32_t)(r0 >> 32); andre@0: + p->R21.d[1] = (uint32_t)(r1 ); andre@0: + p->R21.d[3] = (uint32_t)(r1 >> 32); andre@0: + p->R22.d[1] = (uint32_t)(r2 ); andre@0: + p->R22.d[3] = (uint32_t)(r2 >> 32); andre@0: + p->R23.d[1] = (uint32_t)(pad0 ); andre@0: + p->R23.d[3] = (uint32_t)(pad0 >> 32); andre@0: + p->R24.d[1] = (uint32_t)(pad1 ); andre@0: + p->R24.d[3] = (uint32_t)(pad1 >> 32); andre@0: + andre@0: + /* H = [Mx,My] */ andre@0: + T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 0)), _mm_loadl_epi64((xmmi *)(m + 16))); andre@0: + T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 8)), _mm_loadl_epi64((xmmi *)(m + 24))); andre@0: + st->H[0] = _mm_and_si128(MMASK, T5); andre@0: + st->H[1] = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: + T5 = _mm_or_si128(_mm_srli_epi64(T5, 52), _mm_slli_epi64(T6, 12)); andre@0: + st->H[2] = _mm_and_si128(MMASK, T5); andre@0: + st->H[3] = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: + st->H[4] = _mm_or_si128(_mm_srli_epi64(T6, 40), HIBIT); andre@0: +} andre@0: + andre@0: +static void andre@0: +poly1305_blocks(poly1305_state_internal *st, const uint8_t *m, size_t bytes) { andre@0: + const xmmi MMASK = _mm_load_si128((xmmi *)poly1305_x64_sse2_message_mask); andre@0: + const xmmi FIVE = _mm_load_si128((xmmi*)poly1305_x64_sse2_5); andre@0: + const xmmi HIBIT = _mm_load_si128((xmmi*)poly1305_x64_sse2_1shl128); andre@0: + andre@0: + poly1305_power *p; andre@0: + xmmi H0,H1,H2,H3,H4; andre@0: + xmmi T0,T1,T2,T3,T4,T5,T6; andre@0: + xmmi M0,M1,M2,M3,M4; andre@0: + xmmi C1,C2; andre@0: + andre@0: + H0 = st->H[0]; andre@0: + H1 = st->H[1]; andre@0: + H2 = st->H[2]; andre@0: + H3 = st->H[3]; andre@0: + H4 = st->H[4]; andre@0: + andre@0: + while (bytes >= 64) { andre@0: + /* H *= [r^4,r^4] */ andre@0: + p = &st->P[0]; andre@0: + T0 = _mm_mul_epu32(H0, p->R20.v); andre@0: + T1 = _mm_mul_epu32(H0, p->R21.v); andre@0: + T2 = _mm_mul_epu32(H0, p->R22.v); andre@0: + T3 = _mm_mul_epu32(H0, p->R23.v); andre@0: + T4 = _mm_mul_epu32(H0, p->R24.v); andre@0: + T5 = _mm_mul_epu32(H1, p->S24.v); T6 = _mm_mul_epu32(H1, p->R20.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(H2, p->S23.v); T6 = _mm_mul_epu32(H2, p->S24.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(H3, p->S22.v); T6 = _mm_mul_epu32(H3, p->S23.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(H4, p->S21.v); T6 = _mm_mul_epu32(H4, p->S22.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(H1, p->R21.v); T6 = _mm_mul_epu32(H1, p->R22.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(H2, p->R20.v); T6 = _mm_mul_epu32(H2, p->R21.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(H3, p->S24.v); T6 = _mm_mul_epu32(H3, p->R20.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(H4, p->S23.v); T6 = _mm_mul_epu32(H4, p->S24.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(H1, p->R23.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(H2, p->R22.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(H3, p->R21.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(H4, p->R20.v); T4 = _mm_add_epi64(T4, T5); andre@0: + andre@0: + /* H += [Mx,My]*[r^2,r^2] */ andre@0: + T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 0)), _mm_loadl_epi64((xmmi *)(m + 16))); andre@0: + T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 8)), _mm_loadl_epi64((xmmi *)(m + 24))); andre@0: + M0 = _mm_and_si128(MMASK, T5); andre@0: + M1 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: + T5 = _mm_or_si128(_mm_srli_epi64(T5, 52), _mm_slli_epi64(T6, 12)); andre@0: + M2 = _mm_and_si128(MMASK, T5); andre@0: + M3 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: + M4 = _mm_or_si128(_mm_srli_epi64(T6, 40), HIBIT); andre@0: + andre@0: + p = &st->P[1]; andre@0: + T5 = _mm_mul_epu32(M0, p->R20.v); T6 = _mm_mul_epu32(M0, p->R21.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(M1, p->S24.v); T6 = _mm_mul_epu32(M1, p->R20.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(M2, p->S23.v); T6 = _mm_mul_epu32(M2, p->S24.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(M3, p->S22.v); T6 = _mm_mul_epu32(M3, p->S23.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(M4, p->S21.v); T6 = _mm_mul_epu32(M4, p->S22.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(M0, p->R22.v); T6 = _mm_mul_epu32(M0, p->R23.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(M1, p->R21.v); T6 = _mm_mul_epu32(M1, p->R22.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(M2, p->R20.v); T6 = _mm_mul_epu32(M2, p->R21.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(M3, p->S24.v); T6 = _mm_mul_epu32(M3, p->R20.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(M4, p->S23.v); T6 = _mm_mul_epu32(M4, p->S24.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(M0, p->R24.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(M1, p->R23.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(M2, p->R22.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(M3, p->R21.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(M4, p->R20.v); T4 = _mm_add_epi64(T4, T5); andre@0: + andre@0: + /* H += [Mx,My] */ andre@0: + T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 32)), _mm_loadl_epi64((xmmi *)(m + 48))); andre@0: + T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 40)), _mm_loadl_epi64((xmmi *)(m + 56))); andre@0: + M0 = _mm_and_si128(MMASK, T5); andre@0: + M1 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: + T5 = _mm_or_si128(_mm_srli_epi64(T5, 52), _mm_slli_epi64(T6, 12)); andre@0: + M2 = _mm_and_si128(MMASK, T5); andre@0: + M3 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: + M4 = _mm_or_si128(_mm_srli_epi64(T6, 40), HIBIT); andre@0: + andre@0: + T0 = _mm_add_epi64(T0, M0); andre@0: + T1 = _mm_add_epi64(T1, M1); andre@0: + T2 = _mm_add_epi64(T2, M2); andre@0: + T3 = _mm_add_epi64(T3, M3); andre@0: + T4 = _mm_add_epi64(T4, M4); andre@0: + andre@0: + /* reduce */ andre@0: + C1 = _mm_srli_epi64(T0, 26); C2 = _mm_srli_epi64(T3, 26); T0 = _mm_and_si128(T0, MMASK); T3 = _mm_and_si128(T3, MMASK); T1 = _mm_add_epi64(T1, C1); T4 = _mm_add_epi64(T4, C2); andre@0: + C1 = _mm_srli_epi64(T1, 26); C2 = _mm_srli_epi64(T4, 26); T1 = _mm_and_si128(T1, MMASK); T4 = _mm_and_si128(T4, MMASK); T2 = _mm_add_epi64(T2, C1); T0 = _mm_add_epi64(T0, _mm_mul_epu32(C2, FIVE)); andre@0: + C1 = _mm_srli_epi64(T2, 26); C2 = _mm_srli_epi64(T0, 26); T2 = _mm_and_si128(T2, MMASK); T0 = _mm_and_si128(T0, MMASK); T3 = _mm_add_epi64(T3, C1); T1 = _mm_add_epi64(T1, C2); andre@0: + C1 = _mm_srli_epi64(T3, 26); T3 = _mm_and_si128(T3, MMASK); T4 = _mm_add_epi64(T4, C1); andre@0: + andre@0: + /* H = (H*[r^4,r^4] + [Mx,My]*[r^2,r^2] + [Mx,My]) */ andre@0: + H0 = T0; andre@0: + H1 = T1; andre@0: + H2 = T2; andre@0: + H3 = T3; andre@0: + H4 = T4; andre@0: + andre@0: + m += 64; andre@0: + bytes -= 64; andre@0: + } andre@0: + andre@0: + st->H[0] = H0; andre@0: + st->H[1] = H1; andre@0: + st->H[2] = H2; andre@0: + st->H[3] = H3; andre@0: + st->H[4] = H4; andre@0: +} andre@0: + andre@0: +static size_t andre@0: +poly1305_combine(poly1305_state_internal *st, const uint8_t *m, size_t bytes) { andre@0: + const xmmi MMASK = _mm_load_si128((xmmi *)poly1305_x64_sse2_message_mask); andre@0: + const xmmi HIBIT = _mm_load_si128((xmmi*)poly1305_x64_sse2_1shl128); andre@0: + const xmmi FIVE = _mm_load_si128((xmmi*)poly1305_x64_sse2_5); andre@0: + andre@0: + poly1305_power *p; andre@0: + xmmi H0,H1,H2,H3,H4; andre@0: + xmmi M0,M1,M2,M3,M4; andre@0: + xmmi T0,T1,T2,T3,T4,T5,T6; andre@0: + xmmi C1,C2; andre@0: + andre@0: + uint64_t r0,r1,r2; andre@0: + uint64_t t0,t1,t2,t3,t4; andre@0: + uint64_t c; andre@0: + size_t consumed = 0; andre@0: + andre@0: + H0 = st->H[0]; andre@0: + H1 = st->H[1]; andre@0: + H2 = st->H[2]; andre@0: + H3 = st->H[3]; andre@0: + H4 = st->H[4]; andre@0: + andre@0: + /* p = [r^2,r^2] */ andre@0: + p = &st->P[1]; andre@0: + andre@0: + if (bytes >= 32) { andre@0: + /* H *= [r^2,r^2] */ andre@0: + T0 = _mm_mul_epu32(H0, p->R20.v); andre@0: + T1 = _mm_mul_epu32(H0, p->R21.v); andre@0: + T2 = _mm_mul_epu32(H0, p->R22.v); andre@0: + T3 = _mm_mul_epu32(H0, p->R23.v); andre@0: + T4 = _mm_mul_epu32(H0, p->R24.v); andre@0: + T5 = _mm_mul_epu32(H1, p->S24.v); T6 = _mm_mul_epu32(H1, p->R20.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(H2, p->S23.v); T6 = _mm_mul_epu32(H2, p->S24.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(H3, p->S22.v); T6 = _mm_mul_epu32(H3, p->S23.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(H4, p->S21.v); T6 = _mm_mul_epu32(H4, p->S22.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(H1, p->R21.v); T6 = _mm_mul_epu32(H1, p->R22.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(H2, p->R20.v); T6 = _mm_mul_epu32(H2, p->R21.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(H3, p->S24.v); T6 = _mm_mul_epu32(H3, p->R20.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(H4, p->S23.v); T6 = _mm_mul_epu32(H4, p->S24.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(H1, p->R23.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(H2, p->R22.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(H3, p->R21.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(H4, p->R20.v); T4 = _mm_add_epi64(T4, T5); andre@0: + andre@0: + /* H += [Mx,My] */ andre@0: + T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 0)), _mm_loadl_epi64((xmmi *)(m + 16))); andre@0: + T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 8)), _mm_loadl_epi64((xmmi *)(m + 24))); andre@0: + M0 = _mm_and_si128(MMASK, T5); andre@0: + M1 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: + T5 = _mm_or_si128(_mm_srli_epi64(T5, 52), _mm_slli_epi64(T6, 12)); andre@0: + M2 = _mm_and_si128(MMASK, T5); andre@0: + M3 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: + M4 = _mm_or_si128(_mm_srli_epi64(T6, 40), HIBIT); andre@0: + andre@0: + T0 = _mm_add_epi64(T0, M0); andre@0: + T1 = _mm_add_epi64(T1, M1); andre@0: + T2 = _mm_add_epi64(T2, M2); andre@0: + T3 = _mm_add_epi64(T3, M3); andre@0: + T4 = _mm_add_epi64(T4, M4); andre@0: + andre@0: + /* reduce */ andre@0: + C1 = _mm_srli_epi64(T0, 26); C2 = _mm_srli_epi64(T3, 26); T0 = _mm_and_si128(T0, MMASK); T3 = _mm_and_si128(T3, MMASK); T1 = _mm_add_epi64(T1, C1); T4 = _mm_add_epi64(T4, C2); andre@0: + C1 = _mm_srli_epi64(T1, 26); C2 = _mm_srli_epi64(T4, 26); T1 = _mm_and_si128(T1, MMASK); T4 = _mm_and_si128(T4, MMASK); T2 = _mm_add_epi64(T2, C1); T0 = _mm_add_epi64(T0, _mm_mul_epu32(C2, FIVE)); andre@0: + C1 = _mm_srli_epi64(T2, 26); C2 = _mm_srli_epi64(T0, 26); T2 = _mm_and_si128(T2, MMASK); T0 = _mm_and_si128(T0, MMASK); T3 = _mm_add_epi64(T3, C1); T1 = _mm_add_epi64(T1, C2); andre@0: + C1 = _mm_srli_epi64(T3, 26); T3 = _mm_and_si128(T3, MMASK); T4 = _mm_add_epi64(T4, C1); andre@0: + andre@0: + /* H = (H*[r^2,r^2] + [Mx,My]) */ andre@0: + H0 = T0; andre@0: + H1 = T1; andre@0: + H2 = T2; andre@0: + H3 = T3; andre@0: + H4 = T4; andre@0: + andre@0: + consumed = 32; andre@0: + } andre@0: + andre@0: + /* finalize, H *= [r^2,r] */ andre@0: + r0 = ((uint64_t)p->R20.d[3] << 32) | (uint64_t)p->R20.d[1]; andre@0: + r1 = ((uint64_t)p->R21.d[3] << 32) | (uint64_t)p->R21.d[1]; andre@0: + r2 = ((uint64_t)p->R22.d[3] << 32) | (uint64_t)p->R22.d[1]; andre@0: + andre@0: + p->R20.d[2] = (uint32_t)( r0 ) & 0x3ffffff; andre@0: + p->R21.d[2] = (uint32_t)((r0 >> 26) | (r1 << 18)) & 0x3ffffff; andre@0: + p->R22.d[2] = (uint32_t)((r1 >> 8) ) & 0x3ffffff; andre@0: + p->R23.d[2] = (uint32_t)((r1 >> 34) | (r2 << 10)) & 0x3ffffff; andre@0: + p->R24.d[2] = (uint32_t)((r2 >> 16) ) ; andre@0: + p->S21.d[2] = p->R21.d[2] * 5; andre@0: + p->S22.d[2] = p->R22.d[2] * 5; andre@0: + p->S23.d[2] = p->R23.d[2] * 5; andre@0: + p->S24.d[2] = p->R24.d[2] * 5; andre@0: + andre@0: + /* H *= [r^2,r] */ andre@0: + T0 = _mm_mul_epu32(H0, p->R20.v); andre@0: + T1 = _mm_mul_epu32(H0, p->R21.v); andre@0: + T2 = _mm_mul_epu32(H0, p->R22.v); andre@0: + T3 = _mm_mul_epu32(H0, p->R23.v); andre@0: + T4 = _mm_mul_epu32(H0, p->R24.v); andre@0: + T5 = _mm_mul_epu32(H1, p->S24.v); T6 = _mm_mul_epu32(H1, p->R20.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(H2, p->S23.v); T6 = _mm_mul_epu32(H2, p->S24.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(H3, p->S22.v); T6 = _mm_mul_epu32(H3, p->S23.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(H4, p->S21.v); T6 = _mm_mul_epu32(H4, p->S22.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: + T5 = _mm_mul_epu32(H1, p->R21.v); T6 = _mm_mul_epu32(H1, p->R22.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(H2, p->R20.v); T6 = _mm_mul_epu32(H2, p->R21.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(H3, p->S24.v); T6 = _mm_mul_epu32(H3, p->R20.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(H4, p->S23.v); T6 = _mm_mul_epu32(H4, p->S24.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: + T5 = _mm_mul_epu32(H1, p->R23.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(H2, p->R22.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(H3, p->R21.v); T4 = _mm_add_epi64(T4, T5); andre@0: + T5 = _mm_mul_epu32(H4, p->R20.v); T4 = _mm_add_epi64(T4, T5); andre@0: + andre@0: + C1 = _mm_srli_epi64(T0, 26); C2 = _mm_srli_epi64(T3, 26); T0 = _mm_and_si128(T0, MMASK); T3 = _mm_and_si128(T3, MMASK); T1 = _mm_add_epi64(T1, C1); T4 = _mm_add_epi64(T4, C2); andre@0: + C1 = _mm_srli_epi64(T1, 26); C2 = _mm_srli_epi64(T4, 26); T1 = _mm_and_si128(T1, MMASK); T4 = _mm_and_si128(T4, MMASK); T2 = _mm_add_epi64(T2, C1); T0 = _mm_add_epi64(T0, _mm_mul_epu32(C2, FIVE)); andre@0: + C1 = _mm_srli_epi64(T2, 26); C2 = _mm_srli_epi64(T0, 26); T2 = _mm_and_si128(T2, MMASK); T0 = _mm_and_si128(T0, MMASK); T3 = _mm_add_epi64(T3, C1); T1 = _mm_add_epi64(T1, C2); andre@0: + C1 = _mm_srli_epi64(T3, 26); T3 = _mm_and_si128(T3, MMASK); T4 = _mm_add_epi64(T4, C1); andre@0: + andre@0: + /* H = H[0]+H[1] */ andre@0: + H0 = _mm_add_epi64(T0, _mm_srli_si128(T0, 8)); andre@0: + H1 = _mm_add_epi64(T1, _mm_srli_si128(T1, 8)); andre@0: + H2 = _mm_add_epi64(T2, _mm_srli_si128(T2, 8)); andre@0: + H3 = _mm_add_epi64(T3, _mm_srli_si128(T3, 8)); andre@0: + H4 = _mm_add_epi64(T4, _mm_srli_si128(T4, 8)); andre@0: + andre@0: + t0 = _mm_cvtsi128_si32(H0) ; c = (t0 >> 26); t0 &= 0x3ffffff; andre@0: + t1 = _mm_cvtsi128_si32(H1) + c; c = (t1 >> 26); t1 &= 0x3ffffff; andre@0: + t2 = _mm_cvtsi128_si32(H2) + c; c = (t2 >> 26); t2 &= 0x3ffffff; andre@0: + t3 = _mm_cvtsi128_si32(H3) + c; c = (t3 >> 26); t3 &= 0x3ffffff; andre@0: + t4 = _mm_cvtsi128_si32(H4) + c; c = (t4 >> 26); t4 &= 0x3ffffff; andre@0: + t0 = t0 + (c * 5); c = (t0 >> 26); t0 &= 0x3ffffff; andre@0: + t1 = t1 + c; andre@0: + andre@0: + st->HH[0] = ((t0 ) | (t1 << 26) ) & 0xfffffffffffull; andre@0: + st->HH[1] = ((t1 >> 18) | (t2 << 8) | (t3 << 34)) & 0xfffffffffffull; andre@0: + st->HH[2] = ((t3 >> 10) | (t4 << 16) ) & 0x3ffffffffffull; andre@0: + andre@0: + return consumed; andre@0: +} andre@0: + andre@0: +void andre@0: +Poly1305Update(poly1305_state *state, const unsigned char *m, size_t bytes) { andre@0: + poly1305_state_internal *st = poly1305_aligned_state(state); andre@0: + size_t want; andre@0: + andre@0: + /* need at least 32 initial bytes to start the accelerated branch */ andre@0: + if (!st->started) { andre@0: + if ((st->leftover == 0) && (bytes > 32)) { andre@0: + poly1305_first_block(st, m); andre@0: + m += 32; andre@0: + bytes -= 32; andre@0: + } else { andre@0: + want = poly1305_min(32 - st->leftover, bytes); andre@0: + poly1305_block_copy(st->buffer + st->leftover, m, want); andre@0: + bytes -= want; andre@0: + m += want; andre@0: + st->leftover += want; andre@0: + if ((st->leftover < 32) || (bytes == 0)) andre@0: + return; andre@0: + poly1305_first_block(st, st->buffer); andre@0: + st->leftover = 0; andre@0: + } andre@0: + st->started = 1; andre@0: + } andre@0: + andre@0: + /* handle leftover */ andre@0: + if (st->leftover) { andre@0: + want = poly1305_min(64 - st->leftover, bytes); andre@0: + poly1305_block_copy(st->buffer + st->leftover, m, want); andre@0: + bytes -= want; andre@0: + m += want; andre@0: + st->leftover += want; andre@0: + if (st->leftover < 64) andre@0: + return; andre@0: + poly1305_blocks(st, st->buffer, 64); andre@0: + st->leftover = 0; andre@0: + } andre@0: + andre@0: + /* process 64 byte blocks */ andre@0: + if (bytes >= 64) { andre@0: + want = (bytes & ~63); andre@0: + poly1305_blocks(st, m, want); andre@0: + m += want; andre@0: + bytes -= want; andre@0: + } andre@0: + andre@0: + if (bytes) { andre@0: + poly1305_block_copy(st->buffer + st->leftover, m, bytes); andre@0: + st->leftover += bytes; andre@0: + } andre@0: +} andre@0: + andre@0: +void andre@0: +Poly1305Finish(poly1305_state *state, unsigned char mac[16]) { andre@0: + poly1305_state_internal *st = poly1305_aligned_state(state); andre@0: + size_t leftover = st->leftover; andre@0: + uint8_t *m = st->buffer; andre@0: + uint128_t d[3]; andre@0: + uint64_t h0,h1,h2; andre@0: + uint64_t t0,t1; andre@0: + uint64_t g0,g1,g2,c,nc; andre@0: + uint64_t r0,r1,r2,s1,s2; andre@0: + poly1305_power *p; andre@0: + andre@0: + if (st->started) { andre@0: + size_t consumed = poly1305_combine(st, m, leftover); andre@0: + leftover -= consumed; andre@0: + m += consumed; andre@0: + } andre@0: + andre@0: + /* st->HH will either be 0 or have the combined result */ andre@0: + h0 = st->HH[0]; andre@0: + h1 = st->HH[1]; andre@0: + h2 = st->HH[2]; andre@0: + andre@0: + p = &st->P[1]; andre@0: + r0 = ((uint64_t)p->R20.d[3] << 32) | (uint64_t)p->R20.d[1]; andre@0: + r1 = ((uint64_t)p->R21.d[3] << 32) | (uint64_t)p->R21.d[1]; andre@0: + r2 = ((uint64_t)p->R22.d[3] << 32) | (uint64_t)p->R22.d[1]; andre@0: + s1 = r1 * (5 << 2); andre@0: + s2 = r2 * (5 << 2); andre@0: + andre@0: + if (leftover < 16) andre@0: + goto poly1305_donna_atmost15bytes; andre@0: + andre@0: +poly1305_donna_atleast16bytes: andre@0: + t0 = U8TO64_LE(m + 0); andre@0: + t1 = U8TO64_LE(m + 8); andre@0: + h0 += t0 & 0xfffffffffff; andre@0: + t0 = shr128_pair(t1, t0, 44); andre@0: + h1 += t0 & 0xfffffffffff; andre@0: + h2 += (t1 >> 24) | ((uint64_t)1 << 40); andre@0: + andre@0: +poly1305_donna_mul: andre@0: + d[0] = add128(add128(mul64x64_128(h0, r0), mul64x64_128(h1, s2)), mul64x64_128(h2, s1)); andre@0: + d[1] = add128(add128(mul64x64_128(h0, r1), mul64x64_128(h1, r0)), mul64x64_128(h2, s2)); andre@0: + d[2] = add128(add128(mul64x64_128(h0, r2), mul64x64_128(h1, r1)), mul64x64_128(h2, r0)); andre@0: + h0 = lo128(d[0]) & 0xfffffffffff; c = shr128(d[0], 44); andre@0: + d[1] = add128_64(d[1], c); h1 = lo128(d[1]) & 0xfffffffffff; c = shr128(d[1], 44); andre@0: + d[2] = add128_64(d[2], c); h2 = lo128(d[2]) & 0x3ffffffffff; c = shr128(d[2], 42); andre@0: + h0 += c * 5; andre@0: + andre@0: + m += 16; andre@0: + leftover -= 16; andre@0: + if (leftover >= 16) goto poly1305_donna_atleast16bytes; andre@0: + andre@0: + /* final bytes */ andre@0: +poly1305_donna_atmost15bytes: andre@0: + if (!leftover) goto poly1305_donna_finish; andre@0: + andre@0: + m[leftover++] = 1; andre@0: + poly1305_block_zero(m + leftover, 16 - leftover); andre@0: + leftover = 16; andre@0: + andre@0: + t0 = U8TO64_LE(m+0); andre@0: + t1 = U8TO64_LE(m+8); andre@0: + h0 += t0 & 0xfffffffffff; t0 = shr128_pair(t1, t0, 44); andre@0: + h1 += t0 & 0xfffffffffff; andre@0: + h2 += (t1 >> 24); andre@0: + andre@0: + goto poly1305_donna_mul; andre@0: + andre@0: +poly1305_donna_finish: andre@0: + c = (h0 >> 44); h0 &= 0xfffffffffff; andre@0: + h1 += c; c = (h1 >> 44); h1 &= 0xfffffffffff; andre@0: + h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff; andre@0: + h0 += c * 5; andre@0: + andre@0: + g0 = h0 + 5; c = (g0 >> 44); g0 &= 0xfffffffffff; andre@0: + g1 = h1 + c; c = (g1 >> 44); g1 &= 0xfffffffffff; andre@0: + g2 = h2 + c - ((uint64_t)1 << 42); andre@0: + andre@0: + c = (g2 >> 63) - 1; andre@0: + nc = ~c; andre@0: + h0 = (h0 & nc) | (g0 & c); andre@0: + h1 = (h1 & nc) | (g1 & c); andre@0: + h2 = (h2 & nc) | (g2 & c); andre@0: + andre@0: + /* pad */ andre@0: + t0 = ((uint64_t)p->R23.d[3] << 32) | (uint64_t)p->R23.d[1]; andre@0: + t1 = ((uint64_t)p->R24.d[3] << 32) | (uint64_t)p->R24.d[1]; andre@0: + h0 += (t0 & 0xfffffffffff) ; c = (h0 >> 44); h0 &= 0xfffffffffff; t0 = shr128_pair(t1, t0, 44); andre@0: + h1 += (t0 & 0xfffffffffff) + c; c = (h1 >> 44); h1 &= 0xfffffffffff; t1 = (t1 >> 24); andre@0: + h2 += (t1 ) + c; andre@0: + andre@0: + U64TO8_LE(mac + 0, ((h0 ) | (h1 << 44))); andre@0: + U64TO8_LE(mac + 8, ((h1 >> 20) | (h2 << 24))); andre@0: +} andre@0: diff -r c3565a90b8c4 lib/freebl/poly1305/poly1305.c andre@0: --- /dev/null Thu Jan 01 00:00:00 1970 +0000 andre@0: +++ b/lib/freebl/poly1305/poly1305.c Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -0,0 +1,254 @@ 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: +} andre@0: diff -r c3565a90b8c4 lib/freebl/poly1305/poly1305.h andre@0: --- /dev/null Thu Jan 01 00:00:00 1970 +0000 andre@0: +++ b/lib/freebl/poly1305/poly1305.h Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -0,0 +1,31 @@ andre@0: +/* andre@0: + * poly1305.h - header file for Poly1305 implementation. andre@0: + * 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: +#ifndef FREEBL_POLY1305_H_ andre@0: +#define FREEBL_POLY1305_H_ andre@0: + andre@0: +typedef unsigned char poly1305_state[512]; andre@0: + andre@0: +/* Poly1305Init sets up |state| so that it can be used to calculate an andre@0: + * authentication tag with the one-time key |key|. Note that |key| is a andre@0: + * one-time key and therefore there is no `reset' method because that would andre@0: + * enable several messages to be authenticated with the same key. */ andre@0: +extern void Poly1305Init(poly1305_state* state, andre@0: + const unsigned char key[32]); andre@0: + andre@0: +/* Poly1305Update processes |in_len| bytes from |in|. It can be called zero or andre@0: + * more times after poly1305_init. */ andre@0: +extern void Poly1305Update(poly1305_state* state, andre@0: + const unsigned char *in, andre@0: + size_t inLen); andre@0: + andre@0: +/* Poly1305Finish completes the poly1305 calculation and writes a 16 byte andre@0: + * authentication tag to |mac|. */ andre@0: +extern void Poly1305Finish(poly1305_state* state, andre@0: + unsigned char mac[16]); andre@0: + andre@0: +#endif /* FREEBL_POLY1305_H_ */ andre@0: diff -r c3565a90b8c4 lib/pk11wrap/pk11mech.c andre@0: --- a/lib/pk11wrap/pk11mech.c Fri Jan 03 20:59:10 2014 +0100 andre@0: +++ b/lib/pk11wrap/pk11mech.c Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -152,6 +152,8 @@ andre@0: return CKM_SEED_CBC; andre@0: case CKK_CAMELLIA: andre@0: return CKM_CAMELLIA_CBC; andre@0: + case CKK_NSS_CHACHA20: andre@0: + return CKM_NSS_CHACHA20_POLY1305; andre@0: case CKK_AES: andre@0: return CKM_AES_CBC; andre@0: case CKK_DES: andre@0: @@ -219,6 +221,8 @@ andre@0: case CKM_CAMELLIA_CBC_PAD: andre@0: case CKM_CAMELLIA_KEY_GEN: andre@0: return CKK_CAMELLIA; andre@0: + case CKM_NSS_CHACHA20_POLY1305: andre@0: + return CKK_NSS_CHACHA20; andre@0: case CKM_AES_ECB: andre@0: case CKM_AES_CBC: andre@0: case CKM_AES_CCM: andre@0: @@ -429,6 +433,8 @@ andre@0: case CKM_CAMELLIA_CBC_PAD: andre@0: case CKM_CAMELLIA_KEY_GEN: andre@0: return CKM_CAMELLIA_KEY_GEN; andre@0: + case CKM_NSS_CHACHA20_POLY1305: andre@0: + return CKM_NSS_CHACHA20_KEY_GEN; andre@0: case CKM_AES_ECB: andre@0: case CKM_AES_CBC: andre@0: case CKM_AES_CCM: andre@0: diff -r c3565a90b8c4 lib/softoken/pkcs11.c andre@0: --- a/lib/softoken/pkcs11.c Fri Jan 03 20:59:10 2014 +0100 andre@0: +++ b/lib/softoken/pkcs11.c Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -368,6 +368,9 @@ andre@0: {CKM_SEED_MAC, {16, 16, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SEED_MAC_GENERAL, {16, 16, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SEED_CBC_PAD, {16, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: + /* ------------------------- ChaCha20 Operations ---------------------- */ andre@0: + {CKM_NSS_CHACHA20_KEY_GEN, {32, 32, CKF_GENERATE}, PR_TRUE}, andre@0: + {CKM_NSS_CHACHA20_POLY1305,{32, 32, CKF_EN_DE}, PR_TRUE}, andre@0: /* ------------------------- Hashing Operations ----------------------- */ andre@0: {CKM_MD2, {0, 0, CKF_DIGEST}, PR_FALSE}, andre@0: {CKM_MD2_HMAC, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: diff -r c3565a90b8c4 lib/softoken/pkcs11c.c andre@0: --- a/lib/softoken/pkcs11c.c Fri Jan 03 20:59:10 2014 +0100 andre@0: +++ b/lib/softoken/pkcs11c.c Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -632,6 +632,97 @@ andre@0: return rv; andre@0: } andre@0: andre@0: +static SFTKChaCha20Poly1305Info * andre@0: +sftk_ChaCha20Poly1305_CreateContext(const unsigned char *key, andre@0: + unsigned int keyLen, andre@0: + const CK_NSS_AEAD_PARAMS* params) andre@0: +{ andre@0: + SFTKChaCha20Poly1305Info *ctx; andre@0: + andre@0: + if (params->ulIvLen != sizeof(ctx->nonce)) { andre@0: + PORT_SetError(SEC_ERROR_INPUT_LEN); andre@0: + return NULL; andre@0: + } andre@0: + andre@0: + ctx = PORT_New(SFTKChaCha20Poly1305Info); andre@0: + if (ctx == NULL) { andre@0: + return NULL; andre@0: + } andre@0: + andre@0: + if (ChaCha20Poly1305_InitContext(&ctx->freeblCtx, key, keyLen, andre@0: + params->ulTagLen) != SECSuccess) { andre@0: + PORT_Free(ctx); andre@0: + return NULL; andre@0: + } andre@0: + andre@0: + memcpy(ctx->nonce, params->pIv, sizeof(ctx->nonce)); andre@0: + andre@0: + if (params->ulAADLen > sizeof(ctx->ad)) { andre@0: + /* Need to allocate an overflow buffer for the additional data. */ andre@0: + ctx->adOverflow = (unsigned char *)PORT_Alloc(params->ulAADLen); andre@0: + if (!ctx->adOverflow) { andre@0: + PORT_Free(ctx); andre@0: + return NULL; andre@0: + } andre@0: + memcpy(ctx->adOverflow, params->pAAD, params->ulAADLen); andre@0: + } else { andre@0: + ctx->adOverflow = NULL; andre@0: + memcpy(ctx->ad, params->pAAD, params->ulAADLen); andre@0: + } andre@0: + ctx->adLen = params->ulAADLen; andre@0: + andre@0: + return ctx; andre@0: +} andre@0: + andre@0: +static void andre@0: +sftk_ChaCha20Poly1305_DestroyContext(SFTKChaCha20Poly1305Info *ctx, andre@0: + PRBool freeit) andre@0: +{ andre@0: + ChaCha20Poly1305_DestroyContext(&ctx->freeblCtx, PR_FALSE); andre@0: + if (ctx->adOverflow != NULL) { andre@0: + PORT_Free(ctx->adOverflow); andre@0: + ctx->adOverflow = NULL; andre@0: + } andre@0: + ctx->adLen = 0; andre@0: + if (freeit) { andre@0: + PORT_Free(ctx); andre@0: + } andre@0: +} andre@0: + andre@0: +static SECStatus andre@0: +sftk_ChaCha20Poly1305_Encrypt(const SFTKChaCha20Poly1305Info *ctx, andre@0: + unsigned char *output, unsigned int *outputLen, andre@0: + unsigned int maxOutputLen, andre@0: + const unsigned char *input, unsigned int inputLen) andre@0: +{ andre@0: + const unsigned char *ad = ctx->adOverflow; andre@0: + andre@0: + if (ad == NULL) { andre@0: + ad = ctx->ad; andre@0: + } andre@0: + andre@0: + return ChaCha20Poly1305_Seal(&ctx->freeblCtx, output, outputLen, andre@0: + maxOutputLen, input, inputLen, ctx->nonce, andre@0: + sizeof(ctx->nonce), ad, ctx->adLen); andre@0: +} andre@0: + andre@0: +static SECStatus andre@0: +sftk_ChaCha20Poly1305_Decrypt(const SFTKChaCha20Poly1305Info *ctx, andre@0: + unsigned char *output, unsigned int *outputLen, andre@0: + unsigned int maxOutputLen, andre@0: + const unsigned char *input, unsigned int inputLen) andre@0: +{ andre@0: + const unsigned char *ad = ctx->adOverflow; andre@0: + andre@0: + if (ad == NULL) { andre@0: + ad = ctx->ad; andre@0: + } andre@0: + andre@0: + return ChaCha20Poly1305_Open(&ctx->freeblCtx, output, outputLen, andre@0: + maxOutputLen, input, inputLen, ctx->nonce, andre@0: + sizeof(ctx->nonce), ad, ctx->adLen); andre@0: +} andre@0: + andre@0: /** NSC_CryptInit initializes an encryption/Decryption operation. andre@0: * andre@0: * Always called by NSC_EncryptInit, NSC_DecryptInit, NSC_WrapKey,NSC_UnwrapKey. andre@0: @@ -1027,6 +1118,35 @@ andre@0: context->destroy = (SFTKDestroy) AES_DestroyContext; andre@0: break; andre@0: andre@0: + case CKM_NSS_CHACHA20_POLY1305: andre@0: + if (pMechanism->ulParameterLen != sizeof(CK_NSS_AEAD_PARAMS)) { andre@0: + crv = CKR_MECHANISM_PARAM_INVALID; andre@0: + break; andre@0: + } andre@0: + context->multi = PR_FALSE; andre@0: + if (key_type != CKK_NSS_CHACHA20) { andre@0: + crv = CKR_KEY_TYPE_INCONSISTENT; andre@0: + break; andre@0: + } andre@0: + att = sftk_FindAttribute(key,CKA_VALUE); andre@0: + if (att == NULL) { andre@0: + crv = CKR_KEY_HANDLE_INVALID; andre@0: + break; andre@0: + } andre@0: + context->cipherInfo = sftk_ChaCha20Poly1305_CreateContext( andre@0: + (unsigned char*) att->attrib.pValue, att->attrib.ulValueLen, andre@0: + (CK_NSS_AEAD_PARAMS*) pMechanism->pParameter); andre@0: + sftk_FreeAttribute(att); andre@0: + if (context->cipherInfo == NULL) { andre@0: + crv = sftk_MapCryptError(PORT_GetError()); andre@0: + break; andre@0: + } andre@0: + context->update = (SFTKCipher) (isEncrypt ? andre@0: + sftk_ChaCha20Poly1305_Encrypt : andre@0: + sftk_ChaCha20Poly1305_Decrypt); andre@0: + context->destroy = (SFTKDestroy) sftk_ChaCha20Poly1305_DestroyContext; andre@0: + break; andre@0: + andre@0: case CKM_NETSCAPE_AES_KEY_WRAP_PAD: andre@0: context->doPad = PR_TRUE; andre@0: /* fall thru */ andre@0: @@ -3601,6 +3721,10 @@ andre@0: *key_type = CKK_AES; andre@0: if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; andre@0: break; andre@0: + case CKM_NSS_CHACHA20_KEY_GEN: andre@0: + *key_type = CKK_NSS_CHACHA20; andre@0: + if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; andre@0: + break; andre@0: default: andre@0: PORT_Assert(0); andre@0: crv = CKR_MECHANISM_INVALID; andre@0: @@ -3846,6 +3970,7 @@ andre@0: case CKM_SEED_KEY_GEN: andre@0: case CKM_CAMELLIA_KEY_GEN: andre@0: case CKM_AES_KEY_GEN: andre@0: + case CKM_NSS_CHACHA20_KEY_GEN: andre@0: #if NSS_SOFTOKEN_DOES_RC5 andre@0: case CKM_RC5_KEY_GEN: andre@0: #endif andre@0: diff -r c3565a90b8c4 lib/softoken/pkcs11i.h andre@0: --- a/lib/softoken/pkcs11i.h Fri Jan 03 20:59:10 2014 +0100 andre@0: +++ b/lib/softoken/pkcs11i.h Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -14,6 +14,7 @@ andre@0: #include "pkcs11t.h" andre@0: andre@0: #include "sftkdbt.h" andre@0: +#include "chacha20poly1305.h" andre@0: #include "hasht.h" andre@0: andre@0: /* andre@0: @@ -104,6 +105,7 @@ andre@0: typedef struct SFTKOAEPEncryptInfoStr SFTKOAEPEncryptInfo; andre@0: typedef struct SFTKOAEPDecryptInfoStr SFTKOAEPDecryptInfo; andre@0: typedef struct SFTKSSLMACInfoStr SFTKSSLMACInfo; andre@0: +typedef struct SFTKChaCha20Poly1305InfoStr SFTKChaCha20Poly1305Info; andre@0: typedef struct SFTKItemTemplateStr SFTKItemTemplate; andre@0: andre@0: /* define function pointer typdefs for pointer tables */ andre@0: @@ -399,6 +401,16 @@ andre@0: unsigned int keySize; andre@0: }; andre@0: andre@0: +/* SFTKChaCha20Poly1305Info saves the key, tag length, nonce, and additional andre@0: + * data for a ChaCha20+Poly1305 AEAD operation. */ andre@0: +struct SFTKChaCha20Poly1305InfoStr { andre@0: + ChaCha20Poly1305Context freeblCtx; andre@0: + unsigned char nonce[8]; andre@0: + unsigned char ad[16]; andre@0: + unsigned char *adOverflow; andre@0: + unsigned int adLen; andre@0: +}; andre@0: + andre@0: /* andre@0: * Template based on SECItems, suitable for passing as arrays andre@0: */ andre@0: diff -r c3565a90b8c4 lib/util/pkcs11n.h andre@0: --- a/lib/util/pkcs11n.h Fri Jan 03 20:59:10 2014 +0100 andre@0: +++ b/lib/util/pkcs11n.h Tue Jan 07 12:11:36 2014 -0800 andre@0: @@ -51,6 +51,8 @@ andre@0: #define CKK_NSS_JPAKE_ROUND1 (CKK_NSS + 2) andre@0: #define CKK_NSS_JPAKE_ROUND2 (CKK_NSS + 3) andre@0: andre@0: +#define CKK_NSS_CHACHA20 (CKK_NSS + 4) andre@0: + andre@0: /* andre@0: * NSS-defined certificate types andre@0: * andre@0: @@ -214,6 +216,9 @@ andre@0: #define CKM_NSS_TLS_KEY_AND_MAC_DERIVE_SHA256 (CKM_NSS + 23) andre@0: #define CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256 (CKM_NSS + 24) andre@0: andre@0: +#define CKM_NSS_CHACHA20_KEY_GEN (CKM_NSS + 25) andre@0: +#define CKM_NSS_CHACHA20_POLY1305 (CKM_NSS + 26) andre@0: + andre@0: /* andre@0: * HISTORICAL: andre@0: * Do not attempt to use these. They are only used by NETSCAPE's internal andre@0: @@ -281,6 +286,14 @@ andre@0: CK_ULONG ulHeaderLen; /* in */ andre@0: } CK_NSS_MAC_CONSTANT_TIME_PARAMS; andre@0: andre@0: +typedef struct CK_NSS_AEAD_PARAMS { andre@0: + CK_BYTE_PTR pIv; /* This is the nonce. */ andre@0: + CK_ULONG ulIvLen; andre@0: + CK_BYTE_PTR pAAD; andre@0: + CK_ULONG ulAADLen; andre@0: + CK_ULONG ulTagLen; andre@0: +} CK_NSS_AEAD_PARAMS; andre@0: + andre@0: /* andre@0: * NSS-defined return values andre@0: *