1 /*
2 * Copyright (c) 2015 South Silicon Valley Microelectronics Inc.
3 * Copyright (c) 2015 iComm Corporation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include <linux/module.h>
18 #include <linux/crypto.h>
19 #include <crypto/aes.h>
20 #define AES_MAXNR 14
21 typedef struct {
22 unsigned int rd_key[4 *(AES_MAXNR + 1)];
23 int rounds;
24 } AES_KEY;
25 struct AES_CTX {
26 AES_KEY enc_key;
27 AES_KEY dec_key;
28 };
29 asmlinkage void AES_encrypt(const u8 *in, u8 *out, AES_KEY *ctx);
30 asmlinkage void AES_decrypt(const u8 *in, u8 *out, AES_KEY *ctx);
31 asmlinkage int private_AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key);
32 asmlinkage int private_AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key);
aes_encrypt(struct crypto_tfm * tfm,u8 * dst,const u8 * src)33 static void aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
34 {
35 struct AES_CTX *ctx = crypto_tfm_ctx(tfm);
36 AES_encrypt(src, dst, &ctx->enc_key);
37 }
aes_decrypt(struct crypto_tfm * tfm,u8 * dst,const u8 * src)38 static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
39 {
40 struct AES_CTX *ctx = crypto_tfm_ctx(tfm);
41 AES_decrypt(src, dst, &ctx->dec_key);
42 }
aes_set_key(struct crypto_tfm * tfm,const u8 * in_key,unsigned int key_len)43 static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
44 unsigned int key_len)
45 {
46 struct AES_CTX *ctx = crypto_tfm_ctx(tfm);
47 switch (key_len) {
48 case AES_KEYSIZE_128:
49 key_len = 128;
50 break;
51 case AES_KEYSIZE_192:
52 key_len = 192;
53 break;
54 case AES_KEYSIZE_256:
55 key_len = 256;
56 break;
57 default:
58 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
59 return -EINVAL;
60 }
61 if (private_AES_set_encrypt_key(in_key, key_len, &ctx->enc_key) == -1) {
62 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
63 return -EINVAL;
64 }
65 ctx->dec_key = ctx->enc_key;
66 if (private_AES_set_decrypt_key(in_key, key_len, &ctx->dec_key) == -1) {
67 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
68 return -EINVAL;
69 }
70 return 0;
71 }
72 static struct crypto_alg aes_alg = {
73 .cra_name = "aes",
74 .cra_driver_name = "aes-asm",
75 .cra_priority = 200,
76 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
77 .cra_blocksize = AES_BLOCK_SIZE,
78 .cra_ctxsize = sizeof(struct AES_CTX),
79 .cra_module = THIS_MODULE,
80 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
81 .cra_u = {
82 .cipher = {
83 .cia_min_keysize = AES_MIN_KEY_SIZE,
84 .cia_max_keysize = AES_MAX_KEY_SIZE,
85 .cia_setkey = aes_set_key,
86 .cia_encrypt = aes_encrypt,
87 .cia_decrypt = aes_decrypt
88 }
89 }
90 };
aes_init(void)91 int aes_init(void)
92 {
93 return crypto_register_alg(&aes_alg);
94 }
aes_fini(void)95 void aes_fini(void)
96 {
97 crypto_unregister_alg(&aes_alg);
98 }
99