1 /*
2 * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions
3 *
4 * Copyright (C) 2013 - 2014 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/cpufeature.h>
13 #include <linux/crypto.h>
14 #include <linux/module.h>
15
16 #include "aes-ce-setkey.h"
17
18 MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions");
19 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
20 MODULE_LICENSE("GPL v2");
21
22 extern void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]);
23 extern void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]);
24
25 #ifdef CONFIG_CFI_CLANG
__cfi_aes_cipher_encrypt(struct crypto_tfm * tfm,u8 dst[],u8 const src[])26 static inline void __cfi_aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
27 {
28 aes_cipher_encrypt(tfm, dst, src);
29 }
30
__cfi_aes_cipher_decrypt(struct crypto_tfm * tfm,u8 dst[],u8 const src[])31 static inline void __cfi_aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
32 {
33 aes_cipher_decrypt(tfm, dst, src);
34 }
35
36 #define aes_cipher_encrypt __cfi_aes_cipher_encrypt
37 #define aes_cipher_decrypt __cfi_aes_cipher_decrypt
38 #endif
39
ce_aes_setkey(struct crypto_tfm * tfm,const u8 * in_key,unsigned int key_len)40 int ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
41 unsigned int key_len)
42 {
43 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
44 int ret;
45
46 ret = ce_aes_expandkey(ctx, in_key, key_len);
47 if (!ret)
48 return 0;
49
50 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
51 return -EINVAL;
52 }
53 EXPORT_SYMBOL(ce_aes_setkey);
54
55 static struct crypto_alg aes_alg = {
56 .cra_name = "aes",
57 .cra_driver_name = "aes-ce",
58 .cra_priority = 250,
59 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
60 .cra_blocksize = AES_BLOCK_SIZE,
61 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
62 .cra_module = THIS_MODULE,
63 .cra_cipher = {
64 .cia_min_keysize = AES_MIN_KEY_SIZE,
65 .cia_max_keysize = AES_MAX_KEY_SIZE,
66 .cia_setkey = ce_aes_setkey,
67 .cia_encrypt = aes_cipher_encrypt,
68 .cia_decrypt = aes_cipher_decrypt
69 }
70 };
71
aes_mod_init(void)72 static int __init aes_mod_init(void)
73 {
74 return crypto_register_alg(&aes_alg);
75 }
76
aes_mod_exit(void)77 static void __exit aes_mod_exit(void)
78 {
79 crypto_unregister_alg(&aes_alg);
80 }
81
82 module_cpu_feature_match(AES, aes_mod_init);
83 module_exit(aes_mod_exit);
84