a0a0e5e3a8b57935d4e243a408f7411eef69491c
[platform/kernel/linux-rpi.git] / arch / arm64 / crypto / aes-ce-cipher.c
1 /*
2  * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions
3  *
4  * Copyright (C) 2013 - 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 <asm/neon.h>
12 #include <asm/unaligned.h>
13 #include <crypto/aes.h>
14 #include <linux/cpufeature.h>
15 #include <linux/crypto.h>
16 #include <linux/module.h>
17
18 #include "aes-ce-setkey.h"
19
20 MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions");
21 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
22 MODULE_LICENSE("GPL v2");
23
24 struct aes_block {
25         u8 b[AES_BLOCK_SIZE];
26 };
27
28 static int num_rounds(struct crypto_aes_ctx *ctx)
29 {
30         /*
31          * # of rounds specified by AES:
32          * 128 bit key          10 rounds
33          * 192 bit key          12 rounds
34          * 256 bit key          14 rounds
35          * => n byte key        => 6 + (n/4) rounds
36          */
37         return 6 + ctx->key_length / 4;
38 }
39
40 static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
41 {
42         struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
43         struct aes_block *out = (struct aes_block *)dst;
44         struct aes_block const *in = (struct aes_block *)src;
45         void *dummy0;
46         int dummy1;
47
48         kernel_neon_begin_partial(4);
49
50         __asm__("       ld1     {v0.16b}, %[in]                 ;"
51                 "       ld1     {v1.4s}, [%[key]], #16          ;"
52                 "       cmp     %w[rounds], #10                 ;"
53                 "       bmi     0f                              ;"
54                 "       bne     3f                              ;"
55                 "       mov     v3.16b, v1.16b                  ;"
56                 "       b       2f                              ;"
57                 "0:     mov     v2.16b, v1.16b                  ;"
58                 "       ld1     {v3.4s}, [%[key]], #16          ;"
59                 "1:     aese    v0.16b, v2.16b                  ;"
60                 "       aesmc   v0.16b, v0.16b                  ;"
61                 "2:     ld1     {v1.4s}, [%[key]], #16          ;"
62                 "       aese    v0.16b, v3.16b                  ;"
63                 "       aesmc   v0.16b, v0.16b                  ;"
64                 "3:     ld1     {v2.4s}, [%[key]], #16          ;"
65                 "       subs    %w[rounds], %w[rounds], #3      ;"
66                 "       aese    v0.16b, v1.16b                  ;"
67                 "       aesmc   v0.16b, v0.16b                  ;"
68                 "       ld1     {v3.4s}, [%[key]], #16          ;"
69                 "       bpl     1b                              ;"
70                 "       aese    v0.16b, v2.16b                  ;"
71                 "       eor     v0.16b, v0.16b, v3.16b          ;"
72                 "       st1     {v0.16b}, %[out]                ;"
73
74         :       [out]           "=Q"(*out),
75                 [key]           "=r"(dummy0),
76                 [rounds]        "=r"(dummy1)
77         :       [in]            "Q"(*in),
78                                 "1"(ctx->key_enc),
79                                 "2"(num_rounds(ctx) - 2)
80         :       "cc");
81
82         kernel_neon_end();
83 }
84
85 static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
86 {
87         struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
88         struct aes_block *out = (struct aes_block *)dst;
89         struct aes_block const *in = (struct aes_block *)src;
90         void *dummy0;
91         int dummy1;
92
93         kernel_neon_begin_partial(4);
94
95         __asm__("       ld1     {v0.16b}, %[in]                 ;"
96                 "       ld1     {v1.4s}, [%[key]], #16          ;"
97                 "       cmp     %w[rounds], #10                 ;"
98                 "       bmi     0f                              ;"
99                 "       bne     3f                              ;"
100                 "       mov     v3.16b, v1.16b                  ;"
101                 "       b       2f                              ;"
102                 "0:     mov     v2.16b, v1.16b                  ;"
103                 "       ld1     {v3.4s}, [%[key]], #16          ;"
104                 "1:     aesd    v0.16b, v2.16b                  ;"
105                 "       aesimc  v0.16b, v0.16b                  ;"
106                 "2:     ld1     {v1.4s}, [%[key]], #16          ;"
107                 "       aesd    v0.16b, v3.16b                  ;"
108                 "       aesimc  v0.16b, v0.16b                  ;"
109                 "3:     ld1     {v2.4s}, [%[key]], #16          ;"
110                 "       subs    %w[rounds], %w[rounds], #3      ;"
111                 "       aesd    v0.16b, v1.16b                  ;"
112                 "       aesimc  v0.16b, v0.16b                  ;"
113                 "       ld1     {v3.4s}, [%[key]], #16          ;"
114                 "       bpl     1b                              ;"
115                 "       aesd    v0.16b, v2.16b                  ;"
116                 "       eor     v0.16b, v0.16b, v3.16b          ;"
117                 "       st1     {v0.16b}, %[out]                ;"
118
119         :       [out]           "=Q"(*out),
120                 [key]           "=r"(dummy0),
121                 [rounds]        "=r"(dummy1)
122         :       [in]            "Q"(*in),
123                                 "1"(ctx->key_dec),
124                                 "2"(num_rounds(ctx) - 2)
125         :       "cc");
126
127         kernel_neon_end();
128 }
129
130 /*
131  * aes_sub() - use the aese instruction to perform the AES sbox substitution
132  *             on each byte in 'input'
133  */
134 static u32 aes_sub(u32 input)
135 {
136         u32 ret;
137
138         __asm__("dup    v1.4s, %w[in]           ;"
139                 "movi   v0.16b, #0              ;"
140                 "aese   v0.16b, v1.16b          ;"
141                 "umov   %w[out], v0.4s[0]       ;"
142
143         :       [out]   "=r"(ret)
144         :       [in]    "r"(input)
145         :               "v0","v1");
146
147         return ret;
148 }
149
150 int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
151                      unsigned int key_len)
152 {
153         /*
154          * The AES key schedule round constants
155          */
156         static u8 const rcon[] = {
157                 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
158         };
159
160         u32 kwords = key_len / sizeof(u32);
161         struct aes_block *key_enc, *key_dec;
162         int i, j;
163
164         if (key_len != AES_KEYSIZE_128 &&
165             key_len != AES_KEYSIZE_192 &&
166             key_len != AES_KEYSIZE_256)
167                 return -EINVAL;
168
169         ctx->key_length = key_len;
170         for (i = 0; i < kwords; i++)
171                 ctx->key_enc[i] = get_unaligned_le32(in_key + i * sizeof(u32));
172
173         kernel_neon_begin_partial(2);
174         for (i = 0; i < sizeof(rcon); i++) {
175                 u32 *rki = ctx->key_enc + (i * kwords);
176                 u32 *rko = rki + kwords;
177
178                 rko[0] = ror32(aes_sub(rki[kwords - 1]), 8) ^ rcon[i] ^ rki[0];
179                 rko[1] = rko[0] ^ rki[1];
180                 rko[2] = rko[1] ^ rki[2];
181                 rko[3] = rko[2] ^ rki[3];
182
183                 if (key_len == AES_KEYSIZE_192) {
184                         if (i >= 7)
185                                 break;
186                         rko[4] = rko[3] ^ rki[4];
187                         rko[5] = rko[4] ^ rki[5];
188                 } else if (key_len == AES_KEYSIZE_256) {
189                         if (i >= 6)
190                                 break;
191                         rko[4] = aes_sub(rko[3]) ^ rki[4];
192                         rko[5] = rko[4] ^ rki[5];
193                         rko[6] = rko[5] ^ rki[6];
194                         rko[7] = rko[6] ^ rki[7];
195                 }
196         }
197
198         /*
199          * Generate the decryption keys for the Equivalent Inverse Cipher.
200          * This involves reversing the order of the round keys, and applying
201          * the Inverse Mix Columns transformation on all but the first and
202          * the last one.
203          */
204         key_enc = (struct aes_block *)ctx->key_enc;
205         key_dec = (struct aes_block *)ctx->key_dec;
206         j = num_rounds(ctx);
207
208         key_dec[0] = key_enc[j];
209         for (i = 1, j--; j > 0; i++, j--)
210                 __asm__("ld1    {v0.4s}, %[in]          ;"
211                         "aesimc v1.16b, v0.16b          ;"
212                         "st1    {v1.4s}, %[out] ;"
213
214                 :       [out]   "=Q"(key_dec[i])
215                 :       [in]    "Q"(key_enc[j])
216                 :               "v0","v1");
217         key_dec[i] = key_enc[0];
218
219         kernel_neon_end();
220         return 0;
221 }
222 EXPORT_SYMBOL(ce_aes_expandkey);
223
224 int ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
225                   unsigned int key_len)
226 {
227         struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
228         int ret;
229
230         ret = ce_aes_expandkey(ctx, in_key, key_len);
231         if (!ret)
232                 return 0;
233
234         tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
235         return -EINVAL;
236 }
237 EXPORT_SYMBOL(ce_aes_setkey);
238
239 static struct crypto_alg aes_alg = {
240         .cra_name               = "aes",
241         .cra_driver_name        = "aes-ce",
242         .cra_priority           = 250,
243         .cra_flags              = CRYPTO_ALG_TYPE_CIPHER,
244         .cra_blocksize          = AES_BLOCK_SIZE,
245         .cra_ctxsize            = sizeof(struct crypto_aes_ctx),
246         .cra_module             = THIS_MODULE,
247         .cra_cipher = {
248                 .cia_min_keysize        = AES_MIN_KEY_SIZE,
249                 .cia_max_keysize        = AES_MAX_KEY_SIZE,
250                 .cia_setkey             = ce_aes_setkey,
251                 .cia_encrypt            = aes_cipher_encrypt,
252                 .cia_decrypt            = aes_cipher_decrypt
253         }
254 };
255
256 static int __init aes_mod_init(void)
257 {
258         return crypto_register_alg(&aes_alg);
259 }
260
261 static void __exit aes_mod_exit(void)
262 {
263         crypto_unregister_alg(&aes_alg);
264 }
265
266 module_cpu_feature_match(AES, aes_mod_init);
267 module_exit(aes_mod_exit);