crypto: s390 - Fix aes-cbc IV corruption
[pandora-kernel.git] / arch / s390 / crypto / aes_s390.c
1 /*
2  * Cryptographic API.
3  *
4  * s390 implementation of the AES Cipher Algorithm.
5  *
6  * s390 Version:
7  *   Copyright IBM Corp. 2005,2007
8  *   Author(s): Jan Glauber (jang@de.ibm.com)
9  *              Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback
10  *
11  * Derived from "crypto/aes_generic.c"
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 2 of the License, or (at your option)
16  * any later version.
17  *
18  */
19
20 #define KMSG_COMPONENT "aes_s390"
21 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22
23 #include <crypto/aes.h>
24 #include <crypto/algapi.h>
25 #include <linux/err.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include "crypt_s390.h"
29
30 #define AES_KEYLEN_128          1
31 #define AES_KEYLEN_192          2
32 #define AES_KEYLEN_256          4
33
34 static u8 *ctrblk;
35 static char keylen_flag;
36
37 struct s390_aes_ctx {
38         u8 key[AES_MAX_KEY_SIZE];
39         long enc;
40         long dec;
41         int key_len;
42         union {
43                 struct crypto_blkcipher *blk;
44                 struct crypto_cipher *cip;
45         } fallback;
46 };
47
48 struct pcc_param {
49         u8 key[32];
50         u8 tweak[16];
51         u8 block[16];
52         u8 bit[16];
53         u8 xts[16];
54 };
55
56 struct s390_xts_ctx {
57         u8 key[32];
58         u8 xts_param[16];
59         struct pcc_param pcc;
60         long enc;
61         long dec;
62         int key_len;
63         struct crypto_blkcipher *fallback;
64 };
65
66 /*
67  * Check if the key_len is supported by the HW.
68  * Returns 0 if it is, a positive number if it is not and software fallback is
69  * required or a negative number in case the key size is not valid
70  */
71 static int need_fallback(unsigned int key_len)
72 {
73         switch (key_len) {
74         case 16:
75                 if (!(keylen_flag & AES_KEYLEN_128))
76                         return 1;
77                 break;
78         case 24:
79                 if (!(keylen_flag & AES_KEYLEN_192))
80                         return 1;
81                 break;
82         case 32:
83                 if (!(keylen_flag & AES_KEYLEN_256))
84                         return 1;
85                 break;
86         default:
87                 return -1;
88                 break;
89         }
90         return 0;
91 }
92
93 static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
94                 unsigned int key_len)
95 {
96         struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
97         int ret;
98
99         sctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
100         sctx->fallback.cip->base.crt_flags |= (tfm->crt_flags &
101                         CRYPTO_TFM_REQ_MASK);
102
103         ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
104         if (ret) {
105                 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
106                 tfm->crt_flags |= (sctx->fallback.cip->base.crt_flags &
107                                 CRYPTO_TFM_RES_MASK);
108         }
109         return ret;
110 }
111
112 static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
113                        unsigned int key_len)
114 {
115         struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
116         u32 *flags = &tfm->crt_flags;
117         int ret;
118
119         ret = need_fallback(key_len);
120         if (ret < 0) {
121                 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
122                 return -EINVAL;
123         }
124
125         sctx->key_len = key_len;
126         if (!ret) {
127                 memcpy(sctx->key, in_key, key_len);
128                 return 0;
129         }
130
131         return setkey_fallback_cip(tfm, in_key, key_len);
132 }
133
134 static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
135 {
136         const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
137
138         if (unlikely(need_fallback(sctx->key_len))) {
139                 crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
140                 return;
141         }
142
143         switch (sctx->key_len) {
144         case 16:
145                 crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in,
146                               AES_BLOCK_SIZE);
147                 break;
148         case 24:
149                 crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in,
150                               AES_BLOCK_SIZE);
151                 break;
152         case 32:
153                 crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in,
154                               AES_BLOCK_SIZE);
155                 break;
156         }
157 }
158
159 static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
160 {
161         const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
162
163         if (unlikely(need_fallback(sctx->key_len))) {
164                 crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
165                 return;
166         }
167
168         switch (sctx->key_len) {
169         case 16:
170                 crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in,
171                               AES_BLOCK_SIZE);
172                 break;
173         case 24:
174                 crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in,
175                               AES_BLOCK_SIZE);
176                 break;
177         case 32:
178                 crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in,
179                               AES_BLOCK_SIZE);
180                 break;
181         }
182 }
183
184 static int fallback_init_cip(struct crypto_tfm *tfm)
185 {
186         const char *name = tfm->__crt_alg->cra_name;
187         struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
188
189         sctx->fallback.cip = crypto_alloc_cipher(name, 0,
190                         CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
191
192         if (IS_ERR(sctx->fallback.cip)) {
193                 pr_err("Allocating AES fallback algorithm %s failed\n",
194                        name);
195                 return PTR_ERR(sctx->fallback.cip);
196         }
197
198         return 0;
199 }
200
201 static void fallback_exit_cip(struct crypto_tfm *tfm)
202 {
203         struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
204
205         crypto_free_cipher(sctx->fallback.cip);
206         sctx->fallback.cip = NULL;
207 }
208
209 static struct crypto_alg aes_alg = {
210         .cra_name               =       "aes",
211         .cra_driver_name        =       "aes-s390",
212         .cra_priority           =       CRYPT_S390_PRIORITY,
213         .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER |
214                                         CRYPTO_ALG_NEED_FALLBACK,
215         .cra_blocksize          =       AES_BLOCK_SIZE,
216         .cra_ctxsize            =       sizeof(struct s390_aes_ctx),
217         .cra_module             =       THIS_MODULE,
218         .cra_list               =       LIST_HEAD_INIT(aes_alg.cra_list),
219         .cra_init               =       fallback_init_cip,
220         .cra_exit               =       fallback_exit_cip,
221         .cra_u                  =       {
222                 .cipher = {
223                         .cia_min_keysize        =       AES_MIN_KEY_SIZE,
224                         .cia_max_keysize        =       AES_MAX_KEY_SIZE,
225                         .cia_setkey             =       aes_set_key,
226                         .cia_encrypt            =       aes_encrypt,
227                         .cia_decrypt            =       aes_decrypt,
228                 }
229         }
230 };
231
232 static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
233                 unsigned int len)
234 {
235         struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
236         unsigned int ret;
237
238         sctx->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
239         sctx->fallback.blk->base.crt_flags |= (tfm->crt_flags &
240                         CRYPTO_TFM_REQ_MASK);
241
242         ret = crypto_blkcipher_setkey(sctx->fallback.blk, key, len);
243         if (ret) {
244                 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
245                 tfm->crt_flags |= (sctx->fallback.blk->base.crt_flags &
246                                 CRYPTO_TFM_RES_MASK);
247         }
248         return ret;
249 }
250
251 static int fallback_blk_dec(struct blkcipher_desc *desc,
252                 struct scatterlist *dst, struct scatterlist *src,
253                 unsigned int nbytes)
254 {
255         unsigned int ret;
256         struct crypto_blkcipher *tfm;
257         struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
258
259         tfm = desc->tfm;
260         desc->tfm = sctx->fallback.blk;
261
262         ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
263
264         desc->tfm = tfm;
265         return ret;
266 }
267
268 static int fallback_blk_enc(struct blkcipher_desc *desc,
269                 struct scatterlist *dst, struct scatterlist *src,
270                 unsigned int nbytes)
271 {
272         unsigned int ret;
273         struct crypto_blkcipher *tfm;
274         struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
275
276         tfm = desc->tfm;
277         desc->tfm = sctx->fallback.blk;
278
279         ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
280
281         desc->tfm = tfm;
282         return ret;
283 }
284
285 static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
286                            unsigned int key_len)
287 {
288         struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
289         int ret;
290
291         ret = need_fallback(key_len);
292         if (ret > 0) {
293                 sctx->key_len = key_len;
294                 return setkey_fallback_blk(tfm, in_key, key_len);
295         }
296
297         switch (key_len) {
298         case 16:
299                 sctx->enc = KM_AES_128_ENCRYPT;
300                 sctx->dec = KM_AES_128_DECRYPT;
301                 break;
302         case 24:
303                 sctx->enc = KM_AES_192_ENCRYPT;
304                 sctx->dec = KM_AES_192_DECRYPT;
305                 break;
306         case 32:
307                 sctx->enc = KM_AES_256_ENCRYPT;
308                 sctx->dec = KM_AES_256_DECRYPT;
309                 break;
310         }
311
312         return aes_set_key(tfm, in_key, key_len);
313 }
314
315 static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
316                          struct blkcipher_walk *walk)
317 {
318         int ret = blkcipher_walk_virt(desc, walk);
319         unsigned int nbytes;
320
321         while ((nbytes = walk->nbytes)) {
322                 /* only use complete blocks */
323                 unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
324                 u8 *out = walk->dst.virt.addr;
325                 u8 *in = walk->src.virt.addr;
326
327                 ret = crypt_s390_km(func, param, out, in, n);
328                 BUG_ON((ret < 0) || (ret != n));
329
330                 nbytes &= AES_BLOCK_SIZE - 1;
331                 ret = blkcipher_walk_done(desc, walk, nbytes);
332         }
333
334         return ret;
335 }
336
337 static int ecb_aes_encrypt(struct blkcipher_desc *desc,
338                            struct scatterlist *dst, struct scatterlist *src,
339                            unsigned int nbytes)
340 {
341         struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
342         struct blkcipher_walk walk;
343
344         if (unlikely(need_fallback(sctx->key_len)))
345                 return fallback_blk_enc(desc, dst, src, nbytes);
346
347         blkcipher_walk_init(&walk, dst, src, nbytes);
348         return ecb_aes_crypt(desc, sctx->enc, sctx->key, &walk);
349 }
350
351 static int ecb_aes_decrypt(struct blkcipher_desc *desc,
352                            struct scatterlist *dst, struct scatterlist *src,
353                            unsigned int nbytes)
354 {
355         struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
356         struct blkcipher_walk walk;
357
358         if (unlikely(need_fallback(sctx->key_len)))
359                 return fallback_blk_dec(desc, dst, src, nbytes);
360
361         blkcipher_walk_init(&walk, dst, src, nbytes);
362         return ecb_aes_crypt(desc, sctx->dec, sctx->key, &walk);
363 }
364
365 static int fallback_init_blk(struct crypto_tfm *tfm)
366 {
367         const char *name = tfm->__crt_alg->cra_name;
368         struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
369
370         sctx->fallback.blk = crypto_alloc_blkcipher(name, 0,
371                         CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
372
373         if (IS_ERR(sctx->fallback.blk)) {
374                 pr_err("Allocating AES fallback algorithm %s failed\n",
375                        name);
376                 return PTR_ERR(sctx->fallback.blk);
377         }
378
379         return 0;
380 }
381
382 static void fallback_exit_blk(struct crypto_tfm *tfm)
383 {
384         struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
385
386         crypto_free_blkcipher(sctx->fallback.blk);
387         sctx->fallback.blk = NULL;
388 }
389
390 static struct crypto_alg ecb_aes_alg = {
391         .cra_name               =       "ecb(aes)",
392         .cra_driver_name        =       "ecb-aes-s390",
393         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
394         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER |
395                                         CRYPTO_ALG_NEED_FALLBACK,
396         .cra_blocksize          =       AES_BLOCK_SIZE,
397         .cra_ctxsize            =       sizeof(struct s390_aes_ctx),
398         .cra_type               =       &crypto_blkcipher_type,
399         .cra_module             =       THIS_MODULE,
400         .cra_list               =       LIST_HEAD_INIT(ecb_aes_alg.cra_list),
401         .cra_init               =       fallback_init_blk,
402         .cra_exit               =       fallback_exit_blk,
403         .cra_u                  =       {
404                 .blkcipher = {
405                         .min_keysize            =       AES_MIN_KEY_SIZE,
406                         .max_keysize            =       AES_MAX_KEY_SIZE,
407                         .setkey                 =       ecb_aes_set_key,
408                         .encrypt                =       ecb_aes_encrypt,
409                         .decrypt                =       ecb_aes_decrypt,
410                 }
411         }
412 };
413
414 static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
415                            unsigned int key_len)
416 {
417         struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
418         int ret;
419
420         ret = need_fallback(key_len);
421         if (ret > 0) {
422                 sctx->key_len = key_len;
423                 return setkey_fallback_blk(tfm, in_key, key_len);
424         }
425
426         switch (key_len) {
427         case 16:
428                 sctx->enc = KMC_AES_128_ENCRYPT;
429                 sctx->dec = KMC_AES_128_DECRYPT;
430                 break;
431         case 24:
432                 sctx->enc = KMC_AES_192_ENCRYPT;
433                 sctx->dec = KMC_AES_192_DECRYPT;
434                 break;
435         case 32:
436                 sctx->enc = KMC_AES_256_ENCRYPT;
437                 sctx->dec = KMC_AES_256_DECRYPT;
438                 break;
439         }
440
441         return aes_set_key(tfm, in_key, key_len);
442 }
443
444 static int cbc_aes_crypt(struct blkcipher_desc *desc, long func,
445                          struct blkcipher_walk *walk)
446 {
447         struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
448         int ret = blkcipher_walk_virt(desc, walk);
449         unsigned int nbytes = walk->nbytes;
450         struct {
451                 u8 iv[AES_BLOCK_SIZE];
452                 u8 key[AES_MAX_KEY_SIZE];
453         } param;
454
455         if (!nbytes)
456                 goto out;
457
458         memcpy(param.iv, walk->iv, AES_BLOCK_SIZE);
459         memcpy(param.key, sctx->key, sctx->key_len);
460         do {
461                 /* only use complete blocks */
462                 unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
463                 u8 *out = walk->dst.virt.addr;
464                 u8 *in = walk->src.virt.addr;
465
466                 ret = crypt_s390_kmc(func, &param, out, in, n);
467                 BUG_ON((ret < 0) || (ret != n));
468
469                 nbytes &= AES_BLOCK_SIZE - 1;
470                 ret = blkcipher_walk_done(desc, walk, nbytes);
471         } while ((nbytes = walk->nbytes));
472         memcpy(walk->iv, param.iv, AES_BLOCK_SIZE);
473
474 out:
475         return ret;
476 }
477
478 static int cbc_aes_encrypt(struct blkcipher_desc *desc,
479                            struct scatterlist *dst, struct scatterlist *src,
480                            unsigned int nbytes)
481 {
482         struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
483         struct blkcipher_walk walk;
484
485         if (unlikely(need_fallback(sctx->key_len)))
486                 return fallback_blk_enc(desc, dst, src, nbytes);
487
488         blkcipher_walk_init(&walk, dst, src, nbytes);
489         return cbc_aes_crypt(desc, sctx->enc, &walk);
490 }
491
492 static int cbc_aes_decrypt(struct blkcipher_desc *desc,
493                            struct scatterlist *dst, struct scatterlist *src,
494                            unsigned int nbytes)
495 {
496         struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
497         struct blkcipher_walk walk;
498
499         if (unlikely(need_fallback(sctx->key_len)))
500                 return fallback_blk_dec(desc, dst, src, nbytes);
501
502         blkcipher_walk_init(&walk, dst, src, nbytes);
503         return cbc_aes_crypt(desc, sctx->dec, &walk);
504 }
505
506 static struct crypto_alg cbc_aes_alg = {
507         .cra_name               =       "cbc(aes)",
508         .cra_driver_name        =       "cbc-aes-s390",
509         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
510         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER |
511                                         CRYPTO_ALG_NEED_FALLBACK,
512         .cra_blocksize          =       AES_BLOCK_SIZE,
513         .cra_ctxsize            =       sizeof(struct s390_aes_ctx),
514         .cra_type               =       &crypto_blkcipher_type,
515         .cra_module             =       THIS_MODULE,
516         .cra_list               =       LIST_HEAD_INIT(cbc_aes_alg.cra_list),
517         .cra_init               =       fallback_init_blk,
518         .cra_exit               =       fallback_exit_blk,
519         .cra_u                  =       {
520                 .blkcipher = {
521                         .min_keysize            =       AES_MIN_KEY_SIZE,
522                         .max_keysize            =       AES_MAX_KEY_SIZE,
523                         .ivsize                 =       AES_BLOCK_SIZE,
524                         .setkey                 =       cbc_aes_set_key,
525                         .encrypt                =       cbc_aes_encrypt,
526                         .decrypt                =       cbc_aes_decrypt,
527                 }
528         }
529 };
530
531 static int xts_fallback_setkey(struct crypto_tfm *tfm, const u8 *key,
532                                    unsigned int len)
533 {
534         struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
535         unsigned int ret;
536
537         xts_ctx->fallback->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
538         xts_ctx->fallback->base.crt_flags |= (tfm->crt_flags &
539                         CRYPTO_TFM_REQ_MASK);
540
541         ret = crypto_blkcipher_setkey(xts_ctx->fallback, key, len);
542         if (ret) {
543                 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
544                 tfm->crt_flags |= (xts_ctx->fallback->base.crt_flags &
545                                 CRYPTO_TFM_RES_MASK);
546         }
547         return ret;
548 }
549
550 static int xts_fallback_decrypt(struct blkcipher_desc *desc,
551                 struct scatterlist *dst, struct scatterlist *src,
552                 unsigned int nbytes)
553 {
554         struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
555         struct crypto_blkcipher *tfm;
556         unsigned int ret;
557
558         tfm = desc->tfm;
559         desc->tfm = xts_ctx->fallback;
560
561         ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
562
563         desc->tfm = tfm;
564         return ret;
565 }
566
567 static int xts_fallback_encrypt(struct blkcipher_desc *desc,
568                 struct scatterlist *dst, struct scatterlist *src,
569                 unsigned int nbytes)
570 {
571         struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
572         struct crypto_blkcipher *tfm;
573         unsigned int ret;
574
575         tfm = desc->tfm;
576         desc->tfm = xts_ctx->fallback;
577
578         ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
579
580         desc->tfm = tfm;
581         return ret;
582 }
583
584 static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
585                            unsigned int key_len)
586 {
587         struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
588         u32 *flags = &tfm->crt_flags;
589
590         switch (key_len) {
591         case 32:
592                 xts_ctx->enc = KM_XTS_128_ENCRYPT;
593                 xts_ctx->dec = KM_XTS_128_DECRYPT;
594                 memcpy(xts_ctx->key + 16, in_key, 16);
595                 memcpy(xts_ctx->pcc.key + 16, in_key + 16, 16);
596                 break;
597         case 48:
598                 xts_ctx->enc = 0;
599                 xts_ctx->dec = 0;
600                 xts_fallback_setkey(tfm, in_key, key_len);
601                 break;
602         case 64:
603                 xts_ctx->enc = KM_XTS_256_ENCRYPT;
604                 xts_ctx->dec = KM_XTS_256_DECRYPT;
605                 memcpy(xts_ctx->key, in_key, 32);
606                 memcpy(xts_ctx->pcc.key, in_key + 32, 32);
607                 break;
608         default:
609                 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
610                 return -EINVAL;
611         }
612         xts_ctx->key_len = key_len;
613         return 0;
614 }
615
616 static int xts_aes_crypt(struct blkcipher_desc *desc, long func,
617                          struct s390_xts_ctx *xts_ctx,
618                          struct blkcipher_walk *walk)
619 {
620         unsigned int offset = (xts_ctx->key_len >> 1) & 0x10;
621         int ret = blkcipher_walk_virt(desc, walk);
622         unsigned int nbytes = walk->nbytes;
623         unsigned int n;
624         u8 *in, *out;
625         void *param;
626
627         if (!nbytes)
628                 goto out;
629
630         memset(xts_ctx->pcc.block, 0, sizeof(xts_ctx->pcc.block));
631         memset(xts_ctx->pcc.bit, 0, sizeof(xts_ctx->pcc.bit));
632         memset(xts_ctx->pcc.xts, 0, sizeof(xts_ctx->pcc.xts));
633         memcpy(xts_ctx->pcc.tweak, walk->iv, sizeof(xts_ctx->pcc.tweak));
634         param = xts_ctx->pcc.key + offset;
635         ret = crypt_s390_pcc(func, param);
636         BUG_ON(ret < 0);
637
638         memcpy(xts_ctx->xts_param, xts_ctx->pcc.xts, 16);
639         param = xts_ctx->key + offset;
640         do {
641                 /* only use complete blocks */
642                 n = nbytes & ~(AES_BLOCK_SIZE - 1);
643                 out = walk->dst.virt.addr;
644                 in = walk->src.virt.addr;
645
646                 ret = crypt_s390_km(func, param, out, in, n);
647                 BUG_ON(ret < 0 || ret != n);
648
649                 nbytes &= AES_BLOCK_SIZE - 1;
650                 ret = blkcipher_walk_done(desc, walk, nbytes);
651         } while ((nbytes = walk->nbytes));
652 out:
653         return ret;
654 }
655
656 static int xts_aes_encrypt(struct blkcipher_desc *desc,
657                            struct scatterlist *dst, struct scatterlist *src,
658                            unsigned int nbytes)
659 {
660         struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
661         struct blkcipher_walk walk;
662
663         if (unlikely(xts_ctx->key_len == 48))
664                 return xts_fallback_encrypt(desc, dst, src, nbytes);
665
666         blkcipher_walk_init(&walk, dst, src, nbytes);
667         return xts_aes_crypt(desc, xts_ctx->enc, xts_ctx, &walk);
668 }
669
670 static int xts_aes_decrypt(struct blkcipher_desc *desc,
671                            struct scatterlist *dst, struct scatterlist *src,
672                            unsigned int nbytes)
673 {
674         struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
675         struct blkcipher_walk walk;
676
677         if (unlikely(xts_ctx->key_len == 48))
678                 return xts_fallback_decrypt(desc, dst, src, nbytes);
679
680         blkcipher_walk_init(&walk, dst, src, nbytes);
681         return xts_aes_crypt(desc, xts_ctx->dec, xts_ctx, &walk);
682 }
683
684 static int xts_fallback_init(struct crypto_tfm *tfm)
685 {
686         const char *name = tfm->__crt_alg->cra_name;
687         struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
688
689         xts_ctx->fallback = crypto_alloc_blkcipher(name, 0,
690                         CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
691
692         if (IS_ERR(xts_ctx->fallback)) {
693                 pr_err("Allocating XTS fallback algorithm %s failed\n",
694                        name);
695                 return PTR_ERR(xts_ctx->fallback);
696         }
697         return 0;
698 }
699
700 static void xts_fallback_exit(struct crypto_tfm *tfm)
701 {
702         struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
703
704         crypto_free_blkcipher(xts_ctx->fallback);
705         xts_ctx->fallback = NULL;
706 }
707
708 static struct crypto_alg xts_aes_alg = {
709         .cra_name               =       "xts(aes)",
710         .cra_driver_name        =       "xts-aes-s390",
711         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
712         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER |
713                                         CRYPTO_ALG_NEED_FALLBACK,
714         .cra_blocksize          =       AES_BLOCK_SIZE,
715         .cra_ctxsize            =       sizeof(struct s390_xts_ctx),
716         .cra_type               =       &crypto_blkcipher_type,
717         .cra_module             =       THIS_MODULE,
718         .cra_list               =       LIST_HEAD_INIT(xts_aes_alg.cra_list),
719         .cra_init               =       xts_fallback_init,
720         .cra_exit               =       xts_fallback_exit,
721         .cra_u                  =       {
722                 .blkcipher = {
723                         .min_keysize            =       2 * AES_MIN_KEY_SIZE,
724                         .max_keysize            =       2 * AES_MAX_KEY_SIZE,
725                         .ivsize                 =       AES_BLOCK_SIZE,
726                         .setkey                 =       xts_aes_set_key,
727                         .encrypt                =       xts_aes_encrypt,
728                         .decrypt                =       xts_aes_decrypt,
729                 }
730         }
731 };
732
733 static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
734                            unsigned int key_len)
735 {
736         struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
737
738         switch (key_len) {
739         case 16:
740                 sctx->enc = KMCTR_AES_128_ENCRYPT;
741                 sctx->dec = KMCTR_AES_128_DECRYPT;
742                 break;
743         case 24:
744                 sctx->enc = KMCTR_AES_192_ENCRYPT;
745                 sctx->dec = KMCTR_AES_192_DECRYPT;
746                 break;
747         case 32:
748                 sctx->enc = KMCTR_AES_256_ENCRYPT;
749                 sctx->dec = KMCTR_AES_256_DECRYPT;
750                 break;
751         }
752
753         return aes_set_key(tfm, in_key, key_len);
754 }
755
756 static int ctr_aes_crypt(struct blkcipher_desc *desc, long func,
757                          struct s390_aes_ctx *sctx, struct blkcipher_walk *walk)
758 {
759         int ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
760         unsigned int i, n, nbytes;
761         u8 buf[AES_BLOCK_SIZE];
762         u8 *out, *in;
763
764         if (!walk->nbytes)
765                 return ret;
766
767         memcpy(ctrblk, walk->iv, AES_BLOCK_SIZE);
768         while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
769                 out = walk->dst.virt.addr;
770                 in = walk->src.virt.addr;
771                 while (nbytes >= AES_BLOCK_SIZE) {
772                         /* only use complete blocks, max. PAGE_SIZE */
773                         n = (nbytes > PAGE_SIZE) ? PAGE_SIZE :
774                                                  nbytes & ~(AES_BLOCK_SIZE - 1);
775                         for (i = AES_BLOCK_SIZE; i < n; i += AES_BLOCK_SIZE) {
776                                 memcpy(ctrblk + i, ctrblk + i - AES_BLOCK_SIZE,
777                                        AES_BLOCK_SIZE);
778                                 crypto_inc(ctrblk + i, AES_BLOCK_SIZE);
779                         }
780                         ret = crypt_s390_kmctr(func, sctx->key, out, in, n, ctrblk);
781                         BUG_ON(ret < 0 || ret != n);
782                         if (n > AES_BLOCK_SIZE)
783                                 memcpy(ctrblk, ctrblk + n - AES_BLOCK_SIZE,
784                                        AES_BLOCK_SIZE);
785                         crypto_inc(ctrblk, AES_BLOCK_SIZE);
786                         out += n;
787                         in += n;
788                         nbytes -= n;
789                 }
790                 ret = blkcipher_walk_done(desc, walk, nbytes);
791         }
792         /*
793          * final block may be < AES_BLOCK_SIZE, copy only nbytes
794          */
795         if (nbytes) {
796                 out = walk->dst.virt.addr;
797                 in = walk->src.virt.addr;
798                 ret = crypt_s390_kmctr(func, sctx->key, buf, in,
799                                        AES_BLOCK_SIZE, ctrblk);
800                 BUG_ON(ret < 0 || ret != AES_BLOCK_SIZE);
801                 memcpy(out, buf, nbytes);
802                 crypto_inc(ctrblk, AES_BLOCK_SIZE);
803                 ret = blkcipher_walk_done(desc, walk, 0);
804         }
805         memcpy(walk->iv, ctrblk, AES_BLOCK_SIZE);
806         return ret;
807 }
808
809 static int ctr_aes_encrypt(struct blkcipher_desc *desc,
810                            struct scatterlist *dst, struct scatterlist *src,
811                            unsigned int nbytes)
812 {
813         struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
814         struct blkcipher_walk walk;
815
816         blkcipher_walk_init(&walk, dst, src, nbytes);
817         return ctr_aes_crypt(desc, sctx->enc, sctx, &walk);
818 }
819
820 static int ctr_aes_decrypt(struct blkcipher_desc *desc,
821                            struct scatterlist *dst, struct scatterlist *src,
822                            unsigned int nbytes)
823 {
824         struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
825         struct blkcipher_walk walk;
826
827         blkcipher_walk_init(&walk, dst, src, nbytes);
828         return ctr_aes_crypt(desc, sctx->dec, sctx, &walk);
829 }
830
831 static struct crypto_alg ctr_aes_alg = {
832         .cra_name               =       "ctr(aes)",
833         .cra_driver_name        =       "ctr-aes-s390",
834         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
835         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
836         .cra_blocksize          =       1,
837         .cra_ctxsize            =       sizeof(struct s390_aes_ctx),
838         .cra_type               =       &crypto_blkcipher_type,
839         .cra_module             =       THIS_MODULE,
840         .cra_list               =       LIST_HEAD_INIT(ctr_aes_alg.cra_list),
841         .cra_u                  =       {
842                 .blkcipher = {
843                         .min_keysize            =       AES_MIN_KEY_SIZE,
844                         .max_keysize            =       AES_MAX_KEY_SIZE,
845                         .ivsize                 =       AES_BLOCK_SIZE,
846                         .setkey                 =       ctr_aes_set_key,
847                         .encrypt                =       ctr_aes_encrypt,
848                         .decrypt                =       ctr_aes_decrypt,
849                 }
850         }
851 };
852
853 static int __init aes_s390_init(void)
854 {
855         int ret;
856
857         if (crypt_s390_func_available(KM_AES_128_ENCRYPT, CRYPT_S390_MSA))
858                 keylen_flag |= AES_KEYLEN_128;
859         if (crypt_s390_func_available(KM_AES_192_ENCRYPT, CRYPT_S390_MSA))
860                 keylen_flag |= AES_KEYLEN_192;
861         if (crypt_s390_func_available(KM_AES_256_ENCRYPT, CRYPT_S390_MSA))
862                 keylen_flag |= AES_KEYLEN_256;
863
864         if (!keylen_flag)
865                 return -EOPNOTSUPP;
866
867         /* z9 109 and z9 BC/EC only support 128 bit key length */
868         if (keylen_flag == AES_KEYLEN_128)
869                 pr_info("AES hardware acceleration is only available for"
870                         " 128-bit keys\n");
871
872         ret = crypto_register_alg(&aes_alg);
873         if (ret)
874                 goto aes_err;
875
876         ret = crypto_register_alg(&ecb_aes_alg);
877         if (ret)
878                 goto ecb_aes_err;
879
880         ret = crypto_register_alg(&cbc_aes_alg);
881         if (ret)
882                 goto cbc_aes_err;
883
884         if (crypt_s390_func_available(KM_XTS_128_ENCRYPT,
885                         CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
886             crypt_s390_func_available(KM_XTS_256_ENCRYPT,
887                         CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
888                 ret = crypto_register_alg(&xts_aes_alg);
889                 if (ret)
890                         goto xts_aes_err;
891         }
892
893         if (crypt_s390_func_available(KMCTR_AES_128_ENCRYPT,
894                                 CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
895             crypt_s390_func_available(KMCTR_AES_192_ENCRYPT,
896                                 CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
897             crypt_s390_func_available(KMCTR_AES_256_ENCRYPT,
898                                 CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
899                 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
900                 if (!ctrblk) {
901                         ret = -ENOMEM;
902                         goto ctr_aes_err;
903                 }
904                 ret = crypto_register_alg(&ctr_aes_alg);
905                 if (ret) {
906                         free_page((unsigned long) ctrblk);
907                         goto ctr_aes_err;
908                 }
909         }
910
911 out:
912         return ret;
913
914 ctr_aes_err:
915         crypto_unregister_alg(&xts_aes_alg);
916 xts_aes_err:
917         crypto_unregister_alg(&cbc_aes_alg);
918 cbc_aes_err:
919         crypto_unregister_alg(&ecb_aes_alg);
920 ecb_aes_err:
921         crypto_unregister_alg(&aes_alg);
922 aes_err:
923         goto out;
924 }
925
926 static void __exit aes_s390_fini(void)
927 {
928         crypto_unregister_alg(&ctr_aes_alg);
929         free_page((unsigned long) ctrblk);
930         crypto_unregister_alg(&xts_aes_alg);
931         crypto_unregister_alg(&cbc_aes_alg);
932         crypto_unregister_alg(&ecb_aes_alg);
933         crypto_unregister_alg(&aes_alg);
934 }
935
936 module_init(aes_s390_init);
937 module_exit(aes_s390_fini);
938
939 MODULE_ALIAS("aes-all");
940
941 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
942 MODULE_LICENSE("GPL");