Skip to content

Commit a95f518

Browse files
committed
F-14996 - Run the content cipher after a failed HPKE-0-KE recipient open
1 parent 00c4e3a commit a95f518

3 files changed

Lines changed: 140 additions & 0 deletions

File tree

‎.github/workflows/cose-hpke.yml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ jobs:
7878
make test CFLAGS="$WOLFCOSE_CFLAGS" \
7979
LDFLAGS="-fsanitize=address -L$WOLFSSL_DIR/lib -lwolfssl"
8080
81+
- name: Run experimental HPKE zeroize-hook unit tests
82+
run: |
83+
export WOLFSSL_DIR="$HOME/wolfssl-install"
84+
export LD_LIBRARY_PATH="$WOLFSSL_DIR/lib"
85+
HPKE_FLAGS="-DWOLFCOSE_EXPERIMENTAL \
86+
-DWOLFCOSE_ENABLE_HPKE_0_ENCRYPT \
87+
-DWOLFCOSE_ENABLE_HPKE_0_DECRYPT \
88+
-DWOLFCOSE_ENABLE_HPKE_0_KE_ENCRYPT \
89+
-DWOLFCOSE_ENABLE_HPKE_0_KE_DECRYPT"
90+
WOLFCOSE_CFLAGS="-std=c99 -DHAVE_ANONYMOUS_INLINE_AGGREGATES=1 \
91+
-Og -g -fsanitize=address -fno-omit-frame-pointer \
92+
-Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wvla \
93+
-Werror=vla -DHAVE_ECC_KOBLITZ -I./include \
94+
-I$WOLFSSL_DIR/include $HPKE_FLAGS"
95+
make clean
96+
make zeroize-test CFLAGS="$WOLFCOSE_CFLAGS" \
97+
LDFLAGS="-fsanitize=address -L$WOLFSSL_DIR/lib -lwolfssl"
98+
8199
- name: Run experimental HPKE example
82100
run: |
83101
export WOLFSSL_DIR="$HOME/wolfssl-install"

‎src/wolfcose_encrypt.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ int wc_CoseEncrypt_Decrypt(const WOLFCOSE_RECIPIENT* recipient,
940940
#endif
941941
#if defined(WOLFCOSE_HPKE_0_KE_DECRYPT)
942942
uint8_t cekHpke[32];
943+
int hpkeOpenFailed = 0;
943944
const uint8_t* hpkeWrappedCekData = NULL;
944945
size_t hpkeWrappedCekLen = 0u;
945946
size_t hpkeRecipientInfoLen = 0u;
@@ -1542,6 +1543,13 @@ int wc_CoseEncrypt_Decrypt(const WOLFCOSE_RECIPIENT* recipient,
15421543
recipHpkeHdr.ek, recipHpkeHdr.ekLen,
15431544
hpkeWrappedCekData, hpkeWrappedCekLen,
15441545
cekHpke, sizeof(cekHpke));
1546+
/* A failed open still runs the content AEAD, under a zero CEK, so
1547+
* both layers fail the same way. */
1548+
if (ret == WOLFCOSE_E_COSE_DECRYPT_FAIL) {
1549+
(void)wolfCose_ForceZero(cekHpke, sizeof(cekHpke));
1550+
hpkeOpenFailed = 1;
1551+
ret = WOLFCOSE_SUCCESS;
1552+
}
15451553
}
15461554
if (ret == WOLFCOSE_SUCCESS) {
15471555
decKey = cekHpke;
@@ -1725,6 +1733,11 @@ int wc_CoseEncrypt_Decrypt(const WOLFCOSE_RECIPIENT* recipient,
17251733
else {
17261734
/* No action required */
17271735
}
1736+
#if defined(WOLFCOSE_HPKE_0_KE_DECRYPT)
1737+
if (hpkeOpenFailed != 0) {
1738+
ret = WOLFCOSE_E_COSE_DECRYPT_FAIL;
1739+
}
1740+
#endif
17281741

17291742
/* Cleanup — always runs */
17301743
#if defined(WOLFCOSE_HAVE_AESGCM) || defined(WOLFCOSE_HAVE_AESCCM)

‎tests/test_cose.c‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)