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