Skip to content

Commit 04122c8

Browse files
committed
Fix wolfCLU_sign_data_ecc and wolfCLU_verify_signature_ecc
1 parent c2ecece commit 04122c8

2 files changed

Lines changed: 51 additions & 5 deletions

File tree

src/sign-verify/clu_sign.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,34 @@ int wolfCLU_sign_data_ecc(byte* data, char* out, word32 fSz, char* privKey,
388388
}
389389
}
390390
if (ret == 0) {
391+
int keySz;
392+
enum wc_HashType hashType;
393+
int digestSz;
394+
byte hashBuf[WC_MAX_DIGEST_SIZE];
395+
391396
XMEMSET(outBuf, 0, outBufSz);
392397

393-
/* signing input with ecc priv key to produce signature */
394-
outLen = (word32)outBufSz;
395-
ret = wc_ecc_sign_hash(data, fSz, outBuf, &outLen, &rng, &key);
398+
/* hash the input data before signing -- ECDSA signs a digest, not raw
399+
* data. Select a hash whose digest size matches the curve. */
400+
keySz = wc_ecc_size(&key);
401+
if (keySz <= 32) {
402+
hashType = WC_HASH_TYPE_SHA256;
403+
}
404+
else if (keySz <= 48) {
405+
hashType = WC_HASH_TYPE_SHA384;
406+
}
407+
else {
408+
hashType = WC_HASH_TYPE_SHA512;
409+
}
410+
digestSz = wc_HashGetDigestSize(hashType);
411+
ret = wc_Hash(hashType, data, fSz, hashBuf, digestSz);
412+
413+
/* signing the hash with ecc priv key to produce signature */
414+
if (ret == 0) {
415+
outLen = (word32)outBufSz;
416+
ret = wc_ecc_sign_hash(hashBuf, digestSz, outBuf, &outLen,
417+
&rng, &key);
418+
}
396419
if (ret >= 0) {
397420
XFILE s;
398421
s = XFOPEN(out, "wb");

src/sign-verify/clu_verify.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,10 +566,33 @@ int wolfCLU_verify_signature_ecc(byte* sig, int sigSz, byte* hash, int hashSz,
566566
}
567567
}
568568
if (ret == 0) {
569+
int keySz;
570+
enum wc_HashType hashType;
571+
int digestSz;
572+
byte hashBuf[WC_MAX_DIGEST_SIZE];
573+
569574
XMEMSET(outBuf, 0, outBufSz);
570575

571-
/* verify data with Ecc public key */
572-
ret = wc_ecc_verify_hash(sig, sigSz, hash, hashSz, &stat, &key);
576+
/* hash the input data before verifying -- ECDSA operates on a digest,
577+
* not raw data. Select a hash whose digest size matches the curve. */
578+
keySz = wc_ecc_size(&key);
579+
if (keySz <= 32) {
580+
hashType = WC_HASH_TYPE_SHA256;
581+
}
582+
else if (keySz <= 48) {
583+
hashType = WC_HASH_TYPE_SHA384;
584+
}
585+
else {
586+
hashType = WC_HASH_TYPE_SHA512;
587+
}
588+
digestSz = wc_HashGetDigestSize(hashType);
589+
ret = wc_Hash(hashType, hash, hashSz, hashBuf, digestSz);
590+
591+
/* verify the hash with Ecc public key */
592+
if (ret == 0) {
593+
ret = wc_ecc_verify_hash(sig, sigSz, hashBuf, digestSz,
594+
&stat, &key);
595+
}
573596
if (ret < 0) {
574597
wolfCLU_LogError("Failed to verify data with pub key.\nRET: %d", ret);
575598
}

0 commit comments

Comments
 (0)