1 /*
2  * Scalar AES core transform
3  *
4  * Copyright (C) 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 <crypto/aes.h>
12 #include <linux/crypto.h>
13 #include <linux/module.h>
14 
15 asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
16 EXPORT_SYMBOL(__aes_arm64_encrypt);
17 
18 asmlinkage void __aes_arm64_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
19 EXPORT_SYMBOL(__aes_arm64_decrypt);
20 
21 static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
22 {
23 	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
24 	int rounds = 6 + ctx->key_length / 4;
25 
26 	__aes_arm64_encrypt(ctx->key_enc, out, in, rounds);
27 }
28 
29 static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
30 {
31 	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
32 	int rounds = 6 + ctx->key_length / 4;
33 
34 	__aes_arm64_decrypt(ctx->key_dec, out, in, rounds);
35 }
36 
37 static struct crypto_alg aes_alg = {
38 	.cra_name			= "aes",
39 	.cra_driver_name		= "aes-arm64",
40 	.cra_priority			= 200,
41 	.cra_flags			= CRYPTO_ALG_TYPE_CIPHER,
42 	.cra_blocksize			= AES_BLOCK_SIZE,
43 	.cra_ctxsize			= sizeof(struct crypto_aes_ctx),
44 	.cra_module			= THIS_MODULE,
45 
46 	.cra_cipher.cia_min_keysize	= AES_MIN_KEY_SIZE,
47 	.cra_cipher.cia_max_keysize	= AES_MAX_KEY_SIZE,
48 	.cra_cipher.cia_setkey		= crypto_aes_set_key,
49 	.cra_cipher.cia_encrypt		= aes_encrypt,
50 	.cra_cipher.cia_decrypt		= aes_decrypt
51 };
52 
53 static int __init aes_init(void)
54 {
55 	return crypto_register_alg(&aes_alg);
56 }
57 
58 static void __exit aes_fini(void)
59 {
60 	crypto_unregister_alg(&aes_alg);
61 }
62 
63 module_init(aes_init);
64 module_exit(aes_fini);
65 
66 MODULE_DESCRIPTION("Scalar AES cipher for arm64");
67 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
68 MODULE_LICENSE("GPL v2");
69 MODULE_ALIAS_CRYPTO("aes");
70