Skip to content

Commit 80ee2d5

Browse files
authored
Merge pull request #360 from padelsbach/test-fixes-ed25519
Fixes in ed25519 tests, add prints in timeout tests
2 parents 1c3f887 + 96f8b74 commit 80ee2d5

3 files changed

Lines changed: 49 additions & 17 deletions

File tree

test-refactor/client-server/wh_test_crypto.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,20 @@ int whTest_CryptoEcc256(whClientContext* ctx)
168168
int ret = 0;
169169
WC_RNG rng[1];
170170
ecc_key key[1];
171-
uint8_t hash[32] = {0};
171+
/* Non-zero digest: wolfCrypt rejects all-zero hashes with ECC_BAD_ARG_E
172+
* unless WC_ALLOW_ECC_ZERO_HASH is defined. */
173+
uint8_t hash[32];
172174
uint8_t sig[ECC_MAX_SIG_SIZE] = {0};
173175
word32 sigLen = sizeof(sig);
174176
int verify = 0;
177+
word32 i;
175178

176179
(void)ctx;
177180

181+
for (i = 0; i < sizeof(hash); i++) {
182+
hash[i] = (uint8_t)(i + 1);
183+
}
184+
178185
/* Minimal P-256 sign/verify round-trip routed through the server via
179186
* WH_DEV_ID. Key size 32 selects SECP256R1 as wolfCrypt's default. */
180187
ret = wc_InitRng_ex(rng, NULL, devId);

