From 6d52d4fe853e27733eac9f76135f5e20b77e61a7 Mon Sep 17 00:00:00 2001 From: tb <> Date: Thu, 13 Feb 2025 11:04:20 +0000 Subject: bn: add internal BN_MONT_CTX_create() This does what the public BN_MONT_CTX_new() should have done in the first place rather than doing the toolkit thing of returning an invalid object that you need to figure out how to populate and with what because the docs are abysmal. It takes the required arguments and calls BN_MONT_CTX_set(), which all callers do immediately after _new() (except for DSA which managed to squeeze 170 lines of garbage between the two calls). ok jsing --- src/lib/libcrypto/bn/bn_local.h | 4 +++- src/lib/libcrypto/bn/bn_mont.c | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lib/libcrypto/bn/bn_local.h b/src/lib/libcrypto/bn/bn_local.h index 4b42ae95ee..067ffab3d9 100644 --- a/src/lib/libcrypto/bn/bn_local.h +++ b/src/lib/libcrypto/bn/bn_local.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_local.h,v 1.49 2025/02/12 21:21:34 tb Exp $ */ +/* $OpenBSD: bn_local.h,v 1.50 2025/02/13 11:04:20 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -271,6 +271,8 @@ int bn_rand_interval(BIGNUM *rnd, BN_ULONG lower_word, const BIGNUM *upper_exc); void BN_init(BIGNUM *); +BN_MONT_CTX *BN_MONT_CTX_create(const BIGNUM *bn, BN_CTX *ctx); + BN_RECP_CTX *BN_RECP_CTX_create(const BIGNUM *N); void BN_RECP_CTX_free(BN_RECP_CTX *recp); int BN_div_reciprocal(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, diff --git a/src/lib/libcrypto/bn/bn_mont.c b/src/lib/libcrypto/bn/bn_mont.c index c7e2eefb58..cfdc7e510f 100644 --- a/src/lib/libcrypto/bn/bn_mont.c +++ b/src/lib/libcrypto/bn/bn_mont.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_mont.c,v 1.63 2024/03/26 04:23:04 jsing Exp $ */ +/* $OpenBSD: bn_mont.c,v 1.64 2025/02/13 11:04:20 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -153,6 +153,24 @@ BN_MONT_CTX_free(BN_MONT_CTX *mctx) } LCRYPTO_ALIAS(BN_MONT_CTX_free); +BN_MONT_CTX * +BN_MONT_CTX_create(const BIGNUM *bn, BN_CTX *bn_ctx) +{ + BN_MONT_CTX *mctx; + + if ((mctx = BN_MONT_CTX_new()) == NULL) + goto err; + if (!BN_MONT_CTX_set(mctx, bn, bn_ctx)) + goto err; + + return mctx; + + err: + BN_MONT_CTX_free(mctx); + + return NULL; +} + BN_MONT_CTX * BN_MONT_CTX_copy(BN_MONT_CTX *dst, BN_MONT_CTX *src) { -- cgit v1.2.3-55-g6feb