From patchwork Fri Jun 28 09:35:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 168051 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp3454899ilk; Fri, 28 Jun 2019 02:36:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqwELje7O07z4zjIg6sgAxwRA1HZgusXxU62zx3Dl+scT94g+NGN6w1E1pDiw62D2Wd023zO X-Received: by 2002:a17:90a:b00b:: with SMTP id x11mr12051128pjq.120.1561714574777; Fri, 28 Jun 2019 02:36:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561714574; cv=none; d=google.com; s=arc-20160816; b=xi5f6iOSZLn1OcECAw6Q3exVLNWSgS9DmHGTg2mmf1MhsANE7R76622PnULMm3vDEH si2/rguy8AlblLYOMZqE+JnJm9LlNBsInIe/ucE4BzG1kPrBSJQuu+tJbqYursNp3rIG J6J458dufNDqtTtyANg8AEVXy4TTQlHSyuQi2ZusQZekFDjQEMIQUdkf7uPnXtrnXbDm X131aAN6ReJ5/RLY9X69QAeey//yTrymk/ykgvKlUunHTB3J4745A9DEgKMuuvvzAEB0 17WQ6kGfFx5C8AdKeqs2fVTQ8woeygr9Nm/Ht3+E1lS0XYFN9OiQqLz/7uTFeqRu/soV WKyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=N2BlLzFuQehpzWwGcUqNkw7wuytXhknZeoPcZaYTlhI=; b=XHC9s5DaEajfoSHYVKizf1DVcXKsSlXJ+kGoYI1VQyor5uSyNkO3lYZvdiMw67gJxe 8a5PcUpDd5LPreTcauROYZ3FYM7QzF2e1zRjV375Q0cBu7raAcwBRHNd4NS3SjkXWN3Q +qp32y8dRbWElFDa3WSbtjwdSsIFg4hKiigYHclsbu4/ZPMFedt4OIWsJW5zVtHjncvl p/5AvLe7DNe1sWmWrkwH91c2aVsACE5uKmRxW0HECfFuN+sN512hRParHhZtu2sf6bD4 py61+TH/4SfFLPQFAkIZ0GzUgfJ5wYvXcqxiPuIlx/mzC9GgzoQWaOeSV1ACkliR8Qfr HVWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sy6VsdaN; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j7si2015771pfi.10.2019.06.28.02.36.14; Fri, 28 Jun 2019 02:36:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sy6VsdaN; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726786AbfF1JgL (ORCPT + 3 others); Fri, 28 Jun 2019 05:36:11 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:33757 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726739AbfF1JgK (ORCPT ); Fri, 28 Jun 2019 05:36:10 -0400 Received: by mail-wm1-f67.google.com with SMTP id h19so8945148wme.0 for ; Fri, 28 Jun 2019 02:36:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=N2BlLzFuQehpzWwGcUqNkw7wuytXhknZeoPcZaYTlhI=; b=sy6VsdaNSyMFgmFJUCxIU6iej6cfiNWWZU0aRTHLOW19zyFo/3tshhtcWuIrSmQcrX H0yK3ltIQywTitvKecqzyA1GIWHZpn2THfGf7PVU477vbyAGBT+GhZb7RGm7opP1rjjY fVzoqtiWw8x25KGC4s5rSwbBc/6R9dIDZpVLMaiL19JmUNj+SV24GwuSFdgFnrIKbyDw X1cUPb5eElJhZMHGE9MH8WWIThCMPIsnX8a0VQHF4uBWWfj0rj2Ia8D8XfDBdqnaFqJY oGWT67cqpFF8QjnXfFaT+Jk5YlV9NqgnDADHyqHLj5SHLL1j0IWdI3WVP3+PapHg3t9B WVaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=N2BlLzFuQehpzWwGcUqNkw7wuytXhknZeoPcZaYTlhI=; b=eeOzZpR5IgDXdpmcDbthRej69mI1gHK5vn51f/fDNlUGjTY9gZlHiJ0OE0MjtmcxVX oNDLkFbePVWKS5rE1MxKmRABSfebuJOZYCV3KENiV3QMUi7/g2NUWMNDQk995ZLycfMd BAmlzCjvFheY5bIG9g4QnV5pEWnfOlmPseXqF8Po9DXwqoqs4n6kqxvhsPlGFkl7LDqo /+lYFCDdOks3yUa18pNRTy7+LpmwhlqVd6ybQllGn1yYbhIPdh7nDqPHhE/2DUDPaHfW HLSSbvfid/2JJCtR23MQZjvqdYH67DiVM9wdPA416BjEmJYMP6gpnB/2hnrLTVHOopcq E1sQ== X-Gm-Message-State: APjAAAXa/KfdQDSRRcB3qnicS9KmABSzOaIX0S6qwEfOqD5mhWV34CEO Oo6wQ9ZOjlLzgearsXJApMTWTMAjtA8BSg== X-Received: by 2002:a1c:a654:: with SMTP id p81mr6211235wme.36.1561714568619; Fri, 28 Jun 2019 02:36:08 -0700 (PDT) Received: from localhost.localdomain (laubervilliers-657-1-83-120.w92-154.abo.wanadoo.fr. [92.154.90.120]) by smtp.gmail.com with ESMTPSA id m24sm1709910wmi.39.2019.06.28.02.36.07 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 28 Jun 2019 02:36:08 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: herbert@gondor.apana.org.au, ebiggers@kernel.org, horia.geanta@nxp.com, Ard Biesheuvel Subject: [PATCH v3 28/30] crypto: x86/des - switch to library interface Date: Fri, 28 Jun 2019 11:35:27 +0200 Message-Id: <20190628093529.12281-29-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190628093529.12281-1-ard.biesheuvel@linaro.org> References: <20190628093529.12281-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Signed-off-by: Ard Biesheuvel --- arch/x86/crypto/des3_ede_glue.c | 42 +++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) -- 2.20.1 diff --git a/arch/x86/crypto/des3_ede_glue.c b/arch/x86/crypto/des3_ede_glue.c index dfee4cec9452..cce329b188d1 100644 --- a/arch/x86/crypto/des3_ede_glue.c +++ b/arch/x86/crypto/des3_ede_glue.c @@ -21,7 +21,7 @@ */ #include -#include +#include #include #include #include @@ -29,8 +29,8 @@ #include struct des3_ede_x86_ctx { - u32 enc_expkey[DES3_EDE_EXPKEY_WORDS]; - u32 dec_expkey[DES3_EDE_EXPKEY_WORDS]; + struct des3_ede_ctx enc; + struct des3_ede_ctx dec; }; /* regular block cipher functions */ @@ -44,7 +44,7 @@ asmlinkage void des3_ede_x86_64_crypt_blk_3way(const u32 *expkey, u8 *dst, static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst, const u8 *src) { - u32 *enc_ctx = ctx->enc_expkey; + u32 *enc_ctx = ctx->enc.expkey; des3_ede_x86_64_crypt_blk(enc_ctx, dst, src); } @@ -52,7 +52,7 @@ static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst, static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx *ctx, u8 *dst, const u8 *src) { - u32 *dec_ctx = ctx->dec_expkey; + u32 *dec_ctx = ctx->dec.expkey; des3_ede_x86_64_crypt_blk(dec_ctx, dst, src); } @@ -60,7 +60,7 @@ static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx *ctx, u8 *dst, static inline void des3_ede_enc_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst, const u8 *src) { - u32 *enc_ctx = ctx->enc_expkey; + u32 *enc_ctx = ctx->enc.expkey; des3_ede_x86_64_crypt_blk_3way(enc_ctx, dst, src); } @@ -68,7 +68,7 @@ static inline void des3_ede_enc_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst, static inline void des3_ede_dec_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst, const u8 *src) { - u32 *dec_ctx = ctx->dec_expkey; + u32 *dec_ctx = ctx->dec.expkey; des3_ede_x86_64_crypt_blk_3way(dec_ctx, dst, src); } @@ -132,7 +132,7 @@ static int ecb_encrypt(struct skcipher_request *req) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm); - return ecb_crypt(req, ctx->enc_expkey); + return ecb_crypt(req, ctx->enc.expkey); } static int ecb_decrypt(struct skcipher_request *req) @@ -140,7 +140,7 @@ static int ecb_decrypt(struct skcipher_request *req) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm); - return ecb_crypt(req, ctx->dec_expkey); + return ecb_crypt(req, ctx->dec.expkey); } static unsigned int __cbc_encrypt(struct des3_ede_x86_ctx *ctx, @@ -358,24 +358,28 @@ static int des3_ede_x86_setkey(struct crypto_tfm *tfm, const u8 *key, u32 i, j, tmp; int err; - err = crypto_des3_ede_verify_key(tfm, key); - if (err) - return err; + err = des3_ede_expand_key(&ctx->enc, key, keylen); + if (err == -ENOKEY) { + if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS) + err = -EINVAL; + else + err = 0; + } - /* Generate encryption context using generic implementation. */ - err = __des3_ede_setkey(ctx->enc_expkey, &tfm->crt_flags, key, keylen); - if (err < 0) + if (err) { + memzero_explicit(ctx, sizeof(*ctx)); return err; + } /* Fix encryption context for this implementation and form decryption * context. */ j = DES3_EDE_EXPKEY_WORDS - 2; for (i = 0; i < DES3_EDE_EXPKEY_WORDS; i += 2, j -= 2) { - tmp = ror32(ctx->enc_expkey[i + 1], 4); - ctx->enc_expkey[i + 1] = tmp; + tmp = ror32(ctx->enc.expkey[i + 1], 4); + ctx->enc.expkey[i + 1] = tmp; - ctx->dec_expkey[j + 0] = ctx->enc_expkey[i + 0]; - ctx->dec_expkey[j + 1] = tmp; + ctx->dec.expkey[j + 0] = ctx->enc.expkey[i + 0]; + ctx->dec.expkey[j + 1] = tmp; } return 0;