BN_RAND(3)

πŸ“¦ version 4.4.0, in alpine 3.24, arch rolling

πŸ’– BN_RAND(3) πŸ’–

okay so bestie, this is a page from the Library Functions Manual β€” yes, that's the manual name β€” and it's all about libcrypto making pseudo-random numbers. buckle up, it's giving crypto girlie πŸ’….

🌸 NAME 🌸

BN_rand, BN_rand_range, BN_pseudo_rand, BN_pseudo_rand_range β€” generate pseudo-random number

✨ SYNOPSIS ✨

The page says it's from libcrypto and prints this exact synopsis:

library β€œlibcrypto” #include <openssl/bn.h>

int
BN_rand(BIGNUM *rnd, int bits, int top, int bottom);

int
BN_rand_range(BIGNUM *rnd, const BIGNUM *range);

πŸ¦‹ DESCRIPTION πŸ¦‹

BN_rand() is the main character: it generates a cryptographically strong pseudo-random number of bits in length and stores it in rnd. The top parameter has three moods:

  • If top is BN_RAND_TOP_ANY, the most significant bit of the random number can be zero.
  • If top is BN_RAND_TOP_ONE, the most significant bit is set to 1.
  • If top is BN_RAND_TOP_TWO, the two most significant bits of the number will be set to 1, so that the product of two such random numbers will always have 2*bits length.

Then there's bottom, which is its own little vibe check:

  • If bottom is BN_RAND_BOTTOM_ODD, the number will be odd.
  • If it is BN_RAND_BOTTOM_ANY, it can be odd or even.

Real talk: the value of bits must be zero or greater. If bits is +1 then top cannot be BN_RAND_TOP_TWO. periodt.

BN_rand_range() generates a cryptographically strong pseudo-random number rnd in the range 0 <= rnd < range.

Also heads up: BN_pseudo_rand() is a deprecated alias for BN_rand(), and BN_pseudo_rand_range() for BN_rand_range(). yes, deprecated β€” the manual says so, so treat those as the old names.

πŸ’… RETURN VALUES πŸ’…

The functions return 1 on success, 0 on error. The error codes can be obtained by ERR_get_error(3).

🫢 SEE ALSO 🫢

πŸ‘‘ HISTORY πŸ‘‘

BN_rand() first appeared in SSLeay 0.5.1 and has been available since OpenBSD 2.4.

The top == -1 case and the function BN_rand_range() first appeared in OpenSSL 0.9.6a and have been available since OpenBSD 3.0.


πŸ’Œ from Debian, dated June 8, 2025 β€” this page is BN_RAND(3) β€” byeee, bestie 🫢

πŸ“¦ where this page comes from