summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschwarze <>2022-11-24 19:06:38 +0000
committerschwarze <>2022-11-24 19:06:38 +0000
commit71826d76dc702ad60f7f3e5d48effe1f17dfac1a (patch)
tree3dd4a18192ef63590ed5b7f3785c2822b330f33e
parent28e36c79110ac315df0015ad3a0e74d5be6a8e97 (diff)
downloadopenbsd-71826d76dc702ad60f7f3e5d48effe1f17dfac1a.tar.gz
openbsd-71826d76dc702ad60f7f3e5d48effe1f17dfac1a.tar.bz2
openbsd-71826d76dc702ad60f7f3e5d48effe1f17dfac1a.zip
Major overhaul.
Remove many statements that are no longer true after tb@, in July, massively improved the algorithms used by these functions and also did some cleanup of the interface. Instead, explain many aspects that were missing. Also use more descriptive argument names, drop some redundancy, and improve ordering in various respects. Feedback and enthusiastic OK from tb@.
-rw-r--r--src/lib/libcrypto/man/BN_generate_prime.3426
1 files changed, 216 insertions, 210 deletions
diff --git a/src/lib/libcrypto/man/BN_generate_prime.3 b/src/lib/libcrypto/man/BN_generate_prime.3
index 764ea6f873..df28d3775c 100644
--- a/src/lib/libcrypto/man/BN_generate_prime.3
+++ b/src/lib/libcrypto/man/BN_generate_prime.3
@@ -1,7 +1,24 @@
1.\" $OpenBSD: BN_generate_prime.3,v 1.19 2020/06/24 18:15:00 jmc Exp $ 1.\" $OpenBSD: BN_generate_prime.3,v 1.20 2022/11/24 19:06:38 schwarze Exp $
2.\" full merge up to: OpenSSL f987a4dd Jun 27 10:12:08 2019 +0200 2.\" full merge up to: OpenSSL f987a4dd Jun 27 10:12:08 2019 +0200
3.\" 3.\"
4.\" This file was written by Ulf Moeller <ulf@openssl.org> 4.\" This file is a derived work.
5.\" The changes are covered by the following Copyright and license:
6.\"
7.\" Copyright (c) 2022 Ingo Schwarze <schwarze@openbsd.org>
8.\"
9.\" Permission to use, copy, modify, and distribute this software for any
10.\" purpose with or without fee is hereby granted, provided that the above
11.\" copyright notice and this permission notice appear in all copies.
12.\"
13.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20.\"
21.\" The original file was written by Ulf Moeller <ulf@openssl.org>
5.\" Bodo Moeller <bodo@openssl.org>, and Matt Caswell <matt@openssl.org>. 22.\" Bodo Moeller <bodo@openssl.org>, and Matt Caswell <matt@openssl.org>.
6.\" Copyright (c) 2000, 2003, 2013, 2014, 2018 The OpenSSL Project. 23.\" Copyright (c) 2000, 2003, 2013, 2014, 2018 The OpenSSL Project.
7.\" All rights reserved. 24.\" All rights reserved.
@@ -50,54 +67,56 @@
50.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 67.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51.\" OF THE POSSIBILITY OF SUCH DAMAGE. 68.\" OF THE POSSIBILITY OF SUCH DAMAGE.
52.\" 69.\"
53.Dd $Mdocdate: June 24 2020 $ 70.Dd $Mdocdate: November 24 2022 $
54.Dt BN_GENERATE_PRIME 3 71.Dt BN_GENERATE_PRIME 3
55.Os 72.Os
56.Sh NAME 73.Sh NAME
57.Nm BN_generate_prime_ex ,
58.Nm BN_is_prime_ex , 74.Nm BN_is_prime_ex ,
59.Nm BN_is_prime_fasttest_ex , 75.Nm BN_is_prime_fasttest_ex ,
76.Nm BN_generate_prime_ex ,
60.Nm BN_GENCB_call , 77.Nm BN_GENCB_call ,
61.Nm BN_GENCB_new , 78.Nm BN_GENCB_new ,
62.Nm BN_GENCB_free , 79.Nm BN_GENCB_free ,
63.Nm BN_GENCB_set_old ,
64.Nm BN_GENCB_set , 80.Nm BN_GENCB_set ,
65.Nm BN_GENCB_get_arg , 81.Nm BN_GENCB_get_arg ,
82.Nm BN_GENCB_set_old ,
66.Nm BN_generate_prime , 83.Nm BN_generate_prime ,
67.Nm BN_is_prime , 84.Nm BN_is_prime ,
68.Nm BN_is_prime_fasttest 85.Nm BN_is_prime_fasttest
86.\" Nm BN_prime_checks_for_size is intentionally undocumented
87.\" because it is no longer used by LibreSSL.
69.Nd generate primes and test for primality 88.Nd generate primes and test for primality
70.Sh SYNOPSIS 89.Sh SYNOPSIS
71.In openssl/bn.h 90.In openssl/bn.h
72.Ft int 91.Ft int
73.Fo BN_generate_prime_ex
74.Fa "BIGNUM *ret"
75.Fa "int bits"
76.Fa "int safe"
77.Fa "const BIGNUM *add"
78.Fa "const BIGNUM *rem"
79.Fa "BN_GENCB *cb"
80.Fc
81.Ft int
82.Fo BN_is_prime_ex 92.Fo BN_is_prime_ex
83.Fa "const BIGNUM *p" 93.Fa "const BIGNUM *a"
84.Fa "int nchecks" 94.Fa "int nchecks"
85.Fa "BN_CTX *ctx" 95.Fa "BN_CTX *ctx"
86.Fa "BN_GENCB *cb" 96.Fa "BN_GENCB *cb"
87.Fc 97.Fc
88.Ft int 98.Ft int
89.Fo BN_is_prime_fasttest_ex 99.Fo BN_is_prime_fasttest_ex
90.Fa "const BIGNUM *p" 100.Fa "const BIGNUM *a"
91.Fa "int nchecks" 101.Fa "int nchecks"
92.Fa "BN_CTX *ctx" 102.Fa "BN_CTX *ctx"
93.Fa "int do_trial_division" 103.Fa "int do_trial_division"
94.Fa "BN_GENCB *cb" 104.Fa "BN_GENCB *cb"
95.Fc 105.Fc
96.Ft int 106.Ft int
107.Fo BN_generate_prime_ex
108.Fa "BIGNUM *ret"
109.Fa "int bits"
110.Fa "int safe"
111.Fa "const BIGNUM *modulus"
112.Fa "const BIGNUM *remainder"
113.Fa "BN_GENCB *cb"
114.Fc
115.Ft int
97.Fo BN_GENCB_call 116.Fo BN_GENCB_call
98.Fa "BN_GENCB *cb" 117.Fa "BN_GENCB *cb"
99.Fa "int a" 118.Fa "int state_code"
100.Fa "int b" 119.Fa "int serial_number"
101.Fc 120.Fc
102.Ft BN_GENCB * 121.Ft BN_GENCB *
103.Fn BN_GENCB_new void 122.Fn BN_GENCB_new void
@@ -106,15 +125,9 @@
106.Fa "BN_GENCB *cb" 125.Fa "BN_GENCB *cb"
107.Fc 126.Fc
108.Ft void 127.Ft void
109.Fo BN_GENCB_set_old
110.Fa "BN_GENCB *gencb"
111.Fa "void (*callback)(int, int, void *)"
112.Fa "void *cb_arg"
113.Fc
114.Ft void
115.Fo BN_GENCB_set 128.Fo BN_GENCB_set
116.Fa "BN_GENCB *gencb" 129.Fa "BN_GENCB *cb"
117.Fa "int (*callback)(int, int, BN_GENCB *)" 130.Fa "int (*cb_fp)(int, int, BN_GENCB *)"
118.Fa "void *cb_arg" 131.Fa "void *cb_arg"
119.Fc 132.Fc
120.Ft void * 133.Ft void *
@@ -124,21 +137,27 @@
124.Pp 137.Pp
125Deprecated: 138Deprecated:
126.Pp 139.Pp
140.Ft void
141.Fo BN_GENCB_set_old
142.Fa "BN_GENCB *cb"
143.Fa "void (*cb_fp)(int, int, void *)"
144.Fa "void *cb_arg"
145.Fc
127.Ft BIGNUM * 146.Ft BIGNUM *
128.Fo BN_generate_prime 147.Fo BN_generate_prime
129.Fa "BIGNUM *ret" 148.Fa "BIGNUM *ret"
130.Fa "int num" 149.Fa "int num"
131.Fa "int safe" 150.Fa "int safe"
132.Fa "BIGNUM *add" 151.Fa "BIGNUM *modulus"
133.Fa "BIGNUM *rem" 152.Fa "BIGNUM *remainder"
134.Fa "void (*callback)(int, int, void *)" 153.Fa "void (*cb_fp)(int, int, void *)"
135.Fa "void *cb_arg" 154.Fa "void *cb_arg"
136.Fc 155.Fc
137.Ft int 156.Ft int
138.Fo BN_is_prime 157.Fo BN_is_prime
139.Fa "const BIGNUM *a" 158.Fa "const BIGNUM *a"
140.Fa "int checks" 159.Fa "int checks"
141.Fa "void (*callback)(int, int, void *)" 160.Fa "void (*cb_fp)(int, int, void *)"
142.Fa "BN_CTX *ctx" 161.Fa "BN_CTX *ctx"
143.Fa "void *cb_arg" 162.Fa "void *cb_arg"
144.Fc 163.Fc
@@ -146,22 +165,84 @@ Deprecated:
146.Fo BN_is_prime_fasttest 165.Fo BN_is_prime_fasttest
147.Fa "const BIGNUM *a" 166.Fa "const BIGNUM *a"
148.Fa "int checks" 167.Fa "int checks"
149.Fa "void (*callback)(int, int, void *)" 168.Fa "void (*cb_fp)(int, int, void *)"
150.Fa "BN_CTX *ctx" 169.Fa "BN_CTX *ctx"
151.Fa "void *cb_arg" 170.Fa "void *cb_arg"
152.Fa "int do_trial_division" 171.Fa "int do_trial_division"
153.Fc 172.Fc
154.Sh DESCRIPTION 173.Sh DESCRIPTION
174.Fn BN_is_prime_ex
175and
176.Fn BN_is_prime_fasttest_ex
177test whether the number
178.Fa a
179is prime.
180In LibreSSL, both functions behave identically,
181use the Baillie-Pomerance-Selfridge-Wagstaff algorithm,
182and ignore the
183.Fa checks
184and
185.Fa do_trial_division
186arguments.
187.Pp
188It is unknown whether any composite number exists that the
189Baillie-PSW algorithm misclassifies as a prime.
190Some suspect that there may be infinitely many such numbers,
191but not a single one is currently known.
192It is known that no such number exists below 2\(ha64.
193.Pp
194If
195.Dv NULL
196is passed for the
197.Fa ctx
198argument, these function allocate a
199.Vt BN_CTX
200object internally when they need one and free it before returning.
201Alternatively, to save the overhead of allocating and freeing
202that object for each call, the caller can pre-allocate a
203.Vt BN_CTX
204object and pass it in the
205.Fa ctx
206argument.
207.Pp
155.Fn BN_generate_prime_ex 208.Fn BN_generate_prime_ex
156generates a pseudo-random prime number of at least bit length 209generates a pseudo-random prime number of at least bit length
157.Fa bits . 210.Fa bits
158The returned number is probably prime, but there is a very small 211and places it in
159probability of returning a non-prime number. 212.Fa ret .
160If 213Primality of
161.Fa ret 214.Fa ret
215is tested internally using
216.Fn BN_is_prime_ex .
217Consequently, for
218.Fa bits
219larger than 64, it is theoretically possible
220that this function might place a composite number into
221.Fa ret ;
222the probability of such an event is unknown but very small.
223.Pp
224The prime may have to fulfill additional requirements for use in
225Diffie-Hellman key exchange:
226.Bl -bullet
227.It
228If
229.Fa modulus
162is not 230is not
163.Dv NULL , 231.Dv NULL ,
164it will be used to store the number. 232a prime is generated that fulfills the condition
233.Fa ret No % Fa modulus No = Fa remainder .
234If the
235.Fa remainder
236argument is
237.Dv NULL ,
2381 is used as the desired remainder.
239.It
240If the
241.Fa safe
242argument is non-zero, a safe prime is generated, that is,
243.Po Fa ret No \- 1 Pc Ns /2
244is also prime.
245.El
165.Pp 246.Pp
166If 247If
167.Fa cb 248.Fa cb
@@ -170,15 +251,18 @@ is not
170it is used as follows: 251it is used as follows:
171.Bl -bullet 252.Bl -bullet
172.It 253.It
173.Fn BN_GENCB_call cb 0 i 254.Fn BN_GENCB_call cb 0 serial_number
174is called after generating the i-th potential prime number. 255is called after generating a potential prime number.
175.It 256.It
176While the number is being tested for primality, 257The
177.Fn BN_GENCB_call cb 1 j 258.Fa state_code
178is called as described below. 259of 1 is reserved for callbacks during primality testing,
260but LibreSSL performs no such callbacks.
179.It 261.It
180When a prime has been found, 262When
181.Fn BN_GENCB_call cb 2 i 263.Fa safe
264is non-zero and a safe prime has been found,
265.Fn BN_GENCB_call cb 2 serial_number
182is called. 266is called.
183.It 267.It
184The callers of 268The callers of
@@ -189,207 +273,129 @@ with other values as described in their respective manual pages; see
189.Sx SEE ALSO . 273.Sx SEE ALSO .
190.El 274.El
191.Pp 275.Pp
192The prime may have to fulfill additional requirements for use in 276In all cases, the
193Diffie-Hellman key exchange: 277.Fa serial_number
194.Pp 278is the number of candidates that have already been discarded
195If 279for not being prime; that is,
196.Fa add 280.Fa serial_number
197is not 281is 0 for the first candidate
198.Dv NULL , 282and then incremented whenever a new candidate is generated.
199the prime will fulfill the condition p %
200.Fa add
201==
202.Fa rem
203(p %
204.Fa add
205== 1 if
206.Fa rem
207==
208.Dv NULL )
209in order to suit a given generator.
210.Pp
211If
212.Fa safe
213is true, it will be a safe prime (i.e. a prime p so that (p-1)/2
214is also prime).
215.Pp
216.Fn BN_is_prime_ex
217and
218.Fn BN_is_prime_fasttest_ex
219test if the number
220.Fa p
221is prime.
222The following tests are performed until one of them shows that
223.Fa p
224is composite; if
225.Fa p
226passes all these tests, it is considered prime.
227.Pp 283.Pp
228.Fn BN_is_prime_fasttest_ex , 284.Fn BN_GENCB_call
229when called with 285calls the callback function held in
230.Fa do_trial_division
231== 1, first attempts trial division by a number of small primes;
232if no divisors are found by this test and
233.Fa cb 286.Fa cb
234is not 287and passes the
235.Dv NULL , 288.Fa state_code
236.Sy BN_GENCB_call(cb, 1, -1) 289and the
237is called. 290.Fa serial_number
238If 291as arguments.
239.Fa do_trial_division
240== 0, this test is skipped.
241.Pp
242Both
243.Fn BN_is_prime_ex
244and
245.Fn BN_is_prime_fasttest_ex
246perform a Miller-Rabin probabilistic primality test with
247.Fa nchecks
248iterations.
249If 292If
250.Fa nchecks 293.Fa cb
251== 294is
252.Dv BN_prime_checks , 295.Dv NULL
253a number of iterations is used that yields a false positive rate 296or does not contain a callback function, no action occurs.
254of at most 2\(ha-64 for random input.
255The error rate depends on the size of the prime
256and goes down for bigger primes.
257The rate is 2\(ha-80 starting at 308 bits, 2\(ha-112 at 852 bits,
2582\(ha-128 at 1080 bits, 2\(ha-192 at 3747 bits
259and 2\(ha-256 at 6394 bits.
260.Pp 297.Pp
261When the source of the prime is not random or not trusted, the 298.Fn BN_GENCB_new
262number of checks needs to be much higher to reach the same level 299allocates a new
263of assurance: It should equal half of the targeted security level 300.Vt BN_GENCB
264in bits (rounded up to the next integer if necessary). 301object.
265For instance, to reach the 128-bit security level,
266.Fa nchecks
267should be set to 64.
268.Pp 302.Pp
303.Fn BN_GENCB_free
304frees
305.Fa cb .
269If 306If
270.Fa cb 307.Fa cb
271is not 308is
272.Dv NULL , 309.Dv NULL ,
273.Fa BN_GENCB_call cb 1 j 310no action occurs.
274is called after the j-th iteration (j = 0, 1, ...).
275.Fa ctx
276is a pre-allocated
277.Vt BN_CTX
278(to save the overhead of allocating and freeing the structure in a
279loop), or
280.Dv NULL .
281.Pp 311.Pp
282.Fn BN_GENCB_call 312.Fn BN_GENCB_set
283calls the callback function held in the 313initialises
284.Vt BN_GENCB 314.Fa cb
285structure and passes the ints 315to use the callback function pointer
286.Fa a 316.Fa cb_fp
287and 317and the additional callback argument
288.Fa b 318.Fa cb_arg .
289as arguments.
290There are two types of
291.Vt BN_GENCB
292structures that are supported: "new" style and "old" style.
293New programs should prefer the "new" style, whilst the "old" style is
294provided for backwards compatibility purposes.
295.Pp 319.Pp
296A 320The deprecated function
297.Vt BN_GENCB 321.Fn BN_GENCB_set_old
298structure should be created through a call to 322initialises
299.Fn BN_GENCB_new 323.Fa cb
300and freed through a call to 324to use the old-style callback function pointer
301.Fn BN_GENCB_free . 325.Fa cb_fp
326and the additional callback argument
327.Fa cb_arg .
302.Pp 328.Pp
303For "new" style callbacks a 329.Fn BN_generate_prime
304.Vt BN_GENCB 330is a deprecated wrapper around
305structure should be initialised with a call to
306.Fn BN_GENCB_set ,
307where
308.Fa gencb
309is a
310.Vt BN_GENCB * ,
311.Fa callback
312is of type
313.Vt int (*callback)(int, int, BN_GENCB *)
314and
315.Fa cb_arg
316is a
317.Vt void * .
318"Old" style callbacks are the same except they are initialised with a
319call to
320.Fn BN_GENCB_set_old 331.Fn BN_GENCB_set_old
321and 332and
322.Fa callback 333.Fn BN_generate_prime_ex .
323is of type 334In contrast to
324.Vt void (*callback)(int, int, void *) . 335.Fn BN_generate_prime_ex ,
325.Pp 336if
326A callback is invoked through a call to 337.Dv NULL
327.Fn BN_GENCB_call . 338is passed for the
328This will check the type of the callback and will invoke 339.Fa ret
329.Fn callback a b gencb 340argument, a new
330for new style callbacks or 341.Vt BIGNUM
331.Fn callback a b cb_arg 342object is allocated and returned.
332for old style.
333.Pp
334It is possible to obtain the argument associated with a
335.Vt BN_GENCB
336structure (set via a call to
337.Fn BN_GENCB_set
338or
339.Fn BN_GENCB_set_old )
340using
341.Fn BN_GENCB_get_arg .
342.Pp 343.Pp
343.Fn BN_generate_prime 344Similarly,
344(deprecated) works in the same way as
345.Fn BN_generate_prime_ex
346but expects an old style callback function directly in the
347.Fa callback
348parameter, and an argument to pass to it in the
349.Fa cb_arg .
350Similarly
351.Fn BN_is_prime 345.Fn BN_is_prime
352and 346and
353.Fn BN_is_prime_fasttest 347.Fn BN_is_prime_fasttest
354are deprecated and can be compared to 348are deprecated wrappers around
355.Fn BN_is_prime_ex 349.Fn BN_GENCB_set_old
356and 350and
357.Fn BN_is_prime_fasttest_ex 351.Fn BN_is_prime_ex .
358respectively.
359.Sh RETURN VALUES 352.Sh RETURN VALUES
360.Fn BN_generate_prime_ex
361returns 1 on success or 0 on error.
362.Pp
363.Fn BN_is_prime_ex , 353.Fn BN_is_prime_ex ,
364.Fn BN_is_prime_fasttest_ex , 354.Fn BN_is_prime_fasttest_ex ,
365.Fn BN_is_prime , 355.Fn BN_is_prime ,
366and 356and
367.Fn BN_is_prime_fasttest 357.Fn BN_is_prime_fasttest
368return 0 if the number is composite, 1 if it is prime with an error 358return 0 if the number is composite, 1 if it is prime with a very small
369probability of less than 359error probability, or \-1 on error.
370.Pf 0.25^ Fa nchecks ,
371and -1 on error.
372.Pp 360.Pp
373.Fn BN_generate_prime 361.Fn BN_generate_prime_ex
374returns the prime number on success, 362returns 1 on success or 0 on error.
363.Pp
364.Fn BN_GENCB_call
365returns 1 on success, including when
366.Fa cb
367is
375.Dv NULL 368.Dv NULL
376otherwise. 369or does not contain a callback function,
370or 0 on error.
377.Pp 371.Pp
378.Fn BN_GENCB_new 372.Fn BN_GENCB_new
379returns a pointer to a 373returns a pointer to the newly allocated
380.Vt BN_GENCB 374.Vt BN_GENCB
381structure on success, or 375object or
382.Dv NULL 376.Dv NULL
383otherwise. 377if memory allocation fails.
378.Pp
379The callback functions pointed to by the
380.Fa cb_fp
381arguments are supposed to return 1 on success or 0 on error.
384.Pp 382.Pp
385.Fn BN_GENCB_get_arg 383.Fn BN_GENCB_get_arg
386returns the argument previously associated with a 384returns the
387.Vt BN_GENCB 385.Fa cb_arg
388structure. 386pointer that was previously stored in
387.Fa cb
388using
389.Fn BN_GENCB_set
390or
391.Fn BN_GENCB_set_old .
389.Pp 392.Pp
390Callback functions should return 1 on success or 0 on error. 393.Fn BN_generate_prime
394returns the prime number on success or
395.Dv NULL
396on failure.
391.Pp 397.Pp
392The error codes can be obtained by 398In some cases, error codes can be obtained by
393.Xr ERR_get_error 3 . 399.Xr ERR_get_error 3 .
394.Sh SEE ALSO 400.Sh SEE ALSO
395.Xr BN_new 3 , 401.Xr BN_new 3 ,