Add documentation for ASN1_INTEGER_new() and ASN1_INTEGER_free()
[openssl.git] / doc / man3 / BN_generate_prime.pod
1 =pod
2
3 =head1 NAME
4
5 BN_generate_prime_ex2, BN_generate_prime_ex, BN_is_prime_ex, BN_check_prime,
6 BN_is_prime_fasttest_ex, BN_GENCB_call, BN_GENCB_new, BN_GENCB_free,
7 BN_GENCB_set_old, BN_GENCB_set, BN_GENCB_get_arg, BN_generate_prime,
8 BN_is_prime, BN_is_prime_fasttest - generate primes and test for primality
9
10 =head1 SYNOPSIS
11
12  #include <openssl/bn.h>
13
14  int BN_generate_prime_ex2(BIGNUM *ret, int bits, int safe,
15                            const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb,
16                            BN_CTX *ctx);
17
18  int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
19                           const BIGNUM *rem, BN_GENCB *cb);
20
21  int BN_check_prime(const BIGNUM *p, BN_CTX *ctx, BN_GENCB *cb);
22
23  int BN_GENCB_call(BN_GENCB *cb, int a, int b);
24
25  BN_GENCB *BN_GENCB_new(void);
26
27  void BN_GENCB_free(BN_GENCB *cb);
28
29  void BN_GENCB_set_old(BN_GENCB *gencb,
30                        void (*callback)(int, int, void *), void *cb_arg);
31
32  void BN_GENCB_set(BN_GENCB *gencb,
33                    int (*callback)(int, int, BN_GENCB *), void *cb_arg);
34
35  void *BN_GENCB_get_arg(BN_GENCB *cb);
36
37 Deprecated since OpenSSL 0.9.8, can be hidden entirely by defining
38 B<OPENSSL_API_COMPAT> with a suitable version value, see
39 L<openssl_user_macros(7)>:
40
41  BIGNUM *BN_generate_prime(BIGNUM *ret, int num, int safe, BIGNUM *add,
42                            BIGNUM *rem, void (*callback)(int, int, void *),
43                            void *cb_arg);
44
45  int BN_is_prime(const BIGNUM *p, int nchecks,
46                  void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg);
47
48  int BN_is_prime_fasttest(const BIGNUM *p, int nchecks,
49                           void (*callback)(int, int, void *), BN_CTX *ctx,
50                           void *cb_arg, int do_trial_division);
51
52 Deprecated since OpenSSL 3.0:
53
54  int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb);
55
56  int BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx,
57                              int do_trial_division, BN_GENCB *cb);
58
59 =head1 DESCRIPTION
60
61 BN_generate_prime_ex2() generates a pseudo-random prime number of
62 at least bit length B<bits> using the BN_CTX provided in B<ctx>. The value of
63 B<ctx> must not be NULL.
64
65 The returned number is probably prime with a negligible error.
66 The maximum error rate is 2^-128.
67 It's 2^-287 for a 512 bit prime, 2^-435 for a 1024 bit prime,
68 2^-648 for a 2048 bit prime, and lower than 2^-882 for primes larger
69 than 2048 bit.
70
71 If B<add> is B<NULL> the returned prime number will have exact bit
72 length B<bits> with the top most two bits set.
73
74 If B<ret> is not B<NULL>, it will be used to store the number.
75
76 If B<cb> is not B<NULL>, it is used as follows:
77
78 =over 2
79
80 =item *
81
82 B<BN_GENCB_call(cb, 0, i)> is called after generating the i-th
83 potential prime number.
84
85 =item *
86
87 While the number is being tested for primality,
88 B<BN_GENCB_call(cb, 1, j)> is called as described below.
89
90 =item *
91
92 When a prime has been found, B<BN_GENCB_call(cb, 2, i)> is called.
93
94 =item *
95
96 The callers of BN_generate_prime_ex() may call B<BN_GENCB_call(cb, i, j)> with
97 other values as described in their respective man pages; see L</SEE ALSO>.
98
99 =back
100
101 The prime may have to fulfill additional requirements for use in
102 Diffie-Hellman key exchange:
103
104 If B<add> is not B<NULL>, the prime will fulfill the condition p % B<add>
105 == B<rem> (p % B<add> == 1 if B<rem> == B<NULL>) in order to suit a given
106 generator.
107
108 If B<safe> is true, it will be a safe prime (i.e. a prime p so
109 that (p-1)/2 is also prime). If B<safe> is true, and B<rem> == B<NULL>
110 the condition will be p % B<add> == 3.
111 It is recommended that B<add> is a multiple of 4.
112
113 The random generator must be seeded prior to calling BN_generate_prime_ex().
114 If the automatic seeding or reseeding of the OpenSSL CSPRNG fails due to
115 external circumstances (see L<RAND(7)>), the operation will fail.
116 The random number generator configured for the OPENSSL_CTX associated with
117 B<ctx> will be used.
118
119 BN_generate_prime_ex() is the same as BN_generate_prime_ex2() except that no
120 B<ctx> parameter is passed.
121 In this case the random number generator associated with the default OPENSSL_CTX
122 will be used.
123
124 BN_check_prime(), BN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_is_prime()
125 and BN_is_prime_fasttest() test if the number B<p> is prime.
126 The functions tests until one of the tests shows that B<p> is composite,
127 or all the tests passed.
128 If B<p> passes all these tests, it is considered a probable prime.
129
130 The test performed on B<p> are trial division by a number of small primes
131 and rounds of the of the Miller-Rabin probabilistic primality test.
132
133 The functions do at least 64 rounds of the Miller-Rabin test giving a maximum
134 false positive rate of 2^-128.
135 If the size of B<p> is more than 2048 bits, they do at least 128 rounds
136 giving a maximum false positive rate of 2^-256.
137
138 If B<nchecks> is larger than the minimum above (64 or 128), B<nchecks>
139 rounds of the Miller-Rabin test will be done.
140
141 If B<do_trial_division> set to B<0>, the trial division will be skipped.
142 BN_is_prime_ex() and BN_is_prime() always skip the trial division.
143
144 BN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_is_prime()
145 and BN_is_prime_fasttest() are deprecated.
146
147 BN_is_prime_fasttest() and BN_is_prime() behave just like
148 BN_is_prime_fasttest_ex() and BN_is_prime_ex() respectively, but with the old
149 style call back.
150
151 B<ctx> is a pre-allocated B<BN_CTX> (to save the overhead of allocating and
152 freeing the structure in a loop), or B<NULL>.
153
154 If the trial division is done, and no divisors are found and B<cb>
155 is not B<NULL>, B<BN_GENCB_call(cb, 1, -1)> is called.
156
157 After each round of the Miller-Rabin probabilistic primality test,
158 if B<cb> is not B<NULL>, B<BN_GENCB_call(cb, 1, j)> is called
159 with B<j> the iteration (j = 0, 1, ...).
160
161 BN_GENCB_call() calls the callback function held in the B<BN_GENCB> structure
162 and passes the ints B<a> and B<b> as arguments. There are two types of
163 B<BN_GENCB> structure that are supported: "new" style and "old" style. New
164 programs should prefer the "new" style, whilst the "old" style is provided
165 for backwards compatibility purposes.
166
167 A B<BN_GENCB> structure should be created through a call to BN_GENCB_new(),
168 and freed through a call to BN_GENCB_free().
169
170 For "new" style callbacks a BN_GENCB structure should be initialised with a
171 call to BN_GENCB_set(), where B<gencb> is a B<BN_GENCB *>, B<callback> is of
172 type B<int (*callback)(int, int, BN_GENCB *)> and B<cb_arg> is a B<void *>.
173 "Old" style callbacks are the same except they are initialised with a call
174 to BN_GENCB_set_old() and B<callback> is of type
175 B<void (*callback)(int, int, void *)>.
176
177 A callback is invoked through a call to B<BN_GENCB_call>. This will check
178 the type of the callback and will invoke B<callback(a, b, gencb)> for new
179 style callbacks or B<callback(a, b, cb_arg)> for old style.
180
181 It is possible to obtain the argument associated with a BN_GENCB structure
182 (set via a call to BN_GENCB_set or BN_GENCB_set_old) using BN_GENCB_get_arg.
183
184 BN_generate_prime() (deprecated) works in the same way as
185 BN_generate_prime_ex() but expects an old-style callback function
186 directly in the B<callback> parameter, and an argument to pass to it in
187 the B<cb_arg>. BN_is_prime() and BN_is_prime_fasttest()
188 can similarly be compared to BN_is_prime_ex() and
189 BN_is_prime_fasttest_ex(), respectively.
190
191 =head1 RETURN VALUES
192
193 BN_generate_prime_ex() return 1 on success or 0 on error.
194
195 BN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_is_prime(),
196 BN_is_prime_fasttest() and BN_check_prime return 0 if the number is composite,
197 1 if it is prime with an error probability of less than 0.25^B<nchecks>, and
198 -1 on error.
199
200 BN_generate_prime() returns the prime number on success, B<NULL> otherwise.
201
202 BN_GENCB_new returns a pointer to a BN_GENCB structure on success, or B<NULL>
203 otherwise.
204
205 BN_GENCB_get_arg returns the argument previously associated with a BN_GENCB
206 structure.
207
208 Callback functions should return 1 on success or 0 on error.
209
210 The error codes can be obtained by L<ERR_get_error(3)>.
211
212 =head1 REMOVED FUNCTIONALITY
213
214 As of OpenSSL 1.1.0 it is no longer possible to create a BN_GENCB structure
215 directly, as in:
216
217  BN_GENCB callback;
218
219 Instead applications should create a BN_GENCB structure using BN_GENCB_new:
220
221  BN_GENCB *callback;
222  callback = BN_GENCB_new();
223  if (!callback)
224      /* error */
225  ...
226  BN_GENCB_free(callback);
227
228 =head1 SEE ALSO
229
230 L<DH_generate_parameters(3)>, L<DSA_generate_parameters(3)>,
231 L<RSA_generate_key(3)>, L<ERR_get_error(3)>, L<RAND_bytes(3)>,
232 L<RAND(7)>
233
234 =head1 HISTORY
235
236 The BN_GENCB_new(), BN_GENCB_free(),
237 and BN_GENCB_get_arg() functions were added in OpenSSL 1.1.0.
238
239 BN_check_prime() was added in OpenSSL 3.0.
240
241 =head1 COPYRIGHT
242
243 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
244
245 Licensed under the Apache License 2.0 (the "License").  You may not use
246 this file except in compliance with the License.  You can obtain a copy
247 in the file LICENSE in the source distribution or at
248 L<https://www.openssl.org/source/license.html>.
249
250 =cut