aboutsummaryrefslogtreecommitdiff
path: root/crypto/dsa/dsa_ossl.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/dsa/dsa_ossl.c')
-rw-r--r--crypto/dsa/dsa_ossl.c35
1 files changed, 25 insertions, 10 deletions
diff --git a/crypto/dsa/dsa_ossl.c b/crypto/dsa/dsa_ossl.c
index 86d89f4c724f..62f7c70149f4 100644
--- a/crypto/dsa/dsa_ossl.c
+++ b/crypto/dsa/dsa_ossl.c
@@ -1,5 +1,5 @@
/*
- * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
@@ -21,6 +21,9 @@
#include "dsa_local.h"
#include <openssl/asn1.h>
+#define MIN_DSA_SIGN_QBITS 128
+#define MAX_DSA_SIGN_RETRIES 8
+
static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
BIGNUM **rp);
@@ -75,6 +78,7 @@ DSA_SIG *ossl_dsa_do_sign_int(const unsigned char *dgst, int dlen, DSA *dsa)
int reason = ERR_R_BN_LIB;
DSA_SIG *ret = NULL;
int rv = 0;
+ int retries = 0;
if (dsa->params.p == NULL
|| dsa->params.q == NULL
@@ -129,7 +133,10 @@ DSA_SIG *ossl_dsa_do_sign_int(const unsigned char *dgst, int dlen, DSA *dsa)
* s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod q
*/
- /* Generate a blinding value */
+ /*
+ * Generate a blinding value
+ * The size of q is tested in dsa_sign_setup() so there should not be an infinite loop here.
+ */
do {
if (!BN_priv_rand_ex(blind, BN_num_bits(dsa->params.q) - 1,
BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx))
@@ -164,14 +171,19 @@ DSA_SIG *ossl_dsa_do_sign_int(const unsigned char *dgst, int dlen, DSA *dsa)
goto err;
/*
- * Redo if r or s is zero as required by FIPS 186-3: this is very
- * unlikely.
+ * Redo if r or s is zero as required by FIPS 186-4: Section 4.6
+ * This is very unlikely.
+ * Limit the retries so there is no possibility of an infinite
+ * loop for bad domain parameter values.
*/
- if (BN_is_zero(ret->r) || BN_is_zero(ret->s))
+ if (BN_is_zero(ret->r) || BN_is_zero(ret->s)) {
+ if (retries++ > MAX_DSA_SIGN_RETRIES) {
+ reason = DSA_R_TOO_MANY_RETRIES;
+ goto err;
+ }
goto redo;
-
+ }
rv = 1;
-
err:
if (rv == 0) {
ERR_raise(ERR_LIB_DSA, reason);
@@ -212,7 +224,10 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
/* Reject obviously invalid parameters */
if (BN_is_zero(dsa->params.p)
|| BN_is_zero(dsa->params.q)
- || BN_is_zero(dsa->params.g)) {
+ || BN_is_zero(dsa->params.g)
+ || BN_is_negative(dsa->params.p)
+ || BN_is_negative(dsa->params.q)
+ || BN_is_negative(dsa->params.g)) {
ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_PARAMETERS);
return 0;
}
@@ -220,7 +235,6 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PRIVATE_KEY);
return 0;
}
-
k = BN_new();
l = BN_new();
if (k == NULL || l == NULL)
@@ -236,7 +250,8 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
/* Preallocate space */
q_bits = BN_num_bits(dsa->params.q);
q_words = bn_get_top(dsa->params.q);
- if (!bn_wexpand(k, q_words + 2)
+ if (q_bits < MIN_DSA_SIGN_QBITS
+ || !bn_wexpand(k, q_words + 2)
|| !bn_wexpand(l, q_words + 2))
goto err;