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