Skip to content

Commit 2095be4

Browse files
committed
Fix CMake SHE deps, const-correctness in CryptoCb uid, stale comment, XSTRLEN double call, configure.ac AES-CBC guard, and add LoadKey/LoadKey_Verify test coverage
1 parent d46fd0f commit 2095be4

File tree

8 files changed

+194
-19
lines changed

8 files changed

+194
-19
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,8 @@ if(WOLFSSL_SHE STREQUAL "standard" OR WOLFSSL_SHE STREQUAL "extended")
16521652
else()
16531653
list(APPEND WOLFSSL_DEFINITIONS
16541654
"-DWOLFSSL_SHE")
1655+
override_cache(WOLFSSL_CMAC "yes")
1656+
override_cache(WOLFSSL_AESCBC "yes")
16551657
endif()
16561658
endif()
16571659

configure.ac

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5958,6 +5958,10 @@ AC_ARG_ENABLE([she],
59585958

59595959
if test "x$ENABLED_SHE" = "xstandard" || test "x$ENABLED_SHE" = "xextended"
59605960
then
5961+
if test "$ENABLED_AESCBC" = "no"
5962+
then
5963+
AC_MSG_ERROR([SHE requires AES-CBC. Cannot use --disable-aescbc with --enable-she.])
5964+
fi
59615965
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_SHE -DWOLFSSL_CMAC -DWOLFSSL_AES_DIRECT"
59625966
ENABLED_CMAC=yes
59635967
ENABLED_AESCBC=yes

tests/api/test_she.c

Lines changed: 142 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -499,18 +499,32 @@ static int test_she_crypto_cb(int devIdArg, wc_CryptoInfo* info, void* ctx)
499499
info->she.op.generateM1M2M3.m3Sz);
500500
break;
501501
case WC_SHE_GENERATE_M4M5:
502-
ret = wc_SHE_GenerateM4M5(she,
503-
info->she.op.generateM4M5.uid,
504-
info->she.op.generateM4M5.uidSz,
505-
info->she.op.generateM4M5.authKeyId,
506-
info->she.op.generateM4M5.targetKeyId,
507-
info->she.op.generateM4M5.newKey,
508-
info->she.op.generateM4M5.newKeySz,
509-
info->she.op.generateM4M5.counter,
510-
info->she.op.generateM4M5.m4,
511-
info->she.op.generateM4M5.m4Sz,
512-
info->she.op.generateM4M5.m5,
513-
info->she.op.generateM4M5.m5Sz);
502+
if (info->she.op.generateM4M5.uid == NULL &&
503+
she->generated) {
504+
/* LoadKey flow: M1/M2/M3 already imported, simulate HSM
505+
* returning M4/M5 from known test vectors. */
506+
if (info->she.op.generateM4M5.m4 != NULL)
507+
XMEMCPY(info->she.op.generateM4M5.m4,
508+
sheTestExpM4, WC_SHE_M4_SZ);
509+
if (info->she.op.generateM4M5.m5 != NULL)
510+
XMEMCPY(info->she.op.generateM4M5.m5,
511+
sheTestExpM5, WC_SHE_M5_SZ);
512+
ret = 0;
513+
}
514+
else {
515+
ret = wc_SHE_GenerateM4M5(she,
516+
info->she.op.generateM4M5.uid,
517+
info->she.op.generateM4M5.uidSz,
518+
info->she.op.generateM4M5.authKeyId,
519+
info->she.op.generateM4M5.targetKeyId,
520+
info->she.op.generateM4M5.newKey,
521+
info->she.op.generateM4M5.newKeySz,
522+
info->she.op.generateM4M5.counter,
523+
info->she.op.generateM4M5.m4,
524+
info->she.op.generateM4M5.m4Sz,
525+
info->she.op.generateM4M5.m5,
526+
info->she.op.generateM4M5.m5Sz);
527+
}
514528
break;
515529
case WC_SHE_EXPORT_KEY:
516530
/* Simulate hardware export -- fill with test pattern */
@@ -635,4 +649,120 @@ int test_wc_SHE_CryptoCb(void)
635649
return EXPECT_RESULT();
636650
}
637651

