1*2175Sjp161948=pod 2*2175Sjp161948 3*2175Sjp161948=head1 NAME 4*2175Sjp161948 5*2175Sjp161948BN_generate_prime, BN_is_prime, BN_is_prime_fasttest - generate primes and test for primality 6*2175Sjp161948 7*2175Sjp161948=head1 SYNOPSIS 8*2175Sjp161948 9*2175Sjp161948 #include <openssl/bn.h> 10*2175Sjp161948 11*2175Sjp161948 BIGNUM *BN_generate_prime(BIGNUM *ret, int num, int safe, BIGNUM *add, 12*2175Sjp161948 BIGNUM *rem, void (*callback)(int, int, void *), void *cb_arg); 13*2175Sjp161948 14*2175Sjp161948 int BN_is_prime(const BIGNUM *a, int checks, void (*callback)(int, int, 15*2175Sjp161948 void *), BN_CTX *ctx, void *cb_arg); 16*2175Sjp161948 17*2175Sjp161948 int BN_is_prime_fasttest(const BIGNUM *a, int checks, 18*2175Sjp161948 void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg, 19*2175Sjp161948 int do_trial_division); 20*2175Sjp161948 21*2175Sjp161948=head1 DESCRIPTION 22*2175Sjp161948 23*2175Sjp161948BN_generate_prime() generates a pseudo-random prime number of B<num> 24*2175Sjp161948bits. 25*2175Sjp161948If B<ret> is not B<NULL>, it will be used to store the number. 26*2175Sjp161948 27*2175Sjp161948If B<callback> is not B<NULL>, it is called as follows: 28*2175Sjp161948 29*2175Sjp161948=over 4 30*2175Sjp161948 31*2175Sjp161948=item * 32*2175Sjp161948 33*2175Sjp161948B<callback(0, i, cb_arg)> is called after generating the i-th 34*2175Sjp161948potential prime number. 35*2175Sjp161948 36*2175Sjp161948=item * 37*2175Sjp161948 38*2175Sjp161948While the number is being tested for primality, B<callback(1, j, 39*2175Sjp161948cb_arg)> is called as described below. 40*2175Sjp161948 41*2175Sjp161948=item * 42*2175Sjp161948 43*2175Sjp161948When a prime has been found, B<callback(2, i, cb_arg)> is called. 44*2175Sjp161948 45*2175Sjp161948=back 46*2175Sjp161948 47*2175Sjp161948The prime may have to fulfill additional requirements for use in 48*2175Sjp161948Diffie-Hellman key exchange: 49*2175Sjp161948 50*2175Sjp161948If B<add> is not B<NULL>, the prime will fulfill the condition p % B<add> 51*2175Sjp161948== B<rem> (p % B<add> == 1 if B<rem> == B<NULL>) in order to suit a given 52*2175Sjp161948generator. 53*2175Sjp161948 54*2175Sjp161948If B<safe> is true, it will be a safe prime (i.e. a prime p so 55*2175Sjp161948that (p-1)/2 is also prime). 56*2175Sjp161948 57*2175Sjp161948The PRNG must be seeded prior to calling BN_generate_prime(). 58*2175Sjp161948The prime number generation has a negligible error probability. 59*2175Sjp161948 60*2175Sjp161948BN_is_prime() and BN_is_prime_fasttest() test if the number B<a> is 61*2175Sjp161948prime. The following tests are performed until one of them shows that 62*2175Sjp161948B<a> is composite; if B<a> passes all these tests, it is considered 63*2175Sjp161948prime. 64*2175Sjp161948 65*2175Sjp161948BN_is_prime_fasttest(), when called with B<do_trial_division == 1>, 66*2175Sjp161948first attempts trial division by a number of small primes; 67*2175Sjp161948if no divisors are found by this test and B<callback> is not B<NULL>, 68*2175Sjp161948B<callback(1, -1, cb_arg)> is called. 69*2175Sjp161948If B<do_trial_division == 0>, this test is skipped. 70*2175Sjp161948 71*2175Sjp161948Both BN_is_prime() and BN_is_prime_fasttest() perform a Miller-Rabin 72*2175Sjp161948probabilistic primality test with B<checks> iterations. If 73*2175Sjp161948B<checks == BN_prime_checks>, a number of iterations is used that 74*2175Sjp161948yields a false positive rate of at most 2^-80 for random input. 75*2175Sjp161948 76*2175Sjp161948If B<callback> is not B<NULL>, B<callback(1, j, cb_arg)> is called 77*2175Sjp161948after the j-th iteration (j = 0, 1, ...). B<ctx> is a 78*2175Sjp161948pre-allocated B<BN_CTX> (to save the overhead of allocating and 79*2175Sjp161948freeing the structure in a loop), or B<NULL>. 80*2175Sjp161948 81*2175Sjp161948=head1 RETURN VALUES 82*2175Sjp161948 83*2175Sjp161948BN_generate_prime() returns the prime number on success, B<NULL> otherwise. 84*2175Sjp161948 85*2175Sjp161948BN_is_prime() returns 0 if the number is composite, 1 if it is 86*2175Sjp161948prime with an error probability of less than 0.25^B<checks>, and 87*2175Sjp161948-1 on error. 88*2175Sjp161948 89*2175Sjp161948The error codes can be obtained by L<ERR_get_error(3)|ERR_get_error(3)>. 90*2175Sjp161948 91*2175Sjp161948=head1 SEE ALSO 92*2175Sjp161948 93*2175Sjp161948L<bn(3)|bn(3)>, L<ERR_get_error(3)|ERR_get_error(3)>, L<rand(3)|rand(3)> 94*2175Sjp161948 95*2175Sjp161948=head1 HISTORY 96*2175Sjp161948 97*2175Sjp161948The B<cb_arg> arguments to BN_generate_prime() and to BN_is_prime() 98*2175Sjp161948were added in SSLeay 0.9.0. The B<ret> argument to BN_generate_prime() 99*2175Sjp161948was added in SSLeay 0.9.1. 100*2175Sjp161948BN_is_prime_fasttest() was added in OpenSSL 0.9.5. 101*2175Sjp161948 102*2175Sjp161948=cut 103