blob: 8ee1fb7aaa4fdc9e2535f06d982a90f49fec3881 [file] [log] [blame]
Ard Biesheuvel49788fe2014-03-21 10:19:17 +01001/*
2 * linux/arch/arm64/crypto/aes-glue.c - wrapper code for ARMv8 AES
3 *
4 * Copyright (C) 2013 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/hwcap.h>
13#include <crypto/aes.h>
Herbert Xud0ed0db2016-11-22 20:08:35 +080014#include <crypto/internal/simd.h>
15#include <crypto/internal/skcipher.h>
Ard Biesheuvel49788fe2014-03-21 10:19:17 +010016#include <linux/module.h>
17#include <linux/cpufeature.h>
Stephan Mueller49abc0d2016-02-17 07:00:01 +010018#include <crypto/xts.h>
Ard Biesheuvel49788fe2014-03-21 10:19:17 +010019
Ard Biesheuvel12ac3ef2014-11-03 16:50:01 +000020#include "aes-ce-setkey.h"
21
Ard Biesheuvel49788fe2014-03-21 10:19:17 +010022#ifdef USE_V8_CRYPTO_EXTENSIONS
23#define MODE "ce"
24#define PRIO 300
Ard Biesheuvel12ac3ef2014-11-03 16:50:01 +000025#define aes_setkey ce_aes_setkey
26#define aes_expandkey ce_aes_expandkey
Ard Biesheuvel49788fe2014-03-21 10:19:17 +010027#define aes_ecb_encrypt ce_aes_ecb_encrypt
28#define aes_ecb_decrypt ce_aes_ecb_decrypt
29#define aes_cbc_encrypt ce_aes_cbc_encrypt
30#define aes_cbc_decrypt ce_aes_cbc_decrypt
31#define aes_ctr_encrypt ce_aes_ctr_encrypt
32#define aes_xts_encrypt ce_aes_xts_encrypt
33#define aes_xts_decrypt ce_aes_xts_decrypt
34MODULE_DESCRIPTION("AES-ECB/CBC/CTR/XTS using ARMv8 Crypto Extensions");
35#else
36#define MODE "neon"
37#define PRIO 200
Ard Biesheuvel12ac3ef2014-11-03 16:50:01 +000038#define aes_setkey crypto_aes_set_key
39#define aes_expandkey crypto_aes_expand_key
Ard Biesheuvel49788fe2014-03-21 10:19:17 +010040#define aes_ecb_encrypt neon_aes_ecb_encrypt
41#define aes_ecb_decrypt neon_aes_ecb_decrypt
42#define aes_cbc_encrypt neon_aes_cbc_encrypt
43#define aes_cbc_decrypt neon_aes_cbc_decrypt
44#define aes_ctr_encrypt neon_aes_ctr_encrypt
45#define aes_xts_encrypt neon_aes_xts_encrypt
46#define aes_xts_decrypt neon_aes_xts_decrypt
47MODULE_DESCRIPTION("AES-ECB/CBC/CTR/XTS using ARMv8 NEON");
Kees Cook5d26a102014-11-20 17:05:53 -080048MODULE_ALIAS_CRYPTO("ecb(aes)");
49MODULE_ALIAS_CRYPTO("cbc(aes)");
50MODULE_ALIAS_CRYPTO("ctr(aes)");
51MODULE_ALIAS_CRYPTO("xts(aes)");
Ard Biesheuvel49788fe2014-03-21 10:19:17 +010052#endif
53
54MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
55MODULE_LICENSE("GPL v2");
56
57/* defined in aes-modes.S */
58asmlinkage void aes_ecb_encrypt(u8 out[], u8 const in[], u8 const rk[],
59 int rounds, int blocks, int first);
60asmlinkage void aes_ecb_decrypt(u8 out[], u8 const in[], u8 const rk[],
61 int rounds, int blocks, int first);
62
63asmlinkage void aes_cbc_encrypt(u8 out[], u8 const in[], u8 const rk[],
64 int rounds, int blocks, u8 iv[], int first);
65asmlinkage void aes_cbc_decrypt(u8 out[], u8 const in[], u8 const rk[],
66 int rounds, int blocks, u8 iv[], int first);
67
68asmlinkage void aes_ctr_encrypt(u8 out[], u8 const in[], u8 const rk[],
69 int rounds, int blocks, u8 ctr[], int first);
70
71asmlinkage void aes_xts_encrypt(u8 out[], u8 const in[], u8 const rk1[],
72 int rounds, int blocks, u8 const rk2[], u8 iv[],
73 int first);
74asmlinkage void aes_xts_decrypt(u8 out[], u8 const in[], u8 const rk1[],
75 int rounds, int blocks, u8 const rk2[], u8 iv[],
76 int first);
77
78struct crypto_aes_xts_ctx {
79 struct crypto_aes_ctx key1;
80 struct crypto_aes_ctx __aligned(8) key2;
81};
82
Herbert Xud0ed0db2016-11-22 20:08:35 +080083static int skcipher_aes_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
84 unsigned int key_len)
85{
86 return aes_setkey(crypto_skcipher_tfm(tfm), in_key, key_len);
87}
88
89static int xts_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +010090 unsigned int key_len)
91{
Herbert Xud0ed0db2016-11-22 20:08:35 +080092 struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +010093 int ret;
94
Herbert Xud0ed0db2016-11-22 20:08:35 +080095 ret = xts_verify_key(tfm, in_key, key_len);
Stephan Mueller28856a92016-02-09 15:37:47 +010096 if (ret)
97 return ret;
98
Ard Biesheuvel12ac3ef2014-11-03 16:50:01 +000099 ret = aes_expandkey(&ctx->key1, in_key, key_len / 2);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100100 if (!ret)
Ard Biesheuvel12ac3ef2014-11-03 16:50:01 +0000101 ret = aes_expandkey(&ctx->key2, &in_key[key_len / 2],
102 key_len / 2);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100103 if (!ret)
104 return 0;
105
Herbert Xud0ed0db2016-11-22 20:08:35 +0800106 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100107 return -EINVAL;
108}
109
Herbert Xud0ed0db2016-11-22 20:08:35 +0800110static int ecb_encrypt(struct skcipher_request *req)
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100111{
Herbert Xud0ed0db2016-11-22 20:08:35 +0800112 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
113 struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100114 int err, first, rounds = 6 + ctx->key_length / 4;
Herbert Xud0ed0db2016-11-22 20:08:35 +0800115 struct skcipher_walk walk;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100116 unsigned int blocks;
117
Herbert Xud0ed0db2016-11-22 20:08:35 +0800118 err = skcipher_walk_virt(&walk, req, true);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100119
120 kernel_neon_begin();
121 for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) {
122 aes_ecb_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
123 (u8 *)ctx->key_enc, rounds, blocks, first);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800124 err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100125 }
126 kernel_neon_end();
127 return err;
128}
129
Herbert Xud0ed0db2016-11-22 20:08:35 +0800130static int ecb_decrypt(struct skcipher_request *req)
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100131{
Herbert Xud0ed0db2016-11-22 20:08:35 +0800132 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
133 struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100134 int err, first, rounds = 6 + ctx->key_length / 4;
Herbert Xud0ed0db2016-11-22 20:08:35 +0800135 struct skcipher_walk walk;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100136 unsigned int blocks;
137
Herbert Xud0ed0db2016-11-22 20:08:35 +0800138 err = skcipher_walk_virt(&walk, req, true);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100139
140 kernel_neon_begin();
141 for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) {
142 aes_ecb_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
143 (u8 *)ctx->key_dec, rounds, blocks, first);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800144 err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100145 }
146 kernel_neon_end();
147 return err;
148}
149
Herbert Xud0ed0db2016-11-22 20:08:35 +0800150static int cbc_encrypt(struct skcipher_request *req)
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100151{
Herbert Xud0ed0db2016-11-22 20:08:35 +0800152 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
153 struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100154 int err, first, rounds = 6 + ctx->key_length / 4;
Herbert Xud0ed0db2016-11-22 20:08:35 +0800155 struct skcipher_walk walk;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100156 unsigned int blocks;
157
Herbert Xud0ed0db2016-11-22 20:08:35 +0800158 err = skcipher_walk_virt(&walk, req, true);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100159
160 kernel_neon_begin();
161 for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) {
162 aes_cbc_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
163 (u8 *)ctx->key_enc, rounds, blocks, walk.iv,
164 first);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800165 err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100166 }
167 kernel_neon_end();
168 return err;
169}
170
Herbert Xud0ed0db2016-11-22 20:08:35 +0800171static int cbc_decrypt(struct skcipher_request *req)
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100172{
Herbert Xud0ed0db2016-11-22 20:08:35 +0800173 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
174 struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100175 int err, first, rounds = 6 + ctx->key_length / 4;
Herbert Xud0ed0db2016-11-22 20:08:35 +0800176 struct skcipher_walk walk;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100177 unsigned int blocks;
178
Herbert Xud0ed0db2016-11-22 20:08:35 +0800179 err = skcipher_walk_virt(&walk, req, true);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100180
181 kernel_neon_begin();
182 for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) {
183 aes_cbc_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
184 (u8 *)ctx->key_dec, rounds, blocks, walk.iv,
185 first);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800186 err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100187 }
188 kernel_neon_end();
189 return err;
190}
191
Herbert Xud0ed0db2016-11-22 20:08:35 +0800192static int ctr_encrypt(struct skcipher_request *req)
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100193{
Herbert Xud0ed0db2016-11-22 20:08:35 +0800194 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
195 struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100196 int err, first, rounds = 6 + ctx->key_length / 4;
Herbert Xud0ed0db2016-11-22 20:08:35 +0800197 struct skcipher_walk walk;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100198 int blocks;
199
Herbert Xud0ed0db2016-11-22 20:08:35 +0800200 err = skcipher_walk_virt(&walk, req, true);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100201
202 first = 1;
203 kernel_neon_begin();
204 while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) {
205 aes_ctr_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
206 (u8 *)ctx->key_enc, rounds, blocks, walk.iv,
207 first);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800208 err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
Ard Biesheuvelb3e1e0c2016-11-29 13:05:33 +0000209 first = 0;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100210 }
Herbert Xud0ed0db2016-11-22 20:08:35 +0800211 if (walk.nbytes) {
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100212 u8 __aligned(8) tail[AES_BLOCK_SIZE];
Herbert Xud0ed0db2016-11-22 20:08:35 +0800213 unsigned int nbytes = walk.nbytes;
214 u8 *tdst = walk.dst.virt.addr;
215 u8 *tsrc = walk.src.virt.addr;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100216
217 /*
Ard Biesheuvelccc5d512017-01-28 23:25:34 +0000218 * Tell aes_ctr_encrypt() to process a tail block.
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100219 */
Ard Biesheuvelccc5d512017-01-28 23:25:34 +0000220 blocks = -1;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100221
Ard Biesheuvelccc5d512017-01-28 23:25:34 +0000222 aes_ctr_encrypt(tail, NULL, (u8 *)ctx->key_enc, rounds,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100223 blocks, walk.iv, first);
Ard Biesheuvelccc5d512017-01-28 23:25:34 +0000224 if (tdst != tsrc)
225 memcpy(tdst, tsrc, nbytes);
226 crypto_xor(tdst, tail, nbytes);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800227 err = skcipher_walk_done(&walk, 0);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100228 }
229 kernel_neon_end();
230
231 return err;
232}
233
Herbert Xud0ed0db2016-11-22 20:08:35 +0800234static int xts_encrypt(struct skcipher_request *req)
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100235{
Herbert Xud0ed0db2016-11-22 20:08:35 +0800236 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
237 struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100238 int err, first, rounds = 6 + ctx->key1.key_length / 4;
Herbert Xud0ed0db2016-11-22 20:08:35 +0800239 struct skcipher_walk walk;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100240 unsigned int blocks;
241
Herbert Xud0ed0db2016-11-22 20:08:35 +0800242 err = skcipher_walk_virt(&walk, req, true);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100243
244 kernel_neon_begin();
245 for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) {
246 aes_xts_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
247 (u8 *)ctx->key1.key_enc, rounds, blocks,
248 (u8 *)ctx->key2.key_enc, walk.iv, first);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800249 err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100250 }
251 kernel_neon_end();
252
253 return err;
254}
255
Herbert Xud0ed0db2016-11-22 20:08:35 +0800256static int xts_decrypt(struct skcipher_request *req)
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100257{
Herbert Xud0ed0db2016-11-22 20:08:35 +0800258 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
259 struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100260 int err, first, rounds = 6 + ctx->key1.key_length / 4;
Herbert Xud0ed0db2016-11-22 20:08:35 +0800261 struct skcipher_walk walk;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100262 unsigned int blocks;
263
Herbert Xud0ed0db2016-11-22 20:08:35 +0800264 err = skcipher_walk_virt(&walk, req, true);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100265
266 kernel_neon_begin();
267 for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) {
268 aes_xts_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
269 (u8 *)ctx->key1.key_dec, rounds, blocks,
270 (u8 *)ctx->key2.key_enc, walk.iv, first);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800271 err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100272 }
273 kernel_neon_end();
274
275 return err;
276}
277
Herbert Xud0ed0db2016-11-22 20:08:35 +0800278static struct skcipher_alg aes_algs[] = { {
279 .base = {
280 .cra_name = "__ecb(aes)",
281 .cra_driver_name = "__ecb-aes-" MODE,
282 .cra_priority = PRIO,
283 .cra_flags = CRYPTO_ALG_INTERNAL,
284 .cra_blocksize = AES_BLOCK_SIZE,
285 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
Herbert Xud0ed0db2016-11-22 20:08:35 +0800286 .cra_module = THIS_MODULE,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100287 },
Herbert Xud0ed0db2016-11-22 20:08:35 +0800288 .min_keysize = AES_MIN_KEY_SIZE,
289 .max_keysize = AES_MAX_KEY_SIZE,
290 .setkey = skcipher_aes_setkey,
291 .encrypt = ecb_encrypt,
292 .decrypt = ecb_decrypt,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100293}, {
Herbert Xud0ed0db2016-11-22 20:08:35 +0800294 .base = {
295 .cra_name = "__cbc(aes)",
296 .cra_driver_name = "__cbc-aes-" MODE,
297 .cra_priority = PRIO,
298 .cra_flags = CRYPTO_ALG_INTERNAL,
299 .cra_blocksize = AES_BLOCK_SIZE,
300 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
Herbert Xud0ed0db2016-11-22 20:08:35 +0800301 .cra_module = THIS_MODULE,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100302 },
Herbert Xud0ed0db2016-11-22 20:08:35 +0800303 .min_keysize = AES_MIN_KEY_SIZE,
304 .max_keysize = AES_MAX_KEY_SIZE,
305 .ivsize = AES_BLOCK_SIZE,
306 .setkey = skcipher_aes_setkey,
307 .encrypt = cbc_encrypt,
308 .decrypt = cbc_decrypt,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100309}, {
Herbert Xud0ed0db2016-11-22 20:08:35 +0800310 .base = {
311 .cra_name = "__ctr(aes)",
312 .cra_driver_name = "__ctr-aes-" MODE,
313 .cra_priority = PRIO,
314 .cra_flags = CRYPTO_ALG_INTERNAL,
315 .cra_blocksize = 1,
316 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
Herbert Xud0ed0db2016-11-22 20:08:35 +0800317 .cra_module = THIS_MODULE,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100318 },
Herbert Xud0ed0db2016-11-22 20:08:35 +0800319 .min_keysize = AES_MIN_KEY_SIZE,
320 .max_keysize = AES_MAX_KEY_SIZE,
321 .ivsize = AES_BLOCK_SIZE,
322 .chunksize = AES_BLOCK_SIZE,
323 .setkey = skcipher_aes_setkey,
324 .encrypt = ctr_encrypt,
325 .decrypt = ctr_encrypt,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100326}, {
Herbert Xud0ed0db2016-11-22 20:08:35 +0800327 .base = {
Ard Biesheuvel293614c2017-01-11 16:41:51 +0000328 .cra_name = "ctr(aes)",
329 .cra_driver_name = "ctr-aes-" MODE,
330 .cra_priority = PRIO - 1,
331 .cra_blocksize = 1,
332 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
Ard Biesheuvel293614c2017-01-11 16:41:51 +0000333 .cra_module = THIS_MODULE,
334 },
335 .min_keysize = AES_MIN_KEY_SIZE,
336 .max_keysize = AES_MAX_KEY_SIZE,
337 .ivsize = AES_BLOCK_SIZE,
338 .chunksize = AES_BLOCK_SIZE,
339 .setkey = skcipher_aes_setkey,
340 .encrypt = ctr_encrypt,
341 .decrypt = ctr_encrypt,
342}, {
343 .base = {
Herbert Xud0ed0db2016-11-22 20:08:35 +0800344 .cra_name = "__xts(aes)",
345 .cra_driver_name = "__xts-aes-" MODE,
346 .cra_priority = PRIO,
347 .cra_flags = CRYPTO_ALG_INTERNAL,
348 .cra_blocksize = AES_BLOCK_SIZE,
349 .cra_ctxsize = sizeof(struct crypto_aes_xts_ctx),
Herbert Xud0ed0db2016-11-22 20:08:35 +0800350 .cra_module = THIS_MODULE,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100351 },
Herbert Xud0ed0db2016-11-22 20:08:35 +0800352 .min_keysize = 2 * AES_MIN_KEY_SIZE,
353 .max_keysize = 2 * AES_MAX_KEY_SIZE,
354 .ivsize = AES_BLOCK_SIZE,
355 .setkey = xts_set_key,
356 .encrypt = xts_encrypt,
357 .decrypt = xts_decrypt,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100358} };
359
Ard Biesheuvel7f329c12016-11-29 13:05:30 +0000360static struct simd_skcipher_alg *aes_simd_algs[ARRAY_SIZE(aes_algs)];
Herbert Xud0ed0db2016-11-22 20:08:35 +0800361
362static void aes_exit(void)
363{
364 int i;
365
Ard Biesheuvel293614c2017-01-11 16:41:51 +0000366 for (i = 0; i < ARRAY_SIZE(aes_simd_algs); i++)
367 if (aes_simd_algs[i])
368 simd_skcipher_free(aes_simd_algs[i]);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800369
370 crypto_unregister_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
371}
372
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100373static int __init aes_init(void)
374{
Herbert Xud0ed0db2016-11-22 20:08:35 +0800375 struct simd_skcipher_alg *simd;
376 const char *basename;
377 const char *algname;
378 const char *drvname;
379 int err;
380 int i;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100381
Herbert Xud0ed0db2016-11-22 20:08:35 +0800382 err = crypto_register_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
383 if (err)
384 return err;
385
386 for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
Ard Biesheuvel293614c2017-01-11 16:41:51 +0000387 if (!(aes_algs[i].base.cra_flags & CRYPTO_ALG_INTERNAL))
388 continue;
389
Herbert Xud0ed0db2016-11-22 20:08:35 +0800390 algname = aes_algs[i].base.cra_name + 2;
391 drvname = aes_algs[i].base.cra_driver_name + 2;
392 basename = aes_algs[i].base.cra_driver_name;
393 simd = simd_skcipher_create_compat(algname, drvname, basename);
394 err = PTR_ERR(simd);
395 if (IS_ERR(simd))
396 goto unregister_simds;
397
398 aes_simd_algs[i] = simd;
399 }
400
401 return 0;
402
403unregister_simds:
404 aes_exit();
405 return err;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100406}
407
408#ifdef USE_V8_CRYPTO_EXTENSIONS
409module_cpu_feature_match(AES, aes_init);
410#else
411module_init(aes_init);
412#endif
413module_exit(aes_exit);