BN_NUM_BYTES(3ossl)
π BN_NUM_BYTES π
ok so this is page BN_NUM_BYTES(3ossl) from the OpenSSL manual, and bestie, it's literally the page that answers "how big is my BIGNUM actually" β three little functions, zero drama (well, one tiny drama, see NOTES, iykyk). Let's get into it π«Ά
β¨ 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 β¨
Okay so first up, BN_num_bytes() returns the size of a BIGNUM in bytes. That's it, that's the whole vibe π
Then BN_num_bits_word() returns the number of significant bits in a word. Like, if we take 0x00000432 as an example, it returns 11 β not 16, not 32. Not me being shocked the leading zeros don't count π Basically, except for a zero, it returns floor(log2(w))+1.
And BN_num_bits() returns the number of significant bits in a BIGNUM, following the exact same principle as BN_num_bits_word(). Matching energy, slay β¨
Oh and psst β BN_num_bytes() is a macro π
β¨ RETURN VALUES β¨
The size. Short answer, iconic answer π
β¨ NOTES β¨
okay so story time π Some people have tried using BN_num_bits() on individual numbers inside RSA keys, DH keys and DSA keys, and found that it doesn't always come up with the number of bits they expected (you know, 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 β which makes the number of significant bits come out a little lower. It's giving "the vibes are off but the math is fine" π
So if you actually want to know the "key size" of such a key, either use functions like RSA_size(), DH_size() and DSA_size(), or use BN_num_bytes() and multiply with 8. Full transparency though: there's no real guarantee that'll match the "key size" β it's just a lot more probability. lowkey not a promise, highkey your best bet π«Ά
no cap: multiply-by-8 is the vibe, not the contract. don't come for me if it's off by a bit π
β¨ SEE ALSO β¨
DH_size(3), DSA_size(3), RSA_size(3) β go read these next bestie, they're the glow-up versions π₯
β¨ COPYRIGHT β¨
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 page!! π¦ this was version 3.5.6, dated 2026-04-07, and it's the BN_NUM_BYTES(3ossl) page β go forth and count your bits, bestie, you're gonna slay ππ
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms