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