652+
#ifndef NO_WC_SHE_LOADKEY
653+
654+
int test_wc_SHE_LoadKey(void)
655+
{
656+
EXPECT_DECLS;
657+
int sheTestDevId = 54322;
658+
byte m1[WC_SHE_M1_SZ];
659+
byte m2[WC_SHE_M2_SZ];
660+
byte m3[WC_SHE_M3_SZ];
661+
byte m4[WC_SHE_M4_SZ];
662+
byte m5[WC_SHE_M5_SZ];
663+
664+
ExpectIntEQ(wc_CryptoCb_RegisterDevice(sheTestDevId,
665+
test_she_crypto_cb, NULL), 0);
666+
667+
/* Generate valid M1/M2/M3 from test vectors */
668+
{
669+
wc_SHE she;
670+
ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0);
671+
ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she,
672+
sheTestUid, sizeof(sheTestUid),
673+
WC_SHE_MASTER_ECU_KEY_ID,
674+
sheTestAuthKey, sizeof(sheTestAuthKey),
675+
4, sheTestNewKey, sizeof(sheTestNewKey), 1, 0,
676+
m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ,
677+
m3, WC_SHE_M3_SZ), 0);
678+
wc_SHE_Free(&she);
679+
}
680+
681+
/* Basic: LoadKey should import M1/M2/M3 and produce M4/M5 via callback */
682+
ExpectIntEQ(wc_SHE_LoadKey(NULL, sheTestDevId,
683+
m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ,
684+
m4, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ), 0);
685+
ExpectIntEQ(XMEMCMP(m4, sheTestExpM4, WC_SHE_M4_SZ), 0);
686+
ExpectIntEQ(XMEMCMP(m5, sheTestExpM5, WC_SHE_M5_SZ), 0);
687+
688+
/* Bad args: NULL m1 */
689+
ExpectIntEQ(wc_SHE_LoadKey(NULL, sheTestDevId,
690+
NULL, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ,
691+
m4, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ),
692+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
693+
694+
/* Bad args: NULL m4 output */
695+
ExpectIntEQ(wc_SHE_LoadKey(NULL, sheTestDevId,
696+
m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ,
697+
NULL, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ),
698+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
699+
700+
/* Bad args: INVALID_DEVID */
701+
ExpectIntEQ(wc_SHE_LoadKey(NULL, INVALID_DEVID,
702+
m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ,
703+
m4, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ),
704+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
705+
706+
/* Bad args: wrong M1 size */
707+
ExpectIntEQ(wc_SHE_LoadKey(NULL, sheTestDevId,
708+
m1, WC_SHE_M1_SZ - 1, m2, WC_SHE_M2_SZ,
709+
m3, WC_SHE_M3_SZ,
710+
m4, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ),
711+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
712+
713+
wc_CryptoCb_UnRegisterDevice(sheTestDevId);
714+
return EXPECT_RESULT();
715+
}
716+
717+
int test_wc_SHE_LoadKey_Verify(void)
718+
{
719+
EXPECT_DECLS;
720+
int sheTestDevId = 54323;
721+
byte m1[WC_SHE_M1_SZ];
722+
byte m2[WC_SHE_M2_SZ];
723+
byte m3[WC_SHE_M3_SZ];
724+
byte m4[WC_SHE_M4_SZ];
725+
byte m5[WC_SHE_M5_SZ];
726+
byte badM4[WC_SHE_M4_SZ];
727+
728+
ExpectIntEQ(wc_CryptoCb_RegisterDevice(sheTestDevId,
729+
test_she_crypto_cb, NULL), 0);
730+
731+
/* Generate valid M1/M2/M3 from test vectors */
732+
{
733+
wc_SHE she;
734+
ExpectIntEQ(wc_SHE_Init(&she, NULL, INVALID_DEVID), 0);
735+
ExpectIntEQ(wc_SHE_GenerateM1M2M3(&she,
736+
sheTestUid, sizeof(sheTestUid),
737+
WC_SHE_MASTER_ECU_KEY_ID,
738+
sheTestAuthKey, sizeof(sheTestAuthKey),
739+
4, sheTestNewKey, sizeof(sheTestNewKey), 1, 0,
740+
m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ,
741+
m3, WC_SHE_M3_SZ), 0);
742+
wc_SHE_Free(&she);
743+
}
744+
745+
/* Matching: expected M4/M5 match what the callback produces */
746+
ExpectIntEQ(wc_SHE_LoadKey_Verify(NULL, sheTestDevId,
747+
m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ,
748+
m4, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ,
749+
sheTestExpM4, WC_SHE_M4_SZ,
750+
sheTestExpM5, WC_SHE_M5_SZ), 0);
751+
752+
/* Mismatch: wrong expected M4 should fail with SIG_VERIFY_E */
753+
XMEMCPY(badM4, sheTestExpM4, WC_SHE_M4_SZ);
754+
badM4[0] ^= 0xFF;
755+
ExpectIntEQ(wc_SHE_LoadKey_Verify(NULL, sheTestDevId,
756+
m1, WC_SHE_M1_SZ, m2, WC_SHE_M2_SZ, m3, WC_SHE_M3_SZ,
757+
m4, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ,
758+
badM4, WC_SHE_M4_SZ,
759+
sheTestExpM5, WC_SHE_M5_SZ),
760+
WC_NO_ERR_TRACE(SIG_VERIFY_E));
761+
762+
wc_CryptoCb_UnRegisterDevice(sheTestDevId);
763+
return EXPECT_RESULT();
764+
}
765+
766+
#endif /* !NO_WC_SHE_LOADKEY */
767+
638768
#endif /* WOLF_CRYPTO_CB && WOLFSSL_SHE && !NO_AES */

tests/api/test_she.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ int test_wc_SHE_SetM2M4Header(void);
3838
#endif
3939
#if defined(WOLF_CRYPTO_CB) && defined(WOLFSSL_SHE)
4040
int test_wc_SHE_CryptoCb(void);
41+
#ifndef NO_WC_SHE_LOADKEY
42+
int test_wc_SHE_LoadKey(void);
43+
int test_wc_SHE_LoadKey_Verify(void);
44+
#endif
4145
#endif
4246

4347
#define TEST_SHE_DECLS \
@@ -59,8 +63,15 @@ int test_wc_SHE_CryptoCb(void);
5963
#endif
6064

6165
#if defined(WOLF_CRYPTO_CB) && defined(WOLFSSL_SHE)
66+
#if !defined(NO_WC_SHE_LOADKEY)
67+
#define TEST_SHE_CB_DECLS \
68+
TEST_DECL_GROUP("she", test_wc_SHE_CryptoCb), \
69+
TEST_DECL_GROUP("she", test_wc_SHE_LoadKey), \
70+
TEST_DECL_GROUP("she", test_wc_SHE_LoadKey_Verify)
71+
#else
6272
#define TEST_SHE_CB_DECLS \
6373
TEST_DECL_GROUP("she", test_wc_SHE_CryptoCb)
74+
#endif
6475
#else
6576
#define TEST_SHE_CB_DECLS
6677
#endif

wolfcrypt/src/cryptocb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2036,7 +2036,7 @@ int wc_CryptoCb_Cmac(Cmac* cmac, const byte* key, word32 keySz,
20362036
#endif /* WOLFSSL_CMAC */
20372037

20382038
#ifdef WOLFSSL_SHE
2039-
int wc_CryptoCb_SheGetUid(wc_SHE* she, const byte* uid, word32 uidSz,
2039+
int wc_CryptoCb_SheGetUid(wc_SHE* she, byte* uid, word32 uidSz,
20402040
const void* ctx)
20412041
{
20422042
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);

wolfcrypt/src/wc_she.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,6 @@ int wc_SHE_SetKdfConstants(wc_SHE* she,
359359

360360
#endif /* WOLFSSL_SHE_EXTENDED */
361361

362-
/* -------------------------------------------------------------------------- */
363-
/* GetUID */
364-
365362
#if defined(WOLF_CRYPTO_CB) || !defined(NO_WC_SHE_IMPORT_M123)
366363
/* -------------------------------------------------------------------------- */
367364
/* Import M1/M2/M3 */
@@ -933,6 +930,7 @@ int wc_SHE_LoadKey_Label(
933930
byte* m5, word32 m5Sz)
934931
{
935932
int ret;
933+
word32 labelLen;
936934
WC_DECLARE_VAR(she, wc_SHE, 1, heap);
937935

938936
if (label == NULL || m1 == NULL || m2 == NULL || m3 == NULL ||
@@ -944,7 +942,8 @@ int wc_SHE_LoadKey_Label(
944942
return BAD_FUNC_ARG;
945943
}
946944

947-
if (XSTRLEN(label) == 0 || XSTRLEN(label) > WC_SHE_MAX_LABEL_LEN) {
945+
labelLen = (word32)XSTRLEN(label);
946+
if (labelLen == 0 || labelLen > WC_SHE_MAX_LABEL_LEN) {
948947
return BAD_FUNC_ARG;
949948
}
950949

wolfcrypt/test/test.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56598,6 +56598,35 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t she_test(void)
5659856598
goto exit_SHE_Test;
5659956599
}
5660056600

56601+
#if !defined(NO_WC_SHE_LOADKEY) && \
56602+
(defined(WOLF_CRYPTO_CB) || !defined(NO_WC_SHE_IMPORT_M123))
56603+
/* ---- LoadKey_Verify ---- */
56604+
XMEMSET(m4, 0, WC_SHE_M4_SZ);
56605+
XMEMSET(m5, 0, WC_SHE_M5_SZ);
56606+
ret = wc_SHE_LoadKey_Verify(HEAP_HINT, devId,
56607+
expM1, WC_SHE_M1_SZ, expM2, WC_SHE_M2_SZ,
56608+
expM3, WC_SHE_M3_SZ,
56609+
m4, WC_SHE_M4_SZ, m5, WC_SHE_M5_SZ,
56610+
expM4, WC_SHE_M4_SZ, expM5, WC_SHE_M5_SZ);
56611+
if (devId == INVALID_DEVID) {
56612+
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
56613+
ret = WC_TEST_RET_ENC_EC(ret);
56614+
goto exit_SHE_Test;
56615+
}
56616+
}
56617+
else {
56618+
if (ret != 0) {
56619+
goto exit_SHE_Test;
56620+
}
56621+
if (XMEMCMP(m4, expM4, WC_SHE_M4_SZ) != 0 ||
56622+
XMEMCMP(m5, expM5, WC_SHE_M5_SZ) != 0) {
56623+
ret = WC_TEST_RET_ENC_NC;
56624+
goto exit_SHE_Test;
56625+
}
56626+
}
56627+
ret = 0;
56628+
#endif /* !NO_WC_SHE_LOADKEY */
56629+
5660156630
#if defined(WC_SHE_SW_DEFAULT) && defined(WOLF_CRYPTO_CB) && \
5660256631
!defined(NO_WC_SHE_GETUID) && !defined(NO_WC_SHE_GETCOUNTER)
5660356632
ret = she_sw_default_test();

wolfssl/wolfcrypt/cryptocb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ typedef struct wc_CryptoInfo {
468468
const void* ctx; /* read-only caller context */
469469
union {
470470
struct {
471-
const byte* uid;
471+
byte* uid;
472472
word32 uidSz;
473473
} getUid;
474474
struct {
@@ -821,7 +821,7 @@ WOLFSSL_LOCAL int wc_CryptoCb_Cmac(Cmac* cmac, const byte* key, word32 keySz,
821821
#endif
822822

823823
#ifdef WOLFSSL_SHE
824-
WOLFSSL_LOCAL int wc_CryptoCb_SheGetUid(wc_SHE* she, const byte* uid,
824+
WOLFSSL_LOCAL int wc_CryptoCb_SheGetUid(wc_SHE* she, byte* uid,
825825
word32 uidSz, const void* ctx);
826826
WOLFSSL_LOCAL int wc_CryptoCb_SheGetCounter(wc_SHE* she, word32* counter,
827827
const void* ctx);

0 commit comments

Comments
 (0)