Skip to content

Commit 8bb4ece

Browse files
committed
Add one-shot SHE LoadKey and LoadKey Verify convenience APIs: wc_SHE_LoadKey, wc_SHE_LoadKey_Id, wc_SHE_LoadKey_Label and their verify counterparts
1 parent 60368c8 commit 8bb4ece

2 files changed

Lines changed: 396 additions & 0 deletions

File tree

wolfcrypt/src/wc_she.c

Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,320 @@ int wc_SHE_GenerateM4M5(wc_SHE* she,
770770
return ret;
771771
}
772772

773+
/* -------------------------------------------------------------------------- */
774+
/* One-shot Load Key helpers */
775+
/* */
776+
/* Internal helper that does the actual work: imports M1/M2/M3 into the */
777+
/* already-initialized SHE context, calls GenerateM4M5 (which dispatches to */
778+
/* the crypto callback to send M1/M2/M3 to the HSM and receive M4/M5 back), */
779+
/* and frees the context. */
780+
/* -------------------------------------------------------------------------- */
781+
#ifndef NO_WC_SHE_LOADKEY
782+
#if defined(WOLF_CRYPTO_CB) || !defined(NO_WC_SHE_IMPORT_M123)
783+
static int wc_SHE_LoadKey_Internal(wc_SHE* she,
784+
const byte* m1, word32 m1Sz,
785+
const byte* m2, word32 m2Sz,
786+
const byte* m3, word32 m3Sz,
787+
byte* m4, word32 m4Sz,
788+
byte* m5, word32 m5Sz)
789+
{
790+
int ret;
791+
792+
ret = wc_SHE_ImportM1M2M3(she, m1, m1Sz, m2, m2Sz, m3, m3Sz);
793+
if (ret != 0) {
794+
wc_SHE_Free(she);
795+
return ret;
796+
}
797+
798+
/* GenerateM4M5 with NULL uid/newKey -- the callback reads M1/M2/M3
799+
* from the context and sends them to the HSM which returns M4/M5. */
800+
ret = wc_SHE_GenerateM4M5(she, NULL, 0, 0, 0, NULL, 0, 0,
801+
m4, m4Sz, m5, m5Sz);
802+
803+
wc_SHE_Free(she);
804+
return ret;
805+
}
806+
807+
/* -------------------------------------------------------------------------- */
808+
/* wc_SHE_LoadKey */
809+
/* */
810+
/* One-shot: Init, ImportM1M2M3, GenerateM4M5 (via callback), Free. */
811+
/* Requires a valid devId (not INVALID_DEVID) since the operation dispatches */
812+
/* to a hardware crypto callback. */
813+
/* -------------------------------------------------------------------------- */
814+
int wc_SHE_LoadKey(
815+
void* heap, int devId,
816+
const byte* m1, word32 m1Sz,
817+
const byte* m2, word32 m2Sz,
818+
const byte* m3, word32 m3Sz,
819+
byte* m4, word32 m4Sz,
820+
byte* m5, word32 m5Sz)
821+
{
822+
int ret;
823+
WC_DECLARE_VAR(she, wc_SHE, 1, heap);
824+
825+
if (m1 == NULL || m2 == NULL || m3 == NULL ||
826+
m4 == NULL || m5 == NULL) {
827+
return BAD_FUNC_ARG;
828+
}
829+
830+
if (devId == INVALID_DEVID) {
831+
return BAD_FUNC_ARG;
832+
}
833+
834+
if (m1Sz != WC_SHE_M1_SZ || m2Sz != WC_SHE_M2_SZ ||
835+
m3Sz != WC_SHE_M3_SZ) {
836+
return BAD_FUNC_ARG;
837+
}
838+
839+
if (m4Sz < WC_SHE_M4_SZ || m5Sz < WC_SHE_M5_SZ) {
840+
return BAD_FUNC_ARG;
841+
}
842+
843+
WC_ALLOC_VAR(she, wc_SHE, 1, heap);
844+
if (!WC_VAR_OK(she)) {
845+
return MEMORY_E;
846+
}
847+
848+
ret = wc_SHE_Init(she, heap, devId);
849+
if (ret != 0) {
850+
WC_FREE_VAR(she, heap);
851+
return ret;
852+
}
853+
854+
ret = wc_SHE_LoadKey_Internal(she, m1, m1Sz, m2, m2Sz, m3, m3Sz,
855+
m4, m4Sz, m5, m5Sz);
856+
WC_FREE_VAR(she, heap);
857+
return ret;
858+
}
859+
860+
#ifdef WOLF_PRIVATE_KEY_ID
861+
/* -------------------------------------------------------------------------- */
862+
/* wc_SHE_LoadKey_Id */
863+
/* */
864+
/* One-shot with opaque hardware key identifier. */
865+
/* Requires a valid devId (not INVALID_DEVID) since the operation dispatches */
866+
/* to a hardware crypto callback. */
867+
/* -------------------------------------------------------------------------- */
868+
int wc_SHE_LoadKey_Id(
869+
unsigned char* id, int idLen,
870+
void* heap, int devId,
871+
const byte* m1, word32 m1Sz,
872+
const byte* m2, word32 m2Sz,
873+
const byte* m3, word32 m3Sz,
874+
byte* m4, word32 m4Sz,
875+
byte* m5, word32 m5Sz)
876+
{
877+
int ret;
878+
WC_DECLARE_VAR(she, wc_SHE, 1, heap);
879+
880+
if (id == NULL || m1 == NULL || m2 == NULL || m3 == NULL ||
881+
m4 == NULL || m5 == NULL) {
882+
return BAD_FUNC_ARG;
883+
}
884+
885+
if (devId == INVALID_DEVID) {
886+
return BAD_FUNC_ARG;
887+
}
888+
889+
if (idLen < 0 || idLen > WC_SHE_MAX_ID_LEN) {
890+
return BAD_FUNC_ARG;
891+
}
892+
893+
if (m1Sz != WC_SHE_M1_SZ || m2Sz != WC_SHE_M2_SZ ||
894+
m3Sz != WC_SHE_M3_SZ) {
895+
return BAD_FUNC_ARG;
896+
}
897+
898+
if (m4Sz < WC_SHE_M4_SZ || m5Sz < WC_SHE_M5_SZ) {
899+
return BAD_FUNC_ARG;
900+
}
901+
902+
WC_ALLOC_VAR(she, wc_SHE, 1, heap);
903+
if (!WC_VAR_OK(she)) {
904+
return MEMORY_E;
905+
}
906+
907+
ret = wc_SHE_Init_Id(she, id, idLen, heap, devId);
908+
if (ret != 0) {
909+
WC_FREE_VAR(she, heap);
910+
return ret;
911+
}
912+
913+
ret = wc_SHE_LoadKey_Internal(she, m1, m1Sz, m2, m2Sz, m3, m3Sz,
914+
m4, m4Sz, m5, m5Sz);
915+
WC_FREE_VAR(she, heap);
916+
return ret;
917+
}
918+
919+
/* -------------------------------------------------------------------------- */
920+
/* wc_SHE_LoadKey_Label */
921+
/* */
922+
/* One-shot with human-readable key label. */
923+
/* Requires a valid devId (not INVALID_DEVID) since the operation dispatches */
924+
/* to a hardware crypto callback. */
925+
/* -------------------------------------------------------------------------- */
926+
int wc_SHE_LoadKey_Label(
927+
const char* label,
928+
void* heap, int devId,
929+
const byte* m1, word32 m1Sz,
930+
const byte* m2, word32 m2Sz,
931+
const byte* m3, word32 m3Sz,
932+
byte* m4, word32 m4Sz,
933+
byte* m5, word32 m5Sz)
934+
{
935+
int ret;
936+
WC_DECLARE_VAR(she, wc_SHE, 1, heap);
937+
938+
if (label == NULL || m1 == NULL || m2 == NULL || m3 == NULL ||
939+
m4 == NULL || m5 == NULL) {
940+
return BAD_FUNC_ARG;
941+
}
942+
943+
if (devId == INVALID_DEVID) {
944+
return BAD_FUNC_ARG;
945+
}
946+
947+
if (XSTRLEN(label) == 0 || XSTRLEN(label) > WC_SHE_MAX_LABEL_LEN) {
948+
return BAD_FUNC_ARG;
949+
}
950+
951+
if (m1Sz != WC_SHE_M1_SZ || m2Sz != WC_SHE_M2_SZ ||
952+
m3Sz != WC_SHE_M3_SZ) {
953+
return BAD_FUNC_ARG;
954+
}
955+
956+
if (m4Sz < WC_SHE_M4_SZ || m5Sz < WC_SHE_M5_SZ) {
957+
return BAD_FUNC_ARG;
958+
}
959+
960+
WC_ALLOC_VAR(she, wc_SHE, 1, heap);
961+
if (!WC_VAR_OK(she)) {
962+
return MEMORY_E;
963+
}
964+
965+
ret = wc_SHE_Init_Label(she, label, heap, devId);
966+
if (ret != 0) {
967+
WC_FREE_VAR(she, heap);
968+
return ret;
969+
}
970+
971+
ret = wc_SHE_LoadKey_Internal(she, m1, m1Sz, m2, m2Sz, m3, m3Sz,
972+
m4, m4Sz, m5, m5Sz);
973+
WC_FREE_VAR(she, heap);
974+
return ret;
975+
}
976+
#endif /* WOLF_PRIVATE_KEY_ID */
977+
978+
/* -------------------------------------------------------------------------- */
979+
/* One-shot Load Key with Verification */
980+
/* */
981+
/* Same as the LoadKey variants but also compares the M4/M5 returned by the */
982+
/* HSM against caller-provided expected values. Returns SIG_VERIFY_E on */
983+
/* mismatch. The actual M4/M5 from the HSM are still written to the output */
984+
/* buffers so the caller can inspect them on failure. */
985+
/* -------------------------------------------------------------------------- */
986+
static int wc_SHE_VerifyM4M5(
987+
const byte* m4, word32 m4Sz,
988+
const byte* m5, word32 m5Sz,
989+
const byte* m4Expected, word32 m4ExpectedSz,
990+
const byte* m5Expected, word32 m5ExpectedSz)
991+
{
992+
if (m4Expected == NULL || m5Expected == NULL) {
993+
return BAD_FUNC_ARG;
994+
}
995+
996+
if (m4ExpectedSz != WC_SHE_M4_SZ || m5ExpectedSz != WC_SHE_M5_SZ ||
997+
m4Sz < WC_SHE_M4_SZ || m5Sz < WC_SHE_M5_SZ) {
998+
return BAD_FUNC_ARG;
999+
}
1000+
1001+
if (ConstantCompare(m4, m4Expected, WC_SHE_M4_SZ) != 0 ||
1002+
ConstantCompare(m5, m5Expected, WC_SHE_M5_SZ) != 0) {
1003+
return SIG_VERIFY_E;
1004+
}
1005+
1006+
return 0;
1007+
}
1008+
1009+
int wc_SHE_LoadKey_Verify(
1010+
void* heap, int devId,
1011+
const byte* m1, word32 m1Sz,
1012+
const byte* m2, word32 m2Sz,
1013+
const byte* m3, word32 m3Sz,
1014+
byte* m4, word32 m4Sz,
1015+
byte* m5, word32 m5Sz,
1016+
const byte* m4Expected, word32 m4ExpectedSz,
1017+
const byte* m5Expected, word32 m5ExpectedSz)
1018+
{
1019+
int ret;
1020+
1021+
ret = wc_SHE_LoadKey(heap, devId, m1, m1Sz, m2, m2Sz, m3, m3Sz,
1022+
m4, m4Sz, m5, m5Sz);
1023+
if (ret != 0) {
1024+
return ret;
1025+
}
1026+
1027+
return wc_SHE_VerifyM4M5(m4, m4Sz, m5, m5Sz,
1028+
m4Expected, m4ExpectedSz,
1029+
m5Expected, m5ExpectedSz);
1030+
}
1031+
1032+
#ifdef WOLF_PRIVATE_KEY_ID
1033+
int wc_SHE_LoadKey_Verify_Id(
1034+
unsigned char* id, int idLen,
1035+
void* heap, int devId,
1036+
const byte* m1, word32 m1Sz,
1037+
const byte* m2, word32 m2Sz,
1038+
const byte* m3, word32 m3Sz,
1039+
byte* m4, word32 m4Sz,
1040+
byte* m5, word32 m5Sz,
1041+
const byte* m4Expected, word32 m4ExpectedSz,
1042+
const byte* m5Expected, word32 m5ExpectedSz)
1043+
{
1044+
int ret;
1045+
1046+
ret = wc_SHE_LoadKey_Id(id, idLen, heap, devId,
1047+
m1, m1Sz, m2, m2Sz, m3, m3Sz,
1048+
m4, m4Sz, m5, m5Sz);
1049+
if (ret != 0) {
1050+
return ret;
1051+
}
1052+
1053+
return wc_SHE_VerifyM4M5(m4, m4Sz, m5, m5Sz,
1054+
m4Expected, m4ExpectedSz,
1055+
m5Expected, m5ExpectedSz);
1056+
}
1057+
1058+
int wc_SHE_LoadKey_Verify_Label(
1059+
const char* label,
1060+
void* heap, int devId,
1061+
const byte* m1, word32 m1Sz,
1062+
const byte* m2, word32 m2Sz,
1063+
const byte* m3, word32 m3Sz,
1064+
byte* m4, word32 m4Sz,
1065+
byte* m5, word32 m5Sz,
1066+
const byte* m4Expected, word32 m4ExpectedSz,
1067+
const byte* m5Expected, word32 m5ExpectedSz)
1068+
{
1069+
int ret;
1070+
1071+
ret = wc_SHE_LoadKey_Label(label, heap, devId,
1072+
m1, m1Sz, m2, m2Sz, m3, m3Sz,
1073+
m4, m4Sz, m5, m5Sz);
1074+
if (ret != 0) {
1075+
return ret;
1076+
}
1077+
1078+
return wc_SHE_VerifyM4M5(m4, m4Sz, m5, m5Sz,
1079+
m4Expected, m4ExpectedSz,
1080+
m5Expected, m5ExpectedSz);
1081+
}
1082+
#endif /* WOLF_PRIVATE_KEY_ID */
1083+
1084+
#endif /* WOLF_CRYPTO_CB || !NO_WC_SHE_IMPORT_M123 */
1085+
#endif /* !NO_WC_SHE_LOADKEY */
1086+
7731087
/* -------------------------------------------------------------------------- */
7741088
/* Export Key */
7751089
/* */

0 commit comments

Comments
 (0)