BN_generate_prime.pod 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. =pod
  2. =head1 NAME
  3. BN_generate_prime, BN_is_prime, BN_is_prime_fasttest - generate primes and test for primality
  4. =head1 SYNOPSIS
  5. #include <openssl/bn.h>
  6. BIGNUM *BN_generate_prime(BIGNUM *ret, int num, int safe, BIGNUM *add,
  7. BIGNUM *rem, void (*callback)(int, int, void *), void *cb_arg);
  8. int BN_is_prime(const BIGNUM *a, int checks, void (*callback)(int, int,
  9. void *), BN_CTX *ctx, void *cb_arg);
  10. int BN_is_prime_fasttest(const BIGNUM *a, int checks,
  11. void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg,
  12. int do_trial_division);
  13. =head1 DESCRIPTION
  14. BN_generate_prime() generates a pseudo-random prime number of B<num>
  15. bits.
  16. If B<ret> is not B<NULL>, it will be used to store the number.
  17. If B<callback> is not B<NULL>, it is called as follows:
  18. =over 4
  19. =item *
  20. B<callback(0, i, cb_arg)> is called after generating the i-th
  21. potential prime number.
  22. =item *
  23. While the number is being tested for primality, B<callback(1, j,
  24. cb_arg)> is called as described below.
  25. =item *
  26. When a prime has been found, B<callback(2, i, cb_arg)> is called.
  27. =back
  28. The prime may have to fulfill additional requirements for use in
  29. Diffie-Hellman key exchange:
  30. If B<add> is not B<NULL>, the prime will fulfill the condition p % B<add>
  31. == B<rem> (p % B<add> == 1 if B<rem> == B<NULL>) in order to suit a given
  32. generator.
  33. If B<safe> is true, it will be a safe prime (i.e. a prime p so
  34. that (p-1)/2 is also prime).
  35. The PRNG must be seeded prior to calling BN_generate_prime().
  36. The prime number generation has a negligible error probability.
  37. BN_is_prime() and BN_is_prime_fasttest() test if the number B<a> is
  38. prime. The following tests are performed until one of them shows that
  39. B<a> is composite; if B<a> passes all these tests, it is considered
  40. prime.
  41. BN_is_prime_fasttest(), when called with B<do_trial_division == 1>,
  42. first attempts trial division by a number of small primes;
  43. if no divisors are found by this test and B<callback> is not B<NULL>,
  44. B<callback(1, -1, cb_arg)> is called.
  45. If B<do_trial_division == 0>, this test is skipped.
  46. Both BN_is_prime() and BN_is_prime_fasttest() perform a Miller-Rabin
  47. probabilistic primality test with B<checks> iterations. If
  48. B<checks == BN_prime_checks>, a number of iterations is used that
  49. yields a false positive rate of at most 2^-80 for random input.
  50. If B<callback> is not B<NULL>, B<callback(1, j, cb_arg)> is called
  51. after the j-th iteration (j = 0, 1, ...). B<ctx> is a
  52. pre-allocated B<BN_CTX> (to save the overhead of allocating and
  53. freeing the structure in a loop), or B<NULL>.
  54. =head1 RETURN VALUES
  55. BN_generate_prime() returns the prime number on success, B<NULL> otherwise.
  56. BN_is_prime() returns 0 if the number is composite, 1 if it is
  57. prime with an error probability of less than 0.25^B<checks>, and
  58. -1 on error.
  59. The error codes can be obtained by L<ERR_get_error(3)|ERR_get_error(3)>.
  60. =head1 SEE ALSO
  61. L<bn(3)|bn(3)>, L<ERR_get_error(3)|ERR_get_error(3)>, L<rand(3)|rand(3)>
  62. =head1 HISTORY
  63. The B<cb_arg> arguments to BN_generate_prime() and to BN_is_prime()
  64. were added in SSLeay 0.9.0. The B<ret> argument to BN_generate_prime()
  65. was added in SSLeay 0.9.1.
  66. BN_is_prime_fasttest() was added in OpenSSL 0.9.5.
  67. =cut