BN_RAND(3ossl)
π BN_RAND π β okay bestie, we're making random numbers now
This is the OpenSSL BN_RAND page (manual name BN_RAND, from the 3ossl manual section, duh) and it's literally all about generating pseudo-random numbers. Sit down, hydrate, let's go π«Ά
β¨ NAME β¨
BN_rand_ex, BN_rand, BN_priv_rand_ex, BN_priv_rand, BN_pseudo_rand, BN_rand_range_ex, BN_rand_range, BN_priv_rand_range_ex, BN_priv_rand_range, BN_pseudo_rand_range β generate pseudo-random number π²β¨
πΈ SYNOPSIS πΈ
#include <openssl/bn.h>
int BN_rand_ex(BIGNUM *rnd, int bits, int top, int bottom,
unsigned int strength, BN_CTX *ctx);
int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);
int BN_priv_rand_ex(BIGNUM *rnd, int bits, int top, int bottom,
unsigned int strength, BN_CTX *ctx);
int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom);
int BN_rand_range_ex(BIGNUM *rnd, const BIGNUM *range, unsigned int strength,
BN_CTX *ctx);
int BN_rand_range(BIGNUM *rnd, const BIGNUM *range);
int BN_priv_rand_range_ex(BIGNUM *rnd, const BIGNUM *range, unsigned int strength,
BN_CTX *ctx);
int BN_priv_rand_range(BIGNUM *rnd, const BIGNUM *range);
β οΈ okay so these next ones are giving "old news" energy: the following functions have been deprecated since OpenSSL 3.0, and you can hide them entirely by defining OPENSSL_API_COMPAT with a suitable version value, see openssl_user_macros(7):
int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);
int BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range);
π¦ DESCRIPTION π¦
BN_rand_ex() β the main character β¨
BN_rand_ex() generates a cryptographically strong pseudo-random number of bits in length and security strength at least strength bits, using the random number generator for the library context associated with ctx. The function stores the generated data in rnd. The parameter ctx may be NULL, in which case the default library context is used. π
If bits is less than zero, or too small to accommodate the requirements specified by the top and bottom parameters, an error is returned. Not negotiable, it just says no.
The top parameter specifies requirements on the most significant bit of the generated number:
BN_RAND_TOP_ANYβ no constraint, live your truth πBN_RAND_TOP_ONEβ the top bit must be oneBN_RAND_TOP_TWOβ the two most significant bits of the number will be set to1, so that the product of two such random numbers will always have2*bitslength
And bottom:
BN_RAND_BOTTOM_ODDβ the number will be oddBN_RAND_BOTTOM_ANYβ it can be odd or even
π¨ Also, ifbitsis1thentopcannot also beBN_RAND_TOP_TWO. That combo is not it, bestie.
BN_rand()
BN_rand() is the same as BN_rand_ex() except that the default library context is always used. Same vibes, less options.
BN_rand_range_ex()
BN_rand_range_ex() generates a cryptographically strong pseudo-random number rnd, of security strength at least strength bits, in the range 0 <= rnd < range using the random number generator for the library context associated with ctx. The parameter ctx may be NULL, in which case the default library context is used. π
BN_rand_range()
BN_rand_range() is the same as BN_rand_range_ex() except that the default library context is always used.
the priv ones π
BN_priv_rand_ex(), BN_priv_rand(), BN_priv_rand_rand_ex() and BN_priv_rand_range() have the same semantics as BN_rand_ex(), BN_rand(), BN_rand_range_ex() and BN_rand_range() respectively. They are intended to be used for generating values that should remain private, and mirror the same difference between RAND_bytes(3) and RAND_priv_bytes(3). π€« iykyk
π NOTES π
Always check the error return value of these functions and do not take randomness for granted: an error occurs if the CSPRNG has not been seeded with enough randomness to ensure an unpredictable byte sequence.
π fr fr do not skip the error check. Randomness is not a given, it's a whole situation.
π«Ά RETURN VALUES π«Ά
The functions return 1 on success, 0 on error. The error codes can be obtained by ERR_get_error(3). Simple as. β¨
π SEE ALSO π
ERR_get_error(3), RAND_add(3), RAND_bytes(3), RAND_priv_bytes(3), RAND(7), EVP_RAND(7)
π HISTORY π
- Starting with OpenSSL release 1.1.0,
BN_pseudo_rand()has been identical toBN_rand()andBN_pseudo_rand_range()has been identical toBN_rand_range(). TheBN_pseudo_rand()andBN_pseudo_rand_range()functions were deprecated in OpenSSL 3.0. - The
BN_priv_rand()andBN_priv_rand_range()functions were added in OpenSSL 1.1.1. - The
BN_rand_ex(),BN_priv_rand_ex(),BN_rand_range_ex()andBN_priv_rand_range_ex()functions were added in OpenSSL 3.0.
π COPYRIGHT π
Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
and that's the whole page, bestie π go forth and generate responsibly β this was version 3.5.9, dated 2026-09-29, and yes it slayed πβ¨
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms