Skip to content

Commit 6515271

Browse files
committed
Fix from review
1 parent 04122c8 commit 6515271

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/sign-verify/clu_sign.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ int wolfCLU_sign_data_ecc(byte* data, char* out, word32 fSz, char* privKey,
396396
XMEMSET(outBuf, 0, outBufSz);
397397

398398
/* hash the input data before signing -- ECDSA signs a digest, not raw
399-
* data. Select a hash whose digest size matches the curve. */
399+
* data. Select a curve-appropriate hash paired with the curve
400+
* strength; ECDSA will truncate the digest as needed. */
400401
keySz = wc_ecc_size(&key);
401402
if (keySz <= 32) {
402403
hashType = WC_HASH_TYPE_SHA256;
@@ -408,7 +409,13 @@ int wolfCLU_sign_data_ecc(byte* data, char* out, word32 fSz, char* privKey,
408409
hashType = WC_HASH_TYPE_SHA512;
409410
}
410411
digestSz = wc_HashGetDigestSize(hashType);
411-
ret = wc_Hash(hashType, data, fSz, hashBuf, digestSz);
412+
if (digestSz <= 0 || digestSz > WC_MAX_DIGEST_SIZE) {
413+
wolfCLU_LogError("Invalid hash digest size: %d", digestSz);
414+
ret = BAD_FUNC_ARG;
415+
}
416+
else {
417+
ret = wc_Hash(hashType, data, fSz, hashBuf, digestSz);
418+
}
412419

413420
/* signing the hash with ecc priv key to produce signature */
414421
if (ret == 0) {

src/sign-verify/clu_verify.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,8 @@ int wolfCLU_verify_signature_ecc(byte* sig, int sigSz, byte* hash, int hashSz,
574574
XMEMSET(outBuf, 0, outBufSz);
575575

576576
/* hash the input data before verifying -- ECDSA operates on a digest,
577-
* not raw data. Select a hash whose digest size matches the curve. */
577+
* not raw data. Select a curve-appropriate hash paired with the curve
578+
* strength; ECDSA will truncate the digest as needed. */
578579
keySz = wc_ecc_size(&key);
579580
if (keySz <= 32) {
580581
hashType = WC_HASH_TYPE_SHA256;
@@ -586,7 +587,12 @@ int wolfCLU_verify_signature_ecc(byte* sig, int sigSz, byte* hash, int hashSz,
586587
hashType = WC_HASH_TYPE_SHA512;
587588
}
588589
digestSz = wc_HashGetDigestSize(hashType);
589-
ret = wc_Hash(hashType, hash, hashSz, hashBuf, digestSz);
590+
if (digestSz > 0 && digestSz <= WC_MAX_DIGEST_SIZE) {
591+
ret = wc_Hash(hashType, hash, hashSz, hashBuf, digestSz);
592+
}
593+
else {
594+
ret = BAD_FUNC_ARG;
595+
}
590596

591597
/* verify the hash with Ecc public key */
592598
if (ret == 0) {

0 commit comments

Comments
 (0)