diff --git a/src/include.am b/src/include.am index 1140dd0e8..d3fc04266 100644 --- a/src/include.am +++ b/src/include.am @@ -92,10 +92,9 @@ endif endif if BUILD_AES +src_libwolfssl_la_SOURCES += wolfcrypt/src/aes.c if BUILD_ARMASM src_libwolfssl_la_SOURCES += wolfcrypt/src/port/arm/armv8-aes.c -else -src_libwolfssl_la_SOURCES += wolfcrypt/src/aes.c endif endif diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index cb5ea7941..788e81ddd 100755 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -28,9 +28,9 @@ #include #ifndef NO_AES - #include + /* fips wrapper calls, user can call direct */ #ifdef HAVE_FIPS int wc_AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv, @@ -237,6 +237,8 @@ #include #endif +#ifndef WOLFSSL_ARMASM + #ifdef DEBUG_AESNI #include #endif @@ -5283,6 +5285,68 @@ int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz, #endif /* HAVE_AESCCM */ +/* Initialize Aes for use with async hardware */ +int wc_AesInit(Aes* aes, void* heap, int devId) +{ + int ret = 0; + + if (aes == NULL) + return BAD_FUNC_ARG; + + aes->heap = heap; + +#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES) + ret = wolfAsync_DevCtxInit(&aes->asyncDev, WOLFSSL_ASYNC_MARKER_AES, + aes->heap, devId); +#else + (void)devId; +#endif /* WOLFSSL_ASYNC_CRYPT */ + + return ret; +} + +/* Free Aes from use with async hardware */ +void wc_AesFree(Aes* aes) +{ + if (aes == NULL) + return; + +#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES) + wolfAsync_DevCtxFree(&aes->asyncDev, WOLFSSL_ASYNC_MARKER_AES); +#endif /* WOLFSSL_ASYNC_CRYPT */ +} + + +int wc_AesGetKeySize(Aes* aes, word32* keySize) +{ + int ret = 0; + + if (aes == NULL || keySize == NULL) { + return BAD_FUNC_ARG; + } + + switch (aes->rounds) { + case 10: + *keySize = 16; + break; + case 12: + *keySize = 24; + break; + case 14: + *keySize = 32; + break; + default: + *keySize = 0; + ret = BAD_FUNC_ARG; + } + + return ret; +} + +#endif /* !WOLFSSL_ARMASM */ +#endif /* !WOLFSSL_TI_CRYPT */ + + #ifdef HAVE_AES_KEYWRAP /* Initialize key wrap counter with value */ @@ -5454,67 +5518,5 @@ int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz, #endif /* HAVE_AES_KEYWRAP */ - -/* Initialize Aes for use with async hardware */ -int wc_AesInit(Aes* aes, void* heap, int devId) -{ - int ret = 0; - - if (aes == NULL) - return BAD_FUNC_ARG; - - aes->heap = heap; - -#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES) - ret = wolfAsync_DevCtxInit(&aes->asyncDev, WOLFSSL_ASYNC_MARKER_AES, - aes->heap, devId); -#else - (void)devId; -#endif /* WOLFSSL_ASYNC_CRYPT */ - - return ret; -} - -/* Free Aes from use with async hardware */ -void wc_AesFree(Aes* aes) -{ - if (aes == NULL) - return; - -#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES) - wolfAsync_DevCtxFree(&aes->asyncDev, WOLFSSL_ASYNC_MARKER_AES); -#endif /* WOLFSSL_ASYNC_CRYPT */ -} - - -int wc_AesGetKeySize(Aes* aes, word32* keySize) -{ - int ret = 0; - - if (aes == NULL || keySize == NULL) { - return BAD_FUNC_ARG; - } - - switch (aes->rounds) { - case 10: - *keySize = 16; - break; - case 12: - *keySize = 24; - break; - case 14: - *keySize = 32; - break; - default: - *keySize = 0; - ret = BAD_FUNC_ARG; - } - - return ret; -} - -#endif /* !WOLFSSL_TI_CRYPT */ - #endif /* HAVE_FIPS */ - -#endif /* NO_AES */ +#endif /* !NO_AES */ diff --git a/wolfcrypt/src/port/arm/armv8-aes.c b/wolfcrypt/src/port/arm/armv8-aes.c index 0dc43e4e4..623f27119 100644 --- a/wolfcrypt/src/port/arm/armv8-aes.c +++ b/wolfcrypt/src/port/arm/armv8-aes.c @@ -2531,7 +2531,7 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz, /* sanity checks */ if (aes == NULL || (iv == NULL && ivSz > 0) || (authTag == NULL) || - (authIn == NULL) || + (authIn == NULL && authInSz > 0) || (in == NULL && sz > 0) || (out == NULL && sz > 0)) { WOLFSSL_MSG("a NULL parameter passed in when size is larger than 0"); @@ -2596,7 +2596,7 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz, /* sanity checks */ if (aes == NULL || (iv == NULL && ivSz > 0) || (authTag == NULL) || - (authIn == NULL) || + (authIn == NULL && authInSz > 0) || (in == NULL && sz > 0) || (out == NULL && sz > 0)) { WOLFSSL_MSG("a NULL parameter passed in when size is larger than 0"); diff --git a/wolfcrypt/src/port/arm/armv8-sha256.c b/wolfcrypt/src/port/arm/armv8-sha256.c index 48d7230ef..bb01692d2 100644 --- a/wolfcrypt/src/port/arm/armv8-sha256.c +++ b/wolfcrypt/src/port/arm/armv8-sha256.c @@ -26,7 +26,9 @@ #include -#if !defined(NO_SHA256) && defined(WOLFSSL_ARMASM) +#ifdef WOLFSSL_ARMASM +#if !defined(NO_SHA256) || defined(WOLFSSL_SHA224) + #include #include #include @@ -56,7 +58,7 @@ static const ALIGN32 word32 K[64] = { }; -int wc_InitSha256_ex(Sha256* sha256, void* heap, int devId) +static int InitSha256(Sha256* sha256) { int ret = 0; @@ -77,41 +79,25 @@ int wc_InitSha256_ex(Sha256* sha256, void* heap, int devId) sha256->loLen = 0; sha256->hiLen = 0; - (void)heap; - (void)devId; - return ret; } -int wc_InitSha256(Sha256* sha256) -{ - return wc_InitSha256_ex(sha256, NULL, INVALID_DEVID); -} - -void wc_Sha256Free(Sha256* sha256) -{ - (void)sha256; -} - static INLINE void AddLength(Sha256* sha256, word32 len) { word32 tmp = sha256->loLen; - if ( (sha256->loLen += len) < tmp) + if ((sha256->loLen += len) < tmp) sha256->hiLen++; /* carry low to high */ } #ifdef __aarch64__ + /* ARMv8 hardware accleration */ -int wc_Sha256Update(Sha256* sha256, const byte* data, word32 len) +static INLINE int Sha256Update(Sha256* sha256, const byte* data, word32 len) { word32 add; word32 numBlocks; - if (sha256 == NULL || (data == NULL && len != 0)) { - return BAD_FUNC_ARG; - } - /* only perform actions if a buffer is passed in */ if (len > 0) { /* fill leftover buffer with data */ @@ -320,14 +306,10 @@ int wc_Sha256Update(Sha256* sha256, const byte* data, word32 len) } -int wc_Sha256Final(Sha256* sha256, byte* hash) +static INLINE int Sha256Final(Sha256* sha256, byte* hash) { byte* local; - if (sha256 == NULL || hash == NULL) { - return BAD_FUNC_ARG; - } - local = (byte*)sha256->buffer; AddLength(sha256, sha256->buffLen); /* before adding pads */ @@ -667,20 +649,17 @@ int wc_Sha256Final(Sha256* sha256, byte* hash) "v22", "v23", "v24", "v25" ); - return wc_InitSha256(sha256); /* reset state */ + return 0; } #else /* not using 64 bit */ + /* ARMv8 hardware accleration Aarch32 */ -int wc_Sha256Update(Sha256* sha256, const byte* data, word32 len) +static INLINE int Sha256Update(Sha256* sha256, const byte* data, word32 len) { word32 add; word32 numBlocks; - if (sha256 == NULL || (data == NULL && len != 0)) { - return BAD_FUNC_ARG; - } - /* only perform actions if a buffer is passed in */ if (len > 0) { /* fill leftover buffer with data */ @@ -903,7 +882,7 @@ int wc_Sha256Update(Sha256* sha256, const byte* data, word32 len) } -int wc_Sha256Final(Sha256* sha256, byte* hash) +static INLINE int Sha256Final(Sha256* sha256, byte* hash) { byte* local; @@ -1298,12 +1277,59 @@ int wc_Sha256Final(Sha256* sha256, byte* hash) "q15" ); - return wc_InitSha256(sha256); /* reset state */ + return 0; } #endif /* __aarch64__ */ +#ifndef NO_SHA256 + +int wc_InitSha256_ex(Sha256* sha256, void* heap, int devId) +{ + if (sha256 == NULL) + return BAD_FUNC_ARG; + + sha256->heap = heap; + (void)devId; + + return InitSha256(sha256); +} + +int wc_InitSha256(Sha256* sha256) +{ + return wc_InitSha256_ex(sha256, NULL, INVALID_DEVID); +} + +void wc_Sha256Free(Sha256* sha256) +{ + (void)sha256; +} + +int wc_Sha256Update(Sha256* sha256, const byte* data, word32 len) +{ + if (sha256 == NULL || (data == NULL && len != 0)) { + return BAD_FUNC_ARG; + } + + return Sha256Update(sha256, data, len); +} + +int wc_Sha256Final(Sha256* sha256, byte* hash) +{ + int ret; + + if (sha256 == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + + ret = Sha256Final(sha256, hash); + if (ret != 0) + return ret; + + return InitSha256(sha256); /* reset state */ +} + int wc_Sha256GetHash(Sha256* sha256, byte* hash) { int ret; @@ -1331,4 +1357,115 @@ int wc_Sha256Copy(Sha256* src, Sha256* dst) return ret; } -#endif /* NO_SHA256 and WOLFSSL_ARMASM */ +#endif /* !NO_SHA256 */ + + +#ifdef WOLFSSL_SHA224 + static int InitSha224(Sha224* sha224) + { + + int ret = 0; + + if (sha224 == NULL) { + return BAD_FUNC_ARG; + } + + sha224->digest[0] = 0xc1059ed8; + sha224->digest[1] = 0x367cd507; + sha224->digest[2] = 0x3070dd17; + sha224->digest[3] = 0xf70e5939; + sha224->digest[4] = 0xffc00b31; + sha224->digest[5] = 0x68581511; + sha224->digest[6] = 0x64f98fa7; + sha224->digest[7] = 0xbefa4fa4; + + sha224->buffLen = 0; + sha224->loLen = 0; + sha224->hiLen = 0; + + return ret; + } + + int wc_InitSha224_ex(Sha224* sha224, void* heap, int devId) + { + if (sha224 == NULL) + return BAD_FUNC_ARG; + + sha224->heap = heap; + (void)devId; + + return InitSha224(sha224); + } + + int wc_InitSha224(Sha224* sha224) + { + return wc_InitSha224_ex(sha224, NULL, INVALID_DEVID); + } + + int wc_Sha224Update(Sha224* sha224, const byte* data, word32 len) + { + int ret; + + if (sha224 == NULL || (data == NULL && len > 0)) { + return BAD_FUNC_ARG; + } + + ret = Sha256Update((Sha256 *)sha224, data, len); + + return ret; + } + + int wc_Sha224Final(Sha224* sha224, byte* hash) + { + int ret; + word32 hashTmp[SHA256_DIGEST_SIZE/sizeof(word32)]; + + if (sha224 == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + + ret = Sha256Final((Sha256*)sha224, (byte*)hashTmp); + if (ret != 0) + return ret; + + XMEMCPY(hash, hashTmp, SHA224_DIGEST_SIZE); + + return InitSha224(sha224); /* reset state */ + } + + void wc_Sha224Free(Sha224* sha224) + { + if (sha224 == NULL) + return; + } + + int wc_Sha224GetHash(Sha224* sha224, byte* hash) + { + int ret; + Sha224 tmpSha224; + + if (sha224 == NULL || hash == NULL) + return BAD_FUNC_ARG; + + ret = wc_Sha224Copy(sha224, &tmpSha224); + if (ret == 0) { + ret = wc_Sha224Final(&tmpSha224, hash); + } + return ret; + } + int wc_Sha224Copy(Sha224* src, Sha224* dst) + { + int ret = 0; + + if (src == NULL || dst == NULL) + return BAD_FUNC_ARG; + + XMEMCPY(dst, src, sizeof(Sha224)); + + return ret; + } + +#endif /* WOLFSSL_SHA224 */ + +#endif /* !NO_SHA256 || WOLFSSL_SHA224 */ +#endif /* WOLFSSL_ARMASM */ diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 68e183e0c..c445428ab 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -11723,6 +11723,7 @@ done: int ed25519_test(void) { + int ret; WC_RNG rng; #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\ defined(HAVE_ED25519_KEY_IMPORT) @@ -11731,7 +11732,7 @@ int ed25519_test(void) byte exportSKey[ED25519_KEY_SIZE]; word32 exportPSz; word32 exportSSz; - int i, ret; + int i; word32 outlen; #ifdef HAVE_ED25519_VERIFY int verify; diff --git a/wolfssl/wolfcrypt/fe_operations.h b/wolfssl/wolfcrypt/fe_operations.h index 0cb616e4e..318712a82 100644 --- a/wolfssl/wolfcrypt/fe_operations.h +++ b/wolfssl/wolfcrypt/fe_operations.h @@ -90,11 +90,9 @@ WOLFSSL_LOCAL void fe_mul121666(fe,fe); WOLFSSL_LOCAL void fe_cmov(fe,const fe, int); WOLFSSL_LOCAL void fe_pow22523(fe,const fe); -#if defined(HAVE___UINT128_T) /* 64 type needed for SHA512 */ WOLFSSL_LOCAL uint64_t load_3(const unsigned char *in); WOLFSSL_LOCAL uint64_t load_4(const unsigned char *in); -#endif #endif /* !CURVE25519_SMALL || !ED25519_SMALL */