@@ -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" );
0 commit comments