Skip to content

Commit b019052

Browse files
committed
Add devCtx, PRIVATE_KEY_ID, and label support to LMS and XMSS keys
Mirrors the convention used by RSA, ECC, and Dilithium so a CryptoCb callback can locate a device-resident LMS / XMSS key by either an opaque byte identifier or a textual label, and stash device-specific state on the key without going through the public API. LmsKey / XmssKey gain (under WOLF_CRYPTO_CB): void* devCtx; and (under WOLF_PRIVATE_KEY_ID): byte id[LMS_MAX_ID_LEN]; int idLen; char label[LMS_MAX_LABEL_LEN]; int labelLen; (with XMSS_MAX_* equivalents on XmssKey). The MAX_* constants default to 32 bytes and can be overridden by predefining the macro. Public init helpers, gated on WOLF_PRIVATE_KEY_ID, follow the wc_InitRsaKey_Id / wc_InitRsaKey_Label pattern: wc_LmsKey_InitId(key, id, len, heap, devId) wc_LmsKey_InitLabel(key, label, heap, devId) wc_XmssKey_InitId(key, id, len, heap, devId) wc_XmssKey_InitLabel(key, label, heap, devId) Both validate length bounds, delegate the rest of init to wc_LmsKey_Init / wc_XmssKey_Init, then copy id/label into the key. Verified to build and pass testwolfcrypt under: --enable-lms --enable-xmss --enable-cryptocb (default and with CPPFLAGS=-DWOLF_PRIVATE_KEY_ID) --enable-lms --enable-xmss --enable-cryptocb https://claude.ai/code/session_01MixzJP9kPWkS8bhfDDDBnX
1 parent 07f5f32 commit b019052

6 files changed

Lines changed: 179 additions & 0 deletions

File tree

wolfcrypt/src/wc_lms.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,72 @@ int wc_LmsKey_Init(LmsKey* key, void* heap, int devId)
715715
return ret;
716716
}
717717

