BN_CTX_NEW(3ossl)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

πŸ’… BN_CTX_NEW(3ossl) β€” the BN_CTX page, bestie edition

okay so this is page BN_CTX_NEW(3ossl) from the OpenSSL manual, and we're about to make BN_CTX make sense. it's giving "allocator but make it cute". stay with me 🫢


✨ NAME ✨

BN_CTX_new_ex, BN_CTX_new, BN_CTX_secure_new_ex, BN_CTX_secure_new, BN_CTX_free β€” allocate and free BN_CTX structures. that's the whole job. iconic behaviour honestly πŸ‘‘

🌸 SYNOPSIS 🌸

#include <openssl/bn.h>

BN_CTX *BN_CTX_new_ex(OSSL_LIB_CTX *ctx);
BN_CTX *BN_CTX_new(void);

BN_CTX *BN_CTX_secure_new_ex(OSSL_LIB_CTX *ctx);
BN_CTX *BN_CTX_secure_new(void);

void BN_CTX_free(BN_CTX *c);

πŸ¦‹ DESCRIPTION πŸ¦‹

so a BN_CTX is literally just a structure that holds BIGNUM temporary variables that library functions use. and the reason it exists? because dynamic memory allocation to create BIGNUMs is rather expensive when you're doing it alongside repeated subroutine calls. so instead of constantly malloc-ing like a chaos goblin, you keep a BN_CTX around. slay πŸ’…

  • BN_CTX_new_ex() β€” allocates and initializes a BN_CTX structure for the given library context ctx. the ctx value may be NULL, in which case the default library context gets used. BN_CTX_new() is the same as BN_CTX_new_ex() except that the default library context is always used.
  • BN_CTX_secure_new_ex() β€” allocates and initializes a BN_CTX structure but uses the secure heap (see CRYPTO_secure_malloc(3)) to hold the BIGNUMs, for the given library context ctx. the ctx value may be NULL, in which case the default library context gets used. BN_CTX_secure_new() is the same as BN_CTX_secure_new_ex() except that the default library context is always used. secure heap = main character energy πŸ”₯
  • BN_CTX_free() β€” frees the components of the BN_CTX and the structure itself. and here's the catch: since BN_CTX_start() is required in order to obtain BIGNUMs from the BN_CTX, in most cases BN_CTX_end() must be called before the BN_CTX may be freed by BN_CTX_free(). if c is NULL, nothing is done. no drama, no crash πŸŽ€
⚠️ bestie listen to me: a given BN_CTX must only be used by a single thread of execution. no locking is performed, and the internal pool allocator will not properly handle multiple threads of execution. like. do not share it. it's not built for that and it will not be okay 😭

⭐ RETURN VALUES ⭐

  • BN_CTX_new() and BN_CTX_secure_new() return a pointer to the BN_CTX. if the allocation fails, they return NULL and set an error code that you can grab with ERR_get_error(3). sad but documented πŸ™ˆ
  • BN_CTX_free() has no return values. it just vibes and leaves πŸ’€

πŸ’€ REMOVED FUNCTIONALITY πŸ’€

 void BN_CTX_init(BN_CTX *c);

BN_CTX_init() is no longer available as of OpenSSL 1.1.0. gone. retired. applications should replace use of BN_CTX_init with BN_CTX_new instead:

 BN_CTX *ctx;
 ctx = BN_CTX_new();
 if (!ctx)
     /* error */
 ...
 BN_CTX_free(ctx);

like a glow-up but for your code 🌈

🧚 SEE ALSO 🧚

ERR_get_error(3), BN_add(3), BN_CTX_start(3)

🌸 HISTORY 🌸

BN_CTX_init() was removed in OpenSSL 1.1.0. and that's the whole history lesson, you're welcome πŸŽ“

Copyright 2000-2020 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>.


okay that's the page!! πŸ’– this whole thing is from OpenSSL 3.5.6, dated 2026-04-07, and it's the BN_CTX_NEW(3ossl) page. go forth and allocate responsibly, bestie. mwah 🫢✨

πŸ“¦ where this page comes from