Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions .github/workflows/build-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ on:
required: false
type: string
default: ''
wolfssl_ref:
required: false
type: string
default: ''
wolfssl_base_config:
required: false
type: string
default: >-
--enable-cryptocb --enable-aescfb --enable-rsapss --enable-keygen
--enable-pwdbased --enable-scrypt --enable-md5 --enable-mldsa
--enable-lms=verify-only --enable-xmss=verify-only

jobs:
build:
Expand All @@ -36,15 +47,15 @@ jobs:
- uses: actions/checkout@v4
with:
repository: wolfssl/wolfssl
ref: ${{ inputs.wolfssl_ref }}
path: wolfssl
- name: wolfssl autogen
working-directory: ./wolfssl
run: ./autogen.sh
- name: wolfssl configure
working-directory: ./wolfssl
run: |
./configure --enable-cryptocb --enable-aescfb --enable-rsapss --enable-keygen --enable-pwdbased --enable-scrypt --enable-md5 \
--enable-mldsa --enable-lms=verify-only --enable-xmss=verify-only ${{inputs.wolfssl_config}} \
./configure ${{inputs.wolfssl_base_config}} ${{inputs.wolfssl_config}} \
C_EXTRA_FLAGS="-DWOLFSSL_PUBLIC_MP -DWC_RSA_DIRECT -DHAVE_AES_ECB -DHAVE_AES_KEYWRAP ${{inputs.wolfssl_cflags}}"
- name: wolfssl make
working-directory: ./wolfssl
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,48 @@ jobs:
uses: ./.github/workflows/build-workflow.yml
with:
config: --disable-aescbc
default_devid_no_store:
uses: ./.github/workflows/build-workflow.yml
with:
wolfssl_config: --enable-cmac
config: >-
--enable-singlethreaded --enable-pbkdf2 --enable-aescmac
--with-pbkdf2-iterations=25
CFLAGS="-DWOLFPKCS11_NO_STORE -DWOLFPKCS11_DEFAULT_DEVID=7
-DWOLFPKCS11_PIN_KDF_SOFTWARE"
check: >-
make check &&
WOLFPKCS11_DEFAULT_DEVID_REQUIRED=1 ./tests/default_devid_pin_kdf_test
default_devid_device_pin_kdf:
uses: ./.github/workflows/build-workflow.yml
with:
wolfssl_config: --enable-cmac
config: >-
--enable-pbkdf2 --enable-aescmac
--enable-nss --with-pbkdf2-iterations=25
CFLAGS="-DWOLFPKCS11_NO_STORE -DWOLFPKCS11_DEFAULT_DEVID=7"
check: >-
make check &&
WOLFPKCS11_DEFAULT_DEVID_REQUIRED=1 ./tests/default_devid_pin_kdf_test
default_devid_hkdf_two_step:
uses: ./.github/workflows/build-workflow.yml
with:
wolfssl_ref: v5.8.2-stable
wolfssl_base_config: >-
--enable-cryptocb --enable-aescfb --enable-rsapss --enable-keygen
--enable-pwdbased --enable-scrypt --enable-md5
wolfssl_config: --enable-cmac
config: >-
--enable-pbkdf2 --enable-aescmac --with-pbkdf2-iterations=25
CFLAGS="-DWOLFPKCS11_NO_STORE -DWOLFPKCS11_DEFAULT_DEVID=7"
check: >-
make check &&
WOLFPKCS11_DEFAULT_DEVID_REQUIRED=1 ./tests/default_devid_pin_kdf_test
default_devid_guards:
uses: ./.github/workflows/build-workflow.yml
with:
config: --enable-pbkdf2
check: ./tests/default_devid_guard_test.sh
no_aesgcm:
uses: ./.github/workflows/build-workflow.yml
with:
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,28 @@ cmake -DCMAKE_PREFIX_PATH=/path/to/wolfssl/install ..
| `BUILD_SHARED_LIBS` | `ON` | Build shared (`ON`) or static (`OFF`) library |
| `WOLFPKCS11_STATIC_WOLFSSL` | `no` | Link a static wolfSSL into the shared library and hide its symbols |

