OLD | NEW |
(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 const unsigned int *poly; |
| 127 unsigned char buffer[MAX_BLOCK_SIZE]; |
| 128 unsigned int bufLen; |
| 129 int m; /* XXX what is m? */ |
| 130 unsigned char counterBuf[2*GCM_HASH_LEN_LEN]; |
| 131 PRUint64 cLen; |
| 132 }; |
| 133 |
| 134 /* f = x^128 + x^7 + x^2 + x + 1 */ |
| 135 static const unsigned int poly_128[] = { 128, 7, 2, 1, 0 }; |
| 136 /* f = x^64 + x^4 + x^3 + x + 1 */ |
| 137 static const 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((unsigned int)len <= blocksize); |
| 223 if ((unsigned int)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 char buffer[MAX_BLOCK_SIZE]; |
| 300 unsigned int bufLen; |
| 301 int m; |
| 302 unsigned char counterBuf[2*GCM_HASH_LEN_LEN]; |
| 303 PRUint64 cLen; |
| 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, unsigned int count) |
| 384 { |
| 385 unsigned long carry = 0; |
| 386 unsigned long nextcarry = 0; |
| 387 unsigned 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 unsigned int arraysize = blocksize/sizeof(unsigned long); |
| 412 unsigned 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 (len == 0) { |
| 469 /* didn't add enough to hash the data, nothing more do do */ |
| 470 return SECSuccess; |
| 471 } |
| 472 PORT_Assert(ghash->bufLen == blocksize); |
| 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 ctrParams.cb[3] = 1; |
| 646 PORT_Memcpy(&ctrParams.cb[4], gcmParams->pIv, gcmParams->ulIvLen); |
| 647 } else if ((blocksize == 16) && (gcmParams->ulIvLen == 12)) { |
| 648 PORT_Memcpy(ctrParams.cb, gcmParams->pIv, gcmParams->ulIvLen); |
| 649 ctrParams.cb[blocksize-1] = 1; |
| 650 } else { |
| 651 rv = gcmHash_Update(ghash, gcmParams->pIv, gcmParams->ulIvLen, |
| 652 blocksize); |
| 653 if (rv != SECSuccess) { |
| 654 goto loser; |
| 655 } |
| 656 rv = gcmHash_Final(ghash, ctrParams.cb, &tmp, blocksize, blocksize); |
| 657 if (rv != SECSuccess) { |
| 658 goto loser; |
| 659 } |
| 660 } |
| 661 rv = CTR_InitContext(&gcm->ctr_context, context, cipher, |
| 662 (unsigned char *)&ctrParams, blocksize); |
| 663 if (rv != SECSuccess) { |
| 664 goto loser; |
| 665 } |
| 666 freeCtr = PR_TRUE; |
| 667 |
| 668 /* fill in the gcm structure */ |
| 669 gcm->tagBits = gcmParams->ulTagBits; /* save for final step */ |
| 670 /* calculate the final tag key. NOTE: gcm->tagKey is zero to start with. |
| 671 * if this assumption changes, we would need to explicitly clear it here */ |
| 672 rv = CTR_Update(&gcm->ctr_context, gcm->tagKey, &tmp, blocksize, |
| 673 gcm->tagKey, blocksize, blocksize); |
| 674 if (rv != SECSuccess) { |
| 675 goto loser; |
| 676 } |
| 677 |
| 678 /* finally mix in the AAD data */ |
| 679 rv = gcmHash_Reset(ghash, gcmParams->pAAD, gcmParams->ulAADLen, blocksize); |
| 680 if (rv != SECSuccess) { |
| 681 goto loser; |
| 682 } |
| 683 |
| 684 return gcm; |
| 685 |
| 686 loser: |
| 687 if (freeCtr) { |
| 688 CTR_DestroyContext(&gcm->ctr_context, PR_FALSE); |
| 689 } |
| 690 if (freeHash) { |
| 691 gcmHash_DestroyContext(&gcm->ghash_context, PR_FALSE); |
| 692 } |
| 693 if (gcm) { |
| 694 PORT_Free(gcm); |
| 695 } |
| 696 return NULL; |
| 697 } |
| 698 |
| 699 void |
| 700 GCM_DestroyContext(GCMContext *gcm, PRBool freeit) |
| 701 { |
| 702 /* these two are statically allocated and will be freed when we free |
| 703 * gcm. call their destroy functions to free up any locally |
| 704 * allocated data (like mp_int's) */ |
| 705 CTR_DestroyContext(&gcm->ctr_context, PR_FALSE); |
| 706 gcmHash_DestroyContext(&gcm->ghash_context, PR_FALSE); |
| 707 if (freeit) { |
| 708 PORT_Free(gcm); |
| 709 } |
| 710 } |
| 711 |
| 712 static SECStatus |
| 713 gcm_GetTag(GCMContext *gcm, unsigned char *outbuf, |
| 714 unsigned int *outlen, unsigned int maxout, |
| 715 unsigned int blocksize) |
| 716 { |
| 717 unsigned int tagBytes; |
| 718 unsigned int extra; |
| 719 unsigned int i; |
| 720 SECStatus rv; |
| 721 |
| 722 tagBytes = (gcm->tagBits + (PR_BITS_PER_BYTE-1)) / PR_BITS_PER_BYTE; |
| 723 extra = tagBytes*PR_BITS_PER_BYTE - gcm->tagBits; |
| 724 |
| 725 if (outbuf == NULL) { |
| 726 *outlen = tagBytes; |
| 727 PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
| 728 return SECFailure; |
| 729 } |
| 730 |
| 731 if (maxout < tagBytes) { |
| 732 *outlen = tagBytes; |
| 733 PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
| 734 return SECFailure; |
| 735 } |
| 736 maxout = tagBytes; |
| 737 rv = gcmHash_Final(&gcm->ghash_context, outbuf, outlen, maxout, blocksize); |
| 738 if (rv != SECSuccess) { |
| 739 return SECFailure; |
| 740 } |
| 741 |
| 742 GCM_TRACE_BLOCK("GHASH=", outbuf, blocksize); |
| 743 GCM_TRACE_BLOCK("Y0=", gcm->tagKey, blocksize); |
| 744 for (i=0; i < *outlen; i++) { |
| 745 outbuf[i] ^= gcm->tagKey[i]; |
| 746 } |
| 747 GCM_TRACE_BLOCK("Y0=", gcm->tagKey, blocksize); |
| 748 GCM_TRACE_BLOCK("T=", outbuf, blocksize); |
| 749 /* mask off any extra bits we got */ |
| 750 if (extra) { |
| 751 outbuf[tagBytes-1] &= ~((1 << extra)-1); |
| 752 } |
| 753 return SECSuccess; |
| 754 } |
| 755 |
| 756 |
| 757 /* |
| 758 * See The Galois/Counter Mode of Operation, McGrew and Viega. |
| 759 * GCM is basically counter mode with a specific initialization and |
| 760 * built in macing operation. |
| 761 */ |
| 762 SECStatus |
| 763 GCM_EncryptUpdate(GCMContext *gcm, unsigned char *outbuf, |
| 764 unsigned int *outlen, unsigned int maxout, |
| 765 const unsigned char *inbuf, unsigned int inlen, |
| 766 unsigned int blocksize) |
| 767 { |
| 768 SECStatus rv; |
| 769 unsigned int tagBytes; |
| 770 unsigned int len; |
| 771 |
| 772 tagBytes = (gcm->tagBits + (PR_BITS_PER_BYTE-1)) / PR_BITS_PER_BYTE; |
| 773 if (UINT_MAX - inlen < tagBytes) { |
| 774 PORT_SetError(SEC_ERROR_INPUT_LEN); |
| 775 return SECFailure; |
| 776 } |
| 777 if (maxout < inlen + tagBytes) { |
| 778 *outlen = inlen + tagBytes; |
| 779 PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
| 780 return SECFailure; |
| 781 } |
| 782 |
| 783 rv = CTR_Update(&gcm->ctr_context, outbuf, outlen, maxout, |
| 784 inbuf, inlen, blocksize); |
| 785 if (rv != SECSuccess) { |
| 786 return SECFailure; |
| 787 } |
| 788 rv = gcmHash_Update(&gcm->ghash_context, outbuf, *outlen, blocksize); |
| 789 if (rv != SECSuccess) { |
| 790 PORT_Memset(outbuf, 0, *outlen); /* clear the output buffer */ |
| 791 *outlen = 0; |
| 792 return SECFailure; |
| 793 } |
| 794 rv = gcm_GetTag(gcm, outbuf + *outlen, &len, maxout - *outlen, blocksize); |
| 795 if (rv != SECSuccess) { |
| 796 PORT_Memset(outbuf, 0, *outlen); /* clear the output buffer */ |
| 797 *outlen = 0; |
| 798 return SECFailure; |
| 799 }; |
| 800 *outlen += len; |
| 801 return SECSuccess; |
| 802 } |
| 803 |
| 804 /* |
| 805 * See The Galois/Counter Mode of Operation, McGrew and Viega. |
| 806 * GCM is basically counter mode with a specific initialization and |
| 807 * built in macing operation. NOTE: the only difference between Encrypt |
| 808 * and Decrypt is when we calculate the mac. That is because the mac must |
| 809 * always be calculated on the cipher text, not the plain text, so for |
| 810 * encrypt, we do the CTR update first and for decrypt we do the mac first. |
| 811 */ |
| 812 SECStatus |
| 813 GCM_DecryptUpdate(GCMContext *gcm, unsigned char *outbuf, |
| 814 unsigned int *outlen, unsigned int maxout, |
| 815 const unsigned char *inbuf, unsigned int inlen, |
| 816 unsigned int blocksize) |
| 817 { |
| 818 SECStatus rv; |
| 819 unsigned int tagBytes; |
| 820 unsigned char tag[MAX_BLOCK_SIZE]; |
| 821 const unsigned char *intag; |
| 822 unsigned int len; |
| 823 |
| 824 tagBytes = (gcm->tagBits + (PR_BITS_PER_BYTE-1)) / PR_BITS_PER_BYTE; |
| 825 |
| 826 /* get the authentication block */ |
| 827 if (inlen < tagBytes) { |
| 828 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
| 829 return SECFailure; |
| 830 } |
| 831 |
| 832 inlen -= tagBytes; |
| 833 intag = inbuf + inlen; |
| 834 |
| 835 /* verify the block */ |
| 836 rv = gcmHash_Update(&gcm->ghash_context, inbuf, inlen, blocksize); |
| 837 if (rv != SECSuccess) { |
| 838 return SECFailure; |
| 839 } |
| 840 rv = gcm_GetTag(gcm, tag, &len, blocksize, blocksize); |
| 841 if (rv != SECSuccess) { |
| 842 return SECFailure; |
| 843 } |
| 844 /* Don't decrypt if we can't authenticate the encrypted data! |
| 845 * This assumes that if tagBits is not a multiple of 8, intag will |
| 846 * preserve the masked off missing bits. */ |
| 847 if (NSS_SecureMemcmp(tag, intag, tagBytes) != 0) { |
| 848 /* force a CKR_ENCRYPTED_DATA_INVALID error at in softoken */ |
| 849 PORT_SetError(SEC_ERROR_BAD_DATA); |
| 850 return SECFailure; |
| 851 } |
| 852 /* finish the decryption */ |
| 853 return CTR_Update(&gcm->ctr_context, outbuf, outlen, maxout, |
| 854 inbuf, inlen, blocksize); |
| 855 } |
OLD | NEW |