Skip to content

Commit db220db

Browse files
committed
Add LMS/HSS key generation, signing and verification over PKCS#11
wc_Pkcs11_CryptoDevCb now handles the stateful signature callbacks for LMS keys, using the PKCS#11 HSS mechanisms. Key generation passes CKA_HSS_LEVELS with the per-level lists CKA_HSS_LMS_TYPES and CKA_HSS_LMOTS_TYPES in the private key template, as PKCS#11 3.1, 3.2 and the 3.3 draft define it; the single-value CKA_HSS_LMS_TYPE and CKA_HSS_LMOTS_TYPE are attributes the token gives the public key. The private key is a token object, private, sensitive, not extractable and not copyable, since the token keeps the one-time-signature state; the public key is kept on the token too so a later process can verify by id or label. Keys without an id or label are declined, so software LMS keys that carry a devId still sign in software, and a token key without a name would be left behind on the token. Key generation also fails with BAD_STATE_E when an HSS object, token or session, already has the key's id or label, as a later lookup by name could otherwise find the older key; with WC_PKCS11_FIND_WITH_ID_ONLY the id check covers every private and public key, whatever its type, as the lookups do, and Pkcs11FindKeyOfClass now tells a failed search apart from no match so either stops key generation. Keys are always found by id or label; no object handle is kept in devCtx, as a handle no longer names the key once the library is set up again. A key whose state software holds signs, counts its signatures and verifies in software. These declines, and every XMSS request, are decided from the key before a session is opened, so a token that cannot be opened or logged into never fails an operation software can do, and never leaves a software LMS key marked bad. Before signing or counting signatures left, the token key's CKA_HSS_LEVELS and type lists are compared with the LmsKey's parameters, and a mismatch fails with WC_KEY_MISMATCH_E before the token uses a one-time key, so SigsLeft reports 0 for a key Sign refuses. That includes a token key with more levels than the build supports, whose type lists come back CKR_BUFFER_TOO_SMALL. CKR_KEY_EXHAUSTED maps to KEY_EXHAUSTED_E; any other signing failure, including a signature whose length does not match the parameters, is reported as WC_HW_E and wolfCrypt treats the key as bad. Signatures left come from CKA_KEYS_REMAINING, the PKCS#11 3.3 name, added with CKA_HSS_KEYS_REMAINING as an alias. Verification uses the public key held by the LmsKey when it has one, falling back to software when no session opens, or the token does not take it or fails with it, and otherwise the public key named on the token. CKR_SIGNATURE_LEN_RANGE is reported as SIG_VERIFY_E like CKR_SIGNATURE_INVALID. There is no free handler, as generated private keys are always token objects and freeing a key never destroys one. wc_LmsKey_Reload and wc_XmssKey_Reload now leave a key named by id or label on the device even when a read callback is set, as key generation already does, so a named key is never reloaded in software from storage its key generation never wrote. A devId on an unnamed key with a read callback still means the caller holds the state. NO_PKCS11_LMS leaves LMS out; verify-only LMS builds only get verification. The HSS constants and CKR_BUFFER_TOO_SMALL are added to pkcs11.h.
1 parent 198b4f9 commit db220db

8 files changed

Lines changed: 1795 additions & 47 deletions

File tree

‎.wolfssl_known_macro_extras‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ NO_PKCS11_ECC
562562
NO_PKCS11_ECDH
563563
NO_PKCS11_EC_KEYGEN
564564
NO_PKCS11_HMAC
565+
NO_PKCS11_LMS
565566
NO_PKCS11_MLDSA
566567
NO_PKCS11_MLKEM
567568
NO_PKCS11_RNG

‎tests/api/test_lms_xmss.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,19 @@ int test_wc_LmsKey_reload_devid(void)
500500
ExpectNull(hsmKey.priv_data);
501501
ExpectIntEQ(wc_LmsKey_GetKid(&hsmKey, &kid, &kidSz),
502502
WC_NO_ERR_TRACE(BAD_STATE_E));
503+
#ifdef WOLF_PRIVATE_KEY_ID
504+
/* A named key stays on the device even with a read callback. */
505+
wc_LmsKey_Free(&hsmKey);
506+
XMEMSET(&hsmKey, 0, sizeof(hsmKey));
507+
ExpectIntEQ(wc_LmsKey_InitId(&hsmKey, (const unsigned char*)"lms", 3, NULL,
508+
TEST_LMS_XMSS_CRYPTOCB_DEVID), 0);
509+
ExpectIntEQ(test_lms_set_params(&hsmKey), 0);
510+
ExpectIntEQ(wc_LmsKey_SetReadCb(&hsmKey, test_lms_read_key), 0);
511+
ExpectIntEQ(wc_LmsKey_SetContext(&hsmKey, (void*)LMS_TEST_PRIV_KEY_FILE),
512+
0);
513+
ExpectIntEQ(wc_LmsKey_Reload(&hsmKey), 0);
514+
ExpectNull(hsmKey.priv_data);
515+
#endif
503516

504517
wc_LmsKey_Free(&hsmKey);
505518
wc_LmsKey_Free(&vkey);
@@ -1099,6 +1112,19 @@ int test_wc_XmssKey_reload_devid(void)
10991112
ExpectIntEQ(wc_XmssKey_SetParamStr(&hsmKey, "XMSS-SHA2_10_256"), 0);
11001113
ExpectIntEQ(wc_XmssKey_Reload(&hsmKey), 0);
11011114
ExpectNull(hsmKey.sk);
1115+
#ifdef WOLF_PRIVATE_KEY_ID
1116+
/* A named key stays on the device even with a read callback. */
1117+
wc_XmssKey_Free(&hsmKey);
1118+
XMEMSET(&hsmKey, 0, sizeof(hsmKey));
1119+
ExpectIntEQ(wc_XmssKey_InitId(&hsmKey, (const unsigned char*)"xmss", 4,
1120+
NULL, TEST_LMS_XMSS_CRYPTOCB_DEVID), 0);
1121+
ExpectIntEQ(wc_XmssKey_SetParamStr(&hsmKey, "XMSS-SHA2_10_256"), 0);
1122+
ExpectIntEQ(wc_XmssKey_SetReadCb(&hsmKey, xmss_devid_read_key), 0);
1123+
ExpectIntEQ(wc_XmssKey_SetContext(&hsmKey,
1124+
(void*)XMSS_DEVID_TEST_PRIV_KEY_FILE), 0);
1125+
ExpectIntEQ(wc_XmssKey_Reload(&hsmKey), 0);
1126+
ExpectNull(hsmKey.sk);
1127+
#endif
11021128

11031129
wc_XmssKey_Free(&hsmKey);
11041130
wc_XmssKey_Free(&vkey);

0 commit comments

Comments
 (0)