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

Side by Side Diff: patches/nss-intel-aes-windows.patch

Issue 214183004: Implement AES in different modes of operation, using AES-NI and (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/nss.git@master
Patch Set: Make the changes Ryan suggested. Created 6 years, 8 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
« nss/lib/freebl/intel-gcm-wrap.c ('K') | « nss/lib/freebl/rijndael.c ('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 diff --git a/nss/lib/freebl/ctr.c b/nss/lib/freebl/ctr.c
2 index 3a2f1a6..4d26daa 100644
3 --- a/nss/lib/freebl/ctr.c
4 +++ b/nss/lib/freebl/ctr.c
5 @@ -12,6 +12,11 @@
6 #include "pkcs11t.h"
7 #include "secerr.h"
8
9 +#ifdef USE_HW_AES
10 +#include "intel-aes.h"
11 +#include "rijndael.h"
12 +#endif
13 +
14 SECStatus
15 CTR_InitContext(CTRContext *ctr, void *context, freeblCipherFunc cipher,
16 const unsigned char *param, unsigned int blocksize)
17 @@ -165,3 +170,61 @@ CTR_Update(CTRContext *ctr, unsigned char *outbuf,
18 *outlen += inlen;
19 return SECSuccess;
20 }
21 +
22 +#if defined(USE_HW_AES) && defined(_MSC_VER)
23 +SECStatus
24 +CTR_Update_HW_AES(CTRContext *ctr, unsigned char *outbuf,
25 + unsigned int *outlen, unsigned int maxout,
26 + const unsigned char *inbuf, unsigned int inlen,
27 + unsigned int blocksize)
28 +{
29 + unsigned int fullblocks;
30 + unsigned int tmp;
31 + SECStatus rv;
32 +
33 + if (maxout < inlen) {
34 + *outlen = inlen;
35 + PORT_SetError(SEC_ERROR_OUTPUT_LEN);
36 + return SECFailure;
37 + }
38 + *outlen = 0;
39 + if (ctr->bufPtr != blocksize) {
40 + unsigned int needed = PR_MIN(blocksize-ctr->bufPtr, inlen);
41 + ctr_xor(outbuf, inbuf, ctr->buffer+ctr->bufPtr, needed);
42 + ctr->bufPtr += needed;
43 + outbuf += needed;
44 + inbuf += needed;
45 + *outlen += needed;
46 + inlen -= needed;
47 + if (inlen == 0) {
48 + return SECSuccess;
49 + }
50 + PORT_Assert(ctr->bufPtr == blocksize);
51 + }
52 +
53 + intel_aes_ctr_worker(((AESContext*)(ctr->context))->Nr)(
54 + ctr, outbuf, outlen, maxout, inbuf, inlen, blocksize);
55 + /* XXX intel_aes_ctr_worker should set *outlen. */
56 + PORT_Assert(*outlen == 0);
57 + fullblocks = (inlen/blocksize)*blocksize;
58 + *outlen += fullblocks;
59 + outbuf += fullblocks;
60 + inbuf += fullblocks;
61 + inlen -= fullblocks;
62 +
63 + if (inlen == 0) {
64 + return SECSuccess;
65 + }
66 + rv = (*ctr->cipher)(ctr->context, ctr->buffer, &tmp, blocksize,
67 + ctr->counter, blocksize, blocksize);
68 + ctr_GetNextCtr(ctr->counter, ctr->counterBits, blocksize);
69 + if (rv != SECSuccess) {
70 + return SECFailure;
71 + }
72 + PORT_Assert(tmp == blocksize);
73 + ctr_xor(outbuf, inbuf, ctr->buffer, inlen);
74 + ctr->bufPtr = inlen;
75 + *outlen += inlen;
76 + return SECSuccess;
77 +}
78 +#endif
79 diff --git a/nss/lib/freebl/ctr.h b/nss/lib/freebl/ctr.h
80 index 69ef150..e7645a2 100644
81 --- a/nss/lib/freebl/ctr.h
82 +++ b/nss/lib/freebl/ctr.h
83 @@ -41,4 +41,11 @@ SECStatus CTR_Update(CTRContext *ctr, unsigned char *outbuf,
84 const unsigned char *inbuf, unsigned int inlen,
85 unsigned int blocksize);
86
87 +#ifdef USE_HW_AES
88 +SECStatus CTR_Update_HW_AES(CTRContext *ctr, unsigned char *outbuf,
89 + unsigned int *outlen, unsigned int maxout,
90 + const unsigned char *inbuf, unsigned int inlen,
91 + unsigned int blocksize);
92 +#endif
93 +
94 #endif
95 diff --git a/nss/lib/freebl/intel-aes.h b/nss/lib/freebl/intel-aes.h
96 index 1e18007..3b71e5f 100644
97 --- a/nss/lib/freebl/intel-aes.h
98 +++ b/nss/lib/freebl/intel-aes.h
99 @@ -33,6 +33,12 @@ SECStatus intel_aes_decrypt_cbc_128(AESContext *cx, unsigned char *output,
100 const unsigned char *input,
101 unsigned int inputLen,
102 unsigned int blocksize);
103 +SECStatus intel_aes_encrypt_ctr_128(CTRContext *cx, unsigned char *output,
104 + unsigned int *outputLen,
105 + unsigned int maxOutputLen,
106 + const unsigned char *input,
107 + unsigned int inputLen,
108 + unsigned int blocksize);
109 SECStatus intel_aes_encrypt_ecb_192(AESContext *cx, unsigned char *output,
110 unsigned int *outputLen,
111 unsigned int maxOutputLen,
112 @@ -57,6 +63,12 @@ SECStatus intel_aes_decrypt_cbc_192(AESContext *cx, unsigned char *output,
113 const unsigned char *input,
114 unsigned int inputLen,
115 unsigned int blocksize);
116 +SECStatus intel_aes_encrypt_ctr_192(CTRContext *cx, unsigned char *output,
117 + unsigned int *outputLen,
118 + unsigned int maxOutputLen,
119 + const unsigned char *input,
120 + unsigned int inputLen,
121 + unsigned int blocksize);
122 SECStatus intel_aes_encrypt_ecb_256(AESContext *cx, unsigned char *output,
123 unsigned int *outputLen,
124 unsigned int maxOutputLen,
125 @@ -81,6 +93,12 @@ SECStatus intel_aes_decrypt_cbc_256(AESContext *cx, unsigned char *output,
126 const unsigned char *input,
127 unsigned int inputLen,
128 unsigned int blocksize);
129 +SECStatus intel_aes_encrypt_ctr_256(CTRContext *cx, unsigned char *output,
130 + unsigned int *outputLen,
131 + unsigned int maxOutputLen,
132 + const unsigned char *input,
133 + unsigned int inputLen,
134 + unsigned int blocksize);
135
136
137 #define intel_aes_ecb_worker(encrypt, keysize) \
138 @@ -102,6 +120,11 @@ SECStatus intel_aes_decrypt_cbc_256(AESContext *cx, unsigne d char *output,
139 (keysize) == 24 ? intel_aes_decrypt_cbc_192 : \
140 intel_aes_decrypt_cbc_256))
141
142 +#define intel_aes_ctr_worker(nr) \
143 + ((nr) == 10 ? intel_aes_encrypt_ctr_128 : \
144 + (nr) == 12 ? intel_aes_encrypt_ctr_192 : \
145 + intel_aes_encrypt_ctr_256)
146 +
147
148 #define intel_aes_init(encrypt, keysize) \
149 do { \
150 diff --git a/nss/lib/freebl/intel-gcm-wrap.c b/nss/lib/freebl/intel-gcm-wrap.c
151 index b2f6f5e..afd3029 100644
152 --- a/nss/lib/freebl/intel-gcm-wrap.c
153 +++ b/nss/lib/freebl/intel-gcm-wrap.c
154 @@ -3,7 +3,7 @@
155 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
156 /* Copyright(c) 2013, Intel Corp. */
157
158 -/* Wrapper funcions for Intel optimized implementation of AES-GCM */
159 +/* Wrapper functions for Intel optimized implementation of AES-GCM */
160
161 #ifdef USE_HW_AES
162
163 @@ -24,12 +24,8 @@
164 #include "intel-gcm.h"
165 #include "rijndael.h"
166
167 -#if defined(__INTEL_COMPILER)
168 -#include <ia32intrin.h>
169 -#elif defined(__GNUC__)
170 #include <emmintrin.h>
171 #include <tmmintrin.h>
172 -#endif
173
174
175 struct intel_AES_GCMContextStr{
176 @@ -143,9 +139,9 @@ void intel_AES_GCM_DestroyContext(intel_AES_GCMContext *gcm, PRBool freeit)
177
178 SECStatus intel_AES_GCM_EncryptUpdate(intel_AES_GCMContext *gcm,
179 unsigned char *outbuf,
180 - unsigned int *outlen, unsigned int maxout,
181 - const unsigned char *inbuf, unsigned int inlen,
182 - unsigned int blocksize)
183 + unsigned int *outlen, unsigned int maxout,
184 + const unsigned char *inbuf, unsigned int inlen,
185 + unsigned int blocksize)
186 {
187 unsigned int tagBytes;
188 unsigned char T[AES_BLOCK_SIZE];
189 @@ -189,9 +185,9 @@ SECStatus intel_AES_GCM_EncryptUpdate(intel_AES_GCMContext * gcm,
190
191 SECStatus intel_AES_GCM_DecryptUpdate(intel_AES_GCMContext *gcm,
192 unsigned char *outbuf,
193 - unsigned int *outlen, unsigned int maxout,
194 - const unsigned char *inbuf, unsigned int inlen,
195 - unsigned int blocksize)
196 + unsigned int *outlen, unsigned int maxout,
197 + const unsigned char *inbuf, unsigned int inlen,
198 + unsigned int blocksize)
199 {
200 unsigned int tagBytes;
201 unsigned char T[AES_BLOCK_SIZE];
202 @@ -201,13 +197,19 @@ SECStatus intel_AES_GCM_DecryptUpdate(intel_AES_GCMContext *gcm,
203
204 /* get the authentication block */
205 if (inlen < tagBytes) {
206 - PORT_SetError(SEC_ERROR_INVALID_ARGS);
207 + PORT_SetError(SEC_ERROR_INPUT_LEN);
208 return SECFailure;
209 }
210
211 inlen -= tagBytes;
212 intag = inbuf + inlen;
213
214 + if (maxout < inlen) {
215 + *outlen = inlen;
216 + PORT_SetError(SEC_ERROR_OUTPUT_LEN);
217 + return SECFailure;
218 + }
219 +
220 intel_aes_gcmDEC(
221 inbuf,
222 outbuf,
223 @@ -224,6 +226,8 @@ SECStatus intel_AES_GCM_DecryptUpdate(intel_AES_GCMContext * gcm,
224 T);
225
226 if (NSS_SecureMemcmp(T, intag, tagBytes) != 0) {
227 + memset(outbuf, 0, inlen);
228 + *outlen = 0;
229 /* force a CKR_ENCRYPTED_DATA_INVALID error at in softoken */
230 PORT_SetError(SEC_ERROR_BAD_DATA);
231 return SECFailure;
232 diff --git a/nss/lib/freebl/rijndael.c b/nss/lib/freebl/rijndael.c
233 index 8bb8905..9703867 100644
234 --- a/nss/lib/freebl/rijndael.c
235 +++ b/nss/lib/freebl/rijndael.c
236 @@ -1063,8 +1063,10 @@ aes_InitContext(AESContext *cx, const unsigned char *key, unsigned int keysize,
237 intel_aes_cbc_worker(encrypt, keysize);
238 } else
239 #endif
240 + {
241 cx->worker = (freeblCipherFunc) (encrypt
242 ? &rijndael_encryptCBC : &rijndael_decryptCBC);
243 + }
244 } else {
245 #if USE_HW_AES
246 if (use_hw_aes) {
247 @@ -1072,8 +1074,10 @@ aes_InitContext(AESContext *cx, const unsigned char *key, unsigned int keysize,
248 intel_aes_ecb_worker(encrypt, keysize);
249 } else
250 #endif
251 + {
252 cx->worker = (freeblCipherFunc) (encrypt
253 ? &rijndael_encryptECB : &rijndael_decryptECB);
254 + }
255 }
256 PORT_Assert((cx->Nb * (cx->Nr + 1)) <= RIJNDAEL_MAX_EXP_KEY_SIZE);
257 if ((cx->Nb * (cx->Nr + 1)) > RIJNDAEL_MAX_EXP_KEY_SIZE) {
258 @@ -1171,7 +1175,14 @@ AES_InitContext(AESContext *cx, const unsigned char *key, unsigned int keysize,
259 break;
260 case NSS_AES_CTR:
261 cx->worker_cx = CTR_CreateContext(cx, cx->worker, iv, blocksize);
262 - cx->worker = (freeblCipherFunc) CTR_Update ;
263 +#if defined(USE_HW_AES) && defined(_MSC_VER)
264 + if (use_hw_aes) {
265 + cx->worker = (freeblCipherFunc) CTR_Update_HW_AES;
266 + } else
267 +#endif
268 + {
269 + cx->worker = (freeblCipherFunc) CTR_Update;
270 + }
271 cx->destroy = (freeblDestroyFunc) CTR_DestroyContext;
272 cx->isBlock = PR_FALSE;
273 break;
OLDNEW
« nss/lib/freebl/intel-gcm-wrap.c ('K') | « nss/lib/freebl/rijndael.c ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698