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