Skip to content
Open
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
5 changes: 4 additions & 1 deletion doc/dox_comments/header_files/rsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,13 @@ int wc_RsaPSS_VerifyCheck_ex(byte* in, word32 inLen,
The key has to be associated with RNG by wc_RsaSetRNG when WC_RSA_BLINDING is enabled.

\return the length of the PSS data on success and negative indicates failure.
On the crypto callback path *out is set to NULL though the return stays
positive, so callers must not dereference *out.

\param in The byte array to be decrypted.
\param inLen The length of in.
\param out The byte array for the decrypted data to be stored.
\param out The byte array for the decrypted data to be stored. Set to NULL
when a crypto callback device performed the verify (see \return).
\param digest Hash of the data that is being verified.
\param digestLen Length of hash.
\param hash The hash type to be in message
Expand Down
77 changes: 77 additions & 0 deletions tests/api/test_cmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,3 +720,80 @@ int test_wc_AesCmacVerify_CryptoCb_LenMismatch(void)
return EXPECT_RESULT();
} /* END test_wc_AesCmacVerify_CryptoCb_LenMismatch */

/* Test that wc_CmacFree() dispatches a WC_ALGO_TYPE_FREE / WC_ALGO_TYPE_CMAC
* request to a registered crypto callback (CryptoCb) device. */
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE) && \
defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
/* Spy device: declines every request (software handles the work) but counts
* CMAC free dispatches. */
static int cmac_free_test_crypto_cb(int devIdArg, wc_CryptoInfo* info, void* ctx)
{
int* freeSeen = (int*)ctx;

(void)devIdArg;

if (info == NULL) {
return BAD_FUNC_ARG;
}

if (info->algo_type == WC_ALGO_TYPE_FREE &&
info->free.algo == WC_ALGO_TYPE_CMAC) {
if (freeSeen != NULL) {
(*freeSeen)++;
}
}

return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
}
#endif

int test_wc_CryptoCb_CmacFree(void)
{
EXPECT_DECLS;
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE) && \
defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
int devId = 4460;
int freeSeen = 0;
byte key16[16] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10
};
byte in[16] = {
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00
};
byte tag[WC_AES_BLOCK_SIZE];
word32 tagSz = (word32)sizeof(tag);
WC_DECLARE_VAR(cmac, Cmac, 1, HEAP_HINT);

WC_ALLOC_VAR(cmac, Cmac, 1, HEAP_HINT);
#ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
ExpectNotNull(cmac);
#endif

ExpectIntEQ(wc_CryptoCb_RegisterDevice(devId, cmac_free_test_crypto_cb,
&freeSeen), 0);

/* wc_CmacFinal() internally frees the Cmac -> free dispatched to device. */
ExpectIntEQ(wc_InitCmac_ex(cmac, key16, (word32)sizeof(key16), WC_CMAC_AES,
NULL, HEAP_HINT, devId), 0);
ExpectIntEQ(wc_CmacUpdate(cmac, in, (word32)sizeof(in)), 0);
freeSeen = 0;
ExpectIntEQ(wc_CmacFinal(cmac, tag, &tagSz), 0);
ExpectIntGE(freeSeen, 1);

/* Explicit wc_CmacFree() also dispatches to the device. */
tagSz = (word32)sizeof(tag);
ExpectIntEQ(wc_InitCmac_ex(cmac, key16, (word32)sizeof(key16), WC_CMAC_AES,
NULL, HEAP_HINT, devId), 0);
ExpectIntEQ(wc_CmacUpdate(cmac, in, (word32)sizeof(in)), 0);
freeSeen = 0;
ExpectIntEQ(wc_CmacFree(cmac), 0);
ExpectIntGE(freeSeen, 1);

wc_CryptoCb_UnRegisterDevice(devId);

WC_FREE_VAR(cmac, HEAP_HINT);
#endif
return EXPECT_RESULT();
} /* END test_wc_CryptoCb_CmacFree */
4 changes: 3 additions & 1 deletion tests/api/test_cmac.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ int test_wc_InitCmac_Label(void);
int test_wc_AesCmacGenerateExDecisionCoverage(void);
int test_wc_AesCmacVerifyExDecisionCoverage(void);
int test_wc_AesCmacVerify_CryptoCb_LenMismatch(void);
int test_wc_CryptoCb_CmacFree(void);

