Skip to content

Commit 5e4c33f

Browse files
ColtonWilleypadelsbach
authored andcommitted
Use WC_MIN_DIGEST_SIZE for min digest len check on ECDSA
1 parent ebd520f commit 5e4c33f

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/wp_ecdsa_sig.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@
3232

3333
#ifdef WP_HAVE_ECDSA
3434

35-
/* SHA-1 digest size; literal because WC_SHA_DIGEST_SIZE is !NO_SHA-gated. */
36-
#define WP_ECDSA_MIN_HASH_LEN 20
35+
/* WC_MIN_DIGEST_SIZE was introduced in wolfSSL v5.9.1. Fall back to the
36+
* SHA-1 digest size on older releases so the raw-ECDSA minimum matches the
37+
* behavior FIPS 186-4 requires. */
38+
#ifndef WC_MIN_DIGEST_SIZE
39+
#define WC_MIN_DIGEST_SIZE 20
40+
#endif
3741

3842
/**
3943
* ECDSA signature context.
@@ -287,7 +291,7 @@ static int wp_ecdsa_sign(wp_EcdsaSigCtx *ctx, unsigned char *sig,
287291
ok = 0;
288292
}
289293
else if ((hashType == WC_HASH_TYPE_NONE) &&
290-
(tbsLen < WP_ECDSA_MIN_HASH_LEN)) {
294+
(tbsLen < WC_MIN_DIGEST_SIZE)) {
291295
ok = 0;
292296
}
293297
else if ((ok = wp_ecc_check_usage(ctx->ecc))) {
@@ -384,7 +388,7 @@ static int wp_ecdsa_verify(wp_EcdsaSigCtx *ctx, const unsigned char *sig,
384388
ok = 0;
385389
}
386390
else if ((hashType == WC_HASH_TYPE_NONE) &&
387-
(tbsLen < WP_ECDSA_MIN_HASH_LEN)) {
391+
(tbsLen < WC_MIN_DIGEST_SIZE)) {
388392
ok = 0;
389393
}
390394
else {

test/test_ecc.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
#include <openssl/core_names.h>
2525
#include <openssl/param_build.h>
2626

27+
#include <wolfssl/wolfcrypt/hash.h>
28+
29+
/* Mirror the fallback used in src/wp_ecdsa_sig.c for wolfSSL < v5.9.1. */
30+
#ifndef WC_MIN_DIGEST_SIZE
31+
#define WC_MIN_DIGEST_SIZE 20
32+
#endif
33+
2734
#ifdef WP_HAVE_ECC
2835

2936
#if defined(WP_HAVE_ECDSA) || defined(WP_HAVE_ECDH)
@@ -1196,17 +1203,23 @@ int test_ecdsa_p256_pkey(void *data)
11961203
return err;
11971204
}
11981205

1199-
/* Raw EVP_PKEY_verify must reject sub-SHA-1 inputs. */
1206+
/* Raw EVP_PKEY_verify must reject inputs below WC_MIN_DIGEST_SIZE. */
12001207
int test_ecdsa_verify_undersized_hash(void *data)
12011208
{
1202-
static const size_t sizes[] = { 0, 19, 20, 32 };
1203-
static const int expectFail[] = { 1, 1, 0, 0 };
1209+
/* Boundaries track WC_MIN_DIGEST_SIZE so the assertions stay valid for
1210+
* whatever hash set the wolfSSL build enabled (typically 20 for SHA-1,
1211+
* but 16 when MD5 is on or 28+ in FIPS 186-5 builds). */
1212+
static const size_t sizes[] = { 0,
1213+
WC_MIN_DIGEST_SIZE - 1,
1214+
WC_MIN_DIGEST_SIZE,
1215+
WC_MAX_DIGEST_SIZE };
1216+
static const int expectFail[] = { 1, 1, 0, 0 };
12041217
int err;
12051218
size_t i;
12061219
EVP_PKEY *pkey = NULL;
12071220
unsigned char ecdsaSig[80];
12081221
size_t ecdsaSigLen;
1209-
unsigned char buf[32];
1222+
unsigned char buf[WC_MAX_DIGEST_SIZE];
12101223
const unsigned char *p = ecc_key_der_256;
12111224

12121225
(void)data;

0 commit comments

Comments
 (0)