crypto: caam - Forbid 2-key 3DES in FIPS mode
authorHerbert Xu <herbert@gondor.apana.org.au>
Thu, 11 Apr 2019 08:51:02 +0000 (16:51 +0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Thu, 18 Apr 2019 14:14:59 +0000 (22:14 +0800)
This patch forbids the use of 2-key 3DES (K1 == K3) in FIPS mode.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Reviewed-by: Horia Geantă <horia.geanta@nxp.com>
Tested-by: Iuliana Prodan <iuliana.prodan@nxp.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
drivers/crypto/caam/caamalg.c
drivers/crypto/caam/caamalg_qi.c
drivers/crypto/caam/caamalg_qi2.c

index 579578498debba63f5bbe9efa54b872fc1da1c18..0030cee3e75d17bb96d92a8d5570ba031be50bf9 100644 (file)
@@ -638,6 +638,39 @@ badkey:
        return -EINVAL;
 }
 
+static int des3_aead_setkey(struct crypto_aead *aead, const u8 *key,
+                           unsigned int keylen)
+{
+       struct crypto_authenc_keys keys;
+       u32 flags;
+       int err;
+
+       err = crypto_authenc_extractkeys(&keys, key, keylen);
+       if (unlikely(err))
+               goto badkey;
+
+       err = -EINVAL;
+       if (keys.enckeylen != DES3_EDE_KEY_SIZE)
+               goto badkey;
+
+       flags = crypto_aead_get_flags(aead);
+       err = __des3_verify_key(&flags, keys.enckey);
+       if (unlikely(err)) {
+               crypto_aead_set_flags(aead, flags);
+               goto out;
+       }
+
+       err = aead_setkey(aead, key, keylen);
+
+out:
+       memzero_explicit(&keys, sizeof(keys));
+       return err;
+
+badkey:
+       crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
+       goto out;
+}
+
 static int gcm_setkey(struct crypto_aead *aead,
                      const u8 *key, unsigned int keylen)
 {
@@ -2457,7 +2490,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2479,7 +2512,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2502,7 +2535,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2525,7 +2558,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2548,7 +2581,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2571,7 +2604,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2594,7 +2627,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2617,7 +2650,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2640,7 +2673,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2663,7 +2696,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2686,7 +2719,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2709,7 +2742,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
index c61921d32489a53386d4eafaf258150bff1bd368..70af211d2d018684b012f611fbe4f80446d903af 100644 (file)
@@ -292,6 +292,39 @@ badkey:
        return -EINVAL;
 }
 
+static int des3_aead_setkey(struct crypto_aead *aead, const u8 *key,
+                           unsigned int keylen)
+{
+       struct crypto_authenc_keys keys;
+       u32 flags;
+       int err;
+
+       err = crypto_authenc_extractkeys(&keys, key, keylen);
+       if (unlikely(err))
+               goto badkey;
+
+       err = -EINVAL;
+       if (keys.enckeylen != DES3_EDE_KEY_SIZE)
+               goto badkey;
+
+       flags = crypto_aead_get_flags(aead);
+       err = __des3_verify_key(&flags, keys.enckey);
+       if (unlikely(err)) {
+               crypto_aead_set_flags(aead, flags);
+               goto out;
+       }
+
+       err = aead_setkey(aead, key, keylen);
+
+out:
+       memzero_explicit(&keys, sizeof(keys));
+       return err;
+
+badkey:
+       crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
+       goto out;
+}
+
 static int gcm_set_sh_desc(struct crypto_aead *aead)
 {
        struct caam_ctx *ctx = crypto_aead_ctx(aead);
@@ -667,6 +700,13 @@ badkey:
        return -EINVAL;
 }
 
+static int des3_skcipher_setkey(struct crypto_skcipher *skcipher,
+                               const u8 *key, unsigned int keylen)
+{
+       return unlikely(des3_verify_key(skcipher, key)) ?:
+              skcipher_setkey(skcipher, key, keylen);
+}
+
 static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
                               unsigned int keylen)
 {
@@ -1382,7 +1422,7 @@ static struct caam_skcipher_alg driver_algs[] = {
                                .cra_driver_name = "cbc-3des-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = skcipher_setkey,
+                       .setkey = des3_skcipher_setkey,
                        .encrypt = skcipher_encrypt,
                        .decrypt = skcipher_decrypt,
                        .min_keysize = DES3_EDE_KEY_SIZE,
@@ -1798,7 +1838,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -1820,7 +1860,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -1843,7 +1883,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -1866,7 +1906,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -1889,7 +1929,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -1912,7 +1952,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -1935,7 +1975,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -1958,7 +1998,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -1981,7 +2021,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2004,7 +2044,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2027,7 +2067,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2050,7 +2090,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
index c2c1abc68f81ef241351ab696a5d6c95e03906f6..d19c97acf1aadfa755ed05bf79988f77a7c771c0 100644 (file)
@@ -323,6 +323,39 @@ badkey:
        return -EINVAL;
 }
 
+static int des3_aead_setkey(struct crypto_aead *aead, const u8 *key,
+                           unsigned int keylen)
+{
+       struct crypto_authenc_keys keys;
+       u32 flags;
+       int err;
+
+       err = crypto_authenc_extractkeys(&keys, key, keylen);
+       if (unlikely(err))
+               goto badkey;
+
+       err = -EINVAL;
+       if (keys.enckeylen != DES3_EDE_KEY_SIZE)
+               goto badkey;
+
+       flags = crypto_aead_get_flags(aead);
+       err = __des3_verify_key(&flags, keys.enckey);
+       if (unlikely(err)) {
+               crypto_aead_set_flags(aead, flags);
+               goto out;
+       }
+
+       err = aead_setkey(aead, key, keylen);
+
+out:
+       memzero_explicit(&keys, sizeof(keys));
+       return err;
+
+badkey:
+       crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
+       goto out;
+}
+
 static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
                                           bool encrypt)
 {
@@ -938,6 +971,13 @@ static int skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
        return 0;
 }
 
+static int des3_skcipher_setkey(struct crypto_skcipher *skcipher,
+                               const u8 *key, unsigned int keylen)
+{
+       return unlikely(des3_verify_key(skcipher, key)) ?:
+              skcipher_setkey(skcipher, key, keylen);
+}
+
 static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
                               unsigned int keylen)
 {
@@ -1484,7 +1524,7 @@ static struct caam_skcipher_alg driver_algs[] = {
                                .cra_driver_name = "cbc-3des-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = skcipher_setkey,
+                       .setkey = des3_skcipher_setkey,
                        .encrypt = skcipher_encrypt,
                        .decrypt = skcipher_decrypt,
                        .min_keysize = DES3_EDE_KEY_SIZE,
@@ -1916,7 +1956,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -1938,7 +1978,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -1961,7 +2001,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -1984,7 +2024,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2007,7 +2047,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2030,7 +2070,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2053,7 +2093,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2076,7 +2116,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2099,7 +2139,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2122,7 +2162,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2145,7 +2185,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,
@@ -2168,7 +2208,7 @@ static struct caam_aead_alg driver_aeads[] = {
                                                   "cbc-des3_ede-caam-qi2",
                                .cra_blocksize = DES3_EDE_BLOCK_SIZE,
                        },
-                       .setkey = aead_setkey,
+                       .setkey = des3_aead_setkey,
                        .setauthsize = aead_setauthsize,
                        .encrypt = aead_encrypt,
                        .decrypt = aead_decrypt,