Two related preprocessor settings are available when wolfCrypt callbacks are
used. `WOLFPKCS11_DEFAULT_DEVID` sets the device ID assigned to a slot before
an object-specific device ID is selected; it cannot be combined with the TPM,
MAXQ10XX or STM32U5 DHUK backends, which set the slot's device ID themselves.
Key operations use the object's device ID, which is the slot's unless the
object sets `CKA_WOLFSSL_DEVID`. HKDF uses the base key's device ID on
wolfSSL 5.7.0 or later, except FIPS builds before v7 and self-test builds.
Those builds pass no device ID, which selects software unless a
`WOLF_CRYPTO_CB_FIND` finder remaps it. PBKDF2 key generation has no base key,
so it uses the slot's device ID. Device callbacks must complete synchronously;
an operation whose callback returns `WC_PENDING_E` fails.
`WOLFPKCS11_PIN_KDF_SOFTWARE` keeps PBKDF2 PIN hashing in software while key
operations continue to use the device. This is useful when a device callback
is intended for key operations but is too expensive for each PBKDF2
iteration. With crypto callbacks enabled it needs wolfSSL 5.7.4 or later,
because older SHA-256 init replaced an invalid device ID with the default
registered device. It cannot be combined with `WOLF_CRYPTO_CB_FIND`, because
a finder can remap an invalid device ID back to a registered callback. The
device must be registered with
the same wolfSSL instance that wolfPKCS11 uses, so an application cannot
register it for the self-contained library with a private static wolfSSL.

Note: wolfSSL must be built with the corresponding features enabled for the
options above to work (e.g. enabling `WOLFPKCS11_AESCCM` requires wolfSSL built
with `-DWOLFSSL_AESCCM=yes`).
Expand Down
2 changes: 1 addition & 1 deletion src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -10116,7 +10116,7 @@ static CK_RV wp11_C_GenerateKey(CK_SESSION_HANDLE hSession,
(int)params->ulSaltSourceDataLen,
(int)params->iterations,
(int)derivedKeyLen,
hashType);
hashType, session);

if (ret != 0) {
wc_ForceZero(derivedKey, derivedKeyLen);
Expand Down
115 changes: 96 additions & 19 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
#include "wp11_types.h"
#include "hbs.h"

#if defined(WOLFPKCS11_PIN_KDF_SOFTWARE) && defined(WOLFPKCS11_PBKDF2)
#if defined(WOLF_CRYPTO_CB_FIND)
#error WOLFPKCS11_PIN_KDF_SOFTWARE conflicts with WOLF_CRYPTO_CB_FIND
#elif defined(WOLF_CRYPTO_CB) && LIBWOLFSSL_VERSION_HEX < 0x05007004
/* Older SHA-256 init replaces INVALID_DEVID with the default device */
#error WOLFPKCS11_PIN_KDF_SOFTWARE requires wolfSSL 5.7.4 or later
#endif
#endif
#if defined(WOLFPKCS11_DEFAULT_DEVID) && (defined(WOLFPKCS11_TPM) || \
defined(WOLFSSL_MAXQ10XX_CRYPTO) || defined(WOLFSSL_STM32U5_DHUK))
#error WOLFPKCS11_DEFAULT_DEVID conflicts with a TPM, MAXQ or STM32U5 slot
#endif

