1.Dd $Mdocdate: February 23 2015 $ 2.Dt BN_GENERATE_PRIME 3 3.Os 4.Sh NAME 5.Nm BN_generate_prime_ex , 6.Nm BN_is_prime_ex , 7.Nm BN_is_prime_fasttest_ex , 8.Nm BN_GENCB_call , 9.Nm BN_GENCB_set_old , 10.Nm BN_GENCB_set , 11.Nm BN_generate_prime , 12.Nm BN_is_prime , 13.Nm BN_is_prime_fasttest 14.Nd generate primes and test for primality 15.Sh SYNOPSIS 16.In openssl/bn.h 17.Ft int 18.Fo BN_generate_prime_ex 19.Fa "BIGNUM *ret" 20.Fa "int bits" 21.Fa "int safe" 22.Fa "const BIGNUM *add" 23.Fa "const BIGNUM *rem" 24.Fa "BN_GENCB *cb" 25.Fc 26.Ft int 27.Fo BN_is_prime_ex 28.Fa "const BIGNUM *p" 29.Fa "int nchecks" 30.Fa "BN_CTX *ctx" 31.Fa "BN_GENCB *cb" 32.Fc 33.Ft int 34.Fo BN_is_prime_fasttest_ex 35.Fa "const BIGNUM *p" 36.Fa "int nchecks" 37.Fa "BN_CTX *ctx" 38.Fa "int do_trial_division" 39.Fa "BN_GENCB *cb" 40.Fc 41.Ft int 42.Fo BN_GENCB_call 43.Fa "BN_GENCB *cb" 44.Fa "int a" 45.Fa "int b" 46.Fc 47.Fd #define BN_GENCB_set_old(gencb, callback, cb_arg) ... 48.Fd #define BN_GENCB_set(gencb, callback, cb_arg) ... 49.Pp 50Deprecated: 51.Pp 52.Ft BIGNUM * 53.Fo BN_generate_prime 54.Fa "BIGNUM *ret" 55.Fa "int num" 56.Fa "int safe" 57.Fa "BIGNUM *add" 58.Fa "BIGNUM *rem" 59.Fa "void (*callback)(int, int, void *)" 60.Fa "void *cb_arg" 61.Fc 62.Ft int 63.Fo BN_is_prime 64.Fa "const BIGNUM *a" 65.Fa "int checks" 66.Fa "void (*callback)(int, int, void *)" 67.Fa "BN_CTX *ctx" 68.Fa "void *cb_arg" 69.Fc 70.Ft int 71.Fo BN_is_prime_fasttest 72.Fa "const BIGNUM *a" 73.Fa "int checks" 74.Fa "void (*callback)(int, int, void *)" 75.Fa "BN_CTX *ctx" 76.Fa "void *cb_arg" 77.Fa "int do_trial_division" 78.Fc 79.Sh DESCRIPTION 80.Fn BN_generate_prime_ex 81generates a pseudo-random prime number of bit length 82.Fa bits . 83If 84.Fa ret 85is not 86.Dv NULL , 87it will be used to store the number. 88.Pp 89If 90.Fa cb 91is not 92.Dv NULL , 93it is used as follows: 94.Bl -bullet 95.It 96.Fn BN_GENCB_call cb 0 i 97is called after generating the i-th potential prime number. 98.It 99While the number is being tested for primality, 100.Fn BN_GENCB_call cb 1 j 101is called as described below. 102.It 103When a prime has been found, 104.Fn BN_GENCB_call cb 2 i 105is called. 106.El 107.Pp 108The prime may have to fulfill additional requirements for use in 109Diffie-Hellman key exchange: 110.Pp 111If 112.Fa add 113is not 114.Dv NULL , 115the prime will fulfill the condition p % 116.Fa add 117== 118.Fa rem 119(p % 120.Fa add 121== 1 if 122.Fa rem 123== 124.Dv NULL ) 125in order to suit a given generator. 126.Pp 127If 128.Fa safe 129is true, it will be a safe prime (i.e. a prime p so that (p-1)/2 130is also prime). 131.Pp 132The prime number generation has a negligible error probability. 133.Pp 134.Fn BN_is_prime_ex 135and 136.Fn BN_is_prime_fasttest_ex 137test if the number 138.Fa p 139is prime. 140The following tests are performed until one of them shows that 141.Fa p 142is composite; if 143.Fa p 144passes all these tests, it is considered prime. 145.Pp 146.Fn BN_is_prime_fasttest_ex , 147when called with 148.Fa do_trial_division 149== 1, first attempts trial division by a number of small primes; 150if no divisors are found by this test and 151.Fa cb 152is not 153.Dv NULL , 154.Sy BN_GENCB_call(cb, 1, -1) 155is called. 156If 157.Fa do_trial_division 158== 0, this test is skipped. 159.Pp 160Both 161.Fn BN_is_prime_ex 162and 163.Fn BN_is_prime_fasttest_ex 164perform a Miller-Rabin probabilistic primality test with 165.Fa nchecks 166iterations. 167If 168.Fa nchecks 169== 170.Dv BN_prime_checks , 171a number of iterations is used that yields a false positive rate of at 172most 2^-80 for random input. 173.Pp 174If 175.Fa cb 176is not 177.Dv NULL , 178.Fa BN_GENCB_call cb 1 j 179is called after the j-th iteration (j = 0, 1, ...). 180.Fa ctx 181is a pre-allocated 182.Vt BN_CTX 183(to save the overhead of allocating and freeing the structure in a 184loop), or 185.Dv NULL . 186.Pp 187.Fn BN_GENCB_call 188calls the callback function held in the 189.Vt BN_GENCB 190structure and passes the ints 191.Fa a 192and 193.Fa b 194as arguments. 195There are two types of 196.Vt BN_GENCB 197structures that are supported: "new" style and "old" style. 198New programs should prefer the "new" style, whilst the "old" style is 199provided for backwards compatibility purposes. 200.Pp 201For "new" style callbacks a 202.Vt BN_GENCB 203structure should be initialised with a call to 204.Fn BN_GENCB_set , 205where 206.Fa gencb 207is a 208.Vt BN_GENCB * , 209.Fa callback 210is of type 211.Vt int (*callback)(int, int, BN_GENCB *) 212and 213.Fa cb_arg 214is a 215.Vt void * . 216"Old" style callbacks are the same except they are initialised with a 217call to 218.Fn BN_GENCB_set_old 219and 220.Fa callback 221is of type 222.Vt void (*callback)(int, int, void *) . 223.Pp 224A callback is invoked through a call to 225.Fn BN_GENCB_call . 226This will check the type of the callback and will invoke 227.Fn callback a b gencb 228for new style callbacks or 229.Fn callback a b cb_arg 230for old style. 231.Pp 232.Fn BN_generate_prime 233(deprecated) works in the same way as 234.Fn BN_generate_prime_ex 235but expects an old style callback function directly in the 236.Fa callback 237parameter, and an argument to pass to it in the 238.Fa cb_arg . 239Similarly 240.Fn BN_is_prime 241and 242.Fn BN_is_prime_fasttest 243are deprecated and can be compared to 244.Fn BN_is_prime_ex 245and 246.Fn BN_is_prime_fasttest_ex 247respectively. 248.Sh RETURN VALUES 249.Fn BN_generate_prime_ex 250returns 1 on success or 0 on error. 251.Pp 252.Fn BN_is_prime_ex , 253.Fn BN_is_prime_fasttest_ex , 254.Fn BN_is_prime , 255and 256.Fn BN_is_prime_fasttest 257return 0 if the number is composite, 1 if it is prime with an error 258probability of less than 259.Pf 0.25^ Fa nchecks , 260and -1 on error. 261.Pp 262.Fn BN_generate_prime 263returns the prime number on success, 264.Dv NULL 265otherwise. 266.Pp 267Callback functions should return 1 on success or 0 on error. 268.Pp 269The error codes can be obtained by 270.Xr ERR_get_error 3 . 271.Sh SEE ALSO 272.Xr bn 3 , 273.Xr ERR_get_error 3 , 274.Xr rand 3 275.Sh HISTORY 276The 277.Fa cb_arg 278arguments to 279.Fn BN_generate_prime 280and to 281.Fn BN_is_prime 282were added in SSLeay 0.9.0. 283The 284.Fa ret 285argument to 286.Fn BN_generate_prime 287was added in SSLeay 0.9.1. 288.Fn BN_is_prime_fasttest 289was added in OpenSSL 0.9.5. 290