BN_NUM_BYTES(3ossl)

πŸ“¦ version 3.5.0, in opensuse-leap 16.0

πŸ’… BN_NUM_BYTES β€” get BIGNUM size ✨

okay bestie, welcome to BN_NUM_BYTES(3ossl) β€” the OpenSSL page that answers the eternal question: "how big is this number actually, like really?" πŸŽ€


✨ 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 vibe check on each one, bestie πŸ‘‘

  • BN_num_bytes() β€” tells you the size of a BIGNUM in bytes. That's it, that's the whole gig πŸ’…
  • BN_num_bits_word() β€” returns the number of significant bits in a word. Not me about to blow your mind: take 0x00000432 as an example β€” it returns 11, not 16, and not 32 😭 Basically, except for a zero, it returns floor(log2(w))+1. Periodt.
  • BN_num_bits() β€” returns the number of significant bits in a BIGNUM, following the exact same principle as BN_num_bits_word() 🌈

oh and β€” plot twist β€” BN_num_bytes() is a macro πŸ™ˆ lowkey important, keep that in your pocket.


⭐ RETURN VALUES ⭐

The size. that's the answer. iconic brevity, no notes 🫢


πŸ”₯ NOTES πŸ”₯

okay so this is the part where people get humbled, listen up πŸ’€

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 no, it's not haunted β€” it's 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 lower. the math said "close enough" basically 🧚

so if you 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 β€” although there's no real guarantee that will match the "key size", just a lot more probability πŸ‘€

fr fr, don't @ me when your bit count comes back one short. it's giving expected behaviour πŸ’…


πŸ¦‹ SEE ALSO πŸ¦‹

go read those next, bestie, they're the main characters of this friend group πŸ‘‘


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 masterpiece is from version 3.5.0, dated 2026-09-25 β€” go forth and count your bits responsibly, ily, mwah πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from