security/nss/lib/freebl/gcm.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial