BN_COPY(3)

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

πŸ’… BN_COPY(3) β€” the libcrypto bignum-copying girlies

okay so real talk, bestie β€” we're in the libcrypto part of the OpenBSD-ish manual pages, section 3, and today's main characters are three little functions that copy BIGNUMs around. yes, that's the whole vibe. let's get into it ✨


🌸 SYNOPSIS 🌸

#include <openssl/bn.h>

BIGNUM *
BN_copy(BIGNUM *to, const BIGNUM *from);

BIGNUM *
BN_dup(const BIGNUM *from);

void
BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags);

link against libcrypto like the good little C gremlin you are πŸ’–


πŸŽ€ DESCRIPTION πŸŽ€

BN_copy β€” the simple slay

BN_copy() copies from into to. that's it. that's the function. it's giving "no thoughts, head empty, just memcpy energy" and honestly? iconic.

BN_dup β€” the clone glows up

BN_dup() creates a brand new BIGNUM containing the value from from. new allocation, new bignum, same value. it's giving twin main character energy πŸ‘‘

BN_with_flags β€” the risky one, proceed with caution 😭

okay bestie, listen. BN_with_flags() creates a temporary shallow copy of b in dest. and when I say "temporary" and "shallow" I need you to hear me, because this function places significant restrictions on the copied data. applications that do not adhere to these restrictions may encounter unexpected side effects or crashes. so like. for that reason alone, the use of this function is discouraged. ngl, it's giving "we don't gatekeep but we also don't recommend" πŸ’€

any flags you hand it in flags will be set in dest in addition to any flags already set in b. for example β€” and this is the cute lil use case β€” you can use it to create a temporary copy of a BIGNUM with the BN_FLG_CONSTTIME flag set, for constant time operations. ⚑

the temporary copy in dest will share some internal state with b. so because of that, these restrictions apply to how you use dest:

  • dest should be a newly allocated BIGNUM obtained via a call to BN_new(3). it should not have been used for other purposes or initialised in any way.
  • dest must only be used in "read-only" operations, i.e. typically those functions where the relevant parameter is declared "const".
  • dest must be used and freed before any further subsequent use of b.
🚨 fr fr do not skip these. this is the "shallow copy shares state" warning and it is not a drill. misuse = crashes, and nobody wants that for you 🫢

πŸ’– RETURN VALUES πŸ’–

  • BN_copy() returns to on success, or NULL on error.
  • BN_dup() returns the new BIGNUM, or NULL on error.
  • BN_with_flags() β€” it's void, bestie, it returns nothing. it just does things.

need the actual error codes? grab them with ERR_get_error(3). πŸ›βœ¨


πŸ¦‹ SEE ALSO πŸ¦‹


🌈 HISTORY 🌈

BN_copy() and BN_dup() first appeared in SSLeay 0.5.1, and have been available since OpenBSD 2.4. vintage. antique. still serving.

BN_with_flags() first appeared in OpenSSL 0.9.7h and 0.9.8a, and has been available since OpenBSD 4.0. the risky queen arrived later πŸ’…


and that's the whole page, bestie β€” shoutout to Debian, this one's dated June 8, 2025, and it's filed under BN_COPY(3). now go forth and copy your bignums responsibly πŸŽ€πŸ«ΆπŸ’œ

πŸ“¦ where this page comes from