Merge pull request #2070 from dgarske/fix_cryptocb

Fixes and improvements to Crypto Callbacks and STM32 RNG performance
This commit is contained in:
toddouska
2019-02-13 12:44:19 -08:00
committed by GitHub
28 changed files with 1005 additions and 375 deletions
+5 -2
View File
@@ -152,7 +152,7 @@ typedef struct Aes {
#endif /* WOLFSSL_AESNI */
#ifdef WOLF_CRYPTO_CB
int devId;
word32 devKey[AES_MAX_KEY_SIZE/WOLFSSL_BIT_SIZE/sizeof(word32)]; /* raw key */
void* devCtx;
#endif
#ifdef HAVE_PKCS11
byte id[AES_MAX_ID_LEN];
@@ -182,9 +182,12 @@ typedef struct Aes {
GCM_NONCE_MID_SZ)];
#endif
#endif
#if defined(WOLF_CRYPTO_CB) || (defined(WOLFSSL_DEVCRYPTO) && \
(defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC)))
word32 devKey[AES_MAX_KEY_SIZE/WOLFSSL_BIT_SIZE/sizeof(word32)]; /* raw key */
#endif
#if defined(WOLFSSL_DEVCRYPTO) && \
(defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))
word32 devKey[AES_MAX_KEY_SIZE/WOLFSSL_BIT_SIZE/sizeof(word32)]; /* raw key */
WC_CRYPTODEV ctx;
#endif
void* heap; /* memory hint to use */
+1 -13
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);
+19 -1
View File
@@ -44,6 +44,9 @@
#ifndef NO_SHA256
#include <wolfssl/wolfcrypt/sha256.h>
#endif
#ifndef NO_HMAC
#include <wolfssl/wolfcrypt/hmac.h>
#endif
#ifndef WC_NO_RNG
#include <wolfssl/wolfcrypt/random.h>
#endif
@@ -51,6 +54,7 @@
/* Crypto Information Structure for callbacks */
typedef struct wc_CryptoInfo {
int algo_type; /* enum wc_AlgoType */
#if !defined(NO_RSA) || defined(HAVE_ECC)
struct {
int type; /* enum wc_PkType */
union {
@@ -105,6 +109,7 @@ typedef struct wc_CryptoInfo {
#endif
};
} pk;
#endif /* !NO_RSA || HAVE_ECC */
#ifndef NO_AES
struct {
int type; /* enum wc_CipherType */
@@ -146,7 +151,7 @@ typedef struct wc_CryptoInfo {
#endif /* HAVE_AES_CBC */
};
} cipher;
#endif
#endif /* !NO_AES */
#if !defined(NO_SHA) || !defined(NO_SHA256)
struct {
int type; /* enum wc_HashType */
@@ -163,6 +168,15 @@ typedef struct wc_CryptoInfo {
};
} hash;
#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
struct {
WC_RNG* rng;
@@ -242,6 +256,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,
word32 inSz, byte* digest);
#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
WOLFSSL_LOCAL int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz);
+13
View File
@@ -76,6 +76,12 @@ enum wc_MACAlgorithm {
blake2b_mac
};
enum wc_HashFlags {
WC_HASH_FLAG_NONE = 0x00000000,
WC_HASH_FLAG_WILLCOPY = 0x00000001, /* flag to indicate hash will be copied */
WC_HASH_FLAG_ISCOPY = 0x00000002, /* hash is copy */
};
typedef union {
#ifndef NO_MD5
@@ -150,6 +156,13 @@ WOLFSSL_API int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type,
byte* out);
WOLFSSL_API int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type);
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
WOLFSSL_LOCAL int wc_HashSetFlags(wc_HashAlg* hash, enum wc_HashType type,
word32 flags);
WOLFSSL_LOCAL int wc_HashGetFlags(wc_HashAlg* hash, enum wc_HashType type,
word32* flags);
#endif
#ifndef NO_MD5
#include <wolfssl/wolfcrypt/md5.h>
WOLFSSL_API int wc_Md5Hash(const byte* data, word32 len, byte* hash);
+8 -2
View File
@@ -142,11 +142,17 @@ typedef struct Hmac {
void* heap; /* heap hint */
byte macType; /* md5 sha or sha256 */
byte innerHashKeyed; /* keyed flag */
#ifdef WOLFSSL_ASYNC_CRYPT
WC_ASYNC_DEV asyncDev;
word16 keyLen; /* hmac key length (key in ipad) */
#endif /* WOLFSSL_ASYNC_CRYPT */
#ifdef WOLF_CRYPTO_CB
int devId;
void* devCtx;
const byte* keyRaw;
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
word16 keyLen; /* hmac key length (key in ipad) */
#endif
} Hmac;
#endif /* HAVE_FIPS */
+8
View File
@@ -97,6 +97,9 @@ typedef struct wc_Md5 {
#ifdef WOLFSSL_ASYNC_CRYPT
WC_ASYNC_DEV asyncDev;
#endif /* WOLFSSL_ASYNC_CRYPT */
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
word32 flags; /* enum wc_HashFlags in hash.h */
#endif
} wc_Md5;
#endif /* WOLFSSL_TI_HASH */
@@ -114,6 +117,11 @@ WOLFSSL_API int wc_Md5Copy(wc_Md5*, wc_Md5*);
WOLFSSL_API void wc_Md5SizeSet(wc_Md5* md5, word32 len);
#endif
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
WOLFSSL_LOCAL int wc_Md5SetFlags(wc_Md5* md5, word32 flags);
WOLFSSL_LOCAL int wc_Md5GetFlags(wc_Md5* md5, word32* flags);
#endif
#ifdef __cplusplus
} /* extern "C" */
#endif
+8
View File
@@ -132,6 +132,9 @@ typedef struct wc_Sha {
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
WC_ESP32SHA ctx;
#endif
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
word32 flags; /* enum wc_HashFlags in hash.h */
#endif
} wc_Sha;
#endif /* WOLFSSL_TI_HASH */
@@ -153,6 +156,11 @@ WOLFSSL_API int wc_ShaCopy(wc_Sha*, wc_Sha*);
WOLFSSL_API void wc_ShaSizeSet(wc_Sha* sha, word32 len);
#endif
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
WOLFSSL_LOCAL int wc_ShaSetFlags(wc_Sha* sha, word32 flags);
WOLFSSL_LOCAL int wc_ShaGetFlags(wc_Sha* sha, word32* flags);
#endif
#ifdef __cplusplus
} /* extern "C" */
#endif
+13
View File
@@ -159,6 +159,9 @@ typedef struct wc_Sha256 {
void* devCtx; /* generic crypto callback context */
#endif
#endif
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
word32 flags; /* enum wc_HashFlags in hash.h */
#endif
} wc_Sha256;
#endif
@@ -179,6 +182,11 @@ WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst);
WOLFSSL_API void wc_Sha256SizeSet(wc_Sha256*, word32);
#endif
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
WOLFSSL_LOCAL int wc_Sha256SetFlags(wc_Sha256* sha256, word32 flags);
WOLFSSL_LOCAL int wc_Sha256GetFlags(wc_Sha256* sha256, word32* flags);
#endif
#ifdef WOLFSSL_SHA224
/* avoid redefinition of structs */
#if !defined(HAVE_FIPS) || \
@@ -213,6 +221,11 @@ WOLFSSL_API void wc_Sha224Free(wc_Sha224*);
WOLFSSL_API int wc_Sha224GetHash(wc_Sha224*, byte*);
WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst);
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
WOLFSSL_LOCAL int wc_Sha224SetFlags(wc_Sha224* sha224, word32 flags);
WOLFSSL_LOCAL int wc_Sha224GetFlags(wc_Sha224* sha224, word32* flags);
#endif
#endif /* WOLFSSL_SHA224 */
#ifdef __cplusplus
+8
View File
@@ -101,6 +101,9 @@ typedef struct Sha3 {
#ifdef WOLFSSL_ASYNC_CRYPT
WC_ASYNC_DEV asyncDev;
#endif /* WOLFSSL_ASYNC_CRYPT */
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
word32 flags; /* enum wc_HashFlags in hash.h */
#endif
} wc_Sha3;
#endif
@@ -133,6 +136,11 @@ WOLFSSL_API void wc_Sha3_512_Free(wc_Sha3*);
WOLFSSL_API int wc_Sha3_512_GetHash(wc_Sha3*, byte*);
WOLFSSL_API int wc_Sha3_512_Copy(wc_Sha3* src, wc_Sha3* dst);
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
WOLFSSL_LOCAL int wc_Sha3_SetFlags(wc_Sha3* sha3, word32 flags);
WOLFSSL_LOCAL int wc_Sha3_GetFlags(wc_Sha3* sha3, word32* flags);
#endif
#ifdef __cplusplus
} /* extern "C" */
#endif
+13
View File
@@ -133,6 +133,9 @@ typedef struct wc_Sha512 {
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
WC_ESP32SHA ctx;
#endif
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
word32 flags; /* enum wc_HashFlags in hash.h */
#endif
} wc_Sha512;
#endif
@@ -150,6 +153,11 @@ WOLFSSL_API void wc_Sha512Free(wc_Sha512*);
WOLFSSL_API int wc_Sha512GetHash(wc_Sha512*, byte*);
WOLFSSL_API int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst);
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
WOLFSSL_LOCAL int wc_Sha512SetFlags(wc_Sha512* sha512, word32 flags);
WOLFSSL_LOCAL int wc_Sha512GetFlags(wc_Sha512* sha512, word32* flags);
#endif
#endif /* WOLFSSL_SHA512 */
#if defined(WOLFSSL_SHA384)
@@ -191,6 +199,11 @@ WOLFSSL_API void wc_Sha384Free(wc_Sha384*);
WOLFSSL_API int wc_Sha384GetHash(wc_Sha384*, byte*);
WOLFSSL_API int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst);
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
WOLFSSL_LOCAL int wc_Sha384SetFlags(wc_Sha384* sha384, word32 flags);
WOLFSSL_LOCAL int wc_Sha384GetFlags(wc_Sha384* sha384, word32* flags);
#endif
#endif /* WOLFSSL_SHA384 */
#ifdef __cplusplus
+2 -1
View File
@@ -544,8 +544,9 @@
WC_ALGO_TYPE_PK = 3,
WC_ALGO_TYPE_RNG = 4,
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 */
Executable → Regular
View File