718+
#ifdef WOLF_PRIVATE_KEY_ID
719+
/* Initialize an LmsKey and bind it to a device-side key identifier.
720+
*
721+
* @param [in,out] key LmsKey to initialize.
722+
* @param [in] id Identifier bytes (may be NULL when len is 0).
723+
* @param [in] len Length of id; must be in [0, LMS_MAX_ID_LEN].
724+
* @param [in] heap Heap hint forwarded to wc_LmsKey_Init.
725+
* @param [in] devId Device identifier.
726+
*
727+
* @return 0 on success.
728+
* @return BAD_FUNC_ARG when key is NULL.
729+
* @return BUFFER_E when len is negative or exceeds LMS_MAX_ID_LEN.
730+
*/
731+
int wc_LmsKey_InitId(LmsKey* key, const unsigned char* id, int len, void* heap,
732+
int devId)
733+
{
734+
int ret = 0;
735+
736+
if (key == NULL)
737+
ret = BAD_FUNC_ARG;
738+
if (ret == 0 && (len < 0 || len > LMS_MAX_ID_LEN))
739+
ret = BUFFER_E;
740+
if (ret == 0)
741+
ret = wc_LmsKey_Init(key, heap, devId);
742+
if (ret == 0 && id != NULL && len != 0) {
743+
XMEMCPY(key->id, id, (size_t)len);
744+
key->idLen = len;
745+
}
746+
747+
return ret;
748+
}
749+
750+
/* Initialize an LmsKey and bind it to a device-side key label.
751+
*
752+
* @param [in,out] key LmsKey to initialize.
753+
* @param [in] label NUL-terminated label string (must be non-empty).
754+
* @param [in] heap Heap hint forwarded to wc_LmsKey_Init.
755+
* @param [in] devId Device identifier.
756+
*
757+
* @return 0 on success.
758+
* @return BAD_FUNC_ARG when key or label is NULL.
759+
* @return BUFFER_E when label is empty or longer than LMS_MAX_LABEL_LEN.
760+
*/
761+
int wc_LmsKey_InitLabel(LmsKey* key, const char* label, void* heap, int devId)
762+
{
763+
int ret = 0;
764+
int labelLen = 0;
765+
766+
if (key == NULL || label == NULL)
767+
ret = BAD_FUNC_ARG;
768+
if (ret == 0) {
769+
labelLen = (int)XSTRLEN(label);
770+
if (labelLen == 0 || labelLen > LMS_MAX_LABEL_LEN)
771+
ret = BUFFER_E;
772+
}
773+
if (ret == 0)
774+
ret = wc_LmsKey_Init(key, heap, devId);
775+
if (ret == 0) {
776+
XMEMCPY(key->label, label, (size_t)labelLen);
777+
key->labelLen = labelLen;
778+
}
779+
780+
return ret;
781+
}
782+
#endif /* WOLF_PRIVATE_KEY_ID */
783+
718784
/* Get the string representation of the LMS parameter set.
719785
*
720786
* @param [in] lmsParm LMS parameter set identifier.

wolfcrypt/src/wc_xmss.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,73 @@ int wc_XmssKey_Init(XmssKey* key, void* heap, int devId)
927927
return ret;
928928
}
929929

930+
#ifdef WOLF_PRIVATE_KEY_ID
931+
/* Initialize an XmssKey and bind it to a device-side key identifier.
932+
*
933+
* @param [in,out] key XmssKey to initialize.
934+
* @param [in] id Identifier bytes (may be NULL when len is 0).
935+
* @param [in] len Length of id; must be in [0, XMSS_MAX_ID_LEN].
936+
* @param [in] heap Heap hint forwarded to wc_XmssKey_Init.
937+
* @param [in] devId Device identifier.
938+
*
939+
* @return 0 on success.
940+
* @return BAD_FUNC_ARG when key is NULL.
941+
* @return BUFFER_E when len is negative or exceeds XMSS_MAX_ID_LEN.
942+
*/
943+
int wc_XmssKey_InitId(XmssKey* key, const unsigned char* id, int len,
944+
void* heap, int devId)
945+
{
946+
int ret = 0;
947+
948+
if (key == NULL)
949+
ret = BAD_FUNC_ARG;
950+
if (ret == 0 && (len < 0 || len > XMSS_MAX_ID_LEN))
951+
ret = BUFFER_E;
952+
if (ret == 0)
953+
ret = wc_XmssKey_Init(key, heap, devId);
954+
if (ret == 0 && id != NULL && len != 0) {
955+
XMEMCPY(key->id, id, (size_t)len);
956+
key->idLen = len;
957+
}
958+
959+
return ret;
960+
}
961+
962+
/* Initialize an XmssKey and bind it to a device-side key label.
963+
*
964+
* @param [in,out] key XmssKey to initialize.
965+
* @param [in] label NUL-terminated label string (must be non-empty).
966+
* @param [in] heap Heap hint forwarded to wc_XmssKey_Init.
967+
* @param [in] devId Device identifier.
968+
*
969+
* @return 0 on success.
970+
* @return BAD_FUNC_ARG when key or label is NULL.
971+
* @return BUFFER_E when label is empty or longer than XMSS_MAX_LABEL_LEN.
972+
*/
973+
int wc_XmssKey_InitLabel(XmssKey* key, const char* label, void* heap,
974+
int devId)
975+
{
976+
int ret = 0;
977+
int labelLen = 0;
978+
979+
if (key == NULL || label == NULL)
980+
ret = BAD_FUNC_ARG;
981+
if (ret == 0) {
982+
labelLen = (int)XSTRLEN(label);
983+
if (labelLen == 0 || labelLen > XMSS_MAX_LABEL_LEN)
984+
ret = BUFFER_E;
985+
}
986+
if (ret == 0)
987+
ret = wc_XmssKey_Init(key, heap, devId);
988+
if (ret == 0) {
989+
XMEMCPY(key->label, label, (size_t)labelLen);
990+
key->labelLen = labelLen;
991+
}
992+
993+
return ret;
994+
}
995+
#endif /* WOLF_PRIVATE_KEY_ID */
996+
930997
/* Set the XMSS key parameter string.
931998
*
932999
* The input string must be one of the supported parm set names in

wolfssl/wolfcrypt/lms.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ enum wc_LmsState {
223223
extern "C" {
224224
#endif
225225
WOLFSSL_API int wc_LmsKey_Init(LmsKey * key, void * heap, int devId);
226+
#ifdef WOLF_PRIVATE_KEY_ID
227+
WOLFSSL_API int wc_LmsKey_InitId(LmsKey * key, const unsigned char * id,
228+
int len, void * heap, int devId);
229+
WOLFSSL_API int wc_LmsKey_InitLabel(LmsKey * key, const char * label,
230+
void * heap, int devId);
231+
#endif
226232
WOLFSSL_API int wc_LmsKey_SetLmsParm(LmsKey * key, enum wc_LmsParm lmsParm);
227233
WOLFSSL_API int wc_LmsKey_SetParameters(LmsKey * key, int levels,
228234
int height, int winternitz);

wolfssl/wolfcrypt/wc_lms.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,13 @@ typedef struct HssPrivKey {
546546
#endif
547547
} HssPrivKey;
548548

549+
#ifndef LMS_MAX_ID_LEN
550+
#define LMS_MAX_ID_LEN 32
551+
#endif
552+
#ifndef LMS_MAX_LABEL_LEN
553+
#define LMS_MAX_LABEL_LEN 32
554+
#endif
555+
549556
struct LmsKey {
550557
/* Public key. */
551558
ALIGN16 byte pub[HSS_PUBLIC_KEY_LEN(LMS_MAX_NODE_LEN)];
@@ -574,6 +581,16 @@ struct LmsKey {
574581
#ifdef WOLF_CRYPTO_CB
575582
/* Device Identifier. */
576583
int devId;
584+
/* Per-device opaque context, populated by the callback. */
585+
void* devCtx;
586+
#endif
587+
#ifdef WOLF_PRIVATE_KEY_ID
588+
/* Optional device-side key identifier. */
589+
byte id[LMS_MAX_ID_LEN];
590+
int idLen;
591+
/* Optional device-side key label. */
592+
char label[LMS_MAX_LABEL_LEN];
593+
int labelLen;
577594
#endif
578595
};
579596

