summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authortb <>2023-07-02 12:25:33 +0000
committertb <>2023-07-02 12:25:33 +0000
commit2b424eba396de87f81f5320c70429232c5b44690 (patch)
tree694014b0e0f28fdda2bc25a055b01588fdffe1af /src/lib
parentab0c921abc95eecbe93ec6d3ae73409cbecb4f0d (diff)
downloadopenbsd-2b424eba396de87f81f5320c70429232c5b44690.tar.gz
openbsd-2b424eba396de87f81f5320c70429232c5b44690.tar.bz2
openbsd-2b424eba396de87f81f5320c70429232c5b44690.zip
Simplify things by switching to bn_rand_interval()
This avoids some silly dances in ECDSA signature generation by replacing them with a single API call. Also garbage collect the now unnecessary range. ok beck jsing
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_ossl.c41
1 files changed, 19 insertions, 22 deletions
diff --git a/src/lib/libcrypto/ecdsa/ecs_ossl.c b/src/lib/libcrypto/ecdsa/ecs_ossl.c
index 7c65fa79eb..3fd15f5f62 100644
--- a/src/lib/libcrypto/ecdsa/ecs_ossl.c
+++ b/src/lib/libcrypto/ecdsa/ecs_ossl.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecs_ossl.c,v 1.39 2023/07/02 04:17:00 tb Exp $ */ 1/* $OpenBSD: ecs_ossl.c,v 1.40 2023/07/02 12:25:33 tb Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project 3 * Written by Nils Larsch for the OpenSSL project
4 */ 4 */
@@ -173,13 +173,10 @@ ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp
173 goto err; 173 goto err;
174 174
175 do { 175 do {
176 do { 176 if (!bn_rand_interval(k, BN_value_one(), order)) {
177 if (!BN_rand_range(k, order)) { 177 ECDSAerror(ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
178 ECDSAerror( 178 goto err;
179 ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED); 179 }
180 goto err;
181 }
182 } while (BN_is_zero(k));
183 180
184 /* 181 /*
185 * We do not want timing information to leak the length of k, 182 * We do not want timing information to leak the length of k,
@@ -253,7 +250,7 @@ ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
253 const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey) 250 const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey)
254{ 251{
255 BIGNUM *b = NULL, *binv = NULL, *bm = NULL, *bxr = NULL; 252 BIGNUM *b = NULL, *binv = NULL, *bm = NULL, *bxr = NULL;
256 BIGNUM *kinv = NULL, *m = NULL, *order = NULL, *range = NULL, *s; 253 BIGNUM *kinv = NULL, *m = NULL, *order = NULL, *s;
257 const BIGNUM *ckinv, *priv_key; 254 const BIGNUM *ckinv, *priv_key;
258 BN_CTX *ctx = NULL; 255 BN_CTX *ctx = NULL;
259 const EC_GROUP *group; 256 const EC_GROUP *group;
@@ -276,7 +273,7 @@ ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
276 s = ret->s; 273 s = ret->s;
277 274
278 if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL || 275 if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
279 (range = BN_new()) == NULL || (b = BN_new()) == NULL || 276 (b = BN_new()) == NULL ||
280 (binv = BN_new()) == NULL || (bm = BN_new()) == NULL || 277 (binv = BN_new()) == NULL || (bm = BN_new()) == NULL ||
281 (bxr = BN_new()) == NULL || (m = BN_new()) == NULL) { 278 (bxr = BN_new()) == NULL || (m = BN_new()) == NULL) {
282 ECDSAerror(ERR_R_MALLOC_FAILURE); 279 ECDSAerror(ERR_R_MALLOC_FAILURE);
@@ -316,19 +313,10 @@ ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
316 * 313 *
317 * s = inv(b)(bm + bxr)inv(k) mod order 314 * s = inv(b)(bm + bxr)inv(k) mod order
318 * 315 *
319 * where b is a random value in the range [1, order-1]. 316 * where b is a random value in the range [1, order).
320 */ 317 */
321 318
322 /* Generate b in range [1, order-1]. */ 319 if (!bn_rand_interval(b, BN_value_one(), order)) {
323 if (!BN_sub(range, order, BN_value_one())) {
324 ECDSAerror(ERR_R_BN_LIB);
325 goto err;
326 }
327 if (!BN_rand_range(b, range)) {
328 ECDSAerror(ERR_R_BN_LIB);
329 goto err;
330 }
331 if (!BN_add(b, b, BN_value_one())) {
332 ECDSAerror(ERR_R_BN_LIB); 320 ECDSAerror(ERR_R_BN_LIB);
333 goto err; 321 goto err;
334 } 322 }
@@ -382,6 +370,16 @@ ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
382 break; 370 break;
383 } while (1); 371 } while (1);
384 372
373 /*
374 * Ensure that the signature generated can be verified. This ensures
375 * that our implementation is correct, while also potentially detecting
376 * some forms of side-channel attacks.
377 */
378 if (ECDSA_do_verify(dgst, dgst_len, ret, eckey) <= 0) {
379 ECDSAerror(ERR_R_EC_LIB);
380 goto err;
381 }
382
385 ok = 1; 383 ok = 1;
386 384
387 err: 385 err:
@@ -397,7 +395,6 @@ ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
397 BN_free(kinv); 395 BN_free(kinv);
398 BN_free(m); 396 BN_free(m);
399 BN_free(order); 397 BN_free(order);
400 BN_free(range);
401 return ret; 398 return ret;
402} 399}
403 400