f-2708: Add test cases for KeyUsage#380
Conversation
There was a problem hiding this comment.
Pull request overview
This PR expands whTest_CryptoKeyUsagePolicies to exercise server-side key usage enforcement (wh_Server_KeystoreEnforceKeyUsage) across AES CTR/ECB/GCM as well as the DMA cipher handlers, ensuring that keys cached without the appropriate usage flags are rejected with WH_ERROR_USAGE.
Changes:
- Add negative (rejection) tests for AES-CTR, AES-ECB, and AES-GCM non-DMA handlers when ENCRYPT/DECRYPT usage flags are missing.
- Add negative (rejection) tests for AES CTR/ECB/CBC/GCM DMA handlers (when
WOLFHSM_CFG_DMAis enabled). - Use
wh_Client_AesCtr{Dma}directly for CTR direction testing to ensureencis exercised as intended.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #380
No scan targets match the changed files in this PR. Review skipped.
| if (ret != 0) | ||
| return ret; |
There was a problem hiding this comment.
no braceless if statements please. There are lots in this PR
|
@miyazakh could you also please add the same test to the new refactored test framework in the |
Test: Add key usage policy enforcement tests for AES CTR/ECB/GCM and all DMA handlers
Background
wh_Server_KeystoreEnforceKeyUsageis called in every AES cipher handler on the serverside to prevent a cached key from being used in an operation its policy does not permit
(e.g., an ENCRYPT-only key being used to decrypt). However, the existing positive tests
in
whTestCrypto_Aesalways cache keys with bothWH_NVM_FLAGS_USAGE_ENCRYPTandWH_NVM_FLAGS_USAGE_DECRYPTset, so the enforcement logic is never actually exercised.Problem
Three gaps were identified:
No negative tests for CTR, ECB, or GCM — only
_HandleAesCbchad rejection tests(lines 5101–5214 of
wh_test_crypto.c). Deleting the enforcement call from any ofthe other three handlers caused zero test failures.
No tests at all for the DMA handlers —
_HandleAesCtrDma,_HandleAesEcbDma,_HandleAesCbcDma, and_HandleAesGcmDmaeach contain enforcement code but had notest coverage of any kind.
Fix
Added negative tests to
whTest_CryptoKeyUsagePoliciesintest/wh_test_crypto.c.Each test caches a key with restricted flags, then attempts the forbidden operation and
asserts that the server returns
WH_ERROR_USAGE.Non-DMA handlers
enc=1) without ENCRYPT flagWH_NVM_FLAGS_NONEenc=0) without DECRYPT flagWH_NVM_FLAGS_USAGE_ENCRYPTWH_NVM_FLAGS_NONEWH_NVM_FLAGS_USAGE_ENCRYPTWH_NVM_FLAGS_NONEWH_NVM_FLAGS_USAGE_ENCRYPTDMA handlers (active when built with
DMA=1)Notes
wh_Client_AesCtrdirectly (withenc=0/enc=1) becausewc_AesCtrEncryptis symmetric and always sendsenc=1through the wolfcryptcrypto callback.
wc_AesEcbEncrypt,wc_AesGcmDecrypt, etc.), which set the direction flag correctly through the callback.dec_tagis passed as a non-NULL zero buffer to satisfy theclient-side
BADARGSguard; the server rejects the request at the key usage checkbefore any cryptographic operation or DMA address processing occurs.
Files Changed
test/wh_test_crypto.cwhTest_CryptoKeyUsagePolicies