Commit 2b1ef7ae authored by Eric Biggers's avatar Eric Biggers
Browse files

lib/crypto: arm64/aes: Migrate optimized code into library



Move the ARM64 optimized AES key expansion and single-block AES
en/decryption code into lib/crypto/, wire it up to the AES library API,
and remove the superseded crypto_cipher algorithms.

The result is that both the AES library and crypto_cipher APIs are now
optimized for ARM64, whereas previously only crypto_cipher was (and the
optimizations weren't enabled by default, which this fixes as well).

Note: to see the diff from arch/arm64/crypto/aes-ce-glue.c to
lib/crypto/arm64/aes.h, view this commit with 'git show -M10'.

Acked-by: default avatarArd Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20260112192035.10427-12-ebiggers@kernel.org


Signed-off-by: default avatarEric Biggers <ebiggers@kernel.org>
parent fa229775
Loading
Loading
Loading
Loading
+1 −25
Original line number Diff line number Diff line
@@ -37,34 +37,11 @@ config CRYPTO_SM3_ARM64_CE
	  Architecture: arm64 using:
	  - ARMv8.2 Crypto Extensions

config CRYPTO_AES_ARM64
	tristate "Ciphers: AES, modes: ECB, CBC, CTR, CTS, XCTR, XTS"
	select CRYPTO_AES
	help
	  Block ciphers: AES cipher algorithms (FIPS-197)
	  Length-preserving ciphers: AES with ECB, CBC, CTR, CTS,
	    XCTR, and XTS modes
	  AEAD cipher: AES with CBC, ESSIV, and SHA-256
	    for fscrypt and dm-crypt

	  Architecture: arm64

config CRYPTO_AES_ARM64_CE
	tristate "Ciphers: AES (ARMv8 Crypto Extensions)"
	depends on KERNEL_MODE_NEON
	select CRYPTO_ALGAPI
	select CRYPTO_LIB_AES
	help
	  Block ciphers: AES cipher algorithms (FIPS-197)

	  Architecture: arm64 using:
	  - ARMv8 Crypto Extensions

config CRYPTO_AES_ARM64_CE_BLK
	tristate "Ciphers: AES, modes: ECB/CBC/CTR/XTS (ARMv8 Crypto Extensions)"
	depends on KERNEL_MODE_NEON
	select CRYPTO_SKCIPHER
	select CRYPTO_AES_ARM64_CE
	select CRYPTO_LIB_AES
	select CRYPTO_LIB_SHA256
	help
	  Length-preserving ciphers: AES cipher algorithms (FIPS-197)
@@ -165,7 +142,6 @@ config CRYPTO_AES_ARM64_CE_CCM
	tristate "AEAD cipher: AES in CCM mode (ARMv8 Crypto Extensions)"
	depends on KERNEL_MODE_NEON
	select CRYPTO_ALGAPI
	select CRYPTO_AES_ARM64_CE
	select CRYPTO_AES_ARM64_CE_BLK
	select CRYPTO_AEAD
	select CRYPTO_LIB_AES
+0 −6
Original line number Diff line number Diff line
@@ -29,9 +29,6 @@ sm4-neon-y := sm4-neon-glue.o sm4-neon-core.o
obj-$(CONFIG_CRYPTO_GHASH_ARM64_CE) += ghash-ce.o
ghash-ce-y := ghash-ce-glue.o ghash-ce-core.o

obj-$(CONFIG_CRYPTO_AES_ARM64_CE) += aes-ce-cipher.o
aes-ce-cipher-y := aes-ce-core.o aes-ce-glue.o

obj-$(CONFIG_CRYPTO_AES_ARM64_CE_CCM) += aes-ce-ccm.o
aes-ce-ccm-y := aes-ce-ccm-glue.o aes-ce-ccm-core.o

@@ -41,8 +38,5 @@ aes-ce-blk-y := aes-glue-ce.o aes-ce.o
obj-$(CONFIG_CRYPTO_AES_ARM64_NEON_BLK) += aes-neon-blk.o
aes-neon-blk-y := aes-glue-neon.o aes-neon.o

obj-$(CONFIG_CRYPTO_AES_ARM64) += aes-arm64.o
aes-arm64-y := aes-cipher-core.o aes-cipher-glue.o

obj-$(CONFIG_CRYPTO_AES_ARM64_BS) += aes-neon-bs.o
aes-neon-bs-y := aes-neonbs-core.o aes-neonbs-glue.o
+0 −2
Original line number Diff line number Diff line
@@ -17,8 +17,6 @@

#include <asm/simd.h>

#include "aes-ce-setkey.h"

MODULE_IMPORT_NS("CRYPTO_INTERNAL");

static int num_rounds(struct crypto_aes_ctx *ctx)

arch/arm64/crypto/aes-ce-setkey.h

deleted100644 → 0
+0 −6
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */

int ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
		  unsigned int key_len);
int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
		     unsigned int key_len);
+0 −71
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Scalar AES core transform
 *
 * Copyright (C) 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
 */

#include <crypto/aes.h>
#include <crypto/algapi.h>
#include <linux/module.h>

asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
asmlinkage void __aes_arm64_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds);

static int aes_arm64_setkey(struct crypto_tfm *tfm, const u8 *in_key,
			    unsigned int key_len)
{
	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);

	return aes_expandkey(ctx, in_key, key_len);
}

static void aes_arm64_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
{
	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
	int rounds = 6 + ctx->key_length / 4;

	__aes_arm64_encrypt(ctx->key_enc, out, in, rounds);
}

static void aes_arm64_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
{
	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
	int rounds = 6 + ctx->key_length / 4;

	__aes_arm64_decrypt(ctx->key_dec, out, in, rounds);
}

static struct crypto_alg aes_alg = {
	.cra_name			= "aes",
	.cra_driver_name		= "aes-arm64",
	.cra_priority			= 200,
	.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,
	.cra_cipher.cia_max_keysize	= AES_MAX_KEY_SIZE,
	.cra_cipher.cia_setkey		= aes_arm64_setkey,
	.cra_cipher.cia_encrypt		= aes_arm64_encrypt,
	.cra_cipher.cia_decrypt		= aes_arm64_decrypt
};

static int __init aes_init(void)
{
	return crypto_register_alg(&aes_alg);
}

static void __exit aes_fini(void)
{
	crypto_unregister_alg(&aes_alg);
}

module_init(aes_init);
module_exit(aes_fini);

MODULE_DESCRIPTION("Scalar AES cipher for arm64");
MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS_CRYPTO("aes");
Loading