Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(403)

Side by Side Diff: patches/nss-chacha20-poly1305.patch

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

Powered by Google App Engine
This is Rietveld 408576698