summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2023-07-03 09:35:26 +0000
committertb <>2023-07-03 09:35:26 +0000
commit9f08e6cd150eca26dd5b43911f79cc495fb6d0cd (patch)
treec52529db7e7592c679dd29e56d55766e71131e16
parentc7f387cc45a0e4fdf946ae2787d268e9e2756521 (diff)
downloadopenbsd-9f08e6cd150eca26dd5b43911f79cc495fb6d0cd.tar.gz
openbsd-9f08e6cd150eca26dd5b43911f79cc495fb6d0cd.tar.bz2
openbsd-9f08e6cd150eca26dd5b43911f79cc495fb6d0cd.zip
Convert ossl_ec_key_gen() and EC_KEY_check_key()
These also get the EC_GROUP_get0_order() treatment ok beck jsing
-rw-r--r--src/lib/libcrypto/ec/ec_key.c29
1 files changed, 6 insertions, 23 deletions
diff --git a/src/lib/libcrypto/ec/ec_key.c b/src/lib/libcrypto/ec/ec_key.c
index 4127352523..1006d2d89d 100644
--- a/src/lib/libcrypto/ec/ec_key.c
+++ b/src/lib/libcrypto/ec/ec_key.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_key.c,v 1.33 2023/06/25 18:52:27 tb Exp $ */ 1/* $OpenBSD: ec_key.c,v 1.34 2023/07/03 09:35:26 tb Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project. 3 * Written by Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -243,10 +243,9 @@ EC_KEY_generate_key(EC_KEY *eckey)
243int 243int
244ossl_ec_key_gen(EC_KEY *eckey) 244ossl_ec_key_gen(EC_KEY *eckey)
245{ 245{
246 BN_CTX *ctx = NULL;
247 BIGNUM *priv_key = NULL; 246 BIGNUM *priv_key = NULL;
248 EC_POINT *pub_key = NULL; 247 EC_POINT *pub_key = NULL;
249 BIGNUM *order; 248 const BIGNUM *order;
250 int ret = 0; 249 int ret = 0;
251 250
252 if (eckey == NULL || eckey->group == NULL) { 251 if (eckey == NULL || eckey->group == NULL) {
@@ -259,19 +258,11 @@ ossl_ec_key_gen(EC_KEY *eckey)
259 if ((pub_key = EC_POINT_new(eckey->group)) == NULL) 258 if ((pub_key = EC_POINT_new(eckey->group)) == NULL)
260 goto err; 259 goto err;
261 260
262 if ((ctx = BN_CTX_new()) == NULL) 261 if ((order = EC_GROUP_get0_order(eckey->group)) == NULL)
263 goto err;
264
265 BN_CTX_start(ctx);
266
267 if ((order = BN_CTX_get(ctx)) == NULL)
268 goto err;
269
270 if (!EC_GROUP_get_order(eckey->group, order, ctx))
271 goto err; 262 goto err;
272 if (!bn_rand_interval(priv_key, BN_value_one(), order)) 263 if (!bn_rand_interval(priv_key, BN_value_one(), order))
273 goto err; 264 goto err;
274 if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx)) 265 if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, NULL))
275 goto err; 266 goto err;
276 267
277 BN_free(eckey->priv_key); 268 BN_free(eckey->priv_key);
@@ -287,8 +278,6 @@ ossl_ec_key_gen(EC_KEY *eckey)
287 err: 278 err:
288 EC_POINT_free(pub_key); 279 EC_POINT_free(pub_key);
289 BN_free(priv_key); 280 BN_free(priv_key);
290 BN_CTX_end(ctx);
291 BN_CTX_free(ctx);
292 281
293 return ret; 282 return ret;
294} 283}
@@ -298,7 +287,7 @@ EC_KEY_check_key(const EC_KEY *eckey)
298{ 287{
299 BN_CTX *ctx = NULL; 288 BN_CTX *ctx = NULL;
300 EC_POINT *point = NULL; 289 EC_POINT *point = NULL;
301 BIGNUM *order; 290 const BIGNUM *order;
302 int ret = 0; 291 int ret = 0;
303 292
304 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) { 293 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
@@ -314,11 +303,6 @@ EC_KEY_check_key(const EC_KEY *eckey)
314 if ((ctx = BN_CTX_new()) == NULL) 303 if ((ctx = BN_CTX_new()) == NULL)
315 goto err; 304 goto err;
316 305
317 BN_CTX_start(ctx);
318
319 if ((order = BN_CTX_get(ctx)) == NULL)
320 goto err;
321
322 if ((point = EC_POINT_new(eckey->group)) == NULL) 306 if ((point = EC_POINT_new(eckey->group)) == NULL)
323 goto err; 307 goto err;
324 308
@@ -329,7 +313,7 @@ EC_KEY_check_key(const EC_KEY *eckey)
329 } 313 }
330 314
331 /* Ensure public key multiplied by the order is the point at infinity. */ 315 /* Ensure public key multiplied by the order is the point at infinity. */
332 if (!EC_GROUP_get_order(eckey->group, order, ctx)) { 316 if ((order = EC_GROUP_get0_order(eckey->group)) == NULL) {
333 ECerror(EC_R_INVALID_GROUP_ORDER); 317 ECerror(EC_R_INVALID_GROUP_ORDER);
334 goto err; 318 goto err;
335 } 319 }
@@ -366,7 +350,6 @@ EC_KEY_check_key(const EC_KEY *eckey)
366 ret = 1; 350 ret = 1;
367 351
368 err: 352 err:
369 BN_CTX_end(ctx);
370 BN_CTX_free(ctx); 353 BN_CTX_free(ctx);
371 EC_POINT_free(point); 354 EC_POINT_free(point);
372 355