@@ -15447,6 +15447,111 @@ static void test_cose_key_wrap_context_zeroize(void)
1544715447}
1544815448#endif /* ZEROIZE_HOOK && KEY_WRAP && AESGCM && ENCRYPT */
1544915449
15450+ #if defined(WOLFCOSE_TEST_ZEROIZE_HOOK) && defined(WOLFCOSE_HAVE_AESGCM) && \
15451+ defined(WOLFCOSE_HPKE_0_KE_ENCRYPT) && defined(WOLFCOSE_HPKE_0_KE_DECRYPT)
15452+ /* A recipient that cannot open its CEK still runs the content cipher. */
15453+ static void test_cose_hpke_ke_failed_open_uniform(void)
15454+ {
15455+ static const uint8_t payload[] = { 0x70u, 0x71u, 0x72u };
15456+ static const uint8_t iv[12] = { 0x02u };
15457+ static uint8_t scratch[1024];
15458+ static uint8_t out[1024];
15459+ uint8_t plaintext[64];
15460+ WOLFCOSE_KEY goodKey;
15461+ WOLFCOSE_KEY otherKey;
15462+ WOLFCOSE_RECIPIENT rcpt;
15463+ WOLFCOSE_HDR hdr;
15464+ ecc_key goodEcc;
15465+ ecc_key otherEcc;
15466+ WC_RNG rng;
15467+ size_t outLen = 0u;
15468+ size_t plaintextLen = 0u;
15469+ size_t i;
15470+ int nonZero = 0;
15471+ int rngInit = 0;
15472+ int goodInit = 0;
15473+ int otherInit = 0;
15474+ int keysInit = 0;
15475+ int ret;
15476+
15477+ TEST_LOG(" [HPKE-0-KE failed open runs the content cipher]\n");
15478+ ret = wc_InitRng(&rng);
15479+ if (ret == 0) {
15480+ rngInit = 1;
15481+ ret = wc_ecc_init(&goodEcc);
15482+ }
15483+ if (ret == 0) {
15484+ goodInit = 1;
15485+ ret = wc_ecc_init(&otherEcc);
15486+ }
15487+ if (ret == 0) {
15488+ otherInit = 1;
15489+ }
15490+ if (ret == 0) {
15491+ ret = wc_ecc_make_key(&rng, 32, &goodEcc);
15492+ }
15493+ if (ret == 0) {
15494+ ret = wc_ecc_make_key(&rng, 32, &otherEcc);
15495+ }
15496+ if (ret == 0) {
15497+ (void)wc_CoseKey_Init(&goodKey);
15498+ (void)wc_CoseKey_Init(&otherKey);
15499+ keysInit = 1;
15500+ ret = wc_CoseKey_SetEcc(&goodKey, WOLFCOSE_CRV_P256, &goodEcc);
15501+ }
15502+ if (ret == 0) {
15503+ ret = wc_CoseKey_SetEcc(&otherKey, WOLFCOSE_CRV_P256, &otherEcc);
15504+ }
15505+ TEST_ASSERT(ret == 0, "hpke ke uniform setup");
15506+ if (ret == 0) {
15507+ goodKey.alg = WOLFCOSE_ALG_HPKE_0_KE;
15508+ otherKey.alg = WOLFCOSE_ALG_HPKE_0_KE;
15509+ goodKey.hasPrivate = 0u;
15510+ rcpt.algId = WOLFCOSE_ALG_HPKE_0_KE;
15511+ rcpt.key = &goodKey;
15512+ rcpt.kid = NULL;
15513+ rcpt.kidLen = 0u;
15514+ ret = wc_CoseEncrypt_Encrypt(&rcpt, 1, WOLFCOSE_ALG_A128GCM, iv,
15515+ sizeof(iv), payload, sizeof(payload), NULL, 0u, NULL, 0u,
15516+ scratch, sizeof(scratch), out, sizeof(out), &outLen, &rng);
15517+ TEST_ASSERT(ret == 0, "hpke ke uniform encrypt");
15518+ }
15519+ if (ret == 0) {
15520+ otherKey.hasPrivate = 1u;
15521+ rcpt.key = &otherKey;
15522+ (void)XMEMSET(plaintext, 0xA5, sizeof(plaintext));
15523+ wolfCose_TestZeroizeReset();
15524+ ret = wc_CoseEncrypt_Decrypt(&rcpt, 0, out, outLen, NULL, 0u,
15525+ NULL, 0u, scratch, sizeof(scratch), &hdr, plaintext,
15526+ sizeof(plaintext), &plaintextLen);
15527+ TEST_ASSERT(ret == WOLFCOSE_E_COSE_DECRYPT_FAIL,
15528+ "hpke ke failed open reports decrypt failure");
15529+ TEST_ASSERT(wolfCose_TestZeroizeSawLen(sizeof(Aes)) == 1,
15530+ "hpke ke failed open still runs the content cipher");
15531+ for (i = 0u; i < sizeof(plaintext); i++) {
15532+ if (plaintext[i] != 0u) {
15533+ nonZero = 1;
15534+ }
15535+ }
15536+ TEST_ASSERT((nonZero == 0) && (plaintextLen == 0u),
15537+ "hpke ke failed open leaves no plaintext");
15538+ }
15539+ if (keysInit != 0) {
15540+ wc_CoseKey_Free(&otherKey);
15541+ wc_CoseKey_Free(&goodKey);
15542+ }
15543+ if (otherInit != 0) {
15544+ (void)wc_ecc_free(&otherEcc);
15545+ }
15546+ if (goodInit != 0) {
15547+ (void)wc_ecc_free(&goodEcc);
15548+ }
15549+ if (rngInit != 0) {
15550+ (void)wc_FreeRng(&rng);
15551+ }
15552+ }
15553+ #endif
15554+
1545015555#if defined(WOLFCOSE_TEST_ZEROIZE_HOOK) && defined(WOLFCOSE_HAVE_HMAC256) && \
1545115556 defined(WOLFCOSE_MAC0_CREATE) && defined(WOLFCOSE_MAC0_VERIFY) && \
1545215557 defined(WOLFCOSE_MAC_CREATE) && defined(WOLFCOSE_MAC_VERIFY)
@@ -32790,6 +32895,10 @@ int test_cose(void)
3279032895 defined(WOLFCOSE_ENCRYPT_DECRYPT)
3279132896 test_cose_key_wrap_context_zeroize();
3279232897#endif
32898+ #if defined(WOLFCOSE_TEST_ZEROIZE_HOOK) && defined(WOLFCOSE_HAVE_AESGCM) && \
32899+ defined(WOLFCOSE_HPKE_0_KE_ENCRYPT) && defined(WOLFCOSE_HPKE_0_KE_DECRYPT)
32900+ test_cose_hpke_ke_failed_open_uniform();
32901+ #endif
3279332902#if defined(WOLFCOSE_TEST_ZEROIZE_HOOK) && defined(WOLFCOSE_HAVE_HMAC256) && \
3279432903 defined(WOLFCOSE_MAC0_CREATE) && defined(WOLFCOSE_MAC0_VERIFY) && \
3279532904 defined(WOLFCOSE_MAC_CREATE) && defined(WOLFCOSE_MAC_VERIFY)
0 commit comments