All of lore.kernel.org
 help / color / mirror / Atom feed
From: Herbert Xu <herbert@gondor.apana.org.au>
To: Linux Crypto Mailing List <linux-crypto@vger.kernel.org>
Subject: [PATCH 1/24] crypto: des_generic - Forbid 2-key in 3DES and add helpers
Date: Thu, 11 Apr 2019 16:50:56 +0800	[thread overview]
Message-ID: <E1hEVQ8-0006lO-MM@gondobar> (raw)
In-Reply-To: 20190411084707.h56mz2z7jxusnr7u@gondor.apana.org.au

This patch adds a requirement to the generic 3DES implementation
such that 2-key 3DES (K1 == K3) is no longer allowed in FIPS mode.

We will also provide helpers that may be used by drivers that
implement 3DES to make the same check.
   
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
---

 crypto/des_generic.c |   11 ++++-------
 include/crypto/des.h |   43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/crypto/des_generic.c b/crypto/des_generic.c
index 1e6621665dd9..ebec1fb08c45 100644
--- a/crypto/des_generic.c
+++ b/crypto/des_generic.c
@@ -862,14 +862,11 @@ static void des_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
 int __des3_ede_setkey(u32 *expkey, u32 *flags, const u8 *key,
 		      unsigned int keylen)
 {
-	const u32 *K = (const u32 *)key;
+	int err;
 
-	if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
-		     !((K[2] ^ K[4]) | (K[3] ^ K[5]))) &&
-		     (*flags & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) {
-		*flags |= CRYPTO_TFM_RES_WEAK_KEY;
-		return -EINVAL;
-	}
+	err = __des3_verify_key(flags, key);
+	if (unlikely(err))
+		return err;
 
 	des_ekey(expkey, key); expkey += DES_EXPKEY_WORDS; key += DES_KEY_SIZE;
 	dkey(expkey, key); expkey += DES_EXPKEY_WORDS; key += DES_KEY_SIZE;
diff --git a/include/crypto/des.h b/include/crypto/des.h
index d4094d58ac54..72c7c8e5a5a7 100644
--- a/include/crypto/des.h
+++ b/include/crypto/des.h
@@ -6,6 +6,11 @@
 #ifndef __CRYPTO_DES_H
 #define __CRYPTO_DES_H
 
+#include <crypto/skcipher.h>
+#include <linux/compiler.h>
+#include <linux/fips.h>
+#include <linux/string.h>
+
 #define DES_KEY_SIZE		8
 #define DES_EXPKEY_WORDS	32
 #define DES_BLOCK_SIZE		8
@@ -14,6 +19,44 @@
 #define DES3_EDE_EXPKEY_WORDS	(3 * DES_EXPKEY_WORDS)
 #define DES3_EDE_BLOCK_SIZE	DES_BLOCK_SIZE
 
+static inline int __des3_verify_key(u32 *flags, const u8 *key)
+{
+	int err = -EINVAL;
+	u32 K[6];
+
+	memcpy(K, key, DES3_EDE_KEY_SIZE);
+
+	if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
+		     !((K[2] ^ K[4]) | (K[3] ^ K[5]))) &&
+		     (fips_enabled ||
+		      (*flags & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)))
+		goto bad;
+
+	if (unlikely(!((K[0] ^ K[4]) | (K[1] ^ K[5]))) && fips_enabled)
+		goto bad;
+
+	err = 0;
+
+out:
+	memzero_explicit(K, DES3_EDE_KEY_SIZE);
+
+	return err;
+
+bad:
+	*flags |= CRYPTO_TFM_RES_WEAK_KEY;
+	goto out;
+}
+
+static inline int des3_verify_key(struct crypto_skcipher *tfm, const u8 *key)
+{
+	u32 flags;
+	int err;
+
+	flags = crypto_skcipher_get_flags(tfm);
+	err = __des3_verify_key(&flags, key);
+	crypto_skcipher_set_flags(tfm, flags);
+	return err;
+}
 
 extern unsigned long des_ekey(u32 *pe, const u8 *k);
 

  reply	other threads:[~2019-04-11  8:50 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-11  8:47 [PATCH 0/24] Forbid 2-key 3DES in FIPS mode Herbert Xu
2019-04-11  8:50 ` Herbert Xu [this message]
2019-04-11  8:50 ` [PATCH 2/24] crypto: s390 - " Herbert Xu
2019-04-11  8:50 ` [PATCH 3/24] crypto: sparc " Herbert Xu
2019-04-11  8:51 ` [PATCH 4/24] crypto: atmel " Herbert Xu
2019-04-11  8:51 ` [PATCH 5/24] crypto: bcm " Herbert Xu
2019-04-11  8:51 ` [PATCH 6/24] crypto: caam " Herbert Xu
2019-04-16 12:52   ` Horia Geanta
2019-04-16 12:53   ` Iuliana Prodan
2019-04-11  8:51 ` [PATCH 7/24] crypto: cavium " Herbert Xu
2019-04-11  8:51 ` [PATCH 8/24] crypto: nitrox " Herbert Xu
2019-04-11  8:51 ` [PATCH 9/24] crypto: ccp " Herbert Xu
2019-04-11  8:51 ` [PATCH 10/24] crypto: ccree " Herbert Xu
2019-04-11  9:27   ` Stephan Mueller
2019-04-11  9:30     ` Herbert Xu
2019-04-11 11:07   ` Gilad Ben-Yossef
2019-04-11  8:51 ` [PATCH 11/24] crypto: hifn_795x " Herbert Xu
2019-04-11  8:51 ` [PATCH 12/24] crypto: hisilicon " Herbert Xu
2019-04-11  8:51 ` [PATCH 13/24] crypto: inside-secure " Herbert Xu
2019-04-11  8:51 ` [PATCH 14/24] crypto: ixp4xx " Herbert Xu
2019-04-11  8:51 ` [PATCH 15/24] crypto: marvell " Herbert Xu
2019-04-11  8:51 ` [PATCH 16/24] crypto: n2 " Herbert Xu
2019-04-11  8:51 ` [PATCH 17/24] crypto: omap " Herbert Xu
2019-04-11  8:51 ` [PATCH 18/24] crypto: picoxcell " Herbert Xu
2019-04-11  8:51 ` [PATCH 19/24] crypto: qce " Herbert Xu
2019-04-11  8:51 ` [PATCH 20/24] crypto: rockchip " Herbert Xu
2019-04-11  8:51 ` [PATCH 21/24] crypto: stm32 " Herbert Xu
2019-04-12 13:36   ` Lionel DEBIEVE
2019-04-13 13:50     ` Herbert Xu
2019-04-15  9:45       ` Lionel DEBIEVE
2019-04-11  8:51 ` [PATCH 22/24] crypto: sun4i-ss " Herbert Xu
2019-04-11 13:51   ` Corentin Labbe
2019-04-11  8:51 ` [PATCH 23/24] crypto: talitos " Herbert Xu
2019-04-11  8:51 ` [PATCH 24/24] crypto: ux500 " Herbert Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1hEVQ8-0006lO-MM@gondobar \
    --to=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.