#define TEST_CMAC_DECLS \
TEST_DECL_GROUP("cmac", test_wc_InitCmac), \
Expand All @@ -45,6 +46,7 @@ int test_wc_AesCmacVerify_CryptoCb_LenMismatch(void);
TEST_DECL_GROUP("cmac", test_wc_InitCmac_Label), \
TEST_DECL_GROUP("cmac", test_wc_AesCmacGenerateExDecisionCoverage), \
TEST_DECL_GROUP("cmac", test_wc_AesCmacVerifyExDecisionCoverage), \
TEST_DECL_GROUP("cmac", test_wc_AesCmacVerify_CryptoCb_LenMismatch)
TEST_DECL_GROUP("cmac", test_wc_AesCmacVerify_CryptoCb_LenMismatch), \
TEST_DECL_GROUP("cmac", test_wc_CryptoCb_CmacFree)

#endif /* WOLFCRYPT_TEST_CMAC_H */
126 changes: 126 additions & 0 deletions tests/api/test_ed448.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

#include <wolfssl/wolfcrypt/ed448.h>
#include <wolfssl/wolfcrypt/types.h>
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif
#include <tests/api/api.h>
#include <tests/api/test_ed448.h>

Expand Down Expand Up @@ -1369,3 +1372,126 @@ int test_wc_ed448_check_key_decisions(void)
return EXPECT_RESULT();
} /* END test_wc_ed448_check_key_decisions */

/* Test Ed448 sign/verify routed through a crypto callback (CryptoCb) device. */
#if defined(WOLF_CRYPTO_CB) && defined(HAVE_ED448) && \
defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_VERIFY) && \
!defined(WC_NO_RNG)
typedef struct ed448SpyCtx {
int signSeen;
int verifySeen;
} ed448SpyCtx;

/* Spy device: services Ed448 sign/verify in software (devId cleared) and counts
* each; declines everything else so make_key runs in software. */
static int ed448_test_crypto_cb(int devIdArg, wc_CryptoInfo* info, void* ctx)
{
ed448SpyCtx* spy = (ed448SpyCtx*)ctx;
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);

(void)devIdArg;

if (info == NULL || spy == NULL) {
return BAD_FUNC_ARG;
}

if (info->algo_type == WC_ALGO_TYPE_PK) {
#ifdef HAVE_ED448_SIGN
if (info->pk.type == WC_PK_TYPE_ED448) {
int save = info->pk.ed448sign.key->devId;
info->pk.ed448sign.key->devId = INVALID_DEVID;
ret = wc_ed448_sign_msg_ex(
info->pk.ed448sign.in, info->pk.ed448sign.inLen,
info->pk.ed448sign.out, info->pk.ed448sign.outLen,
info->pk.ed448sign.key, info->pk.ed448sign.type,
info->pk.ed448sign.context, info->pk.ed448sign.contextLen);
info->pk.ed448sign.key->devId = save;
spy->signSeen++;
}
#endif
#ifdef HAVE_ED448_VERIFY
if (info->pk.type == WC_PK_TYPE_ED448_VERIFY) {
int save = info->pk.ed448verify.key->devId;
info->pk.ed448verify.key->devId = INVALID_DEVID;
ret = wc_ed448_verify_msg_ex(
info->pk.ed448verify.sig, info->pk.ed448verify.sigLen,
info->pk.ed448verify.msg, info->pk.ed448verify.msgLen,
info->pk.ed448verify.res, info->pk.ed448verify.key,
info->pk.ed448verify.type, info->pk.ed448verify.context,
info->pk.ed448verify.contextLen);
info->pk.ed448verify.key->devId = save;
spy->verifySeen++;
}
#endif
}

return ret;
}
#endif

