From ee7fe9e1b1353600033449e1469cb50de9a23d0a Mon Sep 17 00:00:00 2001 From: night1rider Date: Fri, 20 Mar 2026 17:34:17 -0600 Subject: [PATCH] SHE API: remove key storage from context, add direct output params --- .github/workflows/os-check.yml | 7 +- .wolfssl_known_macro_extras | 5 + CMakeLists.txt | 12 +- configure.ac | 18 +- tests/api.c | 3 + tests/api/test_she.c | 989 +++++++++++++-------------------- tests/api/test_she.h | 48 +- wolfcrypt/src/cryptocb.c | 85 ++- wolfcrypt/src/she.c | 403 +++++++------- wolfcrypt/test/test.c | 307 +++++++--- wolfssl/wolfcrypt/cryptocb.h | 65 ++- wolfssl/wolfcrypt/she.h | 120 ++-- 12 files changed, 1084 insertions(+), 978 deletions(-) diff --git a/.github/workflows/os-check.yml b/.github/workflows/os-check.yml index 322543cc95..2dc49dc660 100644 --- a/.github/workflows/os-check.yml +++ b/.github/workflows/os-check.yml @@ -42,8 +42,11 @@ jobs: '--enable-dtls --enable-dtlscid --enable-dtls13 --enable-secure-renegotiation --enable-psk --enable-aesccm --enable-nullcipher CPPFLAGS=-DWOLFSSL_STATIC_RSA', - '--enable-she --enable-cmac', - '--enable-she --enable-cmac --enable-cryptocb --enable-cryptocbutils', + '--enable-she=standard --enable-cmac', + '--enable-she=extended --enable-cmac --enable-cryptocb --enable-cryptocbutils', + '--enable-she=standard --enable-cmac CPPFLAGS=''-DNO_WC_SHE_IMPORT_M123'' ', + '--enable-she=extended --enable-cmac --enable-cryptocb --enable-cryptocbutils + CPPFLAGS=''-DNO_WC_SHE_GETUID -DNO_WC_SHE_GETCOUNTER -DNO_WC_SHE_EXPORTKEY'' ', '--enable-all CPPFLAGS=''-DNO_AES_192 -DNO_AES_256'' ', '--enable-sniffer --enable-curve25519 --enable-curve448 --enable-enckeys CPPFLAGS=-DWOLFSSL_DH_EXTRA', diff --git a/.wolfssl_known_macro_extras b/.wolfssl_known_macro_extras index 975063c5bc..61327b9f47 100644 --- a/.wolfssl_known_macro_extras +++ b/.wolfssl_known_macro_extras @@ -446,6 +446,10 @@ NO_TKERNEL_MEM_POOL NO_TLSX_PSKKEM_PLAIN_ANNOUNCE NO_VERIFY_OID NO_WC_DHGENERATEPUBLIC +NO_WC_SHE_EXPORTKEY +NO_WC_SHE_GETCOUNTER +NO_WC_SHE_GETUID +NO_WC_SHE_IMPORT_M123 NO_WC_SSIZE_TYPE NO_WOLFSSL_ALLOC_ALIGN NO_WOLFSSL_AUTOSAR_CRYIF @@ -889,6 +893,7 @@ WOLFSSL_SECURE_RENEGOTIATION_ON_BY_DEFAULT WOLFSSL_SERVER_EXAMPLE WOLFSSL_SETTINGS_FILE WOLFSSL_SHE +WOLFSSL_SHE_EXTENDED WOLFSSL_SH224 WOLFSSL_SHA256_ALT_CH_MAJ WOLFSSL_SHA512_HASHTYPE diff --git a/CMakeLists.txt b/CMakeLists.txt index 53bf54436c..a21eb8d311 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1641,11 +1641,12 @@ if(WOLFSSL_CMAC) endif() # SHE (Secure Hardware Extension) key update message generation +# standard: core SHE support, extended: adds custom KDF/header overrides add_option("WOLFSSL_SHE" - "Enable SHE key update support (default: disabled)" - "no" "yes;no") + "Enable SHE key update support (standard|extended|no)" + "no" "standard;extended;no") -if(WOLFSSL_SHE) +if(WOLFSSL_SHE STREQUAL "standard" OR WOLFSSL_SHE STREQUAL "extended") if (NOT WOLFSSL_AES) message(FATAL_ERROR "Cannot use SHE without AES.") else() @@ -1654,6 +1655,11 @@ if(WOLFSSL_SHE) endif() endif() +if(WOLFSSL_SHE STREQUAL "extended") + list(APPEND WOLFSSL_DEFINITIONS + "-DWOLFSSL_SHE_EXTENDED") +endif() + # TODO: - RC2 # - FIPS, again (there's more logic for FIPS in configure.ac) # - Selftest diff --git a/configure.ac b/configure.ac index a0216b1711..c6c1ba02ba 100644 --- a/configure.ac +++ b/configure.ac @@ -5946,14 +5946,24 @@ AS_IF([test "x$ENABLED_CMAC" = "xyes"], [AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_CMAC -DWOLFSSL_AES_DIRECT"]) # SHE (Secure Hardware Extension) key update message generation +# --enable-she=standard: standard SHE support +# --enable-she=extended: standard + extended overrides (custom KDF/headers) AC_ARG_ENABLE([she], - [AS_HELP_STRING([--enable-she],[Enable SHE key update support (default: disabled)])], + [AS_HELP_STRING([--enable-she@<:@=standard|extended@:>@], + [Enable SHE key update support (default: disabled)])], [ ENABLED_SHE=$enableval ], [ ENABLED_SHE=no ] ) -AS_IF([test "x$ENABLED_SHE" = "xyes"], - [AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_SHE"]) +if test "x$ENABLED_SHE" = "xstandard" || test "x$ENABLED_SHE" = "xextended" +then + AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_SHE" +fi + +if test "x$ENABLED_SHE" = "xextended" +then + AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_SHE_EXTENDED" +fi # AES-XTS AC_ARG_ENABLE([aesxts], @@ -11564,7 +11574,7 @@ AM_CONDITIONAL([BUILD_FIPS_V6],[test $HAVE_FIPS_VERSION = 6]) AM_CONDITIONAL([BUILD_FIPS_V6_PLUS],[test $HAVE_FIPS_VERSION -ge 6]) AM_CONDITIONAL([BUILD_SIPHASH],[test "x$ENABLED_SIPHASH" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"]) AM_CONDITIONAL([BUILD_CMAC],[test "x$ENABLED_CMAC" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"]) -AM_CONDITIONAL([BUILD_SHE],[test "x$ENABLED_SHE" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"]) +AM_CONDITIONAL([BUILD_SHE],[test "x$ENABLED_SHE" = "xstandard" || test "x$ENABLED_SHE" = "xextended" || test "x$ENABLED_USERSETTINGS" = "xyes"]) AM_CONDITIONAL([BUILD_SELFTEST],[test "x$ENABLED_SELFTEST" = "xyes"]) AM_CONDITIONAL([BUILD_SHA224],[test "x$ENABLED_SHA224" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"]) AM_CONDITIONAL([BUILD_SHA3],[test "x$ENABLED_SHA3" != "xno" || test "x$ENABLED_USERSETTINGS" = "xyes"]) diff --git a/tests/api.c b/tests/api.c index ca079f7389..423c9db19d 100644 --- a/tests/api.c +++ b/tests/api.c @@ -35638,6 +35638,9 @@ TEST_CASE testCases[] = { TEST_CMAC_DECLS, /* SHE */ TEST_SHE_DECLS, +#ifdef WOLFSSL_SHE_EXTENDED + TEST_SHE_EXT_DECLS, +#endif #if defined(WOLF_CRYPTO_CB) && defined(WOLFSSL_SHE) TEST_SHE_CB_DECLS, #endif diff --git a/tests/api/test_she.c b/tests/api/test_she.c index 25cc0ae883..2d98826646 100644 --- a/tests/api/test_she.c +++ b/tests/api/test_she.c @@ -36,69 +36,53 @@ #include #include -/* - * Testing wc_SHE_Init() - */ +/* Common test vector data */ +#if defined(WOLFSSL_SHE) && !defined(NO_AES) +static const byte sheTestUid[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 +}; +static const byte sheTestAuthKey[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f +}; +static const byte sheTestNewKey[] = { + 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, + 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 +}; +static const byte sheTestExpM1[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x41 +}; +static const byte sheTestExpM4[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x41, + 0xb4, 0x72, 0xe8, 0xd8, 0x72, 0x7d, 0x70, 0xd5, + 0x72, 0x95, 0xe7, 0x48, 0x49, 0xa2, 0x79, 0x17 +}; +static const byte sheTestExpM5[] = { + 0x82, 0x0d, 0x8d, 0x95, 0xdc, 0x11, 0xb4, 0x66, + 0x88, 0x78, 0x16, 0x0c, 0xb2, 0xa4, 0xe2, 0x3e +}; +#endif + int test_wc_SHE_Init(void) { EXPECT_DECLS; #if defined(WOLFSSL_SHE) && !defined(NO_AES) wc_SHE she; - /* Valid init with default heap/devId */ ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - - /* Verify heap and devId are stored correctly */ ExpectTrue(she.heap == NULL); ExpectIntEQ(she.devId, INVALID_DEVID); - - /* Verify state flags are zeroed */ - ExpectIntEQ(she.generated, 0); - ExpectIntEQ(she.verified, 0); - - /* Verify key material is zeroed */ - { - byte zeros[WC_SHE_KEY_SZ] = {0}; - ExpectIntEQ(XMEMCMP(she.authKey, zeros, WC_SHE_KEY_SZ), 0); - ExpectIntEQ(XMEMCMP(she.newKey, zeros, WC_SHE_KEY_SZ), 0); - } - wc_SHE_Free(&she); - /* Test bad args: NULL pointer */ ExpectIntEQ(wc_SHE_Init(NULL, NULL, INVALID_DEVID), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); #endif return EXPECT_RESULT(); -} /* END test_wc_SHE_Init */ +} -/* - * Testing wc_SHE_Free() - */ -int test_wc_SHE_Free(void) -{ - EXPECT_DECLS; -#if defined(WOLFSSL_SHE) && !defined(NO_AES) - wc_SHE she; - - /* Init, then free — should scrub key material */ - ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - wc_SHE_Free(&she); - - /* After free, context should be zeroed */ - ExpectIntEQ(she.devId, 0); - ExpectIntEQ(she.generated, 0); - ExpectIntEQ(she.verified, 0); - - /* Free with NULL should not crash */ - wc_SHE_Free(NULL); -#endif - return EXPECT_RESULT(); -} /* END test_wc_SHE_Free */ - -/* - * Testing wc_SHE_Init_Id() - */ int test_wc_SHE_Init_Id(void) { EXPECT_DECLS; @@ -106,430 +90,85 @@ int test_wc_SHE_Init_Id(void) wc_SHE she; unsigned char testId[] = {0x01, 0x02, 0x03, 0x04}; - /* Valid init with a 4-byte key ID */ ExpectIntEQ(wc_SHE_Init_Id(&she, testId, (int)sizeof(testId), NULL, INVALID_DEVID), 0); - - /* Verify the ID was copied and length is set */ ExpectIntEQ(she.idLen, (int)sizeof(testId)); - ExpectIntEQ(XMEMCMP(she.id, testId, sizeof(testId)), 0); - - /* Verify label length is cleared */ - ExpectIntEQ(she.labelLen, 0); - - /* Verify heap and devId are stored */ - ExpectTrue(she.heap == NULL); - ExpectIntEQ(she.devId, INVALID_DEVID); - wc_SHE_Free(&she); - /* Test bad args: NULL she pointer */ ExpectIntEQ(wc_SHE_Init_Id(NULL, testId, (int)sizeof(testId), NULL, INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - /* Test bad args: ID length too large */ ExpectIntEQ(wc_SHE_Init_Id(&she, testId, WC_SHE_MAX_ID_LEN + 1, NULL, INVALID_DEVID), WC_NO_ERR_TRACE(BUFFER_E)); - - /* Test bad args: negative ID length */ ExpectIntEQ(wc_SHE_Init_Id(&she, testId, -1, NULL, INVALID_DEVID), WC_NO_ERR_TRACE(BUFFER_E)); - - /* Test zero-length ID is valid */ - ExpectIntEQ(wc_SHE_Init_Id(&she, testId, 0, NULL, INVALID_DEVID), 0); - ExpectIntEQ(she.idLen, 0); - wc_SHE_Free(&she); #endif return EXPECT_RESULT(); -} /* END test_wc_SHE_Init_Id */ +} -/* - * Testing wc_SHE_Init_Label() - */ int test_wc_SHE_Init_Label(void) { EXPECT_DECLS; #if defined(WOLFSSL_SHE) && !defined(NO_AES) && defined(WOLF_PRIVATE_KEY_ID) wc_SHE she; - const char* testLabel = "my_she_key"; - - /* Valid init with a label string */ - ExpectIntEQ(wc_SHE_Init_Label(&she, testLabel, NULL, INVALID_DEVID), 0); - - /* Verify the label was copied and length is set */ - ExpectIntEQ(she.labelLen, (int)XSTRLEN(testLabel)); - ExpectIntEQ(XMEMCMP(she.label, testLabel, XSTRLEN(testLabel)), 0); - - /* Verify ID length is cleared */ - ExpectIntEQ(she.idLen, 0); - - /* Verify heap and devId are stored */ - ExpectTrue(she.heap == NULL); - ExpectIntEQ(she.devId, INVALID_DEVID); + ExpectIntEQ(wc_SHE_Init_Label(&she, "test", NULL, INVALID_DEVID), 0); + ExpectIntEQ(she.labelLen, 4); wc_SHE_Free(&she); - /* Test bad args: NULL she pointer */ - ExpectIntEQ(wc_SHE_Init_Label(NULL, testLabel, NULL, INVALID_DEVID), + ExpectIntEQ(wc_SHE_Init_Label(NULL, "test", NULL, INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - /* Test bad args: NULL label */ ExpectIntEQ(wc_SHE_Init_Label(&she, NULL, NULL, INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - /* Test bad args: empty label */ ExpectIntEQ(wc_SHE_Init_Label(&she, "", NULL, INVALID_DEVID), WC_NO_ERR_TRACE(BUFFER_E)); #endif return EXPECT_RESULT(); -} /* END test_wc_SHE_Init_Label */ +} -/* - * Testing wc_SHE_SetUID() - */ -int test_wc_SHE_SetUID(void) -{ - EXPECT_DECLS; -#if defined(WOLFSSL_SHE) && !defined(NO_AES) - wc_SHE she; - byte uid[WC_SHE_UID_SZ] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 - }; - - ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - - /* Valid UID */ - ExpectIntEQ(wc_SHE_SetUID(&she, uid, WC_SHE_UID_SZ, NULL), 0); - ExpectIntEQ(XMEMCMP(she.uid, uid, WC_SHE_UID_SZ), 0); - - /* Bad args */ - ExpectIntEQ(wc_SHE_SetUID(NULL, uid, WC_SHE_UID_SZ, NULL), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - ExpectIntEQ(wc_SHE_SetUID(&she, NULL, WC_SHE_UID_SZ, NULL), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - ExpectIntEQ(wc_SHE_SetUID(&she, uid, WC_SHE_UID_SZ - 1, NULL), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - ExpectIntEQ(wc_SHE_SetUID(&she, uid, WC_SHE_UID_SZ + 1, NULL), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - wc_SHE_Free(&she); -#endif - return EXPECT_RESULT(); -} /* END test_wc_SHE_SetUID */ - -/* - * Testing wc_SHE_SetAuthKey() - */ -int test_wc_SHE_SetAuthKey(void) -{ - EXPECT_DECLS; -#if defined(WOLFSSL_SHE) && !defined(NO_AES) - wc_SHE she; - byte key[WC_SHE_KEY_SZ] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f - }; - - ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - - /* Valid auth key */ - ExpectIntEQ(wc_SHE_SetAuthKey(&she, WC_SHE_MASTER_ECU_KEY_ID, - key, WC_SHE_KEY_SZ), 0); - ExpectIntEQ(she.authKeyId, WC_SHE_MASTER_ECU_KEY_ID); - ExpectIntEQ(XMEMCMP(she.authKey, key, WC_SHE_KEY_SZ), 0); - - /* Bad args */ - ExpectIntEQ(wc_SHE_SetAuthKey(NULL, 0, key, WC_SHE_KEY_SZ), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - ExpectIntEQ(wc_SHE_SetAuthKey(&she, 0, NULL, WC_SHE_KEY_SZ), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - ExpectIntEQ(wc_SHE_SetAuthKey(&she, 0, key, WC_SHE_KEY_SZ - 1), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - wc_SHE_Free(&she); -#endif - return EXPECT_RESULT(); -} /* END test_wc_SHE_SetAuthKey */ - -/* - * Testing wc_SHE_SetNewKey() - */ -int test_wc_SHE_SetNewKey(void) -{ - EXPECT_DECLS; -#if defined(WOLFSSL_SHE) && !defined(NO_AES) - wc_SHE she; - byte key[WC_SHE_KEY_SZ] = { - 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, - 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 - }; - - ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - - /* Valid new key */ - ExpectIntEQ(wc_SHE_SetNewKey(&she, 4, key, WC_SHE_KEY_SZ), 0); - ExpectIntEQ(she.targetKeyId, 4); - ExpectIntEQ(XMEMCMP(she.newKey, key, WC_SHE_KEY_SZ), 0); - - /* Bad args */ - ExpectIntEQ(wc_SHE_SetNewKey(NULL, 4, key, WC_SHE_KEY_SZ), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - ExpectIntEQ(wc_SHE_SetNewKey(&she, 4, NULL, WC_SHE_KEY_SZ), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - ExpectIntEQ(wc_SHE_SetNewKey(&she, 4, key, 0), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - wc_SHE_Free(&she); -#endif - return EXPECT_RESULT(); -} /* END test_wc_SHE_SetNewKey */ - -/* - * Testing wc_SHE_SetCounter() - */ -int test_wc_SHE_SetCounter(void) +int test_wc_SHE_Free(void) { EXPECT_DECLS; #if defined(WOLFSSL_SHE) && !defined(NO_AES) wc_SHE she; ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - - ExpectIntEQ(wc_SHE_SetCounter(&she, 1), 0); - ExpectIntEQ(she.counter, 1); - - ExpectIntEQ(wc_SHE_SetCounter(&she, 0x0FFFFFFF), 0); - ExpectIntEQ(she.counter, 0x0FFFFFFF); - - /* Bad args */ - ExpectIntEQ(wc_SHE_SetCounter(NULL, 1), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - wc_SHE_Free(&she); + ExpectIntEQ(she.devId, 0); + + wc_SHE_Free(NULL); #endif return EXPECT_RESULT(); -} /* END test_wc_SHE_SetCounter */ +} -/* - * Testing wc_SHE_SetFlags() - */ -int test_wc_SHE_SetFlags(void) +int test_wc_SHE_ImportM1M2M3(void) { EXPECT_DECLS; -#if defined(WOLFSSL_SHE) && !defined(NO_AES) +#if defined(WOLFSSL_SHE) && !defined(NO_AES) && \ + (defined(WOLF_CRYPTO_CB) || !defined(NO_WC_SHE_IMPORT_M123)) wc_SHE she; + byte m1[WC_SHE_M1_SZ] = {0}; + byte m2[WC_SHE_M2_SZ] = {0}; + byte m3[WC_SHE_M3_SZ] = {0}; ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - - ExpectIntEQ(wc_SHE_SetFlags(&she, 0), 0); - ExpectIntEQ(she.flags, 0); - - ExpectIntEQ(wc_SHE_SetFlags(&she, WC_SHE_FLAG_WRITE_PROTECT | - WC_SHE_FLAG_BOOT_PROTECT), 0); - ExpectIntEQ(she.flags, WC_SHE_FLAG_WRITE_PROTECT | - WC_SHE_FLAG_BOOT_PROTECT); - - /* Bad args */ - ExpectIntEQ(wc_SHE_SetFlags(NULL, 0), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - wc_SHE_Free(&she); -#endif - return EXPECT_RESULT(); -} /* END test_wc_SHE_SetFlags */ - -/* - * Testing wc_SHE_SetKdfConstants() - */ -int test_wc_SHE_SetKdfConstants(void) -{ - EXPECT_DECLS; -#if defined(WOLFSSL_SHE) && !defined(NO_AES) - wc_SHE she; - const byte defEncC[] = WC_SHE_KEY_UPDATE_ENC_C; - const byte defMacC[] = WC_SHE_KEY_UPDATE_MAC_C; - byte customEncC[WC_SHE_KEY_SZ]; - byte customMacC[WC_SHE_KEY_SZ]; - - ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - - /* Init should set defaults */ - ExpectIntEQ(XMEMCMP(she.kdfEncC, defEncC, WC_SHE_KEY_SZ), 0); - ExpectIntEQ(XMEMCMP(she.kdfMacC, defMacC, WC_SHE_KEY_SZ), 0); - - /* Override both */ - XMEMCPY(customEncC, defEncC, WC_SHE_KEY_SZ); - XMEMCPY(customMacC, defMacC, WC_SHE_KEY_SZ); - customEncC[1] += 0x80; - customMacC[1] += 0x80; - ExpectIntEQ(wc_SHE_SetKdfConstants(&she, - customEncC, WC_SHE_KEY_SZ, - customMacC, WC_SHE_KEY_SZ), 0); - ExpectIntEQ(XMEMCMP(she.kdfEncC, customEncC, WC_SHE_KEY_SZ), 0); - ExpectIntEQ(XMEMCMP(she.kdfMacC, customMacC, WC_SHE_KEY_SZ), 0); - - /* Override only encC, leave macC unchanged */ - ExpectIntEQ(wc_SHE_SetKdfConstants(&she, - defEncC, WC_SHE_KEY_SZ, NULL, 0), 0); - ExpectIntEQ(XMEMCMP(she.kdfEncC, defEncC, WC_SHE_KEY_SZ), 0); - ExpectIntEQ(XMEMCMP(she.kdfMacC, customMacC, WC_SHE_KEY_SZ), 0); - - /* Bad args: NULL she */ - ExpectIntEQ(wc_SHE_SetKdfConstants(NULL, - defEncC, WC_SHE_KEY_SZ, defMacC, WC_SHE_KEY_SZ), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - /* Bad args: wrong size */ - ExpectIntEQ(wc_SHE_SetKdfConstants(&she, - defEncC, WC_SHE_KEY_SZ - 1, NULL, 0), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - wc_SHE_Free(&she); -#endif - return EXPECT_RESULT(); -} /* END test_wc_SHE_SetKdfConstants */ - -/* - * Testing wc_SHE_SetM2Header() and wc_SHE_SetM4Header() - */ -int test_wc_SHE_SetM2M4Header(void) -{ - EXPECT_DECLS; -#if defined(WOLFSSL_SHE) && !defined(NO_AES) - wc_SHE she, sheOvr; - byte uid[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 - }; - byte authKey[] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f - }; - byte newKey[] = { - 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, - 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 - }; - byte customHeader[WC_SHE_KEY_SZ] = {0}; - byte m1Def[WC_SHE_M1_SZ], m2Def[WC_SHE_M2_SZ], m3Def[WC_SHE_M3_SZ]; - byte m1Ovr[WC_SHE_M1_SZ], m2Ovr[WC_SHE_M2_SZ], m3Ovr[WC_SHE_M3_SZ]; - - /* --- SetM2Header bad args --- */ - ExpectIntEQ(wc_SHE_SetM2Header(NULL, customHeader, WC_SHE_KEY_SZ), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - ExpectIntEQ(wc_SHE_SetM4Header(NULL, customHeader, WC_SHE_KEY_SZ), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - - /* NULL header */ - ExpectIntEQ(wc_SHE_SetM2Header(&she, NULL, WC_SHE_KEY_SZ), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - /* Wrong size */ - ExpectIntEQ(wc_SHE_SetM2Header(&she, customHeader, WC_SHE_KEY_SZ - 1), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - ExpectIntEQ(wc_SHE_SetM4Header(&she, customHeader, WC_SHE_KEY_SZ + 1), - WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - /* Valid set */ - XMEMSET(customHeader, 0xAA, WC_SHE_KEY_SZ); - ExpectIntEQ(wc_SHE_SetM2Header(&she, customHeader, WC_SHE_KEY_SZ), 0); - ExpectIntEQ(XMEMCMP(she.m2pHeader, customHeader, WC_SHE_KEY_SZ), 0); - ExpectIntEQ(she.m2pOverride, 1); - - ExpectIntEQ(wc_SHE_SetM4Header(&she, customHeader, WC_SHE_KEY_SZ), 0); - ExpectIntEQ(XMEMCMP(she.m4pHeader, customHeader, WC_SHE_KEY_SZ), 0); - ExpectIntEQ(she.m4pOverride, 1); - - wc_SHE_Free(&she); - - /* --- Override produces different M2 than default --- */ - /* Default path: counter=1, flags=0, auto-built headers */ - ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - ExpectIntEQ(wc_SHE_SetUID(&she, uid, sizeof(uid), NULL), 0); - ExpectIntEQ(wc_SHE_SetAuthKey(&she, 1, authKey, sizeof(authKey)), 0); - ExpectIntEQ(wc_SHE_SetNewKey(&she, 4, newKey, sizeof(newKey)), 0); - ExpectIntEQ(wc_SHE_SetCounter(&she, 1), 0); - ExpectIntEQ(wc_SHE_SetFlags(&she, 0), 0); - ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she), 0); - ExpectIntEQ(wc_SHE_ExportKey(&she, - m1Def, WC_SHE_M1_SZ, m2Def, WC_SHE_M2_SZ, - m3Def, WC_SHE_M3_SZ, NULL, 0, NULL, 0, NULL), 0); - wc_SHE_Free(&she); - - /* Override path: same inputs but custom m2pHeader */ - ExpectIntEQ(wc_SHE_Init(&sheOvr, NULL, INVALID_DEVID), 0); - ExpectIntEQ(wc_SHE_SetUID(&sheOvr, uid, sizeof(uid), NULL), 0); - ExpectIntEQ(wc_SHE_SetAuthKey(&sheOvr, 1, authKey, sizeof(authKey)), 0); - ExpectIntEQ(wc_SHE_SetNewKey(&sheOvr, 4, newKey, sizeof(newKey)), 0); - ExpectIntEQ(wc_SHE_SetCounter(&sheOvr, 1), 0); - ExpectIntEQ(wc_SHE_SetFlags(&sheOvr, 0), 0); - /* Set a different header ΓÇö should produce different M2/M3 */ - XMEMSET(customHeader, 0, WC_SHE_KEY_SZ); - customHeader[0] = 0xFF; /* different from auto-built */ - ExpectIntEQ(wc_SHE_SetM2Header(&sheOvr, customHeader, WC_SHE_KEY_SZ), 0); - ExpectIntEQ(wc_SHE_GenerateM1M2M3(&sheOvr), 0); - ExpectIntEQ(wc_SHE_ExportKey(&sheOvr, - m1Ovr, WC_SHE_M1_SZ, m2Ovr, WC_SHE_M2_SZ, - m3Ovr, WC_SHE_M3_SZ, NULL, 0, NULL, 0, NULL), 0); - - /* M1 should be same (UID|IDs unchanged), M2 should differ */ - ExpectIntEQ(XMEMCMP(m1Def, m1Ovr, WC_SHE_M1_SZ), 0); - ExpectIntNE(XMEMCMP(m2Def, m2Ovr, WC_SHE_M2_SZ), 0); - - wc_SHE_Free(&sheOvr); -#endif - return EXPECT_RESULT(); -} /* END test_wc_SHE_SetM2M4Header */ - -/* - * Testing wc_SHE_GenerateM1M2M3() - */ -int test_wc_SHE_GenerateM1M2M3(void) -{ - EXPECT_DECLS; -#if defined(WOLFSSL_SHE) && !defined(NO_AES) - wc_SHE she; - byte uid[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 - }; - byte authKey[] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f - }; - byte newKey[] = { - 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, - 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 - }; - - ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - ExpectIntEQ(wc_SHE_SetUID(&she, uid, sizeof(uid), NULL), 0); - ExpectIntEQ(wc_SHE_SetAuthKey(&she, WC_SHE_MASTER_ECU_KEY_ID, - authKey, sizeof(authKey)), 0); - ExpectIntEQ(wc_SHE_SetNewKey(&she, 4, newKey, sizeof(newKey)), 0); - ExpectIntEQ(wc_SHE_SetCounter(&she, 1), 0); - ExpectIntEQ(wc_SHE_SetFlags(&she, 0), 0); - - /* Generate should succeed and set generated flag */ - ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she), 0); + ExpectIntEQ(wc_SHE_ImportM1M2M3(&she, + m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ), 0); ExpectIntEQ(she.generated, 1); - /* Bad args */ - ExpectIntEQ(wc_SHE_GenerateM1M2M3(NULL), + ExpectIntEQ(wc_SHE_ImportM1M2M3(NULL, + m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_SHE_ImportM1M2M3(&she, + m1, WC_SHE_M1_SZ - 1, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); wc_SHE_Free(&she); #endif return EXPECT_RESULT(); -} /* END test_wc_SHE_GenerateM1M2M3 */ +} -/* - * Testing wc_She_AesMp16() — Miyaguchi-Preneel compression - */ int test_wc_She_AesMp16(void) { EXPECT_DECLS; @@ -542,7 +181,6 @@ int test_wc_She_AesMp16(void) 0x01, 0x01, 0x53, 0x48, 0x45, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xB0 }; - /* 17 bytes — not block-aligned, triggers zero-padding path */ byte shortInput[17] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, @@ -550,39 +188,26 @@ int test_wc_She_AesMp16(void) }; ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0); - - /* Valid block-aligned input */ ExpectIntEQ(wc_She_AesMp16(&aes, input, sizeof(input), out), 0); - /* Non-block-aligned input — exercises zero-padding */ ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0); ExpectIntEQ(wc_She_AesMp16(&aes, shortInput, sizeof(shortInput), out), 0); - /* Bad args: NULL aes */ ExpectIntEQ(wc_She_AesMp16(NULL, input, sizeof(input), out), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - /* Bad args: NULL input */ ExpectIntEQ(wc_She_AesMp16(&aes, NULL, sizeof(input), out), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - /* Bad args: zero size */ ExpectIntEQ(wc_She_AesMp16(&aes, input, 0, out), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - - /* Bad args: NULL output */ ExpectIntEQ(wc_She_AesMp16(&aes, input, sizeof(input), NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); wc_AesFree(&aes); #endif return EXPECT_RESULT(); -} /* END test_wc_She_AesMp16 */ +} -/* - * Testing wc_SHE_ExportKey() - */ -int test_wc_SHE_ExportKey(void) +int test_wc_SHE_GenerateM1M2M3(void) { EXPECT_DECLS; #if defined(WOLFSSL_SHE) && !defined(NO_AES) @@ -590,148 +215,225 @@ int test_wc_SHE_ExportKey(void) byte m1[WC_SHE_M1_SZ]; byte m2[WC_SHE_M2_SZ]; byte m3[WC_SHE_M3_SZ]; - byte m4[WC_SHE_M4_SZ]; - byte m5[WC_SHE_M5_SZ]; - byte uid[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 - }; - byte authKey[] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f - }; - byte newKey[] = { - 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, - 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 - }; ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - /* Export before generate should return BAD_STATE_E */ - ExpectIntEQ(wc_SHE_ExportKey(&she, - m1, WC_SHE_M1_SZ, NULL, 0, NULL, 0, - NULL, 0, NULL, 0, NULL), - WC_NO_ERR_TRACE(BAD_STATE_E)); + /* Generate and verify M1 against test vector */ + ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she, + sheTestUid, sizeof(sheTestUid), + WC_SHE_MASTER_ECU_KEY_ID, sheTestAuthKey, sizeof(sheTestAuthKey), + 4, sheTestNewKey, sizeof(sheTestNewKey), + 1, 0, + m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ), 0); + ExpectIntEQ(XMEMCMP(m1, sheTestExpM1, WC_SHE_M1_SZ), 0); - /* NULL she should return BAD_FUNC_ARG */ - ExpectIntEQ(wc_SHE_ExportKey(NULL, - m1, WC_SHE_M1_SZ, NULL, 0, NULL, 0, - NULL, 0, NULL, 0, NULL), + /* Bad args */ + ExpectIntEQ(wc_SHE_GenerateM1M2M3(NULL, + sheTestUid, sizeof(sheTestUid), + 1, sheTestAuthKey, sizeof(sheTestAuthKey), + 4, sheTestNewKey, sizeof(sheTestNewKey), + 1, 0, + m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); - /* Set up, generate, and compute verification */ - ExpectIntEQ(wc_SHE_SetUID(&she, uid, sizeof(uid), NULL), 0); - ExpectIntEQ(wc_SHE_SetAuthKey(&she, WC_SHE_MASTER_ECU_KEY_ID, - authKey, sizeof(authKey)), 0); - ExpectIntEQ(wc_SHE_SetNewKey(&she, 4, newKey, sizeof(newKey)), 0); - ExpectIntEQ(wc_SHE_SetCounter(&she, 1), 0); - ExpectIntEQ(wc_SHE_SetFlags(&she, 0), 0); - ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she), 0); - ExpectIntEQ(wc_SHE_GenerateM4M5(&she), 0); - - /* Export only M1/M2/M3 */ - ExpectIntEQ(wc_SHE_ExportKey(&she, - m1, WC_SHE_M1_SZ, - m2, WC_SHE_M2_SZ, - m3, WC_SHE_M3_SZ, - NULL, 0, NULL, 0, NULL), 0); - - /* Export only M4/M5 */ - ExpectIntEQ(wc_SHE_ExportKey(&she, - NULL, 0, NULL, 0, NULL, 0, - m4, WC_SHE_M4_SZ, - m5, WC_SHE_M5_SZ, NULL), 0); - - /* Export all M1-M5 */ - ExpectIntEQ(wc_SHE_ExportKey(&she, - m1, WC_SHE_M1_SZ, - m2, WC_SHE_M2_SZ, - m3, WC_SHE_M3_SZ, - m4, WC_SHE_M4_SZ, - m5, WC_SHE_M5_SZ, NULL), 0); - - /* Buffer too small */ - ExpectIntEQ(wc_SHE_ExportKey(&she, - m1, 1, NULL, 0, NULL, 0, - NULL, 0, NULL, 0, NULL), - WC_NO_ERR_TRACE(BUFFER_E)); - - /* Export M4/M5 when generated but not verified — BAD_STATE_E */ - { - wc_SHE badShe; - ExpectIntEQ(wc_SHE_Init(&badShe, NULL, INVALID_DEVID), 0); - badShe.generated = 1; /* fake generated state */ - badShe.verified = 0; /* but not verified */ - ExpectIntEQ(wc_SHE_ExportKey(&badShe, - NULL, 0, NULL, 0, NULL, 0, - m4, WC_SHE_M4_SZ, - NULL, 0, NULL), - WC_NO_ERR_TRACE(BAD_STATE_E)); - wc_SHE_Free(&badShe); - } - wc_SHE_Free(&she); #endif return EXPECT_RESULT(); -} /* END test_wc_SHE_ExportKey */ +} -/* - * Testing wc_SHE_GenerateM4M5() - */ int test_wc_SHE_GenerateM4M5(void) { EXPECT_DECLS; #if defined(WOLFSSL_SHE) && !defined(NO_AES) wc_SHE she; - byte uid[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 - }; - byte authKey[] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f - }; - byte newKey[] = { - 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, - 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 - }; + byte m4[WC_SHE_M4_SZ]; + byte m5[WC_SHE_M5_SZ]; ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); - /* GenerateM4M5 before GenerateM1M2M3 should return BAD_STATE_E */ - ExpectIntEQ(wc_SHE_GenerateM4M5(&she), - WC_NO_ERR_TRACE(BAD_STATE_E)); - - /* Set up and generate M1/M2/M3 */ - ExpectIntEQ(wc_SHE_SetUID(&she, uid, sizeof(uid), NULL), 0); - ExpectIntEQ(wc_SHE_SetAuthKey(&she, WC_SHE_MASTER_ECU_KEY_ID, - authKey, sizeof(authKey)), 0); - ExpectIntEQ(wc_SHE_SetNewKey(&she, 4, newKey, sizeof(newKey)), 0); - ExpectIntEQ(wc_SHE_SetCounter(&she, 1), 0); - ExpectIntEQ(wc_SHE_SetFlags(&she, 0), 0); - ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she), 0); - - /* Now compute M4/M5 */ - ExpectIntEQ(wc_SHE_GenerateM4M5(&she), 0); - ExpectIntEQ(she.verified, 1); + /* Generate and verify against test vector */ + ExpectIntEQ(wc_SHE_GenerateM4M5(&she, + sheTestUid, sizeof(sheTestUid), + WC_SHE_MASTER_ECU_KEY_ID, 4, + sheTestNewKey, sizeof(sheTestNewKey), 1, + m4, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ), 0); + ExpectIntEQ(XMEMCMP(m4, sheTestExpM4, WC_SHE_M4_SZ), 0); + ExpectIntEQ(XMEMCMP(m5, sheTestExpM5, WC_SHE_M5_SZ), 0); /* Bad args */ - ExpectIntEQ(wc_SHE_GenerateM4M5(NULL), + ExpectIntEQ(wc_SHE_GenerateM4M5(NULL, + sheTestUid, sizeof(sheTestUid), + 1, 4, sheTestNewKey, sizeof(sheTestNewKey), 1, + m4, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); wc_SHE_Free(&she); #endif return EXPECT_RESULT(); -} /* END test_wc_SHE_GenerateM4M5 */ +} + +#if defined(WOLFSSL_SHE_EXTENDED) && defined(WOLFSSL_SHE) && !defined(NO_AES) + +int test_wc_SHE_SetKdfConstants(void) +{ + EXPECT_DECLS; + wc_SHE she; + byte m1Def[WC_SHE_M1_SZ]; + byte m2Def[WC_SHE_M2_SZ]; + byte m3Def[WC_SHE_M3_SZ]; + byte m1Cust[WC_SHE_M1_SZ]; + byte m2Cust[WC_SHE_M2_SZ]; + byte m3Cust[WC_SHE_M3_SZ]; + byte m4[WC_SHE_M4_SZ]; + byte m5[WC_SHE_M5_SZ]; + byte customEncC[WC_SHE_KEY_SZ] = {0}; + byte customMacC[WC_SHE_KEY_SZ] = {0}; + + customEncC[0] = 0xFF; + customMacC[0] = 0xFE; + + /* Generate with defaults */ + ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); + ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she, + sheTestUid, sizeof(sheTestUid), + WC_SHE_MASTER_ECU_KEY_ID, sheTestAuthKey, sizeof(sheTestAuthKey), + 4, sheTestNewKey, sizeof(sheTestNewKey), 1, 0, + m1Def, WC_SHE_M1_SZ, m2Def, WC_SHE_M2_SZ, + m3Def, WC_SHE_M3_SZ), 0); + wc_SHE_Free(&she); + + /* Generate with custom KDF constants */ + ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); + ExpectIntEQ(wc_SHE_SetKdfConstants(&she, + customEncC, WC_SHE_KEY_SZ, + customMacC, WC_SHE_KEY_SZ), 0); + ExpectIntEQ(she.kdfEncOverride, 1); + ExpectIntEQ(she.kdfMacOverride, 1); + + ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she, + sheTestUid, sizeof(sheTestUid), + WC_SHE_MASTER_ECU_KEY_ID, sheTestAuthKey, sizeof(sheTestAuthKey), + 4, sheTestNewKey, sizeof(sheTestNewKey), 1, 0, + m1Cust, WC_SHE_M1_SZ, m2Cust, WC_SHE_M2_SZ, + m3Cust, WC_SHE_M3_SZ), 0); + + /* M1 same, M2 should differ */ + ExpectIntEQ(XMEMCMP(m1Def, m1Cust, WC_SHE_M1_SZ), 0); + ExpectIntNE(XMEMCMP(m2Def, m2Cust, WC_SHE_M2_SZ), 0); + + /* Bad args */ + ExpectIntEQ(wc_SHE_SetKdfConstants(NULL, + customEncC, WC_SHE_KEY_SZ, NULL, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_SHE_SetKdfConstants(&she, + customEncC, WC_SHE_KEY_SZ - 1, NULL, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_SHE_SetKdfConstants(&she, + NULL, 0, customMacC, WC_SHE_KEY_SZ - 1), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + + /* Test KDF override in M4M5 path */ + ExpectIntEQ(wc_SHE_GenerateM4M5(&she, + sheTestUid, sizeof(sheTestUid), + WC_SHE_MASTER_ECU_KEY_ID, 4, + sheTestNewKey, sizeof(sheTestNewKey), 1, + m4, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ), 0); + + wc_SHE_Free(&she); + return EXPECT_RESULT(); +} + +int test_wc_SHE_SetM2M4Header(void) +{ + EXPECT_DECLS; + wc_SHE she; + byte customHeader[WC_SHE_KEY_SZ] = {0}; + byte m1Def[WC_SHE_M1_SZ]; + byte m2Def[WC_SHE_M2_SZ]; + byte m3Def[WC_SHE_M3_SZ]; + byte m1Ovr[WC_SHE_M1_SZ]; + byte m2Ovr[WC_SHE_M2_SZ]; + byte m3Ovr[WC_SHE_M3_SZ]; + byte m4Def[WC_SHE_M4_SZ]; + byte m5Def[WC_SHE_M5_SZ]; + byte m4Ovr[WC_SHE_M4_SZ]; + byte m5Ovr[WC_SHE_M5_SZ]; + + /* Bad args */ + ExpectIntEQ(wc_SHE_SetM2Header(NULL, customHeader, WC_SHE_KEY_SZ), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_SHE_SetM4Header(NULL, customHeader, WC_SHE_KEY_SZ), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + + ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); + + ExpectIntEQ(wc_SHE_SetM2Header(&she, NULL, WC_SHE_KEY_SZ), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_SHE_SetM2Header(&she, customHeader, WC_SHE_KEY_SZ - 1), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + + /* Generate M1M2M3 with defaults */ + ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she, + sheTestUid, sizeof(sheTestUid), + WC_SHE_MASTER_ECU_KEY_ID, sheTestAuthKey, sizeof(sheTestAuthKey), + 4, sheTestNewKey, sizeof(sheTestNewKey), 1, 0, + m1Def, WC_SHE_M1_SZ, m2Def, WC_SHE_M2_SZ, + m3Def, WC_SHE_M3_SZ), 0); + wc_SHE_Free(&she); + + /* Generate with overridden M2 header */ + ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); + customHeader[0] = 0xFF; + ExpectIntEQ(wc_SHE_SetM2Header(&she, customHeader, WC_SHE_KEY_SZ), 0); + ExpectIntEQ(she.m2pOverride, 1); + + ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she, + sheTestUid, sizeof(sheTestUid), + WC_SHE_MASTER_ECU_KEY_ID, sheTestAuthKey, sizeof(sheTestAuthKey), + 4, sheTestNewKey, sizeof(sheTestNewKey), 1, 0, + m1Ovr, WC_SHE_M1_SZ, m2Ovr, WC_SHE_M2_SZ, + m3Ovr, WC_SHE_M3_SZ), 0); + + ExpectIntEQ(XMEMCMP(m1Def, m1Ovr, WC_SHE_M1_SZ), 0); + ExpectIntNE(XMEMCMP(m2Def, m2Ovr, WC_SHE_M2_SZ), 0); + wc_SHE_Free(&she); + + /* Test M4 header override */ + ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); + ExpectIntEQ(wc_SHE_GenerateM4M5(&she, + sheTestUid, sizeof(sheTestUid), + WC_SHE_MASTER_ECU_KEY_ID, 4, + sheTestNewKey, sizeof(sheTestNewKey), 1, + m4Def, WC_SHE_M4_SZ, m5Def, WC_SHE_M5_SZ), 0); + wc_SHE_Free(&she); + + ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0); + XMEMSET(customHeader, 0xBB, WC_SHE_KEY_SZ); + ExpectIntEQ(wc_SHE_SetM4Header(&she, customHeader, WC_SHE_KEY_SZ), 0); + ExpectIntEQ(she.m4pOverride, 1); + + ExpectIntEQ(wc_SHE_GenerateM4M5(&she, + sheTestUid, sizeof(sheTestUid), + WC_SHE_MASTER_ECU_KEY_ID, 4, + sheTestNewKey, sizeof(sheTestNewKey), 1, + m4Ovr, WC_SHE_M4_SZ, m5Ovr, WC_SHE_M5_SZ), 0); + + ExpectIntNE(XMEMCMP(m4Def, m4Ovr, WC_SHE_M4_SZ), 0); + wc_SHE_Free(&she); + + return EXPECT_RESULT(); +} + +#endif /* WOLFSSL_SHE_EXTENDED && WOLFSSL_SHE && !NO_AES */ #if defined(WOLF_CRYPTO_CB) && defined(WOLFSSL_SHE) && !defined(NO_AES) -/* Simple SHE callback that falls back to software by resetting devId */ +/* SHE callback — re-calls with software devId */ static int test_she_crypto_cb(int devIdArg, wc_CryptoInfo* info, void* ctx) { - int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); wc_SHE* she; int savedDevId; + int ret; (void)ctx; (void)devIdArg; @@ -741,7 +443,6 @@ static int test_she_crypto_cb(int devIdArg, wc_CryptoInfo* info, void* ctx) } #ifdef WOLF_CRYPTO_CB_FREE - /* Handle free callback */ if (info->algo_type == WC_ALGO_TYPE_FREE) { if (info->free.algo == WC_ALGO_TYPE_SHE) { she = (wc_SHE*)info->free.obj; @@ -767,26 +468,68 @@ static int test_she_crypto_cb(int devIdArg, wc_CryptoInfo* info, void* ctx) switch (info->she.type) { case WC_SHE_SET_UID: - ret = wc_SHE_SetUID(she, info->she.op.setUid.uid, - info->she.op.setUid.uidSz, - info->she.ctx); + ret = 0; + break; + case WC_SHE_GET_COUNTER: + { + static word32 simCounter = 0; + if (info->she.op.getCounter.counter != NULL) { + *info->she.op.getCounter.counter = ++simCounter; + } + ret = 0; + break; + } + case WC_SHE_GENERATE_M1M2M3: + ret = wc_SHE_GenerateM1M2M3(she, + info->she.op.generateM1M2M3.uid, + info->she.op.generateM1M2M3.uidSz, + info->she.op.generateM1M2M3.authKeyId, + info->she.op.generateM1M2M3.authKey, + info->she.op.generateM1M2M3.authKeySz, + info->she.op.generateM1M2M3.targetKeyId, + info->she.op.generateM1M2M3.newKey, + info->she.op.generateM1M2M3.newKeySz, + info->she.op.generateM1M2M3.counter, + info->she.op.generateM1M2M3.flags, + info->she.op.generateM1M2M3.m1, + info->she.op.generateM1M2M3.m1Sz, + info->she.op.generateM1M2M3.m2, + info->she.op.generateM1M2M3.m2Sz, + info->she.op.generateM1M2M3.m3, + info->she.op.generateM1M2M3.m3Sz); break; case WC_SHE_GENERATE_M4M5: - ret = wc_SHE_GenerateM4M5(she); + ret = wc_SHE_GenerateM4M5(she, + info->she.op.generateM4M5.uid, + info->she.op.generateM4M5.uidSz, + info->she.op.generateM4M5.authKeyId, + info->she.op.generateM4M5.targetKeyId, + info->she.op.generateM4M5.newKey, + info->she.op.generateM4M5.newKeySz, + info->she.op.generateM4M5.counter, + info->she.op.generateM4M5.m4, + info->she.op.generateM4M5.m4Sz, + info->she.op.generateM4M5.m5, + info->she.op.generateM4M5.m5Sz); break; case WC_SHE_EXPORT_KEY: - ret = wc_SHE_ExportKey(she, - info->she.op.exportKey.m1, - info->she.op.exportKey.m1Sz, - info->she.op.exportKey.m2, - info->she.op.exportKey.m2Sz, - info->she.op.exportKey.m3, - info->she.op.exportKey.m3Sz, - info->she.op.exportKey.m4, - info->she.op.exportKey.m4Sz, - info->she.op.exportKey.m5, - info->she.op.exportKey.m5Sz, - info->she.ctx); + /* Simulate hardware export — fill with test pattern */ + if (info->she.op.exportKey.m1 != NULL) { + XMEMSET(info->she.op.exportKey.m1, 0x11, WC_SHE_M1_SZ); + } + if (info->she.op.exportKey.m2 != NULL) { + XMEMSET(info->she.op.exportKey.m2, 0x22, WC_SHE_M2_SZ); + } + if (info->she.op.exportKey.m3 != NULL) { + XMEMSET(info->she.op.exportKey.m3, 0x33, WC_SHE_M3_SZ); + } + if (info->she.op.exportKey.m4 != NULL) { + XMEMSET(info->she.op.exportKey.m4, 0x44, WC_SHE_M4_SZ); + } + if (info->she.op.exportKey.m5 != NULL) { + XMEMSET(info->she.op.exportKey.m5, 0x55, WC_SHE_M5_SZ); + } + ret = 0; break; default: ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); @@ -797,79 +540,99 @@ static int test_she_crypto_cb(int devIdArg, wc_CryptoInfo* info, void* ctx) return ret; } -/* - * Testing SHE callback path for SetUID and GenerateM4M5 - */ int test_wc_SHE_CryptoCb(void) { EXPECT_DECLS; -#if defined(WOLFSSL_SHE) && !defined(NO_AES) wc_SHE she; int sheTestDevId = 54321; + byte m1[WC_SHE_M1_SZ]; + byte m2[WC_SHE_M2_SZ]; + byte m3[WC_SHE_M3_SZ]; byte m4[WC_SHE_M4_SZ]; byte m5[WC_SHE_M5_SZ]; - byte uid[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 - }; - byte authKey[] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f - }; - byte newKey[] = { - 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, - 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 - }; - /* Register our test callback with a non-INVALID devId */ ExpectIntEQ(wc_CryptoCb_RegisterDevice(sheTestDevId, test_she_crypto_cb, NULL), 0); - - /* Init with the test devId so callback path is used */ ExpectIntEQ(wc_SHE_Init(&she, NULL, sheTestDevId), 0); - /* SetUID via callback — passes uid through to software */ - ExpectIntEQ(wc_SHE_SetUID(&she, uid, sizeof(uid), NULL), 0); - ExpectIntEQ(XMEMCMP(she.uid, uid, WC_SHE_UID_SZ), 0); + /* Generate M1/M2/M3 via callback */ + ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she, + sheTestUid, sizeof(sheTestUid), + WC_SHE_MASTER_ECU_KEY_ID, sheTestAuthKey, sizeof(sheTestAuthKey), + 4, sheTestNewKey, sizeof(sheTestNewKey), 1, 0, + m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ), 0); + ExpectIntEQ(XMEMCMP(m1, sheTestExpM1, WC_SHE_M1_SZ), 0); - /* Set remaining inputs (software only) */ - ExpectIntEQ(wc_SHE_SetAuthKey(&she, WC_SHE_MASTER_ECU_KEY_ID, - authKey, sizeof(authKey)), 0); - ExpectIntEQ(wc_SHE_SetNewKey(&she, 4, newKey, sizeof(newKey)), 0); - ExpectIntEQ(wc_SHE_SetCounter(&she, 1), 0); - ExpectIntEQ(wc_SHE_SetFlags(&she, 0), 0); + /* Generate M4/M5 via callback */ + ExpectIntEQ(wc_SHE_GenerateM4M5(&she, + sheTestUid, sizeof(sheTestUid), + WC_SHE_MASTER_ECU_KEY_ID, 4, + sheTestNewKey, sizeof(sheTestNewKey), 1, + m4, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ), 0); + ExpectIntEQ(XMEMCMP(m4, sheTestExpM4, WC_SHE_M4_SZ), 0); + ExpectIntEQ(XMEMCMP(m5, sheTestExpM5, WC_SHE_M5_SZ), 0); - /* GenerateLoadKey — software, callback not involved */ - ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she), 0); - - /* GenerateM4M5 via callback — falls back to software */ - ExpectIntEQ(wc_SHE_GenerateM4M5(&she), 0); - ExpectIntEQ(she.verified, 1); - - /* ExportKey via callback path */ - ExpectIntEQ(wc_SHE_ExportKey(&she, - NULL, 0, NULL, 0, NULL, 0, - m4, WC_SHE_M4_SZ, - m5, WC_SHE_M5_SZ, NULL), 0); - - /* Export all M1-M5 via callback */ + /* ExportKey via callback — simulated hardware */ +#if !defined(NO_WC_SHE_EXPORTKEY) { - byte cm1[WC_SHE_M1_SZ]; - byte cm2[WC_SHE_M2_SZ]; - byte cm3[WC_SHE_M3_SZ]; + byte em1[WC_SHE_M1_SZ]; + byte em2[WC_SHE_M2_SZ]; + byte em3[WC_SHE_M3_SZ]; + byte em4[WC_SHE_M4_SZ]; + byte em5[WC_SHE_M5_SZ]; + byte pat[WC_SHE_M1_SZ]; + ExpectIntEQ(wc_SHE_ExportKey(&she, - cm1, WC_SHE_M1_SZ, - cm2, WC_SHE_M2_SZ, - cm3, WC_SHE_M3_SZ, - m4, WC_SHE_M4_SZ, - m5, WC_SHE_M5_SZ, NULL), 0); + em1, WC_SHE_M1_SZ, em2, WC_SHE_M2_SZ, + em3, WC_SHE_M3_SZ, em4, WC_SHE_M4_SZ, + em5, WC_SHE_M5_SZ, NULL), 0); + + /* Verify callback filled with test pattern */ + XMEMSET(pat, 0x11, WC_SHE_M1_SZ); + ExpectIntEQ(XMEMCMP(em1, pat, WC_SHE_M1_SZ), 0); + + /* Bad args */ + ExpectIntEQ(wc_SHE_ExportKey(NULL, + em1, WC_SHE_M1_SZ, em2, WC_SHE_M2_SZ, + em3, WC_SHE_M3_SZ, em4, WC_SHE_M4_SZ, + em5, WC_SHE_M5_SZ, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); } +#endif + +#if !defined(NO_WC_SHE_GETUID) + { + byte cbUid[WC_SHE_UID_SZ]; + ExpectIntEQ(wc_SHE_GetUID(&she, cbUid, WC_SHE_UID_SZ, NULL), 0); + ExpectIntEQ(wc_SHE_GetUID(NULL, cbUid, WC_SHE_UID_SZ, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_SHE_GetUID(&she, NULL, WC_SHE_UID_SZ, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + } +#endif + +#if !defined(NO_WC_SHE_GETCOUNTER) + { + word32 cnt1 = 0; + word32 cnt2 = 0; + + /* Callback should return incrementing counter */ + ExpectIntEQ(wc_SHE_GetCounter(&she, &cnt1, NULL), 0); + ExpectIntEQ(wc_SHE_GetCounter(&she, &cnt2, NULL), 0); + ExpectTrue(cnt2 > cnt1); + + /* Bad args */ + ExpectIntEQ(wc_SHE_GetCounter(NULL, &cnt1, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_SHE_GetCounter(&she, NULL, NULL), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + } +#endif wc_SHE_Free(&she); wc_CryptoCb_UnRegisterDevice(sheTestDevId); -#endif + return EXPECT_RESULT(); -} /* END test_wc_SHE_CryptoCb */ +} #endif /* WOLF_CRYPTO_CB && WOLFSSL_SHE && !NO_AES */ - diff --git a/tests/api/test_she.h b/tests/api/test_she.h index 61334aaaa3..79395d6e68 100644 --- a/tests/api/test_she.h +++ b/tests/api/test_she.h @@ -28,37 +28,35 @@ int test_wc_SHE_Init(void); int test_wc_SHE_Init_Id(void); int test_wc_SHE_Init_Label(void); int test_wc_SHE_Free(void); -int test_wc_SHE_SetUID(void); -int test_wc_SHE_SetAuthKey(void); -int test_wc_SHE_SetNewKey(void); -int test_wc_SHE_SetCounter(void); -int test_wc_SHE_SetFlags(void); +int test_wc_SHE_ImportM1M2M3(void); +int test_wc_She_AesMp16(void); +int test_wc_SHE_GenerateM1M2M3(void); +int test_wc_SHE_GenerateM4M5(void); +#ifdef WOLFSSL_SHE_EXTENDED int test_wc_SHE_SetKdfConstants(void); int test_wc_SHE_SetM2M4Header(void); -int test_wc_SHE_GenerateM1M2M3(void); -int test_wc_She_AesMp16(void); -int test_wc_SHE_GenerateM4M5(void); -int test_wc_SHE_ExportKey(void); +#endif #if defined(WOLF_CRYPTO_CB) && defined(WOLFSSL_SHE) int test_wc_SHE_CryptoCb(void); #endif -#define TEST_SHE_DECLS \ - TEST_DECL_GROUP("she", test_wc_SHE_Init), \ - TEST_DECL_GROUP("she", test_wc_SHE_Init_Id), \ - TEST_DECL_GROUP("she", test_wc_SHE_Init_Label), \ - TEST_DECL_GROUP("she", test_wc_SHE_Free), \ - TEST_DECL_GROUP("she", test_wc_SHE_SetUID), \ - TEST_DECL_GROUP("she", test_wc_SHE_SetAuthKey), \ - TEST_DECL_GROUP("she", test_wc_SHE_SetNewKey), \ - TEST_DECL_GROUP("she", test_wc_SHE_SetCounter), \ - TEST_DECL_GROUP("she", test_wc_SHE_SetFlags), \ - TEST_DECL_GROUP("she", test_wc_SHE_SetKdfConstants), \ - TEST_DECL_GROUP("she", test_wc_SHE_SetM2M4Header), \ - TEST_DECL_GROUP("she", test_wc_SHE_GenerateM1M2M3), \ - TEST_DECL_GROUP("she", test_wc_She_AesMp16), \ - TEST_DECL_GROUP("she", test_wc_SHE_GenerateM4M5), \ - TEST_DECL_GROUP("she", test_wc_SHE_ExportKey) +#define TEST_SHE_DECLS \ + TEST_DECL_GROUP("she", test_wc_SHE_Init), \ + TEST_DECL_GROUP("she", test_wc_SHE_Init_Id), \ + TEST_DECL_GROUP("she", test_wc_SHE_Init_Label), \ + TEST_DECL_GROUP("she", test_wc_SHE_Free), \ + TEST_DECL_GROUP("she", test_wc_SHE_ImportM1M2M3), \ + TEST_DECL_GROUP("she", test_wc_She_AesMp16), \ + TEST_DECL_GROUP("she", test_wc_SHE_GenerateM1M2M3), \ + TEST_DECL_GROUP("she", test_wc_SHE_GenerateM4M5) + +#ifdef WOLFSSL_SHE_EXTENDED +#define TEST_SHE_EXT_DECLS \ + TEST_DECL_GROUP("she", test_wc_SHE_SetKdfConstants), \ + TEST_DECL_GROUP("she", test_wc_SHE_SetM2M4Header) +#else +#define TEST_SHE_EXT_DECLS +#endif #if defined(WOLF_CRYPTO_CB) && defined(WOLFSSL_SHE) #define TEST_SHE_CB_DECLS \ diff --git a/wolfcrypt/src/cryptocb.c b/wolfcrypt/src/cryptocb.c index fab013b758..e771f09a5c 100644 --- a/wolfcrypt/src/cryptocb.c +++ b/wolfcrypt/src/cryptocb.c @@ -2149,12 +2149,12 @@ int wc_CryptoCb_SheSetUid(wc_SHE* she, const byte* uid, word32 uidSz, return wc_CryptoCb_TranslateErrorCode(ret); } -int wc_CryptoCb_SheGenerateM1M2M3(wc_SHE* she, const void* ctx) +int wc_CryptoCb_SheGetCounter(wc_SHE* she, word32* counter, const void* ctx) { int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); CryptoCb* dev; - if (she == NULL) { + if (she == NULL || counter == NULL) { return BAD_FUNC_ARG; } @@ -2162,10 +2162,11 @@ int wc_CryptoCb_SheGenerateM1M2M3(wc_SHE* she, const void* ctx) if (dev && dev->cb) { wc_CryptoInfo cryptoInfo; XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); - cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; - cryptoInfo.she.she = she; - cryptoInfo.she.type = WC_SHE_GENERATE_M1M2M3; - cryptoInfo.she.ctx = ctx; + cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; + cryptoInfo.she.she = she; + cryptoInfo.she.type = WC_SHE_GET_COUNTER; + cryptoInfo.she.ctx = ctx; + cryptoInfo.she.op.getCounter.counter = counter; ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); } @@ -2173,7 +2174,14 @@ int wc_CryptoCb_SheGenerateM1M2M3(wc_SHE* she, const void* ctx) return wc_CryptoCb_TranslateErrorCode(ret); } -int wc_CryptoCb_SheGenerateM4M5(wc_SHE* she, const void* ctx) +int wc_CryptoCb_SheGenerateM1M2M3(wc_SHE* she, + const byte* uid, word32 uidSz, + byte authKeyId, const byte* authKey, word32 authKeySz, + byte targetKeyId, const byte* newKey, word32 newKeySz, + word32 counter, byte flags, + byte* m1, word32 m1Sz, + byte* m2, word32 m2Sz, + byte* m3, word32 m3Sz) { int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); CryptoCb* dev; @@ -2186,10 +2194,65 @@ int wc_CryptoCb_SheGenerateM4M5(wc_SHE* she, const void* ctx) if (dev && dev->cb) { wc_CryptoInfo cryptoInfo; XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); - cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; - cryptoInfo.she.she = she; - cryptoInfo.she.type = WC_SHE_GENERATE_M4M5; - cryptoInfo.she.ctx = ctx; + cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; + cryptoInfo.she.she = she; + cryptoInfo.she.type = WC_SHE_GENERATE_M1M2M3; + cryptoInfo.she.op.generateM1M2M3.uid = uid; + cryptoInfo.she.op.generateM1M2M3.uidSz = uidSz; + cryptoInfo.she.op.generateM1M2M3.authKeyId = authKeyId; + cryptoInfo.she.op.generateM1M2M3.authKey = authKey; + cryptoInfo.she.op.generateM1M2M3.authKeySz = authKeySz; + cryptoInfo.she.op.generateM1M2M3.targetKeyId = targetKeyId; + cryptoInfo.she.op.generateM1M2M3.newKey = newKey; + cryptoInfo.she.op.generateM1M2M3.newKeySz = newKeySz; + cryptoInfo.she.op.generateM1M2M3.counter = counter; + cryptoInfo.she.op.generateM1M2M3.flags = flags; + cryptoInfo.she.op.generateM1M2M3.m1 = m1; + cryptoInfo.she.op.generateM1M2M3.m1Sz = m1Sz; + cryptoInfo.she.op.generateM1M2M3.m2 = m2; + cryptoInfo.she.op.generateM1M2M3.m2Sz = m2Sz; + cryptoInfo.she.op.generateM1M2M3.m3 = m3; + cryptoInfo.she.op.generateM1M2M3.m3Sz = m3Sz; + + ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); + } + + return wc_CryptoCb_TranslateErrorCode(ret); +} + +int wc_CryptoCb_SheGenerateM4M5(wc_SHE* she, + const byte* uid, word32 uidSz, + byte authKeyId, byte targetKeyId, + const byte* newKey, word32 newKeySz, + word32 counter, + byte* m4, word32 m4Sz, + byte* m5, word32 m5Sz) +{ + int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); + CryptoCb* dev; + + if (she == NULL) { + return BAD_FUNC_ARG; + } + + dev = wc_CryptoCb_FindDevice(she->devId, WC_ALGO_TYPE_SHE); + if (dev && dev->cb) { + wc_CryptoInfo cryptoInfo; + XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); + cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; + cryptoInfo.she.she = she; + cryptoInfo.she.type = WC_SHE_GENERATE_M4M5; + cryptoInfo.she.op.generateM4M5.uid = uid; + cryptoInfo.she.op.generateM4M5.uidSz = uidSz; + cryptoInfo.she.op.generateM4M5.authKeyId = authKeyId; + cryptoInfo.she.op.generateM4M5.targetKeyId = targetKeyId; + cryptoInfo.she.op.generateM4M5.newKey = newKey; + cryptoInfo.she.op.generateM4M5.newKeySz = newKeySz; + cryptoInfo.she.op.generateM4M5.counter = counter; + cryptoInfo.she.op.generateM4M5.m4 = m4; + cryptoInfo.she.op.generateM4M5.m4Sz = m4Sz; + cryptoInfo.she.op.generateM4M5.m5 = m5; + cryptoInfo.she.op.generateM4M5.m5Sz = m5Sz; ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); } diff --git a/wolfcrypt/src/she.c b/wolfcrypt/src/she.c index 06c4f1b44a..2539efa7f4 100644 --- a/wolfcrypt/src/she.c +++ b/wolfcrypt/src/she.c @@ -116,19 +116,14 @@ int wc_She_AesMp16(Aes* aes, const byte* in, word32 inSz, byte* out) /* -------------------------------------------------------------------------- */ int wc_SHE_Init(wc_SHE* she, void* heap, int devId) { - const byte encC[] = WC_SHE_KEY_UPDATE_ENC_C; - const byte macC[] = WC_SHE_KEY_UPDATE_MAC_C; - if (she == NULL) { return BAD_FUNC_ARG; } - XMEMSET(she, 0, sizeof(wc_SHE)); + ForceZero(she, sizeof(wc_SHE)); she->heap = heap; she->devId = devId; - XMEMCPY(she->kdfEncC, encC, WC_SHE_KEY_SZ); - XMEMCPY(she->kdfMacC, macC, WC_SHE_KEY_SZ); - /* m2pHeader/m4pHeader are zero from XMEMSET ΓÇö correct for counter=0 */ + /* kdfEncOverride/kdfMacOverride are zero from XMEMSET — defaults used */ return 0; } @@ -227,85 +222,45 @@ void wc_SHE_Free(wc_SHE* she) } /* -------------------------------------------------------------------------- */ -/* Setter functions */ +/* GetUID — callback required */ +/* */ +/* Dispatches to callback to fetch UID from hardware. */ +/* Buffer size validation is the callback's responsibility. */ +/* Returns CRYPTOCB_UNAVAILABLE if no callback. */ /* -------------------------------------------------------------------------- */ - -int wc_SHE_SetUID(wc_SHE* she, const byte* uid, word32 uidSz, +#if defined(WOLF_CRYPTO_CB) && !defined(NO_WC_SHE_GETUID) +int wc_SHE_GetUID(wc_SHE* she, byte* uid, word32 uidSz, const void* ctx) { -#ifdef WOLF_CRYPTO_CB - int ret; -#endif - - if (she == NULL) { + if (she == NULL || uid == NULL) { return BAD_FUNC_ARG; } -#ifdef WOLF_CRYPTO_CB - /* Try callback first if a device is registered */ - if (she->devId != INVALID_DEVID) { - ret = wc_CryptoCb_SheSetUid(she, uid, uidSz, ctx); - if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { - return ret; - } - /* fall-through to software path */ - } -#else - (void)ctx; -#endif - - /* Software path: copy caller-provided UID */ - if (uid == NULL || uidSz != WC_SHE_UID_SZ) { - return BAD_FUNC_ARG; - } - - XMEMCPY(she->uid, uid, WC_SHE_UID_SZ); - return 0; + return wc_CryptoCb_SheSetUid(she, uid, uidSz, ctx); } +#endif /* WOLF_CRYPTO_CB && !NO_WC_SHE_GETUID */ -int wc_SHE_SetAuthKey(wc_SHE* she, byte authKeyId, - const byte* authKey, word32 keySz) +/* -------------------------------------------------------------------------- */ +/* GetCounter — callback required */ +/* */ +/* Dispatches to callback to read current counter from hardware. */ +/* Returns CRYPTOCB_UNAVAILABLE if no callback. */ +/* -------------------------------------------------------------------------- */ +#if defined(WOLF_CRYPTO_CB) && !defined(NO_WC_SHE_GETCOUNTER) +int wc_SHE_GetCounter(wc_SHE* she, word32* counter, const void* ctx) { - if (she == NULL || authKey == NULL || keySz != WC_SHE_KEY_SZ) { + if (she == NULL || counter == NULL) { return BAD_FUNC_ARG; } - she->authKeyId = authKeyId; - XMEMCPY(she->authKey, authKey, WC_SHE_KEY_SZ); - return 0; + return wc_CryptoCb_SheGetCounter(she, counter, ctx); } +#endif /* WOLF_CRYPTO_CB && !NO_WC_SHE_GETCOUNTER */ -int wc_SHE_SetNewKey(wc_SHE* she, byte targetKeyId, - const byte* newKey, word32 keySz) -{ - if (she == NULL || newKey == NULL || keySz != WC_SHE_KEY_SZ) { - return BAD_FUNC_ARG; - } - - she->targetKeyId = targetKeyId; - XMEMCPY(she->newKey, newKey, WC_SHE_KEY_SZ); - return 0; -} - -int wc_SHE_SetCounter(wc_SHE* she, word32 counter) -{ - if (she == NULL) { - return BAD_FUNC_ARG; - } - - she->counter = counter; - return 0; -} - -int wc_SHE_SetFlags(wc_SHE* she, byte flags) -{ - if (she == NULL) { - return BAD_FUNC_ARG; - } - - she->flags = flags; - return 0; -} +/* -------------------------------------------------------------------------- */ +/* Extended SHE overrides */ +/* -------------------------------------------------------------------------- */ +#ifdef WOLFSSL_SHE_EXTENDED int wc_SHE_SetKdfConstants(wc_SHE* she, const byte* encC, word32 encCSz, @@ -320,6 +275,7 @@ int wc_SHE_SetKdfConstants(wc_SHE* she, return BAD_FUNC_ARG; } XMEMCPY(she->kdfEncC, encC, WC_SHE_KEY_SZ); + she->kdfEncOverride = 1; } if (macC != NULL) { @@ -327,11 +283,44 @@ int wc_SHE_SetKdfConstants(wc_SHE* she, return BAD_FUNC_ARG; } XMEMCPY(she->kdfMacC, macC, WC_SHE_KEY_SZ); + she->kdfMacOverride = 1; } return 0; } +#endif /* WOLFSSL_SHE_EXTENDED */ + +/* -------------------------------------------------------------------------- */ +/* GetUID */ + +#if defined(WOLF_CRYPTO_CB) || !defined(NO_WC_SHE_IMPORT_M123) +/* -------------------------------------------------------------------------- */ +/* Import M1/M2/M3 */ +/* */ +/* Copy externally-provided M1/M2/M3 into context and set generated flag. */ +/* -------------------------------------------------------------------------- */ +int wc_SHE_ImportM1M2M3(wc_SHE* she, + const byte* m1, word32 m1Sz, + const byte* m2, word32 m2Sz, + const byte* m3, word32 m3Sz) +{ + if (she == NULL || m1 == NULL || m2 == NULL || m3 == NULL) { + return BAD_FUNC_ARG; + } + if (m1Sz != WC_SHE_M1_SZ || m2Sz != WC_SHE_M2_SZ || + m3Sz != WC_SHE_M3_SZ) { + return BAD_FUNC_ARG; + } + + XMEMCPY(she->m1, m1, WC_SHE_M1_SZ); + XMEMCPY(she->m2, m2, WC_SHE_M2_SZ); + XMEMCPY(she->m3, m3, WC_SHE_M3_SZ); + she->generated = 1; + return 0; +} +#endif /* WOLF_CRYPTO_CB || !NO_WC_SHE_IMPORT_M123 */ + /* -------------------------------------------------------------------------- */ /* Portable big-endian 32-bit store */ /* -------------------------------------------------------------------------- */ @@ -343,28 +332,45 @@ static WC_INLINE void she_store_be32(byte* dst, word32 val) dst[3] = (byte)(val); } -/* Build M2P/M4P headers from counter and flags using standard SHE packing. +/* Build M2P and M4P headers from counter and flags using standard SHE packing. * M2P header: counter(28b) | flags(4b) | zeros(96b) = 16 bytes * M4P header: counter(28b) | 1(1b) | zeros(99b) = 16 bytes - * Called internally by GenerateM1M2M3/GenerateM4M5 unless overridden. */ -static void she_build_headers(wc_SHE* she) + * Writes to caller-provided buffers. Skipped if WOLFSSL_SHE_EXTENDED + * override is active on the context. */ +static void she_build_headers(wc_SHE* she, word32 counter, byte flags, + byte* m2pHeader, byte* m4pHeader) { word32 field; - if (!she->m2pOverride) { - XMEMSET(she->m2pHeader, 0, WC_SHE_KEY_SZ); - field = (she->counter << WC_SHE_M2_COUNT_SHIFT) | - (she->flags << WC_SHE_M2_FLAGS_SHIFT); - she_store_be32(she->m2pHeader, field); +#ifdef WOLFSSL_SHE_EXTENDED + if (she->m2pOverride) { + XMEMCPY(m2pHeader, she->m2pHeader, WC_SHE_KEY_SZ); + } + else +#endif + { + XMEMSET(m2pHeader, 0, WC_SHE_KEY_SZ); + field = (counter << WC_SHE_M2_COUNT_SHIFT) | + (flags << WC_SHE_M2_FLAGS_SHIFT); + she_store_be32(m2pHeader, field); } - if (!she->m4pOverride) { - XMEMSET(she->m4pHeader, 0, WC_SHE_KEY_SZ); - field = (she->counter << WC_SHE_M4_COUNT_SHIFT) | WC_SHE_M4_COUNT_PAD; - she_store_be32(she->m4pHeader, field); +#ifdef WOLFSSL_SHE_EXTENDED + if (she->m4pOverride) { + XMEMCPY(m4pHeader, she->m4pHeader, WC_SHE_KEY_SZ); } + else +#endif + { + XMEMSET(m4pHeader, 0, WC_SHE_KEY_SZ); + field = (counter << WC_SHE_M4_COUNT_SHIFT) | WC_SHE_M4_COUNT_PAD; + she_store_be32(m4pHeader, field); + } + + (void)she; } +#ifdef WOLFSSL_SHE_EXTENDED int wc_SHE_SetM2Header(wc_SHE* she, const byte* header, word32 headerSz) { if (she == NULL || header == NULL || headerSz != WC_SHE_KEY_SZ) { @@ -386,6 +392,7 @@ int wc_SHE_SetM4Header(wc_SHE* she, const byte* header, word32 headerSz) she->m4pOverride = 1; return 0; } +#endif /* WOLFSSL_SHE_EXTENDED */ /* -------------------------------------------------------------------------- */ /* M1/M2/M3 generation */ @@ -397,31 +404,43 @@ int wc_SHE_SetM4Header(wc_SHE* she, const byte* header, word32 headerSz) /* */ /* Ported from wolfHSM wh_She_GenerateLoadableKey() in wh_she_crypto.c. */ /* -------------------------------------------------------------------------- */ -int wc_SHE_GenerateM1M2M3(wc_SHE* she) +int wc_SHE_GenerateM1M2M3(wc_SHE* she, + const byte* uid, word32 uidSz, + byte authKeyId, const byte* authKey, word32 authKeySz, + byte targetKeyId, const byte* newKey, word32 newKeySz, + word32 counter, byte flags, + byte* m1, word32 m1Sz, + byte* m2, word32 m2Sz, + byte* m3, word32 m3Sz) { int ret = 0; + byte m2pHeader[WC_SHE_KEY_SZ]; + byte m4pHeader[WC_SHE_KEY_SZ]; byte k1[WC_SHE_KEY_SZ]; byte k2[WC_SHE_KEY_SZ]; byte kdfInput[WC_SHE_KEY_SZ * 2]; + byte encC[] = WC_SHE_KEY_UPDATE_ENC_C; + byte macC[] = WC_SHE_KEY_UPDATE_MAC_C; word32 cmacSz = AES_BLOCK_SIZE; WC_DECLARE_VAR(aes, Aes, 1, 0); WC_DECLARE_VAR(cmac, Cmac, 1, 0); + /* Validate SHE context first — required for both callback and software */ if (she == NULL) { return BAD_FUNC_ARG; } - /* Build M2P/M4P headers from counter/flags (skipped if overridden) */ - she_build_headers(she); - #ifdef WOLF_CRYPTO_CB - /* Try callback first ΓÇö hardware may generate M1/M2/M3 directly */ + /* Try callback first — callback handles its own parameter validation. + * This allows callers to pass NULL authKey/newKey when a secure element + * holds the keys and the callback talks to it directly. */ if (she->devId != INVALID_DEVID) { - ret = wc_CryptoCb_SheGenerateM1M2M3(she, NULL); + ret = wc_CryptoCb_SheGenerateM1M2M3(she, uid, uidSz, + authKeyId, authKey, authKeySz, + targetKeyId, newKey, newKeySz, + counter, flags, + m1, m1Sz, m2, m2Sz, m3, m3Sz); if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { - if (ret == 0) { - she->generated = 1; - } return ret; } /* fall-through to software path */ @@ -429,6 +448,29 @@ int wc_SHE_GenerateM1M2M3(wc_SHE* she) } #endif + /* Software path — validate all parameters */ + if (uid == NULL || uidSz != WC_SHE_UID_SZ || + authKey == NULL || authKeySz != WC_SHE_KEY_SZ || + newKey == NULL || newKeySz != WC_SHE_KEY_SZ || + m1 == NULL || m1Sz < WC_SHE_M1_SZ || + m2 == NULL || m2Sz < WC_SHE_M2_SZ || + m3 == NULL || m3Sz < WC_SHE_M3_SZ) { + return BAD_FUNC_ARG; + } + + /* Override KDF constants if explicitly set */ +#ifdef WOLFSSL_SHE_EXTENDED + if (she->kdfEncOverride) { + XMEMCPY(encC, she->kdfEncC, WC_SHE_KEY_SZ); + } + if (she->kdfMacOverride) { + XMEMCPY(macC, she->kdfMacC, WC_SHE_KEY_SZ); + } +#endif + + /* Build M2P/M4P headers from counter/flags (skipped if overridden) */ + she_build_headers(she, counter, flags, m2pHeader, m4pHeader); + WC_ALLOC_VAR(aes, Aes, 1, she->heap); if (!WC_VAR_OK(aes)) { return MEMORY_E; @@ -449,34 +491,34 @@ int wc_SHE_GenerateM1M2M3(wc_SHE* she) } /* ---- Derive K1 = AES-MP(AuthKey || CENC) ---- */ - XMEMCPY(kdfInput, she->authKey, WC_SHE_KEY_SZ); - XMEMCPY(kdfInput + WC_SHE_KEY_SZ, she->kdfEncC, WC_SHE_KEY_SZ); + XMEMCPY(kdfInput, authKey, WC_SHE_KEY_SZ); + XMEMCPY(kdfInput + WC_SHE_KEY_SZ, encC, WC_SHE_KEY_SZ); ret = wc_She_AesMp16(aes, kdfInput, WC_SHE_KEY_SZ * 2, k1); /* ---- Build M1: UID(15B) | TargetKeyID(4b) | AuthKeyID(4b) ---- */ if (ret == 0) { - XMEMCPY(she->m1, she->uid, WC_SHE_UID_SZ); - she->m1[WC_SHE_M1_KID_OFFSET] = - (byte)((she->targetKeyId << WC_SHE_M1_KID_SHIFT) | - (she->authKeyId << WC_SHE_M1_AID_SHIFT)); + XMEMCPY(m1, uid, WC_SHE_UID_SZ); + m1[WC_SHE_M1_KID_OFFSET] = + (byte)((targetKeyId << WC_SHE_M1_KID_SHIFT) | + (authKeyId << WC_SHE_M1_AID_SHIFT)); } /* ---- Build cleartext M2 and encrypt with K1 ---- */ if (ret == 0) { /* M2P = m2pHeader(16B) | newKey(16B) */ - XMEMCPY(she->m2, she->m2pHeader, WC_SHE_KEY_SZ); - XMEMCPY(she->m2 + WC_SHE_M2_KEY_OFFSET, she->newKey, WC_SHE_KEY_SZ); + XMEMCPY(m2, m2pHeader, WC_SHE_KEY_SZ); + XMEMCPY(m2 + WC_SHE_M2_KEY_OFFSET, newKey, WC_SHE_KEY_SZ); /* Encrypt M2 in-place with AES-128-CBC, IV = 0 */ ret = wc_AesSetKey(aes, k1, WC_SHE_KEY_SZ, NULL, AES_ENCRYPTION); if (ret == 0) { - ret = wc_AesCbcEncrypt(aes, she->m2, she->m2, WC_SHE_M2_SZ); + ret = wc_AesCbcEncrypt(aes, m2, m2, WC_SHE_M2_SZ); } } - /* ---- Derive K2 = AES-MP(AuthKey || CMAC) ---- */ + /* ---- Derive K2 = AES-MP(AuthKey || CMAC_C) ---- */ if (ret == 0) { - XMEMCPY(kdfInput + WC_SHE_KEY_SZ, she->kdfMacC, WC_SHE_KEY_SZ); + XMEMCPY(kdfInput + WC_SHE_KEY_SZ, macC, WC_SHE_KEY_SZ); ret = wc_She_AesMp16(aes, kdfInput, WC_SHE_KEY_SZ * 2, k2); } @@ -486,18 +528,14 @@ int wc_SHE_GenerateM1M2M3(wc_SHE* she) NULL, she->heap, she->devId); } if (ret == 0) { - ret = wc_CmacUpdate(cmac, she->m1, WC_SHE_M1_SZ); + ret = wc_CmacUpdate(cmac, m1, WC_SHE_M1_SZ); } if (ret == 0) { - ret = wc_CmacUpdate(cmac, she->m2, WC_SHE_M2_SZ); + ret = wc_CmacUpdate(cmac, m2, WC_SHE_M2_SZ); } if (ret == 0) { cmacSz = AES_BLOCK_SIZE; - ret = wc_CmacFinal(cmac, she->m3, &cmacSz); - } - - if (ret == 0) { - she->generated = 1; + ret = wc_CmacFinal(cmac, m3, &cmacSz); } /* Scrub temporary key material */ @@ -520,37 +558,68 @@ int wc_SHE_GenerateM1M2M3(wc_SHE* she) /* */ /* These are the expected proof messages that SHE hardware should return. */ /* -------------------------------------------------------------------------- */ -int wc_SHE_GenerateM4M5(wc_SHE* she) +int wc_SHE_GenerateM4M5(wc_SHE* she, + const byte* uid, word32 uidSz, + byte authKeyId, byte targetKeyId, + const byte* newKey, word32 newKeySz, + word32 counter, + byte* m4, word32 m4Sz, + byte* m5, word32 m5Sz) { int ret = 0; + byte m2pHeader[WC_SHE_KEY_SZ]; + byte m4pHeader[WC_SHE_KEY_SZ]; byte k3[WC_SHE_KEY_SZ]; byte k4[WC_SHE_KEY_SZ]; byte kdfInput[WC_SHE_KEY_SZ * 2]; + byte encC[] = WC_SHE_KEY_UPDATE_ENC_C; + byte macC[] = WC_SHE_KEY_UPDATE_MAC_C; word32 cmacSz; WC_DECLARE_VAR(aes, Aes, 1, 0); WC_DECLARE_VAR(cmac, Cmac, 1, 0); + /* Validate SHE context first */ if (she == NULL) { return BAD_FUNC_ARG; } - if (!she->generated) { - return BAD_STATE_E; - } #ifdef WOLF_CRYPTO_CB - /* Try callback first — sends M1/M2/M3 to HW, receives M4/M5 */ + /* Try callback first — useful for uploading M1/M2/M3 to an HSM which + * loads the key and returns the correct M4/M5 proof values. The callback + * handles its own parameter validation. */ if (she->devId != INVALID_DEVID) { - ret = wc_CryptoCb_SheGenerateM4M5(she, NULL); + ret = wc_CryptoCb_SheGenerateM4M5(she, uid, uidSz, + authKeyId, targetKeyId, + newKey, newKeySz, counter, + m4, m4Sz, m5, m5Sz); if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { - if (ret == 0) { - she->verified = 1; - } return ret; } /* fall-through to software path */ } #endif + /* Software path — validate all parameters */ + if (uid == NULL || uidSz != WC_SHE_UID_SZ || + newKey == NULL || newKeySz != WC_SHE_KEY_SZ || + m4 == NULL || m4Sz < WC_SHE_M4_SZ || + m5 == NULL || m5Sz < WC_SHE_M5_SZ) { + return BAD_FUNC_ARG; + } + + /* Override KDF constants if explicitly set */ +#ifdef WOLFSSL_SHE_EXTENDED + if (she->kdfEncOverride) { + XMEMCPY(encC, she->kdfEncC, WC_SHE_KEY_SZ); + } + if (she->kdfMacOverride) { + XMEMCPY(macC, she->kdfMacC, WC_SHE_KEY_SZ); + } +#endif + + /* Build headers from counter (skipped if overridden) */ + she_build_headers(she, counter, 0, m2pHeader, m4pHeader); + WC_ALLOC_VAR(aes, Aes, 1, she->heap); if (!WC_VAR_OK(aes)) { return MEMORY_E; @@ -571,50 +640,46 @@ int wc_SHE_GenerateM4M5(wc_SHE* she) } /* ---- Derive K3 = AES-MP(NewKey || CENC) ---- */ - XMEMCPY(kdfInput, she->newKey, WC_SHE_KEY_SZ); - XMEMCPY(kdfInput + WC_SHE_KEY_SZ, she->kdfEncC, WC_SHE_KEY_SZ); + XMEMCPY(kdfInput, newKey, WC_SHE_KEY_SZ); + XMEMCPY(kdfInput + WC_SHE_KEY_SZ, encC, WC_SHE_KEY_SZ); ret = wc_She_AesMp16(aes, kdfInput, WC_SHE_KEY_SZ * 2, k3); /* ---- Build M4: UID|IDs header + AES-ECB(K3, m4pHeader) ---- */ if (ret == 0) { - XMEMSET(she->m4, 0, WC_SHE_M4_SZ); + XMEMSET(m4, 0, WC_SHE_M4_SZ); - XMEMCPY(she->m4, she->uid, WC_SHE_UID_SZ); - she->m4[WC_SHE_M4_KID_OFFSET] = - (byte)((she->targetKeyId << WC_SHE_M4_KID_SHIFT) | - (she->authKeyId << WC_SHE_M4_AID_SHIFT)); + XMEMCPY(m4, uid, WC_SHE_UID_SZ); + m4[WC_SHE_M4_KID_OFFSET] = + (byte)((targetKeyId << WC_SHE_M4_KID_SHIFT) | + (authKeyId << WC_SHE_M4_AID_SHIFT)); /* Copy pre-built M4P header (counter|pad) into M4 counter block */ - XMEMCPY(she->m4 + WC_SHE_M4_COUNT_OFFSET, she->m4pHeader, + XMEMCPY(m4 + WC_SHE_M4_COUNT_OFFSET, m4pHeader, WC_SHE_KEY_SZ); /* Encrypt the 16-byte counter block in-place with AES-ECB */ ret = wc_AesSetKey(aes, k3, WC_SHE_KEY_SZ, NULL, AES_ENCRYPTION); if (ret == 0) { ret = wc_AesEncryptDirect(aes, - she->m4 + WC_SHE_M4_COUNT_OFFSET, - she->m4 + WC_SHE_M4_COUNT_OFFSET); + m4 + WC_SHE_M4_COUNT_OFFSET, + m4 + WC_SHE_M4_COUNT_OFFSET); } } - /* ---- Derive K4 = AES-MP(NewKey || CMAC) ---- */ + /* ---- Derive K4 = AES-MP(NewKey || CMAC_C) ---- */ if (ret == 0) { - XMEMCPY(kdfInput + WC_SHE_KEY_SZ, she->kdfMacC, WC_SHE_KEY_SZ); + XMEMCPY(kdfInput + WC_SHE_KEY_SZ, macC, WC_SHE_KEY_SZ); ret = wc_She_AesMp16(aes, kdfInput, WC_SHE_KEY_SZ * 2, k4); } /* ---- Build M5 = AES-CMAC(K4, M4) ---- */ if (ret == 0) { cmacSz = AES_BLOCK_SIZE; - ret = wc_AesCmacGenerate_ex(cmac, she->m5, &cmacSz, - she->m4, WC_SHE_M4_SZ, k4, WC_SHE_KEY_SZ, + ret = wc_AesCmacGenerate_ex(cmac, m5, &cmacSz, + m4, WC_SHE_M4_SZ, k4, WC_SHE_KEY_SZ, she->heap, she->devId); } - if (ret == 0) { - she->verified = 1; - } - ForceZero(k3, sizeof(k3)); ForceZero(k4, sizeof(k4)); ForceZero(kdfInput, sizeof(kdfInput)); @@ -633,6 +698,12 @@ int wc_SHE_GenerateM4M5(wc_SHE* she) /* M1/M2/M3 require generated state, M4/M5 require verified state. */ /* Callback: asks hardware to export the key as M1-M5. */ /* -------------------------------------------------------------------------- */ +#if defined(WOLF_CRYPTO_CB) && !defined(NO_WC_SHE_EXPORTKEY) +/* -------------------------------------------------------------------------- */ +/* Export Key — callback required */ +/* */ +/* Asks hardware to export a key slot as M1-M5 in SHE loadable format. */ +/* -------------------------------------------------------------------------- */ int wc_SHE_ExportKey(wc_SHE* she, byte* m1, word32 m1Sz, byte* m2, word32 m2Sz, @@ -645,58 +716,10 @@ int wc_SHE_ExportKey(wc_SHE* she, return BAD_FUNC_ARG; } - /* Verify buffer sizes for any non-NULL pointers */ - if ((m1 != NULL && m1Sz < WC_SHE_M1_SZ) || - (m2 != NULL && m2Sz < WC_SHE_M2_SZ) || - (m3 != NULL && m3Sz < WC_SHE_M3_SZ) || - (m4 != NULL && m4Sz < WC_SHE_M4_SZ) || - (m5 != NULL && m5Sz < WC_SHE_M5_SZ)) { - return BUFFER_E; - } - -#ifdef WOLF_CRYPTO_CB - if (she->devId != INVALID_DEVID) { - int ret = wc_CryptoCb_SheExportKey(she, - m1, m1Sz, m2, m2Sz, m3, m3Sz, - m4, m4Sz, m5, m5Sz, ctx); - if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { - return ret; - } - /* fall-through to software path */ - } -#endif - (void)ctx; - - /* Export M1/M2/M3 if requested */ - if (m1 != NULL || m2 != NULL || m3 != NULL) { - if (!she->generated) { - return BAD_STATE_E; - } - if (m1 != NULL) { - XMEMCPY(m1, she->m1, WC_SHE_M1_SZ); - } - if (m2 != NULL) { - XMEMCPY(m2, she->m2, WC_SHE_M2_SZ); - } - if (m3 != NULL) { - XMEMCPY(m3, she->m3, WC_SHE_M3_SZ); - } - } - - /* Export M4/M5 if requested */ - if (m4 != NULL || m5 != NULL) { - if (!she->verified) { - return BAD_STATE_E; - } - if (m4 != NULL) { - XMEMCPY(m4, she->m4, WC_SHE_M4_SZ); - } - if (m5 != NULL) { - XMEMCPY(m5, she->m5, WC_SHE_M5_SZ); - } - } - - return 0; + return wc_CryptoCb_SheExportKey(she, + m1, m1Sz, m2, m2Sz, m3, m3Sz, + m4, m4Sz, m5, m5Sz, ctx); } +#endif /* WOLF_CRYPTO_CB && !NO_WC_SHE_EXPORTKEY */ #endif /* WOLFSSL_SHE */ diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 3009628df8..a0762a4614 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -56625,6 +56625,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t she_test(void) byte m4[WC_SHE_M4_SZ]; byte m5[WC_SHE_M5_SZ]; WC_DECLARE_VAR(she, wc_SHE, 1, HEAP_HINT); + WC_DECLARE_VAR(she2, wc_SHE, 1, HEAP_HINT); /* SHE specification test vector (from wolfHSM wh_test_she.c) */ WOLFSSL_SMALL_STACK_STATIC const byte sheUid[] = { @@ -56678,47 +56679,15 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t she_test(void) return WC_TEST_RET_ENC_EC(ret); } - /* ---- Set inputs from test vector ---- */ - ret = wc_SHE_SetUID(she, sheUid, sizeof(sheUid), NULL); - if (ret != 0) { - goto exit_SHE_Test; - } - - ret = wc_SHE_SetAuthKey(she, WC_SHE_MASTER_ECU_KEY_ID, - vectorAuthKey, sizeof(vectorAuthKey)); - if (ret != 0) { - goto exit_SHE_Test; - } - - ret = wc_SHE_SetNewKey(she, 4, vectorNewKey, sizeof(vectorNewKey)); - if (ret != 0) { - goto exit_SHE_Test; - } - - ret = wc_SHE_SetCounter(she, 1); - if (ret != 0) { - goto exit_SHE_Test; - } - - ret = wc_SHE_SetFlags(she, 0); - if (ret != 0) { - goto exit_SHE_Test; - } - - /* ---- Generate M1/M2/M3 ---- */ - ret = wc_SHE_GenerateM1M2M3(she); - if (ret != 0) { - goto exit_SHE_Test; - } - - /* ---- Export M1/M2/M3 ---- */ - ret = wc_SHE_ExportKey(she, - m1, WC_SHE_M1_SZ, - m2, WC_SHE_M2_SZ, - m3, WC_SHE_M3_SZ, - NULL, 0, - NULL, 0, - NULL); + /* ---- Generate M1/M2/M3 from test vector inputs ---- */ + ret = wc_SHE_GenerateM1M2M3(she, + sheUid, sizeof(sheUid), + WC_SHE_MASTER_ECU_KEY_ID, vectorAuthKey, sizeof(vectorAuthKey), + 4, vectorNewKey, sizeof(vectorNewKey), + 1, 0, + m1, WC_SHE_M1_SZ, + m2, WC_SHE_M2_SZ, + m3, WC_SHE_M3_SZ); if (ret != 0) { goto exit_SHE_Test; } @@ -56741,20 +56710,19 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t she_test(void) goto exit_SHE_Test; } - /* ---- Compute M4/M5 ---- */ - ret = wc_SHE_GenerateM4M5(she); + /* ---- Compute and export M4/M5 ---- */ + wc_SHE_Free(she); + ret = wc_SHE_Init(she, HEAP_HINT, devId); if (ret != 0) { goto exit_SHE_Test; } - - /* ---- Export M4/M5 ---- */ - ret = wc_SHE_ExportKey(she, - NULL, 0, - NULL, 0, - NULL, 0, - m4, WC_SHE_M4_SZ, - m5, WC_SHE_M5_SZ, - NULL); + ret = wc_SHE_GenerateM4M5(she, + sheUid, sizeof(sheUid), + WC_SHE_MASTER_ECU_KEY_ID, 4, + vectorNewKey, sizeof(vectorNewKey), + 1, + m4, WC_SHE_M4_SZ, + m5, WC_SHE_M5_SZ); if (ret != 0) { goto exit_SHE_Test; } @@ -56771,9 +56739,160 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t she_test(void) goto exit_SHE_Test; } + /* ---- Import M1/M2/M3 and generate M4/M5 (only NewKey needed) ---- */ + wc_SHE_Free(she); + ret = wc_SHE_Init(she, HEAP_HINT, devId); + if (ret != 0) { + goto exit_SHE_Test; + } + + /* Import M1/M2/M3, then generate M4/M5 via one-shot */ + ret = wc_SHE_ImportM1M2M3(she, + expM1, WC_SHE_M1_SZ, + expM2, WC_SHE_M2_SZ, + expM3, WC_SHE_M3_SZ); + if (ret != 0) { + goto exit_SHE_Test; + } + ret = wc_SHE_GenerateM4M5(she, + sheUid, sizeof(sheUid), + WC_SHE_MASTER_ECU_KEY_ID, 4, + vectorNewKey, sizeof(vectorNewKey), + 1, + m4, WC_SHE_M4_SZ, + m5, WC_SHE_M5_SZ); + if (ret != 0) { + goto exit_SHE_Test; + } + + if (XMEMCMP(m4, expM4, WC_SHE_M4_SZ) != 0) { + ret = WC_TEST_RET_ENC_NC; + goto exit_SHE_Test; + } + if (XMEMCMP(m5, expM5, WC_SHE_M5_SZ) != 0) { + ret = WC_TEST_RET_ENC_NC; + goto exit_SHE_Test; + } + + /* ---- One-shot M1/M2/M3 ---- */ + wc_SHE_Free(she); + ret = wc_SHE_Init(she, HEAP_HINT, devId); + if (ret != 0) { + goto exit_SHE_Test; + } + + ret = wc_SHE_GenerateM1M2M3(she, + sheUid, sizeof(sheUid), + WC_SHE_MASTER_ECU_KEY_ID, vectorAuthKey, sizeof(vectorAuthKey), + 4, vectorNewKey, sizeof(vectorNewKey), + 1, 0, + m1, WC_SHE_M1_SZ, + m2, WC_SHE_M2_SZ, + m3, WC_SHE_M3_SZ); + if (ret != 0) { + goto exit_SHE_Test; + } + + if (XMEMCMP(m1, expM1, WC_SHE_M1_SZ) != 0) { + ret = WC_TEST_RET_ENC_NC; + goto exit_SHE_Test; + } + if (XMEMCMP(m2, expM2, WC_SHE_M2_SZ) != 0) { + ret = WC_TEST_RET_ENC_NC; + goto exit_SHE_Test; + } + if (XMEMCMP(m3, expM3, WC_SHE_M3_SZ) != 0) { + ret = WC_TEST_RET_ENC_NC; + goto exit_SHE_Test; + } + + /* ---- One-shot M4/M5 ---- */ + wc_SHE_Free(she); + ret = wc_SHE_Init(she, HEAP_HINT, devId); + if (ret != 0) { + goto exit_SHE_Test; + } + + ret = wc_SHE_GenerateM4M5(she, + sheUid, sizeof(sheUid), + WC_SHE_MASTER_ECU_KEY_ID, 4, + vectorNewKey, sizeof(vectorNewKey), + 1, + m4, WC_SHE_M4_SZ, + m5, WC_SHE_M5_SZ); + if (ret != 0) { + goto exit_SHE_Test; + } + + if (XMEMCMP(m4, expM4, WC_SHE_M4_SZ) != 0) { + ret = WC_TEST_RET_ENC_NC; + goto exit_SHE_Test; + } + if (XMEMCMP(m5, expM5, WC_SHE_M5_SZ) != 0) { + ret = WC_TEST_RET_ENC_NC; + goto exit_SHE_Test; + } + + /* ---- Independence test: two separate contexts, M1M2M3 and M4M5 ---- */ + wc_SHE_Free(she); + ret = wc_SHE_Init(she, HEAP_HINT, devId); + if (ret != 0) { + goto exit_SHE_Test; + } + + WC_ALLOC_VAR(she2, wc_SHE, 1, HEAP_HINT); + if (!WC_VAR_OK(she2)) { + ret = WC_TEST_RET_ENC_EC(MEMORY_E); + goto exit_SHE_Test; + } + ret = wc_SHE_Init(she2, HEAP_HINT, devId); + if (ret != 0) { + WC_FREE_VAR(she2, HEAP_HINT); + goto exit_SHE_Test; + } + + /* Generate M1/M2/M3 on first context */ + ret = wc_SHE_GenerateM1M2M3(she, + sheUid, sizeof(sheUid), + WC_SHE_MASTER_ECU_KEY_ID, vectorAuthKey, sizeof(vectorAuthKey), + 4, vectorNewKey, sizeof(vectorNewKey), + 1, 0, + m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ); + if (ret != 0) { + wc_SHE_Free(she2); + WC_FREE_VAR(she2, HEAP_HINT); + goto exit_SHE_Test; + } + + /* Generate M4/M5 on second context — completely independent */ + ret = wc_SHE_GenerateM4M5(she2, + sheUid, sizeof(sheUid), + WC_SHE_MASTER_ECU_KEY_ID, 4, + vectorNewKey, sizeof(vectorNewKey), + 1, + m4, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ); + + wc_SHE_Free(she2); + WC_FREE_VAR(she2, HEAP_HINT); + + if (ret != 0) { + goto exit_SHE_Test; + } + + /* Verify both match the test vector */ + if (XMEMCMP(m1, expM1, WC_SHE_M1_SZ) != 0 || + XMEMCMP(m2, expM2, WC_SHE_M2_SZ) != 0 || + XMEMCMP(m3, expM3, WC_SHE_M3_SZ) != 0 || + XMEMCMP(m4, expM4, WC_SHE_M4_SZ) != 0 || + XMEMCMP(m5, expM5, WC_SHE_M5_SZ) != 0) { + ret = WC_TEST_RET_ENC_NC; + goto exit_SHE_Test; + } + exit_SHE_Test: wc_SHE_Free(she); WC_FREE_VAR(she, HEAP_HINT); + WC_FREE_VAR(she2, HEAP_HINT); return ret; } @@ -67356,28 +67475,76 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx) switch (info->she.type) { case WC_SHE_SET_UID: - ret = wc_SHE_SetUID(she, info->she.op.setUid.uid, - info->she.op.setUid.uidSz, - info->she.ctx); + /* Test callback: just acknowledge, UID is in caller's buffer */ + ret = 0; + break; + case WC_SHE_GET_COUNTER: + { + static word32 simCounter = 0; + if (info->she.op.getCounter.counter != NULL) { + *info->she.op.getCounter.counter = ++simCounter; + } + ret = 0; + break; + } + case WC_SHE_GENERATE_M1M2M3: + /* Re-call with software devId using params from callback */ + ret = wc_SHE_GenerateM1M2M3(she, + info->she.op.generateM1M2M3.uid, + info->she.op.generateM1M2M3.uidSz, + info->she.op.generateM1M2M3.authKeyId, + info->she.op.generateM1M2M3.authKey, + info->she.op.generateM1M2M3.authKeySz, + info->she.op.generateM1M2M3.targetKeyId, + info->she.op.generateM1M2M3.newKey, + info->she.op.generateM1M2M3.newKeySz, + info->she.op.generateM1M2M3.counter, + info->she.op.generateM1M2M3.flags, + info->she.op.generateM1M2M3.m1, + info->she.op.generateM1M2M3.m1Sz, + info->she.op.generateM1M2M3.m2, + info->she.op.generateM1M2M3.m2Sz, + info->she.op.generateM1M2M3.m3, + info->she.op.generateM1M2M3.m3Sz); break; case WC_SHE_GENERATE_M4M5: - /* Re-call with software devId — fills she->m4/m5 */ - ret = wc_SHE_GenerateM4M5(she); + /* Re-call with software devId using params from callback */ + ret = wc_SHE_GenerateM4M5(she, + info->she.op.generateM4M5.uid, + info->she.op.generateM4M5.uidSz, + info->she.op.generateM4M5.authKeyId, + info->she.op.generateM4M5.targetKeyId, + info->she.op.generateM4M5.newKey, + info->she.op.generateM4M5.newKeySz, + info->she.op.generateM4M5.counter, + info->she.op.generateM4M5.m4, + info->she.op.generateM4M5.m4Sz, + info->she.op.generateM4M5.m5, + info->she.op.generateM4M5.m5Sz); break; case WC_SHE_EXPORT_KEY: - /* Fall back to software export */ - ret = wc_SHE_ExportKey(she, - info->she.op.exportKey.m1, - info->she.op.exportKey.m1Sz, - info->she.op.exportKey.m2, - info->she.op.exportKey.m2Sz, - info->she.op.exportKey.m3, - info->she.op.exportKey.m3Sz, - info->she.op.exportKey.m4, - info->she.op.exportKey.m4Sz, - info->she.op.exportKey.m5, - info->she.op.exportKey.m5Sz, - info->she.ctx); + /* Simulate hardware export — fill with test pattern */ + if (info->she.op.exportKey.m1 != NULL) { + XMEMSET(info->she.op.exportKey.m1, 0x11, + WC_SHE_M1_SZ); + } + if (info->she.op.exportKey.m2 != NULL) { + XMEMSET(info->she.op.exportKey.m2, 0x22, + WC_SHE_M2_SZ); + } + if (info->she.op.exportKey.m3 != NULL) { + XMEMSET(info->she.op.exportKey.m3, 0x33, + WC_SHE_M3_SZ); + } + if (info->she.op.exportKey.m4 != NULL) { + XMEMSET(info->she.op.exportKey.m4, 0x44, + WC_SHE_M4_SZ); + } + if (info->she.op.exportKey.m5 != NULL) { + XMEMSET(info->she.op.exportKey.m5, 0x55, + WC_SHE_M5_SZ); + } + ret = 0; break; default: ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); diff --git a/wolfssl/wolfcrypt/cryptocb.h b/wolfssl/wolfcrypt/cryptocb.h index c0a630f4ee..b2d20f5ac0 100644 --- a/wolfssl/wolfcrypt/cryptocb.h +++ b/wolfssl/wolfcrypt/cryptocb.h @@ -488,19 +488,53 @@ typedef struct wc_CryptoInfo { const void* ctx; /* read-only caller context */ union { struct { - const byte* uid; /* caller-provided UID (may be NULL) */ - word32 uidSz; /* size of uid buffer */ + const byte* uid; + word32 uidSz; } setUid; struct { - byte* m1; /* output: M1 */ + word32* counter; + } getCounter; + struct { + const byte* uid; + word32 uidSz; + byte authKeyId; + const byte* authKey; + word32 authKeySz; + byte targetKeyId; + const byte* newKey; + word32 newKeySz; + word32 counter; + byte flags; + byte* m1; word32 m1Sz; - byte* m2; /* output: M2 */ + byte* m2; word32 m2Sz; - byte* m3; /* output: M3 */ + byte* m3; word32 m3Sz; - byte* m4; /* output: M4 */ + } generateM1M2M3; + struct { + const byte* uid; + word32 uidSz; + byte authKeyId; + byte targetKeyId; + const byte* newKey; + word32 newKeySz; + word32 counter; + byte* m4; word32 m4Sz; - byte* m5; /* output: M5 */ + byte* m5; + word32 m5Sz; + } generateM4M5; + struct { + byte* m1; + word32 m1Sz; + byte* m2; + word32 m2Sz; + byte* m3; + word32 m3Sz; + byte* m4; + word32 m4Sz; + byte* m5; word32 m5Sz; } exportKey; } op; @@ -830,10 +864,23 @@ WOLFSSL_LOCAL int wc_CryptoCb_Cmac(Cmac* cmac, const byte* key, word32 keySz, #ifdef WOLFSSL_SHE WOLFSSL_LOCAL int wc_CryptoCb_SheSetUid(wc_SHE* she, const byte* uid, word32 uidSz, const void* ctx); +WOLFSSL_LOCAL int wc_CryptoCb_SheGetCounter(wc_SHE* she, word32* counter, + const void* ctx); WOLFSSL_LOCAL int wc_CryptoCb_SheGenerateM1M2M3(wc_SHE* she, - const void* ctx); + const byte* uid, word32 uidSz, + byte authKeyId, const byte* authKey, word32 authKeySz, + byte targetKeyId, const byte* newKey, word32 newKeySz, + word32 counter, byte flags, + byte* m1, word32 m1Sz, + byte* m2, word32 m2Sz, + byte* m3, word32 m3Sz); WOLFSSL_LOCAL int wc_CryptoCb_SheGenerateM4M5(wc_SHE* she, - const void* ctx); + const byte* uid, word32 uidSz, + byte authKeyId, byte targetKeyId, + const byte* newKey, word32 newKeySz, + word32 counter, + byte* m4, word32 m4Sz, + byte* m5, word32 m5Sz); WOLFSSL_LOCAL int wc_CryptoCb_SheExportKey(wc_SHE* she, byte* m1, word32 m1Sz, byte* m2, word32 m2Sz, diff --git a/wolfssl/wolfcrypt/she.h b/wolfssl/wolfcrypt/she.h index 0b49a5c677..2c7377d737 100644 --- a/wolfssl/wolfcrypt/she.h +++ b/wolfssl/wolfcrypt/she.h @@ -46,9 +46,10 @@ /* crypto callback sub-types for WC_ALGO_TYPE_SHE */ enum wc_SheType { WC_SHE_SET_UID = 1, - WC_SHE_GENERATE_M1M2M3 = 2, - WC_SHE_GENERATE_M4M5 = 3, - WC_SHE_EXPORT_KEY = 4 + WC_SHE_GET_COUNTER = 2, + WC_SHE_GENERATE_M1M2M3 = 3, + WC_SHE_GENERATE_M4M5 = 4, + WC_SHE_EXPORT_KEY = 5 }; /* test flags (only used for KATs) */ @@ -93,29 +94,26 @@ enum { }; typedef struct wc_SHE { - byte uid[WC_SHE_UID_SZ]; - byte authKeyId; - byte targetKeyId; - byte authKey[WC_SHE_KEY_SZ]; - byte newKey[WC_SHE_KEY_SZ]; - word32 counter; - byte flags; - - byte kdfEncC[WC_SHE_KEY_SZ]; /* KDF encryption constant (CENC) */ - byte kdfMacC[WC_SHE_KEY_SZ]; /* KDF authentication constant (CMAC) */ - byte m2pHeader[WC_SHE_KEY_SZ]; /* M2P cleartext header (counter|flags|pad) */ - byte m4pHeader[WC_SHE_KEY_SZ]; /* M4P cleartext header (counter|pad) */ - byte m2pOverride; /* set by SetM2Header to skip auto-build */ - byte m4pOverride; /* set by SetM4Header to skip auto-build */ +#ifdef WOLFSSL_SHE_EXTENDED + /* Custom KDF constants and header overrides. + * Useful for some HSMs that support multiple key groups with + * different derivation constants. */ + byte kdfEncC[WC_SHE_KEY_SZ]; + byte kdfMacC[WC_SHE_KEY_SZ]; + byte m2pHeader[WC_SHE_KEY_SZ]; + byte m4pHeader[WC_SHE_KEY_SZ]; + byte kdfEncOverride; + byte kdfMacOverride; + byte m2pOverride; + byte m4pOverride; +#endif +#if defined(WOLF_CRYPTO_CB) || !defined(NO_WC_SHE_IMPORT_M123) byte m1[WC_SHE_M1_SZ]; byte m2[WC_SHE_M2_SZ]; byte m3[WC_SHE_M3_SZ]; - byte m4[WC_SHE_M4_SZ]; - byte m5[WC_SHE_M5_SZ]; - byte generated; - byte verified; +#endif void* heap; int devId; @@ -143,54 +141,69 @@ WOLFSSL_API int wc_SHE_Init_Label(wc_SHE* she, const char* label, void* heap, int devId); #endif -/* Scrub key material and zero the context */ +/* Scrub and zero the context */ WOLFSSL_API void wc_SHE_Free(wc_SHE* she); -/* Set UID; callback optional (WC_SHE_SET_UID) */ -WOLFSSL_API int wc_SHE_SetUID(wc_SHE* she, const byte* uid, word32 uidSz, +/* Get UID from hardware; callback required (WC_SHE_SET_UID) */ +#if defined(WOLF_CRYPTO_CB) && !defined(NO_WC_SHE_GETUID) +WOLFSSL_API int wc_SHE_GetUID(wc_SHE* she, byte* uid, word32 uidSz, const void* ctx); +#endif -/* Set authorizing key slot ID and value */ -WOLFSSL_API int wc_SHE_SetAuthKey(wc_SHE* she, byte authKeyId, - const byte* authKey, word32 keySz); +/* Get counter from hardware; callback required */ +#if defined(WOLF_CRYPTO_CB) && !defined(NO_WC_SHE_GETCOUNTER) +WOLFSSL_API int wc_SHE_GetCounter(wc_SHE* she, word32* counter, + const void* ctx); +#endif -/* Set target key slot ID and new key value */ -WOLFSSL_API int wc_SHE_SetNewKey(wc_SHE* she, byte targetKeyId, - const byte* newKey, word32 keySz); - -/* Set monotonic counter value for M2 */ -WOLFSSL_API int wc_SHE_SetCounter(wc_SHE* she, word32 counter); - -/* Set flag byte for M2 */ -WOLFSSL_API int wc_SHE_SetFlags(wc_SHE* she, byte flags); - -/* Set KDF constants (CENC/CMAC) used for key derivation. - * Defaults are set by Init. Either pointer may be NULL to skip. */ +/* Custom KDF constants and header overrides. + * Useful for some HSMs that support multiple key groups with + * different derivation constants. */ +#ifdef WOLFSSL_SHE_EXTENDED +/* Set KDF constants (CENC/CMAC). Defaults set by Init. NULL to skip. */ WOLFSSL_API int wc_SHE_SetKdfConstants(wc_SHE* she, const byte* encC, word32 encCSz, const byte* macC, word32 macCSz); -/* Override M2P cleartext header (first 16 bytes before KID'). - * Skips auto-build from counter/flags in GenerateM1M2M3. */ +/* Override M2P cleartext header. Skips auto-build from counter/flags. */ WOLFSSL_API int wc_SHE_SetM2Header(wc_SHE* she, const byte* header, word32 headerSz); -/* Override M4P cleartext header (16-byte counter block). - * Skips auto-build from counter in GenerateM4M5. */ +/* Override M4P cleartext header. Skips auto-build from counter. */ WOLFSSL_API int wc_SHE_SetM4Header(wc_SHE* she, const byte* header, word32 headerSz); +#endif /* WOLFSSL_SHE_EXTENDED */ -/* Generate M1/M2/M3 from the current context */ -WOLFSSL_API int wc_SHE_GenerateM1M2M3(wc_SHE* she); +/* Import externally-provided M1/M2/M3 into context; sets generated flag */ +#if defined(WOLF_CRYPTO_CB) || !defined(NO_WC_SHE_IMPORT_M123) +WOLFSSL_API int wc_SHE_ImportM1M2M3(wc_SHE* she, + const byte* m1, word32 m1Sz, + const byte* m2, word32 m2Sz, + const byte* m3, word32 m3Sz); +#endif -/* Miyaguchi-Preneel AES-128 compression (internal, exposed for testing) */ -WOLFSSL_TEST_VIS int wc_She_AesMp16(Aes* aes, const byte* in, word32 inSz, - byte* out); +/* Generate M1/M2/M3 and write to caller buffers */ +WOLFSSL_API int wc_SHE_GenerateM1M2M3(wc_SHE* she, + const byte* uid, word32 uidSz, + byte authKeyId, const byte* authKey, word32 authKeySz, + byte targetKeyId, const byte* newKey, word32 newKeySz, + word32 counter, byte flags, + byte* m1, word32 m1Sz, + byte* m2, word32 m2Sz, + byte* m3, word32 m3Sz); -/* Generate M4/M5 verification messages; callback optional (WC_SHE_GENERATE_M4M5) */ -WOLFSSL_API int wc_SHE_GenerateM4M5(wc_SHE* she); +/* Generate M4/M5 and write to caller buffers */ +WOLFSSL_API int wc_SHE_GenerateM4M5(wc_SHE* she, + const byte* uid, word32 uidSz, + byte authKeyId, byte targetKeyId, + const byte* newKey, word32 newKeySz, + word32 counter, + byte* m4, word32 m4Sz, + byte* m5, word32 m5Sz); -/* Export M1-M5 into caller buffers; NULL to skip; callback optional (WC_SHE_EXPORT_KEY) */ +/* Export key from hardware as M1-M5; callback required. + * Some HSMs allow exporting certain key slots (e.g. RAM key) in SHE format. */ +#if defined(WOLF_CRYPTO_CB) && !defined(NO_WC_SHE_EXPORTKEY) WOLFSSL_API int wc_SHE_ExportKey(wc_SHE* she, byte* m1, word32 m1Sz, byte* m2, word32 m2Sz, @@ -198,6 +211,11 @@ WOLFSSL_API int wc_SHE_ExportKey(wc_SHE* she, byte* m4, word32 m4Sz, byte* m5, word32 m5Sz, const void* ctx); +#endif + +/* Internal: Miyaguchi-Preneel AES-128 compression, exposed for testing */ +WOLFSSL_TEST_VIS int wc_She_AesMp16(Aes* aes, const byte* in, word32 inSz, + byte* out); #ifdef __cplusplus } /* extern "C" */