Added Crypto callback HMAC support.

This commit is contained in:
David Garske
2019-02-01 16:54:27 -08:00
parent 18d5b3393c
commit 88d3abb1e6
6 changed files with 132 additions and 12 deletions

View File

@ -491,6 +491,38 @@ int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in,
} }
#endif /* !NO_SHA256 */ #endif /* !NO_SHA256 */
#ifndef NO_HMAC
int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in, word32 inSz, byte* digest)
{
int ret = NOT_COMPILED_IN;
CryptoCb* dev;
/* locate registered callback */
if (hmac) {
dev = wc_CryptoCb_FindDevice(hmac->devId);
}
else {
/* locate first callback and try using it */
dev = wc_CryptoCb_FindDeviceByIndex(0);
}
if (dev && dev->cb) {
wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_HMAC;
cryptoInfo.hmac.macType = macType;
cryptoInfo.hmac.in = in;
cryptoInfo.hmac.inSz = inSz;
cryptoInfo.hmac.digest = digest;
cryptoInfo.hmac.hmac = hmac;
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
}
return ret;
}
#endif /* !NO_HMAC */
#ifndef WC_NO_RNG #ifndef WC_NO_RNG
int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz) int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz)
{ {
@ -527,17 +559,15 @@ int wc_CryptoCb_RandomSeed(OS_Seed* os, byte* seed, word32 sz)
/* locate registered callback */ /* locate registered callback */
dev = wc_CryptoCb_FindDevice(os->devId); dev = wc_CryptoCb_FindDevice(os->devId);
if (dev) { if (dev && dev->cb) {
if (dev->cb) { wc_CryptoInfo cryptoInfo;
wc_CryptoInfo cryptoInfo; XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); cryptoInfo.algo_type = WC_ALGO_TYPE_SEED;
cryptoInfo.algo_type = WC_ALGO_TYPE_SEED; cryptoInfo.seed.os = os;
cryptoInfo.seed.os = os; cryptoInfo.seed.seed = seed;
cryptoInfo.seed.seed = seed; cryptoInfo.seed.sz = sz;
cryptoInfo.seed.sz = sz;
ret = dev->cb(os->devId, &cryptoInfo, dev->ctx); ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
}
} }
return ret; return ret;

View File

@ -43,6 +43,10 @@
#include <wolfssl/wolfcrypt/hmac.h> #include <wolfssl/wolfcrypt/hmac.h>
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif
#ifdef NO_INLINE #ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h> #include <wolfssl/wolfcrypt/misc.h>
#else #else
@ -691,6 +695,15 @@ int wc_HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
} }
#ifdef WOLF_CRYPTO_CB
if (hmac->devId != INVALID_DEVID) {
ret = wc_CryptoCb_Hmac(hmac, hmac->macType, msg, length, NULL);
if (ret != NOT_COMPILED_IN)
return ret;
/* fall-through on not compiled in */
ret = 0; /* reset error code */
}
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) { if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
#if defined(HAVE_CAVIUM) #if defined(HAVE_CAVIUM)
@ -791,6 +804,15 @@ int wc_HmacFinal(Hmac* hmac, byte* hash)
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
} }
#ifdef WOLF_CRYPTO_CB
if (hmac->devId != INVALID_DEVID) {
ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, hash);
if (ret != NOT_COMPILED_IN)
return ret;
/* fall-through on not compiled in */
ret = 0; /* reset error code */
}
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) { if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
int hashLen = wc_HmacSizeByType(hmac->macType); int hashLen = wc_HmacSizeByType(hmac->macType);
@ -1028,6 +1050,10 @@ int wc_HmacInit(Hmac* hmac, void* heap, int devId)
XMEMSET(hmac, 0, sizeof(Hmac)); XMEMSET(hmac, 0, sizeof(Hmac));
hmac->heap = heap; hmac->heap = heap;
#ifdef WOLF_CRYPTO_CB
hmac->devId = devId;
hmac->devCtx = NULL;
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
hmac->keyLen = 0; hmac->keyLen = 0;

View File

@ -22884,6 +22884,10 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
if (info == NULL) if (info == NULL)
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
#ifdef DEBUG_WOLFSSL
printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
#endif
if (info->algo_type == WC_ALGO_TYPE_RNG) { if (info->algo_type == WC_ALGO_TYPE_RNG) {
#ifndef WC_NO_RNG #ifndef WC_NO_RNG
/* set devId to invalid, so software is used */ /* set devId to invalid, so software is used */
@ -23087,6 +23091,9 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
else if (info->algo_type == WC_ALGO_TYPE_HASH) { else if (info->algo_type == WC_ALGO_TYPE_HASH) {
#if !defined(NO_SHA) #if !defined(NO_SHA)
if (info->hash.type == WC_HASH_TYPE_SHA) { if (info->hash.type == WC_HASH_TYPE_SHA) {
if (info->hash.sha1 == NULL)
return NOT_COMPILED_IN;
/* set devId to invalid, so software is used */ /* set devId to invalid, so software is used */
info->hash.sha1->devId = INVALID_DEVID; info->hash.sha1->devId = INVALID_DEVID;
@ -23109,6 +23116,9 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
#endif #endif
#if !defined(NO_SHA256) #if !defined(NO_SHA256)
if (info->hash.type == WC_HASH_TYPE_SHA256) { if (info->hash.type == WC_HASH_TYPE_SHA256) {
if (info->hash.sha256 == NULL)
return NOT_COMPILED_IN;
/* set devId to invalid, so software is used */ /* set devId to invalid, so software is used */
info->hash.sha256->devId = INVALID_DEVID; info->hash.sha256->devId = INVALID_DEVID;
@ -23130,6 +23140,30 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
#endif #endif
} }
#endif /* !NO_SHA || !NO_SHA256 */ #endif /* !NO_SHA || !NO_SHA256 */
#ifndef NO_HMAC
else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
if (info->hmac.hmac == NULL)
return NOT_COMPILED_IN;
/* set devId to invalid, so software is used */
info->hmac.hmac->devId = INVALID_DEVID;
if (info->hash.in != NULL) {
ret = wc_HmacUpdate(
info->hmac.hmac,
info->hmac.in,
info->hmac.inSz);
}
else if (info->hash.digest != NULL) {
ret = wc_HmacFinal(
info->hmac.hmac,
info->hmac.digest);
}
/* reset devId */
info->hmac.hmac->devId = devIdArg;
}
#endif
(void)devIdArg; (void)devIdArg;
(void)myCtx; (void)myCtx;
@ -23181,6 +23215,16 @@ int cryptocb_test(void)
ret = sha256_test(); ret = sha256_test();
#endif #endif
#endif #endif
#ifndef NO_HMAC
#ifndef NO_SHA
if (ret == 0)
ret = hmac_sha_test();
#endif
#ifndef NO_SHA256
if (ret == 0)
ret = hmac_sha256_test();
#endif
#endif
/* reset devId */ /* reset devId */
devId = INVALID_DEVID; devId = INVALID_DEVID;

View File

@ -44,6 +44,9 @@
#ifndef NO_SHA256 #ifndef NO_SHA256
#include <wolfssl/wolfcrypt/sha256.h> #include <wolfssl/wolfcrypt/sha256.h>
#endif #endif
#ifndef NO_HMAC
#include <wolfssl/wolfcrypt/hmac.h>
#endif
#ifndef WC_NO_RNG #ifndef WC_NO_RNG
#include <wolfssl/wolfcrypt/random.h> #include <wolfssl/wolfcrypt/random.h>
#endif #endif
@ -163,6 +166,15 @@ typedef struct wc_CryptoInfo {
}; };
} hash; } hash;
#endif /* !NO_SHA || !NO_SHA256 */ #endif /* !NO_SHA || !NO_SHA256 */
#ifndef NO_HMAC
struct {
int macType; /* enum wc_HashType */
const byte* in;
word32 inSz;
byte* digest;
Hmac* hmac;
} hmac;
#endif
#ifndef WC_NO_RNG #ifndef WC_NO_RNG
struct { struct {
WC_RNG* rng; WC_RNG* rng;
@ -242,6 +254,10 @@ WOLFSSL_LOCAL int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in,
WOLFSSL_LOCAL int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in, WOLFSSL_LOCAL int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in,
word32 inSz, byte* digest); word32 inSz, byte* digest);
#endif /* !NO_SHA256 */ #endif /* !NO_SHA256 */
#ifndef NO_HMAC
WOLFSSL_LOCAL int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in,
word32 inSz, byte* digest);
#endif /* !NO_HMAC */
#ifndef WC_NO_RNG #ifndef WC_NO_RNG
WOLFSSL_LOCAL int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz); WOLFSSL_LOCAL int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz);

