From patchwork Thu Jul 4 18:30:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 168493 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp2582103ilk; Thu, 4 Jul 2019 11:30:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqyKBe2D0hq1R69EmzSdgkJ+Bbnwzh3Camo1MKKDosZFsclM2Bza3EDt29962wdrPv+N1QO3 X-Received: by 2002:a63:1226:: with SMTP id h38mr44147255pgl.196.1562265053484; Thu, 04 Jul 2019 11:30:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562265053; cv=none; d=google.com; s=arc-20160816; b=nwy+WvK1DpFxcIZ7ZV433bZ/JjNk5iF0v+KeQ2weffJ+KJoJfw0LongOBSbEZ+jCP4 k72yjDECEpEeoy7ZhKgay28YMzlKONwO3n2CLx4iZn7frmsYq2eo0YKD1mrjP0GIwEf1 F7PL5j3qhqQviKKbkkewukbEZR2E6StBlUxGZNxwodKO3ht51ws1YxQRLILhZz+j2VxG 7j3swBmksRZdcPbJ53DSBCJOZvJIzEIBR8C7mlYlkn1rXDsQOnbjwy0iNnu8HxQqzPoB kv2JL1AO4bNWciPG9KBNevocXBEdbzJCu9m5rMe5RMV6jEpOG8qp2hz84HfRVT6YB9SI RklQ== 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=q2LNtGCmE5IlgP+bQlpDKN5FkREmAvC3oHX/3jKNrRY=; b=VqmwOJivloalwX86Q0FsWSECXF0Vu2KM8y80n1+LU1g4e4slxXi9CZek4Ubgy7HpyL Q4F8l/3zemMOvphdlUMToirXHotRp7CuSV6Njzgb4SkjoRNhfj2KOMZquiVxDCrCs5mZ +III5UcETHQcEUF/uhIRGWS9gkZhIo63b5WKGN8dHdxcYv3iIbtsFmWXrsKTFla8tg+L yB//4+hCU5HJQFrbmT9rq3kR0FCS+rnFjdiENJAAqdaQVnHZ0NLr33QLd3Db+vz0Iz3r T4hHmhu+tnqiTR1/HXHyGzq/i8wI4v3lW/F0HU7E+GLg2Fpqn2NF3giMlzi1BYNnF7Xd v8cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AWw0X0ld; 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 i69si6197650pge.366.2019.07.04.11.30.53; Thu, 04 Jul 2019 11:30: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=AWw0X0ld; 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 S1726817AbfGDSaw (ORCPT + 3 others); Thu, 4 Jul 2019 14:30:52 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:46047 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727046AbfGDSav (ORCPT ); Thu, 4 Jul 2019 14:30:51 -0400 Received: by mail-wr1-f66.google.com with SMTP id f9so7491033wre.12 for ; Thu, 04 Jul 2019 11:30:51 -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=q2LNtGCmE5IlgP+bQlpDKN5FkREmAvC3oHX/3jKNrRY=; b=AWw0X0ldg6U0xP9z9EIXN7pc0fp+5EvjK30XzbXoxs0EeTO0y5XABAeisWIdXREF2U s/Ja7Bt67iGxB1hb3QkS5sDE8FdLB83mKoVxX3839omjjETHgxjlgqQHezmEFbSCbDin vBzof1dEhE0DdRdjf4IkgPswj6SG4Z+pFPMa/RnDjx7JwXuuDnvuduqZ+gCgws210EGx ASjM+J6SRXvlmaxVMn/DHjB++J6cU1VSvgbwUazKIWlPaGFpjPkWOawsr8XAATsxSSpy SsEwXGg1jTlmCv3YHoPXXkKHlzMw3yM2gP2BSf3kY73pijSgQDyCIDVrwOBV4+BAiCPj GQlQ== 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=q2LNtGCmE5IlgP+bQlpDKN5FkREmAvC3oHX/3jKNrRY=; b=lGWvLrbC2pa9/yT3oOqCIzQs1U5byhrrnkefqn0L1AVPJ6rQ75J4Dt4wJo5NREbG3F 5pXJFArOITqsJ71aDXQFPUYGIRDNviRavvzTqWXEN7clvpbtZO6VMHY1tbs1egudNIp8 h27CjILuXN10N97PvrSbDT0ATDMvlfrMcAwHZeyg/JkmTWofAOW5lvyYV/H3Qd07rzo+ 1qzKL3b7R2b0aikZv9zXtv7Xv0XharBoFJ5PnvOz2QcwVZdJFdBeYzKjt+FKUDBqZlXI oXiolYCD4rbgZ93QIGCmewo9M1xdb+qaRwCIQacc7YkePkjRJlR1Yb1DVtJ/g3yYQE7R 214Q== X-Gm-Message-State: APjAAAUmUzs/WafIv33jNquOEMzYdI09rEHl/+RAIZe8tAWuvWDnk9KD nNb7t9K+26zsspnFm2szCCVlbqIUp2+f8g== X-Received: by 2002:a5d:5752:: with SMTP id q18mr29699813wrw.337.1562265050057; Thu, 04 Jul 2019 11:30:50 -0700 (PDT) Received: from e111045-lin.arm.com (93-143-123-179.adsl.net.t-com.hr. [93.143.123.179]) by smtp.gmail.com with ESMTPSA id o6sm11114695wra.27.2019.07.04.11.30.48 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 04 Jul 2019 11:30: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 v8 6/7] crypto: arm64/aes-cts-cbc - factor out CBC en/decryption of a walk Date: Thu, 4 Jul 2019 20:30:16 +0200 Message-Id: <20190704183017.31570-7-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190704183017.31570-1-ard.biesheuvel@linaro.org> References: <20190704183017.31570-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 f0ceb545bd1e..11b85ce02d7a 100644 --- a/arch/arm64/crypto/aes-glue.c +++ b/arch/arm64/crypto/aes-glue.c @@ -185,46 +185,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)); @@ -250,22 +268,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; @@ -315,22 +323,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;