File Index
Files and directories
Sign in
FILE

aes-ce-glue.c

File preview with syntax highlighting.

linux-6.19.14/arch/arm64/crypto/aes-ce-glue.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions
  4 *
  5 * Copyright (C) 2013 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
  6 */
  7
  8#include <asm/neon.h>
  9#include <asm/simd.h>
 10#include <linux/unaligned.h>
 11#include <crypto/aes.h>
 12#include <crypto/algapi.h>
 13#include <crypto/internal/simd.h>
 14#include <linux/cpufeature.h>
 15#include <linux/module.h>
 16
 17#include "aes-ce-setkey.h"
 18
 19MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions");
 20MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
 21MODULE_LICENSE("GPL v2");
 22
 23struct aes_block {
 24	u8 b[AES_BLOCK_SIZE];
 25};
 26
 27asmlinkage void __aes_ce_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
 28asmlinkage void __aes_ce_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
 29
 30asmlinkage u32 __aes_ce_sub(u32 l);
 31asmlinkage void __aes_ce_invert(struct aes_block *out,
 32				const struct aes_block *in);
 33
 34static int num_rounds(struct crypto_aes_ctx *ctx)
 35{
 36	/*
 37	 * # of rounds specified by AES:
 38	 * 128 bit key		10 rounds
 39	 * 192 bit key		12 rounds
 40	 * 256 bit key		14 rounds
 41	 * => n byte key	=> 6 + (n/4) rounds
 42	 */
 43	return 6 + ctx->key_length / 4;
 44}
 45
 46static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
 47{
 48	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
 49
 50	if (!crypto_simd_usable()) {
 51		aes_encrypt(ctx, dst, src);
 52		return;
 53	}
 54
 55	scoped_ksimd()
 56		__aes_ce_encrypt(ctx->key_enc, dst, src, num_rounds(ctx));
 57}
 58
 59static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
 60{
 61	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
 62
 63	if (!crypto_simd_usable()) {
 64		aes_decrypt(ctx, dst, src);
 65		return;
 66	}
 67
 68	scoped_ksimd()
 69		__aes_ce_decrypt(ctx->key_dec, dst, src, num_rounds(ctx));
 70}
 71
 72int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
 73		     unsigned int key_len)
 74{
 75	/*
 76	 * The AES key schedule round constants
 77	 */
 78	static u8 const rcon[] = {
 79		0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
 80	};
 81
 82	u32 kwords = key_len / sizeof(u32);
 83	struct aes_block *key_enc, *key_dec;
 84	int i, j;
 85
 86	if (key_len != AES_KEYSIZE_128 &&
 87	    key_len != AES_KEYSIZE_192 &&
 88	    key_len != AES_KEYSIZE_256)
 89		return -EINVAL;
 90
 91	ctx->key_length = key_len;
 92	for (i = 0; i < kwords; i++)
 93		ctx->key_enc[i] = get_unaligned_le32(in_key + i * sizeof(u32));
 94
 95	scoped_ksimd() {
 96		for (i = 0; i < sizeof(rcon); i++) {
 97			u32 *rki = ctx->key_enc + (i * kwords);
 98			u32 *rko = rki + kwords;
 99
100			rko[0] = ror32(__aes_ce_sub(rki[kwords - 1]), 8) ^
101				 rcon[i] ^ rki[0];
102			rko[1] = rko[0] ^ rki[1];
103			rko[2] = rko[1] ^ rki[2];
104			rko[3] = rko[2] ^ rki[3];
105
106			if (key_len == AES_KEYSIZE_192) {
107				if (i >= 7)
108					break;
109				rko[4] = rko[3] ^ rki[4];
110				rko[5] = rko[4] ^ rki[5];
111			} else if (key_len == AES_KEYSIZE_256) {
112				if (i >= 6)
113					break;
114				rko[4] = __aes_ce_sub(rko[3]) ^ rki[4];
115				rko[5] = rko[4] ^ rki[5];
116				rko[6] = rko[5] ^ rki[6];
117				rko[7] = rko[6] ^ rki[7];
118			}
119		}
120
121		/*
122		 * Generate the decryption keys for the Equivalent Inverse
123		 * Cipher.  This involves reversing the order of the round
124		 * keys, and applying the Inverse Mix Columns transformation on
125		 * all but the first and the last one.
126		 */
127		key_enc = (struct aes_block *)ctx->key_enc;
128		key_dec = (struct aes_block *)ctx->key_dec;
129		j = num_rounds(ctx);
130
131		key_dec[0] = key_enc[j];
132		for (i = 1, j--; j > 0; i++, j--)
133			__aes_ce_invert(key_dec + i, key_enc + j);
134		key_dec[i] = key_enc[0];
135	}
136
137	return 0;
138}
139EXPORT_SYMBOL(ce_aes_expandkey);
140
141int ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
142		  unsigned int key_len)
143{
144	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
145
146	return ce_aes_expandkey(ctx, in_key, key_len);
147}
148EXPORT_SYMBOL(ce_aes_setkey);
149
150static struct crypto_alg aes_alg = {
151	.cra_name		= "aes",
152	.cra_driver_name	= "aes-ce",
153	.cra_priority		= 250,
154	.cra_flags		= CRYPTO_ALG_TYPE_CIPHER,
155	.cra_blocksize		= AES_BLOCK_SIZE,
156	.cra_ctxsize		= sizeof(struct crypto_aes_ctx),
157	.cra_module		= THIS_MODULE,
158	.cra_cipher = {
159		.cia_min_keysize	= AES_MIN_KEY_SIZE,
160		.cia_max_keysize	= AES_MAX_KEY_SIZE,
161		.cia_setkey		= ce_aes_setkey,
162		.cia_encrypt		= aes_cipher_encrypt,
163		.cia_decrypt		= aes_cipher_decrypt
164	}
165};
166
167static int __init aes_mod_init(void)
168{
169	return crypto_register_alg(&aes_alg);
170}
171
172static void __exit aes_mod_exit(void)
173{
174	crypto_unregister_alg(&aes_alg);
175}
176
177module_cpu_feature_match(AES, aes_mod_init);
178module_exit(aes_mod_exit);
~/ Read-only file preview.