Added Crypto callback support for ASN CalcHashId. Added arg checking to cryptocb functions.

This commit is contained in:
David Garske
2019-01-21 16:46:38 -08:00
parent 9fc0610720
commit 891abe130a
3 changed files with 262 additions and 197 deletions

View File

@ -102,6 +102,10 @@ ASN Options:
#include <wolfssl/wolfcrypt/rsa.h>
#endif
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif
#ifdef WOLFSSL_DEBUG_ENCODING
#if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
#if MQX_USE_IO_OLD
@ -4545,6 +4549,28 @@ WOLFSSL_LOCAL int OBJ_sn2nid(const char *sn)
}
#endif
/* Routine for calculating hashId */
int CalcHashId(const byte* data, word32 len, byte* hash)
{
int ret = NOT_COMPILED_IN;
#ifdef WOLF_CRYPTO_CB
/* try to use a registered crypto callback */
ret = wc_CryptoCb_Sha256Hash(NULL, data, len, hash);
if (ret != NOT_COMPILED_IN)
return ret;
/* for not compiled in case, use software method below */
#endif
#if defined(NO_SHA) && !defined(NO_SHA256)
ret = wc_Sha256Hash(data, len, hash);
#elif !defined(NO_SHA)
ret = wc_ShaHash(data, len, hash);
#endif
return ret;
}
/* process NAME, either issuer or subject */
static int GetName(DecodedCert* cert, int nameType)
{

View File

@ -55,6 +55,15 @@ static CryptoCb* wc_CryptoCb_FindDevice(int devId)
}
return NULL;
}
static CryptoCb* wc_CryptoCb_FindDeviceByIndex(int startIdx)
{
int i;
for (i=startIdx; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) {
if (gCryptoDev[i].devId != INVALID_DEVID)
return &gCryptoDev[i];
}
return NULL;
}
void wc_CryptoCb_Init(void)
{
@ -97,10 +106,12 @@ int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out,
int ret = NOT_COMPILED_IN;
CryptoCb* dev;
if (key == NULL)
return ret;
/* locate registered callback */
dev = wc_CryptoCb_FindDevice(key->devId);
if (dev) {
if (dev->cb) {
if (dev && dev->cb) {
wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
@ -115,7 +126,6 @@ int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out,
ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}
@ -126,10 +136,12 @@ int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
int ret = NOT_COMPILED_IN;
CryptoCb* dev;
if (key == NULL)
return ret;
/* locate registered callback */
dev = wc_CryptoCb_FindDevice(key->devId);
if (dev) {
if (dev->cb) {
if (dev && dev->cb) {
wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
@ -141,7 +153,6 @@ int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}
@ -154,10 +165,12 @@ int wc_CryptoCb_MakeEccKey(WC_RNG* rng, int keySize, ecc_key* key, int curveId)
int ret = NOT_COMPILED_IN;
CryptoCb* dev;
if (key == NULL)
return ret;
/* locate registered callback */
dev = wc_CryptoCb_FindDevice(key->devId);
if (dev) {
if (dev->cb) {
if (dev && dev->cb) {
wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
@ -169,7 +182,6 @@ int wc_CryptoCb_MakeEccKey(WC_RNG* rng, int keySize, ecc_key* key, int curveId)
ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}
@ -180,10 +192,12 @@ int wc_CryptoCb_Ecdh(ecc_key* private_key, ecc_key* public_key,
int ret = NOT_COMPILED_IN;
CryptoCb* dev;
if (private_key == NULL)
return ret;
/* locate registered callback */
dev = wc_CryptoCb_FindDevice(private_key->devId);
if (dev) {
if (dev->cb) {
if (dev && dev->cb) {
wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
@ -195,7 +209,6 @@ int wc_CryptoCb_Ecdh(ecc_key* private_key, ecc_key* public_key,
ret = dev->cb(private_key->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}
@ -206,10 +219,12 @@ int wc_CryptoCb_EccSign(const byte* in, word32 inlen, byte* out,
int ret = NOT_COMPILED_IN;
CryptoCb* dev;
if (key == NULL)
return ret;
/* locate registered callback */
dev = wc_CryptoCb_FindDevice(key->devId);
if (dev) {
if (dev->cb) {
if (dev && dev->cb) {
wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
@ -223,7 +238,6 @@ int wc_CryptoCb_EccSign(const byte* in, word32 inlen, byte* out,
ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}
@ -234,10 +248,12 @@ int wc_CryptoCb_EccVerify(const byte* sig, word32 siglen,
int ret = NOT_COMPILED_IN;
CryptoCb* dev;
if (key == NULL)
return ret;
/* locate registered callback */
dev = wc_CryptoCb_FindDevice(key->devId);
if (dev) {
if (dev->cb) {
if (dev && dev->cb) {
wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
@ -251,7 +267,6 @@ int wc_CryptoCb_EccVerify(const byte* sig, word32 siglen,
ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}
@ -269,9 +284,15 @@ int wc_CryptoCb_AesGcmEncrypt(Aes* aes, byte* out,
CryptoCb* dev;
/* locate registered callback */
if (aes) {
dev = wc_CryptoCb_FindDevice(aes->devId);
if (dev) {
if (dev->cb) {
}
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_CIPHER;
@ -290,7 +311,6 @@ int wc_CryptoCb_AesGcmEncrypt(Aes* aes, byte* out,
ret = dev->cb(aes->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}
@ -305,9 +325,15 @@ int wc_CryptoCb_AesGcmDecrypt(Aes* aes, byte* out,
CryptoCb* dev;
/* locate registered callback */
if (aes) {
dev = wc_CryptoCb_FindDevice(aes->devId);
if (dev) {
if (dev->cb) {
}
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_CIPHER;
@ -326,7 +352,6 @@ int wc_CryptoCb_AesGcmDecrypt(Aes* aes, byte* out,
ret = dev->cb(aes->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}
@ -340,9 +365,16 @@ int wc_CryptoCb_AesCbcEncrypt(Aes* aes, byte* out,
CryptoCb* dev;
/* locate registered callback */
if (aes) {
dev = wc_CryptoCb_FindDevice(aes->devId);
if (dev) {
if (dev->cb) {
}
else {
/* locate first callback and try using it */
dev = wc_CryptoCb_FindDeviceByIndex(0);
}
dev = wc_CryptoCb_FindDevice(aes->devId);
if (dev && dev->cb) {
wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
@ -355,7 +387,6 @@ int wc_CryptoCb_AesCbcEncrypt(Aes* aes, byte* out,
ret = dev->cb(aes->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}
@ -367,9 +398,15 @@ int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out,
CryptoCb* dev;
/* locate registered callback */
if (aes) {
dev = wc_CryptoCb_FindDevice(aes->devId);
if (dev) {
if (dev->cb) {
}
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_CIPHER;
@ -382,7 +419,6 @@ int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out,
ret = dev->cb(aes->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}
@ -397,9 +433,15 @@ int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in,
CryptoCb* dev;
/* locate registered callback */
if (sha) {
dev = wc_CryptoCb_FindDevice(sha->devId);
if (dev) {
if (dev->cb) {
}
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_HASH;
@ -411,7 +453,6 @@ int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in,
ret = dev->cb(sha->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}
@ -425,9 +466,15 @@ int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in,
CryptoCb* dev;
/* locate registered callback */
if (sha256) {
dev = wc_CryptoCb_FindDevice(sha256->devId);
if (dev) {
if (dev->cb) {
}
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_HASH;
@ -439,7 +486,6 @@ int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in,
ret = dev->cb(sha256->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}
@ -452,9 +498,15 @@ int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz)
CryptoCb* dev;
/* locate registered callback */
if (rng) {
dev = wc_CryptoCb_FindDevice(rng->devId);
if (dev) {
if (dev->cb) {
}
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_RNG;
@ -464,7 +516,6 @@ int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz)
ret = dev->cb(rng->devId, &cryptoInfo, dev->ctx);
}
}
return ret;
}

View File

@ -915,19 +915,7 @@ struct TrustedPeerCert {
#define WOLFSSL_ASN_API WOLFSSL_LOCAL
#endif
/* Macro for calculating hashId */
#if defined(NO_SHA) && defined(NO_SHA256)
#ifdef WOLF_CRYPTO_CB
#define CalcHashId(data, len, hash) wc_CryptoDevSha256Hash(data, len, hash)
#else
#define CalcHashId(data, len, hash) NOT_COMPILED_IN
#endif
#elif defined(NO_SHA)
#define CalcHashId(data, len, hash) wc_Sha256Hash(data, len, hash)
#else
#define CalcHashId(data, len, hash) wc_ShaHash(data, len, hash)
#endif
WOLFSSL_LOCAL int CalcHashId(const byte* data, word32 len, byte* hash);
WOLFSSL_ASN_API int wc_BerToDer(const byte* ber, word32 berSz, byte* der,
word32* derSz);