1 /*
2  * Scalar AES core transform
3  *
4  * Copyright (C) 2017 Linaro Ltd.
5  * Author: Ard Biesheuvel <ard.biesheuvel@linaro.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 
12 #include <crypto/aes.h>
13 #include <linux/crypto.h>
14 #include <linux/module.h>
15 
16 asmlinkage void __aes_arm_encrypt(u32 *rk, int rounds, const u8 *in, u8 *out);
17 EXPORT_SYMBOL(__aes_arm_encrypt);
18 
19 asmlinkage void __aes_arm_decrypt(u32 *rk, int rounds, const u8 *in, u8 *out);
20 EXPORT_SYMBOL(__aes_arm_decrypt);
21 
22 static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
23 {
24 	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
25 	int rounds = 6 + ctx->key_length / 4;
26 
27 	__aes_arm_encrypt(ctx->key_enc, rounds, in, out);
28 }
29 
30 static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
31 {
32 	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
33 	int rounds = 6 + ctx->key_length / 4;
34 
35 	__aes_arm_decrypt(ctx->key_dec, rounds, in, out);
36 }
37 
38 static struct crypto_alg aes_alg = {
39 	.cra_name			= "aes",
40 	.cra_driver_name		= "aes-arm",
41 	.cra_priority			= 200,
42 	.cra_flags			= CRYPTO_ALG_TYPE_CIPHER,
43 	.cra_blocksize			= AES_BLOCK_SIZE,
44 	.cra_ctxsize			= sizeof(struct crypto_aes_ctx),
45 	.cra_module			= THIS_MODULE,
46 
47 	.cra_cipher.cia_min_keysize	= AES_MIN_KEY_SIZE,
48 	.cra_cipher.cia_max_keysize	= AES_MAX_KEY_SIZE,
49 	.cra_cipher.cia_setkey		= crypto_aes_set_key,
50 	.cra_cipher.cia_encrypt		= aes_encrypt,
51 	.cra_cipher.cia_decrypt		= aes_decrypt,
52 
53 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
54 	.cra_alignmask			= 3,
55 #endif
56 };
57 
58 static int __init aes_init(void)
59 {
60 	return crypto_register_alg(&aes_alg);
61 }
62 
63 static void __exit aes_fini(void)
64 {
65 	crypto_unregister_alg(&aes_alg);
66 }
67 
68 module_init(aes_init);
69 module_exit(aes_fini);
70 
71 MODULE_DESCRIPTION("Scalar AES cipher for ARM");
72 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
73 MODULE_LICENSE("GPL v2");
74 MODULE_ALIAS_CRYPTO("aes");
75