BN_SWAP(3)

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

πŸ’… BN_SWAP(3) β€” Library Functions Manual πŸ’…

okay so bestie, welcome to the group chat for BN_swap and BN_consttime_swap β€” we're exchanging BIGNUMs today and honestly it's giving iconic pointer-and-buffer drama πŸ’–

✨ NAME ✨

BN_swap, BN_consttime_swap β€” exchange BIGNUMs

🌸 SYNOPSIS 🌸

       library β€œlibcrypto” #include <openssl/bn.h>

       void
       BN_swap(BIGNUM *a, BIGNUM *b);

       void
       BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the tea: BN_swap() and BN_consttime_swap() exchange the values of a and b. That's the whole vibe. But they do it in two very different ways, so let's get into it, no cap.

  • BN_swap() β€” this one implements the exchange by exchanging the pointers to the data buffers of a and b, and also exchanging the values of the BN_FLG_STATIC_DATA bits. Consequently, the operation is fast, and execution time does not depend on any properties of the two numbers. However, execution time obviously differs between swapping (by calling this function) and not swapping (by not calling this function). Not me pointing out the obvious, but the man page said it, so I'm saying it too πŸ’…
  • BN_consttime_swap() β€” this icon only performs the exchange if the condition is non-zero; otherwise, it has no effect. It implements the exchange by exchanging the contents of the data buffers rather than the pointers to the data buffers. This is slower, but implemented in such a way that the execution time is not only independent of the properties of the two numbers, but also independent of the condition argument, i.e. the same for swapping or not swapping. Execution time does however grow in an approximately linear manner with the nwords argument. Slay for constant-time vibes, lowkey.
🚨 warning bestie: BN_consttime_swap() calls abort(3) if at least one of a or b has fewer than nwords data words allocated, or more than nwords data words are currently in use in at least one of them. So like, check your word counts before you send it, fr fr 😭

🌈 SEE ALSO 🌈

go bother these icons next, bestie:

πŸ‘‘ HISTORY πŸ‘‘

  • BN_swap() first appeared in OpenSSL 0.9.7 and has been available since OpenBSD 3.2.
  • BN_consttime_swap() first appeared in OpenSSL 1.0.1g and has been available since OpenBSD 5.6.

okay that's the whole page, mwah πŸ’‹ β€” this cutie is sourced from Debian, dated June 8, 2025, and filed under BN_SWAP(3), periodt 🫢

πŸ“¦ where this page comes from