xref: /freebsd/crypto/openssl/doc/man3/BN_generate_prime.pod (revision a7148ab39c03abd4d1a84997c70bf96f15dd2a09)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5b077aed3SPierre ProncheryBN_generate_prime_ex2, BN_generate_prime_ex, BN_is_prime_ex, BN_check_prime,
6b077aed3SPierre ProncheryBN_is_prime_fasttest_ex, BN_GENCB_call, BN_GENCB_new, BN_GENCB_free,
7b077aed3SPierre ProncheryBN_GENCB_set_old, BN_GENCB_set, BN_GENCB_get_arg, BN_generate_prime,
8b077aed3SPierre 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
14b077aed3SPierre Pronchery int BN_generate_prime_ex2(BIGNUM *ret, int bits, int safe,
15b077aed3SPierre Pronchery                           const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb,
16b077aed3SPierre Pronchery                           BN_CTX *ctx);
17b077aed3SPierre 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
21b077aed3SPierre 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
37b077aed3SPierre ProncheryThe following functions have been deprecated since OpenSSL 0.9.8, and can be
38b077aed3SPierre Proncheryhidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
39b077aed3SPierre 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
45b077aed3SPierre 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
48b077aed3SPierre 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);
51b077aed3SPierre Pronchery
52b077aed3SPierre ProncheryThe following functions have been deprecated since OpenSSL 3.0, and can be
53b077aed3SPierre Proncheryhidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
54b077aed3SPierre Proncherysee L<openssl_user_macros(7)>:
55b077aed3SPierre Pronchery
56b077aed3SPierre Pronchery int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb);
57b077aed3SPierre Pronchery
58b077aed3SPierre Pronchery int BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx,
59b077aed3SPierre Pronchery                             int do_trial_division, BN_GENCB *cb);
60e71b7053SJung-uk Kim
61e71b7053SJung-uk Kim=head1 DESCRIPTION
62e71b7053SJung-uk Kim
63b077aed3SPierre ProncheryBN_generate_prime_ex2() generates a pseudo-random prime number of
64b077aed3SPierre Proncheryat least bit length B<bits> using the BN_CTX provided in B<ctx>. The value of
65b077aed3SPierre ProncheryB<ctx> must not be NULL.
66b077aed3SPierre Pronchery
67b077aed3SPierre ProncheryThe returned number is probably prime with a negligible error.
68b077aed3SPierre ProncheryThe maximum error rate is 2^-128.
69b077aed3SPierre ProncheryIt's 2^-287 for a 512 bit prime, 2^-435 for a 1024 bit prime,
70b077aed3SPierre Pronchery2^-648 for a 2048 bit prime, and lower than 2^-882 for primes larger
71b077aed3SPierre Proncherythan 2048 bit.
72b077aed3SPierre Pronchery
73b077aed3SPierre ProncheryIf B<add> is B<NULL> the returned prime number will have exact bit
74b077aed3SPierre 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.
118b077aed3SPierre ProncheryThe random number generator configured for the OSSL_LIB_CTX associated with
119b077aed3SPierre ProncheryB<ctx> will be used.
120e71b7053SJung-uk Kim
121b077aed3SPierre ProncheryBN_generate_prime_ex() is the same as BN_generate_prime_ex2() except that no
122b077aed3SPierre ProncheryB<ctx> parameter is passed.
123b077aed3SPierre ProncheryIn this case the random number generator associated with the default OSSL_LIB_CTX
124b077aed3SPierre Proncherywill be used.
125e71b7053SJung-uk Kim
126b077aed3SPierre ProncheryBN_check_prime(), BN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_is_prime()
127b077aed3SPierre Proncheryand BN_is_prime_fasttest() test if the number B<p> is prime.
128b077aed3SPierre ProncheryThe functions tests until one of the tests shows that B<p> is composite,
129b077aed3SPierre Proncheryor all the tests passed.
130b077aed3SPierre ProncheryIf B<p> passes all these tests, it is considered a probable prime.
131e71b7053SJung-uk Kim
132b077aed3SPierre ProncheryThe test performed on B<p> are trial division by a number of small primes
133b077aed3SPierre Proncheryand rounds of the of the Miller-Rabin probabilistic primality test.
134e71b7053SJung-uk Kim
135b077aed3SPierre ProncheryThe functions do at least 64 rounds of the Miller-Rabin test giving a maximum
136b077aed3SPierre Proncheryfalse positive rate of 2^-128.
137b077aed3SPierre ProncheryIf the size of B<p> is more than 2048 bits, they do at least 128 rounds
138b077aed3SPierre Proncherygiving a maximum false positive rate of 2^-256.
139e71b7053SJung-uk Kim
140b077aed3SPierre ProncheryIf B<nchecks> is larger than the minimum above (64 or 128), B<nchecks>
141b077aed3SPierre Proncheryrounds of the Miller-Rabin test will be done.
142b077aed3SPierre Pronchery
143b077aed3SPierre ProncheryIf B<do_trial_division> set to B<0>, the trial division will be skipped.
144b077aed3SPierre ProncheryBN_is_prime_ex() and BN_is_prime() always skip the trial division.
145b077aed3SPierre Pronchery
146b077aed3SPierre ProncheryBN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_is_prime()
147b077aed3SPierre Proncheryand BN_is_prime_fasttest() are deprecated.
148b077aed3SPierre Pronchery
149b077aed3SPierre ProncheryBN_is_prime_fasttest() and BN_is_prime() behave just like
150b077aed3SPierre ProncheryBN_is_prime_fasttest_ex() and BN_is_prime_ex() respectively, but with the old
151b077aed3SPierre Proncherystyle call back.
152b077aed3SPierre Pronchery
153b077aed3SPierre 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
156b077aed3SPierre ProncheryIf the trial division is done, and no divisors are found and B<cb>
157b077aed3SPierre Proncheryis not B<NULL>, B<BN_GENCB_call(cb, 1, -1)> is called.
158b077aed3SPierre Pronchery
159b077aed3SPierre ProncheryAfter each round of the Miller-Rabin probabilistic primality test,
160b077aed3SPierre Proncheryif B<cb> is not B<NULL>, B<BN_GENCB_call(cb, 1, j)> is called
161b077aed3SPierre Proncherywith B<j> the iteration (j = 0, 1, ...).
162b077aed3SPierre 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(),
170*a7148ab3SEnji Cooperand freed through a call to BN_GENCB_free(). If the argument is NULL,
171*a7148ab3SEnji Coopernothing is done.
172e71b7053SJung-uk Kim
173e71b7053SJung-uk KimFor "new" style callbacks a BN_GENCB structure should be initialised with a
174e71b7053SJung-uk Kimcall to BN_GENCB_set(), where B<gencb> is a B<BN_GENCB *>, B<callback> is of
175e71b7053SJung-uk Kimtype B<int (*callback)(int, int, BN_GENCB *)> and B<cb_arg> is a B<void *>.
176e71b7053SJung-uk Kim"Old" style callbacks are the same except they are initialised with a call
177e71b7053SJung-uk Kimto BN_GENCB_set_old() and B<callback> is of type
178e71b7053SJung-uk KimB<void (*callback)(int, int, void *)>.
179e71b7053SJung-uk Kim
180e71b7053SJung-uk KimA callback is invoked through a call to B<BN_GENCB_call>. This will check
181e71b7053SJung-uk Kimthe type of the callback and will invoke B<callback(a, b, gencb)> for new
182e71b7053SJung-uk Kimstyle callbacks or B<callback(a, b, cb_arg)> for old style.
183e71b7053SJung-uk Kim
184e71b7053SJung-uk KimIt is possible to obtain the argument associated with a BN_GENCB structure
185e71b7053SJung-uk Kim(set via a call to BN_GENCB_set or BN_GENCB_set_old) using BN_GENCB_get_arg.
186e71b7053SJung-uk Kim
187e71b7053SJung-uk KimBN_generate_prime() (deprecated) works in the same way as
188e71b7053SJung-uk KimBN_generate_prime_ex() but expects an old-style callback function
189e71b7053SJung-uk Kimdirectly in the B<callback> parameter, and an argument to pass to it in
190e71b7053SJung-uk Kimthe B<cb_arg>. BN_is_prime() and BN_is_prime_fasttest()
191e71b7053SJung-uk Kimcan similarly be compared to BN_is_prime_ex() and
192e71b7053SJung-uk KimBN_is_prime_fasttest_ex(), respectively.
193e71b7053SJung-uk Kim
194e71b7053SJung-uk Kim=head1 RETURN VALUES
195e71b7053SJung-uk Kim
196e71b7053SJung-uk KimBN_generate_prime_ex() return 1 on success or 0 on error.
197e71b7053SJung-uk Kim
198b077aed3SPierre ProncheryBN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_is_prime(),
199b077aed3SPierre ProncheryBN_is_prime_fasttest() and BN_check_prime return 0 if the number is composite,
200b077aed3SPierre Pronchery1 if it is prime with an error probability of less than 0.25^B<nchecks>, and
201e71b7053SJung-uk Kim-1 on error.
202e71b7053SJung-uk Kim
203e71b7053SJung-uk KimBN_generate_prime() returns the prime number on success, B<NULL> otherwise.
204e71b7053SJung-uk Kim
205e71b7053SJung-uk KimBN_GENCB_new returns a pointer to a BN_GENCB structure on success, or B<NULL>
206e71b7053SJung-uk Kimotherwise.
207e71b7053SJung-uk Kim
208e71b7053SJung-uk KimBN_GENCB_get_arg returns the argument previously associated with a BN_GENCB
209e71b7053SJung-uk Kimstructure.
210e71b7053SJung-uk Kim
211e71b7053SJung-uk KimCallback functions should return 1 on success or 0 on error.
212e71b7053SJung-uk Kim
213e71b7053SJung-uk KimThe error codes can be obtained by L<ERR_get_error(3)>.
214e71b7053SJung-uk Kim
215e71b7053SJung-uk Kim=head1 REMOVED FUNCTIONALITY
216e71b7053SJung-uk Kim
217e71b7053SJung-uk KimAs of OpenSSL 1.1.0 it is no longer possible to create a BN_GENCB structure
218e71b7053SJung-uk Kimdirectly, as in:
219e71b7053SJung-uk Kim
220e71b7053SJung-uk Kim BN_GENCB callback;
221e71b7053SJung-uk Kim
222e71b7053SJung-uk KimInstead applications should create a BN_GENCB structure using BN_GENCB_new:
223e71b7053SJung-uk Kim
224e71b7053SJung-uk Kim BN_GENCB *callback;
225e71b7053SJung-uk Kim callback = BN_GENCB_new();
226e71b7053SJung-uk Kim if (!callback)
227e71b7053SJung-uk Kim     /* error */
228e71b7053SJung-uk Kim ...
229e71b7053SJung-uk Kim BN_GENCB_free(callback);
230e71b7053SJung-uk Kim
231e71b7053SJung-uk Kim=head1 SEE ALSO
232e71b7053SJung-uk Kim
233e71b7053SJung-uk KimL<DH_generate_parameters(3)>, L<DSA_generate_parameters(3)>,
234da327cd2SJung-uk KimL<RSA_generate_key(3)>, L<ERR_get_error(3)>, L<RAND_bytes(3)>,
235da327cd2SJung-uk KimL<RAND(7)>
236e71b7053SJung-uk Kim
237e71b7053SJung-uk Kim=head1 HISTORY
238e71b7053SJung-uk Kim
239b077aed3SPierre ProncheryThe BN_is_prime_ex() and BN_is_prime_fasttest_ex() functions were
240b077aed3SPierre Proncherydeprecated in OpenSSL 3.0.
241b077aed3SPierre Pronchery
2426935a639SJung-uk KimThe BN_GENCB_new(), BN_GENCB_free(),
2436935a639SJung-uk Kimand BN_GENCB_get_arg() functions were added in OpenSSL 1.1.0.
244e71b7053SJung-uk Kim
245b077aed3SPierre ProncheryBN_check_prime() was added in OpenSSL 3.0.
246b077aed3SPierre Pronchery
247e71b7053SJung-uk Kim=head1 COPYRIGHT
248e71b7053SJung-uk Kim
249*a7148ab3SEnji CooperCopyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.
250e71b7053SJung-uk Kim
251b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
252e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
253e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
254e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
255e71b7053SJung-uk Kim
256e71b7053SJung-uk Kim=cut
257