From patchwork Mon Aug 5 17:00:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 170604 Delivered-To: patch@linaro.org Received: by 2002:a92:512:0:0:0:0:0 with SMTP id q18csp4538822ile; Mon, 5 Aug 2019 10:02:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqzJYm8b7TPpST3d3Lfm7vCksRdYgUYd92uABTfsIFJ81Sjldvaqk861mLM6csxro9aIY70e X-Received: by 2002:a17:902:b698:: with SMTP id c24mr13999930pls.28.1565024555578; Mon, 05 Aug 2019 10:02:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565024555; cv=none; d=google.com; s=arc-20160816; b=uxk7zUB2aKgMnKqpaOzV4cHDm9u94q0n8IYlcN1/o4hYk483ZWmH5mtN1rYDXUQVGB RwyH+Fu7eooFMDcmUkqFrvqJrDz9HxuvSSqCYtW26A5oQpIdhbq7D6TFiiWrmaQ7t+Db X6Lqr09DC8j6/PyupuBhFiGM4Dz/jHsqoHH2TdpnUCtaBdFHlHB23qcF0YLjlWK0HHby ch6TGJBDo7tCjvjhK0Yr+yvzyvL4tbUL3D4tedjIsUR+2A2yBgyLThblIaaWPbC0kFcU Bjoo/eG6WwIR12ynrwYNu+F8ZvJbBmkbhnausa0xG4GISVuJJmHOJHySYV6ula86zfKm fqng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=uMoPWG7ZtNqmAOxM6E+qCA9KIAAzgWl56JeFS/8CuPA=; b=CIcxBWUMwVR/ehtQaXmBIcWKEq7GfenRcryjOxiGa25QeF9QSbccSqGawNE1lbY0ve pQ20/wAiuvzmNqX6jRRbIVuleoW8PIXkyEbLmOr06IVe/Gj1lAiDEVIACfVfA1/m/BSk nOHpSNYm4SIxXLWTpkzeUFmj17II1A44wkbIGeGB2Wp5NfyzvKzdnEwZa29RKI/K636q I7XQzXivFlk+fv54oWCi8bTlnY2ROdxLTZqSacwVgRJ6dyYG8YqYQt9fVDOeEJ7uDgVX an4P2403G92hcVEFiB9Ph7yLkuhnklG6lj/PbF5UTLJ435VPoCiiBMsxR+q3RE3QTBAs ZysQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ufjjEcrg; 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 l63si46181371pfl.41.2019.08.05.10.02.35; Mon, 05 Aug 2019 10:02:35 -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=ufjjEcrg; 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 S1729957AbfHERCd (ORCPT + 3 others); Mon, 5 Aug 2019 13:02:33 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:39561 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729919AbfHERCb (ORCPT ); Mon, 5 Aug 2019 13:02:31 -0400 Received: by mail-wr1-f68.google.com with SMTP id x4so31972029wrt.6 for ; Mon, 05 Aug 2019 10:02:30 -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; bh=uMoPWG7ZtNqmAOxM6E+qCA9KIAAzgWl56JeFS/8CuPA=; b=ufjjEcrg/2Jv/d51R7cDrKbAvrREURUhkX4Dfm8DPcIUtLmY4dZVWaMAoZVJw6ICvt qXvxQY/flcD10W88Et37euPeHlCIHbf+MMTotJLR7E6vulsQtPDM7atoBDXYwow/hOko 7a1R0jM6mtUahtGHTFtM8RPJo+ZxY24z1WLAv3CNyX8VQLrOKmIZNbdK3rHGM65pAqw4 RFjB4Yg1TbYEFjjgrcbHwaeQPjbzBL1FyEQ9dIkoHCvtPYCLe9SHdyMGFWjnAD1j96AB YnaXXgyZNrsqU5z/vbbFZ2/NY/Nv8SLUCt+ehWHdncO5OWu9T7vF+/Rsz12sIWD7DfZu umEQ== 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; bh=uMoPWG7ZtNqmAOxM6E+qCA9KIAAzgWl56JeFS/8CuPA=; b=cMl7l9+sha+FNdygJnD2Nodp/LHHZq9WA1suI8RTTKZwzQ+H+3cExRNGkDmstQeHYW Qe212pRDQs+sZ89FjkNx1WRUmfGtpZNjsVFeJ02UkbyA8zf+NCdJaOr7vuOojYtR5/oD jQsWe6L16byN8I0PzVe00J3ScZC854tvx4R+J4LLDPkJpv53lFx0yQjVC/LzogdPIOFI 1f3XgEEw+1IIKDKEUbsOW6lyEjP9yU6sYW/iNJTBwgtw1SoLRbreiJowXwBe+cqOxyS8 6glqzJtmHJMU12urh4BEK1hrX+C47P0n7a1PHgDFT6EwayJdH6wBzX/E1k31UQ9s0LzY hz/Q== X-Gm-Message-State: APjAAAWRGFNzMPc7e/w6RLThDFVe0R9jGD6tP5cKSNNC0OCzeCO0dNTp P8SiGKU/2JaXBX5jEKc6yLIAj7cbNwcD/w== X-Received: by 2002:adf:e343:: with SMTP id n3mr125259830wrj.103.1565024549785; Mon, 05 Aug 2019 10:02:29 -0700 (PDT) Received: from localhost.localdomain ([2a02:587:a407:da00:582f:8334:9cd9:7241]) by smtp.gmail.com with ESMTPSA id j9sm95669383wrn.81.2019.08.05.10.02.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Aug 2019 10:02:29 -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 v4 28/30] crypto: x86/des - switch to library interface Date: Mon, 5 Aug 2019 20:00:35 +0300 Message-Id: <20190805170037.31330-29-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190805170037.31330-1-ard.biesheuvel@linaro.org> References: <20190805170037.31330-1-ard.biesheuvel@linaro.org> 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.17.1 diff --git a/arch/x86/crypto/des3_ede_glue.c b/arch/x86/crypto/des3_ede_glue.c index f730a312ce35..89830e531350 100644 --- a/arch/x86/crypto/des3_ede_glue.c +++ b/arch/x86/crypto/des3_ede_glue.c @@ -11,7 +11,7 @@ */ #include -#include +#include #include #include #include @@ -19,8 +19,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 */ @@ -34,7 +34,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); } @@ -42,7 +42,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); } @@ -50,7 +50,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); } @@ -58,7 +58,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); } @@ -122,7 +122,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) @@ -130,7 +130,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, @@ -348,24 +348,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) { + memset(ctx, 0, 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;