OLD | NEW |
1 Index: nss/lib/softoken/pkcs11.c | 1 diff -r c3565a90b8c4 lib/freebl/blapi.h |
2 =================================================================== | 2 --- a/lib/freebl/blapi.h» Fri Jan 03 20:59:10 2014 +0100 |
3 --- nss/lib/softoken/pkcs11.c» (revision 228205) | 3 +++ b/lib/freebl/blapi.h» Tue Jan 07 12:11:36 2014 -0800 |
4 +++ nss/lib/softoken/pkcs11.c» (working copy) | 4 @@ -986,6 +986,38 @@ |
5 @@ -368,6 +368,9 @@ | 5 » » unsigned int *outputLen, unsigned int maxOutputLen, |
6 {CKM_SEED_MAC,» » {16, 16, CKF_SN_VR},» » PR_TRUE}, | 6 » » const unsigned char *input, unsigned int inputLen); |
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 | 7 |
23 +static SFTKChaCha20Poly1305Info * | 8 +/******************************************/ |
24 +sftk_ChaCha20Poly1305_CreateContext(const unsigned char *key, | 9 +/* |
25 +» » » » unsigned int keyLen, | 10 +** ChaCha20+Poly1305 AEAD |
26 +» » » » const CK_NSS_AEAD_PARAMS* params) | 11 +*/ |
27 +{ | 12 + |
28 + SFTKChaCha20Poly1305Info *ctx; | 13 +extern SECStatus |
29 + | 14 +ChaCha20Poly1305_InitContext(ChaCha20Poly1305Context *ctx, |
30 + if (params->ulIvLen != sizeof(ctx->nonce)) { | 15 +» » » const unsigned char *key, unsigned int keyLen, |
31 +» PORT_SetError(SEC_ERROR_INPUT_LEN); | 16 +» » » unsigned int tagLen); |
32 +» return NULL; | 17 + |
33 + } | 18 +extern ChaCha20Poly1305Context * |
34 + | 19 +ChaCha20Poly1305_CreateContext(const unsigned char *key, unsigned int keyLen, |
35 + ctx = PORT_New(SFTKChaCha20Poly1305Info); | 20 +» » » unsigned int tagLen); |
36 + if (ctx == NULL) { | 21 + |
37 +» return NULL; | 22 +extern void |
38 + } | 23 +ChaCha20Poly1305_DestroyContext(ChaCha20Poly1305Context *ctx, PRBool freeit); |
39 + | 24 + |
40 + if (ChaCha20Poly1305_InitContext(&ctx->freeblCtx, key, keyLen, | 25 +extern SECStatus |
41 +» » » » params->ulTagLen) != SECSuccess) { | 26 +ChaCha20Poly1305_Seal(const ChaCha20Poly1305Context *ctx, |
42 +» PORT_Free(ctx); | 27 +» » unsigned char *output, unsigned int *outputLen, |
43 +» return NULL; | 28 +» » unsigned int maxOutputLen, |
44 + } | 29 +» » const unsigned char *input, unsigned int inputLen, |
45 + | 30 +» » const unsigned char *nonce, unsigned int nonceLen, |
46 + memcpy(ctx->nonce, params->pIv, sizeof(ctx->nonce)); | 31 +» » const unsigned char *ad, unsigned int adLen); |
47 + | 32 + |
48 + if (params->ulAADLen > sizeof(ctx->ad)) { | 33 +extern SECStatus |
49 +» /* Need to allocate an overflow buffer for the additional data. */ | 34 +ChaCha20Poly1305_Open(const ChaCha20Poly1305Context *ctx, |
50 +» ctx->adOverflow = (unsigned char *)PORT_Alloc(params->ulAADLen); | 35 +» » unsigned char *output, unsigned int *outputLen, |
51 +» if (!ctx->adOverflow) { | 36 +» » unsigned int maxOutputLen, |
52 +» PORT_Free(ctx); | 37 +» » const unsigned char *input, unsigned int inputLen, |
53 +» return NULL; | 38 +» » const unsigned char *nonce, unsigned int nonceLen, |
54 +» } | 39 +» » const unsigned char *ad, unsigned int adLen); |
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 | 40 |
121 + case CKM_NSS_CHACHA20_POLY1305: | 41 /******************************************/ |
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 /* | 42 /* |
207 * Template based on SECItems, suitable for passing as arrays | 43 diff -r c3565a90b8c4 lib/freebl/blapit.h |
208 */ | 44 --- a/lib/freebl/blapit.h» Fri Jan 03 20:59:10 2014 +0100 |
209 Index: nss/lib/freebl/blapit.h | 45 +++ b/lib/freebl/blapit.h» Tue Jan 07 12:11:36 2014 -0800 |
210 =================================================================== | |
211 --- nss/lib/freebl/blapit.h» (revision 228205) | |
212 +++ nss/lib/freebl/blapit.h» (working copy) | |
213 @@ -222,6 +222,7 @@ | 46 @@ -222,6 +222,7 @@ |
214 struct SHA512ContextStr ; | 47 struct SHA512ContextStr ; |
215 struct AESKeyWrapContextStr ; | 48 struct AESKeyWrapContextStr ; |
216 struct SEEDContextStr ; | 49 struct SEEDContextStr ; |
217 +struct ChaCha20Poly1305ContextStr; | 50 +struct ChaCha20Poly1305ContextStr; |
218 | 51 |
219 typedef struct DESContextStr DESContext; | 52 typedef struct DESContextStr DESContext; |
220 typedef struct RC2ContextStr RC2Context; | 53 typedef struct RC2ContextStr RC2Context; |
221 @@ -240,6 +241,7 @@ | 54 @@ -240,6 +241,7 @@ |
222 typedef struct SHA512ContextStr SHA384Context; | 55 typedef struct SHA512ContextStr SHA384Context; |
223 typedef struct AESKeyWrapContextStr AESKeyWrapContext; | 56 typedef struct AESKeyWrapContextStr AESKeyWrapContext; |
224 typedef struct SEEDContextStr SEEDContext; | 57 typedef struct SEEDContextStr SEEDContext; |
225 +typedef struct ChaCha20Poly1305ContextStr ChaCha20Poly1305Context; | 58 +typedef struct ChaCha20Poly1305ContextStr ChaCha20Poly1305Context; |
226 | 59 |
227 /*************************************************************************** | 60 /*************************************************************************** |
228 ** RSA Public and Private Key structures | 61 ** RSA Public and Private Key structures |
229 Index: nss/lib/freebl/blapi.h | 62 diff -r c3565a90b8c4 lib/freebl/chacha20/chacha20.c |
230 =================================================================== | 63 --- /dev/null» Thu Jan 01 00:00:00 1970 +0000 |
231 --- nss/lib/freebl/blapi.h» (revision 228205) | 64 +++ b/lib/freebl/chacha20/chacha20.c» Tue Jan 07 12:11:36 2014 -0800 |
232 +++ nss/lib/freebl/blapi.h» (working copy) | 65 @@ -0,0 +1,108 @@ |
233 @@ -818,7 +818,39 @@ | 66 +/* This Source Code Form is subject to the terms of the Mozilla Public |
234 » » unsigned int *outputLen, unsigned int maxOutputLen, | 67 + * License, v. 2.0. If a copy of the MPL was not distributed with this |
235 » » const unsigned char *input, unsigned int inputLen); | 68 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
236 | 69 + |
237 +/******************************************/ | 70 +/* Adopted from the public domain code in NaCl by djb. */ |
| 71 + |
| 72 +#include <string.h> |
| 73 +#include <stdio.h> |
| 74 + |
| 75 +#include "prtypes.h" |
| 76 +#include "chacha20.h" |
| 77 + |
| 78 +#define ROTL32(v, n) (((v) << (n)) | ((v) >> (32 - (n)))) |
| 79 +#define ROTATE(v, c) ROTL32((v), (c)) |
| 80 +#define XOR(v, w) ((v) ^ (w)) |
| 81 +#define PLUS(x, y) ((x) + (y)) |
| 82 + |
| 83 +#define U32TO8_LITTLE(p, v) \ |
| 84 +» { (p)[0] = ((v) ) & 0xff; (p)[1] = ((v) >> 8) & 0xff; \ |
| 85 +» (p)[2] = ((v) >> 16) & 0xff; (p)[3] = ((v) >> 24) & 0xff; } |
| 86 +#define U8TO32_LITTLE(p) \ |
| 87 +» (((PRUint32)((p)[0]) ) | ((PRUint32)((p)[1]) << 8) | \ |
| 88 +» ((PRUint32)((p)[2]) << 16) | ((PRUint32)((p)[3]) << 24) ) |
| 89 + |
| 90 +#define QUARTERROUND(a,b,c,d) \ |
| 91 + x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \ |
| 92 + x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \ |
| 93 + x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \ |
| 94 + x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7); |
| 95 + |
| 96 +static void ChaChaCore(unsigned char output[64], const PRUint32 input[16], |
| 97 +» » int num_rounds) { |
| 98 + PRUint32 x[16]; |
| 99 + int i; |
| 100 + |
| 101 + memcpy(x, input, sizeof(PRUint32) * 16); |
| 102 + for (i = num_rounds; i > 0; i -= 2) { |
| 103 +» QUARTERROUND( 0, 4, 8,12) |
| 104 +» QUARTERROUND( 1, 5, 9,13) |
| 105 +» QUARTERROUND( 2, 6,10,14) |
| 106 +» QUARTERROUND( 3, 7,11,15) |
| 107 +» QUARTERROUND( 0, 5,10,15) |
| 108 +» QUARTERROUND( 1, 6,11,12) |
| 109 +» QUARTERROUND( 2, 7, 8,13) |
| 110 +» QUARTERROUND( 3, 4, 9,14) |
| 111 + } |
| 112 + |
| 113 + for (i = 0; i < 16; ++i) { |
| 114 +» x[i] = PLUS(x[i], input[i]); |
| 115 + } |
| 116 + for (i = 0; i < 16; ++i) { |
| 117 +» U32TO8_LITTLE(output + 4 * i, x[i]); |
| 118 + } |
| 119 +} |
| 120 + |
| 121 +static const unsigned char sigma[16] = "expand 32-byte k"; |
| 122 + |
| 123 +void ChaCha20XOR(unsigned char *out, const unsigned char *in, unsigned int inLe
n, |
| 124 +» » const unsigned char key[32], const unsigned char nonce[8], |
| 125 +» » uint64_t counter) { |
| 126 + unsigned char block[64]; |
| 127 + PRUint32 input[16]; |
| 128 + unsigned int u; |
| 129 + unsigned int i; |
| 130 + |
| 131 + input[4] = U8TO32_LITTLE(key + 0); |
| 132 + input[5] = U8TO32_LITTLE(key + 4); |
| 133 + input[6] = U8TO32_LITTLE(key + 8); |
| 134 + input[7] = U8TO32_LITTLE(key + 12); |
| 135 + |
| 136 + input[8] = U8TO32_LITTLE(key + 16); |
| 137 + input[9] = U8TO32_LITTLE(key + 20); |
| 138 + input[10] = U8TO32_LITTLE(key + 24); |
| 139 + input[11] = U8TO32_LITTLE(key + 28); |
| 140 + |
| 141 + input[0] = U8TO32_LITTLE(sigma + 0); |
| 142 + input[1] = U8TO32_LITTLE(sigma + 4); |
| 143 + input[2] = U8TO32_LITTLE(sigma + 8); |
| 144 + input[3] = U8TO32_LITTLE(sigma + 12); |
| 145 + |
| 146 + input[12] = counter; |
| 147 + input[13] = counter >> 32; |
| 148 + input[14] = U8TO32_LITTLE(nonce + 0); |
| 149 + input[15] = U8TO32_LITTLE(nonce + 4); |
| 150 + |
| 151 + while (inLen >= 64) { |
| 152 +» ChaChaCore(block, input, 20); |
| 153 +» for (i = 0; i < 64; i++) { |
| 154 +» out[i] = in[i] ^ block[i]; |
| 155 +» } |
| 156 + |
| 157 +» input[12]++; |
| 158 +» if (input[12] == 0) { |
| 159 +» input[13]++; |
| 160 +» } |
| 161 + |
| 162 +» inLen -= 64; |
| 163 +» in += 64; |
| 164 +» out += 64; |
| 165 + } |
| 166 + |
| 167 + if (inLen > 0) { |
| 168 +» ChaChaCore(block, input, 20); |
| 169 +» for (i = 0; i < inLen; i++) { |
| 170 +» out[i] = in[i] ^ block[i]; |
| 171 +» } |
| 172 + } |
| 173 +} |
| 174 diff -r c3565a90b8c4 lib/freebl/chacha20/chacha20.h |
| 175 --- /dev/null» Thu Jan 01 00:00:00 1970 +0000 |
| 176 +++ b/lib/freebl/chacha20/chacha20.h» Tue Jan 07 12:11:36 2014 -0800 |
| 177 @@ -0,0 +1,22 @@ |
238 +/* | 178 +/* |
239 +** ChaCha20+Poly1305 AEAD | 179 + * chacha20.h - header file for ChaCha20 implementation. |
240 +*/ | 180 + * |
241 | 181 + * This Source Code Form is subject to the terms of the Mozilla Public |
242 +extern SECStatus | 182 + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 183 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 184 + |
| 185 +#ifndef FREEBL_CHACHA20_H_ |
| 186 +#define FREEBL_CHACHA20_H_ |
| 187 + |
| 188 +#include <stdint.h> |
| 189 + |
| 190 +/* ChaCha20XOR encrypts |inLen| bytes from |in| with the given key and |
| 191 + * nonce and writes the result to |out|, which may be equal to |in|. The |
| 192 + * initial block counter is specified by |counter|. */ |
| 193 +extern void ChaCha20XOR(unsigned char *out, |
| 194 + const unsigned char *in, unsigned int inLen, |
| 195 + const unsigned char key[32], |
| 196 + const unsigned char nonce[8], |
| 197 + uint64_t counter); |
| 198 + |
| 199 +#endif /* FREEBL_CHACHA20_H_ */ |
| 200 diff -r c3565a90b8c4 lib/freebl/chacha20/chacha20_vec.c |
| 201 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 |
| 202 +++ b/lib/freebl/chacha20/chacha20_vec.c Tue Jan 07 12:11:36 2014 -0800 |
| 203 @@ -0,0 +1,281 @@ |
| 204 +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 205 + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 206 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 207 + |
| 208 +/* This implementation is by Ted Krovetz and was submitted to SUPERCOP and |
| 209 + * marked as public domain. It was been altered to allow for non-aligned inputs |
| 210 + * and to allow the block counter to be passed in specifically. */ |
| 211 + |
| 212 +#include <string.h> |
| 213 + |
| 214 +#include "chacha20.h" |
| 215 + |
| 216 +#ifndef CHACHA_RNDS |
| 217 +#define CHACHA_RNDS 20 /* 8 (high speed), 20 (conservative), 12 (middle) */ |
| 218 +#endif |
| 219 + |
| 220 +/* Architecture-neutral way to specify 16-byte vector of ints */ |
| 221 +typedef unsigned vec __attribute__ ((vector_size (16))); |
| 222 + |
| 223 +/* This implementation is designed for Neon, SSE and AltiVec machines. The |
| 224 + * following specify how to do certain vector operations efficiently on |
| 225 + * each architecture, using intrinsics. |
| 226 + * This implementation supports parallel processing of multiple blocks, |
| 227 + * including potentially using general-purpose registers. |
| 228 + */ |
| 229 +#if __ARM_NEON__ |
| 230 +#include <arm_neon.h> |
| 231 +#define GPR_TOO 1 |
| 232 +#define VBPI 2 |
| 233 +#define ONE (vec)vsetq_lane_u32(1,vdupq_n_u32(0),0) |
| 234 +#define LOAD(m) (vec)(*((vec*)(m))) |
| 235 +#define STORE(m,r) (*((vec*)(m))) = (r) |
| 236 +#define ROTV1(x) (vec)vextq_u32((uint32x4_t)x,(uint32x4_t)x,1) |
| 237 +#define ROTV2(x) (vec)vextq_u32((uint32x4_t)x,(uint32x4_t)x,2) |
| 238 +#define ROTV3(x) (vec)vextq_u32((uint32x4_t)x,(uint32x4_t)x,3) |
| 239 +#define ROTW16(x) (vec)vrev32q_u16((uint16x8_t)x) |
| 240 +#if __clang__ |
| 241 +#define ROTW7(x) (x << ((vec){ 7, 7, 7, 7})) ^ (x >> ((vec){25,25,25,25})) |
| 242 +#define ROTW8(x) (x << ((vec){ 8, 8, 8, 8})) ^ (x >> ((vec){24,24,24,24})) |
| 243 +#define ROTW12(x) (x << ((vec){12,12,12,12})) ^ (x >> ((vec){20,20,20,20})) |
| 244 +#else |
| 245 +#define ROTW7(x) (vec)vsriq_n_u32(vshlq_n_u32((uint32x4_t)x,7),(uint32x4_t)x,2
5) |
| 246 +#define ROTW8(x) (vec)vsriq_n_u32(vshlq_n_u32((uint32x4_t)x,8),(uint32x4_t)x,2
4) |
| 247 +#define ROTW12(x) (vec)vsriq_n_u32(vshlq_n_u32((uint32x4_t)x,12),(uint32x4_t)x,
20) |
| 248 +#endif |
| 249 +#elif __SSE2__ |
| 250 +#include <emmintrin.h> |
| 251 +#define GPR_TOO 0 |
| 252 +#if __clang__ |
| 253 +#define VBPI 4 |
| 254 +#else |
| 255 +#define VBPI 3 |
| 256 +#endif |
| 257 +#define ONE (vec)_mm_set_epi32(0,0,0,1) |
| 258 +#define LOAD(m) (vec)_mm_loadu_si128((__m128i*)(m)) |
| 259 +#define STORE(m,r) _mm_storeu_si128((__m128i*)(m), (__m128i) (r)) |
| 260 +#define ROTV1(x) (vec)_mm_shuffle_epi32((__m128i)x,_MM_SHUFFLE(0,3,2,1)) |
| 261 +#define ROTV2(x) (vec)_mm_shuffle_epi32((__m128i)x,_MM_SHUFFLE(1,0,3,2)) |
| 262 +#define ROTV3(x) (vec)_mm_shuffle_epi32((__m128i)x,_MM_SHUFFLE(2,1,0,3)) |
| 263 +#define ROTW7(x) (vec)(_mm_slli_epi32((__m128i)x, 7) ^ _mm_srli_epi32((__m128i
)x,25)) |
| 264 +#define ROTW12(x) (vec)(_mm_slli_epi32((__m128i)x,12) ^ _mm_srli_epi32((__m128i
)x,20)) |
| 265 +#if __SSSE3__ |
| 266 +#include <tmmintrin.h> |
| 267 +#define ROTW8(x) (vec)_mm_shuffle_epi8((__m128i)x,_mm_set_epi8(14,13,12,15,10,
9,8,11,6,5,4,7,2,1,0,3)) |
| 268 +#define ROTW16(x) (vec)_mm_shuffle_epi8((__m128i)x,_mm_set_epi8(13,12,15,14,9,8
,11,10,5,4,7,6,1,0,3,2)) |
| 269 +#else |
| 270 +#define ROTW8(x) (vec)(_mm_slli_epi32((__m128i)x, 8) ^ _mm_srli_epi32((__m128i
)x,24)) |
| 271 +#define ROTW16(x) (vec)(_mm_slli_epi32((__m128i)x,16) ^ _mm_srli_epi32((__m128i
)x,16)) |
| 272 +#endif |
| 273 +#else |
| 274 +#error -- Implementation supports only machines with neon or SSE2 |
| 275 +#endif |
| 276 + |
| 277 +#ifndef REVV_BE |
| 278 +#define REVV_BE(x) (x) |
| 279 +#endif |
| 280 + |
| 281 +#ifndef REVW_BE |
| 282 +#define REVW_BE(x) (x) |
| 283 +#endif |
| 284 + |
| 285 +#define BPI (VBPI + GPR_TOO) /* Blocks computed per loop iteration */ |
| 286 + |
| 287 +#define DQROUND_VECTORS(a,b,c,d) \ |
| 288 + a += b; d ^= a; d = ROTW16(d); \ |
| 289 + c += d; b ^= c; b = ROTW12(b); \ |
| 290 + a += b; d ^= a; d = ROTW8(d); \ |
| 291 + c += d; b ^= c; b = ROTW7(b); \ |
| 292 + b = ROTV1(b); c = ROTV2(c); d = ROTV3(d); \ |
| 293 + a += b; d ^= a; d = ROTW16(d); \ |
| 294 + c += d; b ^= c; b = ROTW12(b); \ |
| 295 + a += b; d ^= a; d = ROTW8(d); \ |
| 296 + c += d; b ^= c; b = ROTW7(b); \ |
| 297 + b = ROTV3(b); c = ROTV2(c); d = ROTV1(d); |
| 298 + |
| 299 +#define QROUND_WORDS(a,b,c,d) \ |
| 300 + a = a+b; d ^= a; d = d<<16 | d>>16; \ |
| 301 + c = c+d; b ^= c; b = b<<12 | b>>20; \ |
| 302 + a = a+b; d ^= a; d = d<< 8 | d>>24; \ |
| 303 + c = c+d; b ^= c; b = b<< 7 | b>>25; |
| 304 + |
| 305 +#define WRITE_XOR(in, op, d, v0, v1, v2, v3) \ |
| 306 + STORE(op + d + 0, LOAD(in + d + 0) ^ REVV_BE(v0)); \ |
| 307 + STORE(op + d + 4, LOAD(in + d + 4) ^ REVV_BE(v1)); \ |
| 308 + STORE(op + d + 8, LOAD(in + d + 8) ^ REVV_BE(v2)); \ |
| 309 + STORE(op + d +12, LOAD(in + d +12) ^ REVV_BE(v3)); |
| 310 + |
| 311 +void ChaCha20XOR( |
| 312 + unsigned char *out, |
| 313 + const unsigned char *in, |
| 314 + unsigned int inlen, |
| 315 + const unsigned char key[32], |
| 316 + const unsigned char nonce[8], |
| 317 + uint64_t counter) |
| 318 +{ |
| 319 + unsigned iters, i, *op=(unsigned *)out, *ip=(unsigned *)in, *kp; |
| 320 +#if defined(__ARM_NEON__) |
| 321 + unsigned *np; |
| 322 +#endif |
| 323 + vec s0, s1, s2, s3; |
| 324 +#if !defined(__ARM_NEON__) && !defined(__SSE2__) |
| 325 + __attribute__ ((aligned (16))) unsigned key[8], nonce[4]; |
| 326 +#endif |
| 327 + __attribute__ ((aligned (16))) unsigned chacha_const[] = |
| 328 + {0x61707865,0x3320646E,0x79622D32,0x6B206574}; |
| 329 +#if defined(__ARM_NEON__) || defined(__SSE2__) |
| 330 + kp = (unsigned *)key; |
| 331 +#else |
| 332 + ((vec *)key)[0] = REVV_BE(((vec *)key)[0]); |
| 333 + ((vec *)key)[1] = REVV_BE(((vec *)key)[1]); |
| 334 + nonce[0] = REVW_BE(((unsigned *)nonce)[0]); |
| 335 + nonce[1] = REVW_BE(((unsigned *)nonce)[1]); |
| 336 + nonce[2] = REVW_BE(((unsigned *)nonce)[2]); |
| 337 + nonce[3] = REVW_BE(((unsigned *)nonce)[3]); |
| 338 + kp = (unsigned *)key; |
| 339 + np = (unsigned *)nonce; |
| 340 +#endif |
| 341 +#if defined(__ARM_NEON__) |
| 342 + np = (unsigned*) nonce; |
| 343 +#endif |
| 344 + s0 = LOAD(chacha_const); |
| 345 + s1 = LOAD(&((vec*)kp)[0]); |
| 346 + s2 = LOAD(&((vec*)kp)[1]); |
| 347 + s3 = (vec) { |
| 348 + counter & 0xffffffff, |
| 349 + counter >> 32, |
| 350 + ((uint32_t*)nonce)[0], |
| 351 + ((uint32_t*)nonce)[1] |
| 352 + }; |
| 353 + |
| 354 + for (iters = 0; iters < inlen/(BPI*64); iters++) { |
| 355 +#if GPR_TOO |
| 356 + register unsigned x0, x1, x2, x3, x4, x5, x6, x7, x8, |
| 357 + x9, x10, x11, x12, x13, x14, x15; |
| 358 +#endif |
| 359 +#if VBPI > 2 |
| 360 + vec v8,v9,v10,v11; |
| 361 +#endif |
| 362 +#if VBPI > 3 |
| 363 + vec v12,v13,v14,v15; |
| 364 +#endif |
| 365 + |
| 366 + vec v0,v1,v2,v3,v4,v5,v6,v7; |
| 367 + v4 = v0 = s0; v5 = v1 = s1; v6 = v2 = s2; v3 = s3; |
| 368 + v7 = v3 + ONE; |
| 369 +#if VBPI > 2 |
| 370 + v8 = v4; v9 = v5; v10 = v6; |
| 371 + v11 = v7 + ONE; |
| 372 +#endif |
| 373 +#if VBPI > 3 |
| 374 + v12 = v8; v13 = v9; v14 = v10; |
| 375 + v15 = v11 + ONE; |
| 376 +#endif |
| 377 +#if GPR_TOO |
| 378 + x0 = chacha_const[0]; x1 = chacha_const[1]; |
| 379 + x2 = chacha_const[2]; x3 = chacha_const[3]; |
| 380 + x4 = kp[0]; x5 = kp[1]; x6 = kp[2]; x7 = kp[3]; |
| 381 + x8 = kp[4]; x9 = kp[5]; x10 = kp[6]; x11 = kp[7]; |
| 382 + x12 = (counter & 0xffffffff)+BPI*iters+(BPI-1); x13 = counter >> 32; |
| 383 + x14 = np[0]; x15 = np[1]; |
| 384 +#endif |
| 385 + for (i = CHACHA_RNDS/2; i; i--) { |
| 386 + DQROUND_VECTORS(v0,v1,v2,v3) |
| 387 + DQROUND_VECTORS(v4,v5,v6,v7) |
| 388 +#if VBPI > 2 |
| 389 + DQROUND_VECTORS(v8,v9,v10,v11) |
| 390 +#endif |
| 391 +#if VBPI > 3 |
| 392 + DQROUND_VECTORS(v12,v13,v14,v15) |
| 393 +#endif |
| 394 +#if GPR_TOO |
| 395 + QROUND_WORDS( x0, x4, x8,x12) |
| 396 + QROUND_WORDS( x1, x5, x9,x13) |
| 397 + QROUND_WORDS( x2, x6,x10,x14) |
| 398 + QROUND_WORDS( x3, x7,x11,x15) |
| 399 + QROUND_WORDS( x0, x5,x10,x15) |
| 400 + QROUND_WORDS( x1, x6,x11,x12) |
| 401 + QROUND_WORDS( x2, x7, x8,x13) |
| 402 + QROUND_WORDS( x3, x4, x9,x14) |
| 403 +#endif |
| 404 + } |
| 405 + |
| 406 + WRITE_XOR(ip, op, 0, v0+s0, v1+s1, v2+s2, v3+s3) |
| 407 + s3 += ONE; |
| 408 + WRITE_XOR(ip, op, 16, v4+s0, v5+s1, v6+s2, v7+s3) |
| 409 + s3 += ONE; |
| 410 +#if VBPI > 2 |
| 411 + WRITE_XOR(ip, op, 32, v8+s0, v9+s1, v10+s2, v11+s3) |
| 412 + s3 += ONE; |
| 413 +#endif |
| 414 +#if VBPI > 3 |
| 415 + WRITE_XOR(ip, op, 48, v12+s0, v13+s1, v14+s2, v15+s3) |
| 416 + s3 += ONE; |
| 417 +#endif |
| 418 + ip += VBPI*16; |
| 419 + op += VBPI*16; |
| 420 +#if GPR_TOO |
| 421 + op[0] = REVW_BE(REVW_BE(ip[0]) ^ (x0 + chacha_const[0])); |
| 422 + op[1] = REVW_BE(REVW_BE(ip[1]) ^ (x1 + chacha_const[1])); |
| 423 + op[2] = REVW_BE(REVW_BE(ip[2]) ^ (x2 + chacha_const[2])); |
| 424 + op[3] = REVW_BE(REVW_BE(ip[3]) ^ (x3 + chacha_const[3])); |
| 425 + op[4] = REVW_BE(REVW_BE(ip[4]) ^ (x4 + kp[0])); |
| 426 + op[5] = REVW_BE(REVW_BE(ip[5]) ^ (x5 + kp[1])); |
| 427 + op[6] = REVW_BE(REVW_BE(ip[6]) ^ (x6 + kp[2])); |
| 428 + op[7] = REVW_BE(REVW_BE(ip[7]) ^ (x7 + kp[3])); |
| 429 + op[8] = REVW_BE(REVW_BE(ip[8]) ^ (x8 + kp[4])); |
| 430 + op[9] = REVW_BE(REVW_BE(ip[9]) ^ (x9 + kp[5])); |
| 431 + op[10] = REVW_BE(REVW_BE(ip[10]) ^ (x10 + kp[6])); |
| 432 + op[11] = REVW_BE(REVW_BE(ip[11]) ^ (x11 + kp[7])); |
| 433 + op[12] = REVW_BE(REVW_BE(ip[12]) ^ (x12 + (counter & 0xffffffff)+BPI*ite
rs+(BPI-1))); |
| 434 + op[13] = REVW_BE(REVW_BE(ip[13]) ^ (x13 + (counter >> 32))); |
| 435 + op[14] = REVW_BE(REVW_BE(ip[14]) ^ (x14 + np[0])); |
| 436 + op[15] = REVW_BE(REVW_BE(ip[15]) ^ (x15 + np[1])); |
| 437 + s3 += ONE; |
| 438 + ip += 16; |
| 439 + op += 16; |
| 440 +#endif |
| 441 + } |
| 442 + |
| 443 + for (iters = inlen%(BPI*64)/64; iters != 0; iters--) { |
| 444 + vec v0 = s0, v1 = s1, v2 = s2, v3 = s3; |
| 445 + for (i = CHACHA_RNDS/2; i; i--) { |
| 446 + DQROUND_VECTORS(v0,v1,v2,v3); |
| 447 + } |
| 448 + WRITE_XOR(ip, op, 0, v0+s0, v1+s1, v2+s2, v3+s3) |
| 449 + s3 += ONE; |
| 450 + ip += 16; |
| 451 + op += 16; |
| 452 + } |
| 453 + |
| 454 + inlen = inlen % 64; |
| 455 + if (inlen) { |
| 456 + __attribute__ ((aligned (16))) vec buf[4]; |
| 457 + vec v0,v1,v2,v3; |
| 458 + v0 = s0; v1 = s1; v2 = s2; v3 = s3; |
| 459 + for (i = CHACHA_RNDS/2; i; i--) { |
| 460 + DQROUND_VECTORS(v0,v1,v2,v3); |
| 461 + } |
| 462 + |
| 463 + if (inlen >= 16) { |
| 464 + STORE(op + 0, LOAD(ip + 0) ^ REVV_BE(v0 + s0)); |
| 465 + if (inlen >= 32) { |
| 466 + STORE(op + 4, LOAD(ip + 4) ^ REVV_BE(v1 + s1)); |
| 467 + if (inlen >= 48) { |
| 468 + STORE(op + 8, LOAD(ip + 8) ^ REVV_BE(v2 + s2)); |
| 469 + buf[3] = REVV_BE(v3 + s3); |
| 470 + } else { |
| 471 + buf[2] = REVV_BE(v2 + s2); |
| 472 + } |
| 473 + } else { |
| 474 + buf[1] = REVV_BE(v1 + s1); |
| 475 + } |
| 476 + } else { |
| 477 + buf[0] = REVV_BE(v0 + s0); |
| 478 + } |
| 479 + |
| 480 + for (i=inlen & ~15; i<inlen; i++) { |
| 481 + ((char *)op)[i] = ((char *)ip)[i] ^ ((char *)buf)[i]; |
| 482 + } |
| 483 + } |
| 484 +} |
| 485 diff -r c3565a90b8c4 lib/freebl/chacha20poly1305.c |
| 486 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 |
| 487 +++ b/lib/freebl/chacha20poly1305.c Tue Jan 07 12:11:36 2014 -0800 |
| 488 @@ -0,0 +1,169 @@ |
| 489 +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 490 + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 491 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 492 + |
| 493 +#ifdef FREEBL_NO_DEPEND |
| 494 +#include "stubs.h" |
| 495 +#endif |
| 496 + |
| 497 +#include <string.h> |
| 498 +#include <stdio.h> |
| 499 + |
| 500 +#include "seccomon.h" |
| 501 +#include "secerr.h" |
| 502 +#include "blapit.h" |
| 503 +#include "poly1305/poly1305.h" |
| 504 +#include "chacha20/chacha20.h" |
| 505 +#include "chacha20poly1305.h" |
| 506 + |
| 507 +/* Poly1305Do writes the Poly1305 authenticator of the given additional data |
| 508 + * and ciphertext to |out|. */ |
| 509 +static void |
| 510 +Poly1305Do(unsigned char *out, |
| 511 + const unsigned char *ad, unsigned int adLen, |
| 512 + const unsigned char *ciphertext, unsigned int ciphertextLen, |
| 513 + const unsigned char key[32]) |
| 514 +{ |
| 515 + poly1305_state state; |
| 516 + unsigned int j; |
| 517 + unsigned char lengthBytes[8]; |
| 518 + unsigned int i; |
| 519 + |
| 520 + Poly1305Init(&state, key); |
| 521 + j = adLen; |
| 522 + for (i = 0; i < sizeof(lengthBytes); i++) { |
| 523 + lengthBytes[i] = j; |
| 524 + j >>= 8; |
| 525 + } |
| 526 + Poly1305Update(&state, ad, adLen); |
| 527 + Poly1305Update(&state, lengthBytes, sizeof(lengthBytes)); |
| 528 + j = ciphertextLen; |
| 529 + for (i = 0; i < sizeof(lengthBytes); i++) { |
| 530 + lengthBytes[i] = j; |
| 531 + j >>= 8; |
| 532 + } |
| 533 + Poly1305Update(&state, ciphertext, ciphertextLen); |
| 534 + Poly1305Update(&state, lengthBytes, sizeof(lengthBytes)); |
| 535 + Poly1305Finish(&state, out); |
| 536 +} |
| 537 + |
| 538 +SECStatus |
243 +ChaCha20Poly1305_InitContext(ChaCha20Poly1305Context *ctx, | 539 +ChaCha20Poly1305_InitContext(ChaCha20Poly1305Context *ctx, |
244 + const unsigned char *key, unsigned int keyLen, | 540 + const unsigned char *key, unsigned int keyLen, |
245 +» » » unsigned int tagLen); | 541 +» » » unsigned int tagLen) |
246 + | 542 +{ |
247 +extern ChaCha20Poly1305Context * | 543 + if (keyLen != 32) { |
| 544 +» PORT_SetError(SEC_ERROR_BAD_KEY); |
| 545 +» return SECFailure; |
| 546 + } |
| 547 + if (tagLen == 0 || tagLen > 16) { |
| 548 +» PORT_SetError(SEC_ERROR_INPUT_LEN); |
| 549 +» return SECFailure; |
| 550 + } |
| 551 + |
| 552 + memcpy(ctx->key, key, sizeof(ctx->key)); |
| 553 + ctx->tagLen = tagLen; |
| 554 + |
| 555 + return SECSuccess; |
| 556 +} |
| 557 + |
| 558 +ChaCha20Poly1305Context * |
248 +ChaCha20Poly1305_CreateContext(const unsigned char *key, unsigned int keyLen, | 559 +ChaCha20Poly1305_CreateContext(const unsigned char *key, unsigned int keyLen, |
249 +» » » unsigned int tagLen); | 560 +» » » unsigned int tagLen) |
250 + | 561 +{ |
251 +extern void | 562 + ChaCha20Poly1305Context *ctx; |
252 +ChaCha20Poly1305_DestroyContext(ChaCha20Poly1305Context *ctx, PRBool freeit); | 563 + |
253 + | 564 + ctx = PORT_New(ChaCha20Poly1305Context); |
254 +extern SECStatus | 565 + if (ctx == NULL) { |
| 566 +» return NULL; |
| 567 + } |
| 568 + |
| 569 + if (ChaCha20Poly1305_InitContext(ctx, key, keyLen, tagLen) != SECSuccess) { |
| 570 +» PORT_Free(ctx); |
| 571 +» ctx = NULL; |
| 572 + } |
| 573 + |
| 574 + return ctx; |
| 575 +} |
| 576 + |
| 577 +void |
| 578 +ChaCha20Poly1305_DestroyContext(ChaCha20Poly1305Context *ctx, PRBool freeit) |
| 579 +{ |
| 580 + memset(ctx, 0, sizeof(*ctx)); |
| 581 + if (freeit) { |
| 582 +» PORT_Free(ctx); |
| 583 + } |
| 584 +} |
| 585 + |
| 586 +SECStatus |
255 +ChaCha20Poly1305_Seal(const ChaCha20Poly1305Context *ctx, | 587 +ChaCha20Poly1305_Seal(const ChaCha20Poly1305Context *ctx, |
256 + unsigned char *output, unsigned int *outputLen, | 588 + unsigned char *output, unsigned int *outputLen, |
257 + unsigned int maxOutputLen, | 589 + unsigned int maxOutputLen, |
258 + const unsigned char *input, unsigned int inputLen, | 590 + const unsigned char *input, unsigned int inputLen, |
259 + const unsigned char *nonce, unsigned int nonceLen, | 591 + const unsigned char *nonce, unsigned int nonceLen, |
260 +» » const unsigned char *ad, unsigned int adLen); | 592 +» » const unsigned char *ad, unsigned int adLen) |
261 + | 593 +{ |
262 +extern SECStatus | 594 + unsigned char block[64]; |
| 595 + unsigned char tag[16]; |
| 596 + |
| 597 + if (nonceLen != 8) { |
| 598 +» PORT_SetError(SEC_ERROR_INPUT_LEN); |
| 599 +» return SECFailure; |
| 600 + } |
| 601 + *outputLen = inputLen + ctx->tagLen; |
| 602 + if (maxOutputLen < *outputLen) { |
| 603 +» PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
| 604 +» return SECFailure; |
| 605 + } |
| 606 + |
| 607 + memset(block, 0, sizeof(block)); |
| 608 + // Generate a block of keystream. The first 32 bytes will be the poly1305 |
| 609 + // key. The remainder of the block is discarded. |
| 610 + ChaCha20XOR(block, block, sizeof(block), ctx->key, nonce, 0); |
| 611 + ChaCha20XOR(output, input, inputLen, ctx->key, nonce, 1); |
| 612 + |
| 613 + Poly1305Do(tag, ad, adLen, output, inputLen, block); |
| 614 + memcpy(output + inputLen, tag, ctx->tagLen); |
| 615 + |
| 616 + return SECSuccess; |
| 617 +} |
| 618 + |
| 619 +SECStatus |
263 +ChaCha20Poly1305_Open(const ChaCha20Poly1305Context *ctx, | 620 +ChaCha20Poly1305_Open(const ChaCha20Poly1305Context *ctx, |
264 + unsigned char *output, unsigned int *outputLen, | 621 + unsigned char *output, unsigned int *outputLen, |
265 + unsigned int maxOutputLen, | 622 + unsigned int maxOutputLen, |
266 + const unsigned char *input, unsigned int inputLen, | 623 + const unsigned char *input, unsigned int inputLen, |
267 + const unsigned char *nonce, unsigned int nonceLen, | 624 + const unsigned char *nonce, unsigned int nonceLen, |
268 +» » const unsigned char *ad, unsigned int adLen); | 625 +» » const unsigned char *ad, unsigned int adLen) |
269 + | 626 +{ |
270 /******************************************/ | 627 + unsigned char block[64]; |
271 /* | 628 + unsigned char tag[16]; |
272 ** MD5 secure hash function | 629 + |
273 Index: nss/lib/freebl/poly1305/poly1305-donna-x64-sse2-incremental-source.c | 630 + if (nonceLen != 8) { |
274 =================================================================== | 631 +» PORT_SetError(SEC_ERROR_INPUT_LEN); |
275 --- nss/lib/freebl/poly1305/poly1305-donna-x64-sse2-incremental-source.c»
(revision 0) | 632 +» return SECFailure; |
276 +++ nss/lib/freebl/poly1305/poly1305-donna-x64-sse2-incremental-source.c»
(revision 0) | 633 + } |
| 634 + if (inputLen < ctx->tagLen) { |
| 635 +» PORT_SetError(SEC_ERROR_INPUT_LEN); |
| 636 +» return SECFailure; |
| 637 + } |
| 638 + *outputLen = inputLen - ctx->tagLen; |
| 639 + if (maxOutputLen < *outputLen) { |
| 640 +» PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
| 641 +» return SECFailure; |
| 642 + } |
| 643 + |
| 644 + memset(block, 0, sizeof(block)); |
| 645 + // Generate a block of keystream. The first 32 bytes will be the poly1305 |
| 646 + // key. The remainder of the block is discarded. |
| 647 + ChaCha20XOR(block, block, sizeof(block), ctx->key, nonce, 0); |
| 648 + Poly1305Do(tag, ad, adLen, input, inputLen - ctx->tagLen, block); |
| 649 + if (NSS_SecureMemcmp(tag, &input[inputLen - ctx->tagLen], ctx->tagLen) != 0
) { |
| 650 +» PORT_SetError(SEC_ERROR_BAD_DATA); |
| 651 +» return SECFailure; |
| 652 + } |
| 653 + |
| 654 + ChaCha20XOR(output, input, inputLen - ctx->tagLen, ctx->key, nonce, 1); |
| 655 + |
| 656 + return SECSuccess; |
| 657 +} |
| 658 diff -r c3565a90b8c4 lib/freebl/chacha20poly1305.h |
| 659 --- /dev/null» Thu Jan 01 00:00:00 1970 +0000 |
| 660 +++ b/lib/freebl/chacha20poly1305.h» Tue Jan 07 12:11:36 2014 -0800 |
| 661 @@ -0,0 +1,15 @@ |
| 662 +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 663 + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 664 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 665 + |
| 666 +#ifndef _CHACHA20_POLY1305_H_ |
| 667 +#define _CHACHA20_POLY1305_H_ 1 |
| 668 + |
| 669 +/* ChaCha20Poly1305ContextStr saves the key and tag length for a |
| 670 + * ChaCha20+Poly1305 AEAD operation. */ |
| 671 +struct ChaCha20Poly1305ContextStr { |
| 672 + unsigned char key[32]; |
| 673 + unsigned char tagLen; |
| 674 +}; |
| 675 + |
| 676 +#endif /* _CHACHA20_POLY1305_H_ */ |
| 677 diff -r c3565a90b8c4 lib/freebl/poly1305/poly1305-donna-x64-sse2-incremental-sou
rce.c |
| 678 --- /dev/null» Thu Jan 01 00:00:00 1970 +0000 |
| 679 +++ b/lib/freebl/poly1305/poly1305-donna-x64-sse2-incremental-source.c» Tue Jan
07 12:11:36 2014 -0800 |
277 @@ -0,0 +1,623 @@ | 680 @@ -0,0 +1,623 @@ |
278 +/* This Source Code Form is subject to the terms of the Mozilla Public | 681 +/* 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 | 682 + * 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/. */ | 683 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
281 + | 684 + |
282 +/* This implementation of poly1305 is by Andrew Moon | 685 +/* This implementation of poly1305 is by Andrew Moon |
283 + * (https://github.com/floodyberry/poly1305-donna) and released as public | 686 + * (https://github.com/floodyberry/poly1305-donna) and released as public |
284 + * domain. It implements SIMD vectorization based on the algorithm described in | 687 + * 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 | 688 + * http://cr.yp.to/papers.html#neoncrypto. Unrolled to 2 powers, i.e. 64 byte |
286 + * block size. */ | 689 + * block size. */ |
(...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
891 + /* pad */ | 1294 + /* pad */ |
892 + t0 = ((uint64_t)p->R23.d[3] << 32) | (uint64_t)p->R23.d[1]; | 1295 + 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]; | 1296 + 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); | 1297 + 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); | 1298 + h1 += (t0 & 0xfffffffffff) + c; c = (h1 >> 44); h1 &= 0xfffffffffff; t1
= (t1 >> 24); |
896 + h2 += (t1 ) + c; | 1299 + h2 += (t1 ) + c; |
897 + | 1300 + |
898 + U64TO8_LE(mac + 0, ((h0 ) | (h1 << 44))); | 1301 + U64TO8_LE(mac + 0, ((h0 ) | (h1 << 44))); |
899 + U64TO8_LE(mac + 8, ((h1 >> 20) | (h2 << 24))); | 1302 + U64TO8_LE(mac + 8, ((h1 >> 20) | (h2 << 24))); |
900 +} | 1303 +} |
901 | 1304 diff -r c3565a90b8c4 lib/freebl/poly1305/poly1305.c |
902 Property changes on: nss/lib/freebl/poly1305/poly1305-donna-x64-sse2-incremental
-source.c | 1305 --- /dev/null» Thu Jan 01 00:00:00 1970 +0000 |
903 ___________________________________________________________________ | 1306 +++ b/lib/freebl/poly1305/poly1305.c» Tue Jan 07 12:11:36 2014 -0800 |
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 @@ | 1307 @@ -0,0 +1,254 @@ |
954 +/* This Source Code Form is subject to the terms of the Mozilla Public | 1308 +/* 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 | 1309 + * 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/. */ | 1310 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
957 + | 1311 + |
958 +/* This implementation of poly1305 is by Andrew Moon | 1312 +/* This implementation of poly1305 is by Andrew Moon |
959 + * (https://github.com/floodyberry/poly1305-donna) and released as public | 1313 + * (https://github.com/floodyberry/poly1305-donna) and released as public |
960 + * domain. */ | 1314 + * domain. */ |
961 + | 1315 + |
962 +#include <string.h> | 1316 +#include <string.h> |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1198 + f0 = ((state->h0 ) | (state->h1 << 26)) + (uint64_t)U8TO32_LE(&stat
e->key[0]); | 1552 + 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]); | 1553 + 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]); | 1554 + 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]); | 1555 + f3 = ((state->h3 >> 18) | (state->h4 << 8)) + (uint64_t)U8TO32_LE(&stat
e->key[12]); |
1202 + | 1556 + |
1203 + U32TO8_LE(&mac[ 0], f0); f1 += (f0 >> 32); | 1557 + U32TO8_LE(&mac[ 0], f0); f1 += (f0 >> 32); |
1204 + U32TO8_LE(&mac[ 4], f1); f2 += (f1 >> 32); | 1558 + U32TO8_LE(&mac[ 4], f1); f2 += (f1 >> 32); |
1205 + U32TO8_LE(&mac[ 8], f2); f3 += (f2 >> 32); | 1559 + U32TO8_LE(&mac[ 8], f2); f3 += (f2 >> 32); |
1206 + U32TO8_LE(&mac[12], f3); | 1560 + U32TO8_LE(&mac[12], f3); |
1207 +} | 1561 +} |
1208 | 1562 diff -r c3565a90b8c4 lib/freebl/poly1305/poly1305.h |
1209 Property changes on: nss/lib/freebl/poly1305/poly1305.c | 1563 --- /dev/null» Thu Jan 01 00:00:00 1970 +0000 |
1210 ___________________________________________________________________ | 1564 +++ b/lib/freebl/poly1305/poly1305.h» Tue Jan 07 12:11:36 2014 -0800 |
1211 Added: svn:eol-style | 1565 @@ -0,0 +1,31 @@ |
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 +/* | 1566 +/* |
1400 + * chacha20.h - header file for ChaCha20 implementation. | 1567 + * poly1305.h - header file for Poly1305 implementation. |
1401 + * | 1568 + * |
1402 + * This Source Code Form is subject to the terms of the Mozilla Public | 1569 + * 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 | 1570 + * 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/. */ | 1571 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
1405 + | 1572 + |
1406 +#ifndef FREEBL_CHACHA20_H_ | 1573 +#ifndef FREEBL_POLY1305_H_ |
1407 +#define FREEBL_CHACHA20_H_ | 1574 +#define FREEBL_POLY1305_H_ |
1408 + | 1575 + |
1409 +#include <stdint.h> | 1576 +typedef unsigned char poly1305_state[512]; |
1410 + | 1577 + |
1411 +/* ChaCha20XOR encrypts |inLen| bytes from |in| with the given key and | 1578 +/* Poly1305Init sets up |state| so that it can be used to calculate an |
1412 + * nonce and writes the result to |out|, which may be equal to |in|. The | 1579 + * authentication tag with the one-time key |key|. Note that |key| is a |
1413 + * initial block counter is specified by |counter|. */ | 1580 + * one-time key and therefore there is no `reset' method because that would |
1414 +extern void ChaCha20XOR(unsigned char *out, | 1581 + * enable several messages to be authenticated with the same key. */ |
1415 + const unsigned char *in, unsigned int inLen, | 1582 +extern void Poly1305Init(poly1305_state* state, |
1416 + const unsigned char key[32], | 1583 + const unsigned char key[32]); |
1417 + const unsigned char nonce[8], | 1584 + |
1418 + uint64_t counter); | 1585 +/* Poly1305Update processes |in_len| bytes from |in|. It can be called zero or |
1419 + | 1586 + * more times after poly1305_init. */ |
1420 +#endif /* FREEBL_CHACHA20_H_ */ | 1587 +extern void Poly1305Update(poly1305_state* state, |
1421 | 1588 + const unsigned char *in, |
1422 Property changes on: nss/lib/freebl/chacha20/chacha20.h | 1589 + size_t inLen); |
1423 ___________________________________________________________________ | 1590 + |
1424 Added: svn:eol-style | 1591 +/* Poly1305Finish completes the poly1305 calculation and writes a 16 byte |
1425 + LF | 1592 + * authentication tag to |mac|. */ |
1426 | 1593 +extern void Poly1305Finish(poly1305_state* state, |
1427 Index: nss/lib/freebl/chacha20/chacha20_vec.c | 1594 + unsigned char mac[16]); |
1428 =================================================================== | 1595 + |
1429 --- nss/lib/freebl/chacha20/chacha20_vec.c (revision 0) | 1596 +#endif /* FREEBL_POLY1305_H_ */ |
1430 +++ nss/lib/freebl/chacha20/chacha20_vec.c (revision 0) | 1597 diff -r c3565a90b8c4 lib/pk11wrap/pk11mech.c |
1431 @@ -0,0 +1,281 @@ | 1598 --- a/lib/pk11wrap/pk11mech.c Fri Jan 03 20:59:10 2014 +0100 |
1432 +/* This Source Code Form is subject to the terms of the Mozilla Public | 1599 +++ b/lib/pk11wrap/pk11mech.c Tue Jan 07 12:11:36 2014 -0800 |
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 @@ | 1600 @@ -152,6 +152,8 @@ |
1869 return CKM_SEED_CBC; | 1601 return CKM_SEED_CBC; |
1870 case CKK_CAMELLIA: | 1602 case CKK_CAMELLIA: |
1871 return CKM_CAMELLIA_CBC; | 1603 return CKM_CAMELLIA_CBC; |
1872 + case CKK_NSS_CHACHA20: | 1604 + case CKK_NSS_CHACHA20: |
1873 + return CKM_NSS_CHACHA20_POLY1305; | 1605 + return CKM_NSS_CHACHA20_POLY1305; |
1874 case CKK_AES: | 1606 case CKK_AES: |
1875 return CKM_AES_CBC; | 1607 return CKM_AES_CBC; |
1876 case CKK_DES: | 1608 case CKK_DES: |
1877 @@ -219,6 +221,8 @@ | 1609 @@ -219,6 +221,8 @@ |
1878 case CKM_CAMELLIA_CBC_PAD: | 1610 case CKM_CAMELLIA_CBC_PAD: |
1879 case CKM_CAMELLIA_KEY_GEN: | 1611 case CKM_CAMELLIA_KEY_GEN: |
1880 return CKK_CAMELLIA; | 1612 return CKK_CAMELLIA; |
1881 + case CKM_NSS_CHACHA20_POLY1305: | 1613 + case CKM_NSS_CHACHA20_POLY1305: |
1882 + return CKK_NSS_CHACHA20; | 1614 + return CKK_NSS_CHACHA20; |
1883 case CKM_AES_ECB: | 1615 case CKM_AES_ECB: |
1884 case CKM_AES_CBC: | 1616 case CKM_AES_CBC: |
1885 case CKM_AES_CCM: | 1617 case CKM_AES_CCM: |
1886 @@ -429,6 +433,8 @@ | 1618 @@ -429,6 +433,8 @@ |
1887 case CKM_CAMELLIA_CBC_PAD: | 1619 case CKM_CAMELLIA_CBC_PAD: |
1888 case CKM_CAMELLIA_KEY_GEN: | 1620 case CKM_CAMELLIA_KEY_GEN: |
1889 return CKM_CAMELLIA_KEY_GEN; | 1621 return CKM_CAMELLIA_KEY_GEN; |
1890 + case CKM_NSS_CHACHA20_POLY1305: | 1622 + case CKM_NSS_CHACHA20_POLY1305: |
1891 + return CKM_NSS_CHACHA20_KEY_GEN; | 1623 + return CKM_NSS_CHACHA20_KEY_GEN; |
1892 case CKM_AES_ECB: | 1624 case CKM_AES_ECB: |
1893 case CKM_AES_CBC: | 1625 case CKM_AES_CBC: |
1894 case CKM_AES_CCM: | 1626 case CKM_AES_CCM: |
1895 Index: nss/lib/util/pkcs11n.h | 1627 diff -r c3565a90b8c4 lib/softoken/pkcs11.c |
1896 =================================================================== | 1628 --- a/lib/softoken/pkcs11.c» Fri Jan 03 20:59:10 2014 +0100 |
1897 --- nss/lib/util/pkcs11n.h» (revision 228205) | 1629 +++ b/lib/softoken/pkcs11.c» Tue Jan 07 12:11:36 2014 -0800 |
1898 +++ nss/lib/util/pkcs11n.h» (working copy) | 1630 @@ -368,6 +368,9 @@ |
| 1631 {CKM_SEED_MAC,» » {16, 16, CKF_SN_VR},» » PR_TRUE}, |
| 1632 {CKM_SEED_MAC_GENERAL,» {16, 16, CKF_SN_VR},» » PR_TRUE}, |
| 1633 {CKM_SEED_CBC_PAD,» » {16, 16, CKF_EN_DE_WR_UN},» PR_TRUE}
, |
| 1634 + /* ------------------------- ChaCha20 Operations ---------------------- */ |
| 1635 + {CKM_NSS_CHACHA20_KEY_GEN,» {32, 32, CKF_GENERATE},»» PR_TRUE}
, |
| 1636 + {CKM_NSS_CHACHA20_POLY1305,{32, 32, CKF_EN_DE},» » PR_TRUE}, |
| 1637 /* ------------------------- Hashing Operations ----------------------- */ |
| 1638 {CKM_MD2,»» » {0, 0, CKF_DIGEST},» » PR_FALSE}, |
| 1639 {CKM_MD2_HMAC,» » {1, 128, CKF_SN_VR},» » PR_TRUE}, |
| 1640 diff -r c3565a90b8c4 lib/softoken/pkcs11c.c |
| 1641 --- a/lib/softoken/pkcs11c.c» Fri Jan 03 20:59:10 2014 +0100 |
| 1642 +++ b/lib/softoken/pkcs11c.c» Tue Jan 07 12:11:36 2014 -0800 |
| 1643 @@ -632,6 +632,97 @@ |
| 1644 return rv; |
| 1645 } |
| 1646 |
| 1647 +static SFTKChaCha20Poly1305Info * |
| 1648 +sftk_ChaCha20Poly1305_CreateContext(const unsigned char *key, |
| 1649 +» » » » unsigned int keyLen, |
| 1650 +» » » » const CK_NSS_AEAD_PARAMS* params) |
| 1651 +{ |
| 1652 + SFTKChaCha20Poly1305Info *ctx; |
| 1653 + |
| 1654 + if (params->ulIvLen != sizeof(ctx->nonce)) { |
| 1655 +» PORT_SetError(SEC_ERROR_INPUT_LEN); |
| 1656 +» return NULL; |
| 1657 + } |
| 1658 + |
| 1659 + ctx = PORT_New(SFTKChaCha20Poly1305Info); |
| 1660 + if (ctx == NULL) { |
| 1661 +» return NULL; |
| 1662 + } |
| 1663 + |
| 1664 + if (ChaCha20Poly1305_InitContext(&ctx->freeblCtx, key, keyLen, |
| 1665 +» » » » params->ulTagLen) != SECSuccess) { |
| 1666 +» PORT_Free(ctx); |
| 1667 +» return NULL; |
| 1668 + } |
| 1669 + |
| 1670 + memcpy(ctx->nonce, params->pIv, sizeof(ctx->nonce)); |
| 1671 + |
| 1672 + if (params->ulAADLen > sizeof(ctx->ad)) { |
| 1673 +» /* Need to allocate an overflow buffer for the additional data. */ |
| 1674 +» ctx->adOverflow = (unsigned char *)PORT_Alloc(params->ulAADLen); |
| 1675 +» if (!ctx->adOverflow) { |
| 1676 +» PORT_Free(ctx); |
| 1677 +» return NULL; |
| 1678 +» } |
| 1679 +» memcpy(ctx->adOverflow, params->pAAD, params->ulAADLen); |
| 1680 + } else { |
| 1681 +» ctx->adOverflow = NULL; |
| 1682 +» memcpy(ctx->ad, params->pAAD, params->ulAADLen); |
| 1683 + } |
| 1684 + ctx->adLen = params->ulAADLen; |
| 1685 + |
| 1686 + return ctx; |
| 1687 +} |
| 1688 + |
| 1689 +static void |
| 1690 +sftk_ChaCha20Poly1305_DestroyContext(SFTKChaCha20Poly1305Info *ctx, |
| 1691 +» » » » PRBool freeit) |
| 1692 +{ |
| 1693 + ChaCha20Poly1305_DestroyContext(&ctx->freeblCtx, PR_FALSE); |
| 1694 + if (ctx->adOverflow != NULL) { |
| 1695 +» PORT_Free(ctx->adOverflow); |
| 1696 +» ctx->adOverflow = NULL; |
| 1697 + } |
| 1698 + ctx->adLen = 0; |
| 1699 + if (freeit) { |
| 1700 +» PORT_Free(ctx); |
| 1701 + } |
| 1702 +} |
| 1703 + |
| 1704 +static SECStatus |
| 1705 +sftk_ChaCha20Poly1305_Encrypt(const SFTKChaCha20Poly1305Info *ctx, |
| 1706 +» » » unsigned char *output, unsigned int *outputLen, |
| 1707 +» » » unsigned int maxOutputLen, |
| 1708 +» » » const unsigned char *input, unsigned int inputLen) |
| 1709 +{ |
| 1710 + const unsigned char *ad = ctx->adOverflow; |
| 1711 + |
| 1712 + if (ad == NULL) { |
| 1713 +» ad = ctx->ad; |
| 1714 + } |
| 1715 + |
| 1716 + return ChaCha20Poly1305_Seal(&ctx->freeblCtx, output, outputLen, |
| 1717 +» » » » maxOutputLen, input, inputLen, ctx->nonce, |
| 1718 +» » » » sizeof(ctx->nonce), ad, ctx->adLen); |
| 1719 +} |
| 1720 + |
| 1721 +static SECStatus |
| 1722 +sftk_ChaCha20Poly1305_Decrypt(const SFTKChaCha20Poly1305Info *ctx, |
| 1723 +» » » unsigned char *output, unsigned int *outputLen, |
| 1724 +» » » unsigned int maxOutputLen, |
| 1725 +» » » const unsigned char *input, unsigned int inputLen) |
| 1726 +{ |
| 1727 + const unsigned char *ad = ctx->adOverflow; |
| 1728 + |
| 1729 + if (ad == NULL) { |
| 1730 +» ad = ctx->ad; |
| 1731 + } |
| 1732 + |
| 1733 + return ChaCha20Poly1305_Open(&ctx->freeblCtx, output, outputLen, |
| 1734 +» » » » maxOutputLen, input, inputLen, ctx->nonce, |
| 1735 +» » » » sizeof(ctx->nonce), ad, ctx->adLen); |
| 1736 +} |
| 1737 + |
| 1738 /** NSC_CryptInit initializes an encryption/Decryption operation. |
| 1739 * |
| 1740 * Always called by NSC_EncryptInit, NSC_DecryptInit, NSC_WrapKey,NSC_UnwrapKey
. |
| 1741 @@ -1027,6 +1118,35 @@ |
| 1742 » context->destroy = (SFTKDestroy) AES_DestroyContext; |
| 1743 » break; |
| 1744 |
| 1745 + case CKM_NSS_CHACHA20_POLY1305: |
| 1746 +» if (pMechanism->ulParameterLen != sizeof(CK_NSS_AEAD_PARAMS)) { |
| 1747 +» crv = CKR_MECHANISM_PARAM_INVALID; |
| 1748 +» break; |
| 1749 +» } |
| 1750 +» context->multi = PR_FALSE; |
| 1751 +» if (key_type != CKK_NSS_CHACHA20) { |
| 1752 +» crv = CKR_KEY_TYPE_INCONSISTENT; |
| 1753 +» break; |
| 1754 +» } |
| 1755 +» att = sftk_FindAttribute(key,CKA_VALUE); |
| 1756 +» if (att == NULL) { |
| 1757 +» crv = CKR_KEY_HANDLE_INVALID; |
| 1758 +» break; |
| 1759 +» } |
| 1760 +» context->cipherInfo = sftk_ChaCha20Poly1305_CreateContext( |
| 1761 +» » (unsigned char*) att->attrib.pValue, att->attrib.ulValueLen, |
| 1762 +» » (CK_NSS_AEAD_PARAMS*) pMechanism->pParameter); |
| 1763 +» sftk_FreeAttribute(att); |
| 1764 +» if (context->cipherInfo == NULL) { |
| 1765 +» crv = sftk_MapCryptError(PORT_GetError()); |
| 1766 +» break; |
| 1767 +» } |
| 1768 +» context->update = (SFTKCipher) (isEncrypt ? |
| 1769 +» » » » » sftk_ChaCha20Poly1305_Encrypt : |
| 1770 +» » » » » sftk_ChaCha20Poly1305_Decrypt); |
| 1771 +» context->destroy = (SFTKDestroy) sftk_ChaCha20Poly1305_DestroyContext; |
| 1772 +» break; |
| 1773 + |
| 1774 case CKM_NETSCAPE_AES_KEY_WRAP_PAD: |
| 1775 » context->doPad = PR_TRUE; |
| 1776 » /* fall thru */ |
| 1777 @@ -3601,6 +3721,10 @@ |
| 1778 » *key_type = CKK_AES; |
| 1779 » if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; |
| 1780 » break; |
| 1781 + case CKM_NSS_CHACHA20_KEY_GEN: |
| 1782 +» *key_type = CKK_NSS_CHACHA20; |
| 1783 +» if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; |
| 1784 +» break; |
| 1785 default: |
| 1786 » PORT_Assert(0); |
| 1787 » crv = CKR_MECHANISM_INVALID; |
| 1788 @@ -3846,6 +3970,7 @@ |
| 1789 case CKM_SEED_KEY_GEN: |
| 1790 case CKM_CAMELLIA_KEY_GEN: |
| 1791 case CKM_AES_KEY_GEN: |
| 1792 + case CKM_NSS_CHACHA20_KEY_GEN: |
| 1793 #if NSS_SOFTOKEN_DOES_RC5 |
| 1794 case CKM_RC5_KEY_GEN: |
| 1795 #endif |
| 1796 diff -r c3565a90b8c4 lib/softoken/pkcs11i.h |
| 1797 --- a/lib/softoken/pkcs11i.h» Fri Jan 03 20:59:10 2014 +0100 |
| 1798 +++ b/lib/softoken/pkcs11i.h» Tue Jan 07 12:11:36 2014 -0800 |
| 1799 @@ -14,6 +14,7 @@ |
| 1800 #include "pkcs11t.h" |
| 1801 |
| 1802 #include "sftkdbt.h" |
| 1803 +#include "chacha20poly1305.h" |
| 1804 #include "hasht.h" |
| 1805 |
| 1806 /* |
| 1807 @@ -104,6 +105,7 @@ |
| 1808 typedef struct SFTKOAEPEncryptInfoStr SFTKOAEPEncryptInfo; |
| 1809 typedef struct SFTKOAEPDecryptInfoStr SFTKOAEPDecryptInfo; |
| 1810 typedef struct SFTKSSLMACInfoStr SFTKSSLMACInfo; |
| 1811 +typedef struct SFTKChaCha20Poly1305InfoStr SFTKChaCha20Poly1305Info; |
| 1812 typedef struct SFTKItemTemplateStr SFTKItemTemplate; |
| 1813 |
| 1814 /* define function pointer typdefs for pointer tables */ |
| 1815 @@ -399,6 +401,16 @@ |
| 1816 unsigned int» keySize; |
| 1817 }; |
| 1818 |
| 1819 +/* SFTKChaCha20Poly1305Info saves the key, tag length, nonce, and additional |
| 1820 + * data for a ChaCha20+Poly1305 AEAD operation. */ |
| 1821 +struct SFTKChaCha20Poly1305InfoStr { |
| 1822 + ChaCha20Poly1305Context freeblCtx; |
| 1823 + unsigned char nonce[8]; |
| 1824 + unsigned char ad[16]; |
| 1825 + unsigned char *adOverflow; |
| 1826 + unsigned int adLen; |
| 1827 +}; |
| 1828 + |
| 1829 /* |
| 1830 * Template based on SECItems, suitable for passing as arrays |
| 1831 */ |
| 1832 diff -r c3565a90b8c4 lib/util/pkcs11n.h |
| 1833 --- a/lib/util/pkcs11n.h» Fri Jan 03 20:59:10 2014 +0100 |
| 1834 +++ b/lib/util/pkcs11n.h» Tue Jan 07 12:11:36 2014 -0800 |
1899 @@ -51,6 +51,8 @@ | 1835 @@ -51,6 +51,8 @@ |
1900 #define CKK_NSS_JPAKE_ROUND1 (CKK_NSS + 2) | 1836 #define CKK_NSS_JPAKE_ROUND1 (CKK_NSS + 2) |
1901 #define CKK_NSS_JPAKE_ROUND2 (CKK_NSS + 3) | 1837 #define CKK_NSS_JPAKE_ROUND2 (CKK_NSS + 3) |
1902 | 1838 |
1903 +#define CKK_NSS_CHACHA20 (CKK_NSS + 4) | 1839 +#define CKK_NSS_CHACHA20 (CKK_NSS + 4) |
1904 + | 1840 + |
1905 /* | 1841 /* |
1906 * NSS-defined certificate types | 1842 * NSS-defined certificate types |
1907 * | 1843 * |
1908 @@ -214,6 +216,9 @@ | 1844 @@ -214,6 +216,9 @@ |
(...skipping 14 matching lines...) Expand all Loading... |
1923 + CK_BYTE_PTR pIv; /* This is the nonce. */ | 1859 + CK_BYTE_PTR pIv; /* This is the nonce. */ |
1924 + CK_ULONG ulIvLen; | 1860 + CK_ULONG ulIvLen; |
1925 + CK_BYTE_PTR pAAD; | 1861 + CK_BYTE_PTR pAAD; |
1926 + CK_ULONG ulAADLen; | 1862 + CK_ULONG ulAADLen; |
1927 + CK_ULONG ulTagLen; | 1863 + CK_ULONG ulTagLen; |
1928 +} CK_NSS_AEAD_PARAMS; | 1864 +} CK_NSS_AEAD_PARAMS; |
1929 + | 1865 + |
1930 /* | 1866 /* |
1931 * NSS-defined return values | 1867 * NSS-defined return values |
1932 * | 1868 * |
OLD | NEW |