BN_generate_prime(3) OpenSSL BN_generate_prime(3) NNAAMMEE BN_generate_prime_ex, BN_is_prime_ex, BN_is_prime_fasttest_ex, BN_GENCB_call, BN_GENCB_set_old, BN_GENCB_set, BN_generate_prime, BN_is_prime, BN_is_prime_fasttest - generate primes and test for pri- mality SSYYNNOOPPSSIISS #include int BN_generate_prime_ex(BIGNUM *ret,int bits,int safe, const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb); int BN_is_prime_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, BN_GENCB *cb); int BN_is_prime_fasttest_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, int do_trial_division, BN_GENCB *cb); int BN_GENCB_call(BN_GENCB *cb, int a, int b); #define BN_GENCB_set_old(gencb, callback, cb_arg) ... #define BN_GENCB_set(gencb, callback, cb_arg) ... Deprecated: BIGNUM *BN_generate_prime(BIGNUM *ret, int num, int safe, BIGNUM *add, BIGNUM *rem, void (*callback)(int, int, void *), void *cb_arg); int BN_is_prime(const BIGNUM *a, int checks, void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg); int BN_is_prime_fasttest(const BIGNUM *a, int checks, void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg, int do_trial_division); DDEESSCCRRIIPPTTIIOONN _B_N___g_e_n_e_r_a_t_e___p_r_i_m_e___e_x_(_) generates a pseudo-random prime number of bit length bbiittss. If rreett is not NNUULLLL, it will be used to store the number. If ccbb is not NNUULLLL, it is used as follows: +o BBNN__GGEENNCCBB__ccaallll((ccbb,, 00,, ii)) is called after generating the i-th poten- tial prime number. +o While the number is being tested for primality, BBNN__GGEENNCCBB__ccaallll((ccbb,, 11,, jj)) is called as described below. +o When a prime has been found, BBNN__GGEENNCCBB__ccaallll((ccbb,, 22,, ii)) is called. The prime may have to fulfill additional requirements for use in Diffie-Hellman key exchange: If aadddd is not NNUULLLL, the prime will fulfill the condition p % aadddd == rreemm (p % aadddd == 1 if rreemm == NNUULLLL) in order to suit a given generator. If ssaaffee is true, it will be a safe prime (i.e. a prime p so that (p-1)/2 is also prime). The PRNG must be seeded prior to calling _B_N___g_e_n_e_r_a_t_e___p_r_i_m_e___e_x_(_). The prime number generation has a negligible error probability. _B_N___i_s___p_r_i_m_e___e_x_(_) and _B_N___i_s___p_r_i_m_e___f_a_s_t_t_e_s_t___e_x_(_) test if the number pp is prime. The following tests are performed until one of them shows that pp is composite; if pp passes all these tests, it is considered prime. _B_N___i_s___p_r_i_m_e___f_a_s_t_t_e_s_t___e_x_(_), when called with ddoo__ttrriiaall__ddiivviissiioonn ==== 11, first attempts trial division by a number of small primes; if no divi- sors are found by this test and ccbb is not NNUULLLL, BBNN__GGEENNCCBB__ccaallll((ccbb,, 11,, --11)) is called. If ddoo__ttrriiaall__ddiivviissiioonn ==== 00, this test is skipped. Both _B_N___i_s___p_r_i_m_e___e_x_(_) and _B_N___i_s___p_r_i_m_e___f_a_s_t_t_e_s_t___e_x_(_) perform a Miller- Rabin probabilistic primality test with nncchheecckkss iterations. If nncchheecckkss ==== BBNN__pprriimmee__cchheecckkss, a number of iterations is used that yields a false positive rate of at most 2^-64 for random input. The error rate depends on the size of the prime and goes down for bigger primes. The rate is 2^-80 starting at 308 bits, 2^-112 at 852 bits, 2^-128 at 1080 bits, 2^-192 at 3747 bits and 2^-256 at 6394 bits. When the source of the prime is not random or not trusted, the number of checks needs to be much higher to reach the same level of assurance: It should equal half of the targeted security level in bits (rounded up to the next integer if necessary). For instance, to reach the 128 bit security level, nncchheecckkss should be set to 64. If ccbb is not NNUULLLL, BBNN__GGEENNCCBB__ccaallll((ccbb,, 11,, jj)) is called after the j-th iteration (j = 0, 1, ...). ccttxx is a pre-allocated BBNN__CCTTXX (to save the overhead of allocating and freeing the structure in a loop), or NNUULLLL. BN_GENCB_call calls the callback function held in the BBNN__GGEENNCCBB struc- ture and passes the ints aa and bb as arguments. There are two types of BBNN__GGEENNCCBB structure that are supported: "new" style and "old" style. New programs should prefer the "new" style, whilst the "old" style is pro- vided for backwards compatibility purposes. For "new" style callbacks a BN_GENCB structure should be initialised with a call to BN_GENCB_set, where ggeennccbb is a BBNN__GGEENNCCBB **, ccaallllbbaacckk is of type iinntt ((**ccaallllbbaacckk))((iinntt,, iinntt,, BBNN__GGEENNCCBB **)) and ccbb__aarrgg is a vvooiidd **. "Old" style callbacks are the same except they are initialised with a call to BN_GENCB_set_old and ccaallllbbaacckk is of type vvooiidd ((**ccaallllbbaacckk))((iinntt,, iinntt,, vvooiidd **)). A callback is invoked through a call to BBNN__GGEENNCCBB__ccaallll. This will check the type of the callback and will invoke ccaallllbbaacckk((aa,, bb,, ggeennccbb)) for new style callbacks or ccaallllbbaacckk((aa,, bb,, ccbb__aarrgg)) for old style. BN_generate_prime (deprecated) works in the same way as BN_gener- ate_prime_ex but expects an old style callback function directly in the ccaallllbbaacckk parameter, and an argument to pass to it in the ccbb__aarrgg. Simi- larly BN_is_prime and BN_is_prime_fasttest are deprecated and can be compared to BN_is_prime_ex and BN_is_prime_fasttest_ex respectively. RREETTUURRNN VVAALLUUEESS _B_N___g_e_n_e_r_a_t_e___p_r_i_m_e___e_x_(_) return 1 on success or 0 on error. _B_N___i_s___p_r_i_m_e___e_x_(_), _B_N___i_s___p_r_i_m_e___f_a_s_t_t_e_s_t___e_x_(_), _B_N___i_s___p_r_i_m_e_(_) and _B_N___i_s___p_r_i_m_e___f_a_s_t_t_e_s_t_(_) return 0 if the number is composite, 1 if it is prime with an error probability of less than 0.25^nncchheecckkss, and -1 on error. _B_N___g_e_n_e_r_a_t_e___p_r_i_m_e_(_) returns the prime number on success, NNUULLLL other- wise. Callback functions should return 1 on success or 0 on error. The error codes can be obtained by _E_R_R___g_e_t___e_r_r_o_r(3). SSEEEE AALLSSOO _b_n(3), _E_R_R___g_e_t___e_r_r_o_r(3), _r_a_n_d(3) HHIISSTTOORRYY The ccbb__aarrgg arguments to _B_N___g_e_n_e_r_a_t_e___p_r_i_m_e_(_) and to _B_N___i_s___p_r_i_m_e_(_) were added in SSLeay 0.9.0. The rreett argument to _B_N___g_e_n_e_r_a_t_e___p_r_i_m_e_(_) was added in SSLeay 0.9.1. _B_N___i_s___p_r_i_m_e___f_a_s_t_t_e_s_t_(_) was added in OpenSSL 0.9.5. 1.0.2u 2019-12-20 BN_generate_prime(3)