From patchwork Wed Oct 2 14:16:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 175002 Delivered-To: patch@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp818108ill; Wed, 2 Oct 2019 07:17:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqyjb+dQVwI6IteF1xsiNhtRfXcYdeHTkViGlZ71N0BiMPx+flWoj4QhwIQVzjyH4k3MpZGl X-Received: by 2002:a17:906:6994:: with SMTP id i20mr3273759ejr.239.1570025867395; Wed, 02 Oct 2019 07:17:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570025867; cv=none; d=google.com; s=arc-20160816; b=s8udJWYRYqMML4Ch70X1vIrcKPhQOrzOAjkt0nqK997LL3VF5MbKHPA+DgqHryYNi9 t9OHSY4idkph1TvnN2Eg8e23rp9m5HziRCLRySPKPuF5IF/AfgTry7nUYBJI/jckvg5t uzsarK8mwlNX224Gk31Pyr1ECYhu9kgFHnVtoDrxo2uGI2IjYAn4tzmdK2tOnIL8WJDq TQVyoo5r+/A8YkiitoTBlYlbLlXM/O9DbeaOeG13d3x1xWNguDz3ypZy/qNZZ3R+v5/d QptV0+pPZos2V2e6SWQ/3bthiVA36uBzsavs1tjirvwb2z+dTj5F+f0XpRQzPs7YtN8F ClPw== 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=/QbjOJPCo19W05ViTuArld7J+S6ZXXUJyNczgP4mU/c=; b=bC2Eg1hX4xr243sh0uDIXXVvpnXRY5Qj3VY1WAxOvd/68CPkp7OQXkrFiEDAv+diri BGbc4QnMx+KjlxFDg/ZkRHD2599roxpjkm/vLj8iZYwC0HJ3qxZpau5KVq+g4L59xu71 ZK2mS9lmFRdi4ugtxrA5O4sUs7AnDLgrj8Hop8I32RQ5jLLMMq/+0GnW9wT1l1IVBwdF tSv1A/a2GGziDk7XYRIiiFh1i12rQ0V8IEkRcHdfhO+nynK/Eda4XLlk9XoJk5oVzq7s zsPZ/NLTELhW3XmQhNd1IUn3LVDseU9n4q/GMR5s9FeA7zDhmTQNJCjCFtH+8Pa14rAS urLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=t10WMUy7; 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 va7si10939370ejb.4.2019.10.02.07.17.47; Wed, 02 Oct 2019 07:17:47 -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=t10WMUy7; 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 S1727536AbfJBORq (ORCPT + 3 others); Wed, 2 Oct 2019 10:17:46 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:51932 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727708AbfJBORq (ORCPT ); Wed, 2 Oct 2019 10:17:46 -0400 Received: by mail-wm1-f68.google.com with SMTP id 7so7417361wme.1 for ; Wed, 02 Oct 2019 07:17:45 -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=/QbjOJPCo19W05ViTuArld7J+S6ZXXUJyNczgP4mU/c=; b=t10WMUy7IMMg8xayMWCjw986RYIFjZULF97FIr9zmglkv8SB/hOldht1+SVnaApgA/ DvLtIQQVL7tlUWdcHwLabBmfLuwRYQSNPO/82NBSirWj26JN4eh1hFvBsnJ/JvzROiNT rMgKh6guPtdHY2ZjEI59pPjhODJNBI3no5GtY77sGcIz4wUjqY3jDXTnHS3yRGnNhAan Y+nB4lmBUenevQSu2KqXhvnry7tIx3ZJyOPgypCsQB18oomKj4cplzViwz97z8alXULW qYDO2O0yYgL99lmUwZsTgQKoFqyMJX2e2uoQRxzy2k8ymOTtdoKcvClHzBhRGnydsZ3E I8Fw== 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=/QbjOJPCo19W05ViTuArld7J+S6ZXXUJyNczgP4mU/c=; b=NZlofuKbzIqQnWMzMyfNDBiyx5vJpAt62oYPpT8mILqlhowgwbM3cgX0o8kjqJYIuC Ka483OEMfSadga7yuDANiilSk2ni2tQuz/8PyvhitWKEvKMkH/uhp4LAExc8MR0yE4S5 bmo+KFi6whJg8MVcIzaaR2t5Ahy/tBFjG/VZFPTOdJXktLDiCuhMjKvxHUtjPRP1T07a s+7BwtkXBEgXzw8aTNGpjJ9yNu5iNwRcM/u4j0yU1MEYGCodSl2njedts0onCPOrZwyI pVGQCW04vBAZZ+oXkYrqaSrUbcniTf1uUqz2J2ynXu8z8VzJGLvVBZjIGQjeGL3lgC0Z 8WPw== X-Gm-Message-State: APjAAAXMVuh6+5Rpo5ZcZrtjWYoMy/G8LAxXjdnEx3K3BCHYK19ktNQ5 lnsfvOxHbW9STdKs2K5vn7VgMidka2zJX5gQ X-Received: by 2002:a1c:4108:: with SMTP id o8mr3288419wma.129.1570025864186; Wed, 02 Oct 2019 07:17:44 -0700 (PDT) Received: from sudo.home ([2a01:cb1d:112:6f00:f145:3252:fc29:76c9]) by smtp.gmail.com with ESMTPSA id t13sm41078149wra.70.2019.10.02.07.17.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 Oct 2019 07:17:43 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: Ard Biesheuvel , Herbert Xu , David Miller , Greg KH , Linus Torvalds , "Jason A . Donenfeld" , Samuel Neves , Dan Carpenter , Arnd Bergmann , Eric Biggers , Andy Lutomirski , Will Deacon , Marc Zyngier , Catalin Marinas , Martin Willi , Peter Zijlstra , Josh Poimboeuf Subject: [PATCH v2 04/20] crypto: arm/chacha - expose ARM ChaCha routine as library function Date: Wed, 2 Oct 2019 16:16:57 +0200 Message-Id: <20191002141713.31189-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191002141713.31189-1-ard.biesheuvel@linaro.org> References: <20191002141713.31189-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 Expose the accelerated NEON ChaCha routine directly as a symbol export so that users of the ChaCha library can use it directly. Signed-off-by: Ard Biesheuvel --- arch/arm/crypto/Kconfig | 1 + arch/arm/crypto/chacha-neon-glue.c | 40 ++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) -- 2.20.1 diff --git a/arch/arm/crypto/Kconfig b/arch/arm/crypto/Kconfig index b24df84a1d7a..70e4d5fe5bdb 100644 --- a/arch/arm/crypto/Kconfig +++ b/arch/arm/crypto/Kconfig @@ -130,6 +130,7 @@ config CRYPTO_CHACHA20_NEON depends on KERNEL_MODE_NEON select CRYPTO_BLKCIPHER select CRYPTO_CHACHA20 + select CRYPTO_ARCH_HAVE_LIB_CHACHA config CRYPTO_NHPOLY1305_NEON tristate "NEON accelerated NHPoly1305 hash function (for Adiantum)" diff --git a/arch/arm/crypto/chacha-neon-glue.c b/arch/arm/crypto/chacha-neon-glue.c index 26576772f18b..eee0f6e4f5d2 100644 --- a/arch/arm/crypto/chacha-neon-glue.c +++ b/arch/arm/crypto/chacha-neon-glue.c @@ -36,6 +36,8 @@ asmlinkage void chacha_4block_xor_neon(const u32 *state, u8 *dst, const u8 *src, int nrounds); asmlinkage void hchacha_block_neon(const u32 *state, u32 *out, int nrounds); +static bool have_neon __ro_after_init; + static void chacha_doneon(u32 *state, u8 *dst, const u8 *src, unsigned int bytes, int nrounds) { @@ -62,6 +64,36 @@ static void chacha_doneon(u32 *state, u8 *dst, const u8 *src, } } +void hchacha_block(const u32 *state, u32 *stream, int nrounds) +{ + if (!crypto_simd_usable()) { + hchacha_block_generic(state, stream, nrounds); + } else { + kernel_neon_begin(); + hchacha_block_neon(state, stream, nrounds); + kernel_neon_end(); + } +} +EXPORT_SYMBOL(hchacha_block); + +void chacha_init(u32 *state, const u32 *key, const u8 *iv) +{ + chacha_init_generic(state, key, iv); +} +EXPORT_SYMBOL(chacha_init); + +void chacha_crypt(u32 *state, u8 *dst, const u8 *src, unsigned int bytes, + int nrounds) +{ + if (!have_neon || bytes <= CHACHA_BLOCK_SIZE || !crypto_simd_usable()) + return chacha_crypt_generic(state, dst, src, bytes, nrounds); + + kernel_neon_begin(); + chacha_doneon(state, dst, src, bytes, nrounds); + kernel_neon_end(); +} +EXPORT_SYMBOL(chacha_crypt); + static int chacha_neon_stream_xor(struct skcipher_request *req, const struct chacha_ctx *ctx, const u8 *iv) { @@ -177,15 +209,17 @@ static struct skcipher_alg algs[] = { static int __init chacha_simd_mod_init(void) { - if (!(elf_hwcap & HWCAP_NEON)) - return -ENODEV; + have_neon = (elf_hwcap & HWCAP_NEON); + if (!have_neon) + return 0; return crypto_register_skciphers(algs, ARRAY_SIZE(algs)); } static void __exit chacha_simd_mod_fini(void) { - crypto_unregister_skciphers(algs, ARRAY_SIZE(algs)); + if (have_neon) + crypto_unregister_skciphers(algs, ARRAY_SIZE(algs)); } module_init(chacha_simd_mod_init);