Merge pull request #2324 from dgarske/cryptocb_3des

Crypto callback DES3 support
This commit is contained in:
John Safranek
2019-07-03 10:17:23 -07:00
committed by GitHub
6 changed files with 234 additions and 95 deletions

View File

@ -382,7 +382,6 @@ int wc_CryptoCb_AesCbcEncrypt(Aes* aes, byte* out,
dev = wc_CryptoCb_FindDeviceByIndex(0); dev = wc_CryptoCb_FindDeviceByIndex(0);
} }
dev = wc_CryptoCb_FindDevice(aes->devId);
if (dev && dev->cb) { if (dev && dev->cb) {
wc_CryptoInfo cryptoInfo; wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
@ -434,6 +433,72 @@ int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out,
#endif /* HAVE_AES_CBC */ #endif /* HAVE_AES_CBC */
#endif /* !NO_AES */ #endif /* !NO_AES */
#ifndef NO_DES3
int wc_CryptoCb_Des3Encrypt(Des3* des3, byte* out,
const byte* in, word32 sz)
{
int ret = CRYPTOCB_UNAVAILABLE;
CryptoCb* dev;
/* locate registered callback */
if (des3) {
dev = wc_CryptoCb_FindDevice(des3->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_CIPHER;
cryptoInfo.cipher.type = WC_CIPHER_DES3;
cryptoInfo.cipher.enc = 1;
cryptoInfo.cipher.des3.des = des3;
cryptoInfo.cipher.des3.out = out;
cryptoInfo.cipher.des3.in = in;
cryptoInfo.cipher.des3.sz = sz;
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
}
return wc_CryptoCb_TranslateErrorCode(ret);
}
int wc_CryptoCb_Des3Decrypt(Des3* des3, byte* out,
const byte* in, word32 sz)
{
int ret = CRYPTOCB_UNAVAILABLE;
CryptoCb* dev;
/* locate registered callback */
if (des3) {
dev = wc_CryptoCb_FindDevice(des3->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_CIPHER;
cryptoInfo.cipher.type = WC_CIPHER_DES3;
cryptoInfo.cipher.enc = 0;
cryptoInfo.cipher.des3.des = des3;
cryptoInfo.cipher.des3.out = out;
cryptoInfo.cipher.des3.in = in;
cryptoInfo.cipher.des3.sz = sz;
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
}
return wc_CryptoCb_TranslateErrorCode(ret);
}
#endif /* !NO_DES3 */
#ifndef NO_SHA #ifndef NO_SHA
int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in, int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in,
word32 inSz, byte* digest) word32 inSz, byte* digest)

View File

@ -45,6 +45,10 @@
#include <wolfssl/wolfcrypt/des3.h> #include <wolfssl/wolfcrypt/des3.h>
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif
/* fips wrapper calls, user can call direct */ /* fips wrapper calls, user can call direct */
#if defined(HAVE_FIPS) && \ #if defined(HAVE_FIPS) && \
(!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)) (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
@ -1587,6 +1591,15 @@
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
} }
#ifdef WOLF_CRYPTO_CB
if (des->devId != INVALID_DEVID) {
int ret = wc_CryptoCb_Des3Encrypt(des, out, in, sz);
if (ret != CRYPTOCB_UNAVAILABLE)
return ret;
/* fall-through when unavailable */
}
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES) #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)
if (des->asyncDev.marker == WOLFSSL_ASYNC_MARKER_3DES && if (des->asyncDev.marker == WOLFSSL_ASYNC_MARKER_3DES &&
sz >= WC_ASYNC_THRESH_DES3_CBC) { sz >= WC_ASYNC_THRESH_DES3_CBC) {
@ -1629,6 +1642,15 @@
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
} }
#ifdef WOLF_CRYPTO_CB
if (des->devId != INVALID_DEVID) {
int ret = wc_CryptoCb_Des3Decrypt(des, out, in, sz);
if (ret != CRYPTOCB_UNAVAILABLE)
return ret;
/* fall-through when unavailable */
}
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) #if defined(WOLFSSL_ASYNC_CRYPT)
if (des->asyncDev.marker == WOLFSSL_ASYNC_MARKER_3DES && if (des->asyncDev.marker == WOLFSSL_ASYNC_MARKER_3DES &&
sz >= WC_ASYNC_THRESH_DES3_CBC) { sz >= WC_ASYNC_THRESH_DES3_CBC) {
@ -1734,11 +1756,16 @@ int wc_Des3Init(Des3* des3, void* heap, int devId)
des3->heap = heap; des3->heap = heap;
#ifdef WOLF_CRYPTO_CB
des3->devId = devId;
des3->devCtx = NULL;
#else
(void)devId;
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES) #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)
ret = wolfAsync_DevCtxInit(&des3->asyncDev, WOLFSSL_ASYNC_MARKER_3DES, ret = wolfAsync_DevCtxInit(&des3->asyncDev, WOLFSSL_ASYNC_MARKER_3DES,
des3->heap, devId); des3->heap, devId);
#else
(void)devId;
#endif #endif
return ret; return ret;

View File

@ -252,26 +252,26 @@ int _InitHmac(Hmac* hmac, int type, void* heap)
#endif /* HAVE_BLAKE2 */ #endif /* HAVE_BLAKE2 */
#ifdef WOLFSSL_SHA3 #ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_224 #ifndef WOLFSSL_NOSHA3_224
case WC_SHA3_224: case WC_SHA3_224:
ret = wc_InitSha3_224(&hmac->hash.sha3, heap, INVALID_DEVID); ret = wc_InitSha3_224(&hmac->hash.sha3, heap, INVALID_DEVID);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_256 #ifndef WOLFSSL_NOSHA3_256
case WC_SHA3_256: case WC_SHA3_256:
ret = wc_InitSha3_256(&hmac->hash.sha3, heap, INVALID_DEVID); ret = wc_InitSha3_256(&hmac->hash.sha3, heap, INVALID_DEVID);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_384 #ifndef WOLFSSL_NOSHA3_384
case WC_SHA3_384: case WC_SHA3_384:
ret = wc_InitSha3_384(&hmac->hash.sha3, heap, INVALID_DEVID); ret = wc_InitSha3_384(&hmac->hash.sha3, heap, INVALID_DEVID);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_512 #ifndef WOLFSSL_NOSHA3_512
case WC_SHA3_512: case WC_SHA3_512:
ret = wc_InitSha3_512(&hmac->hash.sha3, heap, INVALID_DEVID); ret = wc_InitSha3_512(&hmac->hash.sha3, heap, INVALID_DEVID);
break; break;
#endif #endif
#endif #endif
default: default:
@ -377,7 +377,6 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
#ifdef WOLFSSL_SHA224 #ifdef WOLFSSL_SHA224
case WC_SHA224: case WC_SHA224:
{
hmac_block_size = WC_SHA224_BLOCK_SIZE; hmac_block_size = WC_SHA224_BLOCK_SIZE;
if (length <= WC_SHA224_BLOCK_SIZE) { if (length <= WC_SHA224_BLOCK_SIZE) {
if (key != NULL) { if (key != NULL) {
@ -394,13 +393,11 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
length = WC_SHA224_DIGEST_SIZE; length = WC_SHA224_DIGEST_SIZE;
} }
} break;
break;
#endif /* WOLFSSL_SHA224 */ #endif /* WOLFSSL_SHA224 */
#ifndef NO_SHA256 #ifndef NO_SHA256
case WC_SHA256: case WC_SHA256:
hmac_block_size = WC_SHA256_BLOCK_SIZE; hmac_block_size = WC_SHA256_BLOCK_SIZE;
if (length <= WC_SHA256_BLOCK_SIZE) { if (length <= WC_SHA256_BLOCK_SIZE) {
if (key != NULL) { if (key != NULL) {
XMEMCPY(ip, key, length); XMEMCPY(ip, key, length);
@ -482,7 +479,7 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
#endif /* HAVE_BLAKE2 */ #endif /* HAVE_BLAKE2 */
#ifdef WOLFSSL_SHA3 #ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_224 #ifndef WOLFSSL_NOSHA3_224
case WC_SHA3_224: case WC_SHA3_224:
hmac_block_size = WC_SHA3_224_BLOCK_SIZE; hmac_block_size = WC_SHA3_224_BLOCK_SIZE;
if (length <= WC_SHA3_224_BLOCK_SIZE) { if (length <= WC_SHA3_224_BLOCK_SIZE) {
@ -501,8 +498,8 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
length = WC_SHA3_224_DIGEST_SIZE; length = WC_SHA3_224_DIGEST_SIZE;
} }
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_256 #ifndef WOLFSSL_NOSHA3_256
case WC_SHA3_256: case WC_SHA3_256:
hmac_block_size = WC_SHA3_256_BLOCK_SIZE; hmac_block_size = WC_SHA3_256_BLOCK_SIZE;
if (length <= WC_SHA3_256_BLOCK_SIZE) { if (length <= WC_SHA3_256_BLOCK_SIZE) {
@ -521,8 +518,8 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
length = WC_SHA3_256_DIGEST_SIZE; length = WC_SHA3_256_DIGEST_SIZE;
} }
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_384 #ifndef WOLFSSL_NOSHA3_384
case WC_SHA3_384: case WC_SHA3_384:
hmac_block_size = WC_SHA3_384_BLOCK_SIZE; hmac_block_size = WC_SHA3_384_BLOCK_SIZE;
if (length <= WC_SHA3_384_BLOCK_SIZE) { if (length <= WC_SHA3_384_BLOCK_SIZE) {
@ -541,8 +538,8 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
length = WC_SHA3_384_DIGEST_SIZE; length = WC_SHA3_384_DIGEST_SIZE;
} }
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_512 #ifndef WOLFSSL_NOSHA3_512
case WC_SHA3_512: case WC_SHA3_512:
hmac_block_size = WC_SHA3_512_BLOCK_SIZE; hmac_block_size = WC_SHA3_512_BLOCK_SIZE;
if (length <= WC_SHA3_512_BLOCK_SIZE) { if (length <= WC_SHA3_512_BLOCK_SIZE) {
@ -561,7 +558,7 @@ int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
length = WC_SHA3_512_DIGEST_SIZE; length = WC_SHA3_512_DIGEST_SIZE;
} }
break; break;
#endif #endif
#endif /* WOLFSSL_SHA3 */ #endif /* WOLFSSL_SHA3 */
default: default:
@ -609,7 +606,7 @@ static int HmacKeyInnerHash(Hmac* hmac)
#ifndef NO_MD5 #ifndef NO_MD5
case WC_MD5: case WC_MD5:
ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->ipad, ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->ipad,
WC_MD5_BLOCK_SIZE); WC_MD5_BLOCK_SIZE);
break; break;
#endif /* !NO_MD5 */ #endif /* !NO_MD5 */
@ -623,27 +620,26 @@ static int HmacKeyInnerHash(Hmac* hmac)
#ifdef WOLFSSL_SHA224 #ifdef WOLFSSL_SHA224
case WC_SHA224: case WC_SHA224:
ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->ipad, ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->ipad,
WC_SHA224_BLOCK_SIZE); WC_SHA224_BLOCK_SIZE);
break; break;
#endif /* WOLFSSL_SHA224 */ #endif /* WOLFSSL_SHA224 */
#ifndef NO_SHA256 #ifndef NO_SHA256
case WC_SHA256: case WC_SHA256:
ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->ipad, ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->ipad,
WC_SHA256_BLOCK_SIZE); WC_SHA256_BLOCK_SIZE);
break; break;
#endif /* !NO_SHA256 */ #endif /* !NO_SHA256 */
#ifdef WOLFSSL_SHA384 #ifdef WOLFSSL_SHA384
case WC_SHA384: case WC_SHA384:
ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->ipad, ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->ipad,
WC_SHA384_BLOCK_SIZE); WC_SHA384_BLOCK_SIZE);
break; break;
#endif /* WOLFSSL_SHA384 */ #endif /* WOLFSSL_SHA384 */
#ifdef WOLFSSL_SHA512 #ifdef WOLFSSL_SHA512
case WC_SHA512: case WC_SHA512:
ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->ipad, ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->ipad,
WC_SHA512_BLOCK_SIZE); WC_SHA512_BLOCK_SIZE);
break; break;
#endif /* WOLFSSL_SHA512 */ #endif /* WOLFSSL_SHA512 */
@ -655,30 +651,30 @@ static int HmacKeyInnerHash(Hmac* hmac)
#endif /* HAVE_BLAKE2 */ #endif /* HAVE_BLAKE2 */
#ifdef WOLFSSL_SHA3 #ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_224 #ifndef WOLFSSL_NOSHA3_224
case WC_SHA3_224: case WC_SHA3_224:
ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->ipad, ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
WC_SHA3_224_BLOCK_SIZE); WC_SHA3_224_BLOCK_SIZE);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_256 #ifndef WOLFSSL_NOSHA3_256
case WC_SHA3_256: case WC_SHA3_256:
ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->ipad, ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
WC_SHA3_256_BLOCK_SIZE); WC_SHA3_256_BLOCK_SIZE);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_384 #ifndef WOLFSSL_NOSHA3_384
case WC_SHA3_384: case WC_SHA3_384:
ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->ipad, ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
WC_SHA3_384_BLOCK_SIZE); WC_SHA3_384_BLOCK_SIZE);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_512 #ifndef WOLFSSL_NOSHA3_512
case WC_SHA3_512: case WC_SHA3_512:
ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->ipad, ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->ipad,
WC_SHA3_512_BLOCK_SIZE); WC_SHA3_512_BLOCK_SIZE);
break; break;
#endif #endif
#endif /* WOLFSSL_SHA3 */ #endif /* WOLFSSL_SHA3 */
default: default:
@ -771,26 +767,26 @@ int wc_HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
#endif /* HAVE_BLAKE2 */ #endif /* HAVE_BLAKE2 */
#ifdef WOLFSSL_SHA3 #ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_224 #ifndef WOLFSSL_NOSHA3_224
case WC_SHA3_224: case WC_SHA3_224:
ret = wc_Sha3_224_Update(&hmac->hash.sha3, msg, length); ret = wc_Sha3_224_Update(&hmac->hash.sha3, msg, length);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_256 #ifndef WOLFSSL_NOSHA3_256
case WC_SHA3_256: case WC_SHA3_256:
ret = wc_Sha3_256_Update(&hmac->hash.sha3, msg, length); ret = wc_Sha3_256_Update(&hmac->hash.sha3, msg, length);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_384 #ifndef WOLFSSL_NOSHA3_384
case WC_SHA3_384: case WC_SHA3_384:
ret = wc_Sha3_384_Update(&hmac->hash.sha3, msg, length); ret = wc_Sha3_384_Update(&hmac->hash.sha3, msg, length);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_512 #ifndef WOLFSSL_NOSHA3_512
case WC_SHA3_512: case WC_SHA3_512:
ret = wc_Sha3_512_Update(&hmac->hash.sha3, msg, length); ret = wc_Sha3_512_Update(&hmac->hash.sha3, msg, length);
break; break;
#endif #endif
#endif /* WOLFSSL_SHA3 */ #endif /* WOLFSSL_SHA3 */
default: default:
@ -815,7 +811,6 @@ int wc_HmacFinal(Hmac* hmac, byte* hash)
if (ret != CRYPTOCB_UNAVAILABLE) if (ret != CRYPTOCB_UNAVAILABLE)
return ret; return ret;
/* fall-through when unavailable */ /* fall-through when unavailable */
ret = 0; /* reset error code */
} }
#endif #endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
@ -848,11 +843,11 @@ int wc_HmacFinal(Hmac* hmac, byte* hash)
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->opad, ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->opad,
WC_MD5_BLOCK_SIZE); WC_MD5_BLOCK_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->innerHash, ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->innerHash,
WC_MD5_DIGEST_SIZE); WC_MD5_DIGEST_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Md5Final(&hmac->hash.md5, hash); ret = wc_Md5Final(&hmac->hash.md5, hash);
@ -878,36 +873,33 @@ int wc_HmacFinal(Hmac* hmac, byte* hash)
#ifdef WOLFSSL_SHA224 #ifdef WOLFSSL_SHA224
case WC_SHA224: case WC_SHA224:
{
ret = wc_Sha224Final(&hmac->hash.sha224, (byte*)hmac->innerHash); ret = wc_Sha224Final(&hmac->hash.sha224, (byte*)hmac->innerHash);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->opad, ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->opad,
WC_SHA224_BLOCK_SIZE); WC_SHA224_BLOCK_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->innerHash, ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->innerHash,
WC_SHA224_DIGEST_SIZE); WC_SHA224_DIGEST_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha224Final(&hmac->hash.sha224, hash); ret = wc_Sha224Final(&hmac->hash.sha224, hash);
if (ret != 0) if (ret != 0)
break; break;
} break;
break;
#endif /* WOLFSSL_SHA224 */ #endif /* WOLFSSL_SHA224 */
#ifndef NO_SHA256 #ifndef NO_SHA256
case WC_SHA256: case WC_SHA256:
ret = wc_Sha256Final(&hmac->hash.sha256, (byte*)hmac->innerHash); ret = wc_Sha256Final(&hmac->hash.sha256, (byte*)hmac->innerHash);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->opad, ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->opad,
WC_SHA256_BLOCK_SIZE); WC_SHA256_BLOCK_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->innerHash, ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->innerHash,
WC_SHA256_DIGEST_SIZE); WC_SHA256_DIGEST_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha256Final(&hmac->hash.sha256, hash); ret = wc_Sha256Final(&hmac->hash.sha256, hash);
@ -920,11 +912,11 @@ int wc_HmacFinal(Hmac* hmac, byte* hash)
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->opad, ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->opad,
WC_SHA384_BLOCK_SIZE); WC_SHA384_BLOCK_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->innerHash, ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->innerHash,
WC_SHA384_DIGEST_SIZE); WC_SHA384_DIGEST_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha384Final(&hmac->hash.sha384, hash); ret = wc_Sha384Final(&hmac->hash.sha384, hash);
@ -936,11 +928,11 @@ int wc_HmacFinal(Hmac* hmac, byte* hash)
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->opad, ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->opad,
WC_SHA512_BLOCK_SIZE); WC_SHA512_BLOCK_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->innerHash, ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->innerHash,
WC_SHA512_DIGEST_SIZE); WC_SHA512_DIGEST_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha512Final(&hmac->hash.sha512, hash); ret = wc_Sha512Final(&hmac->hash.sha512, hash);
@ -966,70 +958,70 @@ int wc_HmacFinal(Hmac* hmac, byte* hash)
#endif /* HAVE_BLAKE2 */ #endif /* HAVE_BLAKE2 */
#ifdef WOLFSSL_SHA3 #ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_224 #ifndef WOLFSSL_NOSHA3_224
case WC_SHA3_224: case WC_SHA3_224:
ret = wc_Sha3_224_Final(&hmac->hash.sha3, (byte*)hmac->innerHash); ret = wc_Sha3_224_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->opad, ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->opad,
WC_SHA3_224_BLOCK_SIZE); WC_SHA3_224_BLOCK_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->innerHash, ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
WC_SHA3_224_DIGEST_SIZE); WC_SHA3_224_DIGEST_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha3_224_Final(&hmac->hash.sha3, hash); ret = wc_Sha3_224_Final(&hmac->hash.sha3, hash);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_256 #ifndef WOLFSSL_NOSHA3_256
case WC_SHA3_256: case WC_SHA3_256:
ret = wc_Sha3_256_Final(&hmac->hash.sha3, (byte*)hmac->innerHash); ret = wc_Sha3_256_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->opad, ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->opad,
WC_SHA3_256_BLOCK_SIZE); WC_SHA3_256_BLOCK_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->innerHash, ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
WC_SHA3_256_DIGEST_SIZE); WC_SHA3_256_DIGEST_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha3_256_Final(&hmac->hash.sha3, hash); ret = wc_Sha3_256_Final(&hmac->hash.sha3, hash);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_384 #ifndef WOLFSSL_NOSHA3_384
case WC_SHA3_384: case WC_SHA3_384:
ret = wc_Sha3_384_Final(&hmac->hash.sha3, (byte*)hmac->innerHash); ret = wc_Sha3_384_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->opad, ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->opad,
WC_SHA3_384_BLOCK_SIZE); WC_SHA3_384_BLOCK_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->innerHash, ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
WC_SHA3_384_DIGEST_SIZE); WC_SHA3_384_DIGEST_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha3_384_Final(&hmac->hash.sha3, hash); ret = wc_Sha3_384_Final(&hmac->hash.sha3, hash);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_512 #ifndef WOLFSSL_NOSHA3_512
case WC_SHA3_512: case WC_SHA3_512:
ret = wc_Sha3_512_Final(&hmac->hash.sha3, (byte*)hmac->innerHash); ret = wc_Sha3_512_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->opad, ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->opad,
WC_SHA3_512_BLOCK_SIZE); WC_SHA3_512_BLOCK_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->innerHash, ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
WC_SHA3_512_DIGEST_SIZE); WC_SHA3_512_DIGEST_SIZE);
if (ret != 0) if (ret != 0)
break; break;
ret = wc_Sha3_512_Final(&hmac->hash.sha3, hash); ret = wc_Sha3_512_Final(&hmac->hash.sha3, hash);
break; break;
#endif #endif
#endif /* WOLFSSL_SHA3 */ #endif /* WOLFSSL_SHA3 */
default: default:
@ -1127,7 +1119,6 @@ void wc_HmacFree(Hmac* hmac)
wc_Sha224Free(&hmac->hash.sha224); wc_Sha224Free(&hmac->hash.sha224);
break; break;
#endif /* WOLFSSL_SHA224 */ #endif /* WOLFSSL_SHA224 */
#ifndef NO_SHA256 #ifndef NO_SHA256
case WC_SHA256: case WC_SHA256:
wc_Sha256Free(&hmac->hash.sha256); wc_Sha256Free(&hmac->hash.sha256);
@ -1151,26 +1142,26 @@ void wc_HmacFree(Hmac* hmac)
#endif /* HAVE_BLAKE2 */ #endif /* HAVE_BLAKE2 */
#ifdef WOLFSSL_SHA3 #ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_224 #ifndef WOLFSSL_NOSHA3_224
case WC_SHA3_224: case WC_SHA3_224:
wc_Sha3_224_Free(&hmac->hash.sha3); wc_Sha3_224_Free(&hmac->hash.sha3);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_256 #ifndef WOLFSSL_NOSHA3_256
case WC_SHA3_256: case WC_SHA3_256:
wc_Sha3_256_Free(&hmac->hash.sha3); wc_Sha3_256_Free(&hmac->hash.sha3);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_384 #ifndef WOLFSSL_NOSHA3_384
case WC_SHA3_384: case WC_SHA3_384:
wc_Sha3_384_Free(&hmac->hash.sha3); wc_Sha3_384_Free(&hmac->hash.sha3);
break; break;
#endif #endif
#ifndef WOLFSSL_NOSHA3_512 #ifndef WOLFSSL_NOSHA3_512
case WC_SHA3_512: case WC_SHA3_512:
wc_Sha3_512_Free(&hmac->hash.sha3); wc_Sha3_512_Free(&hmac->hash.sha3);
break; break;
#endif #endif
#endif /* WOLFSSL_SHA3 */ #endif /* WOLFSSL_SHA3 */
default: default:
@ -1199,7 +1190,6 @@ void wc_HmacFree(Hmac* hmac)
wc_Sha224Free(&hmac->hash.sha224); wc_Sha224Free(&hmac->hash.sha224);
break; break;
#endif /* WOLFSSL_SHA224 */ #endif /* WOLFSSL_SHA224 */
#ifndef NO_SHA256 #ifndef NO_SHA256
case WC_SHA256: case WC_SHA256:
wc_Sha256Free(&hmac->hash.sha256); wc_Sha256Free(&hmac->hash.sha256);

