security/nss/lib/freebl/dh.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 /*
michael@0 6 * Diffie-Hellman parameter generation, key generation, and secret derivation.
michael@0 7 * KEA secret generation and verification.
michael@0 8 */
michael@0 9 #ifdef FREEBL_NO_DEPEND
michael@0 10 #include "stubs.h"
michael@0 11 #endif
michael@0 12
michael@0 13 #include "prerr.h"
michael@0 14 #include "secerr.h"
michael@0 15
michael@0 16 #include "blapi.h"
michael@0 17 #include "secitem.h"
michael@0 18 #include "mpi.h"
michael@0 19 #include "mpprime.h"
michael@0 20 #include "secmpi.h"
michael@0 21
michael@0 22 #define KEA_DERIVED_SECRET_LEN 128
michael@0 23
michael@0 24 /* Lengths are in bytes. */
michael@0 25 static unsigned int
michael@0 26 dh_GetSecretKeyLen(unsigned int primeLen)
michael@0 27 {
michael@0 28 /* Based on Table 2 in NIST SP 800-57. */
michael@0 29 if (primeLen >= 1920) { /* 15360 bits */
michael@0 30 return 64; /* 512 bits */
michael@0 31 }
michael@0 32 if (primeLen >= 960) { /* 7680 bits */
michael@0 33 return 48; /* 384 bits */
michael@0 34 }
michael@0 35 if (primeLen >= 384) { /* 3072 bits */
michael@0 36 return 32; /* 256 bits */
michael@0 37 }
michael@0 38 if (primeLen >= 256) { /* 2048 bits */
michael@0 39 return 28; /* 224 bits */
michael@0 40 }
michael@0 41 return 20; /* 160 bits */
michael@0 42 }
michael@0 43
michael@0 44 SECStatus
michael@0 45 DH_GenParam(int primeLen, DHParams **params)
michael@0 46 {
michael@0 47 PLArenaPool *arena;
michael@0 48 DHParams *dhparams;
michael@0 49 unsigned char *pb = NULL;
michael@0 50 unsigned char *ab = NULL;
michael@0 51 unsigned long counter = 0;
michael@0 52 mp_int p, q, a, h, psub1, test;
michael@0 53 mp_err err = MP_OKAY;
michael@0 54 SECStatus rv = SECSuccess;
michael@0 55 if (!params || primeLen < 0) {
michael@0 56 PORT_SetError(SEC_ERROR_INVALID_ARGS);
michael@0 57 return SECFailure;
michael@0 58 }
michael@0 59 arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE);
michael@0 60 if (!arena) {
michael@0 61 PORT_SetError(SEC_ERROR_NO_MEMORY);
michael@0 62 return SECFailure;
michael@0 63 }
michael@0 64 dhparams = (DHParams *)PORT_ArenaZAlloc(arena, sizeof(DHParams));
michael@0 65 if (!dhparams) {
michael@0 66 PORT_SetError(SEC_ERROR_NO_MEMORY);
michael@0 67 PORT_FreeArena(arena, PR_TRUE);
michael@0 68 return SECFailure;
michael@0 69 }
michael@0 70 dhparams->arena = arena;
michael@0 71 MP_DIGITS(&p) = 0;
michael@0 72 MP_DIGITS(&q) = 0;
michael@0 73 MP_DIGITS(&a) = 0;
michael@0 74 MP_DIGITS(&h) = 0;
michael@0 75 MP_DIGITS(&psub1) = 0;
michael@0 76 MP_DIGITS(&test) = 0;
michael@0 77 CHECK_MPI_OK( mp_init(&p) );
michael@0 78 CHECK_MPI_OK( mp_init(&q) );
michael@0 79 CHECK_MPI_OK( mp_init(&a) );
michael@0 80 CHECK_MPI_OK( mp_init(&h) );
michael@0 81 CHECK_MPI_OK( mp_init(&psub1) );
michael@0 82 CHECK_MPI_OK( mp_init(&test) );
michael@0 83 /* generate prime with MPI, uses Miller-Rabin to generate strong prime. */
michael@0 84 pb = PORT_Alloc(primeLen);
michael@0 85 CHECK_SEC_OK( RNG_GenerateGlobalRandomBytes(pb, primeLen) );
michael@0 86 pb[0] |= 0x80; /* set high-order bit */
michael@0 87 pb[primeLen-1] |= 0x01; /* set low-order bit */
michael@0 88 CHECK_MPI_OK( mp_read_unsigned_octets(&p, pb, primeLen) );
michael@0 89 CHECK_MPI_OK( mpp_make_prime(&p, primeLen * 8, PR_TRUE, &counter) );
michael@0 90 /* construct Sophie-Germain prime q = (p-1)/2. */
michael@0 91 CHECK_MPI_OK( mp_sub_d(&p, 1, &psub1) );
michael@0 92 CHECK_MPI_OK( mp_div_2(&psub1, &q) );
michael@0 93 /* construct a generator from the prime. */
michael@0 94 ab = PORT_Alloc(primeLen);
michael@0 95 /* generate a candidate number a in p's field */
michael@0 96 CHECK_SEC_OK( RNG_GenerateGlobalRandomBytes(ab, primeLen) );
michael@0 97 CHECK_MPI_OK( mp_read_unsigned_octets(&a, ab, primeLen) );
michael@0 98 /* force a < p (note that quot(a/p) <= 1) */
michael@0 99 if ( mp_cmp(&a, &p) > 0 )
michael@0 100 CHECK_MPI_OK( mp_sub(&a, &p, &a) );
michael@0 101 do {
michael@0 102 /* check that a is in the range [2..p-1] */
michael@0 103 if ( mp_cmp_d(&a, 2) < 0 || mp_cmp(&a, &psub1) >= 0) {
michael@0 104 /* a is outside of the allowed range. Set a=3 and keep going. */
michael@0 105 mp_set(&a, 3);
michael@0 106 }
michael@0 107 /* if a**q mod p != 1 then a is a generator */
michael@0 108 CHECK_MPI_OK( mp_exptmod(&a, &q, &p, &test) );
michael@0 109 if ( mp_cmp_d(&test, 1) != 0 )
michael@0 110 break;
michael@0 111 /* increment the candidate and try again. */
michael@0 112 CHECK_MPI_OK( mp_add_d(&a, 1, &a) );
michael@0 113 } while (PR_TRUE);
michael@0 114 MPINT_TO_SECITEM(&p, &dhparams->prime, arena);
michael@0 115 MPINT_TO_SECITEM(&a, &dhparams->base, arena);
michael@0 116 *params = dhparams;
michael@0 117 cleanup:
michael@0 118 mp_clear(&p);
michael@0 119 mp_clear(&q);
michael@0 120 mp_clear(&a);
michael@0 121 mp_clear(&h);
michael@0 122 mp_clear(&psub1);
michael@0 123 mp_clear(&test);
michael@0 124 if (pb) PORT_ZFree(pb, primeLen);
michael@0 125 if (ab) PORT_ZFree(ab, primeLen);
michael@0 126 if (err) {
michael@0 127 MP_TO_SEC_ERROR(err);
michael@0 128 rv = SECFailure;
michael@0 129 }
michael@0 130 if (rv)
michael@0 131 PORT_FreeArena(arena, PR_TRUE);
michael@0 132 return rv;
michael@0 133 }
michael@0 134
michael@0 135 SECStatus
michael@0 136 DH_NewKey(DHParams *params, DHPrivateKey **privKey)
michael@0 137 {
michael@0 138 PLArenaPool *arena;
michael@0 139 DHPrivateKey *key;
michael@0 140 mp_int g, xa, p, Ya;
michael@0 141 mp_err err = MP_OKAY;
michael@0 142 SECStatus rv = SECSuccess;
michael@0 143 if (!params || !privKey) {
michael@0 144 PORT_SetError(SEC_ERROR_INVALID_ARGS);
michael@0 145 return SECFailure;
michael@0 146 }
michael@0 147 arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE);
michael@0 148 if (!arena) {
michael@0 149 PORT_SetError(SEC_ERROR_NO_MEMORY);
michael@0 150 return SECFailure;
michael@0 151 }
michael@0 152 key = (DHPrivateKey *)PORT_ArenaZAlloc(arena, sizeof(DHPrivateKey));
michael@0 153 if (!key) {
michael@0 154 PORT_SetError(SEC_ERROR_NO_MEMORY);
michael@0 155 PORT_FreeArena(arena, PR_TRUE);
michael@0 156 return SECFailure;
michael@0 157 }
michael@0 158 key->arena = arena;
michael@0 159 MP_DIGITS(&g) = 0;
michael@0 160 MP_DIGITS(&xa) = 0;
michael@0 161 MP_DIGITS(&p) = 0;
michael@0 162 MP_DIGITS(&Ya) = 0;
michael@0 163 CHECK_MPI_OK( mp_init(&g) );
michael@0 164 CHECK_MPI_OK( mp_init(&xa) );
michael@0 165 CHECK_MPI_OK( mp_init(&p) );
michael@0 166 CHECK_MPI_OK( mp_init(&Ya) );
michael@0 167 /* Set private key's p */
michael@0 168 CHECK_SEC_OK( SECITEM_CopyItem(arena, &key->prime, &params->prime) );
michael@0 169 SECITEM_TO_MPINT(key->prime, &p);
michael@0 170 /* Set private key's g */
michael@0 171 CHECK_SEC_OK( SECITEM_CopyItem(arena, &key->base, &params->base) );
michael@0 172 SECITEM_TO_MPINT(key->base, &g);
michael@0 173 /* Generate private key xa */
michael@0 174 SECITEM_AllocItem(arena, &key->privateValue,
michael@0 175 dh_GetSecretKeyLen(params->prime.len));
michael@0 176 RNG_GenerateGlobalRandomBytes(key->privateValue.data,
michael@0 177 key->privateValue.len);
michael@0 178 SECITEM_TO_MPINT( key->privateValue, &xa );
michael@0 179 /* xa < p */
michael@0 180 CHECK_MPI_OK( mp_mod(&xa, &p, &xa) );
michael@0 181 /* Compute public key Ya = g ** xa mod p */
michael@0 182 CHECK_MPI_OK( mp_exptmod(&g, &xa, &p, &Ya) );
michael@0 183 MPINT_TO_SECITEM(&Ya, &key->publicValue, key->arena);
michael@0 184 *privKey = key;
michael@0 185 cleanup:
michael@0 186 mp_clear(&g);
michael@0 187 mp_clear(&xa);
michael@0 188 mp_clear(&p);
michael@0 189 mp_clear(&Ya);
michael@0 190 if (err) {
michael@0 191 MP_TO_SEC_ERROR(err);
michael@0 192 rv = SECFailure;
michael@0 193 }
michael@0 194 if (rv)
michael@0 195 PORT_FreeArena(arena, PR_TRUE);
michael@0 196 return rv;
michael@0 197 }
michael@0 198
michael@0 199 SECStatus
michael@0 200 DH_Derive(SECItem *publicValue,
michael@0 201 SECItem *prime,
michael@0 202 SECItem *privateValue,
michael@0 203 SECItem *derivedSecret,
michael@0 204 unsigned int outBytes)
michael@0 205 {
michael@0 206 mp_int p, Xa, Yb, ZZ, psub1;
michael@0 207 mp_err err = MP_OKAY;
michael@0 208 int len = 0;
michael@0 209 unsigned int nb;
michael@0 210 unsigned char *secret = NULL;
michael@0 211 if (!publicValue || !prime || !privateValue || !derivedSecret) {
michael@0 212 PORT_SetError(SEC_ERROR_INVALID_ARGS);
michael@0 213 return SECFailure;
michael@0 214 }
michael@0 215 memset(derivedSecret, 0, sizeof *derivedSecret);
michael@0 216 MP_DIGITS(&p) = 0;
michael@0 217 MP_DIGITS(&Xa) = 0;
michael@0 218 MP_DIGITS(&Yb) = 0;
michael@0 219 MP_DIGITS(&ZZ) = 0;
michael@0 220 MP_DIGITS(&psub1) = 0;
michael@0 221 CHECK_MPI_OK( mp_init(&p) );
michael@0 222 CHECK_MPI_OK( mp_init(&Xa) );
michael@0 223 CHECK_MPI_OK( mp_init(&Yb) );
michael@0 224 CHECK_MPI_OK( mp_init(&ZZ) );
michael@0 225 CHECK_MPI_OK( mp_init(&psub1) );
michael@0 226 SECITEM_TO_MPINT(*publicValue, &Yb);
michael@0 227 SECITEM_TO_MPINT(*privateValue, &Xa);
michael@0 228 SECITEM_TO_MPINT(*prime, &p);
michael@0 229 CHECK_MPI_OK( mp_sub_d(&p, 1, &psub1) );
michael@0 230
michael@0 231 /* We assume that the modulus, p, is a safe prime. That is, p = 2q+1 where
michael@0 232 * q is also a prime. Thus the orders of the subgroups are factors of 2q:
michael@0 233 * namely 1, 2, q and 2q.
michael@0 234 *
michael@0 235 * We check that the peer's public value isn't zero (which isn't in the
michael@0 236 * group), one (subgroup of order one) or p-1 (subgroup of order 2). We
michael@0 237 * also check that the public value is less than p, to avoid being fooled
michael@0 238 * by values like p+1 or 2*p-1.
michael@0 239 *
michael@0 240 * Thus we must be operating in the subgroup of size q or 2q. */
michael@0 241 if (mp_cmp_d(&Yb, 1) <= 0 ||
michael@0 242 mp_cmp(&Yb, &psub1) >= 0) {
michael@0 243 err = MP_BADARG;
michael@0 244 goto cleanup;
michael@0 245 }
michael@0 246
michael@0 247 /* ZZ = (Yb)**Xa mod p */
michael@0 248 CHECK_MPI_OK( mp_exptmod(&Yb, &Xa, &p, &ZZ) );
michael@0 249 /* number of bytes in the derived secret */
michael@0 250 len = mp_unsigned_octet_size(&ZZ);
michael@0 251 if (len <= 0) {
michael@0 252 err = MP_BADARG;
michael@0 253 goto cleanup;
michael@0 254 }
michael@0 255 /* allocate a buffer which can hold the entire derived secret. */
michael@0 256 secret = PORT_Alloc(len);
michael@0 257 /* grab the derived secret */
michael@0 258 err = mp_to_unsigned_octets(&ZZ, secret, len);
michael@0 259 if (err >= 0) err = MP_OKAY;
michael@0 260 /*
michael@0 261 ** if outBytes is 0 take all of the bytes from the derived secret.
michael@0 262 ** if outBytes is not 0 take exactly outBytes from the derived secret, zero
michael@0 263 ** pad at the beginning if necessary, and truncate beginning bytes
michael@0 264 ** if necessary.
michael@0 265 */
michael@0 266 if (outBytes > 0)
michael@0 267 nb = outBytes;
michael@0 268 else
michael@0 269 nb = len;
michael@0 270 SECITEM_AllocItem(NULL, derivedSecret, nb);
michael@0 271 if (len < nb) {
michael@0 272 unsigned int offset = nb - len;
michael@0 273 memset(derivedSecret->data, 0, offset);
michael@0 274 memcpy(derivedSecret->data + offset, secret, len);
michael@0 275 } else {
michael@0 276 memcpy(derivedSecret->data, secret + len - nb, nb);
michael@0 277 }
michael@0 278 cleanup:
michael@0 279 mp_clear(&p);
michael@0 280 mp_clear(&Xa);
michael@0 281 mp_clear(&Yb);
michael@0 282 mp_clear(&ZZ);
michael@0 283 mp_clear(&psub1);
michael@0 284 if (secret) {
michael@0 285 /* free the buffer allocated for the full secret. */
michael@0 286 PORT_ZFree(secret, len);
michael@0 287 }
michael@0 288 if (err) {
michael@0 289 MP_TO_SEC_ERROR(err);
michael@0 290 if (derivedSecret->data)
michael@0 291 PORT_ZFree(derivedSecret->data, derivedSecret->len);
michael@0 292 return SECFailure;
michael@0 293 }
michael@0 294 return SECSuccess;
michael@0 295 }
michael@0 296
michael@0 297 SECStatus
michael@0 298 KEA_Derive(SECItem *prime,
michael@0 299 SECItem *public1,
michael@0 300 SECItem *public2,
michael@0 301 SECItem *private1,
michael@0 302 SECItem *private2,
michael@0 303 SECItem *derivedSecret)
michael@0 304 {
michael@0 305 mp_int p, Y, R, r, x, t, u, w;
michael@0 306 mp_err err;
michael@0 307 unsigned char *secret = NULL;
michael@0 308 unsigned int len = 0, offset;
michael@0 309 if (!prime || !public1 || !public2 || !private1 || !private2 ||
michael@0 310 !derivedSecret) {
michael@0 311 PORT_SetError(SEC_ERROR_INVALID_ARGS);
michael@0 312 return SECFailure;
michael@0 313 }
michael@0 314 memset(derivedSecret, 0, sizeof *derivedSecret);
michael@0 315 MP_DIGITS(&p) = 0;
michael@0 316 MP_DIGITS(&Y) = 0;
michael@0 317 MP_DIGITS(&R) = 0;
michael@0 318 MP_DIGITS(&r) = 0;
michael@0 319 MP_DIGITS(&x) = 0;
michael@0 320 MP_DIGITS(&t) = 0;
michael@0 321 MP_DIGITS(&u) = 0;
michael@0 322 MP_DIGITS(&w) = 0;
michael@0 323 CHECK_MPI_OK( mp_init(&p) );
michael@0 324 CHECK_MPI_OK( mp_init(&Y) );
michael@0 325 CHECK_MPI_OK( mp_init(&R) );
michael@0 326 CHECK_MPI_OK( mp_init(&r) );
michael@0 327 CHECK_MPI_OK( mp_init(&x) );
michael@0 328 CHECK_MPI_OK( mp_init(&t) );
michael@0 329 CHECK_MPI_OK( mp_init(&u) );
michael@0 330 CHECK_MPI_OK( mp_init(&w) );
michael@0 331 SECITEM_TO_MPINT(*prime, &p);
michael@0 332 SECITEM_TO_MPINT(*public1, &Y);
michael@0 333 SECITEM_TO_MPINT(*public2, &R);
michael@0 334 SECITEM_TO_MPINT(*private1, &r);
michael@0 335 SECITEM_TO_MPINT(*private2, &x);
michael@0 336 /* t = DH(Y, r, p) = Y ** r mod p */
michael@0 337 CHECK_MPI_OK( mp_exptmod(&Y, &r, &p, &t) );
michael@0 338 /* u = DH(R, x, p) = R ** x mod p */
michael@0 339 CHECK_MPI_OK( mp_exptmod(&R, &x, &p, &u) );
michael@0 340 /* w = (t + u) mod p */
michael@0 341 CHECK_MPI_OK( mp_addmod(&t, &u, &p, &w) );
michael@0 342 /* allocate a buffer for the full derived secret */
michael@0 343 len = mp_unsigned_octet_size(&w);
michael@0 344 secret = PORT_Alloc(len);
michael@0 345 /* grab the secret */
michael@0 346 err = mp_to_unsigned_octets(&w, secret, len);
michael@0 347 if (err > 0) err = MP_OKAY;
michael@0 348 /* allocate output buffer */
michael@0 349 SECITEM_AllocItem(NULL, derivedSecret, KEA_DERIVED_SECRET_LEN);
michael@0 350 memset(derivedSecret->data, 0, derivedSecret->len);
michael@0 351 /* copy in the 128 lsb of the secret */
michael@0 352 if (len >= KEA_DERIVED_SECRET_LEN) {
michael@0 353 memcpy(derivedSecret->data, secret + (len - KEA_DERIVED_SECRET_LEN),
michael@0 354 KEA_DERIVED_SECRET_LEN);
michael@0 355 } else {
michael@0 356 offset = KEA_DERIVED_SECRET_LEN - len;
michael@0 357 memcpy(derivedSecret->data + offset, secret, len);
michael@0 358 }
michael@0 359 cleanup:
michael@0 360 mp_clear(&p);
michael@0 361 mp_clear(&Y);
michael@0 362 mp_clear(&R);
michael@0 363 mp_clear(&r);
michael@0 364 mp_clear(&x);
michael@0 365 mp_clear(&t);
michael@0 366 mp_clear(&u);
michael@0 367 mp_clear(&w);
michael@0 368 if (secret)
michael@0 369 PORT_ZFree(secret, len);
michael@0 370 if (err) {
michael@0 371 MP_TO_SEC_ERROR(err);
michael@0 372 return SECFailure;
michael@0 373 }
michael@0 374 return SECSuccess;
michael@0 375 }
michael@0 376
michael@0 377 PRBool
michael@0 378 KEA_Verify(SECItem *Y, SECItem *prime, SECItem *subPrime)
michael@0 379 {
michael@0 380 mp_int p, q, y, r;
michael@0 381 mp_err err;
michael@0 382 int cmp = 1; /* default is false */
michael@0 383 if (!Y || !prime || !subPrime) {
michael@0 384 PORT_SetError(SEC_ERROR_INVALID_ARGS);
michael@0 385 return SECFailure;
michael@0 386 }
michael@0 387 MP_DIGITS(&p) = 0;
michael@0 388 MP_DIGITS(&q) = 0;
michael@0 389 MP_DIGITS(&y) = 0;
michael@0 390 MP_DIGITS(&r) = 0;
michael@0 391 CHECK_MPI_OK( mp_init(&p) );
michael@0 392 CHECK_MPI_OK( mp_init(&q) );
michael@0 393 CHECK_MPI_OK( mp_init(&y) );
michael@0 394 CHECK_MPI_OK( mp_init(&r) );
michael@0 395 SECITEM_TO_MPINT(*prime, &p);
michael@0 396 SECITEM_TO_MPINT(*subPrime, &q);
michael@0 397 SECITEM_TO_MPINT(*Y, &y);
michael@0 398 /* compute r = y**q mod p */
michael@0 399 CHECK_MPI_OK( mp_exptmod(&y, &q, &p, &r) );
michael@0 400 /* compare to 1 */
michael@0 401 cmp = mp_cmp_d(&r, 1);
michael@0 402 cleanup:
michael@0 403 mp_clear(&p);
michael@0 404 mp_clear(&q);
michael@0 405 mp_clear(&y);
michael@0 406 mp_clear(&r);
michael@0 407 if (err) {
michael@0 408 MP_TO_SEC_ERROR(err);
michael@0 409 return PR_FALSE;
michael@0 410 }
michael@0 411 return (cmp == 0) ? PR_TRUE : PR_FALSE;
michael@0 412 }

mercurial