From patchwork Thu Feb 6 12:25:04 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 24255 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-vb0-f70.google.com (mail-vb0-f70.google.com [209.85.212.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 9A95C20445 for ; Thu, 6 Feb 2014 12:25:13 +0000 (UTC) Received: by mail-vb0-f70.google.com with SMTP id w17sf3956268vbj.9 for ; Thu, 06 Feb 2014 04:25:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=sjiRxf570WWIH61xP4SVHX96KWFxHsAJhp7Nq3NVKVA=; b=DA5Cf6uo+aPrIa9RARSlcM5bBpc5ztrucoRBc/wqgv5ea29451bHKVsbPPIRDxhOeQ aKZEp2wusielyf5aaobu8xDrfFK9M/zeSDUXsv3lJ6EKg8DeSG3/WMQ2Cv28kw8NUsWm zlUMQNI1lrsj13ZrLOXvvBpxWdu0OZg8cVncwvS2y8qg1pDFcQXEVKl7KW1CwPBTnSGN WNjcreGkdSHGpvwye0tpOh5wzke1+Qop3eZhQP/XGtXwvyt4+Y0vwy/bKJKT+xjqdLiI uoXRz0URaaMHmR23ySNkbcq7+WwZbsGzPDZ0xXJZQKI9ReJI8wnerzRK5qGwwChDIeXU Sapg== X-Gm-Message-State: ALoCoQmL1jLsCKX/q8grKpqG/PZisynZRfR+EV0VaZ8dolxLO5Q8+4jSiybGZfEzYsGMjbTyq69r X-Received: by 10.58.107.198 with SMTP id he6mr3148462veb.2.1391689512725; Thu, 06 Feb 2014 04:25:12 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.27.117 with SMTP id 108ls597860qgw.77.gmail; Thu, 06 Feb 2014 04:25:12 -0800 (PST) X-Received: by 10.221.20.199 with SMTP id qp7mr1532270vcb.24.1391689512640; Thu, 06 Feb 2014 04:25:12 -0800 (PST) Received: from mail-vb0-f42.google.com (mail-vb0-f42.google.com [209.85.212.42]) by mx.google.com with ESMTPS id x7si206660vel.0.2014.02.06.04.25.12 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 06 Feb 2014 04:25:12 -0800 (PST) Received-SPF: neutral (google.com: 209.85.212.42 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.212.42; Received: by mail-vb0-f42.google.com with SMTP id i3so1372063vbh.29 for ; Thu, 06 Feb 2014 04:25:12 -0800 (PST) X-Received: by 10.52.243.102 with SMTP id wx6mr4702783vdc.12.1391689512503; Thu, 06 Feb 2014 04:25:12 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.174.196 with SMTP id u4csp8405vcz; Thu, 6 Feb 2014 04:25:11 -0800 (PST) X-Received: by 10.68.239.70 with SMTP id vq6mr11968066pbc.152.1391689511490; Thu, 06 Feb 2014 04:25:11 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id pg10si923480pbb.354.2014.02.06.04.25.10; Thu, 06 Feb 2014 04:25:11 -0800 (PST) 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; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756239AbaBFMZJ (ORCPT + 1 other); Thu, 6 Feb 2014 07:25:09 -0500 Received: from mail-we0-f171.google.com ([74.125.82.171]:42326 "EHLO mail-we0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756204AbaBFMZI (ORCPT ); Thu, 6 Feb 2014 07:25:08 -0500 Received: by mail-we0-f171.google.com with SMTP id u56so1221429wes.2 for ; Thu, 06 Feb 2014 04:25:06 -0800 (PST) X-Received: by 10.194.86.200 with SMTP id r8mr1343112wjz.49.1391689506487; Thu, 06 Feb 2014 04:25:06 -0800 (PST) Received: from ards-macbook-pro.local (cag06-7-83-153-85-71.fbx.proxad.net. [83.153.85.71]) by mx.google.com with ESMTPSA id dd3sm2028920wjb.9.2014.02.06.04.25.05 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 06 Feb 2014 04:25:06 -0800 (PST) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: davem@davemloft.net, herbert@gondor.apana.org.au, jussi.kivilinna@iki.fi, Ard Biesheuvel Subject: [RFC PATCH 3/3] arm64: add Crypto Extensions based core AES cipher Date: Thu, 6 Feb 2014 13:25:04 +0100 Message-Id: <1391689504-28160-4-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1391689504-28160-1-git-send-email-ard.biesheuvel@linaro.org> References: <1391689504-28160-1-git-send-email-ard.biesheuvel@linaro.org> Sender: linux-crypto-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ard.biesheuvel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.212.42 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Signed-off-by: Ard Biesheuvel --- arch/arm64/Makefile | 1 + arch/arm64/crypto/Makefile | 13 ++ arch/arm64/crypto/aes-ce-cipher.c | 257 ++++++++++++++++++++++++++++++++++++++ crypto/Kconfig | 6 + 4 files changed, 277 insertions(+) create mode 100644 arch/arm64/crypto/Makefile create mode 100644 arch/arm64/crypto/aes-ce-cipher.c diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile index 2fceb71ac3b7..8185a913c5ed 100644 --- a/arch/arm64/Makefile +++ b/arch/arm64/Makefile @@ -45,6 +45,7 @@ export TEXT_OFFSET GZFLAGS core-y += arch/arm64/kernel/ arch/arm64/mm/ core-$(CONFIG_KVM) += arch/arm64/kvm/ core-$(CONFIG_XEN) += arch/arm64/xen/ +core-$(CONFIG_CRYPTO) += arch/arm64/crypto/ libs-y := arch/arm64/lib/ $(libs-y) libs-y += $(LIBGCC) diff --git a/arch/arm64/crypto/Makefile b/arch/arm64/crypto/Makefile new file mode 100644 index 000000000000..ac58945c50b3 --- /dev/null +++ b/arch/arm64/crypto/Makefile @@ -0,0 +1,13 @@ +# +# linux/arch/arm64/crypto/Makefile +# +# Copyright (C) 2013 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. +# + +obj-$(CONFIG_CRYPTO_AES_ARM64_CE) += aes-ce-cipher.o + +CFLAGS_aes-ce-cipher.o += -march=armv8-a+crypto diff --git a/arch/arm64/crypto/aes-ce-cipher.c b/arch/arm64/crypto/aes-ce-cipher.c new file mode 100644 index 000000000000..034f90077eed --- /dev/null +++ b/arch/arm64/crypto/aes-ce-cipher.c @@ -0,0 +1,257 @@ +/* + * linux/arch/arm64/crypto/aes-ce-cipher.c + * + * Copyright (C) 2013 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 +#include +#include +#include + +MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions"); +MODULE_AUTHOR("Ard Biesheuvel "); +MODULE_LICENSE("GPL"); + +static int num_rounds(struct crypto_aes_ctx *ctx) +{ + /* + * # of rounds specified by AES: + * 128 bit key 10 rounds + * 192 bit key 12 rounds + * 256 bit key 14 rounds + * => n byte key => 6 + (n/4) rounds + */ + return 6 + ctx->key_length / 4; +} + +static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]) +{ + struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); + + kernel_neon_begin_partial(4); + + __asm__(" ld1 {v0.16b}, [%[in]] ;" + " cmp %[rounds], #10 ;" + " bmi 0f ;" + " bne 3f ;" + " ld1 {v3.2d}, [%[key]], #16 ;" + " b 2f ;" + "0: ld1 {v2.2d-v3.2d}, [%[key]], #32 ;" + "1: aese v0.16b, v2.16b ;" + " aesmc v0.16b, v0.16b ;" + "2: aese v0.16b, v3.16b ;" + " aesmc v0.16b, v0.16b ;" + "3: ld1 {v1.2d-v3.2d}, [%[key]], #48 ;" + " subs %[rounds], %[rounds], #3 ;" + " aese v0.16b, v1.16b ;" + " aesmc v0.16b, v0.16b ;" + " bpl 1b ;" + " aese v0.16b, v2.16b ;" + " eor v0.16b, v0.16b, v3.16b ;" + " st1 {v0.16b}, [%[out]] ;" + : : + [out] "r"(dst), + [in] "r"(src), + [rounds] "r"(num_rounds(ctx) - 2), + [key] "r"(ctx->key_enc) + : "cc", "memory"); + + kernel_neon_end(); +} + +static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]) +{ + struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); + + kernel_neon_begin_partial(4); + + __asm__(" ld1 {v0.16b}, [%[in]] ;" + " cmp %[rounds], #10 ;" + " bmi 0f ;" + " bne 3f ;" + " ld1 {v3.2d}, [%[key]], #16 ;" + " b 2f ;" + "0: ld1 {v2.2d-v3.2d}, [%[key]], #32 ;" + "1: aesd v0.16b, v2.16b ;" + " aesimc v0.16b, v0.16b ;" + "2: aesd v0.16b, v3.16b ;" + " aesimc v0.16b, v0.16b ;" + "3: ld1 {v1.2d-v3.2d}, [%[key]], #48 ;" + " subs %[rounds], %[rounds], #3 ;" + " aesd v0.16b, v1.16b ;" + " aesimc v0.16b, v0.16b ;" + " bpl 1b ;" + " aesd v0.16b, v2.16b ;" + " eor v0.16b, v0.16b, v3.16b ;" + " st1 {v0.16b}, [%[out]] ;" + : : + [out] "r"(dst), + [in] "r"(src), + [rounds] "r"(num_rounds(ctx) - 2), + [key] "r"(ctx->key_dec) + : "cc", "memory"); + + kernel_neon_end(); +} + + +static void aes_cipher_enc_interleave(struct crypto_tfm *tfm, u8 dst[], + u8 const src[]) +{ + struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); + + kernel_neon_begin_partial(7); + + __asm__(" ld1 {v0.16b-v3.16b}, [%[in]] ;" + " cmp %[rounds], #10 ;" + " bmi 0f ;" + " bne 3f ;" + " ld1 {v6.2d}, [%[key]], #16 ;" + " b 2f ;" + "0: ld1 {v5.2d-v6.2d}, [%[key]], #32 ;" + "1: aese v0.16b, v5.16b ;" + " aesmc v0.16b, v0.16b ;" + " aese v1.16b, v5.16b ;" + " aesmc v1.16b, v1.16b ;" + " aese v2.16b, v5.16b ;" + " aesmc v2.16b, v2.16b ;" + " aese v3.16b, v5.16b ;" + " aesmc v3.16b, v3.16b ;" + "2: aese v0.16b, v6.16b ;" + " aesmc v0.16b, v0.16b ;" + " aese v1.16b, v6.16b ;" + " aesmc v1.16b, v1.16b ;" + " aese v2.16b, v6.16b ;" + " aesmc v2.16b, v2.16b ;" + " aese v3.16b, v6.16b ;" + " aesmc v3.16b, v3.16b ;" + "3: ld1 {v4.2d-v6.2d}, [%[key]], #48 ;" + " subs %[rounds], %[rounds], #3 ;" + " aese v0.16b, v4.16b ;" + " aesmc v0.16b, v0.16b ;" + " aese v1.16b, v4.16b ;" + " aesmc v1.16b, v1.16b ;" + " aese v2.16b, v4.16b ;" + " aesmc v2.16b, v2.16b ;" + " aese v3.16b, v4.16b ;" + " aesmc v3.16b, v3.16b ;" + " bpl 1b ;" + " aese v0.16b, v5.16b ;" + " aese v1.16b, v5.16b ;" + " aese v2.16b, v5.16b ;" + " aese v3.16b, v5.16b ;" + " eor v0.16b, v0.16b, v6.16b ;" + " eor v1.16b, v1.16b, v6.16b ;" + " eor v2.16b, v2.16b, v6.16b ;" + " eor v3.16b, v3.16b, v6.16b ;" + " st1 {v0.16b-v3.16b}, [%[out]] ;" + : : + [out] "r"(dst), + [in] "r"(src), + [rounds] "r"(num_rounds(ctx) - 2), + [key] "r"(ctx->key_enc) + : "cc", "memory"); + + kernel_neon_end(); +} + +static void aes_cipher_dec_interleave(struct crypto_tfm *tfm, u8 dst[], + u8 const src[]) +{ + struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); + + kernel_neon_begin_partial(7); + + __asm__(" ld1 {v0.16b-v3.16b}, [%[in]] ;" + " cmp %[rounds], #10 ;" + " bmi 0f ;" + " bne 3f ;" + " ld1 {v6.2d}, [%[key]], #16 ;" + " b 2f ;" + "0: ld1 {v5.2d-v6.2d}, [%[key]], #32 ;" + "1: aesd v0.16b, v5.16b ;" + " aesimc v0.16b, v0.16b ;" + " aesd v1.16b, v5.16b ;" + " aesimc v1.16b, v1.16b ;" + " aesd v2.16b, v5.16b ;" + " aesimc v2.16b, v2.16b ;" + " aesd v3.16b, v5.16b ;" + " aesimc v3.16b, v3.16b ;" + "2: aesd v0.16b, v6.16b ;" + " aesimc v0.16b, v0.16b ;" + " aesd v1.16b, v6.16b ;" + " aesimc v1.16b, v1.16b ;" + " aesd v2.16b, v6.16b ;" + " aesimc v2.16b, v2.16b ;" + " aesd v3.16b, v6.16b ;" + " aesimc v3.16b, v3.16b ;" + "3: ld1 {v4.2d-v6.2d}, [%[key]], #48 ;" + " subs %[rounds], %[rounds], #3 ;" + " aesd v0.16b, v4.16b ;" + " aesimc v0.16b, v0.16b ;" + " aesd v1.16b, v4.16b ;" + " aesimc v1.16b, v1.16b ;" + " aesd v2.16b, v4.16b ;" + " aesimc v2.16b, v2.16b ;" + " aesd v3.16b, v4.16b ;" + " aesimc v3.16b, v3.16b ;" + " bpl 1b ;" + " aesd v0.16b, v5.16b ;" + " aesd v1.16b, v5.16b ;" + " aesd v2.16b, v5.16b ;" + " aesd v3.16b, v5.16b ;" + " eor v0.16b, v0.16b, v6.16b ;" + " eor v1.16b, v1.16b, v6.16b ;" + " eor v2.16b, v2.16b, v6.16b ;" + " eor v3.16b, v3.16b, v6.16b ;" + " st1 {v0.16b-v3.16b}, [%[out]] ;" + : : + [out] "r"(dst), + [in] "r"(src), + [rounds] "r"(num_rounds(ctx) - 2), + [key] "r"(ctx->key_dec) + : "cc", "memory"); + + kernel_neon_end(); +} + +static struct crypto_alg aes_alg = { + .cra_name = "aes", + .cra_driver_name = "aes-ce", + .cra_priority = 300, + .cra_flags = CRYPTO_ALG_TYPE_CIPHER, + .cra_blocksize = AES_BLOCK_SIZE, + .cra_ctxsize = sizeof(struct crypto_aes_ctx), + .cra_module = THIS_MODULE, + .cra_cipher = { + .cia_min_keysize = AES_MIN_KEY_SIZE, + .cia_max_keysize = AES_MAX_KEY_SIZE, + .cia_setkey = crypto_aes_set_key, + .cia_encrypt = aes_cipher_encrypt, + .cia_decrypt = aes_cipher_decrypt, + .cia_interleave = 4, + .cia_enc_interleave = aes_cipher_enc_interleave, + .cia_dec_interleave = aes_cipher_dec_interleave, + } +}; + +static int __init aes_mod_init(void) +{ + if (!(elf_hwcap & HWCAP_AES)) + return -ENODEV; + return crypto_register_alg(&aes_alg); +} + +static void __exit aes_mod_exit(void) +{ + crypto_unregister_alg(&aes_alg); +} + +module_init(aes_mod_init); +module_exit(aes_mod_exit); diff --git a/crypto/Kconfig b/crypto/Kconfig index 7bcb70d216e1..f1d98bc346b6 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -791,6 +791,12 @@ config CRYPTO_AES_ARM_BS This implementation does not rely on any lookup tables so it is believed to be invulnerable to cache timing attacks. +config CRYPTO_AES_ARM64_CE + tristate "Synchronous AES cipher using ARMv8 Crypto Extensions" + depends on ARM64 && KERNEL_MODE_NEON + select CRYPTO_ALGAPI + select CRYPTO_AES + config CRYPTO_ANUBIS tristate "Anubis cipher algorithm" select CRYPTO_ALGAPI