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> #include <wolfssl/wolfcrypt/rsa.h>
#endif #endif
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif
#ifdef WOLFSSL_DEBUG_ENCODING #ifdef WOLFSSL_DEBUG_ENCODING
#if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
#if MQX_USE_IO_OLD #if MQX_USE_IO_OLD
@@ -4545,6 +4549,28 @@ WOLFSSL_LOCAL int OBJ_sn2nid(const char *sn)
} }
#endif #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 */ /* process NAME, either issuer or subject */
static int GetName(DecodedCert* cert, int nameType) static int GetName(DecodedCert* cert, int nameType)
{ {

View File

@@ -55,6 +55,15 @@ static CryptoCb* wc_CryptoCb_FindDevice(int devId)
} }
return NULL; 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) 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; int ret = NOT_COMPILED_IN;
CryptoCb* dev; CryptoCb* dev;
if (key == NULL)
return ret;
/* locate registered callback */ /* locate registered callback */
dev = wc_CryptoCb_FindDevice(key->devId); dev = wc_CryptoCb_FindDevice(key->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_PK; 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); ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; return ret;
} }
@@ -126,10 +136,12 @@ int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
int ret = NOT_COMPILED_IN; int ret = NOT_COMPILED_IN;
CryptoCb* dev; CryptoCb* dev;
if (key == NULL)
return ret;
/* locate registered callback */ /* locate registered callback */
dev = wc_CryptoCb_FindDevice(key->devId); dev = wc_CryptoCb_FindDevice(key->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_PK; 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); ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; 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; int ret = NOT_COMPILED_IN;
CryptoCb* dev; CryptoCb* dev;
if (key == NULL)
return ret;
/* locate registered callback */ /* locate registered callback */
dev = wc_CryptoCb_FindDevice(key->devId); dev = wc_CryptoCb_FindDevice(key->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_PK; 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); ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; return ret;
} }
@@ -180,10 +192,12 @@ int wc_CryptoCb_Ecdh(ecc_key* private_key, ecc_key* public_key,
int ret = NOT_COMPILED_IN; int ret = NOT_COMPILED_IN;
CryptoCb* dev; CryptoCb* dev;
if (private_key == NULL)
return ret;
/* locate registered callback */ /* locate registered callback */
dev = wc_CryptoCb_FindDevice(private_key->devId); dev = wc_CryptoCb_FindDevice(private_key->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_PK; 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); ret = dev->cb(private_key->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; return ret;
} }
@@ -206,10 +219,12 @@ int wc_CryptoCb_EccSign(const byte* in, word32 inlen, byte* out,
int ret = NOT_COMPILED_IN; int ret = NOT_COMPILED_IN;
CryptoCb* dev; CryptoCb* dev;
if (key == NULL)
return ret;
/* locate registered callback */ /* locate registered callback */
dev = wc_CryptoCb_FindDevice(key->devId); dev = wc_CryptoCb_FindDevice(key->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_PK; 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); ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; return ret;
} }
@@ -234,10 +248,12 @@ int wc_CryptoCb_EccVerify(const byte* sig, word32 siglen,
int ret = NOT_COMPILED_IN; int ret = NOT_COMPILED_IN;
CryptoCb* dev; CryptoCb* dev;
if (key == NULL)
return ret;
/* locate registered callback */ /* locate registered callback */
dev = wc_CryptoCb_FindDevice(key->devId); dev = wc_CryptoCb_FindDevice(key->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_PK; 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); ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; return ret;
} }
@@ -269,9 +284,15 @@ int wc_CryptoCb_AesGcmEncrypt(Aes* aes, byte* out,
CryptoCb* dev; CryptoCb* dev;
/* locate registered callback */ /* locate registered callback */
if (aes) {
dev = wc_CryptoCb_FindDevice(aes->devId); 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; wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; 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); ret = dev->cb(aes->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; return ret;
} }
@@ -305,9 +325,15 @@ int wc_CryptoCb_AesGcmDecrypt(Aes* aes, byte* out,
CryptoCb* dev; CryptoCb* dev;
/* locate registered callback */ /* locate registered callback */
if (aes) {
dev = wc_CryptoCb_FindDevice(aes->devId); 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; wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; 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); ret = dev->cb(aes->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; return ret;
} }
@@ -340,9 +365,16 @@ int wc_CryptoCb_AesCbcEncrypt(Aes* aes, byte* out,
CryptoCb* dev; CryptoCb* dev;
/* locate registered callback */ /* locate registered callback */
if (aes) {
dev = wc_CryptoCb_FindDevice(aes->devId); 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; wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; 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); ret = dev->cb(aes->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; return ret;
} }
@@ -367,9 +398,15 @@ int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out,
CryptoCb* dev; CryptoCb* dev;
/* locate registered callback */ /* locate registered callback */
if (aes) {
dev = wc_CryptoCb_FindDevice(aes->devId); 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; wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; 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); ret = dev->cb(aes->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; return ret;
} }
@@ -397,9 +433,15 @@ int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in,
CryptoCb* dev; CryptoCb* dev;
/* locate registered callback */ /* locate registered callback */
if (sha) {
dev = wc_CryptoCb_FindDevice(sha->devId); 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; wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; 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); ret = dev->cb(sha->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; return ret;
} }
@@ -425,9 +466,15 @@ int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in,
CryptoCb* dev; CryptoCb* dev;
/* locate registered callback */ /* locate registered callback */
if (sha256) {
dev = wc_CryptoCb_FindDevice(sha256->devId); 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; wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; 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); ret = dev->cb(sha256->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; return ret;
} }
@@ -452,9 +498,15 @@ int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz)
CryptoCb* dev; CryptoCb* dev;
/* locate registered callback */ /* locate registered callback */
if (rng) {
dev = wc_CryptoCb_FindDevice(rng->devId); 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; wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_RNG; 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); ret = dev->cb(rng->devId, &cryptoInfo, dev->ctx);
} }
}
return ret; return ret;
} }

View File

@@ -915,19 +915,7 @@ struct TrustedPeerCert {
#define WOLFSSL_ASN_API WOLFSSL_LOCAL #define WOLFSSL_ASN_API WOLFSSL_LOCAL
#endif #endif
/* Macro for calculating hashId */ WOLFSSL_LOCAL int CalcHashId(const byte* data, word32 len, byte* hash);
#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_ASN_API int wc_BerToDer(const byte* ber, word32 berSz, byte* der, WOLFSSL_ASN_API int wc_BerToDer(const byte* ber, word32 berSz, byte* der,
word32* derSz); word32* derSz);