View File

@ -24334,7 +24334,7 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
#endif /* HAVE_ECC */ #endif /* HAVE_ECC */
} }
else if (info->algo_type == WC_ALGO_TYPE_CIPHER) { else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
#ifndef NO_AES #if !defined(NO_AES) || !defined(NO_DES3)
#ifdef HAVE_AESGCM #ifdef HAVE_AESGCM
if (info->cipher.type == WC_CIPHER_AES_GCM) { if (info->cipher.type == WC_CIPHER_AES_GCM) {
if (info->cipher.enc) { if (info->cipher.enc) {
@ -24407,7 +24407,37 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
} }
} }
#endif /* HAVE_AES_CBC */ #endif /* HAVE_AES_CBC */
#endif /* !NO_AES */ #ifndef NO_DES3
if (info->cipher.type == WC_CIPHER_DES3) {
if (info->cipher.enc) {
/* set devId to invalid, so software is used */
info->cipher.des3.des->devId = INVALID_DEVID;
ret = wc_Des3_CbcEncrypt(
info->cipher.des3.des,
info->cipher.des3.out,
info->cipher.des3.in,
info->cipher.des3.sz);
/* reset devId */
info->cipher.des3.des->devId = devIdArg;
}
else {
/* set devId to invalid, so software is used */
info->cipher.des3.des->devId = INVALID_DEVID;
ret = wc_Des3_CbcDecrypt(
info->cipher.des3.des,
info->cipher.des3.out,
info->cipher.des3.in,
info->cipher.des3.sz);
/* reset devId */
info->cipher.des3.des->devId = devIdArg;
}
}
#endif /* !NO_DES3 */
#endif /* !NO_AES || !NO_DES3 */
} }
#if !defined(NO_SHA) || !defined(NO_SHA256) #if !defined(NO_SHA) || !defined(NO_SHA256)
else if (info->algo_type == WC_ALGO_TYPE_HASH) { else if (info->algo_type == WC_ALGO_TYPE_HASH) {
@ -24527,6 +24557,10 @@ int cryptocb_test(void)
ret = aes_test(); ret = aes_test();
#endif #endif
#endif /* !NO_AES */ #endif /* !NO_AES */
#ifndef NO_DES3
if (ret == 0)
ret = des3_test();
#endif /* !NO_DES3 */
#if !defined(NO_SHA) || !defined(NO_SHA256) #if !defined(NO_SHA) || !defined(NO_SHA256)
#ifndef NO_SHA #ifndef NO_SHA
if (ret == 0) if (ret == 0)

View File

@ -55,6 +55,10 @@
#ifndef WC_NO_RNG #ifndef WC_NO_RNG
#include <wolfssl/wolfcrypt/random.h> #include <wolfssl/wolfcrypt/random.h>
#endif #endif
#ifndef NO_DES3
#include <wolfssl/wolfcrypt/des3.h>
#endif
/* Crypto Information Structure for callbacks */ /* Crypto Information Structure for callbacks */
typedef struct wc_CryptoInfo { typedef struct wc_CryptoInfo {
@ -115,7 +119,7 @@ typedef struct wc_CryptoInfo {
}; };
} pk; } pk;
#endif /* !NO_RSA || HAVE_ECC */ #endif /* !NO_RSA || HAVE_ECC */
#ifndef NO_AES #if !defined(NO_AES) || !defined(NO_DES3)
struct { struct {
int type; /* enum wc_CipherType */ int type; /* enum wc_CipherType */
int enc; int enc;
@ -154,9 +158,17 @@ typedef struct wc_CryptoInfo {
word32 sz; word32 sz;
} aescbc; } aescbc;
#endif /* HAVE_AES_CBC */ #endif /* HAVE_AES_CBC */
#ifndef NO_DES3
struct {
Des3* des;
byte* out;
const byte* in;
word32 sz;
} des3;
#endif
}; };
} cipher; } cipher;
#endif /* !NO_AES */ #endif /* !NO_AES || !NO_DES3 */
#if !defined(NO_SHA) || !defined(NO_SHA256) #if !defined(NO_SHA) || !defined(NO_SHA256)
struct { struct {
int type; /* enum wc_HashType */ int type; /* enum wc_HashType */
@ -252,6 +264,13 @@ WOLFSSL_LOCAL int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out,
#endif /* HAVE_AES_CBC */ #endif /* HAVE_AES_CBC */
#endif /* !NO_AES */ #endif /* !NO_AES */
#ifndef NO_DES3
WOLFSSL_LOCAL int wc_CryptoCb_Des3Encrypt(Des3* des3, byte* out,
const byte* in, word32 sz);
WOLFSSL_LOCAL int wc_CryptoCb_Des3Decrypt(Des3* des3, byte* out,
const byte* in, word32 sz);
#endif /* !NO_DES3 */
#ifndef NO_SHA #ifndef NO_SHA
WOLFSSL_LOCAL int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in, WOLFSSL_LOCAL int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in,
word32 inSz, byte* digest); word32 inSz, byte* digest);

View File

@ -103,6 +103,10 @@ typedef struct Des3 {
const byte* key_raw; const byte* key_raw;
const byte* iv_raw; const byte* iv_raw;
WC_ASYNC_DEV asyncDev; WC_ASYNC_DEV asyncDev;
#endif
#ifdef WOLF_CRYPTO_CB
int devId;
void* devCtx;
#endif #endif
void* heap; void* heap;
} Des3; } Des3;