int test_wc_ed448_cryptocb(void)
{
EXPECT_DECLS;
#if defined(WOLF_CRYPTO_CB) && defined(HAVE_ED448) && \
defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_VERIFY) && \
!defined(WC_NO_RNG)
int devId = 4448;
ed448SpyCtx spy;
WC_RNG rng;
byte msg[32];
word32 sigLen = ED448_SIG_SIZE;
int verify = 0;
WC_DECLARE_VAR(key, ed448_key, 1, HEAP_HINT);
WC_DECLARE_VAR(sig, byte, ED448_SIG_SIZE, HEAP_HINT);

XMEMSET(&rng, 0, sizeof(rng));
XMEMSET(&spy, 0, sizeof(spy));
XMEMSET(msg, 0x5a, sizeof(msg));

WC_ALLOC_VAR(key, ed448_key, 1, HEAP_HINT);
WC_ALLOC_VAR(sig, byte, ED448_SIG_SIZE, HEAP_HINT);
#ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
ExpectNotNull(key);
ExpectNotNull(sig);
#endif
if (WC_VAR_OK(sig)) {
XMEMSET(sig, 0, ED448_SIG_SIZE);
}

ExpectIntEQ(wc_CryptoCb_RegisterDevice(devId, ed448_test_crypto_cb, &spy),
0);
ExpectIntEQ(wc_InitRng(&rng), 0);
ExpectIntEQ(wc_ed448_init_ex(key, HEAP_HINT, devId), 0);
ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, key), 0);

/* Sign routes through the device callback. */
ExpectIntEQ(wc_ed448_sign_msg(msg, (word32)sizeof(msg), sig, &sigLen, key,
NULL, 0), 0);
ExpectIntGE(spy.signSeen, 1);

/* Verify routes through the device callback. */
ExpectIntEQ(wc_ed448_verify_msg(sig, sigLen, msg, (word32)sizeof(msg),
&verify, key, NULL, 0), 0);
ExpectIntGE(spy.verifySeen, 1);
ExpectIntEQ(verify, 1);

/* Negative: corrupt the signature. Ed448 reports a bad signature as
* SIG_VERIFY_E, exercising the device verify error path (verify == 0). */
if (WC_VAR_OK(sig)) {
sig[0] ^= 0xFF;
}
verify = 1;
ExpectIntEQ(wc_ed448_verify_msg(sig, sigLen, msg, (word32)sizeof(msg),
&verify, key, NULL, 0),
WC_NO_ERR_TRACE(SIG_VERIFY_E));
ExpectIntGE(spy.verifySeen, 2);
ExpectIntEQ(verify, 0);

wc_ed448_free(key);
DoExpectIntEQ(wc_FreeRng(&rng), 0);
wc_CryptoCb_UnRegisterDevice(devId);

WC_FREE_VAR(sig, HEAP_HINT);
WC_FREE_VAR(key, HEAP_HINT);
#endif
return EXPECT_RESULT();
}
4 changes: 3 additions & 1 deletion tests/api/test_ed448.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ int test_wc_Ed448DecisionCoverage(void);
int test_wc_Ed448FeatureCoverage(void);
int test_wc_ed448_import_private_only(void);
int test_wc_ed448_check_key_decisions(void);
int test_wc_ed448_cryptocb(void);

#define TEST_ED448_DECLS \
TEST_DECL_GROUP("ed448", test_wc_ed448_make_key), \
Expand All @@ -61,6 +62,7 @@ int test_wc_ed448_check_key_decisions(void);
TEST_DECL_GROUP("ed448", test_wc_Ed448DecisionCoverage), \
TEST_DECL_GROUP("ed448", test_wc_Ed448FeatureCoverage), \
TEST_DECL_GROUP("ed448", test_wc_ed448_import_private_only), \
TEST_DECL_GROUP("ed448", test_wc_ed448_check_key_decisions)
TEST_DECL_GROUP("ed448", test_wc_ed448_check_key_decisions), \
TEST_DECL_GROUP("ed448", test_wc_ed448_cryptocb)

#endif /* WOLFCRYPT_TEST_ED448_H */
Loading
Loading