BN_RAND(3ossl)
π NAME π
okay so this is the BN_RAND(3ossl) page from OpenSSL, and it's the manual for a whole little friend group of pseudo-random number makers: 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. literally a whole squad π
β¨
β¨ SYNOPSIS β¨
first up, the include bestie:
#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);
The following functions have been deprecated since OpenSSL 3.0, and can be hidden 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()β generates a cryptographically strong pseudo-random number ofbitsin length and security strength at leaststrengthbits using the random number generator for the library context associated withctx. The function stores the generated data inrnd. The parameterctxmay beNULLin which case the default library context is used. Ifbitsis less than zero, or too small to accommodate the requirements specified by thetopandbottomparameters, an error is returned. Thetopparameters specifies requirements on the most significant bit of the generated number. If it isBN_RAND_TOP_ANY, there is no constraint. If it isBN_RAND_TOP_ONE, the top bit must be one. If it isBN_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 have2*bitslength. IfbottomisBN_RAND_BOTTOM_ODD, the number will be odd; if it isBN_RAND_BOTTOM_ANYit can be odd or even. Ifbitsis 1 thentopcannot also beBN_RAND_TOP_TWO.BN_rand()β is the same asBN_rand_ex()except that the default library context is always used. simple as that π«ΆBN_rand_range_ex()β generates a cryptographically strong pseudo-random numberrnd, of security strength at leaststrengthbits, in the range0 <= rnd < rangeusing the random number generator for the library context associated withctx. The parameterctxmay beNULLin which case the default library context is used.BN_rand_range()β is the same asBN_rand_range_ex()except that the default library context is always used.BN_priv_rand_ex(),BN_priv_rand(),BN_priv_rand_rand_ex()andBN_priv_rand_range()β have the same semantics asBN_rand_ex(),BN_rand(),BN_rand_range_ex()andBN_rand_range()respectively. They are intended to be used for generating values that should remain private, and mirror the same difference betweenRAND_bytes(3)andRAND_priv_bytes(3). private vibes only ππ
π 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.
bestie please do not just assume the random is randoming. check the return value, fr fr π
π RETURN VALUES π
The functions return 1 on success, 0 on error. The error codes can be obtained by ERR_get_error(3).
π¦ 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>.
mwah π this page is from OpenSSL 3.5.0, dated 2026-09-25, and yes bestie it's BN_RAND(3ossl) signing off π«Άβ¨
π¦ where this page comes from
- opensuse-leap 16.0: openssl-3-doc 3.5.0-160000.11.1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms