BN_CTX_NEW(3)

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

πŸ’… BN_CTX_NEW(3) β€” the girlies' guide to BN_CTX_new & BN_CTX_free πŸ’…

okay so real talk, this is a page from the Library Functions Manual, and honestly? it's a little one but it's iconic. it's all about the BN_CTX structure β€” the backstage dressing room where BIGNUM temp variables hang out. let's get into it πŸŽ€


✨ NAME ✨

BN_CTX_new, BN_CTX_free β€” allocate and free BN_CTX structures

like… one makes it, one frees it. the circle of life, bestie πŸ¦‹


✨ SYNOPSIS ✨

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

BN_CTX *
BN_CTX_new(void);

void
BN_CTX_free(BN_CTX *c);

she's from libcrypto, she lives in <openssl/bn.h>, and yes that include line is written exactly like that, don't @ me 😌


✨ DESCRIPTION ✨

so what even is a BN_CTX, you ask? 🧚

a BN_CTX is a structure that holds BIGNUM temporary variables used by library functions. and the reason it exists is honestly so real: dynamically allocating memory to create BIGNUMs is rather expensive when you're doing it over and over alongside repeated subroutine calls. so instead of constantly malloc-ing like a chaotic mess, the BN_CTX structure just… holds the temps for you. main character energy tbh πŸ‘‘

BN_CTX_new() β€” allocates and initializes a BN_CTX structure. that's it, that's the whole vibe. spawns her into existence ✨

BN_CTX_free() β€” frees the components of the BN_CTX and, if it was created by BN_CTX_new(), also the structure itself. you're cleaning up after the party bestie, it's called being responsible πŸ’…

⚠️ okay so important caveat, listen up: if BN_CTX_start(3) has been used on the BN_CTX, then BN_CTX_end(3) must be called before the BN_CTX may be freed by BN_CTX_free(). like, you gotta close your loops before you free the thing. no skipping steps πŸ™ˆ

and if c is a NULL pointer, no action occurs. she's unbothered. nothing happens. slay 🌸


✨ RETURN VALUES ✨

BN_CTX_new() returns a pointer to the BN_CTX 🫢

but if the allocation fails? it returns NULL and sets an error code that you can grab with ERR_get_error(3). so like, always check your returns, bestie, that's the lesson 😭


✨ SEE ALSO ✨

go read those next, iykyk πŸ“š


✨ HISTORY ✨

BN_CTX_new() and BN_CTX_free() first appeared in SSLeay 0.5.1 and have been available since OpenBSD 2.4. lowkey legendary. they've been doing this since before most of us existed πŸ’€


mwah πŸ’œ that's the whole page β€” brought to you by the Debian build, dated June 8, 2025, page BN_CTX_NEW(3). go forth and allocate responsibly, bestie πŸŽ€βœ¨

πŸ“¦ where this page comes from