Skip to content

Commit 9a56ded

Browse files
committed
Use actual sha256 digest of message in ECC demos
1 parent f6530db commit 9a56ded

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

examples/demo/client/wh_demo_client_crypto.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,17 @@ int wh_DemoClient_CryptoEcc(whClientContext* clientContext)
492492
WC_RNG rng[1];
493493
byte sharedOne[32];
494494
byte sharedTwo[32];
495-
const char plainMessage[WC_MAX_DIGEST_SIZE] = "message example";
496-
byte message[sizeof(plainMessage)];
495+
const byte plainMessage[] = "The quick brown fox jumps over the lazy dog.";
496+
byte hash[WC_MAX_DIGEST_SIZE];
497497
byte signature[128];
498498

499-
/* Set the message to the test string */
500-
strcpy((char*)message, plainMessage);
499+
/* Hash the plainMessage using SHA256 for signing and verification */
500+
ret = wc_Hash(WC_HASH_TYPE_SHA256, plainMessage, sizeof(plainMessage),
501+
hash, sizeof(hash));
502+
if (ret != 0) {
503+
WOLFHSM_CFG_PRINTF("Failed to wc_Hash %d\n", ret);
504+
goto exit;
505+
}
501506

502507
/* Initialize the rng to make the ecc keys */
503508
ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID);
@@ -557,9 +562,9 @@ int wh_DemoClient_CryptoEcc(whClientContext* clientContext)
557562
WOLFHSM_CFG_PRINTF("ECC shared secrets match\n");
558563
}
559564

560-
/* Sign the plaintext using the private component of Alice's key */
565+
/* Sign the hash of the plaintext using the private component of Alice's key */
561566
outLen = sizeof(signature);
562-
ret = wc_ecc_sign_hash(message, sizeof(message), (void*)signature,
567+
ret = wc_ecc_sign_hash(hash, sizeof(hash), (void*)signature,
563568
(word32*)&outLen, rng, aliceKey);
564569
if (ret != 0) {
565570
WOLFHSM_CFG_PRINTF("Failed to wc_ecc_sign_hash %d\n", ret);
@@ -570,8 +575,8 @@ int wh_DemoClient_CryptoEcc(whClientContext* clientContext)
570575
* the keys generated for Alice and Bob contain both public and private
571576
* parts. In a real scenario, the signing and verifying would occur at
572577
* separate times, and only the public key would be distributed */
573-
ret = wc_ecc_verify_hash((void*)signature, outLen, (void*)message,
574-
sizeof(message), &res, aliceKey);
578+
ret = wc_ecc_verify_hash((void*)signature, outLen, (void*)hash,
579+
sizeof(hash), &res, aliceKey);
575580
if (ret != 0) {
576581
WOLFHSM_CFG_PRINTF("Failed to wc_ecc_verify_hash %d\n", ret);
577582
goto exit;
@@ -621,13 +626,18 @@ int wh_DemoClient_CryptoEccImport(whClientContext* clientContext)
621626
WC_RNG rng[1];
622627
byte sharedOne[32];
623628
byte sharedTwo[32];
624-
const char plainMessage[WC_MAX_DIGEST_SIZE] = "message example";
625-
byte message[sizeof(plainMessage)];
629+
const byte plainMessage[] = "The quick brown fox jumps over the lazy dog.";
630+
byte hash[WC_MAX_DIGEST_SIZE];
626631
byte signature[128];
627632
uint8_t keyBuf[256];
628633

629-
/* Set the message to the test string */
630-
strcpy((char*)message, plainMessage);
634+
/* Hash the plainMessage using SHA256 for signing and verification */
635+
ret = wc_Hash(WC_HASH_TYPE_SHA256, plainMessage, sizeof(plainMessage),
636+
hash, sizeof(hash));
637+
if (ret != 0) {
638+
WOLFHSM_CFG_PRINTF("Failed to wc_Hash %d\n", ret);
639+
goto exit;
640+
}
631641

632642
/* Initialize the rng for signature signing */
633643
ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID);
@@ -757,9 +767,9 @@ int wh_DemoClient_CryptoEccImport(whClientContext* clientContext)
757767
WOLFHSM_CFG_PRINTF("ECC shared secrets match with imported keys\n");
758768
}
759769

760-
/* Sign the plaintext with Alice's private key */
770+
/* Sign the hash of the plaintext with Alice's private key */
761771
sigLen = sizeof(signature);
762-
ret = wc_ecc_sign_hash(message, sizeof(message), (void*)signature,
772+
ret = wc_ecc_sign_hash(hash, sizeof(hash), (void*)signature,
763773
(word32*)&sigLen, rng, aliceKey);
764774
if (ret != 0) {
765775
WOLFHSM_CFG_PRINTF("Failed to wc_ecc_sign_hash %d\n", ret);
@@ -770,8 +780,8 @@ int wh_DemoClient_CryptoEccImport(whClientContext* clientContext)
770780
* HSM contains both public and private parts. In a real scenario, the
771781
* signing and verifying would occur at separate times, and only the public
772782
* key would be distributed */
773-
ret = wc_ecc_verify_hash((void*)signature, sigLen, (void*)message,
774-
sizeof(message), &res, aliceKey);
783+
ret = wc_ecc_verify_hash((void*)signature, sigLen, (void*)hash,
784+
sizeof(hash), &res, aliceKey);
775785
if (ret != 0) {
776786
WOLFHSM_CFG_PRINTF("Failed to wc_ecc_verify_hash %d\n", ret);
777787
goto exit;

0 commit comments

Comments
 (0)