BN_NUM_BYTES(3ossl)

๐Ÿ’– BN_NUM_BYTES ๐Ÿ’–

okay bestie so this is the BN_NUM_BYTES(3ossl) page from the OpenSSL manual, and it's literally all about asking your BIGNUM "girl how big are you actually" ๐Ÿ’… three different flavours of that question, to be exact.


โœจ NAME โœจ

BN_num_bits, BN_num_bytes, BN_num_bits_word โ€” get BIGNUM size ๐ŸŽ€


โœจ SYNOPSIS โœจ

#include <openssl/bn.h>

int BN_num_bytes(const BIGNUM *a);

int BN_num_bits(const BIGNUM *a);

int BN_num_bits_word(BN_ULONG w);

โœจ DESCRIPTION โœจ

so here's the tea, one function at a time ๐Ÿซ–

  • BN_num_bytes() โ€” hands you the size of a BIGNUM in bytes. that's it, that's the whole job, and she does it flawlessly. ๐Ÿ’–
  • BN_num_bits_word() โ€” returns the number of significant bits in a word. ok let's do the classic example: take 0x00000432. she says 11. not 16. not 32. eleven. โœจ basically โ€” except for a zero โ€” she returns floor(log2(w))+1. math queen behavior, no cap. ๐Ÿงฎ๐Ÿ‘‘
  • BN_num_bits() โ€” same energy as BN_num_bits_word() but for a whole BIGNUM: she gives you the number of significant bits, following that exact same principle. ๐ŸŒธ

oh and important lil detail: BN_num_bytes() is a macro. not a real function, a macro. just so you know what you're dealing with, bestie ๐Ÿฆ‹


โœจ RETURN VALUES โœจ

The size. that's it. that's the answer. iconic, unbothered, brief. ๐Ÿ’…


โœจ NOTES โœจ

ok so listen, this is the part where people get confused and honestly I get it ๐Ÿ˜ญ

some people have tried calling BN_num_bits() on individual numbers inside RSA keys, DH keys, and DSA keys, and found that it doesn't always give back the number of bits they expected โ€” like, they were expecting something like 512, 1024, 2048, ... and got something else. ๐Ÿ™ˆ

and the reason is: generating a number with some specific number of bits doesn't always set the highest bits, which makes the number of significant bits come out a little lower. the vibe check fails, basically. ๐Ÿ’€

so if you actually want to know the "key size" of a key like that, don't do the naive thing! instead either use functions like RSA_size(), DH_size() and DSA_size(), or use BN_num_bytes() and multiply with 8. and ngl, even then there's no real guarantee that it'll match the "key size" โ€” it's just a lot more probability. so like. set your expectations accordingly, bestie. ๐Ÿซถ

โœจ SEE ALSO โœจ

DH_size(3), DSA_size(3), RSA_size(3) โ€” go read these, they're the moment ๐Ÿ”ฅ


Copyright 2000-2017 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!! ๐ŸŽ€ this was OpenSSL 3.5.9, dated 2026-09-29 โ€” so if you're reading this in the future, hiii, hope your BIGNUMs are all behaving ๐Ÿ’–โœจ๐Ÿฆ‹ mwah, go count some bits, ily ๐Ÿซถ๐Ÿ’…

๐Ÿ“ฆ where this page comes from