From patchwork Sat Jun 22 19:34: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: 167505 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp2256078ilk; Sat, 22 Jun 2019 12:35:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqyEk9zFN8meqiRq0cP7C85hK1iijMjgH13z+EnEp2vvRWzyGqQAKcrAAraSh51rr39iQ5Ez X-Received: by 2002:a63:db05:: with SMTP id e5mr24724965pgg.121.1561232105958; Sat, 22 Jun 2019 12:35:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561232105; cv=none; d=google.com; s=arc-20160816; b=cX1jZQo07LVDG7SnnT3KvuLBMwd8PZ+8PMCFW253zb+QZpODVgNo3pCeNsFja30LhD 6oj+HwiLBgfZ797S7OCQd7P4ZSGvUWhKcHzKo3MtnmKmAydWVRv7fZ46vDdrudpc+/Z7 aQbkfpOrhNk/nbaXQUJkrS8oqz1qmDIgq+7bXHN7PnplokeACs1tCW17rik91zBKCASL DUsDoOcd8nDyzqVuk9JZH5rrFtdbhTuscWg7my3TK1CHLdER7MdAviXb9knZQ+lZaz/6 H18lVDvqzLvQiTcei9CsvLUlg+HhwSLPKSsAmJDha8dDYwgmyyYWgm/9OLALogGHRYKx WgPA== 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=vL6L5hDUn9mhzlJsgchovHgAG3ROBqwVQK1Apo8D4Bw=; b=fWxFkzWEI07n3elpjSD/AI4Dw/UVnfX4tMU+HsZ0ZZcspDrUXz307WCrGwgyEwZQ9K oity7HK0axo+KaEgXm5PmbLF7hWe0/TPkCWcVfBT/FD1sqzYFOoc3Ny4SZDWRVJEwZ7P Ks40B1qMHh38SrZMuej1vwV8CWpWe7UhILry0XhOi7FY672821DDOwKPTwo7ILxHH4OU Ge6oQknume1+Rc5Fx9Wjvy79KrYt26KpfUZZWUa+hL4Qep79pnlQOjPB7FFYCBuNAM7V FW7qRjth1KSVIT1iRhW12K+NzEtheTpamBmR4ZxONCGumHEQomAL1hR6qhuXXxEeZ+Dt fIVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WhzvsXaM; 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 l21si5511699pgb.409.2019.06.22.12.35.05; Sat, 22 Jun 2019 12:35:05 -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=WhzvsXaM; 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 S1726286AbfFVTfF (ORCPT + 3 others); Sat, 22 Jun 2019 15:35:05 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:46345 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726378AbfFVTfE (ORCPT ); Sat, 22 Jun 2019 15:35:04 -0400 Received: by mail-wr1-f66.google.com with SMTP id n4so9666050wrw.13 for ; Sat, 22 Jun 2019 12:35:03 -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=vL6L5hDUn9mhzlJsgchovHgAG3ROBqwVQK1Apo8D4Bw=; b=WhzvsXaMq4jr9ZNdR2e5Z3+WuuVRSfdwgoJWFzepBHWVIFtw4SJFdFc9Zf+Aj8mfOn Pyu7trs2uhvrJ5c52amgY4SuY/vAvLezoluLtCgbL9M9Cfxugqey5xvibRK1mL+briPO l3BVAOhcl+uSETi8LgvHrawTG5ZOuTAKC7aiMN3OBdpVuQwVgUtbctDkEC4g8SBL5+aH XEIpXFC4Mp5xAdftufoqYvrNqSe+F/WMfIYjD2wyp43lQYJ6VQoIzl9NBLsyR8DGUgSY Dj4FmqNbysPpblbedgJuwhbMgw89ZZDZSX8KdvfbcwE8o1AOLvV3vfj28tF8/e7//kMd Pwew== 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=vL6L5hDUn9mhzlJsgchovHgAG3ROBqwVQK1Apo8D4Bw=; b=quz+NGwl57wjLmSjE3ibq2YT1TJLYWuvAPm87f+KWsVMVk6rzFxQUiWpFxjdcOiGPq OI+Lk1EUJTddmef6MKGofQI6hBbMrXKuc84KpfjNRaL7fGn/t42JRnd+0CuAWibvrKZJ x1NBnoxyYsPQEf5HK+soQfM6EbHYzEV+8CzdYsofxZvSbi7trv8DzaE04zERmLBkSv83 CBs3F3ZoJR9gt9VSkjXOdIiF7JtnB7Pn6cdWYmUZijoNj7lWIT3iaL2ImhGir3spJlxJ xB4O4xWwgZawS7ymPTxTTi96Gn2MpMyqnPXnMSgSZ3eQ8wkTp9Ih4PhI8UKNNlju4g3W 6mww== X-Gm-Message-State: APjAAAVjtMTg3JhDp2WEudxjTX7YQ/Meh5lL72FSqsrbCJfgi9Dgr9Kn Msu7Xhn4i2os4lvFA9e+a00vekR6/bDqRoQ0 X-Received: by 2002:adf:fb81:: with SMTP id a1mr16653991wrr.329.1561232102531; Sat, 22 Jun 2019 12:35:02 -0700 (PDT) Received: from sudo.home ([2a01:cb1d:112:6f00:4bd:3f91:4ef8:ae7e]) by smtp.gmail.com with ESMTPSA id h8sm4814494wmf.12.2019.06.22.12.35.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 22 Jun 2019 12:35:01 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: herbert@gondor.apana.org.au, ebiggers@google.com, Ard Biesheuvel Subject: [PATCH v2 21/26] crypto: arm/aes-neonbs - provide a synchronous version of ctr(aes) Date: Sat, 22 Jun 2019 21:34:22 +0200 Message-Id: <20190622193427.20336-22-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190622193427.20336-1-ard.biesheuvel@linaro.org> References: <20190622193427.20336-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 AES in CTR mode is used by modes such as GCM and CCM, which are often used in contexts where only synchronous ciphers are permitted. So provide a synchronous version of ctr(aes) based on the existing code. This requires a non-SIMD fallback to deal with invocations occurring from a context where SIMD instructions may not be used. We have a helper for this now in the AES library, so wire that up. Signed-off-by: Ard Biesheuvel --- arch/arm/crypto/aes-neonbs-glue.c | 65 ++++++++++++++++++++ 1 file changed, 65 insertions(+) -- 2.20.1 diff --git a/arch/arm/crypto/aes-neonbs-glue.c b/arch/arm/crypto/aes-neonbs-glue.c index f43c9365b6a9..2f1aa199926c 100644 --- a/arch/arm/crypto/aes-neonbs-glue.c +++ b/arch/arm/crypto/aes-neonbs-glue.c @@ -9,8 +9,10 @@ */ #include +#include #include #include +#include #include #include #include @@ -57,6 +59,11 @@ struct aesbs_xts_ctx { struct crypto_cipher *tweak_tfm; }; +struct aesbs_ctr_ctx { + struct aesbs_ctx key; /* must be first member */ + struct crypto_aes_ctx fallback; +}; + static int aesbs_setkey(struct crypto_skcipher *tfm, const u8 *in_key, unsigned int key_len) { @@ -192,6 +199,25 @@ static void cbc_exit(struct crypto_tfm *tfm) crypto_free_cipher(ctx->enc_tfm); } +static int aesbs_ctr_setkey_sync(struct crypto_skcipher *tfm, const u8 *in_key, + unsigned int key_len) +{ + struct aesbs_ctr_ctx *ctx = crypto_skcipher_ctx(tfm); + int err; + + err = aes_expandkey(&ctx->fallback, in_key, key_len); + if (err) + return err; + + ctx->key.rounds = 6 + key_len / 4; + + kernel_neon_begin(); + aesbs_convert_key(ctx->key.rk, ctx->fallback.key_enc, ctx->key.rounds); + kernel_neon_end(); + + return 0; +} + static int ctr_encrypt(struct skcipher_request *req) { struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); @@ -234,6 +260,29 @@ static int ctr_encrypt(struct skcipher_request *req) return err; } +static void ctr_encrypt_one(struct crypto_skcipher *tfm, const u8 *src, u8 *dst) +{ + struct aesbs_ctr_ctx *ctx = crypto_skcipher_ctx(tfm); + unsigned long flags; + + /* + * Temporarily disable interrupts to avoid races where + * 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 crypto_ctr_encrypt_walk(req, ctr_encrypt_one); + + return ctr_encrypt(req); +} + static int aesbs_xts_setkey(struct crypto_skcipher *tfm, const u8 *in_key, unsigned int key_len) { @@ -361,6 +410,22 @@ static struct skcipher_alg aes_algs[] = { { .setkey = aesbs_setkey, .encrypt = ctr_encrypt, .decrypt = ctr_encrypt, +}, { + .base.cra_name = "ctr(aes)", + .base.cra_driver_name = "ctr-aes-neonbs-sync", + .base.cra_priority = 250 - 1, + .base.cra_blocksize = 1, + .base.cra_ctxsize = sizeof(struct aesbs_ctr_ctx), + .base.cra_module = THIS_MODULE, + + .min_keysize = AES_MIN_KEY_SIZE, + .max_keysize = AES_MAX_KEY_SIZE, + .chunksize = AES_BLOCK_SIZE, + .walksize = 8 * AES_BLOCK_SIZE, + .ivsize = AES_BLOCK_SIZE, + .setkey = aesbs_ctr_setkey_sync, + .encrypt = ctr_encrypt_sync, + .decrypt = ctr_encrypt_sync, }, { .base.cra_name = "__xts(aes)", .base.cra_driver_name = "__xts-aes-neonbs",