From c98ce0d18c460c6fed004d61c96c6fde91ee033b Mon Sep 17 00:00:00 2001 From: kaleb-himes Date: Thu, 24 Aug 2023 14:29:32 -0700 Subject: [PATCH 1/2] FIPS 140-3 Pilot Program Check-in --- wolfcrypt/src/aes.c | 13 +++++++++++++ wolfcrypt/src/dh.c | 2 +- wolfcrypt/src/hmac.c | 2 ++ wolfcrypt/src/kdf.c | 1 + wolfssl/wolfcrypt/aes.h | 3 +++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index bc161630c..10c6ed646 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -4385,6 +4385,19 @@ int wc_AesSetIV(Aes* aes, const byte* iv) return 0; } + int wc_AesCtrSetKey(Aes* aes, const byte* key, word32 len, + const byte* iv, int dir) + { + if (aes == NULL) { + return BAD_FUNC_ARG; + } + if (len > sizeof(aes->key)) { + return BAD_FUNC_ARG; + } + + return wc_AesSetKeyLocal(aes, key, len, iv, dir, 0); + } + #endif /* NEED_AES_CTR_SOFT */ #endif /* WOLFSSL_AES_COUNTER */ diff --git a/wolfcrypt/src/dh.c b/wolfcrypt/src/dh.c index 38425aaa8..c1860e24c 100644 --- a/wolfcrypt/src/dh.c +++ b/wolfcrypt/src/dh.c @@ -1336,7 +1336,7 @@ static int GeneratePublicDh(DhKey* key, byte* priv, word32 privSz, *pubSz = binSz; mp_clear(y); - mp_clear(x); + mp_forcezero(x); #ifdef WOLFSSL_SMALL_STACK XFREE(y, key->heap, DYNAMIC_TYPE_DH); XFREE(x, key->heap, DYNAMIC_TYPE_DH); diff --git a/wolfcrypt/src/hmac.c b/wolfcrypt/src/hmac.c index 47ed04285..edaa178c0 100644 --- a/wolfcrypt/src/hmac.c +++ b/wolfcrypt/src/hmac.c @@ -1196,6 +1196,7 @@ int wolfSSL_GetHmacMaxSize(void) ret = wc_HmacUpdate(&myHmac, inKey, inKeySz); if (ret == 0) ret = wc_HmacFinal(&myHmac, out); + ForceZero(&myHmac, sizeof(myHmac)); wc_HmacFree(&myHmac); } @@ -1261,6 +1262,7 @@ int wolfSSL_GetHmacMaxSize(void) n++; } + ForceZero(&myHmac, sizeof(myHmac)); wc_HmacFree(&myHmac); return ret; diff --git a/wolfcrypt/src/kdf.c b/wolfcrypt/src/kdf.c index 6556bfdf9..cece18239 100644 --- a/wolfcrypt/src/kdf.c +++ b/wolfcrypt/src/kdf.c @@ -734,6 +734,7 @@ int wc_SSH_KDF(byte hashId, byte keyId, byte* key, word32 keySz, } } + ForceZero(&hash, sizeof(hash)); _HashFree(enmhashId, &hash); return ret; diff --git a/wolfssl/wolfcrypt/aes.h b/wolfssl/wolfcrypt/aes.h index 3ba981062..9207cc595 100644 --- a/wolfssl/wolfcrypt/aes.h +++ b/wolfssl/wolfcrypt/aes.h @@ -367,6 +367,9 @@ WOLFSSL_API int wc_AesEcbDecrypt(Aes* aes, byte* out, #ifdef WOLFSSL_AES_COUNTER WOLFSSL_API int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz); + WOLFSSL_API int wc_AesCtrSetKey(Aes* aes, const byte* key, word32 len, + const byte* iv, int dir); + #endif /* AES-DIRECT */ #if defined(WOLFSSL_AES_DIRECT) From 3a5e1716363be89f4f4abc59fe366ff62cfb0505 Mon Sep 17 00:00:00 2001 From: kaleb-himes Date: Mon, 28 Aug 2023 14:22:37 -0700 Subject: [PATCH 2/2] Check-in fips_test.h changes to master as well --- wolfssl/wolfcrypt/fips_test.h | 42 ++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/wolfssl/wolfcrypt/fips_test.h b/wolfssl/wolfcrypt/fips_test.h index d2e986714..d3309361c 100644 --- a/wolfssl/wolfcrypt/fips_test.h +++ b/wolfssl/wolfcrypt/fips_test.h @@ -31,6 +31,25 @@ extern "C" { #endif +/* Added for FIPS v5.3 or later */ +#if defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3) + /* Determine FIPS in core hash type and size */ + #ifndef NO_SHA256 + #define FIPS_IN_CORE_DIGEST_SIZE 32 + #define FIPS_IN_CORE_HASH_TYPE WC_SHA256 + #define FIPS_IN_CORE_KEY_SZ 32 + #define FIPS_IN_CORE_VERIFY_SZ FIPS_IN_CORE_KEY_SZ + #elif defined(WOLFSSL_SHA384) + #define FIPS_IN_CORE_DIGEST_SIZE 48 + #define FIPS_IN_CORE_HASH_TYPE WC_SHA384 + #define FIPS_IN_CORE_KEY_SZ 48 + #define FIPS_IN_CORE_VERIFY_SZ FIPS_IN_CORE_KEY_SZ + #else + #error No FIPS hash (SHA2-256 or SHA2-384) + #endif +#endif /* FIPS v5.3 or later */ + + enum FipsCastId { FIPS_CAST_AES_CBC, FIPS_CAST_AES_GCM, @@ -58,10 +77,10 @@ enum FipsCastStateId { }; enum FipsModeId { - FIPS_MODE_INIT, - FIPS_MODE_NORMAL, - FIPS_MODE_DEGRADED, - FIPS_MODE_FAILED + FIPS_MODE_INIT = 0, + FIPS_MODE_NORMAL = 1, + FIPS_MODE_DEGRADED = 2, + FIPS_MODE_FAILED = 3 }; @@ -73,20 +92,21 @@ WOLFSSL_API int wolfCrypt_SetCb_fips(wolfCrypt_fips_cb cbf); /* Public get status functions */ WOLFSSL_API int wolfCrypt_GetStatus_fips(void); +WOLFSSL_API int wolfCrypt_GetMode_fips(void); WOLFSSL_API const char* wolfCrypt_GetCoreHash_fips(void); #ifdef HAVE_FORCE_FIPS_FAILURE /* Public function to force failure mode for operational testing */ - WOLFSSL_API int wolfCrypt_SetStatus_fips(int); + WOLFSSL_API int wolfCrypt_SetStatus_fips(int status); #endif -WOLFSSL_LOCAL int DoIntegrityTest(char*, int); -WOLFSSL_LOCAL int DoPOST(char*, int); -WOLFSSL_LOCAL int DoCAST(int); -WOLFSSL_LOCAL int DoKnownAnswerTests(char*, int); /* FIPSv1 and FIPSv2 */ +WOLFSSL_LOCAL int DoPOST(char* base16_hash, int base16_hashSz); +WOLFSSL_LOCAL int DoCAST(int type); +WOLFSSL_LOCAL int DoKnownAnswerTests(char* base16_hash, int base16_hashSz); /* FIPSv1 and FIPSv2 */ -WOLFSSL_API int wc_RunCast_fips(int); -WOLFSSL_API int wc_GetCastStatus_fips(int); +WOLFSSL_API int wc_RunCast_fips(int type); +WOLFSSL_API int wc_GetCastStatus_fips(int type); +WOLFSSL_API int wc_RunAllCast_fips(void); #ifdef __cplusplus } /* extern "C" */