wolfssl/wolfcrypt/wc_xmss.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ typedef struct XmssParams {
205205
word8 bds_k;
206206
} XmssParams;
207207

208+
#ifndef XMSS_MAX_ID_LEN
209+
#define XMSS_MAX_ID_LEN 32
210+
#endif
211+
#ifndef XMSS_MAX_LABEL_LEN
212+
#define XMSS_MAX_LABEL_LEN 32
213+
#endif
214+
208215
struct XmssKey {
209216
/* Public key. */
210217
unsigned char pk[2 * WC_XMSS_MAX_N];
@@ -231,6 +238,16 @@ struct XmssKey {
231238
#ifdef WOLF_CRYPTO_CB
232239
/* Device Identifier. */
233240
int devId;
241+
/* Per-device opaque context, populated by the callback. */
242+
void* devCtx;
243+
#endif
244+
#ifdef WOLF_PRIVATE_KEY_ID
245+
/* Optional device-side key identifier. */
246+
byte id[XMSS_MAX_ID_LEN];
247+
int idLen;
248+
/* Optional device-side key label. */
249+
char label[XMSS_MAX_LABEL_LEN];
250+
int labelLen;
234251
#endif
235252
};
236253

wolfssl/wolfcrypt/xmss.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ typedef enum wc_XmssRc (*wc_xmss_read_private_key_cb)(byte* priv, word32 privSz,
170170
#endif
171171

172172
WOLFSSL_API int wc_XmssKey_Init(XmssKey* key, void* heap, int devId);
173+
#ifdef WOLF_PRIVATE_KEY_ID
174+
WOLFSSL_API int wc_XmssKey_InitId(XmssKey* key, const unsigned char* id,
175+
int len, void* heap, int devId);
176+
WOLFSSL_API int wc_XmssKey_InitLabel(XmssKey* key, const char* label,
177+
void* heap, int devId);
178+
#endif
173179
WOLFSSL_API int wc_XmssKey_SetParamStr(XmssKey* key, const char* str);
174180
#ifndef WOLFSSL_XMSS_VERIFY_ONLY
175181
WOLFSSL_API int wc_XmssKey_SetWriteCb(XmssKey* key,

0 commit comments

Comments
 (0)