summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ecp_mont.c
diff options
context:
space:
mode:
authorjsing <>2023-04-11 18:58:20 +0000
committerjsing <>2023-04-11 18:58:20 +0000
commit28c1968b342ae3ee2e863c4a47a59d90c3d7da9c (patch)
tree566c48c290ac86140f8df6c959b74661e1d596a7 /src/lib/libcrypto/ec/ecp_mont.c
parentd64e2f3567e88a542a225f4ab620c2851bd7f9e7 (diff)
downloadopenbsd-28c1968b342ae3ee2e863c4a47a59d90c3d7da9c.tar.gz
openbsd-28c1968b342ae3ee2e863c4a47a59d90c3d7da9c.tar.bz2
openbsd-28c1968b342ae3ee2e863c4a47a59d90c3d7da9c.zip
Handle BN_CTX at the EC API boundary.
The EC API allows callers to optionally pass in a BN_CTX, which means that any code needing a BN_CTX has to check if one was provided, allocate one if not, then free it again. Rather than doing this dance throughout the EC code, handle the BN_CTX existance at the EC API boundary. This means that lower level implementation code can simply assume that the BN_CTX is available. ok tb@
Diffstat (limited to 'src/lib/libcrypto/ec/ecp_mont.c')
-rw-r--r--src/lib/libcrypto/ec/ecp_mont.c12
1 files changed, 3 insertions, 9 deletions
diff --git a/src/lib/libcrypto/ec/ecp_mont.c b/src/lib/libcrypto/ec/ecp_mont.c
index 915cf15f72..b113855603 100644
--- a/src/lib/libcrypto/ec/ecp_mont.c
+++ b/src/lib/libcrypto/ec/ecp_mont.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecp_mont.c,v 1.28 2023/03/27 10:25:02 tb Exp $ */ 1/* $OpenBSD: ecp_mont.c,v 1.29 2023/04/11 18:58:20 jsing Exp $ */
2/* 2/*
3 * Originally written by Bodo Moeller for the OpenSSL project. 3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */ 4 */
@@ -127,18 +127,12 @@ static int
127ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, 127ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
128 const BIGNUM *b, BN_CTX *ctx) 128 const BIGNUM *b, BN_CTX *ctx)
129{ 129{
130 BN_CTX *new_ctx = NULL;
131 BN_MONT_CTX *mont = NULL; 130 BN_MONT_CTX *mont = NULL;
132 BIGNUM *one = NULL; 131 BIGNUM *one = NULL;
133 int ret = 0; 132 int ret = 0;
134 133
135 ec_GFp_mont_group_clear(group); 134 ec_GFp_mont_group_clear(group);
136 135
137 if (ctx == NULL) {
138 ctx = new_ctx = BN_CTX_new();
139 if (ctx == NULL)
140 return 0;
141 }
142 mont = BN_MONT_CTX_new(); 136 mont = BN_MONT_CTX_new();
143 if (mont == NULL) 137 if (mont == NULL)
144 goto err; 138 goto err;
@@ -158,14 +152,13 @@ ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
158 one = NULL; 152 one = NULL;
159 153
160 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx); 154 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
161
162 if (!ret) 155 if (!ret)
163 ec_GFp_mont_group_clear(group); 156 ec_GFp_mont_group_clear(group);
164 157
165 err: 158 err:
166 BN_CTX_free(new_ctx);
167 BN_MONT_CTX_free(mont); 159 BN_MONT_CTX_free(mont);
168 BN_free(one); 160 BN_free(one);
161
169 return ret; 162 return ret;
170} 163}
171 164
@@ -222,6 +215,7 @@ ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, BN_CTX *ctx)
222 } 215 }
223 if (!bn_copy(r, group->mont_one)) 216 if (!bn_copy(r, group->mont_one))
224 return 0; 217 return 0;
218
225 return 1; 219 return 1;
226} 220}
227 221