BN_GENERATE_PRIME(3)
π«Ά NAME π«Ά
okay so bestie, welcome to BN_GENERATE_PRIME(3) β the libcrypto manual page for the whole prime-making / primality-checking friend group: BN_is_prime_ex, BN_is_prime_fasttest_ex, BN_generate_prime_ex, BN_GENCB_call, BN_GENCB_new, BN_GENCB_free, BN_GENCB_set, BN_GENCB_get_arg, and BN_GENCB_set_old. their collective vibe? generate primes and test for primality. periodt π
β¨ SYNOPSIS β¨
library βlibcryptoβ
#include <openssl/bn.h>
int
BN_is_prime_ex(const BIGNUM *a, int nchecks, BN_CTX *ctx, BN_GENCB *cb);
int
BN_is_prime_fasttest_ex(const BIGNUM *a, int nchecks, BN_CTX *ctx,
int do_trial_division, BN_GENCB *cb);
int
BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
const BIGNUM *modulus, const BIGNUM *remainder, BN_GENCB *cb);
int
BN_GENCB_call(BN_GENCB *cb, int state_code, int serial_number);
BN_GENCB *
BN_GENCB_new(void);
void
BN_GENCB_free(BN_GENCB *cb);
void
BN_GENCB_set(BN_GENCB *cb, int (*cb_fp)(int, int, BN_GENCB *),
void *cb_arg);
void *
BN_GENCB_get_arg(BN_GENCB *cb);
Deprecated:
void
BN_GENCB_set_old(BN_GENCB *cb, void (*cb_fp)(int, int, void *),
void *cb_arg);
π DESCRIPTION π
so BN_is_prime_ex() and BN_is_prime_fasttest_ex() are the βis this number a prime?β besties. in LibreSSL they behave identically β they use the Baillie-Pomerance-Selfridge-Wagstaff algorithm plus a bunch of Miller-Rabin rounds based on checks. and the do_trial_division argument? literally ignored. π
now hereβs the tea: nobody knows whether any composite number exists that the Baillie-PSW algorithm would misclassify as prime. some people suspect there might be infinitely many such numbers, but not a single one is currently known. what is known is that no such number exists below 2^64. slay for small numbers, i guess π
to lower the chance of a composite sneaking past the primality tests in BN_is_prime_fasttest_ex() and BN_is_prime_ex(), a number of rounds of the probabilistic Miller-Rabin test gets performed. if checks is positive, thatβs the number of rounds. if itβs zero or the special value BN_prime_checks, a suitable number of rounds is calculated from the bit length of a. iykyk β¨
if NULL is passed for the ctx argument, these functions allocate a BN_CTX object internally when they need one and free it before returning. alternatively β to save the overhead of allocating and freeing that object on every call β the caller can pre-allocate a BN_CTX object and pass it in the ctx argument. efficiency queen π
BN_generate_prime_ex() generates a pseudo-random prime number of at least bit length bits and places it in ret. primality of ret is tested internally using BN_is_prime_ex(). consequently, for bits larger than 64, it is theoretically possible that this function might place a composite number into ret; the probability of such an event is unknown but very small. not me being theoretical about it π«
the prime may have to fulfill additional requirements for use in Diffie-Hellman key exchange:
- if
modulusis notNULL, a prime is generated that fulfills the conditionret % modulus = remainder. if theremainderargument isNULL,1is used as the desired remainder. - if the
safeargument is non-zero, a safe prime is generated, that is,(ret - 1)/2is also prime.
if cb is not NULL, it gets used like this:
BN_GENCB_call(cb, 0, serial_number)is called after generating a potential prime number.- the
state_codeof1is reserved for callbacks during primality testing, but LibreSSL performs no such callbacks. - when
safeis non-zero and a safe prime has been found,BN_GENCB_call(cb, 2, serial_number)is called. - the callers of
BN_generate_prime_ex()may callBN_GENCB_call()with other values as described in their respective manual pages; see βSEE ALSOβ.
in all cases, the serial_number is the number of candidates that have already been discarded for not being prime; that is, serial_number is 0 for the first candidate and then incremented whenever a new candidate is generated. tracking the flops, fr fr π
BN_GENCB_call() calls the callback function held in cb and passes the state_code and the serial_number as arguments. if cb is NULL or does not contain a callback function, no action occurs. no callback, no drama π
BN_GENCB_new() allocates a new BN_GENCB object. fresh out the box π
BN_GENCB_free() frees cb. if cb is NULL, no action occurs. clean up after yourself, bestie π§
BN_GENCB_set() initialises cb to use the callback function pointer cb_fp and the additional callback argument cb_arg.
the deprecated function BN_GENCB_set_old() initialises cb to use the old-style callback function pointer cb_fp and the additional callback argument cb_arg. itβs giving legacy behavior, handle with care π«Ά
π RETURN VALUES π
BN_is_prime_ex()andBN_is_prime_fasttest_ex()return0if the number is composite,1if it is prime with a very small error probability, or-1on error.BN_generate_prime_ex()returns1on success or0on error.BN_GENCB_call()returns1on success, including whencbisNULLor does not contain a callback function, or0on error.BN_GENCB_new()returns a pointer to the newly allocatedBN_GENCBobject orNULLif memory allocation fails.- the callback functions pointed to by the
cb_fparguments are supposed to return1on success or0on error. BN_GENCB_get_arg()returns thecb_argpointer that was previously stored incbusingBN_GENCB_set()orBN_GENCB_set_old().- in some cases, error codes can be obtained by
ERR_get_error(3).
π¦ SEE ALSO π¦
πΈ HISTORY πΈ
BN_generate_prime_ex(), BN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_GENCB_call(), BN_GENCB_set_old(), and BN_GENCB_set() first appeared in OpenSSL 0.9.8 and have been available since OpenBSD 4.5.
BN_GENCB_new(), BN_GENCB_free(), and BN_GENCB_get_arg() first appeared in OpenSSL 1.1.0 and have been available since OpenBSD 6.3.
okay thatβs the whole page, bestie β its footer credits Debian, dated September 1, 2026. go forth and generate primes responsibly πβ¨
π¦ where this page comes from
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2022 Ingo Schwarze <schwarze@openbsd.org>
- Copyright (c) 2000, 2003, 2013, 2014, 2018 The OpenSSL Project
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms