Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / arch / x86 / crypto / cast5_avx_glue.c
1 /*
2  * Glue Code for the AVX assembler implemention of the Cast5 Cipher
3  *
4  * Copyright (C) 2012 Johannes Goetzfried
5  *     <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
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 as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
20  * USA
21  *
22  */
23
24 #include <linux/module.h>
25 #include <linux/hardirq.h>
26 #include <linux/types.h>
27 #include <linux/crypto.h>
28 #include <linux/err.h>
29 #include <crypto/ablk_helper.h>
30 #include <crypto/algapi.h>
31 #include <crypto/cast5.h>
32 #include <crypto/cryptd.h>
33 #include <crypto/ctr.h>
34 #include <asm/xcr.h>
35 #include <asm/xsave.h>
36 #include <asm/crypto/glue_helper.h>
37
38 #define CAST5_PARALLEL_BLOCKS 16
39
40 asmlinkage void cast5_ecb_enc_16way(struct cast5_ctx *ctx, u8 *dst,
41                                     const u8 *src);
42 asmlinkage void cast5_ecb_dec_16way(struct cast5_ctx *ctx, u8 *dst,
43                                     const u8 *src);
44 asmlinkage void cast5_cbc_dec_16way(struct cast5_ctx *ctx, u8 *dst,
45                                     const u8 *src);
46 asmlinkage void cast5_ctr_16way(struct cast5_ctx *ctx, u8 *dst, const u8 *src,
47                                 __be64 *iv);
48
49 static inline bool cast5_fpu_begin(bool fpu_enabled, unsigned int nbytes)
50 {
51         return glue_fpu_begin(CAST5_BLOCK_SIZE, CAST5_PARALLEL_BLOCKS,
52                               NULL, fpu_enabled, nbytes);
53 }
54
55 static inline void cast5_fpu_end(bool fpu_enabled)
56 {
57         return glue_fpu_end(fpu_enabled);
58 }
59
60 static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
61                      bool enc)
62 {
63         bool fpu_enabled;
64         struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
65         const unsigned int bsize = CAST5_BLOCK_SIZE;
66         unsigned int nbytes;
67         void (*fn)(struct cast5_ctx *ctx, u8 *dst, const u8 *src);
68         int err;
69
70         fn = (enc) ? cast5_ecb_enc_16way : cast5_ecb_dec_16way;
71
72         err = blkcipher_walk_virt(desc, walk);
73         desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
74
75         while ((nbytes = walk->nbytes)) {
76                 u8 *wsrc = walk->src.virt.addr;
77                 u8 *wdst = walk->dst.virt.addr;
78
79                 fpu_enabled = cast5_fpu_begin(false, nbytes);
80
81                 /* Process multi-block batch */
82                 if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
83                         do {
84                                 fn(ctx, wdst, wsrc);
85
86                                 wsrc += bsize * CAST5_PARALLEL_BLOCKS;
87                                 wdst += bsize * CAST5_PARALLEL_BLOCKS;
88                                 nbytes -= bsize * CAST5_PARALLEL_BLOCKS;
89                         } while (nbytes >= bsize * CAST5_PARALLEL_BLOCKS);
90
91                         if (nbytes < bsize)
92                                 goto done;
93                 }
94
95                 fn = (enc) ? __cast5_encrypt : __cast5_decrypt;
96
97                 /* Handle leftovers */
98                 do {
99                         fn(ctx, wdst, wsrc);
100
101                         wsrc += bsize;
102                         wdst += bsize;
103                         nbytes -= bsize;
104                 } while (nbytes >= bsize);
105
106 done:
107                 cast5_fpu_end(fpu_enabled);
108                 err = blkcipher_walk_done(desc, walk, nbytes);
109         }
110         return err;
111 }
112
113 static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
114                        struct scatterlist *src, unsigned int nbytes)
115 {
116         struct blkcipher_walk walk;
117
118         blkcipher_walk_init(&walk, dst, src, nbytes);
119         return ecb_crypt(desc, &walk, true);
120 }
121
122 static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
123                        struct scatterlist *src, unsigned int nbytes)
124 {
125         struct blkcipher_walk walk;
126
127         blkcipher_walk_init(&walk, dst, src, nbytes);
128         return ecb_crypt(desc, &walk, false);
129 }
130
131 static unsigned int __cbc_encrypt(struct blkcipher_desc *desc,
132                                   struct blkcipher_walk *walk)
133 {
134         struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
135         const unsigned int bsize = CAST5_BLOCK_SIZE;
136         unsigned int nbytes = walk->nbytes;
137         u64 *src = (u64 *)walk->src.virt.addr;
138         u64 *dst = (u64 *)walk->dst.virt.addr;
139         u64 *iv = (u64 *)walk->iv;
140
141         do {
142                 *dst = *src ^ *iv;
143                 __cast5_encrypt(ctx, (u8 *)dst, (u8 *)dst);
144                 iv = dst;
145
146                 src += 1;
147                 dst += 1;
148                 nbytes -= bsize;
149         } while (nbytes >= bsize);
150
151         *(u64 *)walk->iv = *iv;
152         return nbytes;
153 }
154
155 static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
156                        struct scatterlist *src, unsigned int nbytes)
157 {
158         struct blkcipher_walk walk;
159         int err;
160
161         blkcipher_walk_init(&walk, dst, src, nbytes);
162         err = blkcipher_walk_virt(desc, &walk);
163
164         while ((nbytes = walk.nbytes)) {
165                 nbytes = __cbc_encrypt(desc, &walk);
166                 err = blkcipher_walk_done(desc, &walk, nbytes);
167         }
168
169         return err;
170 }
171
172 static unsigned int __cbc_decrypt(struct blkcipher_desc *desc,
173                                   struct blkcipher_walk *walk)
174 {
175         struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
176         const unsigned int bsize = CAST5_BLOCK_SIZE;
177         unsigned int nbytes = walk->nbytes;
178         u64 *src = (u64 *)walk->src.virt.addr;
179         u64 *dst = (u64 *)walk->dst.virt.addr;
180         u64 last_iv;
181
182         /* Start of the last block. */
183         src += nbytes / bsize - 1;
184         dst += nbytes / bsize - 1;
185
186         last_iv = *src;
187
188         /* Process multi-block batch */
189         if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
190                 do {
191                         nbytes -= bsize * (CAST5_PARALLEL_BLOCKS - 1);
192                         src -= CAST5_PARALLEL_BLOCKS - 1;
193                         dst -= CAST5_PARALLEL_BLOCKS - 1;
194
195                         cast5_cbc_dec_16way(ctx, (u8 *)dst, (u8 *)src);
196
197                         nbytes -= bsize;
198                         if (nbytes < bsize)
199                                 goto done;
200
201                         *dst ^= *(src - 1);
202                         src -= 1;
203                         dst -= 1;
204                 } while (nbytes >= bsize * CAST5_PARALLEL_BLOCKS);
205         }
206
207         /* Handle leftovers */
208         for (;;) {
209                 __cast5_decrypt(ctx, (u8 *)dst, (u8 *)src);
210
211                 nbytes -= bsize;
212                 if (nbytes < bsize)
213                         break;
214
215                 *dst ^= *(src - 1);
216                 src -= 1;
217                 dst -= 1;
218         }
219
220 done:
221         *dst ^= *(u64 *)walk->iv;
222         *(u64 *)walk->iv = last_iv;
223
224         return nbytes;
225 }
226
227 static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
228                        struct scatterlist *src, unsigned int nbytes)
229 {
230         bool fpu_enabled;
231         struct blkcipher_walk walk;
232         int err;
233
234         blkcipher_walk_init(&walk, dst, src, nbytes);
235         err = blkcipher_walk_virt(desc, &walk);
236         desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
237
238         while ((nbytes = walk.nbytes)) {
239                 fpu_enabled = cast5_fpu_begin(false, nbytes);
240                 nbytes = __cbc_decrypt(desc, &walk);
241                 cast5_fpu_end(fpu_enabled);
242                 err = blkcipher_walk_done(desc, &walk, nbytes);
243         }
244         return err;
245 }
246
247 static void ctr_crypt_final(struct blkcipher_desc *desc,
248                             struct blkcipher_walk *walk)
249 {
250         struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
251         u8 *ctrblk = walk->iv;
252         u8 keystream[CAST5_BLOCK_SIZE];
253         u8 *src = walk->src.virt.addr;
254         u8 *dst = walk->dst.virt.addr;
255         unsigned int nbytes = walk->nbytes;
256
257         __cast5_encrypt(ctx, keystream, ctrblk);
258         crypto_xor(keystream, src, nbytes);
259         memcpy(dst, keystream, nbytes);
260
261         crypto_inc(ctrblk, CAST5_BLOCK_SIZE);
262 }
263
264 static unsigned int __ctr_crypt(struct blkcipher_desc *desc,
265                                 struct blkcipher_walk *walk)
266 {
267         struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
268         const unsigned int bsize = CAST5_BLOCK_SIZE;
269         unsigned int nbytes = walk->nbytes;
270         u64 *src = (u64 *)walk->src.virt.addr;
271         u64 *dst = (u64 *)walk->dst.virt.addr;
272
273         /* Process multi-block batch */
274         if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
275                 do {
276                         cast5_ctr_16way(ctx, (u8 *)dst, (u8 *)src,
277                                         (__be64 *)walk->iv);
278
279                         src += CAST5_PARALLEL_BLOCKS;
280                         dst += CAST5_PARALLEL_BLOCKS;
281                         nbytes -= bsize * CAST5_PARALLEL_BLOCKS;
282                 } while (nbytes >= bsize * CAST5_PARALLEL_BLOCKS);
283
284                 if (nbytes < bsize)
285                         goto done;
286         }
287
288         /* Handle leftovers */
289         do {
290                 u64 ctrblk;
291
292                 if (dst != src)
293                         *dst = *src;
294
295                 ctrblk = *(u64 *)walk->iv;
296                 be64_add_cpu((__be64 *)walk->iv, 1);
297
298                 __cast5_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
299                 *dst ^= ctrblk;
300
301                 src += 1;
302                 dst += 1;
303                 nbytes -= bsize;
304         } while (nbytes >= bsize);
305
306 done:
307         return nbytes;
308 }
309
310 static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
311                      struct scatterlist *src, unsigned int nbytes)
312 {
313         bool fpu_enabled;
314         struct blkcipher_walk walk;
315         int err;
316
317         blkcipher_walk_init(&walk, dst, src, nbytes);
318         err = blkcipher_walk_virt_block(desc, &walk, CAST5_BLOCK_SIZE);
319         desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
320
321         while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
322                 fpu_enabled = cast5_fpu_begin(false, nbytes);
323                 nbytes = __ctr_crypt(desc, &walk);
324                 cast5_fpu_end(fpu_enabled);
325                 err = blkcipher_walk_done(desc, &walk, nbytes);
326         }
327
328         if (walk.nbytes) {
329                 ctr_crypt_final(desc, &walk);
330                 err = blkcipher_walk_done(desc, &walk, 0);
331         }
332
333         return err;
334 }
335
336
337 static struct crypto_alg cast5_algs[6] = { {
338         .cra_name               = "__ecb-cast5-avx",
339         .cra_driver_name        = "__driver-ecb-cast5-avx",
340         .cra_priority           = 0,
341         .cra_flags              = CRYPTO_ALG_TYPE_BLKCIPHER |
342                                   CRYPTO_ALG_INTERNAL,
343         .cra_blocksize          = CAST5_BLOCK_SIZE,
344         .cra_ctxsize            = sizeof(struct cast5_ctx),
345         .cra_alignmask          = 0,
346         .cra_type               = &crypto_blkcipher_type,
347         .cra_module             = THIS_MODULE,
348         .cra_u = {
349                 .blkcipher = {
350                         .min_keysize    = CAST5_MIN_KEY_SIZE,
351                         .max_keysize    = CAST5_MAX_KEY_SIZE,
352                         .setkey         = cast5_setkey,
353                         .encrypt        = ecb_encrypt,
354                         .decrypt        = ecb_decrypt,
355                 },
356         },
357 }, {
358         .cra_name               = "__cbc-cast5-avx",
359         .cra_driver_name        = "__driver-cbc-cast5-avx",
360         .cra_priority           = 0,
361         .cra_flags              = CRYPTO_ALG_TYPE_BLKCIPHER |
362                                   CRYPTO_ALG_INTERNAL,
363         .cra_blocksize          = CAST5_BLOCK_SIZE,
364         .cra_ctxsize            = sizeof(struct cast5_ctx),
365         .cra_alignmask          = 0,
366         .cra_type               = &crypto_blkcipher_type,
367         .cra_module             = THIS_MODULE,
368         .cra_u = {
369                 .blkcipher = {
370                         .min_keysize    = CAST5_MIN_KEY_SIZE,
371                         .max_keysize    = CAST5_MAX_KEY_SIZE,
372                         .setkey         = cast5_setkey,
373                         .encrypt        = cbc_encrypt,
374                         .decrypt        = cbc_decrypt,
375                 },
376         },
377 }, {
378         .cra_name               = "__ctr-cast5-avx",
379         .cra_driver_name        = "__driver-ctr-cast5-avx",
380         .cra_priority           = 0,
381         .cra_flags              = CRYPTO_ALG_TYPE_BLKCIPHER |
382                                   CRYPTO_ALG_INTERNAL,
383         .cra_blocksize          = 1,
384         .cra_ctxsize            = sizeof(struct cast5_ctx),
385         .cra_alignmask          = 0,
386         .cra_type               = &crypto_blkcipher_type,
387         .cra_module             = THIS_MODULE,
388         .cra_u = {
389                 .blkcipher = {
390                         .min_keysize    = CAST5_MIN_KEY_SIZE,
391                         .max_keysize    = CAST5_MAX_KEY_SIZE,
392                         .ivsize         = CAST5_BLOCK_SIZE,
393                         .setkey         = cast5_setkey,
394                         .encrypt        = ctr_crypt,
395                         .decrypt        = ctr_crypt,
396                 },
397         },
398 }, {
399         .cra_name               = "ecb(cast5)",
400         .cra_driver_name        = "ecb-cast5-avx",
401         .cra_priority           = 200,
402         .cra_flags              = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
403         .cra_blocksize          = CAST5_BLOCK_SIZE,
404         .cra_ctxsize            = sizeof(struct async_helper_ctx),
405         .cra_alignmask          = 0,
406         .cra_type               = &crypto_ablkcipher_type,
407         .cra_module             = THIS_MODULE,
408         .cra_init               = ablk_init,
409         .cra_exit               = ablk_exit,
410         .cra_u = {
411                 .ablkcipher = {
412                         .min_keysize    = CAST5_MIN_KEY_SIZE,
413                         .max_keysize    = CAST5_MAX_KEY_SIZE,
414                         .setkey         = ablk_set_key,
415                         .encrypt        = ablk_encrypt,
416                         .decrypt        = ablk_decrypt,
417                 },
418         },
419 }, {
420         .cra_name               = "cbc(cast5)",
421         .cra_driver_name        = "cbc-cast5-avx",
422         .cra_priority           = 200,
423         .cra_flags              = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
424         .cra_blocksize          = CAST5_BLOCK_SIZE,
425         .cra_ctxsize            = sizeof(struct async_helper_ctx),
426         .cra_alignmask          = 0,
427         .cra_type               = &crypto_ablkcipher_type,
428         .cra_module             = THIS_MODULE,
429         .cra_init               = ablk_init,
430         .cra_exit               = ablk_exit,
431         .cra_u = {
432                 .ablkcipher = {
433                         .min_keysize    = CAST5_MIN_KEY_SIZE,
434                         .max_keysize    = CAST5_MAX_KEY_SIZE,
435                         .ivsize         = CAST5_BLOCK_SIZE,
436                         .setkey         = ablk_set_key,
437                         .encrypt        = __ablk_encrypt,
438                         .decrypt        = ablk_decrypt,
439                 },
440         },
441 }, {
442         .cra_name               = "ctr(cast5)",
443         .cra_driver_name        = "ctr-cast5-avx",
444         .cra_priority           = 200,
445         .cra_flags              = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
446         .cra_blocksize          = 1,
447         .cra_ctxsize            = sizeof(struct async_helper_ctx),
448         .cra_alignmask          = 0,
449         .cra_type               = &crypto_ablkcipher_type,
450         .cra_module             = THIS_MODULE,
451         .cra_init               = ablk_init,
452         .cra_exit               = ablk_exit,
453         .cra_u = {
454                 .ablkcipher = {
455                         .min_keysize    = CAST5_MIN_KEY_SIZE,
456                         .max_keysize    = CAST5_MAX_KEY_SIZE,
457                         .ivsize         = CAST5_BLOCK_SIZE,
458                         .setkey         = ablk_set_key,
459                         .encrypt        = ablk_encrypt,
460                         .decrypt        = ablk_encrypt,
461                         .geniv          = "chainiv",
462                 },
463         },
464 } };
465
466 static int __init cast5_init(void)
467 {
468         u64 xcr0;
469
470         if (!cpu_has_avx || !cpu_has_osxsave) {
471                 pr_info("AVX instructions are not detected.\n");
472                 return -ENODEV;
473         }
474
475         xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
476         if ((xcr0 & (XSTATE_SSE | XSTATE_YMM)) != (XSTATE_SSE | XSTATE_YMM)) {
477                 pr_info("AVX detected but unusable.\n");
478                 return -ENODEV;
479         }
480
481         return crypto_register_algs(cast5_algs, ARRAY_SIZE(cast5_algs));
482 }
483
484 static void __exit cast5_exit(void)
485 {
486         crypto_unregister_algs(cast5_algs, ARRAY_SIZE(cast5_algs));
487 }
488
489 module_init(cast5_init);
490 module_exit(cast5_exit);
491
492 MODULE_DESCRIPTION("Cast5 Cipher Algorithm, AVX optimized");
493 MODULE_LICENSE("GPL");
494 MODULE_ALIAS_CRYPTO("cast5");