xref: /openbsd-src/lib/libcrypto/man/BN_generate_prime.3 (revision 66c749efc0f32a6add489c4c641a5d26d2ed0bef)
1*66c749efStb.\" $OpenBSD: BN_generate_prime.3,v 1.25 2023/12/29 19:12:46 tb Exp $
256a8f046Sschwarze.\" full merge up to: OpenSSL f987a4dd Jun 27 10:12:08 2019 +0200
38974101aSjmc.\"
4bc693221Sschwarze.\" This file is a derived work.
5bc693221Sschwarze.\" The changes are covered by the following Copyright and license:
6bc693221Sschwarze.\"
7bc693221Sschwarze.\" Copyright (c) 2022 Ingo Schwarze <schwarze@openbsd.org>
8bc693221Sschwarze.\"
9bc693221Sschwarze.\" Permission to use, copy, modify, and distribute this software for any
10bc693221Sschwarze.\" purpose with or without fee is hereby granted, provided that the above
11bc693221Sschwarze.\" copyright notice and this permission notice appear in all copies.
12bc693221Sschwarze.\"
13bc693221Sschwarze.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14bc693221Sschwarze.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15bc693221Sschwarze.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16bc693221Sschwarze.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17bc693221Sschwarze.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18bc693221Sschwarze.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19bc693221Sschwarze.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20bc693221Sschwarze.\"
21bc693221Sschwarze.\" The original file was written by Ulf Moeller <ulf@openssl.org>
222897e260Sschwarze.\" Bodo Moeller <bodo@openssl.org>, and Matt Caswell <matt@openssl.org>.
2345984c10Sschwarze.\" Copyright (c) 2000, 2003, 2013, 2014, 2018 The OpenSSL Project.
242897e260Sschwarze.\" All rights reserved.
252897e260Sschwarze.\"
262897e260Sschwarze.\" Redistribution and use in source and binary forms, with or without
272897e260Sschwarze.\" modification, are permitted provided that the following conditions
282897e260Sschwarze.\" are met:
292897e260Sschwarze.\"
302897e260Sschwarze.\" 1. Redistributions of source code must retain the above copyright
312897e260Sschwarze.\"    notice, this list of conditions and the following disclaimer.
322897e260Sschwarze.\"
332897e260Sschwarze.\" 2. Redistributions in binary form must reproduce the above copyright
342897e260Sschwarze.\"    notice, this list of conditions and the following disclaimer in
352897e260Sschwarze.\"    the documentation and/or other materials provided with the
362897e260Sschwarze.\"    distribution.
372897e260Sschwarze.\"
382897e260Sschwarze.\" 3. All advertising materials mentioning features or use of this
392897e260Sschwarze.\"    software must display the following acknowledgment:
402897e260Sschwarze.\"    "This product includes software developed by the OpenSSL Project
412897e260Sschwarze.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
422897e260Sschwarze.\"
432897e260Sschwarze.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
442897e260Sschwarze.\"    endorse or promote products derived from this software without
452897e260Sschwarze.\"    prior written permission. For written permission, please contact
462897e260Sschwarze.\"    openssl-core@openssl.org.
472897e260Sschwarze.\"
482897e260Sschwarze.\" 5. Products derived from this software may not be called "OpenSSL"
492897e260Sschwarze.\"    nor may "OpenSSL" appear in their names without prior written
502897e260Sschwarze.\"    permission of the OpenSSL Project.
512897e260Sschwarze.\"
522897e260Sschwarze.\" 6. Redistributions of any form whatsoever must retain the following
532897e260Sschwarze.\"    acknowledgment:
542897e260Sschwarze.\"    "This product includes software developed by the OpenSSL Project
552897e260Sschwarze.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
562897e260Sschwarze.\"
572897e260Sschwarze.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
582897e260Sschwarze.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
592897e260Sschwarze.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
602897e260Sschwarze.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
612897e260Sschwarze.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
622897e260Sschwarze.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
632897e260Sschwarze.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
642897e260Sschwarze.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
652897e260Sschwarze.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
662897e260Sschwarze.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
672897e260Sschwarze.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
682897e260Sschwarze.\" OF THE POSSIBILITY OF SUCH DAMAGE.
692897e260Sschwarze.\"
70*66c749efStb.Dd $Mdocdate: December 29 2023 $
71f1048625Sschwarze.Dt BN_GENERATE_PRIME 3
72f1048625Sschwarze.Os
73f1048625Sschwarze.Sh NAME
74f1048625Sschwarze.Nm BN_is_prime_ex ,
75f1048625Sschwarze.Nm BN_is_prime_fasttest_ex ,
76bc693221Sschwarze.Nm BN_generate_prime_ex ,
77f1048625Sschwarze.Nm BN_GENCB_call ,
7829dd045aSschwarze.Nm BN_GENCB_new ,
7929dd045aSschwarze.Nm BN_GENCB_free ,
80f1048625Sschwarze.Nm BN_GENCB_set ,
8129dd045aSschwarze.Nm BN_GENCB_get_arg ,
823d096aa7Stb.Nm BN_GENCB_set_old
83bc693221Sschwarze.\" Nm BN_prime_checks_for_size is intentionally undocumented
8482b8a411Stb.\" because it should not be used outside of libcrypto.
85f1048625Sschwarze.Nd generate primes and test for primality
86f1048625Sschwarze.Sh SYNOPSIS
87f1048625Sschwarze.In openssl/bn.h
88f1048625Sschwarze.Ft int
89f1048625Sschwarze.Fo BN_is_prime_ex
90bc693221Sschwarze.Fa "const BIGNUM *a"
91f1048625Sschwarze.Fa "int nchecks"
92f1048625Sschwarze.Fa "BN_CTX *ctx"
93f1048625Sschwarze.Fa "BN_GENCB *cb"
94f1048625Sschwarze.Fc
95f1048625Sschwarze.Ft int
96f1048625Sschwarze.Fo BN_is_prime_fasttest_ex
97bc693221Sschwarze.Fa "const BIGNUM *a"
98f1048625Sschwarze.Fa "int nchecks"
99f1048625Sschwarze.Fa "BN_CTX *ctx"
100f1048625Sschwarze.Fa "int do_trial_division"
101f1048625Sschwarze.Fa "BN_GENCB *cb"
102f1048625Sschwarze.Fc
103f1048625Sschwarze.Ft int
104bc693221Sschwarze.Fo BN_generate_prime_ex
105bc693221Sschwarze.Fa "BIGNUM *ret"
106bc693221Sschwarze.Fa "int bits"
107bc693221Sschwarze.Fa "int safe"
108bc693221Sschwarze.Fa "const BIGNUM *modulus"
109bc693221Sschwarze.Fa "const BIGNUM *remainder"
110bc693221Sschwarze.Fa "BN_GENCB *cb"
111bc693221Sschwarze.Fc
112bc693221Sschwarze.Ft int
113f1048625Sschwarze.Fo BN_GENCB_call
114f1048625Sschwarze.Fa "BN_GENCB *cb"
115bc693221Sschwarze.Fa "int state_code"
116bc693221Sschwarze.Fa "int serial_number"
117f1048625Sschwarze.Fc
11829dd045aSschwarze.Ft BN_GENCB *
11929dd045aSschwarze.Fn BN_GENCB_new void
12029dd045aSschwarze.Ft void
12129dd045aSschwarze.Fo BN_GENCB_free
12229dd045aSschwarze.Fa "BN_GENCB *cb"
12329dd045aSschwarze.Fc
1242897e260Sschwarze.Ft void
1252897e260Sschwarze.Fo BN_GENCB_set
126bc693221Sschwarze.Fa "BN_GENCB *cb"
127bc693221Sschwarze.Fa "int (*cb_fp)(int, int, BN_GENCB *)"
1282897e260Sschwarze.Fa "void *cb_arg"
1292897e260Sschwarze.Fc
13029dd045aSschwarze.Ft void *
13129dd045aSschwarze.Fo BN_GENCB_get_arg
13229dd045aSschwarze.Fa "BN_GENCB *cb"
13329dd045aSschwarze.Fc
134f1048625Sschwarze.Pp
135f1048625SschwarzeDeprecated:
136f1048625Sschwarze.Pp
137bc693221Sschwarze.Ft void
138bc693221Sschwarze.Fo BN_GENCB_set_old
139bc693221Sschwarze.Fa "BN_GENCB *cb"
140bc693221Sschwarze.Fa "void (*cb_fp)(int, int, void *)"
141bc693221Sschwarze.Fa "void *cb_arg"
142bc693221Sschwarze.Fc
143f1048625Sschwarze.Sh DESCRIPTION
144bc693221Sschwarze.Fn BN_is_prime_ex
145bc693221Sschwarzeand
146bc693221Sschwarze.Fn BN_is_prime_fasttest_ex
147bc693221Sschwarzetest whether the number
148bc693221Sschwarze.Fa a
149bc693221Sschwarzeis prime.
15082b8a411StbIn LibreSSL, both functions behave identically
15182b8a411Stband use the Baillie-Pomerance-Selfridge-Wagstaff algorithm
15282b8a411Stbcombined with
153bc693221Sschwarze.Fa checks
15482b8a411StbMiller-Rabin rounds.
15582b8a411StbThe
156bc693221Sschwarze.Fa do_trial_division
15782b8a411Stbargument is ignored.
158bc693221Sschwarze.Pp
159bc693221SschwarzeIt is unknown whether any composite number exists that the
160bc693221SschwarzeBaillie-PSW algorithm misclassifies as a prime.
161bc693221SschwarzeSome suspect that there may be infinitely many such numbers,
162bc693221Sschwarzebut not a single one is currently known.
163bc693221SschwarzeIt is known that no such number exists below 2\(ha64.
164bc693221Sschwarze.Pp
16582b8a411StbIn order to reduce the likelihood of a composite number
1664fe9bd87Sjsgpassing the primality tests
16782b8a411Stb.Fn BN_is_prime_fasttest_ex
16882b8a411Stband
16982b8a411Stb.Fn BN_is_prime_ex ,
17082b8a411Stba number of rounds of the probabilistic Miller-Rabin test is performed.
17182b8a411StbIf
17282b8a411Stb.Fa checks
17382b8a411Stbis positive, it is used as the number of rounds;
17482b8a411Stbif it is zero or the special value
17582b8a411Stb.Dv BN_prime_checks ,
17682b8a411Stba suitable number of rounds is calculated from the bit length of
17782b8a411Stb.Fa a .
17882b8a411Stb.Pp
179bc693221SschwarzeIf
180bc693221Sschwarze.Dv NULL
181bc693221Sschwarzeis passed for the
182bc693221Sschwarze.Fa ctx
183bc693221Sschwarzeargument, these function allocate a
184bc693221Sschwarze.Vt BN_CTX
185bc693221Sschwarzeobject internally when they need one and free it before returning.
186bc693221SschwarzeAlternatively, to save the overhead of allocating and freeing
187bc693221Sschwarzethat object for each call, the caller can pre-allocate a
188bc693221Sschwarze.Vt BN_CTX
189bc693221Sschwarzeobject and pass it in the
190bc693221Sschwarze.Fa ctx
191bc693221Sschwarzeargument.
192bc693221Sschwarze.Pp
193f1048625Sschwarze.Fn BN_generate_prime_ex
1942897e260Sschwarzegenerates a pseudo-random prime number of at least bit length
195bc693221Sschwarze.Fa bits
196bc693221Sschwarzeand places it in
197bc693221Sschwarze.Fa ret .
198bc693221SschwarzePrimality of
199f1048625Sschwarze.Fa ret
200bc693221Sschwarzeis tested internally using
201bc693221Sschwarze.Fn BN_is_prime_ex .
202bc693221SschwarzeConsequently, for
203bc693221Sschwarze.Fa bits
204bc693221Sschwarzelarger than 64, it is theoretically possible
205bc693221Sschwarzethat this function might place a composite number into
206bc693221Sschwarze.Fa ret ;
207bc693221Sschwarzethe probability of such an event is unknown but very small.
208bc693221Sschwarze.Pp
209bc693221SschwarzeThe prime may have to fulfill additional requirements for use in
210bc693221SschwarzeDiffie-Hellman key exchange:
211bc693221Sschwarze.Bl -bullet
212bc693221Sschwarze.It
213bc693221SschwarzeIf
214bc693221Sschwarze.Fa modulus
215f1048625Sschwarzeis not
216f1048625Sschwarze.Dv NULL ,
217bc693221Sschwarzea prime is generated that fulfills the condition
218bc693221Sschwarze.Fa ret No % Fa modulus No = Fa remainder .
219bc693221SschwarzeIf the
220bc693221Sschwarze.Fa remainder
221bc693221Sschwarzeargument is
222bc693221Sschwarze.Dv NULL ,
223bc693221Sschwarze1 is used as the desired remainder.
224bc693221Sschwarze.It
225bc693221SschwarzeIf the
226bc693221Sschwarze.Fa safe
227bc693221Sschwarzeargument is non-zero, a safe prime is generated, that is,
228bc693221Sschwarze.Po Fa ret No \- 1 Pc Ns /2
229bc693221Sschwarzeis also prime.
230bc693221Sschwarze.El
231f1048625Sschwarze.Pp
232f1048625SschwarzeIf
233f1048625Sschwarze.Fa cb
234f1048625Sschwarzeis not
235f1048625Sschwarze.Dv NULL ,
236f1048625Sschwarzeit is used as follows:
237f1048625Sschwarze.Bl -bullet
238f1048625Sschwarze.It
239bc693221Sschwarze.Fn BN_GENCB_call cb 0 serial_number
240bc693221Sschwarzeis called after generating a potential prime number.
241f1048625Sschwarze.It
242bc693221SschwarzeThe
243bc693221Sschwarze.Fa state_code
244bc693221Sschwarzeof 1 is reserved for callbacks during primality testing,
245bc693221Sschwarzebut LibreSSL performs no such callbacks.
246f1048625Sschwarze.It
247bc693221SschwarzeWhen
248bc693221Sschwarze.Fa safe
249bc693221Sschwarzeis non-zero and a safe prime has been found,
250bc693221Sschwarze.Fn BN_GENCB_call cb 2 serial_number
251f1048625Sschwarzeis called.
25245984c10Sschwarze.It
25345984c10SschwarzeThe callers of
25445984c10Sschwarze.Fn BN_generate_prime_ex
25545984c10Sschwarzemay call
25645984c10Sschwarze.Fn BN_GENCB_call
25745984c10Sschwarzewith other values as described in their respective manual pages; see
25845984c10Sschwarze.Sx SEE ALSO .
259f1048625Sschwarze.El
260f1048625Sschwarze.Pp
261bc693221SschwarzeIn all cases, the
262bc693221Sschwarze.Fa serial_number
263bc693221Sschwarzeis the number of candidates that have already been discarded
264bc693221Sschwarzefor not being prime; that is,
265bc693221Sschwarze.Fa serial_number
266bc693221Sschwarzeis 0 for the first candidate
267bc693221Sschwarzeand then incremented whenever a new candidate is generated.
268f1048625Sschwarze.Pp
269f1048625Sschwarze.Fn BN_GENCB_call
270bc693221Sschwarzecalls the callback function held in
271bc693221Sschwarze.Fa cb
272bc693221Sschwarzeand passes the
273bc693221Sschwarze.Fa state_code
274bc693221Sschwarzeand the
275bc693221Sschwarze.Fa serial_number
276f1048625Sschwarzeas arguments.
277bc693221SschwarzeIf
278bc693221Sschwarze.Fa cb
279bc693221Sschwarzeis
280bc693221Sschwarze.Dv NULL
281bc693221Sschwarzeor does not contain a callback function, no action occurs.
282f1048625Sschwarze.Pp
28329dd045aSschwarze.Fn BN_GENCB_new
284bc693221Sschwarzeallocates a new
285f1048625Sschwarze.Vt BN_GENCB
286bc693221Sschwarzeobject.
287f1048625Sschwarze.Pp
288bc693221Sschwarze.Fn BN_GENCB_free
289bc693221Sschwarzefrees
290bc693221Sschwarze.Fa cb .
291bc693221SschwarzeIf
292bc693221Sschwarze.Fa cb
293bc693221Sschwarzeis
294bc693221Sschwarze.Dv NULL ,
295bc693221Sschwarzeno action occurs.
296f1048625Sschwarze.Pp
29729dd045aSschwarze.Fn BN_GENCB_set
298bc693221Sschwarzeinitialises
299bc693221Sschwarze.Fa cb
300bc693221Sschwarzeto use the callback function pointer
301bc693221Sschwarze.Fa cb_fp
302bc693221Sschwarzeand the additional callback argument
303bc693221Sschwarze.Fa cb_arg .
304bc693221Sschwarze.Pp
305bc693221SschwarzeThe deprecated function
306bc693221Sschwarze.Fn BN_GENCB_set_old
307bc693221Sschwarzeinitialises
308bc693221Sschwarze.Fa cb
309bc693221Sschwarzeto use the old-style callback function pointer
310bc693221Sschwarze.Fa cb_fp
311bc693221Sschwarzeand the additional callback argument
312bc693221Sschwarze.Fa cb_arg .
313f1048625Sschwarze.Sh RETURN VALUES
3147fc575d8Stb.Fn BN_is_prime_ex
315f1048625Sschwarzeand
3167fc575d8Stb.Fn BN_is_prime_fasttest_ex
317bc693221Sschwarzereturn 0 if the number is composite, 1 if it is prime with a very small
318bc693221Sschwarzeerror probability, or \-1 on error.
319f1048625Sschwarze.Pp
320bc693221Sschwarze.Fn BN_generate_prime_ex
321bc693221Sschwarzereturns 1 on success or 0 on error.
322bc693221Sschwarze.Pp
323bc693221Sschwarze.Fn BN_GENCB_call
324bc693221Sschwarzereturns 1 on success, including when
325bc693221Sschwarze.Fa cb
326bc693221Sschwarzeis
327f1048625Sschwarze.Dv NULL
328bc693221Sschwarzeor does not contain a callback function,
329bc693221Sschwarzeor 0 on error.
330f1048625Sschwarze.Pp
33129dd045aSschwarze.Fn BN_GENCB_new
332bc693221Sschwarzereturns a pointer to the newly allocated
33329dd045aSschwarze.Vt BN_GENCB
334bc693221Sschwarzeobject or
33529dd045aSschwarze.Dv NULL
336bc693221Sschwarzeif memory allocation fails.
337bc693221Sschwarze.Pp
338bc693221SschwarzeThe callback functions pointed to by the
339bc693221Sschwarze.Fa cb_fp
340bc693221Sschwarzearguments are supposed to return 1 on success or 0 on error.
34129dd045aSschwarze.Pp
34229dd045aSschwarze.Fn BN_GENCB_get_arg
343bc693221Sschwarzereturns the
344bc693221Sschwarze.Fa cb_arg
345bc693221Sschwarzepointer that was previously stored in
346bc693221Sschwarze.Fa cb
347bc693221Sschwarzeusing
348bc693221Sschwarze.Fn BN_GENCB_set
349bc693221Sschwarzeor
350bc693221Sschwarze.Fn BN_GENCB_set_old .
35129dd045aSschwarze.Pp
352bc693221SschwarzeIn some cases, error codes can be obtained by
353f1048625Sschwarze.Xr ERR_get_error 3 .
354f1048625Sschwarze.Sh SEE ALSO
35568d7c31cSschwarze.Xr BN_new 3 ,
35645984c10Sschwarze.Xr DH_generate_parameters 3 ,
357*66c749efStb.Xr DSA_generate_parameters_ex 3 ,
35845984c10Sschwarze.Xr RSA_generate_key 3
359f1048625Sschwarze.Sh HISTORY
3602c07bb3bSschwarze.Fn BN_generate_prime_ex ,
3612c07bb3bSschwarze.Fn BN_is_prime_ex ,
3622c07bb3bSschwarze.Fn BN_is_prime_fasttest_ex ,
3634b12da35Sschwarze.Fn BN_GENCB_call ,
3642c07bb3bSschwarze.Fn BN_GENCB_set_old ,
3652c07bb3bSschwarzeand
3662c07bb3bSschwarze.Fn BN_GENCB_set
3672c07bb3bSschwarzefirst appeared in OpenSSL 0.9.8 and have been available since
3682c07bb3bSschwarze.Ox 4.5 .
3692c07bb3bSschwarze.Pp
37029dd045aSschwarze.Fn BN_GENCB_new ,
37129dd045aSschwarze.Fn BN_GENCB_free ,
37229dd045aSschwarzeand
37329dd045aSschwarze.Fn BN_GENCB_get_arg
3744b12da35Sschwarzefirst appeared in OpenSSL 1.1.0 and have been available since
3754b12da35Sschwarze.Ox 6.3 .
376