From patchwork Mon Aug 12 14:53:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 171082 Delivered-To: patch@linaro.org Received: by 2002:a92:d204:0:0:0:0:0 with SMTP id y4csp2938661ily; Mon, 12 Aug 2019 07:53:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqzPuQ0NO43Wc6MytSkc9Z+yk2cooJE4iEbTPINtQeBqngsvoZXzNo5g7Jg4UyWcGLumGqjQ X-Received: by 2002:a62:3543:: with SMTP id c64mr35260693pfa.242.1565621633962; Mon, 12 Aug 2019 07:53:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565621633; cv=none; d=google.com; s=arc-20160816; b=GadnkSWfrt4049f84woI90LbPAmDLBjhXnZf05ClfG5d6L+6KJLujG/TeCzYmY1Agc vZM/pLa1sTkDw5b79V6h87zLDH6XS/xx/XEZASCMEQCzYXtx5wYtoX+tqbLuqh9ggrzV MShbUnagLLVefguLlt/5E7vqosdYLQmh4uIECWgsY/p05fXQI6ozUdxwgNjwNKo5Vxbg QftH/lPqPBUosM9IOT/hvSE4En2A8NFqk9vF6ftglIyLdGRnDpks90j4wusqB5XVr/Z5 bC1ThxYgRxDz0p+DUcPj3wx3YiVujKSbTsjM1ip5us/fJTnp4PvfSU1q0W5c97PXkH+2 DG5w== 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=huVoFUpMc5dzWwrmJm9Qwvn6MCUViGlDp2QJ3557fZk=; b=ZLQQPJ/WaMzQm+NfJWDbywUSg0n5AYb1E6YiqBDyKNL+J6PnKZ1PpysIunTOKwyDkh XQ91k4ZOtBNRug6C06w8Z0pL1JT1MgnixyxefvJ67YheZ7xO2pPG831hj4REUUEH+Uon yK3nl9F8jDbUM/PQsTMX/ISGokKeNjiakOkizeH9t+9e0q9rp46Qj6SUgw0LzZtW41VA uDRX8a97W5MNnYXr7qtRih/4ARgIsTHjN3R9X5abRAYz8TeJH34KvokQpTED2HUGV79C fOQCzWMksRmfIRnRlK5NNJPj72eGruiW5cWKmoNcGmzmZmXX3Uj8uOh3rhk1+9tFM4ga VaFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="Z5p/C/yw"; 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 z8si25456965pgr.359.2019.08.12.07.53.53; Mon, 12 Aug 2019 07:53:53 -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="Z5p/C/yw"; 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 S1727020AbfHLOxx (ORCPT + 3 others); Mon, 12 Aug 2019 10:53:53 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:51504 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727040AbfHLOxx (ORCPT ); Mon, 12 Aug 2019 10:53:53 -0400 Received: by mail-wm1-f65.google.com with SMTP id 207so12465743wma.1 for ; Mon, 12 Aug 2019 07:53:50 -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=huVoFUpMc5dzWwrmJm9Qwvn6MCUViGlDp2QJ3557fZk=; b=Z5p/C/ywuAJFWai3lSZSzAOfx3tv8Y8Zg/uxQ92OBb4U8PtZVeOVEvUnrtIrWDNp/a iYTosSznMm3S6wsRzr+n5udua7AK1oHk4fjFjA0RF9YS6cgy7Yry8Hk/gS9QNHOvyST+ a0k6+5f2PY0OKg3lmeI/aKWb/qmQTo+xUoKVi7v7PpTC8TbI6Lj5uxULCLW8Xl4yJGHh qgTVSnz3gdAh7UV5HoUgSZnl84E97a4PxZrzN0xxVdJ5R+0JBeMiMoLbG/MF77VuNjCp c9dGZEMNkSd+5SLH+C8D5KvG8eGv7vgY2mjwZB2NpnoscI/0hnmBnkHTsLCOV/NR+L9T vimA== 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=huVoFUpMc5dzWwrmJm9Qwvn6MCUViGlDp2QJ3557fZk=; b=IJCuT/PLd5gkR+ZJ9ZipcXkt+X4cCh8tDJ+GQhW0JzrKPcmHoxMuUh7b6q5rYJW95n +51Ka6dnOQEk/U0mSc4ip+lHEpEDgE2+Sxm359Y+9nnLxyTMEEFrglcjSsQung5qtmpD fMXpILhnzReXoOPIYa89wc1veJQ4On9cCIl/F1W+oEJIl/vB7qYL7mHBarxq6LU3kshU uEBgR+Qo81SSUnKT8OjLEsGJ16qCETFn4TAe7eRCMANeT08HaF9L8XXzufhFnmjE10n7 WtL1PmkLsMB1cBl1xLtx6Soo0mxJm3j9o9MDNZsJKWerJn96+GIlwQ8G6Ga2slKqW1av tEHg== X-Gm-Message-State: APjAAAXPG2JFAIhc3ZWsOo03xjOk0LIGod7r+ZabGCeiU/BxgTbBp20c UINahM1y0bqEuyUEXHoXe3awJKf/YwzWDw== X-Received: by 2002:a1c:f511:: with SMTP id t17mr28227892wmh.53.1565621630120; Mon, 12 Aug 2019 07:53:50 -0700 (PDT) Received: from localhost.localdomain ([2a02:587:a407:da00:1c0e:f938:89a1:8e17]) by smtp.gmail.com with ESMTPSA id k13sm23369190wro.97.2019.08.12.07.53.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Aug 2019 07:53:49 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: Ard Biesheuvel , Herbert Xu , Eric Biggers , dm-devel@redhat.com, linux-fscrypt@vger.kernel.org, Gilad Ben-Yossef , Milan Broz Subject: [PATCH v10 5/7] crypto: arm64/aes-cts-cbc - factor out CBC en/decryption of a walk Date: Mon, 12 Aug 2019 17:53:22 +0300 Message-Id: <20190812145324.27090-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190812145324.27090-1-ard.biesheuvel@linaro.org> References: <20190812145324.27090-1-ard.biesheuvel@linaro.org> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The plain CBC driver and the CTS one share some code that iterates over a scatterwalk and invokes the CBC asm code to do the processing. The upcoming ESSIV/CBC mode will clone that pattern for the third time, so let's factor it out first. Signed-off-by: Ard Biesheuvel --- arch/arm64/crypto/aes-glue.c | 82 ++++++++++---------- 1 file changed, 40 insertions(+), 42 deletions(-) -- 2.17.1 diff --git a/arch/arm64/crypto/aes-glue.c b/arch/arm64/crypto/aes-glue.c index 55d6d4838708..23abf335f1ee 100644 --- a/arch/arm64/crypto/aes-glue.c +++ b/arch/arm64/crypto/aes-glue.c @@ -186,46 +186,64 @@ static int ecb_decrypt(struct skcipher_request *req) return err; } -static int cbc_encrypt(struct skcipher_request *req) +static int cbc_encrypt_walk(struct skcipher_request *req, + struct skcipher_walk *walk) { struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm); - int err, rounds = 6 + ctx->key_length / 4; - struct skcipher_walk walk; + int err = 0, rounds = 6 + ctx->key_length / 4; unsigned int blocks; - err = skcipher_walk_virt(&walk, req, false); - - while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) { + while ((blocks = (walk->nbytes / AES_BLOCK_SIZE))) { kernel_neon_begin(); - aes_cbc_encrypt(walk.dst.virt.addr, walk.src.virt.addr, - ctx->key_enc, rounds, blocks, walk.iv); + aes_cbc_encrypt(walk->dst.virt.addr, walk->src.virt.addr, + ctx->key_enc, rounds, blocks, walk->iv); kernel_neon_end(); - err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE); + err = skcipher_walk_done(walk, walk->nbytes % AES_BLOCK_SIZE); } return err; } -static int cbc_decrypt(struct skcipher_request *req) +static int cbc_encrypt(struct skcipher_request *req) { - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); - struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm); - int err, rounds = 6 + ctx->key_length / 4; struct skcipher_walk walk; - unsigned int blocks; + int err; err = skcipher_walk_virt(&walk, req, false); + if (err) + return err; + return cbc_encrypt_walk(req, &walk); +} - while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) { +static int cbc_decrypt_walk(struct skcipher_request *req, + struct skcipher_walk *walk) +{ + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm); + int err = 0, rounds = 6 + ctx->key_length / 4; + unsigned int blocks; + + while ((blocks = (walk->nbytes / AES_BLOCK_SIZE))) { kernel_neon_begin(); - aes_cbc_decrypt(walk.dst.virt.addr, walk.src.virt.addr, - ctx->key_dec, rounds, blocks, walk.iv); + aes_cbc_decrypt(walk->dst.virt.addr, walk->src.virt.addr, + ctx->key_dec, rounds, blocks, walk->iv); kernel_neon_end(); - err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE); + err = skcipher_walk_done(walk, walk->nbytes % AES_BLOCK_SIZE); } return err; } +static int cbc_decrypt(struct skcipher_request *req) +{ + struct skcipher_walk walk; + int err; + + err = skcipher_walk_virt(&walk, req, false); + if (err) + return err; + return cbc_decrypt_walk(req, &walk); +} + static int cts_cbc_init_tfm(struct crypto_skcipher *tfm) { crypto_skcipher_set_reqsize(tfm, sizeof(struct cts_cbc_req_ctx)); @@ -251,22 +269,12 @@ static int cts_cbc_encrypt(struct skcipher_request *req) } if (cbc_blocks > 0) { - unsigned int blocks; - skcipher_request_set_crypt(&rctx->subreq, req->src, req->dst, cbc_blocks * AES_BLOCK_SIZE, req->iv); - err = skcipher_walk_virt(&walk, &rctx->subreq, false); - - while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) { - kernel_neon_begin(); - aes_cbc_encrypt(walk.dst.virt.addr, walk.src.virt.addr, - ctx->key_enc, rounds, blocks, walk.iv); - kernel_neon_end(); - err = skcipher_walk_done(&walk, - walk.nbytes % AES_BLOCK_SIZE); - } + err = skcipher_walk_virt(&walk, &rctx->subreq, false) ?: + cbc_encrypt_walk(&rctx->subreq, &walk); if (err) return err; @@ -316,22 +324,12 @@ static int cts_cbc_decrypt(struct skcipher_request *req) } if (cbc_blocks > 0) { - unsigned int blocks; - skcipher_request_set_crypt(&rctx->subreq, req->src, req->dst, cbc_blocks * AES_BLOCK_SIZE, req->iv); - err = skcipher_walk_virt(&walk, &rctx->subreq, false); - - while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) { - kernel_neon_begin(); - aes_cbc_decrypt(walk.dst.virt.addr, walk.src.virt.addr, - ctx->key_dec, rounds, blocks, walk.iv); - kernel_neon_end(); - err = skcipher_walk_done(&walk, - walk.nbytes % AES_BLOCK_SIZE); - } + err = skcipher_walk_virt(&walk, &rctx->subreq, false) ?: + cbc_decrypt_walk(&rctx->subreq, &walk); if (err) return err;