]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
fips140: enforcement of hash and MACs use moved to crypto-api.c and hash_int.c
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Wed, 20 Dec 2017 14:36:59 +0000 (15:36 +0100)
committerNikos Mavrogiannopoulos <nmav@redhat.com>
Mon, 19 Feb 2018 07:39:36 +0000 (08:39 +0100)
Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
lib/crypto-api.c
lib/fips.h
lib/hash_int.c
lib/nettle/mac.c

index 788627a1188f9b4f24b9d270db98838b4df3e9f2..eeb2610a53d1e9e08db79f037c142876771ad451 100644 (file)
@@ -299,6 +299,7 @@ void gnutls_cipher_deinit(gnutls_cipher_hd_t handle)
 
 /* HMAC */
 
+
 /**
  * gnutls_hmac_init:
  * @dig: is a #gnutls_hmac_hd_t type
@@ -323,15 +324,9 @@ gnutls_hmac_init(gnutls_hmac_hd_t * dig,
                 gnutls_mac_algorithm_t algorithm,
                 const void *key, size_t keylen)
 {
-#ifdef ENABLE_FIPS140
        /* MD5 is only allowed internally for TLS */
-       if (_gnutls_fips_mode_enabled() != 0 &&
-               _gnutls_get_lib_state() != LIB_STATE_SELFTEST) {
-
-               if (algorithm == GNUTLS_MAC_MD5)
-                       return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
-       }
-#endif
+       if (is_mac_algo_forbidden(algorithm))
+               return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
 
        *dig = gnutls_malloc(sizeof(mac_hd_st));
        if (*dig == NULL) {
@@ -446,6 +441,9 @@ gnutls_hmac_fast(gnutls_mac_algorithm_t algorithm,
                 const void *key, size_t keylen,
                 const void *ptext, size_t ptext_len, void *digest)
 {
+       if (is_mac_algo_forbidden(algorithm))
+               return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
+
        return _gnutls_mac_fast(algorithm, key, keylen, ptext, ptext_len,
                                digest);
 }
@@ -470,15 +468,8 @@ int
 gnutls_hash_init(gnutls_hash_hd_t * dig,
                 gnutls_digest_algorithm_t algorithm)
 {
-#ifdef ENABLE_FIPS140
-       /* MD5 is only allowed internally for TLS */
-       if (_gnutls_fips_mode_enabled() != 0 &&
-               _gnutls_get_lib_state() != LIB_STATE_SELFTEST) {
-
-               if (algorithm == GNUTLS_DIG_MD5)
-                       return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
-       }
-#endif
+       if (is_mac_algo_forbidden(algorithm))
+               return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
 
        *dig = gnutls_malloc(sizeof(digest_hd_st));
        if (*dig == NULL) {
@@ -573,6 +564,9 @@ int
 gnutls_hash_fast(gnutls_digest_algorithm_t algorithm,
                 const void *ptext, size_t ptext_len, void *digest)
 {
+       if (is_mac_algo_forbidden(algorithm))
+               return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
+
        return _gnutls_hash_fast(algorithm, ptext, ptext_len, digest);
 }
 
index 4de5ea24222e3838d1aa1623b1b43928b43a408f..413cb3cd085399a0f81c18842826398ddbc26435 100644 (file)
@@ -73,4 +73,33 @@ void _gnutls_switch_lib_state(gnutls_lib_state_t state);
 void _gnutls_lib_simulate_error(void);
 void _gnutls_lib_force_operational(void);
 
+#ifdef ENABLE_FIPS140
+inline
+static unsigned is_mac_algo_forbidden(gnutls_mac_algorithm_t algo)
+{
+       if (_gnutls_fips_mode_enabled() != 0 &&
+           _gnutls_get_lib_state() != LIB_STATE_SELFTEST) {
+
+               switch(algo) {
+                       case GNUTLS_MAC_SHA1:
+                       case GNUTLS_MAC_SHA256:
+                       case GNUTLS_MAC_SHA384:
+                       case GNUTLS_MAC_SHA512:
+                       case GNUTLS_MAC_SHA224:
+                       case GNUTLS_MAC_SHA3_224:
+                       case GNUTLS_MAC_SHA3_256:
+                       case GNUTLS_MAC_SHA3_384:
+                       case GNUTLS_MAC_SHA3_512:
+                               return 0;
+                       default:
+                               return 1;
+               }
+       }
+
+       return 0;
+}
+#else
+# define is_mac_algo_forbidden(x) 0
+#endif
+
 #endif /* FIPS_H */
index ba2a5f01f10c822034f2e5f6cb2d1cffff14aee9..1c33796e772ba4da666bf7b911cb77214d03e4a3 100644 (file)
@@ -78,6 +78,9 @@ int _gnutls_digest_exists(gnutls_digest_algorithm_t algo)
 {
        const gnutls_crypto_digest_st *cc = NULL;
 
+       if (is_mac_algo_forbidden(algo))
+               return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
+
        cc = _gnutls_get_crypto_digest(algo);
        if (cc != NULL)
                return 1;
@@ -178,6 +181,9 @@ int _gnutls_mac_exists(gnutls_mac_algorithm_t algo)
        if (algo == GNUTLS_MAC_AEAD)
                return 1;
 
+       if (is_mac_algo_forbidden(algo))
+               return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
+
        cc = _gnutls_get_crypto_mac(algo);
        if (cc != NULL)
                return 1;
index e63ff6110b86666ecf4dc43867e71088471f0715..68fb47884942cd27c1ea15561cc8f4dd9bec927a 100644 (file)
@@ -32,7 +32,6 @@
 #include <nettle/sha3.h>
 #include <nettle/hmac.h>
 #include <nettle/umac.h>
-#include <fips.h>
 
 typedef void (*update_func) (void *, size_t, const uint8_t *);
 typedef void (*digest_func) (void *, size_t, uint8_t *);
@@ -109,6 +108,9 @@ _wrap_umac128_set_key(void *ctx, size_t len, const uint8_t * key)
 static int _mac_ctx_init(gnutls_mac_algorithm_t algo,
                         struct nettle_mac_ctx *ctx)
 {
+       /* Any FIPS140-2 related enforcement is performed on
+        * gnutls_hash_init() and gnutls_hmac_init() */
+
        ctx->set_nonce = NULL;
        switch (algo) {
        case GNUTLS_MAC_MD5:
@@ -154,9 +156,6 @@ static int _mac_ctx_init(gnutls_mac_algorithm_t algo,
                ctx->length = SHA512_DIGEST_SIZE;
                break;
        case GNUTLS_MAC_UMAC_96:
-               if (_gnutls_fips_mode_enabled() != 0)
-                       return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
-       
                ctx->update = (update_func) umac96_update;
                ctx->digest = (digest_func) umac96_digest;
                ctx->set_key = _wrap_umac96_set_key;
@@ -165,9 +164,6 @@ static int _mac_ctx_init(gnutls_mac_algorithm_t algo,
                ctx->length = 12;
                break;
        case GNUTLS_MAC_UMAC_128:
-               if (_gnutls_fips_mode_enabled() != 0)
-                       return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
-
                ctx->update = (update_func) umac128_update;
                ctx->digest = (digest_func) umac128_digest;
                ctx->set_key = _wrap_umac128_set_key;
@@ -216,14 +212,9 @@ static int wrap_nettle_mac_exists(gnutls_mac_algorithm_t algo)
        case GNUTLS_MAC_SHA256:
        case GNUTLS_MAC_SHA384:
        case GNUTLS_MAC_SHA512:
-               return 1;
-
        case GNUTLS_MAC_UMAC_96:
        case GNUTLS_MAC_UMAC_128:
-               if (_gnutls_fips_mode_enabled() != 0)
-                       return 0;
-               else
-                       return 1;
+               return 1;
        default:
                return 0;
        }
@@ -348,10 +339,7 @@ static int wrap_nettle_hash_exists(gnutls_digest_algorithm_t algo)
                return 0;
 #endif
        case GNUTLS_DIG_MD2:
-               if (_gnutls_fips_mode_enabled() != 0)
-                       return 0;
-               else
-                       return 1;
+               return 1;
        default:
                return 0;
        }
@@ -380,6 +368,8 @@ static void _md5_sha1_digest(void *_ctx, size_t len, uint8_t *digest)
 static int _ctx_init(gnutls_digest_algorithm_t algo,
                     struct nettle_hash_ctx *ctx)
 {
+       /* Any FIPS140-2 related enforcement is performed on
+        * gnutls_hash_init() and gnutls_hmac_init() */
        switch (algo) {
        case GNUTLS_DIG_MD5:
                md5_init(&ctx->ctx.md5);
@@ -462,9 +452,6 @@ static int _ctx_init(gnutls_digest_algorithm_t algo,
                break;
 #endif
        case GNUTLS_DIG_MD2:
-               if (_gnutls_fips_mode_enabled() != 0)
-                       return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
-
                md2_init(&ctx->ctx.md2);
                ctx->update = (update_func) md2_update;
                ctx->digest = (digest_func) md2_digest;