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