Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3745509ybi; Tue, 2 Jul 2019 12:42:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqwv31e9cV3E/bs+EyCIGZnSERSuTZzDyW+U55r7bMHqeA3occSGTTc/0VZs47IOHz5PFGg1 X-Received: by 2002:a65:47c1:: with SMTP id f1mr8164981pgs.169.1562096574541; Tue, 02 Jul 2019 12:42:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562096574; cv=none; d=google.com; s=arc-20160816; b=B0WBHFcRJa4ffMJPCVhg31zR0WCJSfyj7PtBoxHEElI8LEeEqKgcsWSD3pWEML9eh5 TlZwV0XOIY8rPOnnstPL/0uNY8Xd6Go5f9BiJwcBDQVweiKs+DlCCQ0XV6ZINZbhM/w6 MmNtbvUywZ6IIFs5/0L7c3W4Jz1zQBjSjZZ4/Qzu0xlZOSG/cgFRc6bhPlRv58hgFuDu GP5srhL2UL/B7TigvtNaG8FQqi8R0nNTdijlLGdrgaW4othQfAW6Cj8r8cXbjHoD8G8/ ym8Spml0JyePMN/PPUumlD643m2ekr66LBSUupqNMmmZu2vf4/LRGp7wwcxAYcP8jwxU +iXw== 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=0MCWUw3vDUq5xLL2N3UUfYVxMc1CoRLbEbf+zv4bCoE=; b=ETo1JQh4KO7ppMMn2zusneE15/DDw7GULEZYoAveKD5ZSuAFgOjWoqjJFxg8rkORER PmhqqcgjiQrInj7hr8FM83kFr/qVImxnybs04ZBUssI5tuyXuxsKKVeY3yCO6uVrv/3i OEt/lN1YY3YPYN3MHgTad0n4+W8I2icMsF8n+Sze/S+6wKZxrz6uaeHF3R4ABO+QnDm7 U0nO5Laz2Kg+c91YVrdspK8urB6/ssQ5avPOLjbAmlRW3kYU0hczF/5yU33/Vpv7xxH8 dyTgf1ak0JPyfoHKJeTOTnFzNTd7Yw88mRJj68UGcA19SGVJWiEp6M5HF3HpBBUWBcOJ m+Gw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=i7+sZ9Ig; 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 u27si5014213pgn.231.2019.07.02.12.42.40; Tue, 02 Jul 2019 12:42:54 -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=i7+sZ9Ig; 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 S1727082AbfGBTmh (ORCPT + 99 others); Tue, 2 Jul 2019 15:42:37 -0400 Received: from mail-lj1-f193.google.com ([209.85.208.193]:46350 "EHLO mail-lj1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727069AbfGBTmh (ORCPT ); Tue, 2 Jul 2019 15:42:37 -0400 Received: by mail-lj1-f193.google.com with SMTP id v24so18173079ljg.13 for ; Tue, 02 Jul 2019 12:42:34 -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=0MCWUw3vDUq5xLL2N3UUfYVxMc1CoRLbEbf+zv4bCoE=; b=i7+sZ9Igg4lFlDQipgk5rFK5Kut6D04gI5g4VSt8nyeAs9CnyeExlo369v4slcQel0 3msnORzG9uRH+7JRWmobhA1wREy8DMgFzO80oCFY/7WezPLU7CXPUyquTmEbFrKsLj/d hB9uQXvY6qeoWa+RV0K3L+Sx/KHGeXuNqMdwQE2iGagm3C9fNtzIc6ujO6wOzGmj2VG9 E0Lry3+EwvQE63pH1EcA4ExhBqGL7FZkCczZKpxBTYX0PManuADqNOJpk/xoI2xr+0o5 z3TcEsUIUb2ZA+hvckFl1vZ6p5B56tX4Gu1FvenrIAUxag9tRRAntC5HsKjU5DtL+Mba VmAA== 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=0MCWUw3vDUq5xLL2N3UUfYVxMc1CoRLbEbf+zv4bCoE=; b=gMYJioALH8CtQlRUMqeWJkKuBYO4Y4iO0hodSsvwrEUqcfFbvi6m1iVaiFJGA4S5+l KrFnjLvSrZajj3oNfc184HLU5SIwMbTDIEApmYzdLSX+6Mpk0E1VAxcAOapy3sdHkV+G QLdgNmBTqxM0k0UnEmDC9z+7c3/iiwcznF64wuZx42X7vyDWI7ntdF3QAo4bHjj4wyde 7KGsbjlXOn8WSZNmw+YdAs2eA59DQKI4/zBYGbBiS1Agpz/R9C3Lqotpiyiwm1cxAKx2 lMzp4E3ufS6wg809NVtcjhulOnlN3o0gyM4st6tdevvWTyoHzfAkWjOSS1uYh/iLdyv0 Zr7Q== X-Gm-Message-State: APjAAAUPggCkVv4l+WJVmFclV52sbdzXBXC0CJuRmqvBAC3UpT21Jghj xL0yFN//YoPRZo7NBfAgWxNqPLNxJAv4YCXC X-Received: by 2002:a2e:3602:: with SMTP id d2mr18733543lja.112.1562096553145; Tue, 02 Jul 2019 12:42:33 -0700 (PDT) Received: from e111045-lin.arm.com (89-212-78-239.static.t-2.net. [89.212.78.239]) by smtp.gmail.com with ESMTPSA id 24sm4475163ljs.63.2019.07.02.12.42.32 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 02 Jul 2019 12:42:32 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: herbert@gondor.apana.org.au, ebiggers@google.com, Ard Biesheuvel Subject: [PATCH v4 17/32] crypto: aes - move sync ctr(aes) to AES library and generic helper Date: Tue, 2 Jul 2019 21:41:35 +0200 Message-Id: <20190702194150.10405-18-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190702194150.10405-1-ard.biesheuvel@linaro.org> References: <20190702194150.10405-1-ard.biesheuvel@linaro.org> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org In preparation of duplicating the sync ctr(aes) functionality to modules under arch/arm, move the helper function from a inline .h file to the AES library, which is already depended upon by the drivers that use this fallback. Signed-off-by: Ard Biesheuvel --- arch/arm64/crypto/aes-ctr-fallback.h | 53 -------------------- arch/arm64/crypto/aes-glue.c | 22 ++++++-- arch/arm64/crypto/aes-neonbs-glue.c | 21 ++++++-- 3 files changed, 33 insertions(+), 63 deletions(-) diff --git a/arch/arm64/crypto/aes-ctr-fallback.h b/arch/arm64/crypto/aes-ctr-fallback.h deleted file mode 100644 index c9285717b6b5..000000000000 --- a/arch/arm64/crypto/aes-ctr-fallback.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Fallback for sync aes(ctr) in contexts where kernel mode NEON - * is not allowed - * - * Copyright (C) 2017 Linaro Ltd - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - */ - -#include -#include - -asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds); - -static inline int aes_ctr_encrypt_fallback(struct crypto_aes_ctx *ctx, - struct skcipher_request *req) -{ - struct skcipher_walk walk; - u8 buf[AES_BLOCK_SIZE]; - int err; - - err = skcipher_walk_virt(&walk, req, true); - - while (walk.nbytes > 0) { - u8 *dst = walk.dst.virt.addr; - u8 *src = walk.src.virt.addr; - int nbytes = walk.nbytes; - int tail = 0; - - if (nbytes < walk.total) { - nbytes = round_down(nbytes, AES_BLOCK_SIZE); - tail = walk.nbytes % AES_BLOCK_SIZE; - } - - do { - int bsize = min(nbytes, AES_BLOCK_SIZE); - - __aes_arm64_encrypt(ctx->key_enc, buf, walk.iv, - 6 + ctx->key_length / 4); - crypto_xor_cpy(dst, src, buf, bsize); - crypto_inc(walk.iv, AES_BLOCK_SIZE); - - dst += AES_BLOCK_SIZE; - src += AES_BLOCK_SIZE; - nbytes -= AES_BLOCK_SIZE; - } while (nbytes > 0); - - err = skcipher_walk_done(&walk, tail); - } - return err; -} diff --git a/arch/arm64/crypto/aes-glue.c b/arch/arm64/crypto/aes-glue.c index 3c80345d914f..60303ea625e6 100644 --- a/arch/arm64/crypto/aes-glue.c +++ b/arch/arm64/crypto/aes-glue.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -21,7 +22,6 @@ #include #include "aes-ce-setkey.h" -#include "aes-ctr-fallback.h" #ifdef USE_V8_CRYPTO_EXTENSIONS #define MODE "ce" @@ -404,13 +404,25 @@ static int ctr_encrypt(struct skcipher_request *req) return err; } -static int ctr_encrypt_sync(struct skcipher_request *req) +static void ctr_encrypt_one(struct crypto_skcipher *tfm, const u8 *src, u8 *dst) { - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); - struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm); + const struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm); + unsigned long flags; + + /* + * Temporarily disable interrupts to avoid races where + * cachelines are evicted when the CPU is interrupted + * to do something else. + */ + local_irq_save(flags); + aes_encrypt(ctx, dst, src); + local_irq_restore(flags); +} +static int ctr_encrypt_sync(struct skcipher_request *req) +{ if (!crypto_simd_usable()) - return aes_ctr_encrypt_fallback(ctx, req); + return crypto_ctr_encrypt_walk(req, ctr_encrypt_one); return ctr_encrypt(req); } diff --git a/arch/arm64/crypto/aes-neonbs-glue.c b/arch/arm64/crypto/aes-neonbs-glue.c index cb8d90f795a0..73c17ccb347d 100644 --- a/arch/arm64/crypto/aes-neonbs-glue.c +++ b/arch/arm64/crypto/aes-neonbs-glue.c @@ -11,13 +11,12 @@ #include #include #include +#include #include #include #include #include -#include "aes-ctr-fallback.h" - MODULE_AUTHOR("Ard Biesheuvel "); MODULE_LICENSE("GPL v2"); @@ -283,13 +282,25 @@ static int aesbs_xts_setkey(struct crypto_skcipher *tfm, const u8 *in_key, return aesbs_setkey(tfm, in_key, key_len); } -static int ctr_encrypt_sync(struct skcipher_request *req) +static void ctr_encrypt_one(struct crypto_skcipher *tfm, const u8 *src, u8 *dst) { - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); struct aesbs_ctr_ctx *ctx = crypto_skcipher_ctx(tfm); + unsigned long flags; + + /* + * Temporarily disable interrupts to avoid races where + * cachelines are evicted when the CPU is interrupted + * to do something else. + */ + local_irq_save(flags); + aes_encrypt(&ctx->fallback, dst, src); + local_irq_restore(flags); +} +static int ctr_encrypt_sync(struct skcipher_request *req) +{ if (!crypto_simd_usable()) - return aes_ctr_encrypt_fallback(&ctx->fallback, req); + return crypto_ctr_encrypt_walk(req, ctr_encrypt_one); return ctr_encrypt(req); } -- 2.17.1