test/wh_test_crypto.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,14 +1159,21 @@ static int whTest_CryptoEccExportPublicDma(whClientContext* ctx, int devId,
11591159
int ret = 0;
11601160
whKeyId keyId = WH_KEYID_ERASED;
11611161
ecc_key pubKey[1] = {0};
1162-
uint8_t hash[TEST_ECC_KEYSIZE] = {0};
1162+
/* Non-zero digest: wolfCrypt rejects all-zero hashes with ECC_BAD_ARG_E
1163+
* unless WC_ALLOW_ECC_ZERO_HASH is defined. */
1164+
uint8_t hash[TEST_ECC_KEYSIZE];
11631165
uint8_t sig[ECC_MAX_SIG_SIZE] = {0};
11641166
word32 sigLen = sizeof(sig);
11651167
int verified = 0;
11661168
byte derBuf[ECC_BUFSIZE];
11671169
uint16_t derSz = sizeof(derBuf);
1170+
word32 i;
11681171
(void)devId;
11691172

1173+
for (i = 0; i < sizeof(hash); i++) {
1174+
hash[i] = (uint8_t)(i + 1);
1175+
}
1176+
11701177
ret = wh_Client_EccMakeCacheKey(
11711178
ctx, TEST_ECC_KEYSIZE, TEST_ECC_CURVE_ID, &keyId,
11721179
WH_NVM_FLAGS_USAGE_SIGN | WH_NVM_FLAGS_USAGE_VERIFY |
@@ -2461,13 +2468,18 @@ static int whTest_Ed25519ImportToServer(whClientContext* ctx, int devId,
24612468
}
24622469
}
24632470

2471+
/* Write each out-keyId immediately after its import succeeds so the
2472+
* caller can evict it if a later step fails. */
24642473
if (ret == 0) {
24652474
ret = wh_Client_Ed25519ImportKey(
24662475
ctx, key, &signKeyId, WH_NVM_FLAGS_USAGE_SIGN, labelLen, label);
24672476
if (ret != 0) {
24682477
WH_ERROR_PRINT("Failed to import Ed25519 key to server: %d\n", ret);
24692478
}
24702479
else {
2480+
if (outSignKeyId != NULL) {
2481+
*outSignKeyId = signKeyId;
2482+
}
24712483
/* remove key material from local key structure */
24722484
wc_ed25519_free(key);
24732485
ret = wc_ed25519_init_ex(key, NULL, devId);
@@ -2490,6 +2502,9 @@ static int whTest_Ed25519ImportToServer(whClientContext* ctx, int devId,
24902502
"Failed to import Ed25519 public key to server: %d\n", ret);
24912503
}
24922504
else {
2505+
if (outVerifyKeyId != NULL) {
2506+
*outVerifyKeyId = verifyKeyId;
2507+
}
24932508
/* remove key material from local key structure */
24942509
wc_ed25519_free(pubKey);
24952510
ret = wc_ed25519_init_ex(pubKey, NULL, devId);
@@ -2503,15 +2518,6 @@ static int whTest_Ed25519ImportToServer(whClientContext* ctx, int devId,
25032518
}
25042519
}
25052520

2506-
if (ret == 0) {
2507-
if (outSignKeyId != NULL) {
2508-
*outSignKeyId = signKeyId;
2509-
}
2510-
if (outVerifyKeyId != NULL) {
2511-
*outVerifyKeyId = verifyKeyId;
2512-
}
2513-
}
2514-
25152521
return ret;
25162522
}
25172523

@@ -2582,18 +2588,25 @@ static int whTest_CryptoEd25519Inline(whClientContext* ctx, int devId,
25822588
}
25832589

25842590
if (ret == 0) {
2585-
/* Corrupt signature to ensure verification fails */
2591+
/* Corrupt signature to ensure verification fails. wolfCrypt may
2592+
* signal rejection either as ret==0 with verified==0, or as
2593+
* ret==SIG_VERIFY_E (path-dependent inside wolfCrypt). Anything
2594+
* else is a real error. */
25862595
sig[0] ^= 0xFF;
25872596
verified = 0;
25882597
ret = wc_ed25519_verify_msg(sig, sigSz, msg, msgSz, &verified, pubKey);
2589-
if (ret == 0 && verified == 1) {
2598+
if (verified != 0) {
25902599
WH_ERROR_PRINT(
25912600
"Modified Ed25519 signature unexpectedly verified\n");
25922601
ret = -1;
25932602
}
2594-
else {
2603+
else if (ret == 0 || ret == SIG_VERIFY_E) {
25952604
ret = 0;
25962605
}
2606+
else {
2607+
WH_ERROR_PRINT(
2608+
"wc_ed25519_verify_msg of tampered sig errored: %d\n", ret);
2609+
}
25972610
}
25982611

25992612
if (ret == 0) {
@@ -2677,19 +2690,24 @@ static int whTest_CryptoEd25519ServerKey(whClientContext* ctx, int devId,
26772690
}
26782691

26792692
if (ret == 0) {
2693+
/* Same shape as the inline tampered-sig case above. */
26802694
sig[0] ^= 0xAA;
26812695
verified = 0;
26822696
ret = wh_Client_Ed25519Verify(ctx, pubKey, sig, sigSz, msg,
26832697
(uint32_t)sizeof(msg), (uint8_t)Ed25519,
26842698
NULL, 0, &verified);
2685-
if (ret == 0 && verified == 1) {
2699+
if (verified != 0) {
26862700
WH_ERROR_PRINT("Modified server Ed25519 signature unexpectedly "
26872701
"verified\n");
26882702
ret = -1;
26892703
}
2690-
else {
2704+
else if (ret == 0 || ret == SIG_VERIFY_E) {
26912705
ret = 0;
26922706
}
2707+
else {
2708+
WH_ERROR_PRINT(
2709+
"Server Ed25519 verify of tampered sig errored: %d\n", ret);
2710+
}
26932711
}
26942712

26952713
if (!WH_KEYID_ISERASED(signKeyId)) {

test/wh_test_timeout.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ static int whTest_TimeoutAesCbc(void)
208208
wh_Nvm_Cleanup(nvm);
209209
wolfCrypt_Cleanup();
210210

211+
WH_TEST_PRINT("Timeout AES CBC SUCCESS\n");
211212
return WH_ERROR_OK;
212213
}
213214

@@ -371,6 +372,7 @@ static int whTest_TimeoutAesCbcOverride(void)
371372
wh_Nvm_Cleanup(nvm);
372373
wolfCrypt_Cleanup();
373374

375+
WH_TEST_PRINT("Timeout AES CBC override SUCCESS\n");
374376
return WH_ERROR_OK;
375377
}
376378

@@ -402,6 +404,7 @@ static int whTest_TimeoutApi(void)
402404
WH_TEST_ASSERT_RETURN(wh_Timeout_Expired(0) == WH_ERROR_BADARGS);
403405
WH_TEST_ASSERT_RETURN(wh_Timeout_Cleanup(0) == WH_ERROR_BADARGS);
404406

407+
WH_TEST_PRINT("Timeout API SUCCESS\n");
405408
return WH_ERROR_OK;
406409
}
407410

@@ -423,6 +426,7 @@ static int whTest_TimeoutResponse(whClientContext* client)
423426
} while (rc == WH_ERROR_NOTREADY);
424427
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_TIMEOUT);
425428

429+
WH_TEST_PRINT("Timeout response SUCCESS\n");
426430
return WH_ERROR_OK;
427431
}
428432

@@ -446,6 +450,7 @@ int whTest_TimeoutClientConfig(whClientConfig* config)
446450
WH_TEST_RETURN_ON_FAIL(whTest_TimeoutAesCbcOverride());
447451
#endif
448452

453+
WH_TEST_PRINT("Timeout client config SUCCESS\n");
449454
return WH_ERROR_OK;
450455
}
451456

@@ -483,7 +488,9 @@ int whTest_TimeoutPosix(void)
483488
.comm = ccConf,
484489
}};
485490

486-
return whTest_TimeoutClientConfig(cConf);
491+
WH_TEST_RETURN_ON_FAIL(whTest_TimeoutClientConfig(cConf));
492+
WH_TEST_PRINT("Timeout (POSIX) SUCCESS\n");
493+
return WH_ERROR_OK;
487494
}
488495
#endif /* WOLFHSM_CFG_TEST_POSIX */
489496

0 commit comments

Comments
 (0)