View File

@ -142,11 +142,14 @@ typedef struct Hmac {
void* heap; /* heap hint */ void* heap; /* heap hint */
byte macType; /* md5 sha or sha256 */ byte macType; /* md5 sha or sha256 */
byte innerHashKeyed; /* keyed flag */ byte innerHashKeyed; /* keyed flag */
#ifdef WOLFSSL_ASYNC_CRYPT #ifdef WOLFSSL_ASYNC_CRYPT
WC_ASYNC_DEV asyncDev; WC_ASYNC_DEV asyncDev;
word16 keyLen; /* hmac key length (key in ipad) */ word16 keyLen; /* hmac key length (key in ipad) */
#endif /* WOLFSSL_ASYNC_CRYPT */ #endif /* WOLFSSL_ASYNC_CRYPT */
#ifdef WOLF_CRYPTO_CB
int devId;
void* devCtx;
#endif
} Hmac; } Hmac;
#endif /* HAVE_FIPS */ #endif /* HAVE_FIPS */

View File

@ -544,8 +544,9 @@
WC_ALGO_TYPE_PK = 3, WC_ALGO_TYPE_PK = 3,
WC_ALGO_TYPE_RNG = 4, WC_ALGO_TYPE_RNG = 4,
WC_ALGO_TYPE_SEED = 5, WC_ALGO_TYPE_SEED = 5,
WC_ALGO_TYPE_HMAC = 6,
WC_ALGO_TYPE_MAX = WC_ALGO_TYPE_SEED WC_ALGO_TYPE_MAX = WC_ALGO_TYPE_HMAC
}; };
/* hash types */ /* hash types */