crypto: s390 - fix des and des3_ede ctr concurrency issue
[pandora-kernel.git] / arch / s390 / crypto / des_s390.c
1 /*
2  * Cryptographic API.
3  *
4  * s390 implementation of the DES Cipher Algorithm.
5  *
6  * Copyright IBM Corp. 2003,2011
7  * Author(s): Thomas Spatzier
8  *            Jan Glauber (jan.glauber@de.ibm.com)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  */
16
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/crypto.h>
20 #include <crypto/algapi.h>
21 #include <crypto/des.h>
22
23 #include "crypt_s390.h"
24
25 #define DES3_KEY_SIZE   (3 * DES_KEY_SIZE)
26
27 static u8 *ctrblk;
28 static DEFINE_SPINLOCK(ctrblk_lock);
29
30 struct s390_des_ctx {
31         u8 iv[DES_BLOCK_SIZE];
32         u8 key[DES3_KEY_SIZE];
33 };
34
35 static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
36                       unsigned int key_len)
37 {
38         struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
39         u32 *flags = &tfm->crt_flags;
40         u32 tmp[DES_EXPKEY_WORDS];
41
42         /* check for weak keys */
43         if (!des_ekey(tmp, key) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
44                 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
45                 return -EINVAL;
46         }
47
48         memcpy(ctx->key, key, key_len);
49         return 0;
50 }
51
52 static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
53 {
54         struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
55
56         crypt_s390_km(KM_DEA_ENCRYPT, ctx->key, out, in, DES_BLOCK_SIZE);
57 }
58
59 static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
60 {
61         struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
62
63         crypt_s390_km(KM_DEA_DECRYPT, ctx->key, out, in, DES_BLOCK_SIZE);
64 }
65
66 static struct crypto_alg des_alg = {
67         .cra_name               =       "des",
68         .cra_driver_name        =       "des-s390",
69         .cra_priority           =       CRYPT_S390_PRIORITY,
70         .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
71         .cra_blocksize          =       DES_BLOCK_SIZE,
72         .cra_ctxsize            =       sizeof(struct s390_des_ctx),
73         .cra_module             =       THIS_MODULE,
74         .cra_list               =       LIST_HEAD_INIT(des_alg.cra_list),
75         .cra_u                  =       {
76                 .cipher = {
77                         .cia_min_keysize        =       DES_KEY_SIZE,
78                         .cia_max_keysize        =       DES_KEY_SIZE,
79                         .cia_setkey             =       des_setkey,
80                         .cia_encrypt            =       des_encrypt,
81                         .cia_decrypt            =       des_decrypt,
82                 }
83         }
84 };
85
86 static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
87                             u8 *key, struct blkcipher_walk *walk)
88 {
89         int ret = blkcipher_walk_virt(desc, walk);
90         unsigned int nbytes;
91
92         while ((nbytes = walk->nbytes)) {
93                 /* only use complete blocks */
94                 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
95                 u8 *out = walk->dst.virt.addr;
96                 u8 *in = walk->src.virt.addr;
97
98                 ret = crypt_s390_km(func, key, out, in, n);
99                 if (ret < 0 || ret != n)
100                         return -EIO;
101
102                 nbytes &= DES_BLOCK_SIZE - 1;
103                 ret = blkcipher_walk_done(desc, walk, nbytes);
104         }
105
106         return ret;
107 }
108
109 static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
110                             struct blkcipher_walk *walk)
111 {
112         struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
113         int ret = blkcipher_walk_virt(desc, walk);
114         unsigned int nbytes = walk->nbytes;
115         struct {
116                 u8 iv[DES_BLOCK_SIZE];
117                 u8 key[DES3_KEY_SIZE];
118         } param;
119
120         if (!nbytes)
121                 goto out;
122
123         memcpy(param.iv, walk->iv, DES_BLOCK_SIZE);
124         memcpy(param.key, ctx->key, DES3_KEY_SIZE);
125         do {
126                 /* only use complete blocks */
127                 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
128                 u8 *out = walk->dst.virt.addr;
129                 u8 *in = walk->src.virt.addr;
130
131                 ret = crypt_s390_kmc(func, &param, out, in, n);
132                 if (ret < 0 || ret != n)
133                         return -EIO;
134
135                 nbytes &= DES_BLOCK_SIZE - 1;
136                 ret = blkcipher_walk_done(desc, walk, nbytes);
137         } while ((nbytes = walk->nbytes));
138         memcpy(walk->iv, param.iv, DES_BLOCK_SIZE);
139
140 out:
141         return ret;
142 }
143
144 static int ecb_des_encrypt(struct blkcipher_desc *desc,
145                            struct scatterlist *dst, struct scatterlist *src,
146                            unsigned int nbytes)
147 {
148         struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
149         struct blkcipher_walk walk;
150
151         blkcipher_walk_init(&walk, dst, src, nbytes);
152         return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, ctx->key, &walk);
153 }
154
155 static int ecb_des_decrypt(struct blkcipher_desc *desc,
156                            struct scatterlist *dst, struct scatterlist *src,
157                            unsigned int nbytes)
158 {
159         struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
160         struct blkcipher_walk walk;
161
162         blkcipher_walk_init(&walk, dst, src, nbytes);
163         return ecb_desall_crypt(desc, KM_DEA_DECRYPT, ctx->key, &walk);
164 }
165
166 static struct crypto_alg ecb_des_alg = {
167         .cra_name               =       "ecb(des)",
168         .cra_driver_name        =       "ecb-des-s390",
169         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
170         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
171         .cra_blocksize          =       DES_BLOCK_SIZE,
172         .cra_ctxsize            =       sizeof(struct s390_des_ctx),
173         .cra_type               =       &crypto_blkcipher_type,
174         .cra_module             =       THIS_MODULE,
175         .cra_list               =       LIST_HEAD_INIT(ecb_des_alg.cra_list),
176         .cra_u                  =       {
177                 .blkcipher = {
178                         .min_keysize            =       DES_KEY_SIZE,
179                         .max_keysize            =       DES_KEY_SIZE,
180                         .setkey                 =       des_setkey,
181                         .encrypt                =       ecb_des_encrypt,
182                         .decrypt                =       ecb_des_decrypt,
183                 }
184         }
185 };
186
187 static int cbc_des_encrypt(struct blkcipher_desc *desc,
188                            struct scatterlist *dst, struct scatterlist *src,
189                            unsigned int nbytes)
190 {
191         struct blkcipher_walk walk;
192
193         blkcipher_walk_init(&walk, dst, src, nbytes);
194         return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, &walk);
195 }
196
197 static int cbc_des_decrypt(struct blkcipher_desc *desc,
198                            struct scatterlist *dst, struct scatterlist *src,
199                            unsigned int nbytes)
200 {
201         struct blkcipher_walk walk;
202
203         blkcipher_walk_init(&walk, dst, src, nbytes);
204         return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, &walk);
205 }
206
207 static struct crypto_alg cbc_des_alg = {
208         .cra_name               =       "cbc(des)",
209         .cra_driver_name        =       "cbc-des-s390",
210         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
211         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
212         .cra_blocksize          =       DES_BLOCK_SIZE,
213         .cra_ctxsize            =       sizeof(struct s390_des_ctx),
214         .cra_type               =       &crypto_blkcipher_type,
215         .cra_module             =       THIS_MODULE,
216         .cra_list               =       LIST_HEAD_INIT(cbc_des_alg.cra_list),
217         .cra_u                  =       {
218                 .blkcipher = {
219                         .min_keysize            =       DES_KEY_SIZE,
220                         .max_keysize            =       DES_KEY_SIZE,
221                         .ivsize                 =       DES_BLOCK_SIZE,
222                         .setkey                 =       des_setkey,
223                         .encrypt                =       cbc_des_encrypt,
224                         .decrypt                =       cbc_des_decrypt,
225                 }
226         }
227 };
228
229 /*
230  * RFC2451:
231  *
232  *   For DES-EDE3, there is no known need to reject weak or
233  *   complementation keys.  Any weakness is obviated by the use of
234  *   multiple keys.
235  *
236  *   However, if the first two or last two independent 64-bit keys are
237  *   equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
238  *   same as DES.  Implementers MUST reject keys that exhibit this
239  *   property.
240  *
241  */
242 static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
243                        unsigned int key_len)
244 {
245         struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
246         u32 *flags = &tfm->crt_flags;
247
248         if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
249             memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
250                    DES_KEY_SIZE)) &&
251             (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
252                 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
253                 return -EINVAL;
254         }
255         memcpy(ctx->key, key, key_len);
256         return 0;
257 }
258
259 static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
260 {
261         struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
262
263         crypt_s390_km(KM_TDEA_192_ENCRYPT, ctx->key, dst, src, DES_BLOCK_SIZE);
264 }
265
266 static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
267 {
268         struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
269
270         crypt_s390_km(KM_TDEA_192_DECRYPT, ctx->key, dst, src, DES_BLOCK_SIZE);
271 }
272
273 static struct crypto_alg des3_alg = {
274         .cra_name               =       "des3_ede",
275         .cra_driver_name        =       "des3_ede-s390",
276         .cra_priority           =       CRYPT_S390_PRIORITY,
277         .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
278         .cra_blocksize          =       DES_BLOCK_SIZE,
279         .cra_ctxsize            =       sizeof(struct s390_des_ctx),
280         .cra_module             =       THIS_MODULE,
281         .cra_list               =       LIST_HEAD_INIT(des3_alg.cra_list),
282         .cra_u                  =       {
283                 .cipher = {
284                         .cia_min_keysize        =       DES3_KEY_SIZE,
285                         .cia_max_keysize        =       DES3_KEY_SIZE,
286                         .cia_setkey             =       des3_setkey,
287                         .cia_encrypt            =       des3_encrypt,
288                         .cia_decrypt            =       des3_decrypt,
289                 }
290         }
291 };
292
293 static int ecb_des3_encrypt(struct blkcipher_desc *desc,
294                             struct scatterlist *dst, struct scatterlist *src,
295                             unsigned int nbytes)
296 {
297         struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
298         struct blkcipher_walk walk;
299
300         blkcipher_walk_init(&walk, dst, src, nbytes);
301         return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, ctx->key, &walk);
302 }
303
304 static int ecb_des3_decrypt(struct blkcipher_desc *desc,
305                             struct scatterlist *dst, struct scatterlist *src,
306                             unsigned int nbytes)
307 {
308         struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
309         struct blkcipher_walk walk;
310
311         blkcipher_walk_init(&walk, dst, src, nbytes);
312         return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, ctx->key, &walk);
313 }
314
315 static struct crypto_alg ecb_des3_alg = {
316         .cra_name               =       "ecb(des3_ede)",
317         .cra_driver_name        =       "ecb-des3_ede-s390",
318         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
319         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
320         .cra_blocksize          =       DES_BLOCK_SIZE,
321         .cra_ctxsize            =       sizeof(struct s390_des_ctx),
322         .cra_type               =       &crypto_blkcipher_type,
323         .cra_module             =       THIS_MODULE,
324         .cra_list               =       LIST_HEAD_INIT(
325                                                 ecb_des3_alg.cra_list),
326         .cra_u                  =       {
327                 .blkcipher = {
328                         .min_keysize            =       DES3_KEY_SIZE,
329                         .max_keysize            =       DES3_KEY_SIZE,
330                         .setkey                 =       des3_setkey,
331                         .encrypt                =       ecb_des3_encrypt,
332                         .decrypt                =       ecb_des3_decrypt,
333                 }
334         }
335 };
336
337 static int cbc_des3_encrypt(struct blkcipher_desc *desc,
338                             struct scatterlist *dst, struct scatterlist *src,
339                             unsigned int nbytes)
340 {
341         struct blkcipher_walk walk;
342
343         blkcipher_walk_init(&walk, dst, src, nbytes);
344         return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, &walk);
345 }
346
347 static int cbc_des3_decrypt(struct blkcipher_desc *desc,
348                             struct scatterlist *dst, struct scatterlist *src,
349                             unsigned int nbytes)
350 {
351         struct blkcipher_walk walk;
352
353         blkcipher_walk_init(&walk, dst, src, nbytes);
354         return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, &walk);
355 }
356
357 static struct crypto_alg cbc_des3_alg = {
358         .cra_name               =       "cbc(des3_ede)",
359         .cra_driver_name        =       "cbc-des3_ede-s390",
360         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
361         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
362         .cra_blocksize          =       DES_BLOCK_SIZE,
363         .cra_ctxsize            =       sizeof(struct s390_des_ctx),
364         .cra_type               =       &crypto_blkcipher_type,
365         .cra_module             =       THIS_MODULE,
366         .cra_list               =       LIST_HEAD_INIT(
367                                                 cbc_des3_alg.cra_list),
368         .cra_u                  =       {
369                 .blkcipher = {
370                         .min_keysize            =       DES3_KEY_SIZE,
371                         .max_keysize            =       DES3_KEY_SIZE,
372                         .ivsize                 =       DES_BLOCK_SIZE,
373                         .setkey                 =       des3_setkey,
374                         .encrypt                =       cbc_des3_encrypt,
375                         .decrypt                =       cbc_des3_decrypt,
376                 }
377         }
378 };
379
380 static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes)
381 {
382         unsigned int i, n;
383
384         /* align to block size, max. PAGE_SIZE */
385         n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(DES_BLOCK_SIZE - 1);
386         for (i = DES_BLOCK_SIZE; i < n; i += DES_BLOCK_SIZE) {
387                 memcpy(ctrptr + i, ctrptr + i - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
388                 crypto_inc(ctrptr + i, DES_BLOCK_SIZE);
389         }
390         return n;
391 }
392
393 static int ctr_desall_crypt(struct blkcipher_desc *desc, long func,
394                             struct s390_des_ctx *ctx,
395                             struct blkcipher_walk *walk)
396 {
397         int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
398         unsigned int n, nbytes;
399         u8 buf[DES_BLOCK_SIZE], ctrbuf[DES_BLOCK_SIZE];
400         u8 *out, *in, *ctrptr = ctrbuf;
401
402         if (!walk->nbytes)
403                 return ret;
404
405         if (spin_trylock(&ctrblk_lock))
406                 ctrptr = ctrblk;
407
408         memcpy(ctrptr, walk->iv, DES_BLOCK_SIZE);
409         while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
410                 out = walk->dst.virt.addr;
411                 in = walk->src.virt.addr;
412                 while (nbytes >= DES_BLOCK_SIZE) {
413                         if (ctrptr == ctrblk)
414                                 n = __ctrblk_init(ctrptr, nbytes);
415                         else
416                                 n = DES_BLOCK_SIZE;
417                         ret = crypt_s390_kmctr(func, ctx->key, out, in,
418                                                n, ctrptr);
419                         if (ret < 0 || ret != n) {
420                                 if (ctrptr == ctrblk)
421                                         spin_unlock(&ctrblk_lock);
422                                 return -EIO;
423                         }
424                         if (n > DES_BLOCK_SIZE)
425                                 memcpy(ctrptr, ctrptr + n - DES_BLOCK_SIZE,
426                                        DES_BLOCK_SIZE);
427                         crypto_inc(ctrptr, DES_BLOCK_SIZE);
428                         out += n;
429                         in += n;
430                         nbytes -= n;
431                 }
432                 ret = blkcipher_walk_done(desc, walk, nbytes);
433         }
434         if (ctrptr == ctrblk) {
435                 if (nbytes)
436                         memcpy(ctrbuf, ctrptr, DES_BLOCK_SIZE);
437                 else
438                         memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
439                 spin_unlock(&ctrblk_lock);
440         }
441         /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
442         if (nbytes) {
443                 out = walk->dst.virt.addr;
444                 in = walk->src.virt.addr;
445                 ret = crypt_s390_kmctr(func, ctx->key, buf, in,
446                                        DES_BLOCK_SIZE, ctrbuf);
447                 if (ret < 0 || ret != DES_BLOCK_SIZE)
448                         return -EIO;
449                 memcpy(out, buf, nbytes);
450                 crypto_inc(ctrbuf, DES_BLOCK_SIZE);
451                 ret = blkcipher_walk_done(desc, walk, 0);
452                 memcpy(walk->iv, ctrbuf, DES_BLOCK_SIZE);
453         }
454         return ret;
455 }
456
457 static int ctr_des_encrypt(struct blkcipher_desc *desc,
458                            struct scatterlist *dst, struct scatterlist *src,
459                            unsigned int nbytes)
460 {
461         struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
462         struct blkcipher_walk walk;
463
464         blkcipher_walk_init(&walk, dst, src, nbytes);
465         return ctr_desall_crypt(desc, KMCTR_DEA_ENCRYPT, ctx, &walk);
466 }
467
468 static int ctr_des_decrypt(struct blkcipher_desc *desc,
469                            struct scatterlist *dst, struct scatterlist *src,
470                            unsigned int nbytes)
471 {
472         struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
473         struct blkcipher_walk walk;
474
475         blkcipher_walk_init(&walk, dst, src, nbytes);
476         return ctr_desall_crypt(desc, KMCTR_DEA_DECRYPT, ctx, &walk);
477 }
478
479 static struct crypto_alg ctr_des_alg = {
480         .cra_name               =       "ctr(des)",
481         .cra_driver_name        =       "ctr-des-s390",
482         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
483         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
484         .cra_blocksize          =       1,
485         .cra_ctxsize            =       sizeof(struct s390_des_ctx),
486         .cra_type               =       &crypto_blkcipher_type,
487         .cra_module             =       THIS_MODULE,
488         .cra_list               =       LIST_HEAD_INIT(ctr_des_alg.cra_list),
489         .cra_u                  =       {
490                 .blkcipher = {
491                         .min_keysize            =       DES_KEY_SIZE,
492                         .max_keysize            =       DES_KEY_SIZE,
493                         .ivsize                 =       DES_BLOCK_SIZE,
494                         .setkey                 =       des_setkey,
495                         .encrypt                =       ctr_des_encrypt,
496                         .decrypt                =       ctr_des_decrypt,
497                 }
498         }
499 };
500
501 static int ctr_des3_encrypt(struct blkcipher_desc *desc,
502                             struct scatterlist *dst, struct scatterlist *src,
503                             unsigned int nbytes)
504 {
505         struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
506         struct blkcipher_walk walk;
507
508         blkcipher_walk_init(&walk, dst, src, nbytes);
509         return ctr_desall_crypt(desc, KMCTR_TDEA_192_ENCRYPT, ctx, &walk);
510 }
511
512 static int ctr_des3_decrypt(struct blkcipher_desc *desc,
513                             struct scatterlist *dst, struct scatterlist *src,
514                             unsigned int nbytes)
515 {
516         struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
517         struct blkcipher_walk walk;
518
519         blkcipher_walk_init(&walk, dst, src, nbytes);
520         return ctr_desall_crypt(desc, KMCTR_TDEA_192_DECRYPT, ctx, &walk);
521 }
522
523 static struct crypto_alg ctr_des3_alg = {
524         .cra_name               =       "ctr(des3_ede)",
525         .cra_driver_name        =       "ctr-des3_ede-s390",
526         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
527         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
528         .cra_blocksize          =       1,
529         .cra_ctxsize            =       sizeof(struct s390_des_ctx),
530         .cra_type               =       &crypto_blkcipher_type,
531         .cra_module             =       THIS_MODULE,
532         .cra_list               =       LIST_HEAD_INIT(ctr_des3_alg.cra_list),
533         .cra_u                  =       {
534                 .blkcipher = {
535                         .min_keysize            =       DES3_KEY_SIZE,
536                         .max_keysize            =       DES3_KEY_SIZE,
537                         .ivsize                 =       DES_BLOCK_SIZE,
538                         .setkey                 =       des3_setkey,
539                         .encrypt                =       ctr_des3_encrypt,
540                         .decrypt                =       ctr_des3_decrypt,
541                 }
542         }
543 };
544
545 static int __init des_s390_init(void)
546 {
547         int ret;
548
549         if (!crypt_s390_func_available(KM_DEA_ENCRYPT, CRYPT_S390_MSA) ||
550             !crypt_s390_func_available(KM_TDEA_192_ENCRYPT, CRYPT_S390_MSA))
551                 return -EOPNOTSUPP;
552
553         ret = crypto_register_alg(&des_alg);
554         if (ret)
555                 goto des_err;
556         ret = crypto_register_alg(&ecb_des_alg);
557         if (ret)
558                 goto ecb_des_err;
559         ret = crypto_register_alg(&cbc_des_alg);
560         if (ret)
561                 goto cbc_des_err;
562         ret = crypto_register_alg(&des3_alg);
563         if (ret)
564                 goto des3_err;
565         ret = crypto_register_alg(&ecb_des3_alg);
566         if (ret)
567                 goto ecb_des3_err;
568         ret = crypto_register_alg(&cbc_des3_alg);
569         if (ret)
570                 goto cbc_des3_err;
571
572         if (crypt_s390_func_available(KMCTR_DEA_ENCRYPT,
573                         CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
574             crypt_s390_func_available(KMCTR_TDEA_192_ENCRYPT,
575                         CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
576                 ret = crypto_register_alg(&ctr_des_alg);
577                 if (ret)
578                         goto ctr_des_err;
579                 ret = crypto_register_alg(&ctr_des3_alg);
580                 if (ret)
581                         goto ctr_des3_err;
582                 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
583                 if (!ctrblk) {
584                         ret = -ENOMEM;
585                         goto ctr_mem_err;
586                 }
587         }
588 out:
589         return ret;
590
591 ctr_mem_err:
592         crypto_unregister_alg(&ctr_des3_alg);
593 ctr_des3_err:
594         crypto_unregister_alg(&ctr_des_alg);
595 ctr_des_err:
596         crypto_unregister_alg(&cbc_des3_alg);
597 cbc_des3_err:
598         crypto_unregister_alg(&ecb_des3_alg);
599 ecb_des3_err:
600         crypto_unregister_alg(&des3_alg);
601 des3_err:
602         crypto_unregister_alg(&cbc_des_alg);
603 cbc_des_err:
604         crypto_unregister_alg(&ecb_des_alg);
605 ecb_des_err:
606         crypto_unregister_alg(&des_alg);
607 des_err:
608         goto out;
609 }
610
611 static void __exit des_s390_exit(void)
612 {
613         if (ctrblk) {
614                 crypto_unregister_alg(&ctr_des_alg);
615                 crypto_unregister_alg(&ctr_des3_alg);
616                 free_page((unsigned long) ctrblk);
617         }
618         crypto_unregister_alg(&cbc_des3_alg);
619         crypto_unregister_alg(&ecb_des3_alg);
620         crypto_unregister_alg(&des3_alg);
621         crypto_unregister_alg(&cbc_des_alg);
622         crypto_unregister_alg(&ecb_des_alg);
623         crypto_unregister_alg(&des_alg);
624 }
625
626 module_init(des_s390_init);
627 module_exit(des_s390_exit);
628
629 MODULE_ALIAS("des");
630 MODULE_ALIAS("des3_ede");
631
632 MODULE_LICENSE("GPL");
633 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");