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

Side by Side Diff: nss/mozilla/security/nss/lib/freebl/gcm.c

Issue 10919163: Add GCM, CTR, and CTS modes to AES. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #ifdef FREEBL_NO_DEPEND
6 #include "stubs.h"
7 #endif
8 #include "blapii.h"
9 #include "blapit.h"
10 #include "gcm.h"
11 #include "ctr.h"
12 #include "secerr.h"
13 #include "prtypes.h"
14 #include "pkcs11t.h"
15
16 #include <limits.h>
17
18 /**************************************************************************
19 * First implement the Galois hash function of GCM (gcmHash) *
20 **************************************************************************/
21 #define GCM_HASH_LEN_LEN 8 /* gcm hash defines lengths to be 64 bits */
22
23 typedef struct gcmHashContextStr gcmHashContext;
24
25 static SECStatus gcmHash_InitContext(gcmHashContext *hash,
26 const unsigned char *H,
27 unsigned int blocksize);
28 static void gcmHash_DestroyContext(gcmHashContext *ghash, PRBool freeit);
29 static SECStatus gcmHash_Update(gcmHashContext *ghash,
30 const unsigned char *buf, unsigned int len,
31 unsigned int blocksize);
32 static SECStatus gcmHash_Sync(gcmHashContext *ghash, unsigned int blocksize);
33 static SECStatus gcmHash_Final(gcmHashContext *gcm, unsigned char *outbuf,
34 unsigned int *outlen, unsigned int maxout,
35 unsigned int blocksize);
36 static SECStatus gcmHash_Reset(gcmHashContext *ghash,
37 const unsigned char *inbuf,
38 unsigned int inbufLen, unsigned int blocksize);
39
40 /* compile time defines to select how the GF2 multiply is calculated.
41 * There are currently 2 algorithms implemented here: MPI and ALGORITHM_1.
42 *
43 * MPI uses the GF2m implemented in mpi to support GF2 ECC.
44 * ALGORITHM_1 is the Algorithm 1 in both NIST SP 800-38D and
45 * "The Galois/Counter Mode of Operation (GCM)", McGrew & Viega.
46 */
47 #if !defined(GCM_USE_ALGORITHM_1) && !defined(GCM_USE_MPI)
48 #define GCM_USE_MPI 1 /* MPI is about 5x faster with the
49 * same or less complexity. It's possible to use
50 * tables to speed things up even more */
51 #endif
52
53 /* GCM defines the bit string to be LSB first, which is exactly
54 * opposite everyone else, including hardware. build array
55 * to reverse everything. */
56 static const unsigned char gcm_byte_rev[256] = {
57 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
58 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
59 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
60 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
61 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
62 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
63 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
64 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
65 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
66 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
67 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
68 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
69 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
70 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
71 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
72 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
73 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
74 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
75 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
76 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
77 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
78 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
79 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
80 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
81 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
82 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
83 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
84 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
85 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
86 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
87 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
88 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
89 };
90
91
92 #ifdef GCM_TRACE
93 #include <stdio.h>
94
95 #define GCM_TRACE_X(ghash,label) { \
96 unsigned char _X[MAX_BLOCK_SIZE]; int i; \
97 gcm_getX(ghash, _X, blocksize); \
98 printf(label,(ghash)->m); \
99 for (i=0; i < blocksize; i++) printf("%02x",_X[i]); \
100 printf("\n"); }
101 #define GCM_TRACE_BLOCK(label,buf,blocksize) {\
102 printf(label); \
103 for (i=0; i < blocksize; i++) printf("%02x",buf[i]); \
104 printf("\n"); }
105 #else
106 #define GCM_TRACE_X(ghash,label)
107 #define GCM_TRACE_BLOCK(label,buf,blocksize)
108 #endif
109
110 #ifdef GCM_USE_MPI
111
112 #ifdef GCM_USE_ALGORITHM_1
113 #error "Only define one of GCM_USE_MPI, GCM_USE_ALGORITHM_1"
114 #endif
115 /* use the MPI functions to calculate Xn = (Xn-1^C_i)*H mod poly */
116 #include "mpi.h"
117 #include "secmpi.h"
118 #include "mplogic.h"
119 #include "mp_gf2m.h"
120
121 /* state needed to handle GCM Hash function */
122 struct gcmHashContextStr {
123 mp_int H;
124 mp_int X;
125 mp_int C_i;
126 unsigned int *poly;
127 unsigned long cLen;
128 unsigned char buffer[MAX_BLOCK_SIZE];
129 unsigned int bufLen;
130 int m; /* XXX what is m? */
131 unsigned char counterBuf[2*GCM_HASH_LEN_LEN];
132 };
133
134 /* f = x^128 + x^7 + x^2 + x + 1 */
135 static unsigned int poly_128[] = { 128, 7, 2, 1, 0 };
136 /* f = x^64 + x^4 + x^3 + x + 1 */
137 static unsigned int poly_64[] = { 64, 4, 3, 1, 0 };
138
139 /* sigh, GCM defines the bit strings exactly backwards from everything else */
140 static void
141 gcm_reverse(unsigned char *target, const unsigned char *src,
142 unsigned int blocksize)
143 {
144 unsigned int i;
145 for (i=0; i < blocksize; i++) {
146 target[blocksize-i-1] = gcm_byte_rev[src[i]];
147 }
148 }
149
150 /* Initialize a gcmHashContext */
151 static SECStatus
152 gcmHash_InitContext(gcmHashContext *ghash, const unsigned char *H,
153 unsigned int blocksize)
154 {
155 mp_err err = MP_OKAY;
156 unsigned char H_rev[MAX_BLOCK_SIZE];
157
158 MP_DIGITS(&ghash->H) = 0;
159 MP_DIGITS(&ghash->X) = 0;
160 MP_DIGITS(&ghash->C_i) = 0;
161 CHECK_MPI_OK( mp_init(&ghash->H) );
162 CHECK_MPI_OK( mp_init(&ghash->X) );
163 CHECK_MPI_OK( mp_init(&ghash->C_i) );
164
165 mp_zero(&ghash->X);
166 gcm_reverse(H_rev, H, blocksize);
167 CHECK_MPI_OK( mp_read_unsigned_octets(&ghash->H, H_rev, blocksize) );
168
169 /* set the irreducible polynomial. Each blocksize has its own polynomial.
170 * for now only blocksizes 16 (=128 bits) and 8 (=64 bits) are defined */
171 switch (blocksize) {
172 case 16: /* 128 bits */
173 ghash->poly = poly_128;
174 break;
175 case 8: /* 64 bits */
176 ghash->poly = poly_64;
177 break;
178 default:
179 PORT_SetError(SEC_ERROR_INVALID_ARGS);
180 goto cleanup;
181 }
182 ghash->cLen = 0;
183 ghash->bufLen = 0;
184 ghash->m = 0;
185 PORT_Memset(ghash->counterBuf, 0, sizeof(ghash->counterBuf));
186 return SECSuccess;
187 cleanup:
188 gcmHash_DestroyContext(ghash, PR_FALSE);
189 return SECFailure;
190 }
191
192 /* Destroy a HashContext (Note we zero the digits so this function
193 * is idempotent if called with freeit == PR_FALSE */
194 static void
195 gcmHash_DestroyContext(gcmHashContext *ghash, PRBool freeit)
196 {
197 mp_clear(&ghash->H);
198 mp_clear(&ghash->X);
199 mp_clear(&ghash->C_i);
200 MP_DIGITS(&ghash->H) = 0;
201 MP_DIGITS(&ghash->X) = 0;
202 MP_DIGITS(&ghash->C_i) = 0;
203 if (freeit) {
204 PORT_Free(ghash);
205 }
206 }
207
208 static SECStatus
209 gcm_getX(gcmHashContext *ghash, unsigned char *T, unsigned int blocksize)
210 {
211 int len;
212 mp_err err;
213 unsigned char tmp_buf[MAX_BLOCK_SIZE];
214 unsigned char *X;
215
216 len = mp_unsigned_octet_size(&ghash->X);
217 if (len <= 0) {
218 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
219 return SECFailure;
220 }
221 X = tmp_buf;
222 PORT_Assert(len <= blocksize);
223 if (len > blocksize) {
224 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
225 return SECFailure;
226 }
227 /* zero pad the result */
228 if (len != blocksize) {
229 PORT_Memset(X,0,blocksize-len);
230 X += blocksize-len;
231 }
232
233 err = mp_to_unsigned_octets(&ghash->X, X, len);
234 if (err < 0) {
235 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
236 return SECFailure;
237 }
238 gcm_reverse(T, X, blocksize);
239 return SECSuccess;
240 }
241
242 static SECStatus
243 gcm_HashMult(gcmHashContext *ghash, const unsigned char *buf,
244 unsigned int count, unsigned int blocksize)
245 {
246 SECStatus rv = SECFailure;
247 mp_err err = MP_OKAY;
248 unsigned char tmp_buf[MAX_BLOCK_SIZE];
249 unsigned int i;
250
251 for (i=0; i < count; i++, buf += blocksize) {
252 ghash->m++;
253 gcm_reverse(tmp_buf, buf, blocksize);
254 CHECK_MPI_OK(mp_read_unsigned_octets(&ghash->C_i, tmp_buf, blocksize));
255 CHECK_MPI_OK(mp_badd(&ghash->X, &ghash->C_i, &ghash->C_i));
256 /*
257 * Looking to speed up GCM, this the the place to do it.
258 * There are two areas that can be exploited to speed up this code.
259 *
260 * 1) H is a constant in this multiply. We can precompute H * (0 - 255)
261 * at init time and this becomes an blockize xors of our table lookup.
262 *
263 * 2) poly is a constant for each blocksize. We can calculate the
264 * modulo reduction by a series of adds and shifts.
265 *
266 * For now we are after functionality, so we will go ahead and use
267 * the builtin bmulmod from mpi
268 */
269 CHECK_MPI_OK(mp_bmulmod(&ghash->C_i, &ghash->H,
270 ghash->poly, &ghash->X));
271 GCM_TRACE_X(ghash, "X%d = ")
272 }
273 rv = SECSuccess;
274 cleanup:
275 if (rv != SECSuccess) {
276 MP_TO_SEC_ERROR(err);
277 }
278 return rv;
279 }
280
281 static void
282 gcm_zeroX(gcmHashContext *ghash)
283 {
284 mp_zero(&ghash->X);
285 ghash->m = 0;
286 }
287
288 #endif
289
290 #ifdef GCM_USE_ALGORITHM_1
291 /* use algorithm 1 of McGrew & Viega "The Galois/Counter Mode of Operation" */
292
293 #define GCM_ARRAY_SIZE (MAX_BLOCK_SIZE/sizeof(unsigned long))
294
295 struct gcmHashContextStr {
296 unsigned long H[GCM_ARRAY_SIZE];
297 unsigned long X[GCM_ARRAY_SIZE];
298 unsigned long R;
299 unsigned long cLen;
300 unsigned char buffer[MAX_BLOCK_SIZE];
301 unsigned int bufLen;
302 int m;
303 unsigned char counterBuf[2*GCM_HASH_LEN_LEN];
304 };
305
306 static void
307 gcm_bytes_to_longs(unsigned long *l, const unsigned char *c, unsigned int len)
308 {
309 int i,j;
310 int array_size = len/sizeof(unsigned long);
311
312 PORT_Assert(len % sizeof(unsigned long) == 0);
313 for (i=0; i < array_size; i++) {
314 unsigned long tmp = 0;
315 int byte_offset = i * sizeof(unsigned long);
316 for (j=sizeof(unsigned long)-1; j >= 0; j--) {
317 tmp = (tmp << PR_BITS_PER_BYTE) | gcm_byte_rev[c[byte_offset+j]];
318 }
319 l[i] = tmp;
320 }
321 }
322
323 static void
324 gcm_longs_to_bytes(const unsigned long *l, unsigned char *c, unsigned int len)
325 {
326 int i,j;
327 int array_size = len/sizeof(unsigned long);
328
329 PORT_Assert(len % sizeof(unsigned long) == 0);
330 for (i=0; i < array_size; i++) {
331 unsigned long tmp = l[i];
332 int byte_offset = i * sizeof(unsigned long);
333 for (j=0; j < sizeof(unsigned long); j++) {
334 c[byte_offset+j] = gcm_byte_rev[tmp & 0xff];
335 tmp = (tmp >> PR_BITS_PER_BYTE);
336 }
337 }
338 }
339
340
341 /* Initialize a gcmHashContext */
342 static SECStatus
343 gcmHash_InitContext(gcmHashContext *ghash, const unsigned char *H,
344 unsigned int blocksize)
345 {
346 PORT_Memset(ghash->X, 0, sizeof(ghash->X));
347 PORT_Memset(ghash->H, 0, sizeof(ghash->H));
348 gcm_bytes_to_longs(ghash->H, H, blocksize);
349
350 /* set the irreducible polynomial. Each blocksize has it's own polynommial
351 * for now only blocksizes 16 (=128 bits) and 8 (=64 bits) are defined */
352 switch (blocksize) {
353 case 16: /* 128 bits */
354 ghash->R = (unsigned long) 0x87; /* x^7 + x^2 + x +1 */
355 break;
356 case 8: /* 64 bits */
357 ghash->R = (unsigned long) 0x1b; /* x^4 + x^3 + x + 1 */
358 break;
359 default:
360 PORT_SetError(SEC_ERROR_INVALID_ARGS);
361 goto cleanup;
362 }
363 ghash->cLen = 0;
364 ghash->bufLen = 0;
365 ghash->m = 0;
366 PORT_Memset(ghash->counterBuf, 0, sizeof(ghash->counterBuf));
367 return SECSuccess;
368 cleanup:
369 return SECFailure;
370 }
371
372 /* Destroy a HashContext (Note we zero the digits so this function
373 * is idempotent if called with freeit == PR_FALSE */
374 static void
375 gcmHash_DestroyContext(gcmHashContext *ghash, PRBool freeit)
376 {
377 if (freeit) {
378 PORT_Free(ghash);
379 }
380 }
381
382 static unsigned long
383 gcm_shift_one(unsigned long *t, int count)
384 {
385 unsigned long carry = 0;
386 unsigned long nextcarry = 0;
387 int i;
388 for (i=0; i < count; i++) {
389 nextcarry = t[i] >> ((sizeof(unsigned long)*PR_BITS_PER_BYTE)-1);
390 t[i] = (t[i] << 1) | carry;
391 carry = nextcarry;
392 }
393 return carry;
394 }
395
396 static SECStatus
397 gcm_getX(gcmHashContext *ghash, unsigned char *T, unsigned int blocksize)
398 {
399 gcm_longs_to_bytes(ghash->X, T, blocksize);
400 return SECSuccess;
401 }
402
403 #define GCM_XOR(t, s, len) \
404 for (l=0; l < len; l++) t[l] ^= s[l]
405
406 static SECStatus
407 gcm_HashMult(gcmHashContext *ghash, const unsigned char *buf,
408 unsigned int count, unsigned int blocksize)
409 {
410 unsigned long C_i[GCM_ARRAY_SIZE];
411 int arraysize = blocksize/sizeof(unsigned long);
412 int i, j, k, l;
413
414 for (i=0; i < count; i++, buf += blocksize) {
415 ghash->m++;
416 gcm_bytes_to_longs(C_i, buf, blocksize);
417 GCM_XOR(C_i, ghash->X, arraysize);
418 /* multiply X = C_i * H */
419 PORT_Memset(ghash->X, 0, sizeof(ghash->X));
420 for (j=0; j < arraysize; j++) {
421 unsigned long H = ghash->H[j];
422 for (k=0; k < sizeof(unsigned long)*PR_BITS_PER_BYTE; k++) {
423 if (H & 1) {
424 GCM_XOR(ghash->X, C_i, arraysize);
425 }
426 if (gcm_shift_one(C_i, arraysize)) {
427 C_i[0] = C_i[0] ^ ghash->R;
428 }
429 H = H >> 1;
430 }
431 }
432 GCM_TRACE_X(ghash, "X%d = ")
433 }
434 return SECSuccess;
435 }
436
437
438 static void
439 gcm_zeroX(gcmHashContext *ghash)
440 {
441 PORT_Memset(ghash->X, 0, sizeof(ghash->X));
442 ghash->m = 0;
443 }
444 #endif
445
446 /*
447 * implement GCM GHASH using the freebl GHASH function. The gcm_HashMult
448 * function always takes blocksize lengths of data. gcmHash_Update will
449 * format the data properly.
450 */
451 static SECStatus
452 gcmHash_Update(gcmHashContext *ghash, const unsigned char *buf,
453 unsigned int len, unsigned int blocksize)
454 {
455 unsigned int blocks;
456 SECStatus rv;
457
458 ghash->cLen += (len*PR_BITS_PER_BYTE);
459
460 /* first deal with the current buffer of data. Try to fill it out so
461 * we can hash it */
462 if (ghash->bufLen) {
463 unsigned int needed = PR_MIN(len, blocksize - ghash->bufLen);
464 PORT_Memcpy(ghash->buffer+ghash->bufLen, buf, needed);
465 buf += needed;
466 len -= needed;
467 ghash->bufLen += needed;
468 if (ghash->bufLen != blocksize) {
wtc 2012/09/14 01:16:42 This test is equivalent to the original test (len
wtc 2012/09/18 01:03:40 I changed this back to if (len == 0).
469 /* didn't add enough to hash the data, nothing more do do */
470 PORT_Assert(len == 0);
471 return SECSuccess;
472 }
473 /* hash the buffer and clear it */
474 rv = gcm_HashMult(ghash, ghash->buffer, 1, blocksize);
475 PORT_Memset(ghash->buffer, 0, blocksize);
476 ghash->bufLen = 0;
477 if (rv != SECSuccess) {
478 return SECFailure;
479 }
480 }
481 /* now hash any full blocks remaining in the data stream */
482 blocks = len/blocksize;
483 if (blocks) {
484 rv = gcm_HashMult(ghash, buf, blocks, blocksize);
485 if (rv != SECSuccess) {
486 return SECFailure;
487 }
488 buf += blocks*blocksize;
489 len -= blocks*blocksize;
490 }
491
492 /* save any remainder in the buffer to be hashed with the next call */
493 if (len != 0) {
494 PORT_Memcpy(ghash->buffer, buf, len);
495 ghash->bufLen = len;
496 }
497 return SECSuccess;
498 }
499
500 /*
501 * write out any partial blocks zero padded through the GHASH engine,
502 * save the lengths for the final completion of the hash
503 */
504 static SECStatus
505 gcmHash_Sync(gcmHashContext *ghash, unsigned int blocksize)
506 {
507 int i;
508 SECStatus rv;
509
510 /* copy the previous counter to the upper block */
511 PORT_Memcpy(ghash->counterBuf, &ghash->counterBuf[GCM_HASH_LEN_LEN],
512 GCM_HASH_LEN_LEN);
513 /* copy the current counter in the lower block */
514 for (i=0; i < GCM_HASH_LEN_LEN; i++) {
515 ghash->counterBuf[GCM_HASH_LEN_LEN+i] =
516 (ghash->cLen >> ((GCM_HASH_LEN_LEN-1-i)*PR_BITS_PER_BYTE)) & 0xff;
517 }
518 ghash->cLen = 0;
519
520 /* now zero fill the buffer and hash the last block */
521 if (ghash->bufLen) {
522 PORT_Memset(ghash->buffer+ghash->bufLen, 0, blocksize - ghash->bufLen);
523 rv = gcm_HashMult(ghash, ghash->buffer, 1, blocksize);
524 PORT_Memset(ghash->buffer, 0, blocksize);
525 ghash->bufLen = 0;
526 if (rv != SECSuccess) {
527 return SECFailure;
528 }
529 }
530 return SECSuccess;
531 }
532
533 /*
534 * This does the final sync, hashes the lengths, then returns
535 * "T", the hashed output.
536 */
537 static SECStatus
538 gcmHash_Final(gcmHashContext *ghash, unsigned char *outbuf,
539 unsigned int *outlen, unsigned int maxout,
540 unsigned int blocksize)
541 {
542 unsigned char T[MAX_BLOCK_SIZE];
543 SECStatus rv;
544
545 rv = gcmHash_Sync(ghash, blocksize);
546 if (rv != SECSuccess) {
547 return SECFailure;
548 }
549
550 rv = gcm_HashMult(ghash, ghash->counterBuf, (GCM_HASH_LEN_LEN*2)/blocksize,
551 blocksize);
552 if (rv != SECSuccess) {
553 return SECFailure;
554 }
555
556 GCM_TRACE_X(ghash, "GHASH(H,A,C) = ")
557
558 rv = gcm_getX(ghash, T, blocksize);
559 if (rv != SECSuccess) {
560 return SECFailure;
561 }
562
563 if (maxout > blocksize) maxout = blocksize;
564 PORT_Memcpy(outbuf, T, maxout);
565 *outlen = maxout;
566 return SECSuccess;
567 }
568
569 SECStatus
570 gcmHash_Reset(gcmHashContext *ghash, const unsigned char *AAD,
571 unsigned int AADLen, unsigned int blocksize)
572 {
573 SECStatus rv;
574
575 ghash->cLen = 0;
576 PORT_Memset(ghash->counterBuf, 0, GCM_HASH_LEN_LEN*2);
577 ghash->bufLen = 0;
578 gcm_zeroX(ghash);
579
580 /* now kick things off by hashing the Additional Authenticated Data */
581 if (AADLen != 0) {
582 rv = gcmHash_Update(ghash, AAD, AADLen, blocksize);
583 if (rv != SECSuccess) {
584 return SECFailure;
585 }
586 rv = gcmHash_Sync(ghash, blocksize);
587 if (rv != SECSuccess) {
588 return SECFailure;
589 }
590 }
591 return SECSuccess;
592 }
593
594 /**************************************************************************
595 * Now implement the GCM using gcmHash and CTR *
596 **************************************************************************/
597
598 /* state to handle the full GCM operation (hash and counter) */
599 struct GCMContextStr {
600 gcmHashContext ghash_context;
601 CTRContext ctr_context;
602 unsigned long tagBits;
603 unsigned char tagKey[MAX_BLOCK_SIZE];
604 };
605
606 GCMContext *
607 GCM_CreateContext(void *context, freeblCipherFunc cipher,
608 const unsigned char *params, unsigned int blocksize)
609 {
610 GCMContext *gcm = NULL;
611 gcmHashContext *ghash;
612 unsigned char H[MAX_BLOCK_SIZE];
613 unsigned int tmp;
614 PRBool freeCtr = PR_FALSE;
615 PRBool freeHash = PR_FALSE;
616 const CK_AES_GCM_PARAMS *gcmParams = (const CK_AES_GCM_PARAMS *)params;
617 CK_AES_CTR_PARAMS ctrParams;
618 SECStatus rv;
619
620 if (blocksize > MAX_BLOCK_SIZE || blocksize > sizeof(ctrParams.cb)) {
621 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
622 return NULL;
623 }
624 gcm = PORT_ZNew(GCMContext);
625 if (gcm == NULL) {
626 return NULL;
627 }
628 /* first fill in the ghash context */
629 ghash = &gcm->ghash_context;
630 PORT_Memset(H, 0, blocksize);
631 rv = (*cipher)(context, H, &tmp, blocksize, H, blocksize, blocksize);
632 if (rv != SECSuccess) {
633 goto loser;
634 }
635 rv = gcmHash_InitContext(ghash, H, blocksize);
636 if (rv != SECSuccess) {
637 goto loser;
638 }
639 freeHash = PR_TRUE;
640
641 /* fill in the Counter context */
642 ctrParams.ulCounterBits = 32;
643 PORT_Memset(ctrParams.cb, 0, sizeof(ctrParams.cb));
644 if ((blocksize == 8) && (gcmParams->ulIvLen == 4)) {
645 PORT_Memcpy(&ctrParams.cb[4], gcmParams->pIv, gcmParams->ulIvLen);
646 } else if ((blocksize == 16) && (gcmParams->ulIvLen == 12)) {
647 PORT_Memcpy(ctrParams.cb, gcmParams->pIv, gcmParams->ulIvLen);
648 ctrParams.cb[blocksize-1] = 1;
649 } else {
650 rv = gcmHash_Update(ghash, gcmParams->pIv, gcmParams->ulIvLen,
651 blocksize);
652 if (rv != SECSuccess) {
653 goto loser;
654 }
655 rv = gcmHash_Final(ghash, ctrParams.cb, &tmp, blocksize, blocksize);
656 if (rv != SECSuccess) {
657 goto loser;
658 }
659 }
660 rv = CTR_InitContext(&gcm->ctr_context, context, cipher,
661 (unsigned char *)&ctrParams, blocksize);
662 if (rv != SECSuccess) {
663 goto loser;
664 }
665 freeCtr = PR_TRUE;
666
667 /* fill in the gcm structure */
668 gcm->tagBits = gcmParams->ulTagBits; /* save for final step */
669 /* calculate the final tag key. NOTE: gcm->tagKey is zero to start with.
670 * if this assumption changes, we would need to explicitly clear it here */
671 rv = CTR_Update(&gcm->ctr_context, gcm->tagKey, &tmp, blocksize,
672 gcm->tagKey, blocksize, blocksize);
673 if (rv != SECSuccess) {
674 goto loser;
675 }
676
677 /* finally mix in the AAD data */
678 rv = gcmHash_Reset(ghash, gcmParams->pAAD, gcmParams->ulAADLen, blocksize);
679 if (rv != SECSuccess) {
680 goto loser;
681 }
682
683 return gcm;
684
685 loser:
686 if (freeCtr) {
687 CTR_DestroyContext(&gcm->ctr_context, PR_FALSE);
688 }
689 if (freeHash) {
690 gcmHash_DestroyContext(&gcm->ghash_context, PR_FALSE);
691 }
692 if (gcm) {
693 PORT_Free(gcm);
694 }
695 return NULL;
696 }
697
698 void
699 GCM_DestroyContext(GCMContext *gcm, PRBool freeit)
700 {
701 /* these two are statically allocated and will be freed when we free
702 * gcm. call their destroy functions to free up any locally
703 * allocated data (like mp_int's) */
704 CTR_DestroyContext(&gcm->ctr_context, PR_FALSE);
705 gcmHash_DestroyContext(&gcm->ghash_context, PR_FALSE);
706 if (freeit) {
707 PORT_Free(gcm);
708 }
709 }
710
711 static SECStatus
712 gcm_GetTag(GCMContext *gcm, unsigned char *outbuf,
713 unsigned int *outlen, unsigned int maxout,
714 unsigned int blocksize)
715 {
716 unsigned int tagBytes;
717 unsigned int extra;
718 unsigned int i;
719 SECStatus rv;
720
721 tagBytes = (gcm->tagBits + (PR_BITS_PER_BYTE-1)) / PR_BITS_PER_BYTE;
722 extra = tagBytes*PR_BITS_PER_BYTE - gcm->tagBits;
723
724 if (outbuf == NULL) {
725 *outlen = tagBytes;
726 PORT_SetError(SEC_ERROR_OUTPUT_LEN);
727 return SECFailure;
728 }
729
730 if (maxout < tagBytes) {
731 *outlen = tagBytes;
732 PORT_SetError(SEC_ERROR_OUTPUT_LEN);
733 return SECFailure;
734 }
735 maxout = tagBytes;
736 rv = gcmHash_Final(&gcm->ghash_context, outbuf, outlen, maxout, blocksize);
737 if (rv != SECSuccess) {
738 return SECFailure;
739 }
740
741 GCM_TRACE_BLOCK("GHASH=", outbuf, blocksize);
742 GCM_TRACE_BLOCK("Y0=", gcm->tagKey, blocksize);
743 for (i=0; i < *outlen; i++) {
744 outbuf[i] ^= gcm->tagKey[i];
745 }
746 GCM_TRACE_BLOCK("Y0=", gcm->tagKey, blocksize);
747 GCM_TRACE_BLOCK("T=", outbuf, blocksize);
748 /* mask off any extra bits we got */
749 if (extra) {
750 outbuf[tagBytes-1] &= ~((1 << extra)-1);
751 }
752 return SECSuccess;
753 }
754
755
756 /*
757 * See The Galois/Counter Mode of Operation, McGrew and Viega.
758 * GCM is basically counter mode with a specific initialization and
759 * built in macing operation.
760 */
761 SECStatus
762 GCM_EncryptUpdate(GCMContext *gcm, unsigned char *outbuf,
763 unsigned int *outlen, unsigned int maxout,
764 const unsigned char *inbuf, unsigned int inlen,
765 unsigned int blocksize)
766 {
767 SECStatus rv;
768 unsigned int tagBytes;
769 unsigned int len;
770
771 tagBytes = (gcm->tagBits + (PR_BITS_PER_BYTE-1)) / PR_BITS_PER_BYTE;
772 if (UINT_MAX - inlen < tagBytes) {
773 PORT_SetError(SEC_ERROR_INPUT_LEN);
774 return SECFailure;
775 }
776 if (maxout < inlen + tagBytes) {
777 *outlen = inlen + tagBytes;
778 PORT_SetError(SEC_ERROR_OUTPUT_LEN);
779 return SECFailure;
780 }
781
782 rv = CTR_Update(&gcm->ctr_context, outbuf, outlen, maxout,
783 inbuf, inlen, blocksize);
784 if (rv != SECSuccess) {
785 return SECFailure;
786 }
787 rv = gcmHash_Update(&gcm->ghash_context, outbuf, *outlen, blocksize);
788 if (rv != SECSuccess) {
789 PORT_Memset(outbuf, 0, *outlen); /* clear the output buffer */
790 *outlen = 0;
791 return SECFailure;
792 }
793 rv = gcm_GetTag(gcm, outbuf + *outlen, &len, maxout - *outlen, blocksize);
794 if (rv != SECSuccess) {
795 PORT_Memset(outbuf, 0, *outlen); /* clear the output buffer */
796 *outlen = 0;
797 return SECFailure;
798 };
799 *outlen += len;
800 return SECSuccess;
801 }
802
803 /*
804 * See The Galois/Counter Mode of Operation, McGrew and Viega.
805 * GCM is basically counter mode with a specific initialization and
806 * built in macing operation. NOTE: the only difference between Encrypt
807 * and Decrypt is when we calculate the mac. That is because the mac must
808 * always be calculated on the cipher text, not the plain text, so for
809 * encrypt, we do the CTR update first and for decrypt we do the mac first.
810 */
811 SECStatus
812 GCM_DecryptUpdate(GCMContext *gcm, unsigned char *outbuf,
813 unsigned int *outlen, unsigned int maxout,
814 const unsigned char *inbuf, unsigned int inlen,
815 unsigned int blocksize)
816 {
817 SECStatus rv;
818 unsigned int tagBytes;
819 unsigned char tag[MAX_BLOCK_SIZE];
820 const unsigned char *intag;
821 unsigned int len;
822
823 tagBytes = (gcm->tagBits + (PR_BITS_PER_BYTE-1)) / PR_BITS_PER_BYTE;
824
825 /* get the authentication block */
826 if (inlen < tagBytes) {
827 PORT_SetError(SEC_ERROR_INVALID_ARGS);
828 return SECFailure;
829 }
830
831 inlen -= tagBytes;
832 intag = inbuf + inlen;
833
834 /* verify the block */
835 rv = gcmHash_Update(&gcm->ghash_context, inbuf, inlen, blocksize);
836 if (rv != SECSuccess) {
837 return SECFailure;
838 }
839 rv = gcm_GetTag(gcm, tag, &len, blocksize, blocksize);
840 if (rv != SECSuccess) {
841 return SECFailure;
842 }
843 /* Don't decrypt if we can't authenticate the encrypted data!
844 * This assumes that if tagBits is not a multiple of 8, intag will
845 * preserve the masked off missing bits. */
846 if (NSS_SecureMemcmp(tag, intag, tagBytes) != 0) {
847 /* force a CKR_ENCRYPTED_DATA_INVALID error at in softoken */
848 PORT_SetError(SEC_ERROR_BAD_DATA);
849 return SECFailure;
850 }
851 /* finish the decryption */
852 return CTR_Update(&gcm->ctr_context, outbuf, outlen, maxout,
853 inbuf, inlen, blocksize);
854 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698