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