BN_NUM_BYTES(3)

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

πŸŽ€ NAME πŸŽ€

okay so welcome to the BN_NUM_BYTES(3) page, straight out of the Library Functions Manual πŸ’… this is the lil squad that’s all about getting BIGNUM size:

  • BN_num_bits_word, BN_num_bits, BN_num_bytes β€” get BIGNUM size ✨

πŸ¦‹ SYNOPSIS πŸ¦‹

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

int
BN_num_bits_word(BN_ULONG w);

int
BN_num_bits(const BIGNUM *a);

int
BN_num_bytes(const BIGNUM *a);

🌸 DESCRIPTION 🌸

So BN_num_bits_word() returns the number of significant bits in w β€” aka the minimum number of digits you’d need to write w as a binary number, no cap. Except when the argument is 0, the math is literally:

floor(log2(w)) + 1.

BN_ULONG is a macro that expands to unsigned long (= uint64_t) on _LP64 platforms and unsigned int (= uint32_t) elsewhere. iykyk 🫢

BN_num_bits() returns the number of significant bits in the value of the BIGNUM *a, following the same principle as BN_num_bits_word(). same energy, different parameter πŸ’«

BN_num_bytes() is a macro that returns the number of significant bytes in a β€” i.e. the minimum number of bytes needed to store the value of a, that is, BN_num_bits(a) divided by eight and rounded up to the next integer number. slay πŸ’…


🌈 RETURN VALUES 🌈

BN_num_bits_word() returns the number of significant bits in w or 0 if w is 0. The maximum return value that can occur is BN_BITS2, which is 64 on _LP64 platforms and 32 elsewhere. iconic max values, honestly πŸ‘‘

BN_num_bits() returns the number of significant bits and BN_num_bytes() the number of significant bytes in a, or 0 if the value of a is 0. periodt πŸ’–


πŸ”₯ SEE ALSO πŸ”₯

go read these next bestie, the lore is interconnected 🧚


⭐ HISTORY ⭐

BN_num_bytes() and BN_num_bits() first appeared in SSLeay 0.5.1. BN_num_bits_word() first appeared in SSLeay 0.5.2. These functions have been available since OpenBSD 2.4. vintage behavior, but make it still relevant πŸ’œ


πŸ’€ CAVEATS πŸ’€

Some have tried using BN_num_bits() on individual numbers in RSA keys, DH keys and DSA keys, and found that they don’t always come up with the number of bits they expected β€” something like 512, 1024, 2048, ... and they were like ??? 😭

This is because generating a number with some specific number of bits doesn’t always set the highest bits, thereby making the number of significant bits a little smaller. so not a bug, just bit math being bit math 🫠

If you want to know the "key size" of such a key, use functions like RSA_size(3), DH_size(3), and DSA_size(3). trust, that’s the move πŸ™ˆ


bestie warning: don’t use BN_num_bits() on RSA/DH/DSA key numbers and expect a clean 512, 1024, 2048, etc. it’s giving β€œsignificant bits,” not necessarily β€œkey size” πŸ’…

and that’s the page, mwah πŸ’‹ brought to you by Debian, dated June 8, 2025, with the footer tag BN_NUM_BYTES(3) β€” stay cryptographic, bestie πŸŽ€βœ¨

πŸ“¦ where this page comes from