Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* This Source Code Form is subject to the terms of the Mozilla Public | 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 | 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/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | 4 |
| 5 /* | 5 /* |
| 6 * PQG parameter generation/verification. Based on FIPS 186-1. | 6 * PQG parameter generation/verification. Based on FIPS 186-3. |
| 7 * | 7 * |
| 8 * $Id: pqg.c,v 1.18 2012/04/25 14:49:43 gerv%gerv.net Exp $ | 8 * $Id: pqg.c,v 1.21 2012/06/25 17:30:17 rrelyea%redhat.com Exp $ |
| 9 */ | 9 */ |
| 10 #ifdef FREEBL_NO_DEPEND | 10 #ifdef FREEBL_NO_DEPEND |
| 11 #include "stubs.h" | 11 #include "stubs.h" |
| 12 #endif | 12 #endif |
| 13 | 13 |
| 14 #include "prerr.h" | 14 #include "prerr.h" |
| 15 #include "secerr.h" | 15 #include "secerr.h" |
| 16 | 16 |
| 17 #include "prtypes.h" | 17 #include "prtypes.h" |
| 18 #include "blapi.h" | 18 #include "blapi.h" |
| 19 #include "secitem.h" | 19 #include "secitem.h" |
| 20 #include "mpi.h" | 20 #include "mpi.h" |
| 21 #include "mpprime.h" | 21 #include "mpprime.h" |
| 22 #include "mplogic.h" | 22 #include "mplogic.h" |
| 23 #include "secmpi.h" | 23 #include "secmpi.h" |
| 24 | 24 |
| 25 #define MAX_ITERATIONS 1000 /* Maximum number of iterations of primegen */ | 25 #define MAX_ITERATIONS 1000 /* Maximum number of iterations of primegen */ |
| 26 #define PQG_Q_PRIMALITY_TESTS 18 /* from HAC table 4.4 */ | 26 |
| 27 #define PQG_P_PRIMALITY_TESTS 5 /* from HAC table 4.4 */ | 27 typedef enum { |
| 28 | 28 FIPS186_1_TYPE,» » /* Probablistic */ |
| 29 /* XXX to be replaced by define in blapit.h */ | 29 FIPS186_3_TYPE,» » /* Probablistic */ |
| 30 #define BITS_IN_Q 160 | 30 FIPS186_3_ST_TYPE» » /* Shawe-Taylor provable */ |
| 31 | 31 } pqgGenType; |
| 32 /* For FIPS-compliance testing. | 32 |
| 33 ** The following array holds the seed defined in FIPS 186-1 appendix 5. | 33 /* |
| 34 ** This seed is used to generate P and Q according to appendix 2; use of | 34 * These test iterations are quite a bit larger than we previously had. |
| 35 ** this seed will exactly generate the PQG specified in appendix 2. | 35 * This is because FIPS 186-3 is worried about the primes in PQG generation. |
| 36 */ | 36 * It may be possible to purposefully construct composites which more |
| 37 #ifdef FIPS_186_1_A5_TEST | 37 * iterations of Miller-Rabin than the for your normal randomly selected |
|
wtc
2012/09/26 00:21:09
This sentence also has typos. I don't know how to
| |
| 38 static const unsigned char fips_186_1_a5_pqseed[] = { | 38 * numbers.There are 3 ways to counter this: 1) use one of the cool provably |
|
Ryan Sleevi
2012/09/25 21:56:55
typo: numbers.There -> numbers. There
| |
| 39 0xd5, 0x01, 0x4e, 0x4b, 0x60, 0xef, 0x2b, 0xa8, | 39 * prime algorithms (which would require a lot more work than DSA-2 deservers. |
| 40 0xb6, 0x21, 0x1b, 0x40, 0x62, 0xba, 0x32, 0x24, | 40 * 2) add a Lucas primality test (which requires coding a Lucas primality test, |
| 41 0xe0, 0x42, 0x7d, 0xd3 | 41 * or 3) use a larger M-R test count. I chose the latter. It increases the time |
|
Ryan Sleevi
2012/09/25 21:56:55
nit: "latter" in lists with > 2 items = weird way
wtc
2012/09/26 00:21:09
Should I change this to "the last"?
| |
| 42 }; | 42 * that it takes to prove the selected prime, but it shouldn't increase the |
| 43 #endif | 43 * overall time to run the algorithm (non-primes should still faile M-R |
| 44 * realively quickly). If you want to get that last bit of performance, | |
| 45 * implement Lucas and adjust these two functions. See FIPS 186-3 Appendix C | |
| 46 * and F for more information. | |
| 47 */ | |
| 48 int prime_testcount_p(int L, int N) | |
| 49 { | |
| 50 switch (L) { | |
| 51 case 1024: | |
| 52 » return 40; | |
| 53 case 2048: | |
| 54 » return 56; | |
| 55 case 3072: | |
| 56 » return 64; | |
| 57 default: | |
| 58 » break; | |
| 59 } | |
| 60 return 50; /* L = 512-960 */ | |
| 61 } | |
| 62 | |
| 63 /* The q numbers are different if you run M-R followd by Lucas. I created | |
| 64 * a separate function so if someone wanted to add the Lucas check, they | |
| 65 * could do so fairly easily */ | |
| 66 int prime_testcount_q(int L, int N) | |
| 67 { | |
| 68 return prime_testcount_p(L,N); | |
| 69 } | |
| 70 | |
| 71 /* | |
| 72 * generic function to make sure our input matches DSA2 requirements | |
| 73 * this gives us one place to go if we need to bump the requirements in the | |
| 74 * future. | |
| 75 */ | |
| 76 SECStatus static | |
| 77 pqg_validate_dsa2(unsigned int L, unsigned int N) | |
| 78 { | |
| 79 | |
| 80 switch (L) { | |
| 81 case 1024: | |
| 82 » if (N != DSA1_Q_BITS) { | |
| 83 » PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 84 » return SECFailure; | |
| 85 » } | |
| 86 » break; | |
| 87 case 2048: | |
| 88 » if ((N != 224) && (N != 256)) { | |
| 89 » PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 90 » return SECFailure; | |
| 91 » } | |
| 92 » break; | |
| 93 case 3072: | |
| 94 » if (N != 256) { | |
| 95 » PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 96 » return SECFailure; | |
| 97 » } | |
| 98 » break; | |
| 99 default: | |
| 100 » PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 101 » return SECFailure; | |
| 102 } | |
| 103 return SECSuccess; | |
| 104 } | |
| 105 | |
| 106 /* | |
| 107 * Select the lowest hash algorithm usable | |
| 108 */ | |
| 109 static HASH_HashType | |
| 110 getFirstHash(unsigned int L, unsigned int N) | |
| 111 { | |
| 112 if (N < 224) { | |
| 113 » return HASH_AlgSHA1; | |
| 114 } | |
| 115 if (N < 256) { | |
| 116 » return HASH_AlgSHA224; | |
| 117 } | |
| 118 if (N < 384) { | |
| 119 » return HASH_AlgSHA256; | |
| 120 } | |
| 121 if (N < 512) { | |
| 122 » return HASH_AlgSHA384; | |
| 123 } | |
| 124 return HASH_AlgSHA512; | |
| 125 } | |
| 126 | |
| 127 /* | |
| 128 * find the next usable hash algorthim | |
| 129 */ | |
| 130 static HASH_HashType | |
| 131 getNextHash(HASH_HashType hashtype) | |
| 132 { | |
| 133 switch (hashtype) { | |
| 134 case HASH_AlgSHA1: | |
| 135 » hashtype = HASH_AlgSHA224; | |
| 136 » break; | |
| 137 case HASH_AlgSHA224: | |
| 138 » hashtype = HASH_AlgSHA256; | |
| 139 » break; | |
| 140 case HASH_AlgSHA256: | |
| 141 » hashtype = HASH_AlgSHA384; | |
| 142 » break; | |
| 143 case HASH_AlgSHA384: | |
| 144 » hashtype = HASH_AlgSHA512; | |
| 145 » break; | |
| 146 case HASH_AlgSHA512: | |
| 147 default: | |
| 148 » hashtype = HASH_AlgTOTAL; | |
| 149 » break; | |
| 150 } | |
| 151 return hashtype; | |
| 152 } | |
| 153 | |
| 154 | |
| 155 unsigned int | |
| 156 PQG_GetLength(const SECItem *obj) | |
| 157 { | |
| 158 unsigned int len = obj->len; | |
| 159 | |
| 160 if (obj->data == NULL) { | |
| 161 » return 0; | |
| 162 } | |
| 163 if (len > 1 && obj->data[0] == 0) { | |
| 164 » len--; | |
| 165 } | |
| 166 return len; | |
| 167 } | |
| 168 | |
| 169 SECStatus | |
| 170 PQG_Check(const PQGParams *params) | |
| 171 { | |
| 172 unsigned int L,N; | |
| 173 SECStatus rv = SECSuccess; | |
| 174 | |
| 175 if (params == NULL) { | |
| 176 » PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 177 » return SECFailure; | |
| 178 } | |
| 179 | |
| 180 L = PQG_GetLength(¶ms->prime)*BITS_PER_BYTE; | |
| 181 N = PQG_GetLength(¶ms->subPrime)*BITS_PER_BYTE; | |
| 182 | |
| 183 if (L < 1024) { | |
| 184 » int j; | |
| 185 | |
| 186 » /* handle DSA1 pqg parameters with less thatn 1024 bits*/ | |
| 187 » if ( N != DSA1_Q_BITS ) { | |
| 188 » PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 189 » return SECFailure; | |
| 190 » } | |
| 191 » j = PQG_PBITS_TO_INDEX(L); | |
| 192 » if ( j >= 0 && j <= 8 ) { | |
| 193 » PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 194 » rv = SECFailure; | |
| 195 » } | |
| 196 } else { | |
| 197 » /* handle DSA2 parameters (includes DSA1, 1024 bits) */ | |
| 198 » rv = pqg_validate_dsa2(L, N); | |
| 199 } | |
| 200 return rv; | |
| 201 } | |
| 202 | |
| 203 HASH_HashType | |
| 204 PQG_GetHashType(const PQGParams *params) | |
| 205 { | |
| 206 unsigned int L,N; | |
| 207 | |
| 208 if (params == NULL) { | |
| 209 » PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 210 » return SECFailure; | |
| 211 } | |
| 212 | |
| 213 L = PQG_GetLength(¶ms->prime)*BITS_PER_BYTE; | |
| 214 N = PQG_GetLength(¶ms->subPrime)*BITS_PER_BYTE; | |
| 215 return getFirstHash(L, N); | |
| 216 } | |
| 217 | |
| 218 static unsigned int | |
| 219 HASH_ResultLen(HASH_HashType type) | |
| 220 { | |
| 221 const SECHashObject *hash_obj = HASH_GetRawHashObject(type); | |
| 222 if (hash_obj == NULL) { | |
| 223 » return 0; | |
| 224 } | |
| 225 return hash_obj->length; | |
| 226 } | |
| 227 | |
| 228 static SECStatus | |
| 229 HASH_HashBuf(HASH_HashType type, unsigned char *dest, | |
| 230 » const unsigned char *src, PRUint32 src_len) | |
| 231 { | |
| 232 const SECHashObject *hash_obj = HASH_GetRawHashObject(type); | |
| 233 void *hashcx = NULL; | |
| 234 unsigned int dummy; | |
| 235 | |
| 236 if (hash_obj == NULL) { | |
| 237 » return SECFailure; | |
| 238 } | |
| 239 | |
| 240 hashcx = hash_obj->create(); | |
| 241 if (hashcx == NULL) { | |
| 242 » return SECFailure; | |
| 243 } | |
| 244 hash_obj->begin(hashcx); | |
| 245 hash_obj->update(hashcx,src,src_len); | |
| 246 hash_obj->end(hashcx,dest, &dummy, hash_obj->length); | |
| 247 hash_obj->destroy(hashcx, PR_TRUE); | |
| 248 return SECSuccess; | |
| 249 } | |
| 44 | 250 |
| 45 /* Get a seed for generating P and Q. If in testing mode, copy in the | 251 /* Get a seed for generating P and Q. If in testing mode, copy in the |
| 46 ** seed from FIPS 186-1 appendix 5. Otherwise, obtain bytes from the | 252 ** seed from FIPS 186-1 appendix 5. Otherwise, obtain bytes from the |
| 47 ** global random number generator. | 253 ** global random number generator. |
| 48 */ | 254 */ |
| 49 static SECStatus | 255 static SECStatus |
| 50 getPQseed(SECItem *seed, PRArenaPool* arena) | 256 getPQseed(SECItem *seed, PRArenaPool* arena) |
| 51 { | 257 { |
| 52 SECStatus rv; | 258 SECStatus rv; |
| 53 | 259 |
| 54 if (!seed->data) { | 260 if (!seed->data) { |
| 55 seed->data = (unsigned char*)PORT_ArenaZAlloc(arena, seed->len); | 261 seed->data = (unsigned char*)PORT_ArenaZAlloc(arena, seed->len); |
| 56 } | 262 } |
| 57 if (!seed->data) { | 263 if (!seed->data) { |
| 58 PORT_SetError(SEC_ERROR_NO_MEMORY); | 264 PORT_SetError(SEC_ERROR_NO_MEMORY); |
| 59 return SECFailure; | 265 return SECFailure; |
| 60 } | 266 } |
| 61 #ifdef FIPS_186_1_A5_TEST | |
| 62 memcpy(seed->data, fips_186_1_a5_pqseed, seed->len); | |
| 63 return SECSuccess; | |
| 64 #else | |
| 65 rv = RNG_GenerateGlobalRandomBytes(seed->data, seed->len); | 267 rv = RNG_GenerateGlobalRandomBytes(seed->data, seed->len); |
| 66 /* | 268 /* |
| 67 * NIST CMVP disallows a sequence of 20 bytes with the most | 269 * NIST CMVP disallows a sequence of 20 bytes with the most |
| 68 * significant byte equal to 0. Perhaps they interpret | 270 * significant byte equal to 0. Perhaps they interpret |
| 69 * "a sequence of at least 160 bits" as "a number >= 2^159". | 271 * "a sequence of at least 160 bits" as "a number >= 2^159". |
| 70 * So we always set the most significant bit to 1. (bug 334533) | 272 * So we always set the most significant bit to 1. (bug 334533) |
| 71 */ | 273 */ |
| 72 seed->data[0] |= 0x80; | 274 seed->data[0] |= 0x80; |
| 73 return rv; | 275 return rv; |
| 74 #endif | |
| 75 } | 276 } |
| 76 | 277 |
| 77 /* Generate a candidate h value. If in testing mode, use the h value | 278 /* Generate a candidate h value. If in testing mode, use the h value |
| 78 ** specified in FIPS 186-1 appendix 5, h = 2. Otherwise, obtain bytes | 279 ** specified in FIPS 186-1 appendix 5, h = 2. Otherwise, obtain bytes |
| 79 ** from the global random number generator. | 280 ** from the global random number generator. |
| 80 */ | 281 */ |
| 81 static SECStatus | 282 static SECStatus |
| 82 generate_h_candidate(SECItem *hit, mp_int *H) | 283 generate_h_candidate(SECItem *hit, mp_int *H) |
| 83 { | 284 { |
| 84 SECStatus rv = SECSuccess; | 285 SECStatus rv = SECSuccess; |
| 85 mp_err err = MP_OKAY; | 286 mp_err err = MP_OKAY; |
| 86 #ifdef FIPS_186_1_A5_TEST | 287 #ifdef FIPS_186_1_A5_TEST |
| 87 memset(hit->data, 0, hit->len); | 288 memset(hit->data, 0, hit->len); |
| 88 hit->data[hit->len-1] = 0x02; | 289 hit->data[hit->len-1] = 0x02; |
| 89 #else | 290 #else |
| 90 rv = RNG_GenerateGlobalRandomBytes(hit->data, hit->len); | 291 rv = RNG_GenerateGlobalRandomBytes(hit->data, hit->len); |
| 91 #endif | 292 #endif |
| 92 if (rv) | 293 if (rv) |
| 93 return SECFailure; | 294 return SECFailure; |
| 94 err = mp_read_unsigned_octets(H, hit->data, hit->len); | 295 err = mp_read_unsigned_octets(H, hit->data, hit->len); |
| 95 if (err) { | 296 if (err) { |
| 96 MP_TO_SEC_ERROR(err); | 297 MP_TO_SEC_ERROR(err); |
| 97 return SECFailure; | 298 return SECFailure; |
| 98 } | 299 } |
| 99 return SECSuccess; | 300 return SECSuccess; |
| 100 } | 301 } |
| 101 | 302 |
| 102 /* Compute SHA[(SEED + addend) mod 2**g] | |
| 103 ** Result is placed in shaOutBuf. | |
| 104 ** This computation is used in steps 2 and 7 of FIPS 186 Appendix 2.2 . | |
| 105 */ | |
| 106 static SECStatus | 303 static SECStatus |
| 107 addToSeedThenSHA(const SECItem * seed, | 304 addToSeed(const SECItem * seed, |
| 108 unsigned long addend, | 305 unsigned long addend, |
| 109 int g, | 306 int seedlen, /* g in 186-1 */ |
| 110 unsigned char * shaOutBuf) | 307 SECItem * seedout) |
| 111 { | 308 { |
| 112 SECItem str = { 0, 0, 0 }; | |
| 113 mp_int s, sum, modulus, tmp; | 309 mp_int s, sum, modulus, tmp; |
| 114 mp_err err = MP_OKAY; | 310 mp_err err = MP_OKAY; |
| 115 SECStatus rv = SECSuccess; | 311 SECStatus rv = SECSuccess; |
| 116 MP_DIGITS(&s) = 0; | 312 MP_DIGITS(&s) = 0; |
| 117 MP_DIGITS(&sum) = 0; | 313 MP_DIGITS(&sum) = 0; |
| 118 MP_DIGITS(&modulus) = 0; | 314 MP_DIGITS(&modulus) = 0; |
| 119 MP_DIGITS(&tmp) = 0; | 315 MP_DIGITS(&tmp) = 0; |
| 120 CHECK_MPI_OK( mp_init(&s) ); | 316 CHECK_MPI_OK( mp_init(&s) ); |
| 121 CHECK_MPI_OK( mp_init(&sum) ); | 317 CHECK_MPI_OK( mp_init(&sum) ); |
| 122 CHECK_MPI_OK( mp_init(&modulus) ); | 318 CHECK_MPI_OK( mp_init(&modulus) ); |
| 123 SECITEM_TO_MPINT(*seed, &s); /* s = seed */ | 319 SECITEM_TO_MPINT(*seed, &s); /* s = seed */ |
| 124 /* seed += addend */ | 320 /* seed += addend */ |
| 125 if (addend < MP_DIGIT_MAX) { | 321 if (addend < MP_DIGIT_MAX) { |
| 126 CHECK_MPI_OK( mp_add_d(&s, (mp_digit)addend, &s) ); | 322 CHECK_MPI_OK( mp_add_d(&s, (mp_digit)addend, &s) ); |
| 127 } else { | 323 } else { |
| 128 CHECK_MPI_OK( mp_init(&tmp) ); | 324 CHECK_MPI_OK( mp_init(&tmp) ); |
| 129 CHECK_MPI_OK( mp_set_ulong(&tmp, addend) ); | 325 CHECK_MPI_OK( mp_set_ulong(&tmp, addend) ); |
| 130 CHECK_MPI_OK( mp_add(&s, &tmp, &s) ); | 326 CHECK_MPI_OK( mp_add(&s, &tmp, &s) ); |
| 131 } | 327 } |
| 132 CHECK_MPI_OK( mp_div_2d(&s, (mp_digit)g, NULL, &sum) );/*sum = s mod 2**g */ | 328 /*sum = s mod 2**seedlen */ |
| 133 MPINT_TO_SECITEM(&sum, &str, NULL); | 329 CHECK_MPI_OK( mp_div_2d(&s, (mp_digit)seedlen, NULL, &sum) ); |
| 134 rv = SHA1_HashBuf(shaOutBuf, str.data, str.len); /* SHA1 hash result */ | 330 if (seedout->data != NULL) { |
| 331 » SECITEM_ZfreeItem(seedout, PR_FALSE); | |
| 332 } | |
| 333 MPINT_TO_SECITEM(&sum, seedout, NULL); | |
| 135 cleanup: | 334 cleanup: |
| 136 mp_clear(&s); | 335 mp_clear(&s); |
| 137 mp_clear(&sum); | 336 mp_clear(&sum); |
| 138 mp_clear(&modulus); | 337 mp_clear(&modulus); |
| 139 mp_clear(&tmp); | 338 mp_clear(&tmp); |
| 140 if (str.data) | |
| 141 SECITEM_ZfreeItem(&str, PR_FALSE); | |
| 142 if (err) { | 339 if (err) { |
| 143 MP_TO_SEC_ERROR(err); | 340 MP_TO_SEC_ERROR(err); |
| 144 return SECFailure; | 341 return SECFailure; |
| 145 } | 342 } |
| 146 return rv; | 343 return rv; |
| 147 } | 344 } |
| 148 | 345 |
| 346 /* Compute Hash[(SEED + addend) mod 2**g] | |
| 347 ** Result is placed in shaOutBuf. | |
| 348 ** This computation is used in steps 2 and 7 of FIPS 186 Appendix 2.2 and | |
| 349 ** step 11.2 of FIPS 186-3 Appendix A.1.1.2 . | |
| 350 */ | |
| 351 static SECStatus | |
| 352 addToSeedThenHash( HASH_HashType hashtype, | |
| 353 const SECItem * seed, | |
| 354 unsigned long addend, | |
| 355 int seedlen, /* g in 186-1 */ | |
| 356 unsigned char * hashOutBuf) | |
| 357 { | |
| 358 SECItem str = { 0, 0, 0 }; | |
| 359 SECStatus rv; | |
| 360 rv = addToSeed(seed, addend, seedlen, &str); | |
| 361 if (rv != SECSuccess) { | |
| 362 return rv; | |
| 363 } | |
| 364 rv = HASH_HashBuf(hashtype, hashOutBuf, str.data, str.len);/* hash result */ | |
| 365 if (str.data) | |
| 366 SECITEM_ZfreeItem(&str, PR_FALSE); | |
| 367 return rv; | |
| 368 } | |
| 369 | |
| 149 /* | 370 /* |
| 150 ** Perform steps 2 and 3 of FIPS 186, appendix 2.2. | 371 ** Perform steps 2 and 3 of FIPS 186-1, appendix 2.2. |
| 151 ** Generate Q from seed. | 372 ** Generate Q from seed. |
| 152 */ | 373 */ |
| 153 static SECStatus | 374 static SECStatus |
| 154 makeQfromSeed( | 375 makeQfromSeed( |
| 155 unsigned int g, /* input. Length of seed in bits. */ | 376 unsigned int g, /* input. Length of seed in bits. */ |
| 156 const SECItem * seed, /* input. */ | 377 const SECItem * seed, /* input. */ |
| 157 mp_int * Q) /* output. */ | 378 mp_int * Q) /* output. */ |
| 158 { | 379 { |
| 159 unsigned char sha1[SHA1_LENGTH]; | 380 unsigned char sha1[SHA1_LENGTH]; |
| 160 unsigned char sha2[SHA1_LENGTH]; | 381 unsigned char sha2[SHA1_LENGTH]; |
| 161 unsigned char U[SHA1_LENGTH]; | 382 unsigned char U[SHA1_LENGTH]; |
| 162 SECStatus rv = SECSuccess; | 383 SECStatus rv = SECSuccess; |
| 163 mp_err err = MP_OKAY; | 384 mp_err err = MP_OKAY; |
| 164 int i; | 385 int i; |
| 165 /* ****************************************************************** | 386 /* ****************************************************************** |
| 166 ** Step 2. | 387 ** Step 2. |
| 167 ** "Compute U = SHA[SEED] XOR SHA[(SEED+1) mod 2**g]." | 388 ** "Compute U = SHA[SEED] XOR SHA[(SEED+1) mod 2**g]." |
| 168 **/ | 389 **/ |
| 169 CHECK_SEC_OK( SHA1_HashBuf(sha1, seed->data, seed->len) ); | 390 CHECK_SEC_OK( SHA1_HashBuf(sha1, seed->data, seed->len) ); |
| 170 CHECK_SEC_OK( addToSeedThenSHA(seed, 1, g, sha2) ); | 391 CHECK_SEC_OK( addToSeedThenHash(HASH_AlgSHA1, seed, 1, g, sha2) ); |
| 171 for (i=0; i<SHA1_LENGTH; ++i) | 392 for (i=0; i<SHA1_LENGTH; ++i) |
| 172 U[i] = sha1[i] ^ sha2[i]; | 393 U[i] = sha1[i] ^ sha2[i]; |
| 173 /* ****************************************************************** | 394 /* ****************************************************************** |
| 174 ** Step 3. | 395 ** Step 3. |
| 175 ** "Form Q from U by setting the most signficant bit (the 2**159 bit) | 396 ** "Form Q from U by setting the most signficant bit (the 2**159 bit) |
| 176 ** and the least signficant bit to 1. In terms of boolean operations, | 397 ** and the least signficant bit to 1. In terms of boolean operations, |
| 177 ** Q = U OR 2**159 OR 1. Note that 2**159 < Q < 2**160." | 398 ** Q = U OR 2**159 OR 1. Note that 2**159 < Q < 2**160." |
| 178 */ | 399 */ |
| 179 U[0] |= 0x80; /* U is MSB first */ | 400 U[0] |= 0x80; /* U is MSB first */ |
| 180 U[SHA1_LENGTH-1] |= 0x01; | 401 U[SHA1_LENGTH-1] |= 0x01; |
| 181 err = mp_read_unsigned_octets(Q, U, SHA1_LENGTH); | 402 err = mp_read_unsigned_octets(Q, U, SHA1_LENGTH); |
| 182 cleanup: | 403 cleanup: |
| 183 memset(U, 0, SHA1_LENGTH); | 404 memset(U, 0, SHA1_LENGTH); |
| 184 memset(sha1, 0, SHA1_LENGTH); | 405 memset(sha1, 0, SHA1_LENGTH); |
| 185 memset(sha2, 0, SHA1_LENGTH); | 406 memset(sha2, 0, SHA1_LENGTH); |
| 186 if (err) { | 407 if (err) { |
| 187 MP_TO_SEC_ERROR(err); | 408 MP_TO_SEC_ERROR(err); |
| 188 return SECFailure; | 409 return SECFailure; |
| 189 } | 410 } |
| 190 return rv; | 411 return rv; |
| 191 } | 412 } |
| 192 | 413 |
| 193 /* Perform steps 7, 8 and 9 of FIPS 186, appendix 2.2. | 414 /* |
| 415 ** Perform steps 6 and 7 of FIPS 186-3, appendix A.1.1.2. | |
| 416 ** Generate Q from seed. | |
| 417 */ | |
| 418 static SECStatus | |
| 419 makeQ2fromSeed( | |
| 420 HASH_HashType hashtype, /* selected Hashing algorithm */ | |
| 421 unsigned int N, /* input. Length of q in bits. */ | |
| 422 const SECItem * seed, /* input. */ | |
| 423 mp_int * Q) /* output. */ | |
| 424 { | |
| 425 unsigned char U[HASH_LENGTH_MAX]; | |
| 426 SECStatus rv = SECSuccess; | |
| 427 mp_err err = MP_OKAY; | |
| 428 int N_bytes = N/BITS_PER_BYTE; /* length of N in bytes rather than bits */ | |
| 429 int hashLen = HASH_ResultLen(hashtype); | |
| 430 int offset = 0; | |
| 431 | |
| 432 /* ****************************************************************** | |
| 433 ** Step 6. | |
| 434 ** "Compute U = hash[SEED] mod 2**N-1]." | |
| 435 **/ | |
| 436 CHECK_SEC_OK( HASH_HashBuf(hashtype, U, seed->data, seed->len) ); | |
| 437 /* mod 2**N . Step 7 will explicitly set the top bit to 1, so no need | |
| 438 * to handle mod 2**N-1 */ | |
| 439 if (hashLen > N_bytes) { | |
| 440 offset = hashLen - N_bytes; | |
| 441 } | |
| 442 /* ****************************************************************** | |
| 443 ** Step 7. | |
| 444 ** computed_q = 2**(N-1) + U + 1 - (U mod 2) | |
| 445 ** | |
| 446 ** This is the same as: | |
| 447 ** computed_q = 2**(N-1) | U | 1; | |
| 448 */ | |
| 449 U[offset] |= 0x80; /* U is MSB first */ | |
| 450 U[hashLen-1] |= 0x01; | |
| 451 err = mp_read_unsigned_octets(Q, &U[offset], N_bytes); | |
| 452 cleanup: | |
| 453 memset(U, 0, HASH_LENGTH_MAX); | |
| 454 if (err) { | |
| 455 MP_TO_SEC_ERROR(err); | |
| 456 return SECFailure; | |
| 457 } | |
| 458 return rv; | |
| 459 } | |
| 460 | |
| 461 /* | |
| 462 ** Perform steps from FIPS 186-3, Appendix A.1.2.1 and Appendix C.6 | |
| 463 ** | |
| 464 ** This generates a provable prime from two smaller prime. The resulting | |
| 465 ** prime p will have q0 as a multiple of p-1. q0 can be 1. | |
| 466 ** | |
| 467 ** This implments steps 4 thorough 22 of FIPS 186-3 A.1.2.1 and | |
| 468 ** steps 16 through 34 of FIPS 186-2 C.6 | |
| 469 */ | |
| 470 #define MAX_ST_SEED_BITS HASH_LENGTH_MAX*BITS_PER_BYTE | |
| 471 SECStatus | |
| 472 makePrimefromPrimesShaweTaylor( | |
| 473 HASH_HashType hashtype, /* selected Hashing algorithm */ | |
| 474 unsigned int length, /* input. Length of prime in bits. */ | |
| 475 mp_int * c0, /* seed prime */ | |
| 476 mp_int * q, /* sub prime, can be 1 */ | |
| 477 mp_int * prime, /* output. */ | |
| 478 SECItem * prime_seed, /* input/output. */ | |
| 479 int * prime_gen_counter) /* input/output. */ | |
| 480 { | |
| 481 mp_int c; | |
| 482 mp_int c0_2; | |
| 483 mp_int t; | |
| 484 mp_int a; | |
| 485 mp_int z; | |
| 486 mp_int two_length_minus_1; | |
| 487 SECStatus rv = SECFailure; | |
| 488 int hashlen = HASH_ResultLen(hashtype); | |
| 489 int outlen = hashlen*BITS_PER_BYTE; | |
| 490 int offset; | |
| 491 unsigned char bit, mask; | |
| 492 /* x needs to hold roundup(L/outlen)*outlen. | |
| 493 * This can be no larger than L+outlen-1, So we set it's size to | |
| 494 * our max L + max outlen and know we are safe */ | |
| 495 unsigned char x[DSA_MAX_P_BITS/8+HASH_LENGTH_MAX]; | |
| 496 mp_err err = MP_OKAY; | |
| 497 int i; | |
| 498 int iterations; | |
| 499 int old_counter; | |
| 500 | |
| 501 MP_DIGITS(&c) = 0; | |
| 502 MP_DIGITS(&c0_2) = 0; | |
| 503 MP_DIGITS(&t) = 0; | |
| 504 MP_DIGITS(&a) = 0; | |
| 505 MP_DIGITS(&z) = 0; | |
| 506 MP_DIGITS(&two_length_minus_1) = 0; | |
| 507 CHECK_MPI_OK( mp_init(&c) ); | |
| 508 CHECK_MPI_OK( mp_init(&c0_2) ); | |
| 509 CHECK_MPI_OK( mp_init(&t) ); | |
| 510 CHECK_MPI_OK( mp_init(&a) ); | |
| 511 CHECK_MPI_OK( mp_init(&z) ); | |
| 512 CHECK_MPI_OK( mp_init(&two_length_minus_1) ); | |
| 513 | |
| 514 | |
| 515 /* | |
| 516 ** There is a slight mapping of variable names depending on which | |
| 517 ** FIPS 186 steps are being carried out. The mapping is as follows: | |
| 518 ** variable A.1.2.1 C.6 | |
| 519 ** c0 p0 c0 | |
| 520 ** q q 1 | |
| 521 ** c p c | |
| 522 ** c0_2 2*p0*q 2*c0 | |
| 523 ** length L length | |
| 524 ** prime_seed pseed prime_seed | |
| 525 ** prime_gen_counter pgen_counter prime_gen_counter | |
| 526 ** | |
| 527 ** Also note: or iterations variable is actually iterations+1, since | |
| 528 ** iterations+1 works better in C. | |
| 529 */ | |
| 530 | |
| 531 /* Step 4/16 iterations = ceiling(length/outlen)-1 */ | |
| 532 iterations = (length+outlen-1)/outlen; /* NOTE: iterations +1 */ | |
| 533 /* Step 5/17 old_counter = prime_gen_counter */ | |
| 534 old_counter = *prime_gen_counter; | |
| 535 /* | |
| 536 ** Comment: Generate a pseudorandom integer x in the interval | |
| 537 ** [2**(lenght-1), 2**length]. | |
| 538 ** | |
| 539 ** Step 6/18 x = 0 | |
| 540 */ | |
| 541 PORT_Memset(x, 0, sizeof(x)); | |
| 542 /* | |
| 543 ** Step 7/19 for i = 0 to iterations do | |
| 544 ** x = x + (HASH(prime_seed + i) * 2^(i*outlen)) | |
| 545 */ | |
| 546 for (i=0; i < iterations; i++) { | |
| 547 /* is bigger than prime_seed should get to */ | |
| 548 CHECK_SEC_OK( addToSeedThenHash(hashtype, prime_seed, i, | |
| 549 MAX_ST_SEED_BITS,&x[(iterations - i - 1)*hashlen])); | |
| 550 } | |
| 551 /* Step 8/20 prime_seed = prime_seed + iterations + 1 */ | |
| 552 CHECK_SEC_OK(addToSeed(prime_seed, iterations, MAX_ST_SEED_BITS, | |
| 553 prime_seed)); | |
| 554 /* | |
| 555 ** Step 9/21 x = 2 ** (length-1) + x mod 2 ** (length-1) | |
| 556 ** | |
| 557 ** This step mathematically sets the high bit and clears out | |
| 558 ** all the other bits higher than length. 'x' is stored | |
| 559 ** in the x array, MSB first. The above formula gives us an 'x' | |
| 560 ** which is length bytes long and has the high bit set. We also know | |
| 561 ** that length <= iterations*outlen since | |
| 562 ** iterations=ceiling(length/outlen). First we find the offset in | |
| 563 ** bytes into the array where the high bit is. | |
| 564 */ | |
| 565 offset = (outlen*iterations - length)/BITS_PER_BYTE; | |
| 566 /* now we want to set the 'high bit', since length may not be a | |
| 567 * multiple of 8,*/ | |
| 568 bit = 1 << ((length-1) & 0x7); /* select the proper bit in the byte */ | |
| 569 /* we need to zero out the rest of the bits in the byte above */ | |
| 570 mask = (bit-1); | |
| 571 /* now we set it */ | |
| 572 x[offset] = (mask & x[offset]) | bit; | |
| 573 /* | |
| 574 ** Comment: Generate a candidate prime c in the interval | |
| 575 ** [2**(lenght-1), 2**length]. | |
| 576 ** | |
| 577 ** Step 10 t = ceiling(x/(2q(p0))) | |
| 578 ** Step 22 t = ceiling(x/(2(c0))) | |
| 579 */ | |
| 580 CHECK_MPI_OK( mp_read_unsigned_octets(&t, &x[offset], | |
| 581 hashlen*iterations - offset ) ); /* t = x */ | |
| 582 CHECK_MPI_OK( mp_mul(c0, q, &c0_2) ); /* c0_2 is now c0*q */ | |
| 583 CHECK_MPI_OK( mp_add(&c0_2, &c0_2, &c0_2) ); /* c0_2 is now 2*q*c0 */ | |
| 584 CHECK_MPI_OK( mp_add(&t, &c0_2, &t) ); /* t = x+2*q*c0 */ | |
| 585 CHECK_MPI_OK( mp_sub_d(&t, (mp_digit) 1, &t) ); /* t = x+2*q*c0 -1 */ | |
| 586 /* t = floor((x+2qc0-1)/2qc0) = ceil(x/2qc0) */ | |
| 587 CHECK_MPI_OK( mp_div(&t, &c0_2, &t, NULL) ); | |
| 588 /* | |
| 589 ** step 11: if (2tqp0 +1 > 2**length), then t = ceiling(2**(length-1)/2qp0) | |
| 590 ** step 12: t = 2tqp0 +1. | |
| 591 ** | |
| 592 ** step 23: if (2tc0 +1 > 2**length), then t = ceiling(2**(length-1)/2c0) | |
| 593 ** step 24: t = 2tc0 +1. | |
| 594 */ | |
| 595 CHECK_MPI_OK( mp_2expt(&two_length_minus_1, length-1) ); | |
| 596 step_23: | |
| 597 CHECK_MPI_OK( mp_mul(&t, &c0_2, &c) ); /* c = t*2qc0 */ | |
| 598 CHECK_MPI_OK( mp_add_d(&c, (mp_digit)1, &c) ); /* c= 2tqc0 + 1*/ | |
| 599 if (mpl_significant_bits(&c) > length) { /* if c > 2**length */ | |
| 600 CHECK_MPI_OK( mp_sub_d(&c0_2, (mp_digit) 1, &t) ); /* t = 2qc0-1 */ | |
| 601 /* t = 2**(length-1) + 2qc0 -1 */ | |
| 602 CHECK_MPI_OK( mp_add(&two_length_minus_1,&t, &t) ); | |
| 603 /* t = floor((2**(length-1)+2qc0 -1)/2qco) | |
| 604 * = ceil(2**(lenght-2)/2qc0) */ | |
| 605 CHECK_MPI_OK( mp_div(&t, &c0_2, &t, NULL) ); | |
| 606 CHECK_MPI_OK( mp_mul(&t, &c0_2, &c) ); | |
| 607 CHECK_MPI_OK( mp_add_d(&c, (mp_digit)1, &c) ); /* c= 2tqc0 + 1*/ | |
| 608 } | |
| 609 /* Step 13/25 prime_gen_counter = prime_gen_counter + 1*/ | |
| 610 (*prime_gen_counter)++; | |
| 611 /* | |
| 612 ** Comment: Test the candidate prime c for primality; first pick an | |
| 613 ** integer a between 2 and c-2. | |
| 614 ** | |
| 615 ** Step 14/26 a=0 | |
| 616 */ | |
| 617 PORT_Memset(x, 0, sizeof(x)); /* use x for a */ | |
| 618 /* | |
| 619 ** Step 15/27 for i = 0 to iterations do | |
| 620 ** a = a + (HASH(prime_seed + i) * 2^(i*outlen)) | |
| 621 ** | |
| 622 ** NOTE: we reuse the x array for 'a' initially. | |
| 623 */ | |
| 624 for (i=0; i < iterations; i++) { | |
| 625 /* MAX_ST_SEED_BITS is bigger than prime_seed should get to */ | |
| 626 CHECK_SEC_OK(addToSeedThenHash(hashtype, prime_seed, i, | |
| 627 MAX_ST_SEED_BITS,&x[(iterations - i - 1)*hashlen])); | |
| 628 } | |
| 629 /* Step 16/28 prime_seed = prime_seed + iterations + 1 */ | |
| 630 CHECK_SEC_OK(addToSeed(prime_seed, iterations, MAX_ST_SEED_BITS, | |
| 631 prime_seed)); | |
| 632 /* Step 17/29 a = 2 + (a mod (c-3)). */ | |
| 633 CHECK_MPI_OK( mp_read_unsigned_octets(&a, x, iterations*hashlen) ); | |
| 634 CHECK_MPI_OK( mp_sub_d(&c, (mp_digit) 3, &z) ); /* z = c -3 */ | |
| 635 CHECK_MPI_OK( mp_mod(&a, &z, &a) ); /* a = a mod c -3 */ | |
| 636 CHECK_MPI_OK( mp_add_d(&a, (mp_digit) 2, &a) ); /* a = 2 + a mod c -3 */ | |
| 637 /* | |
| 638 ** Step 18 z = a**(2tq) mod p. | |
| 639 ** Step 30 z = a**(2t) mod c. | |
| 640 */ | |
| 641 CHECK_MPI_OK( mp_mul(&t, q, &z) ); /* z = tq */ | |
| 642 CHECK_MPI_OK( mp_add(&z, &z, &z) ); /* z = 2tq */ | |
| 643 CHECK_MPI_OK( mp_exptmod(&a, &z, &c, &z) ); /* z = a**(2tq) mod c */ | |
| 644 /* | |
| 645 ** Step 19 if (( 1 == GCD(z-1,p)) and ( 1 == z**p0 mod p )), then | |
| 646 ** Step 31 if (( 1 == GCD(z-1,c)) and ( 1 == z**c0 mod c )), then | |
| 647 */ | |
| 648 CHECK_MPI_OK( mp_sub_d(&z, (mp_digit) 1, &a) ); | |
| 649 CHECK_MPI_OK( mp_gcd(&a,&c,&a )); | |
| 650 if (mp_cmp_d(&a, (mp_digit)1) == 0) { | |
| 651 CHECK_MPI_OK( mp_exptmod(&z, c0, &c, &a) ); | |
| 652 if (mp_cmp_d(&a, (mp_digit)1) == 0) { | |
| 653 /* Step 31.1 prime = c */ | |
| 654 CHECK_MPI_OK( mp_copy(&c, prime) ); | |
| 655 /* | |
| 656 ** Step 31.2 return Success, prime, prime_seed, | |
| 657 ** prime_gen_counter | |
| 658 */ | |
| 659 rv = SECSuccess; | |
| 660 goto cleanup; | |
| 661 } | |
| 662 } | |
| 663 /* | |
| 664 ** Step 20/32 If (prime_gen_counter > 4 * length + old_counter then | |
| 665 ** return (FAILURE, 0, 0, 0). | |
| 666 ** NOTE: the test is reversed, so we fall through on failure to the | |
| 667 ** cleanup routine | |
| 668 */ | |
| 669 if (*prime_gen_counter < (4*length + old_counter)) { | |
| 670 /* Step 21/33 t = t + 1 */ | |
| 671 CHECK_MPI_OK( mp_add_d(&t, (mp_digit) 1, &t) ); | |
| 672 /* Step 22/34 Go to step 23/11 */ | |
| 673 goto step_23; | |
| 674 } | |
| 675 | |
| 676 /* if (prime_gencont > (4*length + old_counter), fall through to failure */ | |
| 677 rv = SECFailure; /* really is already set, but paranoia is good */ | |
| 678 | |
| 679 cleanup: | |
| 680 mp_clear(&c); | |
| 681 mp_clear(&c0_2); | |
| 682 mp_clear(&t); | |
| 683 mp_clear(&a); | |
| 684 mp_clear(&z); | |
| 685 mp_clear(&two_length_minus_1); | |
| 686 if (err) { | |
| 687 MP_TO_SEC_ERROR(err); | |
| 688 rv = SECFailure; | |
| 689 } | |
| 690 if (rv == SECFailure) { | |
| 691 mp_zero(prime); | |
| 692 if (prime_seed->data) { | |
| 693 SECITEM_FreeItem(prime_seed, PR_FALSE); | |
| 694 } | |
| 695 *prime_gen_counter = 0; | |
| 696 } | |
| 697 return rv; | |
| 698 } | |
| 699 | |
| 700 /* | |
| 701 ** Perform steps from FIPS 186-3, Appendix C.6 | |
| 702 ** | |
| 703 ** This generates a provable prime from a seed | |
| 704 */ | |
| 705 SECStatus | |
| 706 makePrimefromSeedShaweTaylor( | |
| 707 HASH_HashType hashtype, /* selected Hashing algorithm */ | |
| 708 unsigned int length, /* input. Length of prime in bits. */ | |
| 709 const SECItem * input_seed, /* input. */ | |
| 710 mp_int * prime, /* output. */ | |
| 711 SECItem * prime_seed, /* output. */ | |
| 712 int * prime_gen_counter) /* output. */ | |
| 713 { | |
| 714 mp_int c; | |
| 715 mp_int c0; | |
| 716 mp_int one; | |
| 717 SECStatus rv = SECFailure; | |
| 718 int hashlen = HASH_ResultLen(hashtype); | |
| 719 int outlen = hashlen*BITS_PER_BYTE; | |
| 720 int offset; | |
| 721 unsigned char bit, mask; | |
| 722 unsigned char x[HASH_LENGTH_MAX*2]; | |
| 723 mp_digit dummy; | |
| 724 mp_err err = MP_OKAY; | |
| 725 int i; | |
| 726 | |
| 727 MP_DIGITS(&c) = 0; | |
| 728 MP_DIGITS(&c0) = 0; | |
| 729 MP_DIGITS(&one) = 0; | |
| 730 CHECK_MPI_OK( mp_init(&c) ); | |
| 731 CHECK_MPI_OK( mp_init(&c0) ); | |
| 732 CHECK_MPI_OK( mp_init(&one) ); | |
| 733 | |
| 734 /* Step 1. if length < 2 then return (FAILURE, 0, 0, 0) */ | |
| 735 if (length < 2) { | |
| 736 rv = SECFailure; | |
| 737 goto cleanup; | |
| 738 } | |
| 739 /* Step 2. if length >= 33 then goto step 14 */ | |
| 740 if (length >= 33) { | |
| 741 mp_zero(&one); | |
| 742 CHECK_MPI_OK( mp_add_d(&one, (mp_digit) 1, &one) ); | |
| 743 | |
| 744 /* Step 14 (status, c0, prime_seed, prime_gen_counter) = | |
| 745 ** (ST_Random_Prime((ceil(length/2)+1, input_seed) | |
| 746 */ | |
| 747 rv = makePrimefromSeedShaweTaylor(hashtype, (length+1)/2+1, | |
| 748 input_seed, &c0, prime_seed, prime_gen_counter); | |
| 749 /* Step 15 if FAILURE is returned, return (FAILURE, 0, 0, 0). */ | |
| 750 if (rv != SECSuccess) { | |
| 751 goto cleanup; | |
| 752 } | |
| 753 /* Steps 16-34 */ | |
| 754 rv = makePrimefromPrimesShaweTaylor(hashtype,length, &c0, &one, | |
| 755 prime, prime_seed, prime_gen_counter); | |
| 756 goto cleanup; /* we're done, one way or the other */ | |
| 757 } | |
| 758 /* Step 3 prime_seed = input_seed */ | |
| 759 CHECK_SEC_OK(SECITEM_CopyItem(NULL, prime_seed, input_seed)); | |
| 760 /* Step 4 prime_gen_count = 0 */ | |
| 761 *prime_gen_counter = 0; | |
| 762 | |
| 763 step_5: | |
| 764 /* Step 5 c = Hash(prime_seed) xor Hash(prime_seed+1). */ | |
| 765 CHECK_SEC_OK(HASH_HashBuf(hashtype, x, prime_seed->data, prime_seed->len) ); | |
| 766 CHECK_SEC_OK(addToSeedThenHash(hashtype, prime_seed, 1, | |
| 767 MAX_ST_SEED_BITS, &x[hashlen]) ); | |
| 768 for (i=0; i < hashlen; i++) { | |
| 769 x[i] = x[i] ^ x[i+hashlen]; | |
| 770 } | |
| 771 /* Step 6 c = 2**length-1 + c mod 2**length-1 */ | |
| 772 /* This step mathematically sets the high bit and clears out | |
| 773 ** all the other bits higher than length. Right now c is stored | |
| 774 ** in the x array, MSB first. The above formula gives us a c which | |
| 775 ** is length bytes long and has the high bit set. We also know that | |
| 776 ** length < outlen since the smallest outlen is 160 bits and the largest | |
| 777 ** length at this point is 32 bits. So first we find the offset in bytes | |
| 778 ** into the array where the high bit is. | |
| 779 */ | |
| 780 offset = (outlen - length)/BITS_PER_BYTE; | |
| 781 /* now we want to set the 'high bit'. We have to calculate this since | |
| 782 * length may not be a multiple of 8.*/ | |
| 783 bit = 1 << ((length-1) & 0x7); /* select the proper bit in the byte */ | |
| 784 /* we need to zero out the rest of the bits in the byte above */ | |
| 785 mask = (bit-1); | |
| 786 /* now we set it */ | |
| 787 x[offset] = (mask & x[offset]) | bit; | |
| 788 /* Step 7 c = c*floor(c/2) + 1 */ | |
| 789 /* set the low bit. much easier to find (the end of the array) */ | |
| 790 x[hashlen-1] |= 1; | |
| 791 /* now that we've set our bits, we can create our candidate "c" */ | |
| 792 CHECK_MPI_OK( mp_read_unsigned_octets(&c, &x[offset], hashlen-offset) ); | |
| 793 /* Step 8 prime_gen_counter = prime_gen_counter + 1 */ | |
| 794 (*prime_gen_counter)++; | |
| 795 /* Step 9 prime_seed = prime_seed + 2 */ | |
| 796 CHECK_SEC_OK(addToSeed(prime_seed, 2, MAX_ST_SEED_BITS, prime_seed)); | |
| 797 /* Step 10 Perform deterministic primality test on c. For example, since | |
| 798 ** c is small, it's primality can be tested by trial division, See | |
| 799 ** See Appendic C.7. | |
| 800 ** | |
| 801 ** We in fact test with trial division. mpi has a built int trial divider | |
| 802 ** that divides all divisors up to 2^16. | |
| 803 */ | |
| 804 if (prime_tab[prime_tab_size-1] < 0xFFF1) { | |
| 805 /* we aren't testing all the primes between 0 and 2^16, we really | |
| 806 * can't use this construction. Just fail. */ | |
| 807 rv = SECFailure; | |
| 808 goto cleanup; | |
| 809 } | |
| 810 dummy = prime_tab_size; | |
| 811 err = mpp_divis_primes(&c, &dummy); | |
| 812 /* Step 11 if c is prime then */ | |
| 813 if (err == MP_NO) { | |
| 814 /* Step 11.1 prime = c */ | |
| 815 CHECK_MPI_OK( mp_copy(&c, prime) ); | |
| 816 /* Step 11.2 return SUCCESS prime, prime_seed, prime_gen_counter */ | |
| 817 err = MP_OKAY; | |
| 818 rv = SECSuccess; | |
| 819 goto cleanup; | |
| 820 } else if (err != MP_YES) { | |
| 821 goto cleanup; /* function failed, bail out */ | |
| 822 } else { | |
| 823 /* reset mp_err */ | |
| 824 err = MP_OKAY; | |
| 825 } | |
| 826 /* | |
| 827 ** Step 12 if (prime_gen_counter > (4*len)) | |
| 828 ** then return (FAILURE, 0, 0, 0)) | |
| 829 ** Step 13 goto step 5 | |
| 830 */ | |
| 831 if (*prime_gen_counter <= (4*length)) { | |
| 832 goto step_5; | |
| 833 } | |
| 834 /* if (prime_gencont > 4*length), fall through to failure */ | |
| 835 rv = SECFailure; /* really is already set, but paranoia is good */ | |
| 836 | |
| 837 cleanup: | |
| 838 mp_clear(&c); | |
| 839 mp_clear(&c0); | |
| 840 mp_clear(&one); | |
| 841 if (err) { | |
| 842 MP_TO_SEC_ERROR(err); | |
| 843 rv = SECFailure; | |
| 844 } | |
| 845 if (rv == SECFailure) { | |
| 846 mp_zero(prime); | |
| 847 if (prime_seed->data) { | |
| 848 SECITEM_FreeItem(prime_seed, PR_FALSE); | |
| 849 } | |
| 850 *prime_gen_counter = 0; | |
| 851 } | |
| 852 return rv; | |
| 853 } | |
| 854 | |
| 855 | |
| 856 /* | |
| 857 * Find a Q and algorithm from Seed. | |
| 858 */ | |
| 859 static SECStatus | |
| 860 findQfromSeed( | |
| 861 unsigned int L, /* input. Length of p in bits. */ | |
| 862 unsigned int N, /* input. Length of q in bits. */ | |
| 863 unsigned int g, /* input. Length of seed in bits. */ | |
| 864 const SECItem * seed, /* input. */ | |
| 865 mp_int * Q, /* input. */ | |
| 866 mp_int * Q_, /* output. */ | |
| 867 int * qseed_len, /* output */ | |
| 868 HASH_HashType *hashtypePtr, /* output. Hash uses */ | |
| 869 pqgGenType *typePtr) /* output. Generation Type used */ | |
| 870 { | |
| 871 HASH_HashType hashtype; | |
| 872 SECItem firstseed = { 0, 0, 0 }; | |
| 873 SECItem qseed = { 0, 0, 0 }; | |
| 874 SECStatus rv; | |
| 875 | |
| 876 *qseed_len = 0; /* only set if FIPS186_3_ST_TYPE */ | |
| 877 | |
| 878 /* handle legacy small DSA first can only be FIPS186_1_TYPE */ | |
| 879 if (L < 1024) { | |
| 880 rv =makeQfromSeed(g,seed,Q_); | |
| 881 if ((rv == SECSuccess) && (mp_cmp(Q,Q_) == 0)) { | |
| 882 *hashtypePtr = HASH_AlgSHA1; | |
| 883 *typePtr = FIPS186_1_TYPE; | |
| 884 return SECSuccess; | |
| 885 } | |
| 886 return SECFailure; | |
| 887 } | |
| 888 /* 1024 could use FIPS186_1 or FIPS186_3 algorithms, we need to try | |
| 889 * them both */ | |
| 890 if (L == 1024) { | |
| 891 rv = makeQfromSeed(g,seed,Q_); | |
| 892 if (rv == SECSuccess) { | |
| 893 if (mp_cmp(Q,Q_) == 0) { | |
| 894 *hashtypePtr = HASH_AlgSHA1; | |
| 895 *typePtr = FIPS186_1_TYPE; | |
| 896 return SECSuccess; | |
| 897 } | |
| 898 } | |
| 899 /* fall through for FIPS186_3 types */ | |
| 900 } | |
| 901 /* at this point we know we aren't using FIPS186_1, start trying FIPS186_3 | |
| 902 * with appropriate hash types */ | |
| 903 for (hashtype = getFirstHash(L,N); hashtype != HASH_AlgTOTAL; | |
| 904 hashtype=getNextHash(hashtype)) { | |
| 905 rv = makeQ2fromSeed(hashtype, N, seed, Q_); | |
| 906 if (rv != SECSuccess) { | |
| 907 continue; | |
| 908 } | |
| 909 if (mp_cmp(Q,Q_) == 0) { | |
| 910 *hashtypePtr = hashtype; | |
| 911 *typePtr = FIPS186_3_TYPE; | |
| 912 return SECSuccess; | |
| 913 } | |
| 914 } | |
| 915 /* | |
| 916 * OK finally try FIPS186_3 Shawe-Taylor | |
| 917 */ | |
| 918 firstseed = *seed; | |
| 919 firstseed.len = seed->len/3; | |
| 920 for (hashtype = getFirstHash(L,N); hashtype != HASH_AlgTOTAL; | |
| 921 hashtype=getNextHash(hashtype)) { | |
| 922 int count; | |
| 923 | |
| 924 rv = makePrimefromSeedShaweTaylor(hashtype, N, &firstseed, Q_, | |
| 925 &qseed, &count); | |
| 926 if (rv != SECSuccess) { | |
| 927 continue; | |
| 928 } | |
| 929 if (mp_cmp(Q,Q_) == 0) { | |
| 930 /* check qseed as well... */ | |
| 931 int offset = seed->len - qseed.len; | |
| 932 if ((offset < 0) || | |
| 933 (PORT_Memcmp(&seed->data[offset],qseed.data,qseed.len) != 0)) { | |
| 934 /* we found q, but the seeds don't match. This isn't an | |
| 935 * accident, someone has been tweeking with the seeds, just | |
| 936 * fail a this point. */ | |
| 937 SECITEM_FreeItem(&qseed,PR_FALSE); | |
| 938 return SECFailure; | |
| 939 } | |
| 940 *qseed_len = qseed.len; | |
| 941 *hashtypePtr = hashtype; | |
| 942 *typePtr = FIPS186_3_ST_TYPE; | |
| 943 SECITEM_FreeItem(&qseed, PR_FALSE); | |
| 944 return SECSuccess; | |
| 945 } | |
| 946 SECITEM_FreeItem(&qseed, PR_FALSE); | |
| 947 } | |
| 948 /* no hash algorithms found which match seed to Q, fail */ | |
| 949 return SECFailure; | |
| 950 } | |
| 951 | |
| 952 | |
| 953 | |
| 954 /* | |
| 955 ** Perform steps 7, 8 and 9 of FIPS 186, appendix 2.2. | |
| 956 ** which are the same as steps 11.1-11.5 of FIPS 186-2, App A.1.1.2 | |
| 194 ** Generate P from Q, seed, L, and offset. | 957 ** Generate P from Q, seed, L, and offset. |
| 195 */ | 958 */ |
| 196 static SECStatus | 959 static SECStatus |
| 197 makePfromQandSeed( | 960 makePfromQandSeed( |
| 961 HASH_HashType hashtype, /* selected Hashing algorithm */ | |
| 198 unsigned int L, /* Length of P in bits. Per FIPS 186. */ | 962 unsigned int L, /* Length of P in bits. Per FIPS 186. */ |
| 199 unsigned int offset, /* Per FIPS 186, appendix 2.2. */ | 963 unsigned int N, /* Length of Q in bits. Per FIPS 186. */ |
| 200 unsigned int g, /* input. Length of seed in bits. */ | 964 unsigned int offset, /* Per FIPS 186, App 2.2. & 186-3 App A.1.1.2 */ |
| 965 unsigned int seedlen, /* input. Length of seed in bits. (g in 186-1)*/ | |
| 201 const SECItem * seed, /* input. */ | 966 const SECItem * seed, /* input. */ |
| 202 const mp_int * Q, /* input. */ | 967 const mp_int * Q, /* input. */ |
| 203 mp_int * P) /* output. */ | 968 mp_int * P) /* output. */ |
| 204 { | 969 { |
| 205 unsigned int k; /* Per FIPS 186, appendix 2.2. */ | 970 unsigned int j; /* Per FIPS 186-3 App. A.1.1.2 (k in 186-1)*/ |
| 206 unsigned int n; /* Per FIPS 186, appendix 2.2. */ | 971 unsigned int n; /* Per FIPS 186, appendix 2.2. */ |
| 207 mp_digit b; /* Per FIPS 186, appendix 2.2. */ | 972 mp_digit b; /* Per FIPS 186, appendix 2.2. */ |
| 208 unsigned char V_k[SHA1_LENGTH]; | 973 unsigned int outlen; /* Per FIPS 186-3 App. A.1.1.2 */ |
| 974 unsigned int hashlen; /* outlen in bytes */ | |
| 975 unsigned char V_j[HASH_LENGTH_MAX]; | |
| 209 mp_int W, X, c, twoQ, V_n, tmp; | 976 mp_int W, X, c, twoQ, V_n, tmp; |
| 210 mp_err err = MP_OKAY; | 977 mp_err err = MP_OKAY; |
| 211 SECStatus rv = SECSuccess; | 978 SECStatus rv = SECSuccess; |
| 212 /* Initialize bignums */ | 979 /* Initialize bignums */ |
| 213 MP_DIGITS(&W) = 0; | 980 MP_DIGITS(&W) = 0; |
| 214 MP_DIGITS(&X) = 0; | 981 MP_DIGITS(&X) = 0; |
| 215 MP_DIGITS(&c) = 0; | 982 MP_DIGITS(&c) = 0; |
| 216 MP_DIGITS(&twoQ) = 0; | 983 MP_DIGITS(&twoQ) = 0; |
| 217 MP_DIGITS(&V_n) = 0; | 984 MP_DIGITS(&V_n) = 0; |
| 218 MP_DIGITS(&tmp) = 0; | 985 MP_DIGITS(&tmp) = 0; |
| 219 CHECK_MPI_OK( mp_init(&W) ); | 986 CHECK_MPI_OK( mp_init(&W) ); |
| 220 CHECK_MPI_OK( mp_init(&X) ); | 987 CHECK_MPI_OK( mp_init(&X) ); |
| 221 CHECK_MPI_OK( mp_init(&c) ); | 988 CHECK_MPI_OK( mp_init(&c) ); |
| 222 CHECK_MPI_OK( mp_init(&twoQ) ); | 989 CHECK_MPI_OK( mp_init(&twoQ) ); |
| 223 CHECK_MPI_OK( mp_init(&tmp) ); | 990 CHECK_MPI_OK( mp_init(&tmp) ); |
| 224 CHECK_MPI_OK( mp_init(&V_n) ); | 991 CHECK_MPI_OK( mp_init(&V_n) ); |
| 225 /* L - 1 = n*160 + b */ | 992 |
| 226 n = (L - 1) / BITS_IN_Q; | 993 hashlen = HASH_ResultLen(hashtype); |
| 227 b = (L - 1) % BITS_IN_Q; | 994 outlen = hashlen*BITS_PER_BYTE; |
| 995 | |
| 996 /* L - 1 = n*outlen + b */ | |
| 997 n = (L - 1) / outlen; | |
| 998 b = (L - 1) % outlen; | |
| 999 | |
| 228 /* ****************************************************************** | 1000 /* ****************************************************************** |
| 229 ** Step 7. | 1001 ** Step 11.1 (Step 7 in 186-1) |
| 230 ** "for k = 0 ... n let | 1002 ** "for j = 0 ... n let |
| 231 ** V_k = SHA[(SEED + offset + k) mod 2**g]." | 1003 ** V_j = SHA[(SEED + offset + j) mod 2**seedlen]." |
| 232 ** | 1004 ** |
| 233 ** Step 8. | 1005 ** Step 11.2 (Step 8 in 186-1) |
| 234 ** "Let W be the integer | 1006 ** "W = V_0 + (V_1 * 2**outlen) + ... + (V_n-1 * 2**((n-1)*outlen)) |
| 235 ** W = V_0 + (V_1 * 2**160) + ... + (V_n-1 * 2**((n-1)*160)) | 1007 ** + ((V_n mod 2**b) * 2**(n*outlen)) |
| 236 ** + ((V_n mod 2**b) * 2**(n*160)) | |
| 237 */ | 1008 */ |
| 238 for (k=0; k<n; ++k) { /* Do the first n terms of V_k */ | 1009 for (j=0; j<n; ++j) { /* Do the first n terms of V_j */ |
| 239 » /* Do step 7 for iteration k. | 1010 » /* Do step 11.1 for iteration j. |
| 240 » ** V_k = SHA[(seed + offset + k) mod 2**g] | 1011 » ** V_j = HASH[(seed + offset + j) mod 2**g] |
| 241 */ | 1012 */ |
| 242 » CHECK_SEC_OK( addToSeedThenSHA(seed, offset + k, g, V_k) ); | 1013 » CHECK_SEC_OK( addToSeedThenHash(hashtype,seed,offset+j, seedlen, V_j) ); |
| 243 » /* Do step 8 for iteration k. | 1014 » /* Do step 11.2 for iteration j. |
| 244 » ** W += V_k * 2**(k*160) | 1015 » ** W += V_j * 2**(j*outlen) |
| 245 */ | 1016 */ |
| 246 » OCTETS_TO_MPINT(V_k, &tmp, SHA1_LENGTH); /* get bignum V_k */ | 1017 » OCTETS_TO_MPINT(V_j, &tmp, hashlen); /* get bignum V_j */ |
| 247 » CHECK_MPI_OK( mpl_lsh(&tmp, &tmp, k*160) ); /* tmp = V_k << k*160 */ | 1018 » CHECK_MPI_OK( mpl_lsh(&tmp, &tmp, j*outlen) );/* tmp=V_j << j*outlen */ |
| 248 CHECK_MPI_OK( mp_add(&W, &tmp, &W) ); /* W += tmp */ | 1019 CHECK_MPI_OK( mp_add(&W, &tmp, &W) ); /* W += tmp */ |
| 249 } | 1020 } |
| 250 /* Step 8, continued. | 1021 /* Step 11.2, continued. |
| 251 ** [W += ((V_n mod 2**b) * 2**(n*160))] | 1022 ** [W += ((V_n mod 2**b) * 2**(n*outlen))] |
| 252 */ | 1023 */ |
| 253 CHECK_SEC_OK( addToSeedThenSHA(seed, offset + n, g, V_k) ); | 1024 CHECK_SEC_OK( addToSeedThenHash(hashtype, seed, offset + n, seedlen, V_j) ); |
| 254 OCTETS_TO_MPINT(V_k, &V_n, SHA1_LENGTH); /* get bignum V_n */ | 1025 OCTETS_TO_MPINT(V_j, &V_n, hashlen); /* get bignum V_n */ |
| 255 CHECK_MPI_OK( mp_div_2d(&V_n, b, NULL, &tmp) ); /* tmp = V_n mod 2**b */ | 1026 CHECK_MPI_OK( mp_div_2d(&V_n, b, NULL, &tmp) ); /* tmp = V_n mod 2**b */ |
| 256 CHECK_MPI_OK( mpl_lsh(&tmp, &tmp, n*160) ); /* tmp = tmp << n*160 */ | 1027 CHECK_MPI_OK( mpl_lsh(&tmp, &tmp, n*outlen) ); /* tmp = tmp << n*outlen */ |
| 257 CHECK_MPI_OK( mp_add(&W, &tmp, &W) ); /* W += tmp */ | 1028 CHECK_MPI_OK( mp_add(&W, &tmp, &W) ); /* W += tmp */ |
| 258 /* Step 8, continued. | 1029 /* Step 11.3, (Step 8 in 186-1) |
| 259 ** "and let X = W + 2**(L-1). | 1030 ** "X = W + 2**(L-1). |
| 260 ** Note that 0 <= W < 2**(L-1) and hence 2**(L-1) <= X < 2**L." | 1031 ** Note that 0 <= W < 2**(L-1) and hence 2**(L-1) <= X < 2**L." |
| 261 */ | 1032 */ |
| 262 CHECK_MPI_OK( mpl_set_bit(&X, (mp_size)(L-1), 1) ); /* X = 2**(L-1) */ | 1033 CHECK_MPI_OK( mpl_set_bit(&X, (mp_size)(L-1), 1) ); /* X = 2**(L-1) */ |
| 263 CHECK_MPI_OK( mp_add(&X, &W, &X) ); /* X += W */ | 1034 CHECK_MPI_OK( mp_add(&X, &W, &X) ); /* X += W */ |
| 264 /************************************************************* | 1035 /************************************************************* |
| 265 ** Step 9. | 1036 ** Step 11.4. (Step 9 in 186-1) |
| 266 ** "Let c = X mod 2q and set p = X - (c - 1). | 1037 ** "c = X mod 2q" |
| 267 ** Note that p is congruent to 1 mod 2q." | |
| 268 */ | 1038 */ |
| 269 CHECK_MPI_OK( mp_mul_2(Q, &twoQ) ); /* 2q */ | 1039 CHECK_MPI_OK( mp_mul_2(Q, &twoQ) ); /* 2q */ |
| 270 CHECK_MPI_OK( mp_mod(&X, &twoQ, &c) ); /* c = X mod 2q */ | 1040 CHECK_MPI_OK( mp_mod(&X, &twoQ, &c) ); /* c = X mod 2q */ |
| 1041 /************************************************************* | |
| 1042 ** Step 11.5. (Step 9 in 186-1) | |
| 1043 ** "p = X - (c - 1). | |
| 1044 ** Note that p is congruent to 1 mod 2q." | |
| 1045 */ | |
| 271 CHECK_MPI_OK( mp_sub_d(&c, 1, &c) ); /* c -= 1 */ | 1046 CHECK_MPI_OK( mp_sub_d(&c, 1, &c) ); /* c -= 1 */ |
| 272 CHECK_MPI_OK( mp_sub(&X, &c, P) ); /* P = X - c */ | 1047 CHECK_MPI_OK( mp_sub(&X, &c, P) ); /* P = X - c */ |
| 273 cleanup: | 1048 cleanup: |
| 274 mp_clear(&W); | 1049 mp_clear(&W); |
| 275 mp_clear(&X); | 1050 mp_clear(&X); |
| 276 mp_clear(&c); | 1051 mp_clear(&c); |
| 277 mp_clear(&twoQ); | 1052 mp_clear(&twoQ); |
| 278 mp_clear(&V_n); | 1053 mp_clear(&V_n); |
| 279 mp_clear(&tmp); | 1054 mp_clear(&tmp); |
| 280 if (err) { | 1055 if (err) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 326 cleanup: | 1101 cleanup: |
| 327 mp_clear(&exp); | 1102 mp_clear(&exp); |
| 328 mp_clear(&pm1); | 1103 mp_clear(&pm1); |
| 329 if (err) { | 1104 if (err) { |
| 330 MP_TO_SEC_ERROR(err); | 1105 MP_TO_SEC_ERROR(err); |
| 331 rv = SECFailure; | 1106 rv = SECFailure; |
| 332 } | 1107 } |
| 333 return rv; | 1108 return rv; |
| 334 } | 1109 } |
| 335 | 1110 |
| 336 SECStatus | 1111 /* |
| 337 PQG_ParamGen(unsigned int j, PQGParams **pParams, PQGVerify **pVfy) | 1112 ** Generate G from seed, index, P, and Q. |
| 1113 */ | |
| 1114 static SECStatus | |
| 1115 makeGfromIndex(HASH_HashType hashtype, | |
| 1116 » » const mp_int *P,» /* input. */ | |
| 1117 » const mp_int *Q,» /* input. */ | |
| 1118 const SECItem *seed,» /* input. */ | |
| 1119 » » unsigned char index,» /* input. */ | |
| 1120 » » mp_int *G)» » /* input/output */ | |
| 338 { | 1121 { |
| 339 unsigned int L; /* Length of P in bits. Per FIPS 186. */ | 1122 mp_int e, pm1, W; |
| 340 unsigned int seedBytes; | 1123 unsigned int count; |
| 1124 unsigned char data[HASH_LENGTH_MAX]; | |
| 1125 unsigned int len; | |
| 1126 mp_err err = MP_OKAY; | |
| 1127 SECStatus rv = SECSuccess; | |
| 1128 const SECHashObject *hashobj; | |
| 1129 void *hashcx = NULL; | |
| 341 | 1130 |
| 342 if (j > 8 || !pParams || !pVfy) { | 1131 MP_DIGITS(&e) = 0; |
| 343 » PORT_SetError(SEC_ERROR_INVALID_ARGS); | 1132 MP_DIGITS(&pm1) = 0; |
| 344 return SECFailure; | 1133 MP_DIGITS(&W) = 0; |
| 1134 CHECK_MPI_OK( mp_init(&e) ); | |
| 1135 CHECK_MPI_OK( mp_init(&pm1) ); | |
| 1136 CHECK_MPI_OK( mp_init(&W) ); | |
| 1137 | |
| 1138 /* initialize our hash stuff */ | |
| 1139 hashobj = HASH_GetRawHashObject(hashtype); | |
| 1140 if (hashobj == NULL) { | |
| 1141 » /* shouldn't happen */ | |
| 1142 » PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); | |
| 1143 » rv = SECFailure; | |
| 1144 » goto cleanup; | |
| 345 } | 1145 } |
| 346 L = 512 + (j * 64); /* bits in P */ | 1146 hashcx = hashobj->create(); |
| 347 seedBytes = L/8; | 1147 if (hashcx == NULL) { |
| 348 return PQG_ParamGenSeedLen(j, seedBytes, pParams, pVfy); | 1148 » rv = SECFailure; |
| 1149 » goto cleanup; | |
| 1150 } | |
| 1151 | |
| 1152 CHECK_MPI_OK( mp_sub_d(P, 1, &pm1) ); /* P - 1 */ | |
| 1153 /* Step 3 e = (p-1)/q */ | |
| 1154 CHECK_MPI_OK( mp_div(&pm1, Q, &e, NULL) ); /* e = (P-1)/Q */ | |
| 1155 /* Steps 4, 5, and 6 */ | |
| 1156 /* count is a 16 bit value in the spec. We actually represent count | |
| 1157 * as more than 16 bits so we can easily detect the 16 bit overflow */ | |
| 1158 #define MAX_COUNT 0x10000 | |
| 1159 for (count = 1; count < MAX_COUNT; count++) { | |
| 1160 » /* step 7 | |
| 1161 » * U = domain_param_seed || "ggen" || index || count | |
| 1162 * step 8 | |
| 1163 » * W = HASH(U) | |
| 1164 » */ | |
| 1165 » hashobj->begin(hashcx); | |
| 1166 » hashobj->update(hashcx,seed->data,seed->len); | |
| 1167 » hashobj->update(hashcx, (unsigned char *)"ggen", 4); | |
| 1168 » hashobj->update(hashcx,&index, 1); | |
| 1169 » data[0] = (count >> 8) & 0xff; | |
| 1170 » data[1] = count & 0xff; | |
| 1171 » hashobj->update(hashcx, data, 2); | |
| 1172 » hashobj->end(hashcx, data, &len, sizeof(data)); | |
| 1173 » OCTETS_TO_MPINT(data, &W, len); | |
| 1174 » /* step 9. g = W**e mod p */ | |
| 1175 » CHECK_MPI_OK( mp_exptmod(&W, &e, P, G) ); | |
| 1176 » /* step 10. if (g < 2) then goto step 5 */ | |
| 1177 » /* NOTE: this weird construct is to keep the flow according to the spec. | |
| 1178 » * the continue puts us back to step 5 of the for loop */ | |
| 1179 » if (mp_cmp_d(G, 2) < 0) { | |
| 1180 » continue; | |
| 1181 » } | |
| 1182 » break; /* step 11 follows step 10 if the test condition is false */ | |
| 1183 } | |
| 1184 if (count >= MAX_COUNT) { | |
| 1185 » rv = SECFailure; /* last part of step 6 */ | |
| 1186 } | |
| 1187 /* step 11. | |
| 1188 * return valid G */ | |
| 1189 cleanup: | |
| 1190 PORT_Memset(data, 0, sizeof(data)); | |
| 1191 if (hashcx) { | |
| 1192 » hashobj->destroy(hashcx, PR_TRUE); | |
| 1193 } | |
| 1194 mp_clear(&e); | |
| 1195 mp_clear(&pm1); | |
| 1196 mp_clear(&W); | |
| 1197 if (err) { | |
| 1198 » MP_TO_SEC_ERROR(err); | |
| 1199 » rv = SECFailure; | |
| 1200 } | |
| 1201 return rv; | |
| 349 } | 1202 } |
| 350 | 1203 |
| 351 /* This code uses labels and gotos, so that it can follow the numbered | 1204 /* This code uses labels and gotos, so that it can follow the numbered |
| 352 ** steps in the algorithms from FIPS 186 appendix 2.2 very closely, | 1205 ** steps in the algorithms from FIPS 186-3 appendix A.1.1.2 very closely, |
| 353 ** and so that the correctness of this code can be easily verified. | 1206 ** and so that the correctness of this code can be easily verified. |
| 354 ** So, please forgive the ugly c code. | 1207 ** So, please forgive the ugly c code. |
| 355 **/ | 1208 **/ |
| 356 SECStatus | 1209 static SECStatus |
| 357 PQG_ParamGenSeedLen(unsigned int j, unsigned int seedBytes, | 1210 pqg_ParamGen(unsigned int L, unsigned int N, pqgGenType type, |
| 358 PQGParams **pParams, PQGVerify **pVfy) | 1211 » unsigned int seedBytes, PQGParams **pParams, PQGVerify **pVfy) |
| 359 { | 1212 { |
| 360 unsigned int L; /* Length of P in bits. Per FIPS 186. */ | 1213 unsigned int n; /* Per FIPS 186, app 2.2. 186-3 app A.1.1.2 */ |
| 361 unsigned int n; /* Per FIPS 186, appendix 2.2. */ | 1214 unsigned int b; /* Per FIPS 186, app 2.2. 186-3 app A.1.1.2 */ |
| 362 unsigned int b; /* Per FIPS 186, appendix 2.2. */ | 1215 unsigned int seedlen; /* Per FIPS 186-3 app A.1.1.2 (was 'g' 186-1)*/ |
| 363 unsigned int g; /* Per FIPS 186, appendix 2.2. */ | 1216 unsigned int counter; /* Per FIPS 186, app 2.2. 186-3 app A.1.1.2 */ |
| 364 unsigned int counter; /* Per FIPS 186, appendix 2.2. */ | 1217 unsigned int offset; /* Per FIPS 186, app 2.2. 186-3 app A.1.1.2 */ |
| 365 unsigned int offset; /* Per FIPS 186, appendix 2.2. */ | 1218 unsigned int outlen; /* Per FIPS 186-3, appendix A.1.1.2. */ |
| 366 SECItem *seed; /* Per FIPS 186, appendix 2.2. */ | 1219 unsigned int maxCount; |
| 1220 HASH_HashType hashtype; | |
| 1221 SECItem *seed; /* Per FIPS 186, app 2.2. 186-3 app A.1.1.2 */ | |
| 367 PRArenaPool *arena = NULL; | 1222 PRArenaPool *arena = NULL; |
| 368 PQGParams *params = NULL; | 1223 PQGParams *params = NULL; |
| 369 PQGVerify *verify = NULL; | 1224 PQGVerify *verify = NULL; |
| 370 PRBool passed; | 1225 PRBool passed; |
| 371 SECItem hit = { 0, 0, 0 }; | 1226 SECItem hit = { 0, 0, 0 }; |
| 372 mp_int P, Q, G, H, l; | 1227 mp_int P, Q, G, H, l; |
| 373 mp_err err = MP_OKAY; | 1228 mp_err err = MP_OKAY; |
| 374 SECStatus rv = SECFailure; | 1229 SECStatus rv = SECFailure; |
| 375 int iterations = 0; | 1230 int iterations = 0; |
| 376 if (j > 8 || seedBytes < 20 || !pParams || !pVfy) { | 1231 |
| 1232 | |
| 1233 /* Step 1. L and N already checked by caller*/ | |
| 1234 /* Step 2. if (seedlen < N) return INVALID; */ | |
| 1235 if (seedBytes < N/BITS_PER_BYTE || !pParams || !pVfy) { | |
| 377 PORT_SetError(SEC_ERROR_INVALID_ARGS); | 1236 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
| 378 return SECFailure; | 1237 return SECFailure; |
| 379 } | 1238 } |
| 380 /* Initialize an arena for the params. */ | 1239 /* Initialize an arena for the params. */ |
| 381 arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE); | 1240 arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE); |
| 382 if (!arena) { | 1241 if (!arena) { |
| 383 PORT_SetError(SEC_ERROR_NO_MEMORY); | 1242 PORT_SetError(SEC_ERROR_NO_MEMORY); |
| 384 return SECFailure; | 1243 return SECFailure; |
| 385 } | 1244 } |
| 386 params = (PQGParams *)PORT_ArenaZAlloc(arena, sizeof(PQGParams)); | 1245 params = (PQGParams *)PORT_ArenaZAlloc(arena, sizeof(PQGParams)); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 411 MP_DIGITS(&P) = 0; | 1270 MP_DIGITS(&P) = 0; |
| 412 MP_DIGITS(&Q) = 0; | 1271 MP_DIGITS(&Q) = 0; |
| 413 MP_DIGITS(&G) = 0; | 1272 MP_DIGITS(&G) = 0; |
| 414 MP_DIGITS(&H) = 0; | 1273 MP_DIGITS(&H) = 0; |
| 415 MP_DIGITS(&l) = 0; | 1274 MP_DIGITS(&l) = 0; |
| 416 CHECK_MPI_OK( mp_init(&P) ); | 1275 CHECK_MPI_OK( mp_init(&P) ); |
| 417 CHECK_MPI_OK( mp_init(&Q) ); | 1276 CHECK_MPI_OK( mp_init(&Q) ); |
| 418 CHECK_MPI_OK( mp_init(&G) ); | 1277 CHECK_MPI_OK( mp_init(&G) ); |
| 419 CHECK_MPI_OK( mp_init(&H) ); | 1278 CHECK_MPI_OK( mp_init(&H) ); |
| 420 CHECK_MPI_OK( mp_init(&l) ); | 1279 CHECK_MPI_OK( mp_init(&l) ); |
| 421 /* Compute lengths. */ | 1280 |
| 422 L = 512 + (j * 64); /* bits in P */ | 1281 /* Select Hash and Compute lengths. */ |
| 423 n = (L - 1) / BITS_IN_Q; /* BITS_IN_Q is 160 */ | 1282 /* getFirstHash gives us the smallest acceptable hash for this key |
| 424 b = (L - 1) % BITS_IN_Q; | 1283 * strength */ |
| 425 g = seedBytes * BITS_PER_BYTE; /* bits in seed, NOT G of PQG. */ | 1284 hashtype = getFirstHash(L,N); |
| 426 step_1: | 1285 outlen = HASH_ResultLen(hashtype)*BITS_PER_BYTE; |
| 1286 | |
| 1287 /* Step 3: n = Ceil(L/outlen)-1; (same as n = Floor((L-1)/outlen)) */ | |
| 1288 n = (L - 1) / outlen; | |
| 1289 /* Step 4: b = L -1 - (n*outlen); (same as n = (L-1) mod outlen) */ | |
| 1290 b = (L - 1) % outlen; | |
| 1291 seedlen = seedBytes * BITS_PER_BYTE; /* bits in seed */ | |
| 1292 step_5: | |
| 427 /* ****************************************************************** | 1293 /* ****************************************************************** |
| 428 ** Step 1. | 1294 ** Step 5. (Step 1 in 186-1) |
| 429 ** "Choose an abitrary sequence of at least 160 bits and call it SEED. | 1295 ** "Choose an abitrary sequence of at least N bits and call it SEED. |
| 430 ** Let g be the length of SEED in bits." | 1296 ** Let g be the length of SEED in bits." |
| 431 */ | 1297 */ |
| 432 if (++iterations > MAX_ITERATIONS) { /* give up after a while */ | 1298 if (++iterations > MAX_ITERATIONS) { /* give up after a while */ |
| 433 PORT_SetError(SEC_ERROR_NEED_RANDOM); | 1299 PORT_SetError(SEC_ERROR_NEED_RANDOM); |
| 434 goto cleanup; | 1300 goto cleanup; |
| 435 } | 1301 } |
| 436 seed->len = seedBytes; | 1302 seed->len = seedBytes; |
| 437 CHECK_SEC_OK( getPQseed(seed, verify->arena) ); | 1303 CHECK_SEC_OK( getPQseed(seed, verify->arena) ); |
| 438 /* ****************************************************************** | 1304 /* ****************************************************************** |
| 439 ** Step 2. | 1305 ** Step 6. (Step 2 in 186-1) |
| 440 ** "Compute U = SHA[SEED] XOR SHA[(SEED+1) mod 2**g]." | |
| 441 ** | 1306 ** |
| 442 ** Step 3. | 1307 ** "Compute U = SHA[SEED] XOR SHA[(SEED+1) mod 2**g]. (186-1)" |
| 1308 ** "Compute U = HASH[SEED] 2**(N-1). (186-3)" | |
| 1309 ** | |
| 1310 ** Step 7. (Step 3 in 186-1) | |
| 443 ** "Form Q from U by setting the most signficant bit (the 2**159 bit) | 1311 ** "Form Q from U by setting the most signficant bit (the 2**159 bit) |
| 444 ** and the least signficant bit to 1. In terms of boolean operations, | 1312 ** and the least signficant bit to 1. In terms of boolean operations, |
| 445 ** Q = U OR 2**159 OR 1. Note that 2**159 < Q < 2**160." | 1313 ** Q = U OR 2**159 OR 1. Note that 2**159 < Q < 2**160. (186-1)" |
| 1314 ** | |
| 1315 ** "q = 2**(N-1) + U + 1 - (U mod 2) (186-3) | |
| 1316 ** | |
| 1317 ** Note: Both formulations are the same for U < 2**(N-1) and N=160 | |
| 446 */ | 1318 */ |
| 447 CHECK_SEC_OK( makeQfromSeed(g, seed, &Q) ); | 1319 if (type == FIPS186_1_TYPE) { |
| 1320 » CHECK_SEC_OK( makeQfromSeed(seedlen, seed, &Q) ); | |
| 1321 } else { | |
| 1322 » CHECK_SEC_OK( makeQ2fromSeed(hashtype, N, seed, &Q) ); | |
| 1323 } | |
| 448 /* ****************************************************************** | 1324 /* ****************************************************************** |
| 449 ** Step 4. | 1325 ** Step 8. (Step 4 in 186-1) |
| 450 ** "Use a robust primality testing algorithm to test whether q is prime." | 1326 ** "Use a robust primality testing algorithm to test whether q is prime." |
| 451 ** | 1327 ** |
| 452 ** Appendix 2.1 states that a Rabin test with at least 50 iterations | 1328 ** Appendix 2.1 states that a Rabin test with at least 50 iterations |
| 453 ** "will give an acceptable probability of error." | 1329 ** "will give an acceptable probability of error." |
| 454 */ | 1330 */ |
| 455 /*CHECK_SEC_OK( prm_RabinTest(&Q, &passed) );*/ | 1331 /*CHECK_SEC_OK( prm_RabinTest(&Q, &passed) );*/ |
| 456 err = mpp_pprime(&Q, PQG_Q_PRIMALITY_TESTS); | 1332 err = mpp_pprime(&Q, prime_testcount_q(L,N)); |
| 457 passed = (err == MP_YES) ? SECSuccess : SECFailure; | 1333 passed = (err == MP_YES) ? SECSuccess : SECFailure; |
| 458 /* ****************************************************************** | 1334 /* ****************************************************************** |
| 459 ** Step 5. "If q is not prime, goto step 1." | 1335 ** Step 9. (Step 5 in 186-1) "If q is not prime, goto step 5 (1 in 186-1)." |
| 460 */ | 1336 */ |
| 461 if (passed != SECSuccess) | 1337 if (passed != SECSuccess) |
| 462 goto step_1; | 1338 goto step_5; |
| 463 /* ****************************************************************** | 1339 /* ****************************************************************** |
| 464 ** Step 6. "Let counter = 0 and offset = 2." | 1340 ** Step 10. |
| 1341 ** offset = 1; | |
| 1342 **( Step 6b 186-1)"Let counter = 0 and offset = 2." | |
| 465 */ | 1343 */ |
| 466 counter = 0; | 1344 offset = (type == FIPS186_1_TYPE) ? 2 : 1; |
| 467 offset = 2; | 1345 /* |
| 468 step_7: | 1346 ** Step 11. (Step 6a,13a,14 in 186-1) |
| 1347 ** For counter - 0 to (4L-1) do | |
| 1348 ** | |
| 1349 */ | |
| 1350 maxCount = L >= 1024 ? (4*L - 1) : 4095; | |
| 1351 for (counter = 0; counter <= maxCount; counter++) { | |
| 1352 » /* ****************************************************************** | |
| 1353 » ** Step 11.1 (Step 7 in 186-1) | |
| 1354 » ** "for j = 0 ... n let | |
| 1355 » ** V_j = HASH[(SEED + offset + j) mod 2**seedlen]." | |
| 1356 » ** | |
| 1357 » ** Step 11.2 (Step 8 in 186-1) | |
| 1358 » ** "W = V_0 + V_1*2**outlen+...+ V_n-1 * 2**((n-1)*outlen) + | |
| 1359 » ** ((Vn* mod 2**b)*2**(n*outlen))" | |
| 1360 » ** Step 11.3 (Step 8 in 186-1) | |
| 1361 » ** "X = W + 2**(L-1) | |
| 1362 » ** Note that 0 <= W < 2**(L-1) and hence 2**(L-1) <= X < 2**L." | |
| 1363 » ** | |
| 1364 » ** Step 11.4 (Step 9 in 186-1). | |
| 1365 » ** "c = X mod 2q" | |
| 1366 » ** | |
| 1367 » ** Step 11.5 (Step 9 in 186-1). | |
| 1368 » ** " p = X - (c - 1). | |
| 1369 » ** Note that p is congruent to 1 mod 2q." | |
| 1370 » */ | |
| 1371 » CHECK_SEC_OK( makePfromQandSeed(hashtype, L, N, offset, seedlen, | |
| 1372 » » » » » seed, &Q, &P) ); | |
| 1373 » /************************************************************* | |
| 1374 » ** Step 11.6. (Step 10 in 186-1) | |
| 1375 » ** "if p < 2**(L-1), then goto step 11.9. (step 13 in 186-1)" | |
| 1376 » */ | |
| 1377 » CHECK_MPI_OK( mpl_set_bit(&l, (mp_size)(L-1), 1) ); /* l = 2**(L-1) */ | |
| 1378 » if (mp_cmp(&P, &l) < 0) | |
| 1379 goto step_11_9; | |
| 1380 » /************************************************************ | |
| 1381 » ** Step 11.7 (step 11 in 186-1) | |
| 1382 » ** "Perform a robust primality test on p." | |
| 1383 » */ | |
| 1384 » /*CHECK_SEC_OK( prm_RabinTest(&P, &passed) );*/ | |
| 1385 » err = mpp_pprime(&P, prime_testcount_p(L, N)); | |
| 1386 » passed = (err == MP_YES) ? SECSuccess : SECFailure; | |
| 1387 » /* ****************************************************************** | |
| 1388 » ** Step 11.8. "If p is determined to be primed return VALID | |
| 1389 ** values of p, q, seed and counter." | |
| 1390 » */ | |
| 1391 » if (passed == SECSuccess) | |
| 1392 » break; | |
| 1393 step_11_9: | |
| 1394 » /* ****************************************************************** | |
| 1395 » ** Step 11.9. "offset = offset + n + 1." | |
| 1396 » */ | |
| 1397 » offset += n + 1; | |
| 1398 } | |
| 469 /* ****************************************************************** | 1399 /* ****************************************************************** |
| 470 ** Step 7. | 1400 ** Step 12. "goto step 5." |
| 471 ** "for k = 0 ... n let | |
| 472 ** V_k = SHA[(SEED + offset + k) mod 2**g]." | |
| 473 ** | 1401 ** |
| 474 ** Step 8. | 1402 ** NOTE: if counter <= maxCount, then we exited the loop at Step 11.8 |
| 475 ** "Let W be the sum of (V_k * 2**(k*160)) for k = 0 ... n | 1403 ** and now need to return p,q, seed, and counter. |
| 476 ** and let X = W + 2**(L-1). | |
| 477 ** Note that 0 <= W < 2**(L-1) and hence 2**(L-1) <= X < 2**L." | |
| 478 ** | |
| 479 ** Step 9. | |
| 480 ** "Let c = X mod 2q and set p = X - (c - 1). | |
| 481 ** Note that p is congruent to 1 mod 2q." | |
| 482 */ | 1404 */ |
| 483 CHECK_SEC_OK( makePfromQandSeed(L, offset, g, seed, &Q, &P) ); | 1405 if (counter > maxCount) |
| 484 /************************************************************* | 1406 » goto step_5; |
| 485 ** Step 10. | 1407 /* ****************************************************************** |
| 486 ** "if p < 2**(L-1), then goto step 13." | 1408 ** returning p, q, seed and counter |
| 487 */ | 1409 */ |
| 488 CHECK_MPI_OK( mpl_set_bit(&l, (mp_size)(L-1), 1) ); /* l = 2**(L-1) */ | 1410 if (type == FIPS186_1_TYPE) { |
| 489 if (mp_cmp(&P, &l) < 0) | 1411 » /* Generate g, This is called the "Unverifiable Generation of g |
| 490 goto step_13; | 1412 » * in FIPA186-3 Appedix A.2.1. For compatibility we maintain |
| 491 /************************************************************ | 1413 » * this version of the code */ |
| 492 ** Step 11. | 1414 » SECITEM_AllocItem(NULL, &hit, L/8); /* h is no longer than p */ |
| 493 ** "Perform a robust primality test on p." | 1415 » if (!hit.data) goto cleanup; |
| 494 */ | 1416 » do { |
| 495 /*CHECK_SEC_OK( prm_RabinTest(&P, &passed) );*/ | 1417 » /* loop generate h until 1<h<p-1 and (h**[(p-1)/q])mod p > 1 */ |
| 496 err = mpp_pprime(&P, PQG_P_PRIMALITY_TESTS); | 1418 » CHECK_SEC_OK( generate_h_candidate(&hit, &H) ); |
| 497 passed = (err == MP_YES) ? SECSuccess : SECFailure; | 1419 CHECK_SEC_OK( makeGfromH(&P, &Q, &H, &G, &passed) ); |
| 498 /* ****************************************************************** | 1420 » } while (passed != PR_TRUE); |
| 499 ** Step 12. "If p passes the test performed in step 11, go to step 15." | 1421 MPINT_TO_SECITEM(&H, &verify->h, verify->arena); |
| 500 */ | 1422 } else { |
| 501 if (passed == SECSuccess) | 1423 » unsigned char index = 1; /* default to 1 */ |
| 502 goto step_15; | 1424 » verify->h.data = (unsigned char *)PORT_ArenaZAlloc(verify->arena, 1); |
| 503 step_13: | 1425 » if (verify->h.data == NULL) { goto cleanup; } |
| 504 /* ****************************************************************** | 1426 » verify->h.len = 1; |
| 505 ** Step 13. "Let counter = counter + 1 and offset = offset + n + 1." | 1427 » verify->h.data[0] = index; |
| 506 */ | 1428 » /* Generate g, using the FIPS 186-3 Appendix A.23 */ |
| 507 counter++; | 1429 » CHECK_SEC_OK(makeGfromIndex(hashtype, &P, &Q, seed, index, &G) ); |
| 508 offset += n + 1; | 1430 } |
| 509 /* ****************************************************************** | |
| 510 ** Step 14. "If counter >= 4096 goto step 1, otherwise go to step 7." | |
| 511 */ | |
| 512 if (counter >= 4096) | |
| 513 goto step_1; | |
| 514 goto step_7; | |
| 515 step_15: | |
| 516 /* ****************************************************************** | |
| 517 ** Step 15. | |
| 518 ** "Save the value of SEED and the value of counter for use | |
| 519 ** in certifying the proper generation of p and q." | |
| 520 */ | |
| 521 /* Generate h. */ | |
| 522 SECITEM_AllocItem(NULL, &hit, L/8); /* h is no longer than p */ | |
| 523 if (!hit.data) goto cleanup; | |
| 524 do { | |
| 525 » /* loop generate h until 1<h<p-1 and (h**[(p-1)/q])mod p > 1 */ | |
| 526 » CHECK_SEC_OK( generate_h_candidate(&hit, &H) ); | |
| 527 CHECK_SEC_OK( makeGfromH(&P, &Q, &H, &G, &passed) ); | |
| 528 } while (passed != PR_TRUE); | |
| 529 /* All generation is done. Now, save the PQG params. */ | 1431 /* All generation is done. Now, save the PQG params. */ |
| 530 MPINT_TO_SECITEM(&P, ¶ms->prime, params->arena); | 1432 MPINT_TO_SECITEM(&P, ¶ms->prime, params->arena); |
| 531 MPINT_TO_SECITEM(&Q, ¶ms->subPrime, params->arena); | 1433 MPINT_TO_SECITEM(&Q, ¶ms->subPrime, params->arena); |
| 532 MPINT_TO_SECITEM(&G, ¶ms->base, params->arena); | 1434 MPINT_TO_SECITEM(&G, ¶ms->base, params->arena); |
| 533 MPINT_TO_SECITEM(&H, &verify->h, verify->arena); | |
| 534 verify->counter = counter; | 1435 verify->counter = counter; |
| 535 *pParams = params; | 1436 *pParams = params; |
| 536 *pVfy = verify; | 1437 *pVfy = verify; |
| 537 cleanup: | 1438 cleanup: |
| 538 mp_clear(&P); | 1439 mp_clear(&P); |
| 539 mp_clear(&Q); | 1440 mp_clear(&Q); |
| 540 mp_clear(&G); | 1441 mp_clear(&G); |
| 541 mp_clear(&H); | 1442 mp_clear(&H); |
| 542 mp_clear(&l); | 1443 mp_clear(&l); |
| 543 if (err) { | 1444 if (err) { |
| 544 MP_TO_SEC_ERROR(err); | 1445 MP_TO_SEC_ERROR(err); |
| 545 rv = SECFailure; | 1446 rv = SECFailure; |
| 546 } | 1447 } |
| 547 if (rv) { | 1448 if (rv) { |
| 548 PORT_FreeArena(params->arena, PR_TRUE); | 1449 PORT_FreeArena(params->arena, PR_TRUE); |
| 549 PORT_FreeArena(verify->arena, PR_TRUE); | 1450 PORT_FreeArena(verify->arena, PR_TRUE); |
| 550 } | 1451 } |
| 551 if (hit.data) { | 1452 if (hit.data) { |
| 552 SECITEM_FreeItem(&hit, PR_FALSE); | 1453 SECITEM_FreeItem(&hit, PR_FALSE); |
| 553 } | 1454 } |
| 554 return rv; | 1455 return rv; |
| 555 } | 1456 } |
| 556 | 1457 |
| 1458 SECStatus | |
| 1459 PQG_ParamGen(unsigned int j, PQGParams **pParams, PQGVerify **pVfy) | |
| 1460 { | |
| 1461 unsigned int L; /* Length of P in bits. Per FIPS 186. */ | |
| 1462 unsigned int seedBytes; | |
| 1463 | |
| 1464 if (j > 8 || !pParams || !pVfy) { | |
| 1465 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 1466 return SECFailure; | |
| 1467 } | |
| 1468 L = 512 + (j * 64); /* bits in P */ | |
| 1469 seedBytes = L/8; | |
| 1470 return pqg_ParamGen(L, DSA1_Q_BITS, FIPS186_1_TYPE, seedBytes, | |
| 1471 pParams, pVfy); | |
| 1472 } | |
| 1473 | |
| 1474 SECStatus | |
| 1475 PQG_ParamGenSeedLen(unsigned int j, unsigned int seedBytes, | |
| 1476 PQGParams **pParams, PQGVerify **pVfy) | |
| 1477 { | |
| 1478 unsigned int L; /* Length of P in bits. Per FIPS 186. */ | |
| 1479 | |
| 1480 if (j > 8 || !pParams || !pVfy) { | |
| 1481 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 1482 return SECFailure; | |
| 1483 } | |
| 1484 L = 512 + (j * 64); /* bits in P */ | |
| 1485 return pqg_ParamGen(L, DSA1_Q_BITS, FIPS186_1_TYPE, seedBytes, | |
| 1486 pParams, pVfy); | |
| 1487 } | |
| 1488 | |
| 1489 SECStatus | |
| 1490 PQG_ParamGenV2(unsigned int L, unsigned int N, unsigned int seedBytes, | |
| 1491 PQGParams **pParams, PQGVerify **pVfy) | |
| 1492 { | |
| 1493 if (pqg_validate_dsa2(L,N) != SECSuccess) { | |
| 1494 /* error code already set */ | |
| 1495 return SECFailure; | |
| 1496 } | |
| 1497 return pqg_ParamGen(L, N, FIPS186_3_TYPE, seedBytes, pParams, pVfy); | |
| 1498 } | |
| 1499 | |
| 1500 | |
| 1501 /* | |
| 1502 * verify can use vfy structures returned from either FIPS186-1 or | |
| 1503 * FIPS186-2, and can handle differences in selected Hash functions to | |
| 1504 * generate the parameters. | |
| 1505 */ | |
| 557 SECStatus | 1506 SECStatus |
| 558 PQG_VerifyParams(const PQGParams *params, | 1507 PQG_VerifyParams(const PQGParams *params, |
| 559 const PQGVerify *vfy, SECStatus *result) | 1508 const PQGVerify *vfy, SECStatus *result) |
| 560 { | 1509 { |
| 561 SECStatus rv = SECSuccess; | 1510 SECStatus rv = SECSuccess; |
| 562 int passed; | 1511 unsigned int g, n, L, N, offset, outlen; |
| 563 unsigned int g, n, L, offset; | 1512 mp_int p0, P, Q, G, P_, Q_, G_, r, h; |
| 564 mp_int P, Q, G, P_, Q_, G_, r, h; | |
| 565 mp_err err = MP_OKAY; | 1513 mp_err err = MP_OKAY; |
| 566 int j; | 1514 int j; |
| 1515 unsigned int counter_max = 0; /* handle legacy L < 1024 */ | |
| 1516 int qseed_len; | |
| 1517 SECItem pseed_ = {0, 0, 0}; | |
| 1518 HASH_HashType hashtype; | |
| 1519 pqgGenType type; | |
| 1520 | |
| 567 #define CHECKPARAM(cond) \ | 1521 #define CHECKPARAM(cond) \ |
| 568 if (!(cond)) { \ | 1522 if (!(cond)) { \ |
| 569 *result = SECFailure; \ | 1523 *result = SECFailure; \ |
| 570 goto cleanup; \ | 1524 goto cleanup; \ |
| 571 } | 1525 } |
| 572 if (!params || !vfy || !result) { | 1526 if (!params || !vfy || !result) { |
| 573 PORT_SetError(SEC_ERROR_INVALID_ARGS); | 1527 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
| 574 return SECFailure; | 1528 return SECFailure; |
| 575 } | 1529 } |
| 1530 /* always need at least p, q, and seed for any meaningful check */ | |
| 1531 if ((params->prime.len == 0) || (params->subPrime.len == 0) || | |
| 1532 (vfy->seed.len == 0)) { | |
| 1533 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 1534 return SECFailure; | |
| 1535 } | |
| 1536 /* we want to either check PQ or G or both. If we don't have G, make | |
| 1537 * sure we have count so we can check P. */ | |
| 1538 if ((params->base.len == 0) && (vfy->counter == -1)) { | |
| 1539 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 1540 return SECFailure; | |
| 1541 } | |
| 1542 | |
| 1543 MP_DIGITS(&p0) = 0; | |
| 576 MP_DIGITS(&P) = 0; | 1544 MP_DIGITS(&P) = 0; |
| 577 MP_DIGITS(&Q) = 0; | 1545 MP_DIGITS(&Q) = 0; |
| 578 MP_DIGITS(&G) = 0; | 1546 MP_DIGITS(&G) = 0; |
| 579 MP_DIGITS(&P_) = 0; | 1547 MP_DIGITS(&P_) = 0; |
| 580 MP_DIGITS(&Q_) = 0; | 1548 MP_DIGITS(&Q_) = 0; |
| 581 MP_DIGITS(&G_) = 0; | 1549 MP_DIGITS(&G_) = 0; |
| 582 MP_DIGITS(&r) = 0; | 1550 MP_DIGITS(&r) = 0; |
| 583 MP_DIGITS(&h) = 0; | 1551 MP_DIGITS(&h) = 0; |
| 1552 CHECK_MPI_OK( mp_init(&p0) ); | |
| 584 CHECK_MPI_OK( mp_init(&P) ); | 1553 CHECK_MPI_OK( mp_init(&P) ); |
| 585 CHECK_MPI_OK( mp_init(&Q) ); | 1554 CHECK_MPI_OK( mp_init(&Q) ); |
| 586 CHECK_MPI_OK( mp_init(&G) ); | 1555 CHECK_MPI_OK( mp_init(&G) ); |
| 587 CHECK_MPI_OK( mp_init(&P_) ); | 1556 CHECK_MPI_OK( mp_init(&P_) ); |
| 588 CHECK_MPI_OK( mp_init(&Q_) ); | 1557 CHECK_MPI_OK( mp_init(&Q_) ); |
| 589 CHECK_MPI_OK( mp_init(&G_) ); | 1558 CHECK_MPI_OK( mp_init(&G_) ); |
| 590 CHECK_MPI_OK( mp_init(&r) ); | 1559 CHECK_MPI_OK( mp_init(&r) ); |
| 591 CHECK_MPI_OK( mp_init(&h) ); | 1560 CHECK_MPI_OK( mp_init(&h) ); |
| 592 *result = SECSuccess; | 1561 *result = SECSuccess; |
| 593 SECITEM_TO_MPINT(params->prime, &P); | 1562 SECITEM_TO_MPINT(params->prime, &P); |
| 594 SECITEM_TO_MPINT(params->subPrime, &Q); | 1563 SECITEM_TO_MPINT(params->subPrime, &Q); |
| 595 SECITEM_TO_MPINT(params->base, &G); | 1564 /* if G isn't specified, just check P and Q */ |
| 596 /* 1. Q is 160 bits long. */ | 1565 if (params->base.len != 0) { |
| 597 CHECKPARAM( mpl_significant_bits(&Q) == 160 ); | 1566 » SECITEM_TO_MPINT(params->base, &G); |
| 598 /* 2. P is one of the 9 valid lengths. */ | 1567 } |
| 1568 /* 1. Check (L,N) pair */ | |
| 1569 N = mpl_significant_bits(&Q); | |
| 599 L = mpl_significant_bits(&P); | 1570 L = mpl_significant_bits(&P); |
| 600 j = PQG_PBITS_TO_INDEX(L); | 1571 if (L < 1024) { |
| 601 CHECKPARAM( j >= 0 && j <= 8 ); | 1572 » /* handle DSA1 pqg parameters with less thatn 1024 bits*/ |
| 1573 » CHECKPARAM( N == DSA1_Q_BITS ); | |
| 1574 » j = PQG_PBITS_TO_INDEX(L); | |
| 1575 » CHECKPARAM( j >= 0 && j <= 8 ); | |
| 1576 » counter_max = 4096; | |
| 1577 } else { | |
| 1578 » /* handle DSA2 parameters (includes DSA1, 1024 bits) */ | |
| 1579 » CHECKPARAM(pqg_validate_dsa2(L, N) == SECSuccess); | |
| 1580 » counter_max = 4*L; | |
| 1581 } | |
| 602 /* 3. G < P */ | 1582 /* 3. G < P */ |
| 603 CHECKPARAM( mp_cmp(&G, &P) < 0 ); | 1583 if (params->base.len != 0) { |
| 1584 » CHECKPARAM( mp_cmp(&G, &P) < 0 ); | |
| 1585 } | |
| 604 /* 4. P % Q == 1 */ | 1586 /* 4. P % Q == 1 */ |
| 605 CHECK_MPI_OK( mp_mod(&P, &Q, &r) ); | 1587 CHECK_MPI_OK( mp_mod(&P, &Q, &r) ); |
| 606 CHECKPARAM( mp_cmp_d(&r, 1) == 0 ); | 1588 CHECKPARAM( mp_cmp_d(&r, 1) == 0 ); |
| 607 /* 5. Q is prime */ | 1589 /* 5. Q is prime */ |
| 608 CHECKPARAM( mpp_pprime(&Q, PQG_Q_PRIMALITY_TESTS) == MP_YES ); | 1590 CHECKPARAM( mpp_pprime(&Q, prime_testcount_q(L,N)) == MP_YES ); |
| 609 /* 6. P is prime */ | 1591 /* 6. P is prime */ |
| 610 CHECKPARAM( mpp_pprime(&P, PQG_P_PRIMALITY_TESTS) == MP_YES ); | 1592 CHECKPARAM( mpp_pprime(&P, prime_testcount_p(L,N)) == MP_YES ); |
| 611 /* Steps 7-12 are done only if the optional PQGVerify is supplied. */ | 1593 /* Steps 7-12 are done only if the optional PQGVerify is supplied. */ |
| 612 /* 7. counter < 4096 */ | 1594 /* continue processing P */ |
| 613 CHECKPARAM( vfy->counter < 4096 ); | 1595 /* 7. counter < 4*L */ |
| 614 /* 8. g >= 160 and g < 2048 (g is length of seed in bits) */ | 1596 CHECKPARAM( (vfy->counter == -1) || (vfy->counter < counter_max) ); |
| 1597 /* 8. g >= N and g < 2*L (g is length of seed in bits) */ | |
| 615 g = vfy->seed.len * 8; | 1598 g = vfy->seed.len * 8; |
| 616 CHECKPARAM( g >= 160 && g < 2048 ); | 1599 CHECKPARAM( g >= N && g < counter_max/2 ); |
| 617 /* 9. Q generated from SEED matches Q in PQGParams. */ | 1600 /* 9. Q generated from SEED matches Q in PQGParams. */ |
| 618 CHECK_SEC_OK( makeQfromSeed(g, &vfy->seed, &Q_) ); | 1601 /* This function checks all possible hash and generation types to |
| 1602 * find a Q_ which matches Q. */ | |
| 1603 CHECKPARAM( findQfromSeed(L, N, g, &vfy->seed, &Q, &Q_, &qseed_len, | |
| 1604 » » » » » &hashtype, &type) == SECSuccess ); | |
| 619 CHECKPARAM( mp_cmp(&Q, &Q_) == 0 ); | 1605 CHECKPARAM( mp_cmp(&Q, &Q_) == 0 ); |
| 620 /* 10. P generated from (L, counter, g, SEED, Q) matches P in PQGParams. */ | 1606 if (type == FIPS186_3_ST_TYPE) { |
| 621 n = (L - 1) / BITS_IN_Q; | 1607 » SECItem qseed = { 0, 0, 0 }; |
| 622 offset = vfy->counter * (n + 1) + 2; | 1608 » SECItem pseed = { 0, 0, 0 }; |
| 623 CHECK_SEC_OK( makePfromQandSeed(L, offset, g, &vfy->seed, &Q, &P_) ); | 1609 » int first_seed_len; |
| 624 CHECKPARAM( mp_cmp(&P, &P_) == 0 ); | 1610 » int pgen_counter = 0; |
| 625 /* Next two are optional: if h == 0 ignore */ | 1611 |
| 626 if (vfy->h.len == 0) goto cleanup; | 1612 » /* extract pseed and qseed from domain_parameter_seed, which is |
| 627 /* 11. 1 < h < P-1 */ | 1613 » * first_seed || pseed || qseed. qseed is first_seed + small_integer |
| 628 SECITEM_TO_MPINT(vfy->h, &h); | 1614 » * pseed is qseed + small_integer. This means most of the time |
| 629 CHECK_MPI_OK( mpl_set_bit(&P, 0, 0) ); /* P is prime, p-1 == zero 1st bit */ | 1615 » * first_seed.len == qseed.len == pseed.len. Rarely qseed.len and/or |
| 630 CHECKPARAM( mp_cmp_d(&h, 1) > 0 && mp_cmp(&h, &P) ); | 1616 » * pseed.len will be one greater than first_seed.len, so we can |
| 1617 » * depend on the fact that | |
| 1618 » * first_seed.len = floor(domain_parameter_seed.len/3). | |
| 1619 » * findQfromSeed returned qseed.len, so we can calculate pseed.len as | |
| 1620 » * pseed.len = domain_parameter_seed.len - first_seed.len - qseed.len | |
| 1621 » * this is probably over kill, since 99.999% of the time they will all | |
| 1622 » * be equal. | |
| 1623 » * | |
| 1624 » * With the lengths, we can now find the offsets; | |
| 1625 » * first_seed.data = domain_parameter_seed.data + 0 | |
| 1626 » * pseed.data = domain_parameter_seed.data + first_seed.len | |
| 1627 » * qseed.data = domain_parameter_seed.data | |
| 1628 » * + domain_paramter_seed.len - qseed.len | |
| 1629 » * | |
| 1630 » */ | |
| 1631 » first_seed_len = vfy->seed.len/3; | |
| 1632 » CHECKPARAM(qseed_len < vfy->seed.len); | |
| 1633 » CHECKPARAM(first_seed_len*8 > N-1); | |
| 1634 » CHECKPARAM(first_seed_len+qseed_len < vfy->seed.len); | |
| 1635 » qseed.len = qseed_len; | |
| 1636 » qseed.data = vfy->seed.data + vfy->seed.len - qseed.len; | |
| 1637 » pseed.len = vfy->seed.len - (first_seed_len+qseed_len); | |
| 1638 » pseed.data = vfy->seed.data + first_seed_len; | |
| 1639 | |
| 1640 » /* | |
| 1641 » * now complete FIPS 186-3 A.1.2.1.2. Step 1 was completed | |
| 1642 » * above in our initial checks, Step 2 was completed by | |
| 1643 » * findQfromSeed */ | |
| 1644 | |
| 1645 » /* Step 3 (status, c0, prime_seed, prime_gen_counter) = | |
| 1646 » ** (ST_Random_Prime((ceil(length/2)+1, input_seed) | |
| 1647 » */ | |
| 1648 » CHECK_SEC_OK( makePrimefromSeedShaweTaylor(hashtype, (L+1)/2+1, | |
| 1649 » » » &qseed, &p0, &pseed_, &pgen_counter) ); | |
| 1650 » /* Steps 4-22 FIPS 186-3 appendix A.1.2.1.2 */ | |
| 1651 » CHECK_SEC_OK( makePrimefromPrimesShaweTaylor(hashtype, L, | |
| 1652 » » &p0, &Q_, &P_, &pseed_, &pgen_counter) ); | |
| 1653 » CHECKPARAM( mp_cmp(&P, &P_) == 0 ); | |
| 1654 » /* make sure pseed wasn't tampered with (since it is part of | |
| 1655 » * calculating G) */ | |
| 1656 » CHECKPARAM( SECITEM_CompareItem(&pseed, &pseed_) == SECEqual ); | |
| 1657 } else if (vfy->counter == -1) { | |
| 1658 » /* If counter is set to -1, we are really only verifying G, skip | |
| 1659 » * the remainder of the checks for P */ | |
| 1660 » CHECKPARAM(type != FIPS186_1_TYPE); /* we only do this for DSA2 */ | |
| 1661 } else { | |
| 1662 » /* 10. P generated from (L, counter, g, SEED, Q) matches P | |
| 1663 » * in PQGParams. */ | |
| 1664 » outlen = HASH_ResultLen(hashtype)*BITS_PER_BYTE; | |
| 1665 » n = (L - 1) / outlen; | |
| 1666 » offset = vfy->counter * (n + 1) + ((type == FIPS186_1_TYPE) ? 2 : 1); | |
| 1667 » CHECK_SEC_OK( makePfromQandSeed(hashtype, L, N, offset, g, &vfy->seed, | |
| 1668 » » » » » &Q, &P_) ); | |
| 1669 » CHECKPARAM( mp_cmp(&P, &P_) == 0 ); | |
| 1670 } | |
| 1671 | |
| 1672 /* now check G, skip if don't have a g */ | |
| 1673 if (params->base.len == 0) goto cleanup; | |
| 1674 | |
| 1675 /* first Always check that G is OK FIPS186-3 A.2.2 & A.2.4*/ | |
| 1676 /* 1. 2 < G < P-1 */ | |
| 1677 /* P is prime, p-1 == zero 1st bit */ | |
| 1678 CHECK_MPI_OK( mpl_set_bit(&P, 0, 0) ); | |
| 1679 CHECKPARAM( mp_cmp_d(&G, 2) > 0 && mp_cmp(&G, &P) < 0 ); | |
| 631 CHECK_MPI_OK( mpl_set_bit(&P, 0, 1) ); /* set it back */ | 1680 CHECK_MPI_OK( mpl_set_bit(&P, 0, 1) ); /* set it back */ |
| 632 /* 12. G generated from h matches G in PQGParams. */ | 1681 /* 2. verify g**q mod p == 1 */ |
| 633 CHECK_SEC_OK( makeGfromH(&P, &Q, &h, &G_, &passed) ); | 1682 CHECK_MPI_OK( mp_exptmod(&G, &Q, &P, &h) ); /* h = G ** Q mod P */ |
| 634 CHECKPARAM( passed && mp_cmp(&G, &G_) == 0 ); | 1683 CHECKPARAM(mp_cmp_d(&h, 1) == 0); |
| 1684 | |
| 1685 /* no h, the above is the best we can do */ | |
| 1686 if (vfy->h.len == 0) { | |
| 1687 » if (type != FIPS186_1_TYPE) { | |
| 1688 » *result = SECWouldBlock; | |
| 1689 » } | |
| 1690 » goto cleanup; | |
| 1691 } | |
| 1692 | |
| 1693 /* | |
| 1694 * If h is one byte and FIPS186-3 was used to generate Q (we've verified | |
| 1695 * Q was generated from seed already, then we assume that FIPS 186-3 | |
| 1696 * appendix A.2.3 was used to generate G. Otherwise we assume A.2.1 was | |
| 1697 * used to generate G. | |
| 1698 */ | |
| 1699 if ((vfy->h.len == 1) && (type != FIPS186_1_TYPE)) { | |
| 1700 » /* A.2.3 */ | |
| 1701 » CHECK_SEC_OK(makeGfromIndex(hashtype, &P, &Q, &vfy->seed, | |
| 1702 » » » » vfy->h.data[0], &G_) ); | |
| 1703 » CHECKPARAM( mp_cmp(&G, &G_) == 0 ); | |
| 1704 } else { | |
| 1705 » int passed; | |
| 1706 » /* A.2.1 */ | |
| 1707 » SECITEM_TO_MPINT(vfy->h, &h); | |
| 1708 » /* 11. 1 < h < P-1 */ | |
| 1709 » /* P is prime, p-1 == zero 1st bit */ | |
| 1710 » CHECK_MPI_OK( mpl_set_bit(&P, 0, 0) ); | |
| 1711 » CHECKPARAM( mp_cmp_d(&G, 2) > 0 && mp_cmp(&G, &P) ); | |
| 1712 » CHECK_MPI_OK( mpl_set_bit(&P, 0, 1) ); /* set it back */ | |
| 1713 » /* 12. G generated from h matches G in PQGParams. */ | |
| 1714 » CHECK_SEC_OK( makeGfromH(&P, &Q, &h, &G_, &passed) ); | |
| 1715 » CHECKPARAM( passed && mp_cmp(&G, &G_) == 0 ); | |
| 1716 } | |
| 635 cleanup: | 1717 cleanup: |
| 1718 mp_clear(&p0); | |
| 636 mp_clear(&P); | 1719 mp_clear(&P); |
| 637 mp_clear(&Q); | 1720 mp_clear(&Q); |
| 638 mp_clear(&G); | 1721 mp_clear(&G); |
| 639 mp_clear(&P_); | 1722 mp_clear(&P_); |
| 640 mp_clear(&Q_); | 1723 mp_clear(&Q_); |
| 641 mp_clear(&G_); | 1724 mp_clear(&G_); |
| 642 mp_clear(&r); | 1725 mp_clear(&r); |
| 643 mp_clear(&h); | 1726 mp_clear(&h); |
| 1727 if (pseed_.data) { | |
| 1728 SECITEM_FreeItem(&pseed_,PR_FALSE); | |
| 1729 } | |
| 644 if (err) { | 1730 if (err) { |
| 645 MP_TO_SEC_ERROR(err); | 1731 MP_TO_SEC_ERROR(err); |
| 646 rv = SECFailure; | 1732 rv = SECFailure; |
| 647 } | 1733 } |
| 648 return rv; | 1734 return rv; |
| 649 } | 1735 } |
| 650 | 1736 |
| 651 /************************************************************************** | 1737 /************************************************************************** |
| 652 * Free the PQGParams struct and the things it points to. * | 1738 * Free the PQGParams struct and the things it points to. * |
| 653 **************************************************************************/ | 1739 **************************************************************************/ |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 676 if (vfy == NULL) | 1762 if (vfy == NULL) |
| 677 return; | 1763 return; |
| 678 if (vfy->arena != NULL) { | 1764 if (vfy->arena != NULL) { |
| 679 PORT_FreeArena(vfy->arena, PR_FALSE); /* don't zero it */ | 1765 PORT_FreeArena(vfy->arena, PR_FALSE); /* don't zero it */ |
| 680 } else { | 1766 } else { |
| 681 SECITEM_FreeItem(&vfy->seed, PR_FALSE); /* don't free seed */ | 1767 SECITEM_FreeItem(&vfy->seed, PR_FALSE); /* don't free seed */ |
| 682 SECITEM_FreeItem(&vfy->h, PR_FALSE); /* don't free h */ | 1768 SECITEM_FreeItem(&vfy->h, PR_FALSE); /* don't free h */ |
| 683 PORT_Free(vfy); | 1769 PORT_Free(vfy); |
| 684 } | 1770 } |
| 685 } | 1771 } |
| OLD | NEW |