Mercurial > trustbridge > nss-cmake-static
comparison patches/nss-chacha20-poly1305.patch @ 0:1e5118fa0cb1
This is NSS with a Cmake Buildsyste
To compile a static NSS library for Windows we've used the
Chromium-NSS fork and added a Cmake buildsystem to compile
it statically for Windows. See README.chromium for chromium
changes and README.trustbridge for our modifications.
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Mon, 28 Jul 2014 10:47:06 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1e5118fa0cb1 |
---|---|
1 diff -r c3565a90b8c4 lib/freebl/blapi.h | |
2 --- a/lib/freebl/blapi.h Fri Jan 03 20:59:10 2014 +0100 | |
3 +++ b/lib/freebl/blapi.h Tue Jan 07 12:11:36 2014 -0800 | |
4 @@ -986,6 +986,38 @@ | |
5 unsigned int *outputLen, unsigned int maxOutputLen, | |
6 const unsigned char *input, unsigned int inputLen); | |
7 | |
8 +/******************************************/ | |
9 +/* | |
10 +** ChaCha20+Poly1305 AEAD | |
11 +*/ | |
12 + | |
13 +extern SECStatus | |
14 +ChaCha20Poly1305_InitContext(ChaCha20Poly1305Context *ctx, | |
15 + const unsigned char *key, unsigned int keyLen, | |
16 + unsigned int tagLen); | |
17 + | |
18 +extern ChaCha20Poly1305Context * | |
19 +ChaCha20Poly1305_CreateContext(const unsigned char *key, unsigned int keyLen, | |
20 + unsigned int tagLen); | |
21 + | |
22 +extern void | |
23 +ChaCha20Poly1305_DestroyContext(ChaCha20Poly1305Context *ctx, PRBool freeit); | |
24 + | |
25 +extern SECStatus | |
26 +ChaCha20Poly1305_Seal(const ChaCha20Poly1305Context *ctx, | |
27 + unsigned char *output, unsigned int *outputLen, | |
28 + unsigned int maxOutputLen, | |
29 + const unsigned char *input, unsigned int inputLen, | |
30 + const unsigned char *nonce, unsigned int nonceLen, | |
31 + const unsigned char *ad, unsigned int adLen); | |
32 + | |
33 +extern SECStatus | |
34 +ChaCha20Poly1305_Open(const ChaCha20Poly1305Context *ctx, | |
35 + unsigned char *output, unsigned int *outputLen, | |
36 + unsigned int maxOutputLen, | |
37 + const unsigned char *input, unsigned int inputLen, | |
38 + const unsigned char *nonce, unsigned int nonceLen, | |
39 + const unsigned char *ad, unsigned int adLen); | |
40 | |
41 /******************************************/ | |
42 /* | |
43 diff -r c3565a90b8c4 lib/freebl/blapit.h | |
44 --- a/lib/freebl/blapit.h Fri Jan 03 20:59:10 2014 +0100 | |
45 +++ b/lib/freebl/blapit.h Tue Jan 07 12:11:36 2014 -0800 | |
46 @@ -222,6 +222,7 @@ | |
47 struct SHA512ContextStr ; | |
48 struct AESKeyWrapContextStr ; | |
49 struct SEEDContextStr ; | |
50 +struct ChaCha20Poly1305ContextStr; | |
51 | |
52 typedef struct DESContextStr DESContext; | |
53 typedef struct RC2ContextStr RC2Context; | |
54 @@ -240,6 +241,7 @@ | |
55 typedef struct SHA512ContextStr SHA384Context; | |
56 typedef struct AESKeyWrapContextStr AESKeyWrapContext; | |
57 typedef struct SEEDContextStr SEEDContext; | |
58 +typedef struct ChaCha20Poly1305ContextStr ChaCha20Poly1305Context; | |
59 | |
60 /*************************************************************************** | |
61 ** RSA Public and Private Key structures | |
62 diff -r c3565a90b8c4 lib/freebl/chacha20/chacha20.c | |
63 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 | |
64 +++ b/lib/freebl/chacha20/chacha20.c Tue Jan 07 12:11:36 2014 -0800 | |
65 @@ -0,0 +1,108 @@ | |
66 +/* This Source Code Form is subject to the terms of the Mozilla Public | |
67 + * License, v. 2.0. If a copy of the MPL was not distributed with this | |
68 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
69 + | |
70 +/* Adopted from the public domain code in NaCl by djb. */ | |
71 + | |
72 +#include <string.h> | |
73 +#include <stdio.h> | |
74 + | |
75 +#include "prtypes.h" | |
76 +#include "chacha20.h" | |
77 + | |
78 +#define ROTL32(v, n) (((v) << (n)) | ((v) >> (32 - (n)))) | |
79 +#define ROTATE(v, c) ROTL32((v), (c)) | |
80 +#define XOR(v, w) ((v) ^ (w)) | |
81 +#define PLUS(x, y) ((x) + (y)) | |
82 + | |
83 +#define U32TO8_LITTLE(p, v) \ | |
84 + { (p)[0] = ((v) ) & 0xff; (p)[1] = ((v) >> 8) & 0xff; \ | |
85 + (p)[2] = ((v) >> 16) & 0xff; (p)[3] = ((v) >> 24) & 0xff; } | |
86 +#define U8TO32_LITTLE(p) \ | |
87 + (((PRUint32)((p)[0]) ) | ((PRUint32)((p)[1]) << 8) | \ | |
88 + ((PRUint32)((p)[2]) << 16) | ((PRUint32)((p)[3]) << 24) ) | |
89 + | |
90 +#define QUARTERROUND(a,b,c,d) \ | |
91 + x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \ | |
92 + x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \ | |
93 + x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \ | |
94 + x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7); | |
95 + | |
96 +static void ChaChaCore(unsigned char output[64], const PRUint32 input[16], | |
97 + int num_rounds) { | |
98 + PRUint32 x[16]; | |
99 + int i; | |
100 + | |
101 + memcpy(x, input, sizeof(PRUint32) * 16); | |
102 + for (i = num_rounds; i > 0; i -= 2) { | |
103 + QUARTERROUND( 0, 4, 8,12) | |
104 + QUARTERROUND( 1, 5, 9,13) | |
105 + QUARTERROUND( 2, 6,10,14) | |
106 + QUARTERROUND( 3, 7,11,15) | |
107 + QUARTERROUND( 0, 5,10,15) | |
108 + QUARTERROUND( 1, 6,11,12) | |
109 + QUARTERROUND( 2, 7, 8,13) | |
110 + QUARTERROUND( 3, 4, 9,14) | |
111 + } | |
112 + | |
113 + for (i = 0; i < 16; ++i) { | |
114 + x[i] = PLUS(x[i], input[i]); | |
115 + } | |
116 + for (i = 0; i < 16; ++i) { | |
117 + U32TO8_LITTLE(output + 4 * i, x[i]); | |
118 + } | |
119 +} | |
120 + | |
121 +static const unsigned char sigma[16] = "expand 32-byte k"; | |
122 + | |
123 +void ChaCha20XOR(unsigned char *out, const unsigned char *in, unsigned int inLen, | |
124 + const unsigned char key[32], const unsigned char nonce[8], | |
125 + uint64_t counter) { | |
126 + unsigned char block[64]; | |
127 + PRUint32 input[16]; | |
128 + unsigned int u; | |
129 + unsigned int i; | |
130 + | |
131 + input[4] = U8TO32_LITTLE(key + 0); | |
132 + input[5] = U8TO32_LITTLE(key + 4); | |
133 + input[6] = U8TO32_LITTLE(key + 8); | |
134 + input[7] = U8TO32_LITTLE(key + 12); | |
135 + | |
136 + input[8] = U8TO32_LITTLE(key + 16); | |
137 + input[9] = U8TO32_LITTLE(key + 20); | |
138 + input[10] = U8TO32_LITTLE(key + 24); | |
139 + input[11] = U8TO32_LITTLE(key + 28); | |
140 + | |
141 + input[0] = U8TO32_LITTLE(sigma + 0); | |
142 + input[1] = U8TO32_LITTLE(sigma + 4); | |
143 + input[2] = U8TO32_LITTLE(sigma + 8); | |
144 + input[3] = U8TO32_LITTLE(sigma + 12); | |
145 + | |
146 + input[12] = counter; | |
147 + input[13] = counter >> 32; | |
148 + input[14] = U8TO32_LITTLE(nonce + 0); | |
149 + input[15] = U8TO32_LITTLE(nonce + 4); | |
150 + | |
151 + while (inLen >= 64) { | |
152 + ChaChaCore(block, input, 20); | |
153 + for (i = 0; i < 64; i++) { | |
154 + out[i] = in[i] ^ block[i]; | |
155 + } | |
156 + | |
157 + input[12]++; | |
158 + if (input[12] == 0) { | |
159 + input[13]++; | |
160 + } | |
161 + | |
162 + inLen -= 64; | |
163 + in += 64; | |
164 + out += 64; | |
165 + } | |
166 + | |
167 + if (inLen > 0) { | |
168 + ChaChaCore(block, input, 20); | |
169 + for (i = 0; i < inLen; i++) { | |
170 + out[i] = in[i] ^ block[i]; | |
171 + } | |
172 + } | |
173 +} | |
174 diff -r c3565a90b8c4 lib/freebl/chacha20/chacha20.h | |
175 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 | |
176 +++ b/lib/freebl/chacha20/chacha20.h Tue Jan 07 12:11:36 2014 -0800 | |
177 @@ -0,0 +1,22 @@ | |
178 +/* | |
179 + * chacha20.h - header file for ChaCha20 implementation. | |
180 + * | |
181 + * This Source Code Form is subject to the terms of the Mozilla Public | |
182 + * License, v. 2.0. If a copy of the MPL was not distributed with this | |
183 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
184 + | |
185 +#ifndef FREEBL_CHACHA20_H_ | |
186 +#define FREEBL_CHACHA20_H_ | |
187 + | |
188 +#include <stdint.h> | |
189 + | |
190 +/* ChaCha20XOR encrypts |inLen| bytes from |in| with the given key and | |
191 + * nonce and writes the result to |out|, which may be equal to |in|. The | |
192 + * initial block counter is specified by |counter|. */ | |
193 +extern void ChaCha20XOR(unsigned char *out, | |
194 + const unsigned char *in, unsigned int inLen, | |
195 + const unsigned char key[32], | |
196 + const unsigned char nonce[8], | |
197 + uint64_t counter); | |
198 + | |
199 +#endif /* FREEBL_CHACHA20_H_ */ | |
200 diff -r c3565a90b8c4 lib/freebl/chacha20/chacha20_vec.c | |
201 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 | |
202 +++ b/lib/freebl/chacha20/chacha20_vec.c Tue Jan 07 12:11:36 2014 -0800 | |
203 @@ -0,0 +1,281 @@ | |
204 +/* This Source Code Form is subject to the terms of the Mozilla Public | |
205 + * License, v. 2.0. If a copy of the MPL was not distributed with this | |
206 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
207 + | |
208 +/* This implementation is by Ted Krovetz and was submitted to SUPERCOP and | |
209 + * marked as public domain. It was been altered to allow for non-aligned inputs | |
210 + * and to allow the block counter to be passed in specifically. */ | |
211 + | |
212 +#include <string.h> | |
213 + | |
214 +#include "chacha20.h" | |
215 + | |
216 +#ifndef CHACHA_RNDS | |
217 +#define CHACHA_RNDS 20 /* 8 (high speed), 20 (conservative), 12 (middle) */ | |
218 +#endif | |
219 + | |
220 +/* Architecture-neutral way to specify 16-byte vector of ints */ | |
221 +typedef unsigned vec __attribute__ ((vector_size (16))); | |
222 + | |
223 +/* This implementation is designed for Neon, SSE and AltiVec machines. The | |
224 + * following specify how to do certain vector operations efficiently on | |
225 + * each architecture, using intrinsics. | |
226 + * This implementation supports parallel processing of multiple blocks, | |
227 + * including potentially using general-purpose registers. | |
228 + */ | |
229 +#if __ARM_NEON__ | |
230 +#include <arm_neon.h> | |
231 +#define GPR_TOO 1 | |
232 +#define VBPI 2 | |
233 +#define ONE (vec)vsetq_lane_u32(1,vdupq_n_u32(0),0) | |
234 +#define LOAD(m) (vec)(*((vec*)(m))) | |
235 +#define STORE(m,r) (*((vec*)(m))) = (r) | |
236 +#define ROTV1(x) (vec)vextq_u32((uint32x4_t)x,(uint32x4_t)x,1) | |
237 +#define ROTV2(x) (vec)vextq_u32((uint32x4_t)x,(uint32x4_t)x,2) | |
238 +#define ROTV3(x) (vec)vextq_u32((uint32x4_t)x,(uint32x4_t)x,3) | |
239 +#define ROTW16(x) (vec)vrev32q_u16((uint16x8_t)x) | |
240 +#if __clang__ | |
241 +#define ROTW7(x) (x << ((vec){ 7, 7, 7, 7})) ^ (x >> ((vec){25,25,25,25})) | |
242 +#define ROTW8(x) (x << ((vec){ 8, 8, 8, 8})) ^ (x >> ((vec){24,24,24,24})) | |
243 +#define ROTW12(x) (x << ((vec){12,12,12,12})) ^ (x >> ((vec){20,20,20,20})) | |
244 +#else | |
245 +#define ROTW7(x) (vec)vsriq_n_u32(vshlq_n_u32((uint32x4_t)x,7),(uint32x4_t)x,25) | |
246 +#define ROTW8(x) (vec)vsriq_n_u32(vshlq_n_u32((uint32x4_t)x,8),(uint32x4_t)x,24) | |
247 +#define ROTW12(x) (vec)vsriq_n_u32(vshlq_n_u32((uint32x4_t)x,12),(uint32x4_t)x,20) | |
248 +#endif | |
249 +#elif __SSE2__ | |
250 +#include <emmintrin.h> | |
251 +#define GPR_TOO 0 | |
252 +#if __clang__ | |
253 +#define VBPI 4 | |
254 +#else | |
255 +#define VBPI 3 | |
256 +#endif | |
257 +#define ONE (vec)_mm_set_epi32(0,0,0,1) | |
258 +#define LOAD(m) (vec)_mm_loadu_si128((__m128i*)(m)) | |
259 +#define STORE(m,r) _mm_storeu_si128((__m128i*)(m), (__m128i) (r)) | |
260 +#define ROTV1(x) (vec)_mm_shuffle_epi32((__m128i)x,_MM_SHUFFLE(0,3,2,1)) | |
261 +#define ROTV2(x) (vec)_mm_shuffle_epi32((__m128i)x,_MM_SHUFFLE(1,0,3,2)) | |
262 +#define ROTV3(x) (vec)_mm_shuffle_epi32((__m128i)x,_MM_SHUFFLE(2,1,0,3)) | |
263 +#define ROTW7(x) (vec)(_mm_slli_epi32((__m128i)x, 7) ^ _mm_srli_epi32((__m128i)x,25)) | |
264 +#define ROTW12(x) (vec)(_mm_slli_epi32((__m128i)x,12) ^ _mm_srli_epi32((__m128i)x,20)) | |
265 +#if __SSSE3__ | |
266 +#include <tmmintrin.h> | |
267 +#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)) | |
268 +#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)) | |
269 +#else | |
270 +#define ROTW8(x) (vec)(_mm_slli_epi32((__m128i)x, 8) ^ _mm_srli_epi32((__m128i)x,24)) | |
271 +#define ROTW16(x) (vec)(_mm_slli_epi32((__m128i)x,16) ^ _mm_srli_epi32((__m128i)x,16)) | |
272 +#endif | |
273 +#else | |
274 +#error -- Implementation supports only machines with neon or SSE2 | |
275 +#endif | |
276 + | |
277 +#ifndef REVV_BE | |
278 +#define REVV_BE(x) (x) | |
279 +#endif | |
280 + | |
281 +#ifndef REVW_BE | |
282 +#define REVW_BE(x) (x) | |
283 +#endif | |
284 + | |
285 +#define BPI (VBPI + GPR_TOO) /* Blocks computed per loop iteration */ | |
286 + | |
287 +#define DQROUND_VECTORS(a,b,c,d) \ | |
288 + a += b; d ^= a; d = ROTW16(d); \ | |
289 + c += d; b ^= c; b = ROTW12(b); \ | |
290 + a += b; d ^= a; d = ROTW8(d); \ | |
291 + c += d; b ^= c; b = ROTW7(b); \ | |
292 + b = ROTV1(b); c = ROTV2(c); d = ROTV3(d); \ | |
293 + a += b; d ^= a; d = ROTW16(d); \ | |
294 + c += d; b ^= c; b = ROTW12(b); \ | |
295 + a += b; d ^= a; d = ROTW8(d); \ | |
296 + c += d; b ^= c; b = ROTW7(b); \ | |
297 + b = ROTV3(b); c = ROTV2(c); d = ROTV1(d); | |
298 + | |
299 +#define QROUND_WORDS(a,b,c,d) \ | |
300 + a = a+b; d ^= a; d = d<<16 | d>>16; \ | |
301 + c = c+d; b ^= c; b = b<<12 | b>>20; \ | |
302 + a = a+b; d ^= a; d = d<< 8 | d>>24; \ | |
303 + c = c+d; b ^= c; b = b<< 7 | b>>25; | |
304 + | |
305 +#define WRITE_XOR(in, op, d, v0, v1, v2, v3) \ | |
306 + STORE(op + d + 0, LOAD(in + d + 0) ^ REVV_BE(v0)); \ | |
307 + STORE(op + d + 4, LOAD(in + d + 4) ^ REVV_BE(v1)); \ | |
308 + STORE(op + d + 8, LOAD(in + d + 8) ^ REVV_BE(v2)); \ | |
309 + STORE(op + d +12, LOAD(in + d +12) ^ REVV_BE(v3)); | |
310 + | |
311 +void ChaCha20XOR( | |
312 + unsigned char *out, | |
313 + const unsigned char *in, | |
314 + unsigned int inlen, | |
315 + const unsigned char key[32], | |
316 + const unsigned char nonce[8], | |
317 + uint64_t counter) | |
318 +{ | |
319 + unsigned iters, i, *op=(unsigned *)out, *ip=(unsigned *)in, *kp; | |
320 +#if defined(__ARM_NEON__) | |
321 + unsigned *np; | |
322 +#endif | |
323 + vec s0, s1, s2, s3; | |
324 +#if !defined(__ARM_NEON__) && !defined(__SSE2__) | |
325 + __attribute__ ((aligned (16))) unsigned key[8], nonce[4]; | |
326 +#endif | |
327 + __attribute__ ((aligned (16))) unsigned chacha_const[] = | |
328 + {0x61707865,0x3320646E,0x79622D32,0x6B206574}; | |
329 +#if defined(__ARM_NEON__) || defined(__SSE2__) | |
330 + kp = (unsigned *)key; | |
331 +#else | |
332 + ((vec *)key)[0] = REVV_BE(((vec *)key)[0]); | |
333 + ((vec *)key)[1] = REVV_BE(((vec *)key)[1]); | |
334 + nonce[0] = REVW_BE(((unsigned *)nonce)[0]); | |
335 + nonce[1] = REVW_BE(((unsigned *)nonce)[1]); | |
336 + nonce[2] = REVW_BE(((unsigned *)nonce)[2]); | |
337 + nonce[3] = REVW_BE(((unsigned *)nonce)[3]); | |
338 + kp = (unsigned *)key; | |
339 + np = (unsigned *)nonce; | |
340 +#endif | |
341 +#if defined(__ARM_NEON__) | |
342 + np = (unsigned*) nonce; | |
343 +#endif | |
344 + s0 = LOAD(chacha_const); | |
345 + s1 = LOAD(&((vec*)kp)[0]); | |
346 + s2 = LOAD(&((vec*)kp)[1]); | |
347 + s3 = (vec) { | |
348 + counter & 0xffffffff, | |
349 + counter >> 32, | |
350 + ((uint32_t*)nonce)[0], | |
351 + ((uint32_t*)nonce)[1] | |
352 + }; | |
353 + | |
354 + for (iters = 0; iters < inlen/(BPI*64); iters++) { | |
355 +#if GPR_TOO | |
356 + register unsigned x0, x1, x2, x3, x4, x5, x6, x7, x8, | |
357 + x9, x10, x11, x12, x13, x14, x15; | |
358 +#endif | |
359 +#if VBPI > 2 | |
360 + vec v8,v9,v10,v11; | |
361 +#endif | |
362 +#if VBPI > 3 | |
363 + vec v12,v13,v14,v15; | |
364 +#endif | |
365 + | |
366 + vec v0,v1,v2,v3,v4,v5,v6,v7; | |
367 + v4 = v0 = s0; v5 = v1 = s1; v6 = v2 = s2; v3 = s3; | |
368 + v7 = v3 + ONE; | |
369 +#if VBPI > 2 | |
370 + v8 = v4; v9 = v5; v10 = v6; | |
371 + v11 = v7 + ONE; | |
372 +#endif | |
373 +#if VBPI > 3 | |
374 + v12 = v8; v13 = v9; v14 = v10; | |
375 + v15 = v11 + ONE; | |
376 +#endif | |
377 +#if GPR_TOO | |
378 + x0 = chacha_const[0]; x1 = chacha_const[1]; | |
379 + x2 = chacha_const[2]; x3 = chacha_const[3]; | |
380 + x4 = kp[0]; x5 = kp[1]; x6 = kp[2]; x7 = kp[3]; | |
381 + x8 = kp[4]; x9 = kp[5]; x10 = kp[6]; x11 = kp[7]; | |
382 + x12 = (counter & 0xffffffff)+BPI*iters+(BPI-1); x13 = counter >> 32; | |
383 + x14 = np[0]; x15 = np[1]; | |
384 +#endif | |
385 + for (i = CHACHA_RNDS/2; i; i--) { | |
386 + DQROUND_VECTORS(v0,v1,v2,v3) | |
387 + DQROUND_VECTORS(v4,v5,v6,v7) | |
388 +#if VBPI > 2 | |
389 + DQROUND_VECTORS(v8,v9,v10,v11) | |
390 +#endif | |
391 +#if VBPI > 3 | |
392 + DQROUND_VECTORS(v12,v13,v14,v15) | |
393 +#endif | |
394 +#if GPR_TOO | |
395 + QROUND_WORDS( x0, x4, x8,x12) | |
396 + QROUND_WORDS( x1, x5, x9,x13) | |
397 + QROUND_WORDS( x2, x6,x10,x14) | |
398 + QROUND_WORDS( x3, x7,x11,x15) | |
399 + QROUND_WORDS( x0, x5,x10,x15) | |
400 + QROUND_WORDS( x1, x6,x11,x12) | |
401 + QROUND_WORDS( x2, x7, x8,x13) | |
402 + QROUND_WORDS( x3, x4, x9,x14) | |
403 +#endif | |
404 + } | |
405 + | |
406 + WRITE_XOR(ip, op, 0, v0+s0, v1+s1, v2+s2, v3+s3) | |
407 + s3 += ONE; | |
408 + WRITE_XOR(ip, op, 16, v4+s0, v5+s1, v6+s2, v7+s3) | |
409 + s3 += ONE; | |
410 +#if VBPI > 2 | |
411 + WRITE_XOR(ip, op, 32, v8+s0, v9+s1, v10+s2, v11+s3) | |
412 + s3 += ONE; | |
413 +#endif | |
414 +#if VBPI > 3 | |
415 + WRITE_XOR(ip, op, 48, v12+s0, v13+s1, v14+s2, v15+s3) | |
416 + s3 += ONE; | |
417 +#endif | |
418 + ip += VBPI*16; | |
419 + op += VBPI*16; | |
420 +#if GPR_TOO | |
421 + op[0] = REVW_BE(REVW_BE(ip[0]) ^ (x0 + chacha_const[0])); | |
422 + op[1] = REVW_BE(REVW_BE(ip[1]) ^ (x1 + chacha_const[1])); | |
423 + op[2] = REVW_BE(REVW_BE(ip[2]) ^ (x2 + chacha_const[2])); | |
424 + op[3] = REVW_BE(REVW_BE(ip[3]) ^ (x3 + chacha_const[3])); | |
425 + op[4] = REVW_BE(REVW_BE(ip[4]) ^ (x4 + kp[0])); | |
426 + op[5] = REVW_BE(REVW_BE(ip[5]) ^ (x5 + kp[1])); | |
427 + op[6] = REVW_BE(REVW_BE(ip[6]) ^ (x6 + kp[2])); | |
428 + op[7] = REVW_BE(REVW_BE(ip[7]) ^ (x7 + kp[3])); | |
429 + op[8] = REVW_BE(REVW_BE(ip[8]) ^ (x8 + kp[4])); | |
430 + op[9] = REVW_BE(REVW_BE(ip[9]) ^ (x9 + kp[5])); | |
431 + op[10] = REVW_BE(REVW_BE(ip[10]) ^ (x10 + kp[6])); | |
432 + op[11] = REVW_BE(REVW_BE(ip[11]) ^ (x11 + kp[7])); | |
433 + op[12] = REVW_BE(REVW_BE(ip[12]) ^ (x12 + (counter & 0xffffffff)+BPI*iters+(BPI-1))); | |
434 + op[13] = REVW_BE(REVW_BE(ip[13]) ^ (x13 + (counter >> 32))); | |
435 + op[14] = REVW_BE(REVW_BE(ip[14]) ^ (x14 + np[0])); | |
436 + op[15] = REVW_BE(REVW_BE(ip[15]) ^ (x15 + np[1])); | |
437 + s3 += ONE; | |
438 + ip += 16; | |
439 + op += 16; | |
440 +#endif | |
441 + } | |
442 + | |
443 + for (iters = inlen%(BPI*64)/64; iters != 0; iters--) { | |
444 + vec v0 = s0, v1 = s1, v2 = s2, v3 = s3; | |
445 + for (i = CHACHA_RNDS/2; i; i--) { | |
446 + DQROUND_VECTORS(v0,v1,v2,v3); | |
447 + } | |
448 + WRITE_XOR(ip, op, 0, v0+s0, v1+s1, v2+s2, v3+s3) | |
449 + s3 += ONE; | |
450 + ip += 16; | |
451 + op += 16; | |
452 + } | |
453 + | |
454 + inlen = inlen % 64; | |
455 + if (inlen) { | |
456 + __attribute__ ((aligned (16))) vec buf[4]; | |
457 + vec v0,v1,v2,v3; | |
458 + v0 = s0; v1 = s1; v2 = s2; v3 = s3; | |
459 + for (i = CHACHA_RNDS/2; i; i--) { | |
460 + DQROUND_VECTORS(v0,v1,v2,v3); | |
461 + } | |
462 + | |
463 + if (inlen >= 16) { | |
464 + STORE(op + 0, LOAD(ip + 0) ^ REVV_BE(v0 + s0)); | |
465 + if (inlen >= 32) { | |
466 + STORE(op + 4, LOAD(ip + 4) ^ REVV_BE(v1 + s1)); | |
467 + if (inlen >= 48) { | |
468 + STORE(op + 8, LOAD(ip + 8) ^ REVV_BE(v2 + s2)); | |
469 + buf[3] = REVV_BE(v3 + s3); | |
470 + } else { | |
471 + buf[2] = REVV_BE(v2 + s2); | |
472 + } | |
473 + } else { | |
474 + buf[1] = REVV_BE(v1 + s1); | |
475 + } | |
476 + } else { | |
477 + buf[0] = REVV_BE(v0 + s0); | |
478 + } | |
479 + | |
480 + for (i=inlen & ~15; i<inlen; i++) { | |
481 + ((char *)op)[i] = ((char *)ip)[i] ^ ((char *)buf)[i]; | |
482 + } | |
483 + } | |
484 +} | |
485 diff -r c3565a90b8c4 lib/freebl/chacha20poly1305.c | |
486 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 | |
487 +++ b/lib/freebl/chacha20poly1305.c Tue Jan 07 12:11:36 2014 -0800 | |
488 @@ -0,0 +1,169 @@ | |
489 +/* This Source Code Form is subject to the terms of the Mozilla Public | |
490 + * License, v. 2.0. If a copy of the MPL was not distributed with this | |
491 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
492 + | |
493 +#ifdef FREEBL_NO_DEPEND | |
494 +#include "stubs.h" | |
495 +#endif | |
496 + | |
497 +#include <string.h> | |
498 +#include <stdio.h> | |
499 + | |
500 +#include "seccomon.h" | |
501 +#include "secerr.h" | |
502 +#include "blapit.h" | |
503 +#include "poly1305/poly1305.h" | |
504 +#include "chacha20/chacha20.h" | |
505 +#include "chacha20poly1305.h" | |
506 + | |
507 +/* Poly1305Do writes the Poly1305 authenticator of the given additional data | |
508 + * and ciphertext to |out|. */ | |
509 +static void | |
510 +Poly1305Do(unsigned char *out, | |
511 + const unsigned char *ad, unsigned int adLen, | |
512 + const unsigned char *ciphertext, unsigned int ciphertextLen, | |
513 + const unsigned char key[32]) | |
514 +{ | |
515 + poly1305_state state; | |
516 + unsigned int j; | |
517 + unsigned char lengthBytes[8]; | |
518 + unsigned int i; | |
519 + | |
520 + Poly1305Init(&state, key); | |
521 + j = adLen; | |
522 + for (i = 0; i < sizeof(lengthBytes); i++) { | |
523 + lengthBytes[i] = j; | |
524 + j >>= 8; | |
525 + } | |
526 + Poly1305Update(&state, ad, adLen); | |
527 + Poly1305Update(&state, lengthBytes, sizeof(lengthBytes)); | |
528 + j = ciphertextLen; | |
529 + for (i = 0; i < sizeof(lengthBytes); i++) { | |
530 + lengthBytes[i] = j; | |
531 + j >>= 8; | |
532 + } | |
533 + Poly1305Update(&state, ciphertext, ciphertextLen); | |
534 + Poly1305Update(&state, lengthBytes, sizeof(lengthBytes)); | |
535 + Poly1305Finish(&state, out); | |
536 +} | |
537 + | |
538 +SECStatus | |
539 +ChaCha20Poly1305_InitContext(ChaCha20Poly1305Context *ctx, | |
540 + const unsigned char *key, unsigned int keyLen, | |
541 + unsigned int tagLen) | |
542 +{ | |
543 + if (keyLen != 32) { | |
544 + PORT_SetError(SEC_ERROR_BAD_KEY); | |
545 + return SECFailure; | |
546 + } | |
547 + if (tagLen == 0 || tagLen > 16) { | |
548 + PORT_SetError(SEC_ERROR_INPUT_LEN); | |
549 + return SECFailure; | |
550 + } | |
551 + | |
552 + memcpy(ctx->key, key, sizeof(ctx->key)); | |
553 + ctx->tagLen = tagLen; | |
554 + | |
555 + return SECSuccess; | |
556 +} | |
557 + | |
558 +ChaCha20Poly1305Context * | |
559 +ChaCha20Poly1305_CreateContext(const unsigned char *key, unsigned int keyLen, | |
560 + unsigned int tagLen) | |
561 +{ | |
562 + ChaCha20Poly1305Context *ctx; | |
563 + | |
564 + ctx = PORT_New(ChaCha20Poly1305Context); | |
565 + if (ctx == NULL) { | |
566 + return NULL; | |
567 + } | |
568 + | |
569 + if (ChaCha20Poly1305_InitContext(ctx, key, keyLen, tagLen) != SECSuccess) { | |
570 + PORT_Free(ctx); | |
571 + ctx = NULL; | |
572 + } | |
573 + | |
574 + return ctx; | |
575 +} | |
576 + | |
577 +void | |
578 +ChaCha20Poly1305_DestroyContext(ChaCha20Poly1305Context *ctx, PRBool freeit) | |
579 +{ | |
580 + memset(ctx, 0, sizeof(*ctx)); | |
581 + if (freeit) { | |
582 + PORT_Free(ctx); | |
583 + } | |
584 +} | |
585 + | |
586 +SECStatus | |
587 +ChaCha20Poly1305_Seal(const ChaCha20Poly1305Context *ctx, | |
588 + unsigned char *output, unsigned int *outputLen, | |
589 + unsigned int maxOutputLen, | |
590 + const unsigned char *input, unsigned int inputLen, | |
591 + const unsigned char *nonce, unsigned int nonceLen, | |
592 + const unsigned char *ad, unsigned int adLen) | |
593 +{ | |
594 + unsigned char block[64]; | |
595 + unsigned char tag[16]; | |
596 + | |
597 + if (nonceLen != 8) { | |
598 + PORT_SetError(SEC_ERROR_INPUT_LEN); | |
599 + return SECFailure; | |
600 + } | |
601 + *outputLen = inputLen + ctx->tagLen; | |
602 + if (maxOutputLen < *outputLen) { | |
603 + PORT_SetError(SEC_ERROR_OUTPUT_LEN); | |
604 + return SECFailure; | |
605 + } | |
606 + | |
607 + memset(block, 0, sizeof(block)); | |
608 + // Generate a block of keystream. The first 32 bytes will be the poly1305 | |
609 + // key. The remainder of the block is discarded. | |
610 + ChaCha20XOR(block, block, sizeof(block), ctx->key, nonce, 0); | |
611 + ChaCha20XOR(output, input, inputLen, ctx->key, nonce, 1); | |
612 + | |
613 + Poly1305Do(tag, ad, adLen, output, inputLen, block); | |
614 + memcpy(output + inputLen, tag, ctx->tagLen); | |
615 + | |
616 + return SECSuccess; | |
617 +} | |
618 + | |
619 +SECStatus | |
620 +ChaCha20Poly1305_Open(const ChaCha20Poly1305Context *ctx, | |
621 + unsigned char *output, unsigned int *outputLen, | |
622 + unsigned int maxOutputLen, | |
623 + const unsigned char *input, unsigned int inputLen, | |
624 + const unsigned char *nonce, unsigned int nonceLen, | |
625 + const unsigned char *ad, unsigned int adLen) | |
626 +{ | |
627 + unsigned char block[64]; | |
628 + unsigned char tag[16]; | |
629 + | |
630 + if (nonceLen != 8) { | |
631 + PORT_SetError(SEC_ERROR_INPUT_LEN); | |
632 + return SECFailure; | |
633 + } | |
634 + if (inputLen < ctx->tagLen) { | |
635 + PORT_SetError(SEC_ERROR_INPUT_LEN); | |
636 + return SECFailure; | |
637 + } | |
638 + *outputLen = inputLen - ctx->tagLen; | |
639 + if (maxOutputLen < *outputLen) { | |
640 + PORT_SetError(SEC_ERROR_OUTPUT_LEN); | |
641 + return SECFailure; | |
642 + } | |
643 + | |
644 + memset(block, 0, sizeof(block)); | |
645 + // Generate a block of keystream. The first 32 bytes will be the poly1305 | |
646 + // key. The remainder of the block is discarded. | |
647 + ChaCha20XOR(block, block, sizeof(block), ctx->key, nonce, 0); | |
648 + Poly1305Do(tag, ad, adLen, input, inputLen - ctx->tagLen, block); | |
649 + if (NSS_SecureMemcmp(tag, &input[inputLen - ctx->tagLen], ctx->tagLen) != 0) { | |
650 + PORT_SetError(SEC_ERROR_BAD_DATA); | |
651 + return SECFailure; | |
652 + } | |
653 + | |
654 + ChaCha20XOR(output, input, inputLen - ctx->tagLen, ctx->key, nonce, 1); | |
655 + | |
656 + return SECSuccess; | |
657 +} | |
658 diff -r c3565a90b8c4 lib/freebl/chacha20poly1305.h | |
659 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 | |
660 +++ b/lib/freebl/chacha20poly1305.h Tue Jan 07 12:11:36 2014 -0800 | |
661 @@ -0,0 +1,15 @@ | |
662 +/* This Source Code Form is subject to the terms of the Mozilla Public | |
663 + * License, v. 2.0. If a copy of the MPL was not distributed with this | |
664 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
665 + | |
666 +#ifndef _CHACHA20_POLY1305_H_ | |
667 +#define _CHACHA20_POLY1305_H_ 1 | |
668 + | |
669 +/* ChaCha20Poly1305ContextStr saves the key and tag length for a | |
670 + * ChaCha20+Poly1305 AEAD operation. */ | |
671 +struct ChaCha20Poly1305ContextStr { | |
672 + unsigned char key[32]; | |
673 + unsigned char tagLen; | |
674 +}; | |
675 + | |
676 +#endif /* _CHACHA20_POLY1305_H_ */ | |
677 diff -r c3565a90b8c4 lib/freebl/poly1305/poly1305-donna-x64-sse2-incremental-source.c | |
678 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 | |
679 +++ b/lib/freebl/poly1305/poly1305-donna-x64-sse2-incremental-source.c Tue Jan 07 12:11:36 2014 -0800 | |
680 @@ -0,0 +1,623 @@ | |
681 +/* This Source Code Form is subject to the terms of the Mozilla Public | |
682 + * License, v. 2.0. If a copy of the MPL was not distributed with this | |
683 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
684 + | |
685 +/* This implementation of poly1305 is by Andrew Moon | |
686 + * (https://github.com/floodyberry/poly1305-donna) and released as public | |
687 + * domain. It implements SIMD vectorization based on the algorithm described in | |
688 + * http://cr.yp.to/papers.html#neoncrypto. Unrolled to 2 powers, i.e. 64 byte | |
689 + * block size. */ | |
690 + | |
691 +#include <emmintrin.h> | |
692 +#include <stdint.h> | |
693 + | |
694 +#include "poly1305.h" | |
695 + | |
696 +#define ALIGN(x) __attribute__((aligned(x))) | |
697 +#define INLINE inline | |
698 +#define U8TO64_LE(m) (*(uint64_t*)(m)) | |
699 +#define U8TO32_LE(m) (*(uint32_t*)(m)) | |
700 +#define U64TO8_LE(m,v) (*(uint64_t*)(m)) = v | |
701 + | |
702 +typedef __m128i xmmi; | |
703 +typedef unsigned __int128 uint128_t; | |
704 + | |
705 +static const uint32_t ALIGN(16) poly1305_x64_sse2_message_mask[4] = {(1 << 26) - 1, 0, (1 << 26) - 1, 0}; | |
706 +static const uint32_t ALIGN(16) poly1305_x64_sse2_5[4] = {5, 0, 5, 0}; | |
707 +static const uint32_t ALIGN(16) poly1305_x64_sse2_1shl128[4] = {(1 << 24), 0, (1 << 24), 0}; | |
708 + | |
709 +static uint128_t INLINE | |
710 +add128(uint128_t a, uint128_t b) { | |
711 + return a + b; | |
712 +} | |
713 + | |
714 +static uint128_t INLINE | |
715 +add128_64(uint128_t a, uint64_t b) { | |
716 + return a + b; | |
717 +} | |
718 + | |
719 +static uint128_t INLINE | |
720 +mul64x64_128(uint64_t a, uint64_t b) { | |
721 + return (uint128_t)a * b; | |
722 +} | |
723 + | |
724 +static uint64_t INLINE | |
725 +lo128(uint128_t a) { | |
726 + return (uint64_t)a; | |
727 +} | |
728 + | |
729 +static uint64_t INLINE | |
730 +shr128(uint128_t v, const int shift) { | |
731 + return (uint64_t)(v >> shift); | |
732 +} | |
733 + | |
734 +static uint64_t INLINE | |
735 +shr128_pair(uint64_t hi, uint64_t lo, const int shift) { | |
736 + return (uint64_t)((((uint128_t)hi << 64) | lo) >> shift); | |
737 +} | |
738 + | |
739 +typedef struct poly1305_power_t { | |
740 + union { | |
741 + xmmi v; | |
742 + uint64_t u[2]; | |
743 + uint32_t d[4]; | |
744 + } R20,R21,R22,R23,R24,S21,S22,S23,S24; | |
745 +} poly1305_power; | |
746 + | |
747 +typedef struct poly1305_state_internal_t { | |
748 + poly1305_power P[2]; /* 288 bytes, top 32 bit halves unused = 144 bytes of free storage */ | |
749 + union { | |
750 + xmmi H[5]; /* 80 bytes */ | |
751 + uint64_t HH[10]; | |
752 + }; | |
753 + /* uint64_t r0,r1,r2; [24 bytes] */ | |
754 + /* uint64_t pad0,pad1; [16 bytes] */ | |
755 + uint64_t started; /* 8 bytes */ | |
756 + uint64_t leftover; /* 8 bytes */ | |
757 + uint8_t buffer[64]; /* 64 bytes */ | |
758 +} poly1305_state_internal; /* 448 bytes total + 63 bytes for alignment = 511 bytes raw */ | |
759 + | |
760 +static poly1305_state_internal INLINE | |
761 +*poly1305_aligned_state(poly1305_state *state) { | |
762 + return (poly1305_state_internal *)(((uint64_t)state + 63) & ~63); | |
763 +} | |
764 + | |
765 +/* copy 0-63 bytes */ | |
766 +static void INLINE | |
767 +poly1305_block_copy(uint8_t *dst, const uint8_t *src, size_t bytes) { | |
768 + size_t offset = src - dst; | |
769 + if (bytes & 32) { | |
770 + _mm_storeu_si128((xmmi *)(dst + 0), _mm_loadu_si128((xmmi *)(dst + offset + 0))); | |
771 + _mm_storeu_si128((xmmi *)(dst + 16), _mm_loadu_si128((xmmi *)(dst + offset + 16))); | |
772 + dst += 32; | |
773 + } | |
774 + if (bytes & 16) { _mm_storeu_si128((xmmi *)dst, _mm_loadu_si128((xmmi *)(dst + offset))); dst += 16; } | |
775 + if (bytes & 8) { *(uint64_t *)dst = *(uint64_t *)(dst + offset); dst += 8; } | |
776 + if (bytes & 4) { *(uint32_t *)dst = *(uint32_t *)(dst + offset); dst += 4; } | |
777 + if (bytes & 2) { *(uint16_t *)dst = *(uint16_t *)(dst + offset); dst += 2; } | |
778 + if (bytes & 1) { *( uint8_t *)dst = *( uint8_t *)(dst + offset); } | |
779 +} | |
780 + | |
781 +/* zero 0-15 bytes */ | |
782 +static void INLINE | |
783 +poly1305_block_zero(uint8_t *dst, size_t bytes) { | |
784 + if (bytes & 8) { *(uint64_t *)dst = 0; dst += 8; } | |
785 + if (bytes & 4) { *(uint32_t *)dst = 0; dst += 4; } | |
786 + if (bytes & 2) { *(uint16_t *)dst = 0; dst += 2; } | |
787 + if (bytes & 1) { *( uint8_t *)dst = 0; } | |
788 +} | |
789 + | |
790 +static size_t INLINE | |
791 +poly1305_min(size_t a, size_t b) { | |
792 + return (a < b) ? a : b; | |
793 +} | |
794 + | |
795 +void | |
796 +Poly1305Init(poly1305_state *state, const unsigned char key[32]) { | |
797 + poly1305_state_internal *st = poly1305_aligned_state(state); | |
798 + poly1305_power *p; | |
799 + uint64_t r0,r1,r2; | |
800 + uint64_t t0,t1; | |
801 + | |
802 + /* clamp key */ | |
803 + t0 = U8TO64_LE(key + 0); | |
804 + t1 = U8TO64_LE(key + 8); | |
805 + r0 = t0 & 0xffc0fffffff; t0 >>= 44; t0 |= t1 << 20; | |
806 + r1 = t0 & 0xfffffc0ffff; t1 >>= 24; | |
807 + r2 = t1 & 0x00ffffffc0f; | |
808 + | |
809 + /* store r in un-used space of st->P[1] */ | |
810 + p = &st->P[1]; | |
811 + p->R20.d[1] = (uint32_t)(r0 ); | |
812 + p->R20.d[3] = (uint32_t)(r0 >> 32); | |
813 + p->R21.d[1] = (uint32_t)(r1 ); | |
814 + p->R21.d[3] = (uint32_t)(r1 >> 32); | |
815 + p->R22.d[1] = (uint32_t)(r2 ); | |
816 + p->R22.d[3] = (uint32_t)(r2 >> 32); | |
817 + | |
818 + /* store pad */ | |
819 + p->R23.d[1] = U8TO32_LE(key + 16); | |
820 + p->R23.d[3] = U8TO32_LE(key + 20); | |
821 + p->R24.d[1] = U8TO32_LE(key + 24); | |
822 + p->R24.d[3] = U8TO32_LE(key + 28); | |
823 + | |
824 + /* H = 0 */ | |
825 + st->H[0] = _mm_setzero_si128(); | |
826 + st->H[1] = _mm_setzero_si128(); | |
827 + st->H[2] = _mm_setzero_si128(); | |
828 + st->H[3] = _mm_setzero_si128(); | |
829 + st->H[4] = _mm_setzero_si128(); | |
830 + | |
831 + st->started = 0; | |
832 + st->leftover = 0; | |
833 +} | |
834 + | |
835 +static void | |
836 +poly1305_first_block(poly1305_state_internal *st, const uint8_t *m) { | |
837 + const xmmi MMASK = _mm_load_si128((xmmi *)poly1305_x64_sse2_message_mask); | |
838 + const xmmi FIVE = _mm_load_si128((xmmi*)poly1305_x64_sse2_5); | |
839 + const xmmi HIBIT = _mm_load_si128((xmmi*)poly1305_x64_sse2_1shl128); | |
840 + xmmi T5,T6; | |
841 + poly1305_power *p; | |
842 + uint128_t d[3]; | |
843 + uint64_t r0,r1,r2; | |
844 + uint64_t r20,r21,r22,s22; | |
845 + uint64_t pad0,pad1; | |
846 + uint64_t c; | |
847 + uint64_t i; | |
848 + | |
849 + /* pull out stored info */ | |
850 + p = &st->P[1]; | |
851 + | |
852 + r0 = ((uint64_t)p->R20.d[3] << 32) | (uint64_t)p->R20.d[1]; | |
853 + r1 = ((uint64_t)p->R21.d[3] << 32) | (uint64_t)p->R21.d[1]; | |
854 + r2 = ((uint64_t)p->R22.d[3] << 32) | (uint64_t)p->R22.d[1]; | |
855 + pad0 = ((uint64_t)p->R23.d[3] << 32) | (uint64_t)p->R23.d[1]; | |
856 + pad1 = ((uint64_t)p->R24.d[3] << 32) | (uint64_t)p->R24.d[1]; | |
857 + | |
858 + /* compute powers r^2,r^4 */ | |
859 + r20 = r0; | |
860 + r21 = r1; | |
861 + r22 = r2; | |
862 + for (i = 0; i < 2; i++) { | |
863 + s22 = r22 * (5 << 2); | |
864 + | |
865 + d[0] = add128(mul64x64_128(r20, r20), mul64x64_128(r21 * 2, s22)); | |
866 + d[1] = add128(mul64x64_128(r22, s22), mul64x64_128(r20 * 2, r21)); | |
867 + d[2] = add128(mul64x64_128(r21, r21), mul64x64_128(r22 * 2, r20)); | |
868 + | |
869 + r20 = lo128(d[0]) & 0xfffffffffff; c = shr128(d[0], 44); | |
870 + d[1] = add128_64(d[1], c); r21 = lo128(d[1]) & 0xfffffffffff; c = shr128(d[1], 44); | |
871 + d[2] = add128_64(d[2], c); r22 = lo128(d[2]) & 0x3ffffffffff; c = shr128(d[2], 42); | |
872 + r20 += c * 5; c = (r20 >> 44); r20 = r20 & 0xfffffffffff; | |
873 + r21 += c; | |
874 + | |
875 + p->R20.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)( r20 ) & 0x3ffffff), _MM_SHUFFLE(1,0,1,0)); | |
876 + p->R21.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)((r20 >> 26) | (r21 << 18)) & 0x3ffffff), _MM_SHUFFLE(1,0,1,0)); | |
877 + p->R22.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)((r21 >> 8) ) & 0x3ffffff), _MM_SHUFFLE(1,0,1,0)); | |
878 + p->R23.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)((r21 >> 34) | (r22 << 10)) & 0x3ffffff), _MM_SHUFFLE(1,0,1,0)); | |
879 + p->R24.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)((r22 >> 16) ) ), _MM_SHUFFLE(1,0,1,0)); | |
880 + p->S21.v = _mm_mul_epu32(p->R21.v, FIVE); | |
881 + p->S22.v = _mm_mul_epu32(p->R22.v, FIVE); | |
882 + p->S23.v = _mm_mul_epu32(p->R23.v, FIVE); | |
883 + p->S24.v = _mm_mul_epu32(p->R24.v, FIVE); | |
884 + p--; | |
885 + } | |
886 + | |
887 + /* put saved info back */ | |
888 + p = &st->P[1]; | |
889 + p->R20.d[1] = (uint32_t)(r0 ); | |
890 + p->R20.d[3] = (uint32_t)(r0 >> 32); | |
891 + p->R21.d[1] = (uint32_t)(r1 ); | |
892 + p->R21.d[3] = (uint32_t)(r1 >> 32); | |
893 + p->R22.d[1] = (uint32_t)(r2 ); | |
894 + p->R22.d[3] = (uint32_t)(r2 >> 32); | |
895 + p->R23.d[1] = (uint32_t)(pad0 ); | |
896 + p->R23.d[3] = (uint32_t)(pad0 >> 32); | |
897 + p->R24.d[1] = (uint32_t)(pad1 ); | |
898 + p->R24.d[3] = (uint32_t)(pad1 >> 32); | |
899 + | |
900 + /* H = [Mx,My] */ | |
901 + T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 0)), _mm_loadl_epi64((xmmi *)(m + 16))); | |
902 + T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 8)), _mm_loadl_epi64((xmmi *)(m + 24))); | |
903 + st->H[0] = _mm_and_si128(MMASK, T5); | |
904 + st->H[1] = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); | |
905 + T5 = _mm_or_si128(_mm_srli_epi64(T5, 52), _mm_slli_epi64(T6, 12)); | |
906 + st->H[2] = _mm_and_si128(MMASK, T5); | |
907 + st->H[3] = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); | |
908 + st->H[4] = _mm_or_si128(_mm_srli_epi64(T6, 40), HIBIT); | |
909 +} | |
910 + | |
911 +static void | |
912 +poly1305_blocks(poly1305_state_internal *st, const uint8_t *m, size_t bytes) { | |
913 + const xmmi MMASK = _mm_load_si128((xmmi *)poly1305_x64_sse2_message_mask); | |
914 + const xmmi FIVE = _mm_load_si128((xmmi*)poly1305_x64_sse2_5); | |
915 + const xmmi HIBIT = _mm_load_si128((xmmi*)poly1305_x64_sse2_1shl128); | |
916 + | |
917 + poly1305_power *p; | |
918 + xmmi H0,H1,H2,H3,H4; | |
919 + xmmi T0,T1,T2,T3,T4,T5,T6; | |
920 + xmmi M0,M1,M2,M3,M4; | |
921 + xmmi C1,C2; | |
922 + | |
923 + H0 = st->H[0]; | |
924 + H1 = st->H[1]; | |
925 + H2 = st->H[2]; | |
926 + H3 = st->H[3]; | |
927 + H4 = st->H[4]; | |
928 + | |
929 + while (bytes >= 64) { | |
930 + /* H *= [r^4,r^4] */ | |
931 + p = &st->P[0]; | |
932 + T0 = _mm_mul_epu32(H0, p->R20.v); | |
933 + T1 = _mm_mul_epu32(H0, p->R21.v); | |
934 + T2 = _mm_mul_epu32(H0, p->R22.v); | |
935 + T3 = _mm_mul_epu32(H0, p->R23.v); | |
936 + T4 = _mm_mul_epu32(H0, p->R24.v); | |
937 + 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); | |
938 + 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); | |
939 + 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); | |
940 + 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); | |
941 + 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); | |
942 + 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); | |
943 + 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); | |
944 + 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); | |
945 + T5 = _mm_mul_epu32(H1, p->R23.v); T4 = _mm_add_epi64(T4, T5); | |
946 + T5 = _mm_mul_epu32(H2, p->R22.v); T4 = _mm_add_epi64(T4, T5); | |
947 + T5 = _mm_mul_epu32(H3, p->R21.v); T4 = _mm_add_epi64(T4, T5); | |
948 + T5 = _mm_mul_epu32(H4, p->R20.v); T4 = _mm_add_epi64(T4, T5); | |
949 + | |
950 + /* H += [Mx,My]*[r^2,r^2] */ | |
951 + T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 0)), _mm_loadl_epi64((xmmi *)(m + 16))); | |
952 + T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 8)), _mm_loadl_epi64((xmmi *)(m + 24))); | |
953 + M0 = _mm_and_si128(MMASK, T5); | |
954 + M1 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); | |
955 + T5 = _mm_or_si128(_mm_srli_epi64(T5, 52), _mm_slli_epi64(T6, 12)); | |
956 + M2 = _mm_and_si128(MMASK, T5); | |
957 + M3 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); | |
958 + M4 = _mm_or_si128(_mm_srli_epi64(T6, 40), HIBIT); | |
959 + | |
960 + p = &st->P[1]; | |
961 + 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); | |
962 + 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); | |
963 + 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); | |
964 + 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); | |
965 + 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); | |
966 + 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); | |
967 + 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); | |
968 + 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); | |
969 + 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); | |
970 + 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); | |
971 + T5 = _mm_mul_epu32(M0, p->R24.v); T4 = _mm_add_epi64(T4, T5); | |
972 + T5 = _mm_mul_epu32(M1, p->R23.v); T4 = _mm_add_epi64(T4, T5); | |
973 + T5 = _mm_mul_epu32(M2, p->R22.v); T4 = _mm_add_epi64(T4, T5); | |
974 + T5 = _mm_mul_epu32(M3, p->R21.v); T4 = _mm_add_epi64(T4, T5); | |
975 + T5 = _mm_mul_epu32(M4, p->R20.v); T4 = _mm_add_epi64(T4, T5); | |
976 + | |
977 + /* H += [Mx,My] */ | |
978 + T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 32)), _mm_loadl_epi64((xmmi *)(m + 48))); | |
979 + T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 40)), _mm_loadl_epi64((xmmi *)(m + 56))); | |
980 + M0 = _mm_and_si128(MMASK, T5); | |
981 + M1 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); | |
982 + T5 = _mm_or_si128(_mm_srli_epi64(T5, 52), _mm_slli_epi64(T6, 12)); | |
983 + M2 = _mm_and_si128(MMASK, T5); | |
984 + M3 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); | |
985 + M4 = _mm_or_si128(_mm_srli_epi64(T6, 40), HIBIT); | |
986 + | |
987 + T0 = _mm_add_epi64(T0, M0); | |
988 + T1 = _mm_add_epi64(T1, M1); | |
989 + T2 = _mm_add_epi64(T2, M2); | |
990 + T3 = _mm_add_epi64(T3, M3); | |
991 + T4 = _mm_add_epi64(T4, M4); | |
992 + | |
993 + /* reduce */ | |
994 + 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); | |
995 + 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)); | |
996 + 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); | |
997 + C1 = _mm_srli_epi64(T3, 26); T3 = _mm_and_si128(T3, MMASK); T4 = _mm_add_epi64(T4, C1); | |
998 + | |
999 + /* H = (H*[r^4,r^4] + [Mx,My]*[r^2,r^2] + [Mx,My]) */ | |
1000 + H0 = T0; | |
1001 + H1 = T1; | |
1002 + H2 = T2; | |
1003 + H3 = T3; | |
1004 + H4 = T4; | |
1005 + | |
1006 + m += 64; | |
1007 + bytes -= 64; | |
1008 + } | |
1009 + | |
1010 + st->H[0] = H0; | |
1011 + st->H[1] = H1; | |
1012 + st->H[2] = H2; | |
1013 + st->H[3] = H3; | |
1014 + st->H[4] = H4; | |
1015 +} | |
1016 + | |
1017 +static size_t | |
1018 +poly1305_combine(poly1305_state_internal *st, const uint8_t *m, size_t bytes) { | |
1019 + const xmmi MMASK = _mm_load_si128((xmmi *)poly1305_x64_sse2_message_mask); | |
1020 + const xmmi HIBIT = _mm_load_si128((xmmi*)poly1305_x64_sse2_1shl128); | |
1021 + const xmmi FIVE = _mm_load_si128((xmmi*)poly1305_x64_sse2_5); | |
1022 + | |
1023 + poly1305_power *p; | |
1024 + xmmi H0,H1,H2,H3,H4; | |
1025 + xmmi M0,M1,M2,M3,M4; | |
1026 + xmmi T0,T1,T2,T3,T4,T5,T6; | |
1027 + xmmi C1,C2; | |
1028 + | |
1029 + uint64_t r0,r1,r2; | |
1030 + uint64_t t0,t1,t2,t3,t4; | |
1031 + uint64_t c; | |
1032 + size_t consumed = 0; | |
1033 + | |
1034 + H0 = st->H[0]; | |
1035 + H1 = st->H[1]; | |
1036 + H2 = st->H[2]; | |
1037 + H3 = st->H[3]; | |
1038 + H4 = st->H[4]; | |
1039 + | |
1040 + /* p = [r^2,r^2] */ | |
1041 + p = &st->P[1]; | |
1042 + | |
1043 + if (bytes >= 32) { | |
1044 + /* H *= [r^2,r^2] */ | |
1045 + T0 = _mm_mul_epu32(H0, p->R20.v); | |
1046 + T1 = _mm_mul_epu32(H0, p->R21.v); | |
1047 + T2 = _mm_mul_epu32(H0, p->R22.v); | |
1048 + T3 = _mm_mul_epu32(H0, p->R23.v); | |
1049 + T4 = _mm_mul_epu32(H0, p->R24.v); | |
1050 + 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); | |
1051 + 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); | |
1052 + 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); | |
1053 + 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); | |
1054 + 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); | |
1055 + 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); | |
1056 + 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); | |
1057 + 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); | |
1058 + T5 = _mm_mul_epu32(H1, p->R23.v); T4 = _mm_add_epi64(T4, T5); | |
1059 + T5 = _mm_mul_epu32(H2, p->R22.v); T4 = _mm_add_epi64(T4, T5); | |
1060 + T5 = _mm_mul_epu32(H3, p->R21.v); T4 = _mm_add_epi64(T4, T5); | |
1061 + T5 = _mm_mul_epu32(H4, p->R20.v); T4 = _mm_add_epi64(T4, T5); | |
1062 + | |
1063 + /* H += [Mx,My] */ | |
1064 + T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 0)), _mm_loadl_epi64((xmmi *)(m + 16))); | |
1065 + T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 8)), _mm_loadl_epi64((xmmi *)(m + 24))); | |
1066 + M0 = _mm_and_si128(MMASK, T5); | |
1067 + M1 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); | |
1068 + T5 = _mm_or_si128(_mm_srli_epi64(T5, 52), _mm_slli_epi64(T6, 12)); | |
1069 + M2 = _mm_and_si128(MMASK, T5); | |
1070 + M3 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); | |
1071 + M4 = _mm_or_si128(_mm_srli_epi64(T6, 40), HIBIT); | |
1072 + | |
1073 + T0 = _mm_add_epi64(T0, M0); | |
1074 + T1 = _mm_add_epi64(T1, M1); | |
1075 + T2 = _mm_add_epi64(T2, M2); | |
1076 + T3 = _mm_add_epi64(T3, M3); | |
1077 + T4 = _mm_add_epi64(T4, M4); | |
1078 + | |
1079 + /* reduce */ | |
1080 + 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); | |
1081 + 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)); | |
1082 + 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); | |
1083 + C1 = _mm_srli_epi64(T3, 26); T3 = _mm_and_si128(T3, MMASK); T4 = _mm_add_epi64(T4, C1); | |
1084 + | |
1085 + /* H = (H*[r^2,r^2] + [Mx,My]) */ | |
1086 + H0 = T0; | |
1087 + H1 = T1; | |
1088 + H2 = T2; | |
1089 + H3 = T3; | |
1090 + H4 = T4; | |
1091 + | |
1092 + consumed = 32; | |
1093 + } | |
1094 + | |
1095 + /* finalize, H *= [r^2,r] */ | |
1096 + r0 = ((uint64_t)p->R20.d[3] << 32) | (uint64_t)p->R20.d[1]; | |
1097 + r1 = ((uint64_t)p->R21.d[3] << 32) | (uint64_t)p->R21.d[1]; | |
1098 + r2 = ((uint64_t)p->R22.d[3] << 32) | (uint64_t)p->R22.d[1]; | |
1099 + | |
1100 + p->R20.d[2] = (uint32_t)( r0 ) & 0x3ffffff; | |
1101 + p->R21.d[2] = (uint32_t)((r0 >> 26) | (r1 << 18)) & 0x3ffffff; | |
1102 + p->R22.d[2] = (uint32_t)((r1 >> 8) ) & 0x3ffffff; | |
1103 + p->R23.d[2] = (uint32_t)((r1 >> 34) | (r2 << 10)) & 0x3ffffff; | |
1104 + p->R24.d[2] = (uint32_t)((r2 >> 16) ) ; | |
1105 + p->S21.d[2] = p->R21.d[2] * 5; | |
1106 + p->S22.d[2] = p->R22.d[2] * 5; | |
1107 + p->S23.d[2] = p->R23.d[2] * 5; | |
1108 + p->S24.d[2] = p->R24.d[2] * 5; | |
1109 + | |
1110 + /* H *= [r^2,r] */ | |
1111 + T0 = _mm_mul_epu32(H0, p->R20.v); | |
1112 + T1 = _mm_mul_epu32(H0, p->R21.v); | |
1113 + T2 = _mm_mul_epu32(H0, p->R22.v); | |
1114 + T3 = _mm_mul_epu32(H0, p->R23.v); | |
1115 + T4 = _mm_mul_epu32(H0, p->R24.v); | |
1116 + 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); | |
1117 + 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); | |
1118 + 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); | |
1119 + 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); | |
1120 + 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); | |
1121 + 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); | |
1122 + 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); | |
1123 + 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); | |
1124 + T5 = _mm_mul_epu32(H1, p->R23.v); T4 = _mm_add_epi64(T4, T5); | |
1125 + T5 = _mm_mul_epu32(H2, p->R22.v); T4 = _mm_add_epi64(T4, T5); | |
1126 + T5 = _mm_mul_epu32(H3, p->R21.v); T4 = _mm_add_epi64(T4, T5); | |
1127 + T5 = _mm_mul_epu32(H4, p->R20.v); T4 = _mm_add_epi64(T4, T5); | |
1128 + | |
1129 + 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); | |
1130 + 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)); | |
1131 + 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); | |
1132 + C1 = _mm_srli_epi64(T3, 26); T3 = _mm_and_si128(T3, MMASK); T4 = _mm_add_epi64(T4, C1); | |
1133 + | |
1134 + /* H = H[0]+H[1] */ | |
1135 + H0 = _mm_add_epi64(T0, _mm_srli_si128(T0, 8)); | |
1136 + H1 = _mm_add_epi64(T1, _mm_srli_si128(T1, 8)); | |
1137 + H2 = _mm_add_epi64(T2, _mm_srli_si128(T2, 8)); | |
1138 + H3 = _mm_add_epi64(T3, _mm_srli_si128(T3, 8)); | |
1139 + H4 = _mm_add_epi64(T4, _mm_srli_si128(T4, 8)); | |
1140 + | |
1141 + t0 = _mm_cvtsi128_si32(H0) ; c = (t0 >> 26); t0 &= 0x3ffffff; | |
1142 + t1 = _mm_cvtsi128_si32(H1) + c; c = (t1 >> 26); t1 &= 0x3ffffff; | |
1143 + t2 = _mm_cvtsi128_si32(H2) + c; c = (t2 >> 26); t2 &= 0x3ffffff; | |
1144 + t3 = _mm_cvtsi128_si32(H3) + c; c = (t3 >> 26); t3 &= 0x3ffffff; | |
1145 + t4 = _mm_cvtsi128_si32(H4) + c; c = (t4 >> 26); t4 &= 0x3ffffff; | |
1146 + t0 = t0 + (c * 5); c = (t0 >> 26); t0 &= 0x3ffffff; | |
1147 + t1 = t1 + c; | |
1148 + | |
1149 + st->HH[0] = ((t0 ) | (t1 << 26) ) & 0xfffffffffffull; | |
1150 + st->HH[1] = ((t1 >> 18) | (t2 << 8) | (t3 << 34)) & 0xfffffffffffull; | |
1151 + st->HH[2] = ((t3 >> 10) | (t4 << 16) ) & 0x3ffffffffffull; | |
1152 + | |
1153 + return consumed; | |
1154 +} | |
1155 + | |
1156 +void | |
1157 +Poly1305Update(poly1305_state *state, const unsigned char *m, size_t bytes) { | |
1158 + poly1305_state_internal *st = poly1305_aligned_state(state); | |
1159 + size_t want; | |
1160 + | |
1161 + /* need at least 32 initial bytes to start the accelerated branch */ | |
1162 + if (!st->started) { | |
1163 + if ((st->leftover == 0) && (bytes > 32)) { | |
1164 + poly1305_first_block(st, m); | |
1165 + m += 32; | |
1166 + bytes -= 32; | |
1167 + } else { | |
1168 + want = poly1305_min(32 - st->leftover, bytes); | |
1169 + poly1305_block_copy(st->buffer + st->leftover, m, want); | |
1170 + bytes -= want; | |
1171 + m += want; | |
1172 + st->leftover += want; | |
1173 + if ((st->leftover < 32) || (bytes == 0)) | |
1174 + return; | |
1175 + poly1305_first_block(st, st->buffer); | |
1176 + st->leftover = 0; | |
1177 + } | |
1178 + st->started = 1; | |
1179 + } | |
1180 + | |
1181 + /* handle leftover */ | |
1182 + if (st->leftover) { | |
1183 + want = poly1305_min(64 - st->leftover, bytes); | |
1184 + poly1305_block_copy(st->buffer + st->leftover, m, want); | |
1185 + bytes -= want; | |
1186 + m += want; | |
1187 + st->leftover += want; | |
1188 + if (st->leftover < 64) | |
1189 + return; | |
1190 + poly1305_blocks(st, st->buffer, 64); | |
1191 + st->leftover = 0; | |
1192 + } | |
1193 + | |
1194 + /* process 64 byte blocks */ | |
1195 + if (bytes >= 64) { | |
1196 + want = (bytes & ~63); | |
1197 + poly1305_blocks(st, m, want); | |
1198 + m += want; | |
1199 + bytes -= want; | |
1200 + } | |
1201 + | |
1202 + if (bytes) { | |
1203 + poly1305_block_copy(st->buffer + st->leftover, m, bytes); | |
1204 + st->leftover += bytes; | |
1205 + } | |
1206 +} | |
1207 + | |
1208 +void | |
1209 +Poly1305Finish(poly1305_state *state, unsigned char mac[16]) { | |
1210 + poly1305_state_internal *st = poly1305_aligned_state(state); | |
1211 + size_t leftover = st->leftover; | |
1212 + uint8_t *m = st->buffer; | |
1213 + uint128_t d[3]; | |
1214 + uint64_t h0,h1,h2; | |
1215 + uint64_t t0,t1; | |
1216 + uint64_t g0,g1,g2,c,nc; | |
1217 + uint64_t r0,r1,r2,s1,s2; | |
1218 + poly1305_power *p; | |
1219 + | |
1220 + if (st->started) { | |
1221 + size_t consumed = poly1305_combine(st, m, leftover); | |
1222 + leftover -= consumed; | |
1223 + m += consumed; | |
1224 + } | |
1225 + | |
1226 + /* st->HH will either be 0 or have the combined result */ | |
1227 + h0 = st->HH[0]; | |
1228 + h1 = st->HH[1]; | |
1229 + h2 = st->HH[2]; | |
1230 + | |
1231 + p = &st->P[1]; | |
1232 + r0 = ((uint64_t)p->R20.d[3] << 32) | (uint64_t)p->R20.d[1]; | |
1233 + r1 = ((uint64_t)p->R21.d[3] << 32) | (uint64_t)p->R21.d[1]; | |
1234 + r2 = ((uint64_t)p->R22.d[3] << 32) | (uint64_t)p->R22.d[1]; | |
1235 + s1 = r1 * (5 << 2); | |
1236 + s2 = r2 * (5 << 2); | |
1237 + | |
1238 + if (leftover < 16) | |
1239 + goto poly1305_donna_atmost15bytes; | |
1240 + | |
1241 +poly1305_donna_atleast16bytes: | |
1242 + t0 = U8TO64_LE(m + 0); | |
1243 + t1 = U8TO64_LE(m + 8); | |
1244 + h0 += t0 & 0xfffffffffff; | |
1245 + t0 = shr128_pair(t1, t0, 44); | |
1246 + h1 += t0 & 0xfffffffffff; | |
1247 + h2 += (t1 >> 24) | ((uint64_t)1 << 40); | |
1248 + | |
1249 +poly1305_donna_mul: | |
1250 + d[0] = add128(add128(mul64x64_128(h0, r0), mul64x64_128(h1, s2)), mul64x64_128(h2, s1)); | |
1251 + d[1] = add128(add128(mul64x64_128(h0, r1), mul64x64_128(h1, r0)), mul64x64_128(h2, s2)); | |
1252 + d[2] = add128(add128(mul64x64_128(h0, r2), mul64x64_128(h1, r1)), mul64x64_128(h2, r0)); | |
1253 + h0 = lo128(d[0]) & 0xfffffffffff; c = shr128(d[0], 44); | |
1254 + d[1] = add128_64(d[1], c); h1 = lo128(d[1]) & 0xfffffffffff; c = shr128(d[1], 44); | |
1255 + d[2] = add128_64(d[2], c); h2 = lo128(d[2]) & 0x3ffffffffff; c = shr128(d[2], 42); | |
1256 + h0 += c * 5; | |
1257 + | |
1258 + m += 16; | |
1259 + leftover -= 16; | |
1260 + if (leftover >= 16) goto poly1305_donna_atleast16bytes; | |
1261 + | |
1262 + /* final bytes */ | |
1263 +poly1305_donna_atmost15bytes: | |
1264 + if (!leftover) goto poly1305_donna_finish; | |
1265 + | |
1266 + m[leftover++] = 1; | |
1267 + poly1305_block_zero(m + leftover, 16 - leftover); | |
1268 + leftover = 16; | |
1269 + | |
1270 + t0 = U8TO64_LE(m+0); | |
1271 + t1 = U8TO64_LE(m+8); | |
1272 + h0 += t0 & 0xfffffffffff; t0 = shr128_pair(t1, t0, 44); | |
1273 + h1 += t0 & 0xfffffffffff; | |
1274 + h2 += (t1 >> 24); | |
1275 + | |
1276 + goto poly1305_donna_mul; | |
1277 + | |
1278 +poly1305_donna_finish: | |
1279 + c = (h0 >> 44); h0 &= 0xfffffffffff; | |
1280 + h1 += c; c = (h1 >> 44); h1 &= 0xfffffffffff; | |
1281 + h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff; | |
1282 + h0 += c * 5; | |
1283 + | |
1284 + g0 = h0 + 5; c = (g0 >> 44); g0 &= 0xfffffffffff; | |
1285 + g1 = h1 + c; c = (g1 >> 44); g1 &= 0xfffffffffff; | |
1286 + g2 = h2 + c - ((uint64_t)1 << 42); | |
1287 + | |
1288 + c = (g2 >> 63) - 1; | |
1289 + nc = ~c; | |
1290 + h0 = (h0 & nc) | (g0 & c); | |
1291 + h1 = (h1 & nc) | (g1 & c); | |
1292 + h2 = (h2 & nc) | (g2 & c); | |
1293 + | |
1294 + /* pad */ | |
1295 + t0 = ((uint64_t)p->R23.d[3] << 32) | (uint64_t)p->R23.d[1]; | |
1296 + t1 = ((uint64_t)p->R24.d[3] << 32) | (uint64_t)p->R24.d[1]; | |
1297 + h0 += (t0 & 0xfffffffffff) ; c = (h0 >> 44); h0 &= 0xfffffffffff; t0 = shr128_pair(t1, t0, 44); | |
1298 + h1 += (t0 & 0xfffffffffff) + c; c = (h1 >> 44); h1 &= 0xfffffffffff; t1 = (t1 >> 24); | |
1299 + h2 += (t1 ) + c; | |
1300 + | |
1301 + U64TO8_LE(mac + 0, ((h0 ) | (h1 << 44))); | |
1302 + U64TO8_LE(mac + 8, ((h1 >> 20) | (h2 << 24))); | |
1303 +} | |
1304 diff -r c3565a90b8c4 lib/freebl/poly1305/poly1305.c | |
1305 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 | |
1306 +++ b/lib/freebl/poly1305/poly1305.c Tue Jan 07 12:11:36 2014 -0800 | |
1307 @@ -0,0 +1,254 @@ | |
1308 +/* This Source Code Form is subject to the terms of the Mozilla Public | |
1309 + * License, v. 2.0. If a copy of the MPL was not distributed with this | |
1310 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
1311 + | |
1312 +/* This implementation of poly1305 is by Andrew Moon | |
1313 + * (https://github.com/floodyberry/poly1305-donna) and released as public | |
1314 + * domain. */ | |
1315 + | |
1316 +#include <string.h> | |
1317 +#include <stdint.h> | |
1318 + | |
1319 +#include "poly1305.h" | |
1320 + | |
1321 +#if defined(NSS_X86) || defined(NSS_X64) | |
1322 +/* We can assume little-endian. */ | |
1323 +static uint32_t U8TO32_LE(const unsigned char *m) { | |
1324 + uint32_t r; | |
1325 + memcpy(&r, m, sizeof(r)); | |
1326 + return r; | |
1327 +} | |
1328 + | |
1329 +static void U32TO8_LE(unsigned char *m, uint32_t v) { | |
1330 + memcpy(m, &v, sizeof(v)); | |
1331 +} | |
1332 +#else | |
1333 +static uint32_t U8TO32_LE(const unsigned char *m) { | |
1334 + return (uint32_t)m[0] | | |
1335 + (uint32_t)m[1] << 8 | | |
1336 + (uint32_t)m[2] << 16 | | |
1337 + (uint32_t)m[3] << 24; | |
1338 +} | |
1339 + | |
1340 +static void U32TO8_LE(unsigned char *m, uint32_t v) { | |
1341 + m[0] = v; | |
1342 + m[1] = v >> 8; | |
1343 + m[2] = v >> 16; | |
1344 + m[3] = v >> 24; | |
1345 +} | |
1346 +#endif | |
1347 + | |
1348 +static uint64_t | |
1349 +mul32x32_64(uint32_t a, uint32_t b) { | |
1350 + return (uint64_t)a * b; | |
1351 +} | |
1352 + | |
1353 +struct poly1305_state_st { | |
1354 + uint32_t r0,r1,r2,r3,r4; | |
1355 + uint32_t s1,s2,s3,s4; | |
1356 + uint32_t h0,h1,h2,h3,h4; | |
1357 + unsigned char buf[16]; | |
1358 + unsigned int buf_used; | |
1359 + unsigned char key[16]; | |
1360 +}; | |
1361 + | |
1362 +/* update updates |state| given some amount of input data. This function may | |
1363 + * only be called with a |len| that is not a multiple of 16 at the end of the | |
1364 + * data. Otherwise the input must be buffered into 16 byte blocks. */ | |
1365 +static void update(struct poly1305_state_st *state, const unsigned char *in, | |
1366 + size_t len) { | |
1367 + uint32_t t0,t1,t2,t3; | |
1368 + uint64_t t[5]; | |
1369 + uint32_t b; | |
1370 + uint64_t c; | |
1371 + size_t j; | |
1372 + unsigned char mp[16]; | |
1373 + | |
1374 + if (len < 16) | |
1375 + goto poly1305_donna_atmost15bytes; | |
1376 + | |
1377 +poly1305_donna_16bytes: | |
1378 + t0 = U8TO32_LE(in); | |
1379 + t1 = U8TO32_LE(in+4); | |
1380 + t2 = U8TO32_LE(in+8); | |
1381 + t3 = U8TO32_LE(in+12); | |
1382 + | |
1383 + in += 16; | |
1384 + len -= 16; | |
1385 + | |
1386 + state->h0 += t0 & 0x3ffffff; | |
1387 + state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff; | |
1388 + state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff; | |
1389 + state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff; | |
1390 + state->h4 += (t3 >> 8) | (1 << 24); | |
1391 + | |
1392 +poly1305_donna_mul: | |
1393 + t[0] = mul32x32_64(state->h0,state->r0) + | |
1394 + mul32x32_64(state->h1,state->s4) + | |
1395 + mul32x32_64(state->h2,state->s3) + | |
1396 + mul32x32_64(state->h3,state->s2) + | |
1397 + mul32x32_64(state->h4,state->s1); | |
1398 + t[1] = mul32x32_64(state->h0,state->r1) + | |
1399 + mul32x32_64(state->h1,state->r0) + | |
1400 + mul32x32_64(state->h2,state->s4) + | |
1401 + mul32x32_64(state->h3,state->s3) + | |
1402 + mul32x32_64(state->h4,state->s2); | |
1403 + t[2] = mul32x32_64(state->h0,state->r2) + | |
1404 + mul32x32_64(state->h1,state->r1) + | |
1405 + mul32x32_64(state->h2,state->r0) + | |
1406 + mul32x32_64(state->h3,state->s4) + | |
1407 + mul32x32_64(state->h4,state->s3); | |
1408 + t[3] = mul32x32_64(state->h0,state->r3) + | |
1409 + mul32x32_64(state->h1,state->r2) + | |
1410 + mul32x32_64(state->h2,state->r1) + | |
1411 + mul32x32_64(state->h3,state->r0) + | |
1412 + mul32x32_64(state->h4,state->s4); | |
1413 + t[4] = mul32x32_64(state->h0,state->r4) + | |
1414 + mul32x32_64(state->h1,state->r3) + | |
1415 + mul32x32_64(state->h2,state->r2) + | |
1416 + mul32x32_64(state->h3,state->r1) + | |
1417 + mul32x32_64(state->h4,state->r0); | |
1418 + | |
1419 + state->h0 = (uint32_t)t[0] & 0x3ffffff; c = (t[0] >> 26); | |
1420 + t[1] += c; state->h1 = (uint32_t)t[1] & 0x3ffffff; b = (uint32_t)(t[1] >> 26); | |
1421 + t[2] += b; state->h2 = (uint32_t)t[2] & 0x3ffffff; b = (uint32_t)(t[2] >> 26); | |
1422 + t[3] += b; state->h3 = (uint32_t)t[3] & 0x3ffffff; b = (uint32_t)(t[3] >> 26); | |
1423 + t[4] += b; state->h4 = (uint32_t)t[4] & 0x3ffffff; b = (uint32_t)(t[4] >> 26); | |
1424 + state->h0 += b * 5; | |
1425 + | |
1426 + if (len >= 16) | |
1427 + goto poly1305_donna_16bytes; | |
1428 + | |
1429 + /* final bytes */ | |
1430 +poly1305_donna_atmost15bytes: | |
1431 + if (!len) | |
1432 + return; | |
1433 + | |
1434 + for (j = 0; j < len; j++) | |
1435 + mp[j] = in[j]; | |
1436 + mp[j++] = 1; | |
1437 + for (; j < 16; j++) | |
1438 + mp[j] = 0; | |
1439 + len = 0; | |
1440 + | |
1441 + t0 = U8TO32_LE(mp+0); | |
1442 + t1 = U8TO32_LE(mp+4); | |
1443 + t2 = U8TO32_LE(mp+8); | |
1444 + t3 = U8TO32_LE(mp+12); | |
1445 + | |
1446 + state->h0 += t0 & 0x3ffffff; | |
1447 + state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff; | |
1448 + state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff; | |
1449 + state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff; | |
1450 + state->h4 += (t3 >> 8); | |
1451 + | |
1452 + goto poly1305_donna_mul; | |
1453 +} | |
1454 + | |
1455 +void Poly1305Init(poly1305_state *statep, const unsigned char key[32]) { | |
1456 + struct poly1305_state_st *state = (struct poly1305_state_st*) statep; | |
1457 + uint32_t t0,t1,t2,t3; | |
1458 + | |
1459 + t0 = U8TO32_LE(key+0); | |
1460 + t1 = U8TO32_LE(key+4); | |
1461 + t2 = U8TO32_LE(key+8); | |
1462 + t3 = U8TO32_LE(key+12); | |
1463 + | |
1464 + /* precompute multipliers */ | |
1465 + state->r0 = t0 & 0x3ffffff; t0 >>= 26; t0 |= t1 << 6; | |
1466 + state->r1 = t0 & 0x3ffff03; t1 >>= 20; t1 |= t2 << 12; | |
1467 + state->r2 = t1 & 0x3ffc0ff; t2 >>= 14; t2 |= t3 << 18; | |
1468 + state->r3 = t2 & 0x3f03fff; t3 >>= 8; | |
1469 + state->r4 = t3 & 0x00fffff; | |
1470 + | |
1471 + state->s1 = state->r1 * 5; | |
1472 + state->s2 = state->r2 * 5; | |
1473 + state->s3 = state->r3 * 5; | |
1474 + state->s4 = state->r4 * 5; | |
1475 + | |
1476 + /* init state */ | |
1477 + state->h0 = 0; | |
1478 + state->h1 = 0; | |
1479 + state->h2 = 0; | |
1480 + state->h3 = 0; | |
1481 + state->h4 = 0; | |
1482 + | |
1483 + state->buf_used = 0; | |
1484 + memcpy(state->key, key + 16, sizeof(state->key)); | |
1485 +} | |
1486 + | |
1487 +void Poly1305Update(poly1305_state *statep, const unsigned char *in, | |
1488 + size_t in_len) { | |
1489 + unsigned int i; | |
1490 + struct poly1305_state_st *state = (struct poly1305_state_st*) statep; | |
1491 + | |
1492 + if (state->buf_used) { | |
1493 + unsigned int todo = 16 - state->buf_used; | |
1494 + if (todo > in_len) | |
1495 + todo = in_len; | |
1496 + for (i = 0; i < todo; i++) | |
1497 + state->buf[state->buf_used + i] = in[i]; | |
1498 + state->buf_used += todo; | |
1499 + in_len -= todo; | |
1500 + in += todo; | |
1501 + | |
1502 + if (state->buf_used == 16) { | |
1503 + update(state, state->buf, 16); | |
1504 + state->buf_used = 0; | |
1505 + } | |
1506 + } | |
1507 + | |
1508 + if (in_len >= 16) { | |
1509 + size_t todo = in_len & ~0xf; | |
1510 + update(state, in, todo); | |
1511 + in += todo; | |
1512 + in_len &= 0xf; | |
1513 + } | |
1514 + | |
1515 + if (in_len) { | |
1516 + for (i = 0; i < in_len; i++) | |
1517 + state->buf[i] = in[i]; | |
1518 + state->buf_used = in_len; | |
1519 + } | |
1520 +} | |
1521 + | |
1522 +void Poly1305Finish(poly1305_state *statep, unsigned char mac[16]) { | |
1523 + struct poly1305_state_st *state = (struct poly1305_state_st*) statep; | |
1524 + uint64_t f0,f1,f2,f3; | |
1525 + uint32_t g0,g1,g2,g3,g4; | |
1526 + uint32_t b, nb; | |
1527 + | |
1528 + if (state->buf_used) | |
1529 + update(state, state->buf, state->buf_used); | |
1530 + | |
1531 + b = state->h0 >> 26; state->h0 = state->h0 & 0x3ffffff; | |
1532 + state->h1 += b; b = state->h1 >> 26; state->h1 = state->h1 & 0x3ffffff; | |
1533 + state->h2 += b; b = state->h2 >> 26; state->h2 = state->h2 & 0x3ffffff; | |
1534 + state->h3 += b; b = state->h3 >> 26; state->h3 = state->h3 & 0x3ffffff; | |
1535 + state->h4 += b; b = state->h4 >> 26; state->h4 = state->h4 & 0x3ffffff; | |
1536 + state->h0 += b * 5; | |
1537 + | |
1538 + g0 = state->h0 + 5; b = g0 >> 26; g0 &= 0x3ffffff; | |
1539 + g1 = state->h1 + b; b = g1 >> 26; g1 &= 0x3ffffff; | |
1540 + g2 = state->h2 + b; b = g2 >> 26; g2 &= 0x3ffffff; | |
1541 + g3 = state->h3 + b; b = g3 >> 26; g3 &= 0x3ffffff; | |
1542 + g4 = state->h4 + b - (1 << 26); | |
1543 + | |
1544 + b = (g4 >> 31) - 1; | |
1545 + nb = ~b; | |
1546 + state->h0 = (state->h0 & nb) | (g0 & b); | |
1547 + state->h1 = (state->h1 & nb) | (g1 & b); | |
1548 + state->h2 = (state->h2 & nb) | (g2 & b); | |
1549 + state->h3 = (state->h3 & nb) | (g3 & b); | |
1550 + state->h4 = (state->h4 & nb) | (g4 & b); | |
1551 + | |
1552 + f0 = ((state->h0 ) | (state->h1 << 26)) + (uint64_t)U8TO32_LE(&state->key[0]); | |
1553 + f1 = ((state->h1 >> 6) | (state->h2 << 20)) + (uint64_t)U8TO32_LE(&state->key[4]); | |
1554 + f2 = ((state->h2 >> 12) | (state->h3 << 14)) + (uint64_t)U8TO32_LE(&state->key[8]); | |
1555 + f3 = ((state->h3 >> 18) | (state->h4 << 8)) + (uint64_t)U8TO32_LE(&state->key[12]); | |
1556 + | |
1557 + U32TO8_LE(&mac[ 0], f0); f1 += (f0 >> 32); | |
1558 + U32TO8_LE(&mac[ 4], f1); f2 += (f1 >> 32); | |
1559 + U32TO8_LE(&mac[ 8], f2); f3 += (f2 >> 32); | |
1560 + U32TO8_LE(&mac[12], f3); | |
1561 +} | |
1562 diff -r c3565a90b8c4 lib/freebl/poly1305/poly1305.h | |
1563 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 | |
1564 +++ b/lib/freebl/poly1305/poly1305.h Tue Jan 07 12:11:36 2014 -0800 | |
1565 @@ -0,0 +1,31 @@ | |
1566 +/* | |
1567 + * poly1305.h - header file for Poly1305 implementation. | |
1568 + * | |
1569 + * This Source Code Form is subject to the terms of the Mozilla Public | |
1570 + * License, v. 2.0. If a copy of the MPL was not distributed with this | |
1571 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
1572 + | |
1573 +#ifndef FREEBL_POLY1305_H_ | |
1574 +#define FREEBL_POLY1305_H_ | |
1575 + | |
1576 +typedef unsigned char poly1305_state[512]; | |
1577 + | |
1578 +/* Poly1305Init sets up |state| so that it can be used to calculate an | |
1579 + * authentication tag with the one-time key |key|. Note that |key| is a | |
1580 + * one-time key and therefore there is no `reset' method because that would | |
1581 + * enable several messages to be authenticated with the same key. */ | |
1582 +extern void Poly1305Init(poly1305_state* state, | |
1583 + const unsigned char key[32]); | |
1584 + | |
1585 +/* Poly1305Update processes |in_len| bytes from |in|. It can be called zero or | |
1586 + * more times after poly1305_init. */ | |
1587 +extern void Poly1305Update(poly1305_state* state, | |
1588 + const unsigned char *in, | |
1589 + size_t inLen); | |
1590 + | |
1591 +/* Poly1305Finish completes the poly1305 calculation and writes a 16 byte | |
1592 + * authentication tag to |mac|. */ | |
1593 +extern void Poly1305Finish(poly1305_state* state, | |
1594 + unsigned char mac[16]); | |
1595 + | |
1596 +#endif /* FREEBL_POLY1305_H_ */ | |
1597 diff -r c3565a90b8c4 lib/pk11wrap/pk11mech.c | |
1598 --- a/lib/pk11wrap/pk11mech.c Fri Jan 03 20:59:10 2014 +0100 | |
1599 +++ b/lib/pk11wrap/pk11mech.c Tue Jan 07 12:11:36 2014 -0800 | |
1600 @@ -152,6 +152,8 @@ | |
1601 return CKM_SEED_CBC; | |
1602 case CKK_CAMELLIA: | |
1603 return CKM_CAMELLIA_CBC; | |
1604 + case CKK_NSS_CHACHA20: | |
1605 + return CKM_NSS_CHACHA20_POLY1305; | |
1606 case CKK_AES: | |
1607 return CKM_AES_CBC; | |
1608 case CKK_DES: | |
1609 @@ -219,6 +221,8 @@ | |
1610 case CKM_CAMELLIA_CBC_PAD: | |
1611 case CKM_CAMELLIA_KEY_GEN: | |
1612 return CKK_CAMELLIA; | |
1613 + case CKM_NSS_CHACHA20_POLY1305: | |
1614 + return CKK_NSS_CHACHA20; | |
1615 case CKM_AES_ECB: | |
1616 case CKM_AES_CBC: | |
1617 case CKM_AES_CCM: | |
1618 @@ -429,6 +433,8 @@ | |
1619 case CKM_CAMELLIA_CBC_PAD: | |
1620 case CKM_CAMELLIA_KEY_GEN: | |
1621 return CKM_CAMELLIA_KEY_GEN; | |
1622 + case CKM_NSS_CHACHA20_POLY1305: | |
1623 + return CKM_NSS_CHACHA20_KEY_GEN; | |
1624 case CKM_AES_ECB: | |
1625 case CKM_AES_CBC: | |
1626 case CKM_AES_CCM: | |
1627 diff -r c3565a90b8c4 lib/softoken/pkcs11.c | |
1628 --- a/lib/softoken/pkcs11.c Fri Jan 03 20:59:10 2014 +0100 | |
1629 +++ b/lib/softoken/pkcs11.c Tue Jan 07 12:11:36 2014 -0800 | |
1630 @@ -368,6 +368,9 @@ | |
1631 {CKM_SEED_MAC, {16, 16, CKF_SN_VR}, PR_TRUE}, | |
1632 {CKM_SEED_MAC_GENERAL, {16, 16, CKF_SN_VR}, PR_TRUE}, | |
1633 {CKM_SEED_CBC_PAD, {16, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, | |
1634 + /* ------------------------- ChaCha20 Operations ---------------------- */ | |
1635 + {CKM_NSS_CHACHA20_KEY_GEN, {32, 32, CKF_GENERATE}, PR_TRUE}, | |
1636 + {CKM_NSS_CHACHA20_POLY1305,{32, 32, CKF_EN_DE}, PR_TRUE}, | |
1637 /* ------------------------- Hashing Operations ----------------------- */ | |
1638 {CKM_MD2, {0, 0, CKF_DIGEST}, PR_FALSE}, | |
1639 {CKM_MD2_HMAC, {1, 128, CKF_SN_VR}, PR_TRUE}, | |
1640 diff -r c3565a90b8c4 lib/softoken/pkcs11c.c | |
1641 --- a/lib/softoken/pkcs11c.c Fri Jan 03 20:59:10 2014 +0100 | |
1642 +++ b/lib/softoken/pkcs11c.c Tue Jan 07 12:11:36 2014 -0800 | |
1643 @@ -632,6 +632,97 @@ | |
1644 return rv; | |
1645 } | |
1646 | |
1647 +static SFTKChaCha20Poly1305Info * | |
1648 +sftk_ChaCha20Poly1305_CreateContext(const unsigned char *key, | |
1649 + unsigned int keyLen, | |
1650 + const CK_NSS_AEAD_PARAMS* params) | |
1651 +{ | |
1652 + SFTKChaCha20Poly1305Info *ctx; | |
1653 + | |
1654 + if (params->ulIvLen != sizeof(ctx->nonce)) { | |
1655 + PORT_SetError(SEC_ERROR_INPUT_LEN); | |
1656 + return NULL; | |
1657 + } | |
1658 + | |
1659 + ctx = PORT_New(SFTKChaCha20Poly1305Info); | |
1660 + if (ctx == NULL) { | |
1661 + return NULL; | |
1662 + } | |
1663 + | |
1664 + if (ChaCha20Poly1305_InitContext(&ctx->freeblCtx, key, keyLen, | |
1665 + params->ulTagLen) != SECSuccess) { | |
1666 + PORT_Free(ctx); | |
1667 + return NULL; | |
1668 + } | |
1669 + | |
1670 + memcpy(ctx->nonce, params->pIv, sizeof(ctx->nonce)); | |
1671 + | |
1672 + if (params->ulAADLen > sizeof(ctx->ad)) { | |
1673 + /* Need to allocate an overflow buffer for the additional data. */ | |
1674 + ctx->adOverflow = (unsigned char *)PORT_Alloc(params->ulAADLen); | |
1675 + if (!ctx->adOverflow) { | |
1676 + PORT_Free(ctx); | |
1677 + return NULL; | |
1678 + } | |
1679 + memcpy(ctx->adOverflow, params->pAAD, params->ulAADLen); | |
1680 + } else { | |
1681 + ctx->adOverflow = NULL; | |
1682 + memcpy(ctx->ad, params->pAAD, params->ulAADLen); | |
1683 + } | |
1684 + ctx->adLen = params->ulAADLen; | |
1685 + | |
1686 + return ctx; | |
1687 +} | |
1688 + | |
1689 +static void | |
1690 +sftk_ChaCha20Poly1305_DestroyContext(SFTKChaCha20Poly1305Info *ctx, | |
1691 + PRBool freeit) | |
1692 +{ | |
1693 + ChaCha20Poly1305_DestroyContext(&ctx->freeblCtx, PR_FALSE); | |
1694 + if (ctx->adOverflow != NULL) { | |
1695 + PORT_Free(ctx->adOverflow); | |
1696 + ctx->adOverflow = NULL; | |
1697 + } | |
1698 + ctx->adLen = 0; | |
1699 + if (freeit) { | |
1700 + PORT_Free(ctx); | |
1701 + } | |
1702 +} | |
1703 + | |
1704 +static SECStatus | |
1705 +sftk_ChaCha20Poly1305_Encrypt(const SFTKChaCha20Poly1305Info *ctx, | |
1706 + unsigned char *output, unsigned int *outputLen, | |
1707 + unsigned int maxOutputLen, | |
1708 + const unsigned char *input, unsigned int inputLen) | |
1709 +{ | |
1710 + const unsigned char *ad = ctx->adOverflow; | |
1711 + | |
1712 + if (ad == NULL) { | |
1713 + ad = ctx->ad; | |
1714 + } | |
1715 + | |
1716 + return ChaCha20Poly1305_Seal(&ctx->freeblCtx, output, outputLen, | |
1717 + maxOutputLen, input, inputLen, ctx->nonce, | |
1718 + sizeof(ctx->nonce), ad, ctx->adLen); | |
1719 +} | |
1720 + | |
1721 +static SECStatus | |
1722 +sftk_ChaCha20Poly1305_Decrypt(const SFTKChaCha20Poly1305Info *ctx, | |
1723 + unsigned char *output, unsigned int *outputLen, | |
1724 + unsigned int maxOutputLen, | |
1725 + const unsigned char *input, unsigned int inputLen) | |
1726 +{ | |
1727 + const unsigned char *ad = ctx->adOverflow; | |
1728 + | |
1729 + if (ad == NULL) { | |
1730 + ad = ctx->ad; | |
1731 + } | |
1732 + | |
1733 + return ChaCha20Poly1305_Open(&ctx->freeblCtx, output, outputLen, | |
1734 + maxOutputLen, input, inputLen, ctx->nonce, | |
1735 + sizeof(ctx->nonce), ad, ctx->adLen); | |
1736 +} | |
1737 + | |
1738 /** NSC_CryptInit initializes an encryption/Decryption operation. | |
1739 * | |
1740 * Always called by NSC_EncryptInit, NSC_DecryptInit, NSC_WrapKey,NSC_UnwrapKey. | |
1741 @@ -1027,6 +1118,35 @@ | |
1742 context->destroy = (SFTKDestroy) AES_DestroyContext; | |
1743 break; | |
1744 | |
1745 + case CKM_NSS_CHACHA20_POLY1305: | |
1746 + if (pMechanism->ulParameterLen != sizeof(CK_NSS_AEAD_PARAMS)) { | |
1747 + crv = CKR_MECHANISM_PARAM_INVALID; | |
1748 + break; | |
1749 + } | |
1750 + context->multi = PR_FALSE; | |
1751 + if (key_type != CKK_NSS_CHACHA20) { | |
1752 + crv = CKR_KEY_TYPE_INCONSISTENT; | |
1753 + break; | |
1754 + } | |
1755 + att = sftk_FindAttribute(key,CKA_VALUE); | |
1756 + if (att == NULL) { | |
1757 + crv = CKR_KEY_HANDLE_INVALID; | |
1758 + break; | |
1759 + } | |
1760 + context->cipherInfo = sftk_ChaCha20Poly1305_CreateContext( | |
1761 + (unsigned char*) att->attrib.pValue, att->attrib.ulValueLen, | |
1762 + (CK_NSS_AEAD_PARAMS*) pMechanism->pParameter); | |
1763 + sftk_FreeAttribute(att); | |
1764 + if (context->cipherInfo == NULL) { | |
1765 + crv = sftk_MapCryptError(PORT_GetError()); | |
1766 + break; | |
1767 + } | |
1768 + context->update = (SFTKCipher) (isEncrypt ? | |
1769 + sftk_ChaCha20Poly1305_Encrypt : | |
1770 + sftk_ChaCha20Poly1305_Decrypt); | |
1771 + context->destroy = (SFTKDestroy) sftk_ChaCha20Poly1305_DestroyContext; | |
1772 + break; | |
1773 + | |
1774 case CKM_NETSCAPE_AES_KEY_WRAP_PAD: | |
1775 context->doPad = PR_TRUE; | |
1776 /* fall thru */ | |
1777 @@ -3601,6 +3721,10 @@ | |
1778 *key_type = CKK_AES; | |
1779 if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; | |
1780 break; | |
1781 + case CKM_NSS_CHACHA20_KEY_GEN: | |
1782 + *key_type = CKK_NSS_CHACHA20; | |
1783 + if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; | |
1784 + break; | |
1785 default: | |
1786 PORT_Assert(0); | |
1787 crv = CKR_MECHANISM_INVALID; | |
1788 @@ -3846,6 +3970,7 @@ | |
1789 case CKM_SEED_KEY_GEN: | |
1790 case CKM_CAMELLIA_KEY_GEN: | |
1791 case CKM_AES_KEY_GEN: | |
1792 + case CKM_NSS_CHACHA20_KEY_GEN: | |
1793 #if NSS_SOFTOKEN_DOES_RC5 | |
1794 case CKM_RC5_KEY_GEN: | |
1795 #endif | |
1796 diff -r c3565a90b8c4 lib/softoken/pkcs11i.h | |
1797 --- a/lib/softoken/pkcs11i.h Fri Jan 03 20:59:10 2014 +0100 | |
1798 +++ b/lib/softoken/pkcs11i.h Tue Jan 07 12:11:36 2014 -0800 | |
1799 @@ -14,6 +14,7 @@ | |
1800 #include "pkcs11t.h" | |
1801 | |
1802 #include "sftkdbt.h" | |
1803 +#include "chacha20poly1305.h" | |
1804 #include "hasht.h" | |
1805 | |
1806 /* | |
1807 @@ -104,6 +105,7 @@ | |
1808 typedef struct SFTKOAEPEncryptInfoStr SFTKOAEPEncryptInfo; | |
1809 typedef struct SFTKOAEPDecryptInfoStr SFTKOAEPDecryptInfo; | |
1810 typedef struct SFTKSSLMACInfoStr SFTKSSLMACInfo; | |
1811 +typedef struct SFTKChaCha20Poly1305InfoStr SFTKChaCha20Poly1305Info; | |
1812 typedef struct SFTKItemTemplateStr SFTKItemTemplate; | |
1813 | |
1814 /* define function pointer typdefs for pointer tables */ | |
1815 @@ -399,6 +401,16 @@ | |
1816 unsigned int keySize; | |
1817 }; | |
1818 | |
1819 +/* SFTKChaCha20Poly1305Info saves the key, tag length, nonce, and additional | |
1820 + * data for a ChaCha20+Poly1305 AEAD operation. */ | |
1821 +struct SFTKChaCha20Poly1305InfoStr { | |
1822 + ChaCha20Poly1305Context freeblCtx; | |
1823 + unsigned char nonce[8]; | |
1824 + unsigned char ad[16]; | |
1825 + unsigned char *adOverflow; | |
1826 + unsigned int adLen; | |
1827 +}; | |
1828 + | |
1829 /* | |
1830 * Template based on SECItems, suitable for passing as arrays | |
1831 */ | |
1832 diff -r c3565a90b8c4 lib/util/pkcs11n.h | |
1833 --- a/lib/util/pkcs11n.h Fri Jan 03 20:59:10 2014 +0100 | |
1834 +++ b/lib/util/pkcs11n.h Tue Jan 07 12:11:36 2014 -0800 | |
1835 @@ -51,6 +51,8 @@ | |
1836 #define CKK_NSS_JPAKE_ROUND1 (CKK_NSS + 2) | |
1837 #define CKK_NSS_JPAKE_ROUND2 (CKK_NSS + 3) | |
1838 | |
1839 +#define CKK_NSS_CHACHA20 (CKK_NSS + 4) | |
1840 + | |
1841 /* | |
1842 * NSS-defined certificate types | |
1843 * | |
1844 @@ -214,6 +216,9 @@ | |
1845 #define CKM_NSS_TLS_KEY_AND_MAC_DERIVE_SHA256 (CKM_NSS + 23) | |
1846 #define CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256 (CKM_NSS + 24) | |
1847 | |
1848 +#define CKM_NSS_CHACHA20_KEY_GEN (CKM_NSS + 25) | |
1849 +#define CKM_NSS_CHACHA20_POLY1305 (CKM_NSS + 26) | |
1850 + | |
1851 /* | |
1852 * HISTORICAL: | |
1853 * Do not attempt to use these. They are only used by NETSCAPE's internal | |
1854 @@ -281,6 +286,14 @@ | |
1855 CK_ULONG ulHeaderLen; /* in */ | |
1856 } CK_NSS_MAC_CONSTANT_TIME_PARAMS; | |
1857 | |
1858 +typedef struct CK_NSS_AEAD_PARAMS { | |
1859 + CK_BYTE_PTR pIv; /* This is the nonce. */ | |
1860 + CK_ULONG ulIvLen; | |
1861 + CK_BYTE_PTR pAAD; | |
1862 + CK_ULONG ulAADLen; | |
1863 + CK_ULONG ulTagLen; | |
1864 +} CK_NSS_AEAD_PARAMS; | |
1865 + | |
1866 /* | |
1867 * NSS-defined return values | |
1868 * |