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