BN_NUM_BYTES(3)
π 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 useBN_num_bits()on RSA/DH/DSA key numbers and expect a clean512,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
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2022 Ingo Schwarze <schwarze@openbsd.org>
- Copyright (c) 2000, 2004 The OpenSSL Project. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms