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
3 changes: 3 additions & 0 deletions .github/workflows/cryptocb-only.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name: cryptocb-only Tests

# START OF COMMON SECTION
Expand Down Expand Up @@ -202,6 +202,9 @@
{"name": "only", "minutes": 6.0,
"comment": "Same coverage as the \"all\" entry above, but driven by ./configure --enable-cryptocb=only instead of a hand-written CPPFLAGS list. This is the regression test for the configure option: it must emit exactly the WOLF_CRYPTO_CB_ONLY_* set that \"all\" passes by hand, for the algorithms enabled here. The \"all\" entry deliberately stays on explicit CPPFLAGS so a bug in the configure logic cannot silently weaken both. Note the base already passes --enable-cryptocb; this entry's flags are appended after the base, so --enable-cryptocb=only wins.",
"configure": ["--enable-lms", "--enable-xmss", "--enable-cryptocb=only"]},
{"name": "keygen-sw-fallback", "minutes": 4.0,
"comment": "WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK: an id or label key whose registered device declines key generation is generated in software again instead of failing with NO_VALID_DEVID. test_wc_ecc_make_key_id_dev checks that fallback here; every other entry builds the default, which refuses. No ONLY_* strip, so software key generation is available.",
"configure": ["CPPFLAGS=-DWOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK"]},
{"name": "setkey", "minutes": 4.0,
"comment": "--enable-cryptocbutils enables all cryptocb utilities (COPY/FREE/SETKEY/EXPORT_KEY/KEYSTORE); WOLF_CRYPTO_CB_SETKEY is what lets test_wc_CryptoCb_TLS_CBC_HMAC run -- a TLS 1.2 CBC MtE handshake whose record HMAC is computed by a key-owning callback, the only config exercising the TLS_hmac update/final routing. No ONLY_* strip, so the device's software HMAC keeps its providers.",
"configure": ["--enable-cryptocbutils"]}
Expand Down
1 change: 1 addition & 0 deletions .wolfssl_known_macro_extras
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,7 @@ WOLFSSL_nRF5x_SDK_15_2
WOLF_ALLOW_BUILTIN
WOLF_CONF_ASN_TIME
WOLF_CRYPTO_CB_ASYNC_POLL
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK
WOLF_CRYPTO_CB_ONLY_SHA
WOLF_CRYPTO_CB_SHAKE_XOF
WOLF_CRYPTO_DEV
Expand Down
12 changes: 12 additions & 0 deletions doc/dox_comments/header_files/ecc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
ecc key
\return MP_MEM may be returned if there is an error while computing the
ecc key
\return NO_VALID_DEVID if the key was initialized with an id or label and
its devId, or the device a WOLF_CRYPTO_CB_FIND callback picks for it, is a
registered device that did not generate it, unless
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK is defined.

\param rng pointer to an initialized RNG object with which to generate
the key
Expand Down Expand Up @@ -85,6 +89,10 @@ int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key);
ecc key
\return MP_MEM may be returned if there is an error while computing the
ecc key
\return NO_VALID_DEVID if the key was initialized with an id or label and
its devId, or the device a WOLF_CRYPTO_CB_FIND callback picks for it, is a
registered device that did not generate it, unless
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK is defined.

\param key Pointer to store the created key.
\param keysize size of key to be created in bytes, set based on curveId
Expand Down Expand Up @@ -2465,6 +2473,10 @@ const char* wc_ecc_get_name(int curve_id);
\brief Makes ECC key with extended options.

\return 0 on success
\return NO_VALID_DEVID if the key was initialized with an id or label and
its devId, or the device a WOLF_CRYPTO_CB_FIND callback picks for it, is a
registered device that did not generate it, unless
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK is defined.
\return negative on error

\param rng Random number generator
Expand Down
4 changes: 4 additions & 0 deletions doc/dox_comments/header_files/ed25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ int wc_ed25519_make_public(ed25519_key* key, unsigned char* pubKey,
specified key size is not 32 bytes (Ed25519 has 32 byte keys).
\return MEMORY_E Returned if there is an error allocating memory
during function execution.
\return NO_VALID_DEVID if the key was initialized with an id or label and
its devId, or the device a WOLF_CRYPTO_CB_FIND callback picks for it, is a
registered device that did not generate it, unless
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK is defined.

\param [in] rng Pointer to an initialized RNG object with which to
generate the key.
Expand Down
4 changes: 4 additions & 0 deletions doc/dox_comments/header_files/ed448.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ int wc_ed448_make_public(ed448_key* key, unsigned char* pubKey,
specified key size is not 57 bytes (Ed448 has 57 byte keys).
\return MEMORY_E Returned if there is an error allocating memory
during function execution.
\return NO_VALID_DEVID if the key was initialized with an id or label and
its devId, or the device a WOLF_CRYPTO_CB_FIND callback picks for it, is a
registered device that did not generate it, unless
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK is defined.

\param [in] rng Pointer to an initialized RNG object with which to
generate the key.
Expand Down
4 changes: 4 additions & 0 deletions doc/dox_comments/header_files/falcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ void wc_falcon_free(falcon_key* key);
\return 0 on success.
\return BAD_FUNC_ARG if key or rng is NULL or the level is unset.
\return MEMORY_E on allocation failure.
\return NO_VALID_DEVID if the key was initialized with an id or label and
its devId, or the device a WOLF_CRYPTO_CB_FIND callback picks for it, is a
registered device that did not generate it, unless
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK is defined.

\param [in,out] key Pointer to an initialized falcon_key with a level set.
\param [in] rng Pointer to an initialized WC_RNG.
Expand Down
4 changes: 4 additions & 0 deletions doc/dox_comments/header_files/rsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,10 @@ int wc_RsaKeyToPublicDer_ex(RsaKey* key, byte* output, word32 outLen,
library used while generating the RSA key
\return MP_ZERO_E May be returned if there is an error in the math
library used while generating the RSA key
\return NO_VALID_DEVID if the key was initialized with an id or label and
its devId, or the device a WOLF_CRYPTO_CB_FIND callback picks for it, is a
registered device that did not generate it, unless
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK is defined.

\param key pointer to the RsaKey structure in which to store the
generated private key
Expand Down
4 changes: 4 additions & 0 deletions doc/dox_comments/header_files/wc_lms.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ int wc_LmsKey_SetContext(LmsKey* key, void* context);
\return 0 on success.
\return BAD_FUNC_ARG if any required pointer is NULL.
\return MEMORY_E on allocation failure.
\return NO_VALID_DEVID if the key was initialized with an id or label and
its devId, or the device a WOLF_CRYPTO_CB_FIND callback picks for it, is a
registered device that did not generate it, unless
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK is defined.

\param [in,out] key Pointer to an LmsKey in state
WC_LMS_STATE_PARMSET with callbacks set.
Expand Down
4 changes: 4 additions & 0 deletions doc/dox_comments/header_files/wc_mldsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ void wc_MlDsaKey_Free(wc_MlDsaKey* key);
\return 0 on success.
\return BAD_FUNC_ARG if key or rng is NULL.
\return MEMORY_E on allocation failure.
\return NO_VALID_DEVID if the key was initialized with an id or label and
its devId, or the device a WOLF_CRYPTO_CB_FIND callback picks for it, is a
registered device that did not generate it, unless
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK is defined.

\param [in,out] key Pointer to a wc_MlDsaKey with parameters set.
\param [in] rng Pointer to an initialized WC_RNG.
Expand Down
4 changes: 4 additions & 0 deletions doc/dox_comments/header_files/wc_mlkem.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ int wc_MlKemKey_Init_Label(MlKemKey* key, int type, const char* label,
\return 0 on success.
\return BAD_FUNC_ARG if key or rng is NULL.
\return MEMORY_E on allocation failure.
\return NO_VALID_DEVID if the key was initialized with an id or label and
its devId, or the device a WOLF_CRYPTO_CB_FIND callback picks for it, is a
registered device that did not generate it, unless
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK is defined.

\param [in,out] key Pointer to an initialized MlKemKey.
\param [in] rng Pointer to an initialized WC_RNG.
Expand Down
4 changes: 4 additions & 0 deletions doc/dox_comments/header_files/wc_slhdsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ void wc_SlhDsaKey_Free(SlhDsaKey* key);

\return 0 on success.
\return BAD_FUNC_ARG if key or rng is NULL, or key is not initialized.
\return NO_VALID_DEVID if the key was initialized with an id or label and
its devId, or the device a WOLF_CRYPTO_CB_FIND callback picks for it, is a
registered device that did not generate it, unless
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK is defined.

\param [in,out] key Pointer to an initialized SlhDsaKey.
\param [in] rng Pointer to an initialized WC_RNG.
Expand Down
4 changes: 4 additions & 0 deletions doc/dox_comments/header_files/wc_xmss.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ int wc_XmssKey_SetContext(XmssKey* key, void* context);
\return 0 on success.
\return BAD_FUNC_ARG if any required pointer is NULL.
\return MEMORY_E on allocation failure.
\return NO_VALID_DEVID if the key was initialized with an id or label and
its devId, or the device a WOLF_CRYPTO_CB_FIND callback picks for it, is a
registered device that did not generate it, unless
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK is defined.

\param [in,out] key Pointer to an XmssKey in state
WC_XMSS_STATE_PARMSET with callbacks set.
Expand Down
16 changes: 4 additions & 12 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -36372,15 +36372,7 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
#endif /* ifdef WOLF_CRYPTO_CB_RSA_PAD */
#endif /* !NO_RSA */
#ifdef HAVE_ECC
if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
/* mark this key as ephemeral */
if (info->pk.eckg.key != NULL) {
XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
sizeof(info->pk.eckg.key->label));
info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
}
}
else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
ecc_key key;

/* perform software based ECC sign */
Expand All @@ -36389,11 +36381,11 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
#endif

if (info->pk.eccsign.key != NULL &&
XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
/* this is an empheral key */
info->pk.eccsign.key->type == ECC_PRIVATEKEY) {
/* skip keys generated in software */
#ifdef DEBUG_WOLFSSL
fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
"ephemeral key\n");
"software key\n");
#endif
return CRYPTOCB_UNAVAILABLE;
}
Expand Down
193 changes: 193 additions & 0 deletions tests/api/test_ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4819,3 +4819,196 @@ int test_wc_EccPublicKeyDecode_specifiedOverflow(void)
#endif
return EXPECT_RESULT();
} /* END test_wc_EccPublicKeyDecode_specifiedOverflow */

#if defined(HAVE_ECC) && defined(WOLF_CRYPTO_CB) && \
defined(WOLF_PRIVATE_KEY_ID) && !defined(WC_NO_RNG) && \
!defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
(!defined(ECC_MIN_KEY_SZ) || (ECC_MIN_KEY_SZ <= 256)) && \
!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
#define TEST_ECC_KEYGEN_ID
#endif

#ifdef TEST_ECC_KEYGEN_ID
#define ECC_KEYGEN_ID_DEVID 2401

static int ecc_keygen_decline_cb(int devIdArg, wc_CryptoInfo* info, void* ctx)
{
(void)devIdArg;
(void)info;
(void)ctx;
return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
}

#ifdef WC_CRYPTOCB_KEYGEN_ID_CHECK
static int ecc_keygen_clear_cb(int devIdArg, wc_CryptoInfo* info, void* ctx)
{
(void)devIdArg;
(void)ctx;

if ((info->algo_type == WC_ALGO_TYPE_PK) &&
(info->pk.type == WC_PK_TYPE_EC_KEYGEN)) {
info->pk.eckg.key->devId = INVALID_DEVID;
}
return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
}

#ifdef WOLF_CRYPTO_CB_FIND
#ifdef WOLFSSL_SWDEV
#include <tests/swdev/swdev_loader.h>
#endif
static int ecc_keygen_find_cb(int devIdArg, int algoType)
{
if ((devIdArg == INVALID_DEVID) && (algoType == WC_ALGO_TYPE_PK))
return ECC_KEYGEN_ID_DEVID;
return devIdArg;
}
#endif

#if defined(HAVE_ECC_DHE) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
!defined(WOLFSSL_ASYNC_CRYPT)
/* Generates in software on the caller's key, as many device callbacks do. */
static int ecc_keygen_sw_cb(int devIdArg, wc_CryptoInfo* info, void* ctx)
{
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);

(void)ctx;

if ((info->algo_type == WC_ALGO_TYPE_PK) &&
(info->pk.type == WC_PK_TYPE_EC_KEYGEN)) {
info->pk.eckg.key->devId = INVALID_DEVID;
ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
info->pk.eckg.key, info->pk.eckg.curveId);
info->pk.eckg.key->devId = devIdArg;
}
return ret;
}
#endif
#endif /* WC_CRYPTOCB_KEYGEN_ID_CHECK */
#endif /* TEST_ECC_KEYGEN_ID */

/*
* Testing wc_ecc_make_key() on an id or label key whose device declines keygen.
*/
int test_wc_ecc_make_key_id_dev(void)
{
EXPECT_DECLS;
#if defined(TEST_ECC_KEYGEN_ID) && defined(WC_CRYPTOCB_KEYGEN_ID_CHECK)
int devId = ECC_KEYGEN_ID_DEVID;
#ifndef WOLF_CRYPTO_CB_ONLY_ECC
int ret;
#endif
ecc_key key;
WC_RNG rng;
byte id[] = { 0x01, 0x02, 0x03, 0x04 };

XMEMSET(&key, 0, sizeof(key));
XMEMSET(&rng, 0, sizeof(rng));
ExpectIntEQ(wc_InitRng(&rng), 0);
ExpectIntEQ(wc_CryptoCb_RegisterDevice(devId, ecc_keygen_decline_cb, NULL),
0);

ExpectIntEQ(wc_ecc_init_id(&key, id, (int)sizeof(id), NULL, devId), 0);
ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key),
WC_NO_ERR_TRACE(NO_VALID_DEVID));
wc_ecc_free(&key);

ExpectIntEQ(wc_ecc_init_label(&key, "dev-key", NULL, devId), 0);
ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key),
WC_NO_ERR_TRACE(NO_VALID_DEVID));
wc_ecc_free(&key);

ExpectIntEQ(wc_CryptoCb_RegisterDevice(devId + 300, ecc_keygen_clear_cb,
NULL), 0);
ExpectIntEQ(wc_ecc_init_id(&key, id, (int)sizeof(id), NULL, devId + 300),
0);
ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key),
WC_NO_ERR_TRACE(NO_VALID_DEVID));
wc_ecc_free(&key);
wc_CryptoCb_UnRegisterDevice(devId + 300);

#if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
/* Without a registered device the key stays in software. */
ExpectIntEQ(wc_ecc_init_id(&key, id, (int)sizeof(id), NULL, devId + 100),
0);
ret = wc_ecc_make_key(&rng, 32, &key);
#if defined(WOLFSSL_ASYNC_CRYPT)
ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
#endif
ExpectIntEQ(ret, 0);
wc_ecc_free(&key);

ExpectIntEQ(wc_ecc_init_ex(&key, NULL, devId), 0);
ret = wc_ecc_make_key(&rng, 32, &key);
#if defined(WOLFSSL_ASYNC_CRYPT)
ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
#endif
ExpectIntEQ(ret, 0);
wc_ecc_free(&key);
#endif

#ifdef WOLF_CRYPTO_CB_FIND
wc_CryptoCb_SetDeviceFindCb(ecc_keygen_find_cb);
ExpectIntEQ(wc_ecc_init_id(&key, id, (int)sizeof(id), NULL,
INVALID_DEVID), 0);
ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key),
WC_NO_ERR_TRACE(NO_VALID_DEVID));
wc_ecc_free(&key);
#ifdef WOLFSSL_SWDEV
wc_CryptoCb_SetDeviceFindCb(wc_SwDev_FindCb);
#else
wc_CryptoCb_SetDeviceFindCb(NULL);
#endif
#endif

#if defined(HAVE_ECC_DHE) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
!defined(WOLFSSL_ASYNC_CRYPT)
ExpectIntEQ(wc_CryptoCb_RegisterDevice(devId + 200, ecc_keygen_sw_cb,
NULL), 0);
ExpectIntEQ(wc_ecc_init_id(&key, id, (int)sizeof(id), NULL, devId + 200),
0);
ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
wc_ecc_free(&key);
wc_CryptoCb_UnRegisterDevice(devId + 200);
#endif

wc_CryptoCb_UnRegisterDevice(devId);
DoExpectIntEQ(wc_FreeRng(&rng), 0);
#elif defined(TEST_ECC_KEYGEN_ID)
int devId = ECC_KEYGEN_ID_DEVID;
int ret;
ecc_key key;
WC_RNG rng;
byte id[] = { 0x01, 0x02, 0x03, 0x04 };
#ifdef WOLF_CRYPTO_CB_ONLY_ECC
int expRet = WC_NO_ERR_TRACE(NO_VALID_DEVID);
#else
int expRet = 0;
#endif

XMEMSET(&key, 0, sizeof(key));
XMEMSET(&rng, 0, sizeof(rng));
ExpectIntEQ(wc_InitRng(&rng), 0);
ExpectIntEQ(wc_CryptoCb_RegisterDevice(devId, ecc_keygen_decline_cb, NULL),
0);

ExpectIntEQ(wc_ecc_init_id(&key, id, (int)sizeof(id), NULL, devId), 0);
ret = wc_ecc_make_key(&rng, 32, &key);
#if defined(WOLFSSL_ASYNC_CRYPT)
ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
#endif
ExpectIntEQ(ret, expRet);
wc_ecc_free(&key);

ExpectIntEQ(wc_ecc_init_label(&key, "dev-key", NULL, devId), 0);
ret = wc_ecc_make_key(&rng, 32, &key);
#if defined(WOLFSSL_ASYNC_CRYPT)
ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
#endif
ExpectIntEQ(ret, expRet);
wc_ecc_free(&key);

wc_CryptoCb_UnRegisterDevice(devId);
DoExpectIntEQ(wc_FreeRng(&rng), 0);
#endif
return EXPECT_RESULT();
}
4 changes: 3 additions & 1 deletion tests/api/test_ecc.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ int test_wc_EccDecisionCoverage3(void);
int test_wc_EccDecisionCoverage4(void);
int test_wc_EccPublicKeyDecode_explicit_curve_size(void);
int test_wc_EccPublicKeyDecode_specifiedOverflow(void);
int test_wc_ecc_make_key_id_dev(void);

#define TEST_ECC_DECLS \
TEST_DECL_GROUP("ecc", test_wc_ecc_get_curve_size_from_name), \
Expand Down Expand Up @@ -140,6 +141,7 @@ int test_wc_EccPublicKeyDecode_specifiedOverflow(void);
TEST_DECL_GROUP("ecc", test_wc_EccDecisionCoverage4), \
TEST_DECL_GROUP("ecc", \
test_wc_EccPublicKeyDecode_explicit_curve_size), \
TEST_DECL_GROUP("ecc", test_wc_EccPublicKeyDecode_specifiedOverflow)
TEST_DECL_GROUP("ecc", test_wc_EccPublicKeyDecode_specifiedOverflow), \
TEST_DECL_GROUP("ecc", test_wc_ecc_make_key_id_dev)

#endif /* WOLFCRYPT_TEST_ECC_H */
Loading
Loading