#ifdef WOLFPKCS11_TPM
/* Hoisted: the store section's forward declaration is compiled out under
* WOLFPKCS11_NO_STORE, but the wrap path below still calls it. */
Expand Down Expand Up @@ -8671,6 +8684,10 @@ static int wp11_Slot_Init(WP11_Slot* slot, int id)
if (ret == 0) {
#if defined(WOLFPKCS11_TPM)
ret = wp11_TpmInit(slot);
#elif defined(WOLFPKCS11_DEFAULT_DEVID)
/* Let embedded integrations route slot crypto through a device
* registered with wolfCrypt without enabling a TPM-specific backend. */
slot->devId = WOLFPKCS11_DEFAULT_DEVID;
Comment thread
Frauschi marked this conversation as resolved.
Resolved
#elif defined (WOLFSSL_MAXQ10XX_CRYPTO)
slot->devId = MAXQ_DEVICE_ID;
#elif defined (WOLFSSL_STM32U5_DHUK)
Expand Down Expand Up @@ -9218,6 +9235,18 @@ static int HashPIN(char* pin, int pinLen, byte* seed, int seedLen, byte* hash,
int hashLen, WP11_Slot* slot)
{
#ifdef WOLFPKCS11_PBKDF2
int devId;

/* A provider whose regular key operations use a crypto device may keep
* the high-iteration PIN KDF local. This avoids turning one token login
* into hundreds of thousands of device transactions while preserving
* the configured PBKDF2 work factor. */
#ifdef WOLFPKCS11_PIN_KDF_SOFTWARE
(void)slot;
devId = INVALID_DEVID;
#else
devId = slot->devId;
#endif
#if defined(HAVE_FIPS) && defined(WOLFPKCS11_NSS)
if (pinLen == 0) {
/* For FIPS, use empty pin of HMAC_FIPS_MIN_KEY bytes when pinLen is 0.
Expand All @@ -9228,11 +9257,11 @@ static int HashPIN(char* pin, int pinLen, byte* seed, int seedLen, byte* hash,
byte emptyPin[HMAC_FIPS_MIN_KEY];
XMEMSET(emptyPin, 0, sizeof(emptyPin));
return wc_PBKDF2_ex(hash, emptyPin, sizeof(emptyPin), seed, seedLen,
PBKDF2_ITERATIONS, hashLen, WC_SHA256, NULL, slot->devId);
PBKDF2_ITERATIONS, hashLen, WC_SHA256, NULL, devId);
}
#endif
return wc_PBKDF2_ex(hash, (byte*)pin, pinLen, seed, seedLen,
PBKDF2_ITERATIONS, hashLen, WC_SHA256, NULL, slot->devId);
PBKDF2_ITERATIONS, hashLen, WC_SHA256, NULL, devId);
#elif defined(HAVE_SCRYPT)
/* Convert PIN into secret using scrypt algorithm. */
(void)slot;
Expand Down Expand Up @@ -18871,10 +18900,23 @@ int WP11_GenerateRandomKey(WP11_Object* secret, WP11_Slot* slot,
* 0 on success
*/

#if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(7,0)) && \
!defined(HAVE_SELFTEST) && defined(LIBWOLFSSL_VERSION_HEX) && \
LIBWOLFSSL_VERSION_HEX >= 0x05007000
#define WP11_HAVE_HKDF_DEVID
#if LIBWOLFSSL_VERSION_HEX < 0x05008004
/* wc_HKDF_ex() arrived in 5.8.4; earlier releases have the parts */
#define WP11_HKDF_TWO_STEP
#endif
#endif

int WP11_KDF_Derive(WP11_Session* session, CK_HKDF_PARAMS_PTR params,
unsigned char* key, word32* keyLen, WP11_Object* priv)
{
int ret = 0;
#ifdef WP11_HKDF_TWO_STEP
byte prk[WC_MAX_DIGEST_SIZE];
#endif
byte* privData = NULL;
word32 privLen = 0;
byte* salt = NULL;
Expand Down Expand Up @@ -18955,6 +18997,37 @@ int WP11_KDF_Derive(WP11_Session* session, CK_HKDF_PARAMS_PTR params,
privData = priv->data.symmKey->data;
privLen = priv->data.symmKey->len;
}
#ifdef WP11_HAVE_HKDF_DEVID
if (ret == 0 && params->bExtract && !params->bExpand) {
ret = wc_HKDF_Extract_ex(hashType, salt, (word32)saltLen,
privData, privLen, key, NULL, priv->devId);

if (!ret)
*keyLen = hashLen;
}
else if (ret == 0 && !params->bExtract && params->bExpand) {
ret = wc_HKDF_Expand_ex(hashType, privData, privLen,
params->pInfo, (word32)params->ulInfoLen,
key, *keyLen, NULL, priv->devId);
}
else if (ret == 0) {
/* Both */
#ifdef WP11_HKDF_TWO_STEP
ret = wc_HKDF_Extract_ex(hashType, salt, (word32)saltLen,
privData, privLen, prk, NULL, priv->devId);
if (ret == 0) {
ret = wc_HKDF_Expand_ex(hashType, prk, hashLen,
params->pInfo, (word32)params->ulInfoLen,
key, *keyLen, NULL, priv->devId);
}
wc_ForceZero(prk, sizeof(prk));
#else
ret = wc_HKDF_ex(hashType, privData, privLen, salt,
(word32)saltLen, params->pInfo, (word32)params->ulInfoLen,
key, *keyLen, NULL, priv->devId);
#endif
}
#else
if (ret == 0 && params->bExtract && !params->bExpand) {
ret = wc_HKDF_Extract(hashType, salt, (word32)saltLen,
privData, privLen, key);
Expand All @@ -18972,6 +19045,7 @@ int WP11_KDF_Derive(WP11_Session* session, CK_HKDF_PARAMS_PTR params,
ret = wc_HKDF(hashType, privData, privLen, salt, (word32)saltLen,
params->pInfo, (word32)params->ulInfoLen, key, *keyLen);
}
#endif
PRIVATE_KEY_LOCK();
if (secondLock != NULL)
WP11_Lock_UnlockRO(secondLock);
Expand Down Expand Up @@ -19503,7 +19577,7 @@ int WP11_Tls12_Master_Key_Derive(CK_SSL3_RANDOM_DATA* random,
ret = wc_PRF_TLS(enc, (word32)encLen, key->data.symmKey->data,
key->data.symmKey->len, (const byte*)label,
(word32)ulLabelLen, pSeed, (word32)ulSeedLen,
1, macType, NULL, 0);
1, macType, NULL, key->devId);
Comment thread
Frauschi marked this conversation as resolved.
Resolved
PRIVATE_KEY_LOCK();
}
if (key->lock != NULL)
Expand Down Expand Up @@ -19683,7 +19757,8 @@ int WP11_Nss_Tls12_Master_Key_Derive(CK_BYTE_PTR pSessionHash,
ret = wc_PRF_TLS(enc, (word32)encLen, key->data.symmKey->data,
key->data.symmKey->len, (const byte*)label,
(word32)ulLabelLen, pSessionHash,
(word32)ulSessionHashLen, 1, macType, NULL, 0);
(word32)ulSessionHashLen, 1, macType, NULL,
key->devId);
PRIVATE_KEY_LOCK();
}
if (key->lock != NULL)
Expand Down Expand Up @@ -21783,7 +21858,7 @@ int WP11_Aes_Cmac_Init(WP11_Object* secret, WP11_Session* session,
ret = wp11_Object_KeyStateLocked(secret);
if (ret == 0) {
ret = wc_InitCmac_ex(&cmac->cmac, key->data, key->len, WC_CMAC_AES,
NULL, NULL, secret->slot->devId);
NULL, NULL, secret->devId);
}
if (secret->lock != NULL)
WP11_Lock_UnlockRO(secret->lock);
Expand Down Expand Up @@ -22137,10 +22212,11 @@ int WP11_Digest_Single(unsigned char* data, word32 dataLen,

#ifndef NO_HMAC
int WP11_PBKDF2(byte* output, const byte* passwd, int pLen,
const byte* salt, int sLen, int iterations, int kLen, int hashType)
const byte* salt, int sLen, int iterations, int kLen, int hashType,
WP11_Session* session)
{
return wc_PBKDF2(output, passwd, pLen, salt, sLen, iterations, kLen,
hashType);
return wc_PBKDF2_ex(output, passwd, pLen, salt, sLen, iterations, kLen,
hashType, NULL, session->devId);
}
#endif

Expand Down Expand Up @@ -22242,19 +22318,20 @@ int WP11_Hmac_Init(CK_MECHANISM_TYPE mechanism, WP11_Object* secret,
if (ret == 0) {
WP11_Session_AbortOp(session);
hmac->hmacSz = wc_HmacSizeByType(hashType);
ret = wc_HmacInit(&hmac->hmac, NULL, secret->slot->devId);
}
if (ret == 0) {
if (secret->lock != NULL)
WP11_Lock_LockRO(secret->lock);
key = secret->data.symmKey;
ret = wp11_Object_KeyStateLocked(secret);
if (ret == 0)
ret = wc_HmacSetKey(&hmac->hmac, hashType, key->data, key->len);
ret = wc_HmacInit(&hmac->hmac, NULL, secret->devId);
if (ret == 0) {
key = secret->data.symmKey;
ret = wp11_Object_KeyStateLocked(secret);
if (ret == 0)
ret = wc_HmacSetKey(&hmac->hmac, hashType, key->data,
key->len);
if (ret != 0)
wc_HmacFree(&hmac->hmac);
}
if (secret->lock != NULL)
WP11_Lock_UnlockRO(secret->lock);
if (ret != 0)
wc_HmacFree(&hmac->hmac);
}

return ret;
Expand Down Expand Up @@ -22458,7 +22535,7 @@ int WP11_TLS_MAC_sign(byte* data, word32 dataLen, byte* sig, word32* sigLen,
if (ret == 0 && mac->isTlsPrf) {
outLen = mac->macSz;
ret = wc_PRF_TLSv1(sig, outLen, key->data, key->len, label, 15,
data, dataLen, NULL, secret->slot->devId);
data, dataLen, NULL, secret->devId);
}
else if (ret == 0) {
/* Use maximum requested size if MAC size is unspecified */
Expand All @@ -22467,7 +22544,7 @@ int WP11_TLS_MAC_sign(byte* data, word32 dataLen, byte* sig, word32* sigLen,
else
outLen = mac->macSz;
ret = wc_PRF_TLS(sig, outLen, key->data, key->len, label, 15,
data, dataLen, 1, mac->mac, NULL, secret->slot->devId);
data, dataLen, 1, mac->mac, NULL, secret->devId);
}
PRIVATE_KEY_LOCK();

Expand Down
Loading
Loading