diff options
Diffstat (limited to 'moduli.c')
-rw-r--r-- | moduli.c | 52 |
1 files changed, 34 insertions, 18 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: moduli.c,v 1.18 2006/08/03 03:34:42 deraadt Exp $ */ | 1 | /* $OpenBSD: moduli.c,v 1.19 2006/11/06 21:25:28 markus Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright 1994 Phil Karn <karn@qualcomm.com> | 3 | * Copyright 1994 Phil Karn <karn@qualcomm.com> |
4 | * Copyright 1996-1998, 2003 William Allen Simpson <wsimpson@greendragon.com> | 4 | * Copyright 1996-1998, 2003 William Allen Simpson <wsimpson@greendragon.com> |
@@ -327,20 +327,26 @@ gen_candidates(FILE *out, u_int32_t memory, u_int32_t power, BIGNUM *start) | |||
327 | 327 | ||
328 | /* validation check: count the number of primes tried */ | 328 | /* validation check: count the number of primes tried */ |
329 | largetries = 0; | 329 | largetries = 0; |
330 | q = BN_new(); | 330 | if ((q = BN_new()) == NULL) |
331 | fatal("BN_new failed"); | ||
331 | 332 | ||
332 | /* | 333 | /* |
333 | * Generate random starting point for subprime search, or use | 334 | * Generate random starting point for subprime search, or use |
334 | * specified parameter. | 335 | * specified parameter. |
335 | */ | 336 | */ |
336 | largebase = BN_new(); | 337 | if ((largebase = BN_new()) == NULL) |
337 | if (start == NULL) | 338 | fatal("BN_new failed"); |
338 | BN_rand(largebase, power, 1, 1); | 339 | if (start == NULL) { |
339 | else | 340 | if (BN_rand(largebase, power, 1, 1) == 0) |
340 | BN_copy(largebase, start); | 341 | fatal("BN_rand failed"); |
342 | } else { | ||
343 | if (BN_copy(largebase, start) == NULL) | ||
344 | fatal("BN_copy: failed"); | ||
345 | } | ||
341 | 346 | ||
342 | /* ensure odd */ | 347 | /* ensure odd */ |
343 | BN_set_bit(largebase, 0); | 348 | if (BN_set_bit(largebase, 0) == 0) |
349 | fatal("BN_set_bit: failed"); | ||
344 | 350 | ||
345 | time(&time_start); | 351 | time(&time_start); |
346 | 352 | ||
@@ -424,8 +430,10 @@ gen_candidates(FILE *out, u_int32_t memory, u_int32_t power, BIGNUM *start) | |||
424 | continue; /* Definitely composite, skip */ | 430 | continue; /* Definitely composite, skip */ |
425 | 431 | ||
426 | debug2("test q = largebase+%u", 2 * j); | 432 | debug2("test q = largebase+%u", 2 * j); |
427 | BN_set_word(q, 2 * j); | 433 | if (BN_set_word(q, 2 * j) == 0) |
428 | BN_add(q, q, largebase); | 434 | fatal("BN_set_word failed"); |
435 | if (BN_add(q, q, largebase) == 0) | ||
436 | fatal("BN_add failed"); | ||
429 | if (qfileout(out, QTYPE_SOPHIE_GERMAIN, QTEST_SIEVE, | 437 | if (qfileout(out, QTYPE_SOPHIE_GERMAIN, QTEST_SIEVE, |
430 | largetries, (power - 1) /* MSB */, (0), q) == -1) { | 438 | largetries, (power - 1) /* MSB */, (0), q) == -1) { |
431 | ret = -1; | 439 | ret = -1; |
@@ -470,9 +478,12 @@ prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted) | |||
470 | 478 | ||
471 | time(&time_start); | 479 | time(&time_start); |
472 | 480 | ||
473 | p = BN_new(); | 481 | if ((p = BN_new()) == NULL) |
474 | q = BN_new(); | 482 | fatal("BN_new failed"); |
475 | ctx = BN_CTX_new(); | 483 | if ((q = BN_new()) == NULL) |
484 | fatal("BN_new failed"); | ||
485 | if ((ctx = BN_CTX_new()) == NULL) | ||
486 | fatal("BN_CTX_new failed"); | ||
476 | 487 | ||
477 | debug2("%.24s Final %u Miller-Rabin trials (%x generator)", | 488 | debug2("%.24s Final %u Miller-Rabin trials (%x generator)", |
478 | ctime(&time_start), trials, generator_wanted); | 489 | ctime(&time_start), trials, generator_wanted); |
@@ -520,10 +531,13 @@ prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted) | |||
520 | case QTYPE_SOPHIE_GERMAIN: | 531 | case QTYPE_SOPHIE_GERMAIN: |
521 | debug2("%10u: (%u) Sophie-Germain", count_in, in_type); | 532 | debug2("%10u: (%u) Sophie-Germain", count_in, in_type); |
522 | a = q; | 533 | a = q; |
523 | BN_hex2bn(&a, cp); | 534 | if (BN_hex2bn(&a, cp) == 0) |
535 | fatal("BN_hex2bn failed"); | ||
524 | /* p = 2*q + 1 */ | 536 | /* p = 2*q + 1 */ |
525 | BN_lshift(p, q, 1); | 537 | if (BN_lshift(p, q, 1) == 0) |
526 | BN_add_word(p, 1); | 538 | fatal("BN_lshift failed"); |
539 | if (BN_add_word(p, 1) == 0) | ||
540 | fatal("BN_add_word failed"); | ||
527 | in_size += 1; | 541 | in_size += 1; |
528 | generator_known = 0; | 542 | generator_known = 0; |
529 | break; | 543 | break; |
@@ -534,9 +548,11 @@ prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted) | |||
534 | case QTYPE_UNKNOWN: | 548 | case QTYPE_UNKNOWN: |
535 | debug2("%10u: (%u)", count_in, in_type); | 549 | debug2("%10u: (%u)", count_in, in_type); |
536 | a = p; | 550 | a = p; |
537 | BN_hex2bn(&a, cp); | 551 | if (BN_hex2bn(&a, cp) == 0) |
552 | fatal("BN_hex2bn failed"); | ||
538 | /* q = (p-1) / 2 */ | 553 | /* q = (p-1) / 2 */ |
539 | BN_rshift(q, p, 1); | 554 | if (BN_rshift(q, p, 1) == 0) |
555 | fatal("BN_rshift failed"); | ||
540 | break; | 556 | break; |
541 | default: | 557 | default: |
542 | debug2("Unknown prime type"); | 558 | debug2("Unknown prime type"); |