diff --git a/linuxkm/linuxkm_wc_port.h b/linuxkm/linuxkm_wc_port.h index f611dbc53..c8a2ddc92 100644 --- a/linuxkm/linuxkm_wc_port.h +++ b/linuxkm/linuxkm_wc_port.h @@ -30,6 +30,11 @@ #error Unsupported kernel. #endif + #if defined(HAVE_FIPS) && defined(LINUXKM_LKCAPI_REGISTER_AESXTS) && defined(CONFIG_CRYPTO_MANAGER_EXTRA_TESTS) + /* CONFIG_CRYPTO_MANAGER_EXTRA_TESTS expects AES-XTS-384 to work, even when CONFIG_CRYPTO_FIPS, but FIPS 140-3 only allows AES-XTS-256 and AES-XTS-512. */ + #error CONFIG_CRYPTO_MANAGER_EXTRA_TESTS is incompatible with FIPS wolfCrypt AES-XTS -- please reconfigure the target kernel to disable CONFIG_CRYPTO_MANAGER_EXTRA_TESTS. + #endif + #ifdef HAVE_CONFIG_H #ifndef PACKAGE_NAME #error wc_port.h included before config.h diff --git a/src/internal.c b/src/internal.c index 86ba579fd..652adc5c0 100644 --- a/src/internal.c +++ b/src/internal.c @@ -41793,7 +41793,7 @@ static int DefTicketEncCb(WOLFSSL* ssl, byte key_name[WOLFSSL_TICKET_NAME_SZ], ret = args->lastErr; args->lastErr = 0; /* reset */ /* On error 'ret' will be negative */ - mask = (byte)((ret >> ((sizeof(ret) * 8) - 1)) & 0xFF) - 1; + mask = (byte)(((unsigned int)ret >> ((sizeof(ret) * 8) - 1)) - 1); /* build PreMasterSecret */ ssl->arrays->preMasterSecret[0] = ssl->chVersion.major; diff --git a/tests/api/test_sha256.c b/tests/api/test_sha256.c index acb7748c6..9bd31c5b2 100644 --- a/tests/api/test_sha256.c +++ b/tests/api/test_sha256.c @@ -202,6 +202,7 @@ int test_wc_Sha256Transform(void) { EXPECT_DECLS; #if !defined(NO_SHA256) && (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && \ + !defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_AFALG_HASH) && \ !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \ (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) DIGEST_TRANSFORM_FINAL_RAW_TEST(wc_Sha256, Sha256, SHA256, diff --git a/tests/api/test_sha3.c b/tests/api/test_sha3.c index d5bb453f4..dc362a3ae 100644 --- a/tests/api/test_sha3.c +++ b/tests/api/test_sha3.c @@ -861,6 +861,7 @@ int test_wc_Shake128_Absorb(void) ExpectIntEQ(wc_InitShake128(&shake128, HEAP_HINT, INVALID_DEVID), 0); +#if !defined(HAVE_FIPS) || FIPS_VERSION_GE(7,0) ExpectIntEQ(wc_Shake128_Absorb(NULL , NULL , 1), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Shake128_Absorb(&shake128, NULL , 1), @@ -869,6 +870,8 @@ int test_wc_Shake128_Absorb(void) WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Shake128_Absorb(&shake128, NULL, 0), 0); +#endif + ExpectIntEQ(wc_Shake128_Absorb(&shake128, (byte*)"a", 1), 0); wc_Shake128_Free(&shake128); @@ -885,6 +888,7 @@ int test_wc_Shake128_SqueezeBlocks(void) ExpectIntEQ(wc_InitShake128(&shake128, HEAP_HINT, INVALID_DEVID), 0); +#if !defined(HAVE_FIPS) || FIPS_VERSION_GE(7,0) ExpectIntEQ(wc_Shake128_SqueezeBlocks(NULL , NULL, 1), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Shake128_SqueezeBlocks(&shake128, NULL, 1), @@ -893,6 +897,7 @@ int test_wc_Shake128_SqueezeBlocks(void) WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Shake128_SqueezeBlocks(&shake128, NULL, 0), 0); +#endif ExpectIntEQ(wc_Shake128_SqueezeBlocks(&shake128, hash, 1), 0); wc_Shake128_Free(&shake128); @@ -1281,6 +1286,7 @@ int test_wc_Shake256_Absorb(void) ExpectIntEQ(wc_InitShake256(&shake256, HEAP_HINT, INVALID_DEVID), 0); +#if !defined(HAVE_FIPS) || FIPS_VERSION_GE(7,0) ExpectIntEQ(wc_Shake256_Absorb(NULL , NULL , 1), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Shake256_Absorb(&shake256, NULL , 1), @@ -1289,6 +1295,7 @@ int test_wc_Shake256_Absorb(void) WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Shake256_Absorb(&shake256, NULL, 0), 0); +#endif ExpectIntEQ(wc_Shake256_Absorb(&shake256, (byte*)"a", 1), 0); wc_Shake256_Free(&shake256); @@ -1305,6 +1312,7 @@ int test_wc_Shake256_SqueezeBlocks(void) ExpectIntEQ(wc_InitShake256(&shake256, HEAP_HINT, INVALID_DEVID), 0); +#if !defined(HAVE_FIPS) || FIPS_VERSION_GE(7,0) ExpectIntEQ(wc_Shake256_SqueezeBlocks(NULL , NULL, 1), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Shake256_SqueezeBlocks(&shake256, NULL, 1), @@ -1313,6 +1321,7 @@ int test_wc_Shake256_SqueezeBlocks(void) WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_Shake256_SqueezeBlocks(&shake256, NULL, 0), 0); +#endif ExpectIntEQ(wc_Shake256_SqueezeBlocks(&shake256, hash, 1), 0); wc_Shake256_Free(&shake256); diff --git a/wolfcrypt/src/misc.c b/wolfcrypt/src/misc.c index 5af55ec67..766ce5db8 100644 --- a/wolfcrypt/src/misc.c +++ b/wolfcrypt/src/misc.c @@ -189,6 +189,28 @@ WC_MISC_STATIC WC_INLINE void ByteReverseWords(word32* out, const word32* in, out[i] = ByteReverseWord32(in[i]); } #ifdef WOLFSSL_USE_ALIGN + else if (((size_t)in & 0x3) == 0) { + byte *out_bytes = (byte *)out; + word32 scratch; + + byteCount &= ~0x3U; + + for (i = 0; i < byteCount; i += (word32)sizeof(word32)) { + scratch = ByteReverseWord32(*in++); + XMEMCPY(out_bytes + i, &scratch, sizeof(scratch)); + } + } + else if (((size_t)out & 0x3) == 0) { + byte *in_bytes = (byte *)in; + word32 scratch; + + byteCount &= ~0x3U; + + for (i = 0; i < byteCount; i += (word32)sizeof(word32)) { + XMEMCPY(&scratch, in_bytes + i, sizeof(scratch)); + *out++ = ByteReverseWord32(scratch); + } + } else { byte *in_bytes = (byte *)in; byte *out_bytes = (byte *)out; @@ -335,9 +357,51 @@ WC_MISC_STATIC WC_INLINE void ByteReverseWords64(word64* out, const word64* in, { word32 count = byteCount/(word32)sizeof(word64), i; - for (i = 0; i < count; i++) - out[i] = ByteReverseWord64(in[i]); +#ifdef WOLFSSL_USE_ALIGN + if ((((size_t)in & 0x7) == 0) && + (((size_t)out & 0x7) == 0)) +#endif + { + for (i = 0; i < count; i++) + out[i] = ByteReverseWord64(in[i]); + } +#ifdef WOLFSSL_USE_ALIGN + else if (((size_t)in & 0x7) == 0) { + byte *out_bytes = (byte *)out; + word64 scratch; + byteCount &= ~0x7U; + + for (i = 0; i < byteCount; i += (word32)sizeof(word64)) { + scratch = ByteReverseWord64(*in++); + XMEMCPY(out_bytes + i, &scratch, sizeof(scratch)); + } + } + else if (((size_t)out & 0x7) == 0) { + byte *in_bytes = (byte *)in; + word64 scratch; + + byteCount &= ~0x7U; + + for (i = 0; i < byteCount; i += (word32)sizeof(word64)) { + XMEMCPY(&scratch, in_bytes + i, sizeof(scratch)); + *out++ = ByteReverseWord64(scratch); + } + } + else { + byte *in_bytes = (byte *)in; + byte *out_bytes = (byte *)out; + word64 scratch; + + byteCount &= ~0x7U; + + for (i = 0; i < byteCount; i += (word32)sizeof(word64)) { + XMEMCPY(&scratch, in_bytes + i, sizeof(scratch)); + scratch = ByteReverseWord64(scratch); + XMEMCPY(out_bytes + i, &scratch, sizeof(scratch)); + } + } +#endif } #endif /* WORD64_AVAILABLE && !WOLFSSL_NO_WORD64_OPS */ diff --git a/wolfssl/wolfcrypt/hash.h b/wolfssl/wolfcrypt/hash.h index 84e5212c1..02d99d4c2 100644 --- a/wolfssl/wolfcrypt/hash.h +++ b/wolfssl/wolfcrypt/hash.h @@ -83,16 +83,6 @@ enum wc_MACAlgorithm { sm3_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 */ -#ifdef WOLFSSL_SHA3 - WC_HASH_SHA3_KECCAK256 =0x00010000, /* Older KECCAK256 */ -#endif - WOLF_ENUM_DUMMY_LAST_ELEMENT(WC_HASH) -}; - /* hash union */ typedef union { #ifndef NO_MD5 diff --git a/wolfssl/wolfcrypt/sha256.h b/wolfssl/wolfcrypt/sha256.h index 831230614..7a064a0ca 100644 --- a/wolfssl/wolfcrypt/sha256.h +++ b/wolfssl/wolfcrypt/sha256.h @@ -264,10 +264,14 @@ struct wc_Sha256 { WOLFSSL_API int wc_InitSha256(wc_Sha256* sha); WOLFSSL_API int wc_InitSha256_ex(wc_Sha256* sha, void* heap, int devId); WOLFSSL_API int wc_Sha256Update(wc_Sha256* sha, const byte* data, word32 len); + +#if !defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_AFALG_HASH) WOLFSSL_API int wc_Sha256FinalRaw(wc_Sha256* sha256, byte* hash); +#endif WOLFSSL_API int wc_Sha256Final(wc_Sha256* sha256, byte* hash); WOLFSSL_API void wc_Sha256Free(wc_Sha256* sha256); -#if defined(OPENSSL_EXTRA) || defined(HAVE_CURL) +#if (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && \ + !defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_AFALG_HASH) WOLFSSL_API int wc_Sha256Transform(wc_Sha256* sha, const unsigned char* data); #endif #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_FULL_HASH) diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index a87a309ab..97b269957 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -1247,6 +1247,16 @@ typedef struct w64wrapper { #endif /* HAVE_SELFTEST */ }; + 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 */ + #ifdef WOLFSSL_SHA3 + WC_HASH_SHA3_KECCAK256 =0x00010000, /* Older KECCAK256 */ + #endif + WOLF_ENUM_DUMMY_LAST_ELEMENT(WC_HASH) + }; + /* cipher types */ enum wc_CipherType { WC_CIPHER_NONE = 0,