blob: 5164aaf82c6a204c50b06b74d97a3d477bf7db97 [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 /*
218 * Minimum alignment is 8 bytes, so if nbytes is <= 8, we need
219 * to tell aes_ctr_encrypt() to only read half a block.
220 */
221 blocks = (nbytes <= 8) ? -1 : 1;
222
223 aes_ctr_encrypt(tail, tsrc, (u8 *)ctx->key_enc, rounds,
224 blocks, walk.iv, first);
225 memcpy(tdst, tail, nbytes);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800226 err = skcipher_walk_done(&walk, 0);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100227 }
228 kernel_neon_end();
229
230 return err;
231}
232
Herbert Xud0ed0db2016-11-22 20:08:35 +0800233static int xts_encrypt(struct skcipher_request *req)
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100234{
Herbert Xud0ed0db2016-11-22 20:08:35 +0800235 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
236 struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100237 int err, first, rounds = 6 + ctx->key1.key_length / 4;
Herbert Xud0ed0db2016-11-22 20:08:35 +0800238 struct skcipher_walk walk;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100239 unsigned int blocks;
240
Herbert Xud0ed0db2016-11-22 20:08:35 +0800241 err = skcipher_walk_virt(&walk, req, true);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100242
243 kernel_neon_begin();
244 for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) {
245 aes_xts_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
246 (u8 *)ctx->key1.key_enc, rounds, blocks,
247 (u8 *)ctx->key2.key_enc, walk.iv, first);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800248 err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100249 }
250 kernel_neon_end();
251
252 return err;
253}
254
Herbert Xud0ed0db2016-11-22 20:08:35 +0800255static int xts_decrypt(struct skcipher_request *req)
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100256{
Herbert Xud0ed0db2016-11-22 20:08:35 +0800257 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
258 struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100259 int err, first, rounds = 6 + ctx->key1.key_length / 4;
Herbert Xud0ed0db2016-11-22 20:08:35 +0800260 struct skcipher_walk walk;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100261 unsigned int blocks;
262
Herbert Xud0ed0db2016-11-22 20:08:35 +0800263 err = skcipher_walk_virt(&walk, req, true);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100264
265 kernel_neon_begin();
266 for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) {
267 aes_xts_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
268 (u8 *)ctx->key1.key_dec, rounds, blocks,
269 (u8 *)ctx->key2.key_enc, walk.iv, first);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800270 err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100271 }
272 kernel_neon_end();
273
274 return err;
275}
276
Herbert Xud0ed0db2016-11-22 20:08:35 +0800277static struct skcipher_alg aes_algs[] = { {
278 .base = {
279 .cra_name = "__ecb(aes)",
280 .cra_driver_name = "__ecb-aes-" MODE,
281 .cra_priority = PRIO,
282 .cra_flags = CRYPTO_ALG_INTERNAL,
283 .cra_blocksize = AES_BLOCK_SIZE,
284 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
285 .cra_alignmask = 7,
286 .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),
301 .cra_alignmask = 7,
302 .cra_module = THIS_MODULE,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100303 },
Herbert Xud0ed0db2016-11-22 20:08:35 +0800304 .min_keysize = AES_MIN_KEY_SIZE,
305 .max_keysize = AES_MAX_KEY_SIZE,
306 .ivsize = AES_BLOCK_SIZE,
307 .setkey = skcipher_aes_setkey,
308 .encrypt = cbc_encrypt,
309 .decrypt = cbc_decrypt,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100310}, {
Herbert Xud0ed0db2016-11-22 20:08:35 +0800311 .base = {
312 .cra_name = "__ctr(aes)",
313 .cra_driver_name = "__ctr-aes-" MODE,
314 .cra_priority = PRIO,
315 .cra_flags = CRYPTO_ALG_INTERNAL,
316 .cra_blocksize = 1,
317 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
318 .cra_alignmask = 7,
319 .cra_module = THIS_MODULE,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100320 },
Herbert Xud0ed0db2016-11-22 20:08:35 +0800321 .min_keysize = AES_MIN_KEY_SIZE,
322 .max_keysize = AES_MAX_KEY_SIZE,
323 .ivsize = AES_BLOCK_SIZE,
324 .chunksize = AES_BLOCK_SIZE,
325 .setkey = skcipher_aes_setkey,
326 .encrypt = ctr_encrypt,
327 .decrypt = ctr_encrypt,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100328}, {
Herbert Xud0ed0db2016-11-22 20:08:35 +0800329 .base = {
Ard Biesheuvel293614c2017-01-11 16:41:51 +0000330 .cra_name = "ctr(aes)",
331 .cra_driver_name = "ctr-aes-" MODE,
332 .cra_priority = PRIO - 1,
333 .cra_blocksize = 1,
334 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
335 .cra_alignmask = 7,
336 .cra_module = THIS_MODULE,
337 },
338 .min_keysize = AES_MIN_KEY_SIZE,
339 .max_keysize = AES_MAX_KEY_SIZE,
340 .ivsize = AES_BLOCK_SIZE,
341 .chunksize = AES_BLOCK_SIZE,
342 .setkey = skcipher_aes_setkey,
343 .encrypt = ctr_encrypt,
344 .decrypt = ctr_encrypt,
345}, {
346 .base = {
Herbert Xud0ed0db2016-11-22 20:08:35 +0800347 .cra_name = "__xts(aes)",
348 .cra_driver_name = "__xts-aes-" MODE,
349 .cra_priority = PRIO,
350 .cra_flags = CRYPTO_ALG_INTERNAL,
351 .cra_blocksize = AES_BLOCK_SIZE,
352 .cra_ctxsize = sizeof(struct crypto_aes_xts_ctx),
353 .cra_alignmask = 7,
354 .cra_module = THIS_MODULE,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100355 },
Herbert Xud0ed0db2016-11-22 20:08:35 +0800356 .min_keysize = 2 * AES_MIN_KEY_SIZE,
357 .max_keysize = 2 * AES_MAX_KEY_SIZE,
358 .ivsize = AES_BLOCK_SIZE,
359 .setkey = xts_set_key,
360 .encrypt = xts_encrypt,
361 .decrypt = xts_decrypt,
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100362} };
363
Ard Biesheuvel7f329c12016-11-29 13:05:30 +0000364static struct simd_skcipher_alg *aes_simd_algs[ARRAY_SIZE(aes_algs)];
Herbert Xud0ed0db2016-11-22 20:08:35 +0800365
366static void aes_exit(void)
367{
368 int i;
369
Ard Biesheuvel293614c2017-01-11 16:41:51 +0000370 for (i = 0; i < ARRAY_SIZE(aes_simd_algs); i++)
371 if (aes_simd_algs[i])
372 simd_skcipher_free(aes_simd_algs[i]);
Herbert Xud0ed0db2016-11-22 20:08:35 +0800373
374 crypto_unregister_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
375}
376
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100377static int __init aes_init(void)
378{
Herbert Xud0ed0db2016-11-22 20:08:35 +0800379 struct simd_skcipher_alg *simd;
380 const char *basename;
381 const char *algname;
382 const char *drvname;
383 int err;
384 int i;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100385
Herbert Xud0ed0db2016-11-22 20:08:35 +0800386 err = crypto_register_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
387 if (err)
388 return err;
389
390 for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
Ard Biesheuvel293614c2017-01-11 16:41:51 +0000391 if (!(aes_algs[i].base.cra_flags & CRYPTO_ALG_INTERNAL))
392 continue;
393
Herbert Xud0ed0db2016-11-22 20:08:35 +0800394 algname = aes_algs[i].base.cra_name + 2;
395 drvname = aes_algs[i].base.cra_driver_name + 2;
396 basename = aes_algs[i].base.cra_driver_name;
397 simd = simd_skcipher_create_compat(algname, drvname, basename);
398 err = PTR_ERR(simd);
399 if (IS_ERR(simd))
400 goto unregister_simds;
401
402 aes_simd_algs[i] = simd;
403 }
404
405 return 0;
406
407unregister_simds:
408 aes_exit();
409 return err;
Ard Biesheuvel49788fe2014-03-21 10:19:17 +0100410}
411
412#ifdef USE_V8_CRYPTO_EXTENSIONS
413module_cpu_feature_match(AES, aes_init);
414#else
415module_init(aes_init);
416#endif
417module_exit(aes_exit);