From patchwork Sun Oct 13 12:15:01 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 3033261 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id A28829F372 for ; Sun, 13 Oct 2013 12:18:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B3BFB20328 for ; Sun, 13 Oct 2013 12:18:39 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 94B8320320 for ; Sun, 13 Oct 2013 12:18:38 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VVKbN-0003pG-EU; Sun, 13 Oct 2013 12:16:54 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1VVKaw-00010F-57; Sun, 13 Oct 2013 12:16:26 +0000 Received: from mail-wg0-f41.google.com ([74.125.82.41]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VVKaC-0000vE-DD for linux-arm-kernel@lists.infradead.org; Sun, 13 Oct 2013 12:15:44 +0000 Received: by mail-wg0-f41.google.com with SMTP id b13so1210814wgh.4 for ; Sun, 13 Oct 2013 05:15:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=/tlZ6MX115pk4vSdJq57z9W3aEJkK9047nF4HnGEFFM=; b=YwvDZK0OdKolyvx6XGYCtByZGxuhCAdRER9wmjT0lhq1XhaEndb0mjUprisLFCgBkm 5Ly4J5fmpMCmELkNz2PO/Byk8gqag28HTbbeRwUpN2xgfik2PRRfOpSZxLrU9dEnFheX j/k1ZIiU7OJzAogB7zUiGOyKHyZMxZ4fD2t2sSrPVCG7QDS1OwQ2h4vaB4Id2cbzvGzJ tzRw53OXpkNyuR5JdrQZDfdZbpMyVoPgNQCGVVYtwllsvTsrBuVa609pKghO/3JLr/XM iWHrB3jhGSgU/KRnEgX++6xFY4impseH9ufpPiOMQ9WHp2Zlr165RTCnVDRfXrWGQwOp SS5A== X-Gm-Message-State: ALoCoQlNan5hYtAJMwUQruIaKCXPrD9/avDbVyw8rQG2CHnhmSl9qQTDHhNvltwv97XJYo19zcGg X-Received: by 10.180.92.100 with SMTP id cl4mr10756687wib.1.1381666518865; Sun, 13 Oct 2013 05:15:18 -0700 (PDT) Received: from ards-mac-mini.local (cag06-7-83-153-85-71.fbx.proxad.net. [83.153.85.71]) by mx.google.com with ESMTPSA id bs15sm13032515wib.10.2013.10.13.05.15.17 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 13 Oct 2013 05:15:18 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Subject: [RFC v3 PATCH 5/7] ARM64: add Crypto Extensions based synchronous core AES cipher Date: Sun, 13 Oct 2013 14:15:01 +0200 Message-Id: <1381666503-23726-6-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1381666503-23726-1-git-send-email-ard.biesheuvel@linaro.org> References: <1381666503-23726-1-git-send-email-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20131013_081540_657550_A229ACB2 X-CRM114-Status: GOOD ( 13.13 ) X-Spam-Score: -2.6 (--) Cc: catalin.marinas@arm.com, Ard Biesheuvel , linux@arm.linux.org.uk, nico@linaro.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This implements the core AES cipher using the Crypto Extensions, using only NEON register q0 and q1. Signed-off-by: Ard Biesheuvel --- arch/arm64/Makefile | 11 +++-- arch/arm64/crypto/Makefile | 14 ++++++ arch/arm64/crypto/aes-sync.c | 106 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 arch/arm64/crypto/Makefile create mode 100644 arch/arm64/crypto/aes-sync.c diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile index d90cf79..d1ca9d8 100644 --- a/arch/arm64/Makefile +++ b/arch/arm64/Makefile @@ -36,11 +36,12 @@ TEXT_OFFSET := 0x00080000 export TEXT_OFFSET GZFLAGS -core-y += arch/arm64/kernel/ arch/arm64/mm/ -core-$(CONFIG_KVM) += arch/arm64/kvm/ -core-$(CONFIG_XEN) += arch/arm64/xen/ -libs-y := arch/arm64/lib/ $(libs-y) -libs-y += $(LIBGCC) +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) # Default target when executing plain make KBUILD_IMAGE := Image.gz diff --git a/arch/arm64/crypto/Makefile b/arch/arm64/crypto/Makefile new file mode 100644 index 0000000..269d9be --- /dev/null +++ b/arch/arm64/crypto/Makefile @@ -0,0 +1,14 @@ +# +# 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. +# + +aesce-sync-y := aes-sync.o +obj-m += aesce-sync.o + +CFLAGS_aes-sync.o += -march=armv8-a+crypto diff --git a/arch/arm64/crypto/aes-sync.c b/arch/arm64/crypto/aes-sync.c new file mode 100644 index 0000000..5d7ed4e --- /dev/null +++ b/arch/arm64/crypto/aes-sync.c @@ -0,0 +1,106 @@ +/* + * linux/arch/arm64/crypto/aes-sync.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 + +static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]) +{ + struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); + u32 rounds = 6 + ctx->key_length / 4; + DEFINE_NEON_REGSTACK_PARTIAL(regs, 2); + + kernel_neon_begin(regs); + + __asm__(" ld1 {v0.16b}, [%[in]] ;" + " ld1 {v1.16b}, [%[key]], #16 ;" + "0: aese v0.16b, v1.16b ;" + " subs %[rounds], %[rounds], #1 ;" + " ld1 {v1.16b}, [%[key]], #16 ;" + " beq 1f ;" + " aesmc v0.16b, v0.16b ;" + " b 0b ;" + "1: eor v0.16b, v0.16b, v1.16b ;" + " st1 {v0.16b}, [%[out]] ;" + : : + [out] "r"(dst), + [in] "r"(src), + [rounds] "r"(rounds), + [key] "r"(ctx->key_enc) + : "cc"); + + kernel_neon_end(regs); +} + +static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]) +{ + struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); + u32 rounds = 6 + ctx->key_length / 4; + DEFINE_NEON_REGSTACK_PARTIAL(regs, 2); + + kernel_neon_begin(regs); + + __asm__(" ld1 {v0.16b}, [%[in]] ;" + " ld1 {v1.16b}, [%[key]], #16 ;" + "0: aesd v0.16b, v1.16b ;" + " ld1 {v1.16b}, [%[key]], #16 ;" + " subs %[rounds], %[rounds], #1 ;" + " beq 1f ;" + " aesimc v0.16b, v0.16b ;" + " b 0b ;" + "1: eor v0.16b, v0.16b, v1.16b ;" + " st1 {v0.16b}, [%[out]] ;" + : : + [out] "r"(dst), + [in] "r"(src), + [rounds] "r"(rounds), + [key] "r"(ctx->key_dec) + : "cc"); + + kernel_neon_end(regs); +} + +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 + } +}; + +static int __init aes_mod_init(void) +{ + if (0) // TODO check for crypto extensions + 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); + +MODULE_DESCRIPTION("Synchronous AES using ARMv8 Crypto Extensions"); +MODULE_AUTHOR("Ard Biesheuvel "); +MODULE_LICENSE("GPL");