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