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