From patchwork Wed Apr 25 12:20:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 10363003 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 28C016032C for ; Wed, 25 Apr 2018 12:23:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1737828FB0 for ; Wed, 25 Apr 2018 12:23:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0BC8728FB5; Wed, 25 Apr 2018 12:23:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 57DCB28FB0 for ; Wed, 25 Apr 2018 12:23:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=bwcvlApxNzWmQ3rMNoAEQOBOjr7ZzIR9i5Zo+FP5v3E=; b=iWKEoItK0gTBDTDw8VI9jT/Qem Ll8HhBvEu+jp+92/7g4xPJDkb5DvqP9bd13r3VmR0WLTBusMwBlb3ANGZKLlNWPWHmQjPyFaRK1ha 2fRP37z3+H68KbVap9USN7xH5/PL+aDgVC8unmm7HmmzrMRl45yC04R9gkwoRUEXOS5TSyLNK6Gpi 9G2pQIuAS+CleATtcTplZ0xJKhd3aUHS7MMprPEbIlE/WSzkoa4InrkBcGtQlXampgoojEaBGyzyt ru9+/a3X8s4mW0ubSDFKvHHPzK3jsdQUychDtYD2U37cxFkhxMVUPzWeGYZFJiDywvCCUUzfEKTyP XbiT0PSw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fBJRs-000403-Vf; Wed, 25 Apr 2018 12:23:01 +0000 Received: from merlin.infradead.org ([2001:8b0:10b:1231::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fBJQG-0003FC-CV for linux-arm-kernel@bombadil.infradead.org; Wed, 25 Apr 2018 12:21:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=9RE/uvXvcerLd9sxEPNOb3OfCjz1ATjAIcohc0iVVNQ=; b=b7nn4vbexu0rJfxnGp1Zp06RR 1KuIltH21ScbY6CYt1mJhc7jRmo6vtBWI6fetoLBbHFKgRdjYsvwZuNwSl2IdsHb40lGlKUKVKdP+ Ji3DVAc0BOXNpx0CDexHs0yvZO54LFP47m8dlsNd3fsvRX17fdO+J2MLVi/NZoes+tsHE8UJSiACE M45iDH6x/nx6HGnb1/WvLWgZ1pPRvhWXZe71h/pEWVNtNJ5BomCdDKgCkLgEkw/NQtbmgl0t1jBgj wkH5aldacfVOdvAUSxb7RFgVAx7B0q72nzvWSCHPTkijmMkKmyP+Ci5QO+feBISIJUpkwDFHJXbfD +ZFFOnbSA==; Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]) by merlin.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fBJQC-0000Vv-0Y for linux-arm-kernel@lists.infradead.org; Wed, 25 Apr 2018 12:21:18 +0000 Received: by mail-wm0-x241.google.com with SMTP id w2so4361045wmw.1 for ; Wed, 25 Apr 2018 05:21:02 -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=9RE/uvXvcerLd9sxEPNOb3OfCjz1ATjAIcohc0iVVNQ=; b=HqMCvHI/ngswWXAqPmfSPPNtJd287P24qbUXHDWGN2/PbvQlUmirofDhlUKLXcIYZ/ K9sWy1ZjkTKl9U6sny5Jm1zWbcgL/apCBltMORIklnU0nQDx9fc+WtXaQB03r28ywzbS 6OXyYsoKBZ0+TTQj8/lO2X6Poxp0Z9oBFyszc= 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=9RE/uvXvcerLd9sxEPNOb3OfCjz1ATjAIcohc0iVVNQ=; b=FReYcT6tVQ44yiO2jsZICtPf5piXf3+v4kYVPy+QlVqmvAHbP5gzViW8uXONNwV44m gyznbgKnE0JBrxSD5f+S7fD3BiG7DK9gDIE95dj50Vnh89HrGs4fJ2LToBY3o516iyVh xbVlPHdcJhA5rINpzmvDTZNze7TeM3P0jN2tjJW9dYVEiadFEovKOt8Yff9bKbY14PGS Io/l6RN6c9IdKrWMVr5uc4+tNXQxw6YFafEjHCgujGNrw3N8xI2T2sPzHRU8eRx7MrUc cmoXBeScZzKtpEvCfiUH1h17WYoWaZ3zoU7wuBVTYO1dUAG2fZh7lCgI1pr+d+PkZnpS Ky+A== X-Gm-Message-State: ALQs6tBG9zWpwsJkvteAGabWTpnzhnmSe50g3eOh7ZLoeCtvL+Ke2Lik cO6f3JMILNo+tyTNffeofNS9RUlT/aU= X-Google-Smtp-Source: AB8JxZqxKUnOGD8I4cZ6FQcpaDsT/FdLOHusUbUP5/5jc4lSElx+VfdlFhZdkfZAIA7vgQnDhMF76w== X-Received: by 10.28.71.13 with SMTP id u13mr3938272wma.56.1524658860160; Wed, 25 Apr 2018 05:21:00 -0700 (PDT) Received: from localhost.localdomain ([2a01:e35:3995:5470:200:1aff:fe1b:b328]) by smtp.gmail.com with ESMTPSA id s14sm13073572wmb.5.2018.04.25.05.20.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 25 Apr 2018 05:20:59 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Subject: [PATCH 2/2] crypto: arm64 - add support for SM4 encryption using special instructions Date: Wed, 25 Apr 2018 14:20:46 +0200 Message-Id: <20180425122046.12695-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180425122046.12695-1-ard.biesheuvel@linaro.org> References: <20180425122046.12695-1-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180425_082116_252011_209C4F09 X-CRM114-Status: GOOD ( 18.65 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: gilad@benyossef.com, herbert@gondor.apana.org.au, linux-arm-kernel@lists.infradead.org, Ard Biesheuvel MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add support for the SM4 symmetric cipher implemented using the special SM4 instructions introduced in ARM architecture revision 8.2. Signed-off-by: Ard Biesheuvel --- arch/arm64/crypto/Kconfig | 6 ++ arch/arm64/crypto/Makefile | 3 + arch/arm64/crypto/sm4-ce-core.S | 36 ++++++++++ arch/arm64/crypto/sm4-ce-glue.c | 73 ++++++++++++++++++++ 4 files changed, 118 insertions(+) diff --git a/arch/arm64/crypto/Kconfig b/arch/arm64/crypto/Kconfig index cb5a243110c4..e3fdb0fd6f70 100644 --- a/arch/arm64/crypto/Kconfig +++ b/arch/arm64/crypto/Kconfig @@ -47,6 +47,12 @@ config CRYPTO_SM3_ARM64_CE select CRYPTO_HASH select CRYPTO_SM3 +config CRYPTO_SM4_ARM64_CE + tristate "SM4 symmetric cipher (ARMv8.2 Crypto Extensions)" + depends on KERNEL_MODE_NEON + select CRYPTO_ALGAPI + select CRYPTO_SM4 + config CRYPTO_GHASH_ARM64_CE tristate "GHASH/AES-GCM using ARMv8 Crypto Extensions" depends on KERNEL_MODE_NEON diff --git a/arch/arm64/crypto/Makefile b/arch/arm64/crypto/Makefile index f35ac684b1c0..bcafd016618e 100644 --- a/arch/arm64/crypto/Makefile +++ b/arch/arm64/crypto/Makefile @@ -23,6 +23,9 @@ sha3-ce-y := sha3-ce-glue.o sha3-ce-core.o obj-$(CONFIG_CRYPTO_SM3_ARM64_CE) += sm3-ce.o sm3-ce-y := sm3-ce-glue.o sm3-ce-core.o +obj-$(CONFIG_CRYPTO_SM4_ARM64_CE) += sm4-ce.o +sm4-ce-y := sm4-ce-glue.o sm4-ce-core.o + obj-$(CONFIG_CRYPTO_GHASH_ARM64_CE) += ghash-ce.o ghash-ce-y := ghash-ce-glue.o ghash-ce-core.o diff --git a/arch/arm64/crypto/sm4-ce-core.S b/arch/arm64/crypto/sm4-ce-core.S new file mode 100644 index 000000000000..af3bfbc3f4d4 --- /dev/null +++ b/arch/arm64/crypto/sm4-ce-core.S @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + + .irp b, 0, 1, 2, 3, 4, 5, 6, 7, 8 + .set .Lv\b\().4s, \b + .endr + + .macro sm4e, rd, rn + .inst 0xcec08400 | .L\rd | (.L\rn << 5) + .endm + + /* + * void sm4_ce_do_crypt(const u32 *rk, u32 *out, const u32 *in); + */ + .text +ENTRY(sm4_ce_do_crypt) + ld1 {v8.4s}, [x2] + ld1 {v0.4s-v3.4s}, [x0], #64 +CPU_LE( rev32 v8.16b, v8.16b ) + ld1 {v4.4s-v7.4s}, [x0] + sm4e v8.4s, v0.4s + sm4e v8.4s, v1.4s + sm4e v8.4s, v2.4s + sm4e v8.4s, v3.4s + sm4e v8.4s, v4.4s + sm4e v8.4s, v5.4s + sm4e v8.4s, v6.4s + sm4e v8.4s, v7.4s + rev64 v8.4s, v8.4s + ext v8.16b, v8.16b, v8.16b, #8 +CPU_LE( rev32 v8.16b, v8.16b ) + st1 {v8.4s}, [x1] + ret +ENDPROC(sm4_ce_do_crypt) diff --git a/arch/arm64/crypto/sm4-ce-glue.c b/arch/arm64/crypto/sm4-ce-glue.c new file mode 100644 index 000000000000..b7fb5274b250 --- /dev/null +++ b/arch/arm64/crypto/sm4-ce-glue.c @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include + +MODULE_ALIAS_CRYPTO("sm4"); +MODULE_ALIAS_CRYPTO("sm4-ce"); +MODULE_DESCRIPTION("SM4 symmetric cipher using ARMv8 Crypto Extensions"); +MODULE_AUTHOR("Ard Biesheuvel "); +MODULE_LICENSE("GPL v2"); + +asmlinkage void sm4_ce_do_crypt(const u32 *rk, void *out, const void *in); + +static void sm4_ce_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) +{ + const struct crypto_sm4_ctx *ctx = crypto_tfm_ctx(tfm); + + if (!may_use_simd()) { + crypto_sm4_encrypt(tfm, out, in); + } else { + kernel_neon_begin(); + sm4_ce_do_crypt(ctx->rkey_enc, out, in); + kernel_neon_end(); + } +} + +static void sm4_ce_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) +{ + const struct crypto_sm4_ctx *ctx = crypto_tfm_ctx(tfm); + + if (!may_use_simd()) { + crypto_sm4_decrypt(tfm, out, in); + } else { + kernel_neon_begin(); + sm4_ce_do_crypt(ctx->rkey_dec, out, in); + kernel_neon_end(); + } +} + +static struct crypto_alg sm4_ce_alg = { + .cra_name = "sm4", + .cra_driver_name = "sm4-ce", + .cra_priority = 200, + .cra_flags = CRYPTO_ALG_TYPE_CIPHER, + .cra_blocksize = SM4_BLOCK_SIZE, + .cra_ctxsize = sizeof(struct crypto_sm4_ctx), + .cra_module = THIS_MODULE, + .cra_u.cipher = { + .cia_min_keysize = SM4_KEY_SIZE, + .cia_max_keysize = SM4_KEY_SIZE, + .cia_setkey = crypto_sm4_set_key, + .cia_encrypt = sm4_ce_encrypt, + .cia_decrypt = sm4_ce_decrypt + } +}; + +static int __init sm4_ce_mod_init(void) +{ + return crypto_register_alg(&sm4_ce_alg); +} + +static void __exit sm4_ce_mod_fini(void) +{ + crypto_unregister_alg(&sm4_ce_alg); +} + +module_cpu_feature_match(SM3, sm4_ce_mod_init); +module_exit(sm4_ce_mod_fini);