From patchwork Sat Jun 22 00:31:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 167454 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp1376944ilk; Fri, 21 Jun 2019 17:32:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqxU6CNoAn/vYuvEn2S2XI/VcUkgYytGdKMX9tFEvrf5FqZACJSf3HF9XB3ZZ2ENSd8BZ+Wf X-Received: by 2002:a17:902:28e9:: with SMTP id f96mr85796888plb.114.1561163538147; Fri, 21 Jun 2019 17:32:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561163538; cv=none; d=google.com; s=arc-20160816; b=jMWUgjxkng21UkV9JqiJGRXv6VkR3kvLuYQ1RiZBcr5JOeSrK/ipovrUrHMdJWq6rO 9GwUt/vh0yT37SwbX1LIfebtnYnblDtzBZXs2u07njqpL0LZuEbU/k8sF9ILHpdreDZ2 KWxk4nhE7UALsMbDeSmCZg/Z12Uh+/KH+eARMDGLOkxPEyYr8BEG5XwkcjCCuJVtoJZH Ujp0cN1MR0sZpxJd5cxiqTwro4Ky/K0HsiE1fWJ4fzLYmMFb29PLJUx5VeLoA4hvOBro 6uQf1M/VSEAbWdwxsQSwlMNCeb1TmgtqtsXKnmsHasgg3sxMz4ypXgF0WD3iLpVlu1CF MQZQ== 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=MVcWeMwNTv13v+WqRc8HlJLKxMyNI5qCt4nrC5wTvjo=; b=WdgfRcK/mHY8oZHm20mvjWy7//33Tfu7aE8PDvKE+02xmdtEQhDnI80qlTZjfKJ2y1 DEnsvHYPyZfRxa+ZSSLqfKNYmEl4u8vvRpqC7LQFUyUsmzD5yYITvaXl3Xv0W0zET75o Y6ELiFnoQxCNaZMv/V375jWNIih2Lff2Bcz+P15sR2tL1kXTukSxtRg2+6NefBJoB+Ij 4vGZZigVDDiOZczh1MGPBN11GgzkDq5lo2/0HH/CyoWVEaygj28/Pm3IJ494g5ii15xj 76t9x5mVeSG6tLAUibSNq7+2vfApWa4/Usqi163k6mbJ9k49Ob3n9nXGoa3m4Iqc6LPG ujow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="SnxxU/+1"; 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 v10si4096271plg.320.2019.06.21.17.32.17; Fri, 21 Jun 2019 17:32:18 -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="SnxxU/+1"; 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 S1726262AbfFVAcR (ORCPT + 3 others); Fri, 21 Jun 2019 20:32:17 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:50959 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726237AbfFVAcR (ORCPT ); Fri, 21 Jun 2019 20:32:17 -0400 Received: by mail-wm1-f65.google.com with SMTP id c66so7736085wmf.0 for ; Fri, 21 Jun 2019 17:32:15 -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=MVcWeMwNTv13v+WqRc8HlJLKxMyNI5qCt4nrC5wTvjo=; b=SnxxU/+1YIkAksFjBl7jlWcjEgLlSQvEIKU5f3Y5jXNHiT1x2bO7m4NUwScl+HqnTd dqGBCq93hYTszUOjMlAXazCf6ZWWLqYxGWR7EuQpfAZguIwL4rqN251VeIYmAbbFM0SH b/CErMBQh532OAlfZMtZgx0LKohy7V168TIM40QLDmb8Js3DI9KL797mzOYi3dk5Mfb1 s2GtekeIRNMGoYo0JO2aR9VUvR9lBKSzvpGw8fh5ro7zIPoQ8FjF9mcJnqjYYWEEdO/u wYjumyIP+qBdDhwUQidqfM0Imm/dvXkkU+Qvu8wYzOMtgj57wEcSS8HY+QPpphVSv6Oe 7kuA== 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=MVcWeMwNTv13v+WqRc8HlJLKxMyNI5qCt4nrC5wTvjo=; b=MK8YmtkaELjM7gzvxlKxgDvNHXNr5FBxR1GAEoGwvZvit76DUz2wx2xjUJFNGPAptf gJa0XQnEqgeADlTnQEpdV1rYFjlcNkUe8AsNELWN28M2JZ3m4Rz33gS13hYL3G99uXcc kuAOse+Is5ZyhwZCIZyi/4Um+WrB5to1o514J/XW10SI5nyljKVCR/G7lYhFPFJyv3pK XjjGe7m/erUbAJpb8xpb48PDHBvvPtfivofA3AFIXVeGjINoETW9PpSeoYfkCXQ0dmVm BWXKdWYBEqF8DyeXbhJDN7ysrRYjUorH0kbTXaVEP9WaDb8+2p1XePrYeSIpPKW5wEUr Oq8g== X-Gm-Message-State: APjAAAUX/az1y9Zxe3o1tDrL8FsI3ay5QLmwD52nvsQ4i76pGw0r+8H2 8ynnfkc3CloNJ5XHx4hd6yczB62sX4PVQpqr X-Received: by 2002:a05:600c:2201:: with SMTP id z1mr5301146wml.59.1561163534021; Fri, 21 Jun 2019 17:32:14 -0700 (PDT) Received: from sudo.home ([2a01:cb1d:112:6f00:99d4:1ff0:ed6:dfbb]) by smtp.gmail.com with ESMTPSA id v18sm4792019wrd.51.2019.06.21.17.32.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Jun 2019 17:32:13 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: herbert@gondor.apana.org.au, ebiggers@google.com, Ard Biesheuvel Subject: [RFC PATCH 28/30] crypto: x86/des - switch to library interface Date: Sat, 22 Jun 2019 02:31:10 +0200 Message-Id: <20190622003112.31033-29-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190622003112.31033-1-ard.biesheuvel@linaro.org> References: <20190622003112.31033-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 | 39 ++++++++++---------- 1 file changed, 20 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 571966e5c542..9c743246f5ad 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,25 @@ static int des3_ede_x86_setkey(struct crypto_tfm *tfm, const u8 *key, u32 i, j, tmp; int err; - err = des3_ede_verify_key(tfm, key, keylen); - if (unlikely(err)) - return err; + err = des3_ede_expand_key(&ctx->enc, key, keylen); + if (err == -ENOKEY && + !(crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) + 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;