Use BN_CTX cached OSSL_FN_CTX in BN_mul() and BN_sqr()

The previous commit added bn_ctx_acquire_ossl_fn_ctx() and
bn_ctx_release_ossl_fn_ctx() to cache an OSSL_FN_CTX inside a BN_CTX.
However, BN_mul() and BN_sqr() were still creating and freeing their own
temporary OSSL_FN_CTX on every call, ignoring the passed-in BN_CTX entirely.

Update both functions to acquire the OSSL_FN_CTX from the BN_CTX and release
it afterwards, allowing the cached context to be reused across calls.

Assisted-by: Pi:moonshotai/kimi-k2.6
Signed-off-by: Richard Levitte <levitte@openssl.foundation>

Reviewed-by: Igor Ustinov <igus@openssl.foundation>
Reviewed-by: Tomas Mraz <tomas@openssl.foundation>
MergeDate: Sun Jun 14 08:40:47 2026
(Merged from https://github.com/openssl/openssl/pull/31431)
This commit is contained in:
Richard Levitte 2026-06-09 17:41:51 +02:00 committed by Tomas Mraz
parent e3213b0c03
commit a7b662c282
2 changed files with 4 additions and 14 deletions

View file

@ -37,19 +37,14 @@ int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
size_t max = a->dmax + b->dmax;
/*
* Unfortunately, OSSL_FN_CTX and BN_CTX are too wildly different to
* be interchangeable. We must therefore create an OSSL_FN_CTX here.
* (OSSL_FN_CTX is only really useful within OSSL_FN functionality)
*/
OSSL_FN_CTX *fnctx = OSSL_FN_CTX_new(NULL, 1, 1, max);
OSSL_FN_CTX *fnctx = bn_ctx_acquire_ossl_fn_ctx(ctx, 1, 1, max);
int ret = OSSL_FN_mul(rf, a->data, b->data, fnctx);
bn_release(r, (int)top);
if (ret && !BN_is_zero(r))
r->neg = a->neg ^ b->neg;
OSSL_FN_CTX_free(fnctx);
bn_ctx_release_ossl_fn_ctx(ctx);
return ret;
}

View file

@ -39,17 +39,12 @@ int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
size_t max = a->dmax * 2;
/*
* Unfortunately, OSSL_FN_CTX and BN_CTX are too wildly different to
* be interchangeable. We must therefore create an OSSL_FN_CTX here.
* (OSSL_FN_CTX is only really useful within OSSL_FN functionality)
*/
OSSL_FN_CTX *fnctx = OSSL_FN_CTX_new(NULL, 1, 2, max * 4);
OSSL_FN_CTX *fnctx = bn_ctx_acquire_ossl_fn_ctx(ctx, 1, 2, max * 4);
int ret = OSSL_FN_sqr(rf, a->data, fnctx);
bn_release(r, (int)top);
r->neg = 0;
OSSL_FN_CTX_free(fnctx);
bn_ctx_release_ossl_fn_ctx(ctx);
return ret;
}