[CRYPTO] s390: Added block cipher versions of CBC/ECB
[linux-2.6] / arch / s390 / crypto / des_s390.c
1 /*
2  * Cryptographic API.
3  *
4  * s390 implementation of the DES Cipher Algorithm.
5  *
6  * Copyright (c) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  * Author(s): Thomas Spatzier (tspat@de.ibm.com)
8  *
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 <crypto/algapi.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20
21 #include "crypt_s390.h"
22 #include "crypto_des.h"
23
24 #define DES_BLOCK_SIZE 8
25 #define DES_KEY_SIZE 8
26
27 #define DES3_128_KEY_SIZE       (2 * DES_KEY_SIZE)
28 #define DES3_128_BLOCK_SIZE     DES_BLOCK_SIZE
29
30 #define DES3_192_KEY_SIZE       (3 * DES_KEY_SIZE)
31 #define DES3_192_BLOCK_SIZE     DES_BLOCK_SIZE
32
33 struct crypt_s390_des_ctx {
34         u8 iv[DES_BLOCK_SIZE];
35         u8 key[DES_KEY_SIZE];
36 };
37
38 struct crypt_s390_des3_128_ctx {
39         u8 iv[DES_BLOCK_SIZE];
40         u8 key[DES3_128_KEY_SIZE];
41 };
42
43 struct crypt_s390_des3_192_ctx {
44         u8 iv[DES_BLOCK_SIZE];
45         u8 key[DES3_192_KEY_SIZE];
46 };
47
48 static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
49                       unsigned int keylen)
50 {
51         struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
52         u32 *flags = &tfm->crt_flags;
53         int ret;
54
55         /* test if key is valid (not a weak key) */
56         ret = crypto_des_check_key(key, keylen, flags);
57         if (ret == 0)
58                 memcpy(dctx->key, key, keylen);
59         return ret;
60 }
61
62 static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
63 {
64         struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
65
66         crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
67 }
68
69 static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
70 {
71         struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
72
73         crypt_s390_km(KM_DEA_DECRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
74 }
75
76 static unsigned int des_encrypt_ecb(const struct cipher_desc *desc, u8 *out,
77                                     const u8 *in, unsigned int nbytes)
78 {
79         struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
80         int ret;
81
82         /* only use complete blocks */
83         nbytes &= ~(DES_BLOCK_SIZE - 1);
84         ret = crypt_s390_km(KM_DEA_ENCRYPT, sctx->key, out, in, nbytes);
85         BUG_ON((ret < 0) || (ret != nbytes));
86
87         return nbytes;
88 }
89
90 static unsigned int des_decrypt_ecb(const struct cipher_desc *desc, u8 *out,
91                                     const u8 *in, unsigned int nbytes)
92 {
93         struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
94         int ret;
95
96         /* only use complete blocks */
97         nbytes &= ~(DES_BLOCK_SIZE - 1);
98         ret = crypt_s390_km(KM_DEA_DECRYPT, sctx->key, out, in, nbytes);
99         BUG_ON((ret < 0) || (ret != nbytes));
100
101         return nbytes;
102 }
103
104 static unsigned int des_encrypt_cbc(const struct cipher_desc *desc, u8 *out,
105                                     const u8 *in, unsigned int nbytes)
106 {
107         struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
108         int ret;
109
110         /* only use complete blocks */
111         nbytes &= ~(DES_BLOCK_SIZE - 1);
112
113         memcpy(sctx->iv, desc->info, DES_BLOCK_SIZE);
114         ret = crypt_s390_kmc(KMC_DEA_ENCRYPT, &sctx->iv, out, in, nbytes);
115         BUG_ON((ret < 0) || (ret != nbytes));
116
117         memcpy(desc->info, sctx->iv, DES_BLOCK_SIZE);
118         return nbytes;
119 }
120
121 static unsigned int des_decrypt_cbc(const struct cipher_desc *desc, u8 *out,
122                                     const u8 *in, unsigned int nbytes)
123 {
124         struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
125         int ret;
126
127         /* only use complete blocks */
128         nbytes &= ~(DES_BLOCK_SIZE - 1);
129
130         memcpy(&sctx->iv, desc->info, DES_BLOCK_SIZE);
131         ret = crypt_s390_kmc(KMC_DEA_DECRYPT, &sctx->iv, out, in, nbytes);
132         BUG_ON((ret < 0) || (ret != nbytes));
133
134         return nbytes;
135 }
136
137 static struct crypto_alg des_alg = {
138         .cra_name               =       "des",
139         .cra_driver_name        =       "des-s390",
140         .cra_priority           =       CRYPT_S390_PRIORITY,
141         .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
142         .cra_blocksize          =       DES_BLOCK_SIZE,
143         .cra_ctxsize            =       sizeof(struct crypt_s390_des_ctx),
144         .cra_module             =       THIS_MODULE,
145         .cra_list               =       LIST_HEAD_INIT(des_alg.cra_list),
146         .cra_u                  =       {
147                 .cipher = {
148                         .cia_min_keysize        =       DES_KEY_SIZE,
149                         .cia_max_keysize        =       DES_KEY_SIZE,
150                         .cia_setkey             =       des_setkey,
151                         .cia_encrypt            =       des_encrypt,
152                         .cia_decrypt            =       des_decrypt,
153                         .cia_encrypt_ecb        =       des_encrypt_ecb,
154                         .cia_decrypt_ecb        =       des_decrypt_ecb,
155                         .cia_encrypt_cbc        =       des_encrypt_cbc,
156                         .cia_decrypt_cbc        =       des_decrypt_cbc,
157                 }
158         }
159 };
160
161 static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
162                             void *param, struct blkcipher_walk *walk)
163 {
164         int ret = blkcipher_walk_virt(desc, walk);
165         unsigned int nbytes;
166
167         while ((nbytes = walk->nbytes)) {
168                 /* only use complete blocks */
169                 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
170                 u8 *out = walk->dst.virt.addr;
171                 u8 *in = walk->src.virt.addr;
172
173                 ret = crypt_s390_km(func, param, out, in, n);
174                 BUG_ON((ret < 0) || (ret != n));
175
176                 nbytes &= DES_BLOCK_SIZE - 1;
177                 ret = blkcipher_walk_done(desc, walk, nbytes);
178         }
179
180         return ret;
181 }
182
183 static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
184                             void *param, struct blkcipher_walk *walk)
185 {
186         int ret = blkcipher_walk_virt(desc, walk);
187         unsigned int nbytes = walk->nbytes;
188
189         if (!nbytes)
190                 goto out;
191
192         memcpy(param, walk->iv, DES_BLOCK_SIZE);
193         do {
194                 /* only use complete blocks */
195                 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
196                 u8 *out = walk->dst.virt.addr;
197                 u8 *in = walk->src.virt.addr;
198
199                 ret = crypt_s390_kmc(func, param, out, in, n);
200                 BUG_ON((ret < 0) || (ret != n));
201
202                 nbytes &= DES_BLOCK_SIZE - 1;
203                 ret = blkcipher_walk_done(desc, walk, nbytes);
204         } while ((nbytes = walk->nbytes));
205         memcpy(walk->iv, param, DES_BLOCK_SIZE);
206
207 out:
208         return ret;
209 }
210
211 static int ecb_des_encrypt(struct blkcipher_desc *desc,
212                            struct scatterlist *dst, struct scatterlist *src,
213                            unsigned int nbytes)
214 {
215         struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
216         struct blkcipher_walk walk;
217
218         blkcipher_walk_init(&walk, dst, src, nbytes);
219         return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, sctx->key, &walk);
220 }
221
222 static int ecb_des_decrypt(struct blkcipher_desc *desc,
223                            struct scatterlist *dst, struct scatterlist *src,
224                            unsigned int nbytes)
225 {
226         struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
227         struct blkcipher_walk walk;
228
229         blkcipher_walk_init(&walk, dst, src, nbytes);
230         return ecb_desall_crypt(desc, KM_DEA_DECRYPT, sctx->key, &walk);
231 }
232
233 static struct crypto_alg ecb_des_alg = {
234         .cra_name               =       "ecb(des)",
235         .cra_driver_name        =       "ecb-des-s390",
236         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
237         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
238         .cra_blocksize          =       DES_BLOCK_SIZE,
239         .cra_ctxsize            =       sizeof(struct crypt_s390_des_ctx),
240         .cra_type               =       &crypto_blkcipher_type,
241         .cra_module             =       THIS_MODULE,
242         .cra_list               =       LIST_HEAD_INIT(ecb_des_alg.cra_list),
243         .cra_u                  =       {
244                 .blkcipher = {
245                         .min_keysize            =       DES_KEY_SIZE,
246                         .max_keysize            =       DES_KEY_SIZE,
247                         .setkey                 =       des_setkey,
248                         .encrypt                =       ecb_des_encrypt,
249                         .decrypt                =       ecb_des_decrypt,
250                 }
251         }
252 };
253
254 static int cbc_des_encrypt(struct blkcipher_desc *desc,
255                            struct scatterlist *dst, struct scatterlist *src,
256                            unsigned int nbytes)
257 {
258         struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
259         struct blkcipher_walk walk;
260
261         blkcipher_walk_init(&walk, dst, src, nbytes);
262         return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, sctx->iv, &walk);
263 }
264
265 static int cbc_des_decrypt(struct blkcipher_desc *desc,
266                            struct scatterlist *dst, struct scatterlist *src,
267                            unsigned int nbytes)
268 {
269         struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
270         struct blkcipher_walk walk;
271
272         blkcipher_walk_init(&walk, dst, src, nbytes);
273         return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, sctx->iv, &walk);
274 }
275
276 static struct crypto_alg cbc_des_alg = {
277         .cra_name               =       "cbc(des)",
278         .cra_driver_name        =       "cbc-des-s390",
279         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
280         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
281         .cra_blocksize          =       DES_BLOCK_SIZE,
282         .cra_ctxsize            =       sizeof(struct crypt_s390_des_ctx),
283         .cra_type               =       &crypto_blkcipher_type,
284         .cra_module             =       THIS_MODULE,
285         .cra_list               =       LIST_HEAD_INIT(cbc_des_alg.cra_list),
286         .cra_u                  =       {
287                 .blkcipher = {
288                         .min_keysize            =       DES_KEY_SIZE,
289                         .max_keysize            =       DES_KEY_SIZE,
290                         .ivsize                 =       DES_BLOCK_SIZE,
291                         .setkey                 =       des_setkey,
292                         .encrypt                =       cbc_des_encrypt,
293                         .decrypt                =       cbc_des_decrypt,
294                 }
295         }
296 };
297
298 /*
299  * RFC2451:
300  *
301  *   For DES-EDE3, there is no known need to reject weak or
302  *   complementation keys.  Any weakness is obviated by the use of
303  *   multiple keys.
304  *
305  *   However, if the two  independent 64-bit keys are equal,
306  *   then the DES3 operation is simply the same as DES.
307  *   Implementers MUST reject keys that exhibit this property.
308  *
309  */
310 static int des3_128_setkey(struct crypto_tfm *tfm, const u8 *key,
311                            unsigned int keylen)
312 {
313         int i, ret;
314         struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
315         const u8 *temp_key = key;
316         u32 *flags = &tfm->crt_flags;
317
318         if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
319                 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
320                 return -EINVAL;
321         }
322         for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
323                 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
324                 if (ret < 0)
325                         return ret;
326         }
327         memcpy(dctx->key, key, keylen);
328         return 0;
329 }
330
331 static void des3_128_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
332 {
333         struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
334
335         crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
336                       DES3_128_BLOCK_SIZE);
337 }
338
339 static void des3_128_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
340 {
341         struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
342
343         crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
344                       DES3_128_BLOCK_SIZE);
345 }
346
347 static unsigned int des3_128_encrypt_ecb(const struct cipher_desc *desc,
348                                          u8 *out, const u8 *in,
349                                          unsigned int nbytes)
350 {
351         struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
352         int ret;
353
354         /* only use complete blocks */
355         nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
356         ret = crypt_s390_km(KM_TDEA_128_ENCRYPT, sctx->key, out, in, nbytes);
357         BUG_ON((ret < 0) || (ret != nbytes));
358
359         return nbytes;
360 }
361
362 static unsigned int des3_128_decrypt_ecb(const struct cipher_desc *desc,
363                                          u8 *out, const u8 *in,
364                                          unsigned int nbytes)
365 {
366         struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
367         int ret;
368
369         /* only use complete blocks */
370         nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
371         ret = crypt_s390_km(KM_TDEA_128_DECRYPT, sctx->key, out, in, nbytes);
372         BUG_ON((ret < 0) || (ret != nbytes));
373
374         return nbytes;
375 }
376
377 static unsigned int des3_128_encrypt_cbc(const struct cipher_desc *desc,
378                                          u8 *out, const u8 *in,
379                                          unsigned int nbytes)
380 {
381         struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
382         int ret;
383
384         /* only use complete blocks */
385         nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
386
387         memcpy(sctx->iv, desc->info, DES3_128_BLOCK_SIZE);
388         ret = crypt_s390_kmc(KMC_TDEA_128_ENCRYPT, &sctx->iv, out, in, nbytes);
389         BUG_ON((ret < 0) || (ret != nbytes));
390
391         memcpy(desc->info, sctx->iv, DES3_128_BLOCK_SIZE);
392         return nbytes;
393 }
394
395 static unsigned int des3_128_decrypt_cbc(const struct cipher_desc *desc,
396                                          u8 *out, const u8 *in,
397                                          unsigned int nbytes)
398 {
399         struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
400         int ret;
401
402         /* only use complete blocks */
403         nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
404
405         memcpy(&sctx->iv, desc->info, DES3_128_BLOCK_SIZE);
406         ret = crypt_s390_kmc(KMC_TDEA_128_DECRYPT, &sctx->iv, out, in, nbytes);
407         BUG_ON((ret < 0) || (ret != nbytes));
408
409         return nbytes;
410 }
411
412 static struct crypto_alg des3_128_alg = {
413         .cra_name               =       "des3_ede128",
414         .cra_driver_name        =       "des3_ede128-s390",
415         .cra_priority           =       CRYPT_S390_PRIORITY,
416         .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
417         .cra_blocksize          =       DES3_128_BLOCK_SIZE,
418         .cra_ctxsize            =       sizeof(struct crypt_s390_des3_128_ctx),
419         .cra_module             =       THIS_MODULE,
420         .cra_list               =       LIST_HEAD_INIT(des3_128_alg.cra_list),
421         .cra_u                  =       {
422                 .cipher = {
423                         .cia_min_keysize        =       DES3_128_KEY_SIZE,
424                         .cia_max_keysize        =       DES3_128_KEY_SIZE,
425                         .cia_setkey             =       des3_128_setkey,
426                         .cia_encrypt            =       des3_128_encrypt,
427                         .cia_decrypt            =       des3_128_decrypt,
428                         .cia_encrypt_ecb        =       des3_128_encrypt_ecb,
429                         .cia_decrypt_ecb        =       des3_128_decrypt_ecb,
430                         .cia_encrypt_cbc        =       des3_128_encrypt_cbc,
431                         .cia_decrypt_cbc        =       des3_128_decrypt_cbc,
432                 }
433         }
434 };
435
436 static int ecb_des3_128_encrypt(struct blkcipher_desc *desc,
437                                 struct scatterlist *dst,
438                                 struct scatterlist *src, unsigned int nbytes)
439 {
440         struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
441         struct blkcipher_walk walk;
442
443         blkcipher_walk_init(&walk, dst, src, nbytes);
444         return ecb_desall_crypt(desc, KM_TDEA_128_ENCRYPT, sctx->key, &walk);
445 }
446
447 static int ecb_des3_128_decrypt(struct blkcipher_desc *desc,
448                                 struct scatterlist *dst,
449                                 struct scatterlist *src, unsigned int nbytes)
450 {
451         struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
452         struct blkcipher_walk walk;
453
454         blkcipher_walk_init(&walk, dst, src, nbytes);
455         return ecb_desall_crypt(desc, KM_TDEA_128_DECRYPT, sctx->key, &walk);
456 }
457
458 static struct crypto_alg ecb_des3_128_alg = {
459         .cra_name               =       "ecb(des3_ede128)",
460         .cra_driver_name        =       "ecb-des3_ede128-s390",
461         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
462         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
463         .cra_blocksize          =       DES3_128_BLOCK_SIZE,
464         .cra_ctxsize            =       sizeof(struct crypt_s390_des3_128_ctx),
465         .cra_type               =       &crypto_blkcipher_type,
466         .cra_module             =       THIS_MODULE,
467         .cra_list               =       LIST_HEAD_INIT(
468                                                 ecb_des3_128_alg.cra_list),
469         .cra_u                  =       {
470                 .blkcipher = {
471                         .min_keysize            =       DES3_128_KEY_SIZE,
472                         .max_keysize            =       DES3_128_KEY_SIZE,
473                         .setkey                 =       des3_128_setkey,
474                         .encrypt                =       ecb_des3_128_encrypt,
475                         .decrypt                =       ecb_des3_128_decrypt,
476                 }
477         }
478 };
479
480 static int cbc_des3_128_encrypt(struct blkcipher_desc *desc,
481                                 struct scatterlist *dst,
482                                 struct scatterlist *src, unsigned int nbytes)
483 {
484         struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
485         struct blkcipher_walk walk;
486
487         blkcipher_walk_init(&walk, dst, src, nbytes);
488         return cbc_desall_crypt(desc, KMC_TDEA_128_ENCRYPT, sctx->iv, &walk);
489 }
490
491 static int cbc_des3_128_decrypt(struct blkcipher_desc *desc,
492                                 struct scatterlist *dst,
493                                 struct scatterlist *src, unsigned int nbytes)
494 {
495         struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
496         struct blkcipher_walk walk;
497
498         blkcipher_walk_init(&walk, dst, src, nbytes);
499         return cbc_desall_crypt(desc, KMC_TDEA_128_DECRYPT, sctx->iv, &walk);
500 }
501
502 static struct crypto_alg cbc_des3_128_alg = {
503         .cra_name               =       "cbc(des3_ede128)",
504         .cra_driver_name        =       "cbc-des3_ede128-s390",
505         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
506         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
507         .cra_blocksize          =       DES3_128_BLOCK_SIZE,
508         .cra_ctxsize            =       sizeof(struct crypt_s390_des3_128_ctx),
509         .cra_type               =       &crypto_blkcipher_type,
510         .cra_module             =       THIS_MODULE,
511         .cra_list               =       LIST_HEAD_INIT(
512                                                 cbc_des3_128_alg.cra_list),
513         .cra_u                  =       {
514                 .blkcipher = {
515                         .min_keysize            =       DES3_128_KEY_SIZE,
516                         .max_keysize            =       DES3_128_KEY_SIZE,
517                         .ivsize                 =       DES3_128_BLOCK_SIZE,
518                         .setkey                 =       des3_128_setkey,
519                         .encrypt                =       cbc_des3_128_encrypt,
520                         .decrypt                =       cbc_des3_128_decrypt,
521                 }
522         }
523 };
524
525 /*
526  * RFC2451:
527  *
528  *   For DES-EDE3, there is no known need to reject weak or
529  *   complementation keys.  Any weakness is obviated by the use of
530  *   multiple keys.
531  *
532  *   However, if the first two or last two independent 64-bit keys are
533  *   equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
534  *   same as DES.  Implementers MUST reject keys that exhibit this
535  *   property.
536  *
537  */
538 static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
539                            unsigned int keylen)
540 {
541         int i, ret;
542         struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
543         const u8 *temp_key = key;
544         u32 *flags = &tfm->crt_flags;
545
546         if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
547             memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
548                    DES_KEY_SIZE))) {
549
550                 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
551                 return -EINVAL;
552         }
553         for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
554                 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
555                 if (ret < 0)
556                         return ret;
557         }
558         memcpy(dctx->key, key, keylen);
559         return 0;
560 }
561
562 static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
563 {
564         struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
565
566         crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
567                       DES3_192_BLOCK_SIZE);
568 }
569
570 static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
571 {
572         struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
573
574         crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
575                       DES3_192_BLOCK_SIZE);
576 }
577
578 static unsigned int des3_192_encrypt_ecb(const struct cipher_desc *desc,
579                                          u8 *out, const u8 *in,
580                                          unsigned int nbytes)
581 {
582         struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
583         int ret;
584
585         /* only use complete blocks */
586         nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
587         ret = crypt_s390_km(KM_TDEA_192_ENCRYPT, sctx->key, out, in, nbytes);
588         BUG_ON((ret < 0) || (ret != nbytes));
589
590         return nbytes;
591 }
592
593 static unsigned int des3_192_decrypt_ecb(const struct cipher_desc *desc,
594                                          u8 *out, const u8 *in,
595                                          unsigned int nbytes)
596 {
597         struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
598         int ret;
599
600         /* only use complete blocks */
601         nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
602         ret = crypt_s390_km(KM_TDEA_192_DECRYPT, sctx->key, out, in, nbytes);
603         BUG_ON((ret < 0) || (ret != nbytes));
604
605         return nbytes;
606 }
607
608 static unsigned int des3_192_encrypt_cbc(const struct cipher_desc *desc,
609                                          u8 *out, const u8 *in,
610                                          unsigned int nbytes)
611 {
612         struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
613         int ret;
614
615         /* only use complete blocks */
616         nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
617
618         memcpy(sctx->iv, desc->info, DES3_192_BLOCK_SIZE);
619         ret = crypt_s390_kmc(KMC_TDEA_192_ENCRYPT, &sctx->iv, out, in, nbytes);
620         BUG_ON((ret < 0) || (ret != nbytes));
621
622         memcpy(desc->info, sctx->iv, DES3_192_BLOCK_SIZE);
623         return nbytes;
624 }
625
626 static unsigned int des3_192_decrypt_cbc(const struct cipher_desc *desc,
627                                          u8 *out, const u8 *in,
628                                          unsigned int nbytes)
629 {
630         struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
631         int ret;
632
633         /* only use complete blocks */
634         nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
635
636         memcpy(&sctx->iv, desc->info, DES3_192_BLOCK_SIZE);
637         ret = crypt_s390_kmc(KMC_TDEA_192_DECRYPT, &sctx->iv, out, in, nbytes);
638         BUG_ON((ret < 0) || (ret != nbytes));
639
640         return nbytes;
641 }
642
643 static struct crypto_alg des3_192_alg = {
644         .cra_name               =       "des3_ede",
645         .cra_driver_name        =       "des3_ede-s390",
646         .cra_priority           =       CRYPT_S390_PRIORITY,
647         .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
648         .cra_blocksize          =       DES3_192_BLOCK_SIZE,
649         .cra_ctxsize            =       sizeof(struct crypt_s390_des3_192_ctx),
650         .cra_module             =       THIS_MODULE,
651         .cra_list               =       LIST_HEAD_INIT(des3_192_alg.cra_list),
652         .cra_u                  =       {
653                 .cipher = {
654                         .cia_min_keysize        =       DES3_192_KEY_SIZE,
655                         .cia_max_keysize        =       DES3_192_KEY_SIZE,
656                         .cia_setkey             =       des3_192_setkey,
657                         .cia_encrypt            =       des3_192_encrypt,
658                         .cia_decrypt            =       des3_192_decrypt,
659                         .cia_encrypt_ecb        =       des3_192_encrypt_ecb,
660                         .cia_decrypt_ecb        =       des3_192_decrypt_ecb,
661                         .cia_encrypt_cbc        =       des3_192_encrypt_cbc,
662                         .cia_decrypt_cbc        =       des3_192_decrypt_cbc,
663                 }
664         }
665 };
666
667 static int ecb_des3_192_encrypt(struct blkcipher_desc *desc,
668                                 struct scatterlist *dst,
669                                 struct scatterlist *src, unsigned int nbytes)
670 {
671         struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
672         struct blkcipher_walk walk;
673
674         blkcipher_walk_init(&walk, dst, src, nbytes);
675         return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, sctx->key, &walk);
676 }
677
678 static int ecb_des3_192_decrypt(struct blkcipher_desc *desc,
679                                 struct scatterlist *dst,
680                                 struct scatterlist *src, unsigned int nbytes)
681 {
682         struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
683         struct blkcipher_walk walk;
684
685         blkcipher_walk_init(&walk, dst, src, nbytes);
686         return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, sctx->key, &walk);
687 }
688
689 static struct crypto_alg ecb_des3_192_alg = {
690         .cra_name               =       "ecb(des3_ede)",
691         .cra_driver_name        =       "ecb-des3_ede-s390",
692         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
693         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
694         .cra_blocksize          =       DES3_192_BLOCK_SIZE,
695         .cra_ctxsize            =       sizeof(struct crypt_s390_des3_192_ctx),
696         .cra_type               =       &crypto_blkcipher_type,
697         .cra_module             =       THIS_MODULE,
698         .cra_list               =       LIST_HEAD_INIT(
699                                                 ecb_des3_192_alg.cra_list),
700         .cra_u                  =       {
701                 .blkcipher = {
702                         .min_keysize            =       DES3_192_KEY_SIZE,
703                         .max_keysize            =       DES3_192_KEY_SIZE,
704                         .setkey                 =       des3_192_setkey,
705                         .encrypt                =       ecb_des3_192_encrypt,
706                         .decrypt                =       ecb_des3_192_decrypt,
707                 }
708         }
709 };
710
711 static int cbc_des3_192_encrypt(struct blkcipher_desc *desc,
712                                 struct scatterlist *dst,
713                                 struct scatterlist *src, unsigned int nbytes)
714 {
715         struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
716         struct blkcipher_walk walk;
717
718         blkcipher_walk_init(&walk, dst, src, nbytes);
719         return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, sctx->iv, &walk);
720 }
721
722 static int cbc_des3_192_decrypt(struct blkcipher_desc *desc,
723                                 struct scatterlist *dst,
724                                 struct scatterlist *src, unsigned int nbytes)
725 {
726         struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
727         struct blkcipher_walk walk;
728
729         blkcipher_walk_init(&walk, dst, src, nbytes);
730         return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, sctx->iv, &walk);
731 }
732
733 static struct crypto_alg cbc_des3_192_alg = {
734         .cra_name               =       "cbc(des3_ede)",
735         .cra_driver_name        =       "cbc-des3_ede-s390",
736         .cra_priority           =       CRYPT_S390_COMPOSITE_PRIORITY,
737         .cra_flags              =       CRYPTO_ALG_TYPE_BLKCIPHER,
738         .cra_blocksize          =       DES3_192_BLOCK_SIZE,
739         .cra_ctxsize            =       sizeof(struct crypt_s390_des3_192_ctx),
740         .cra_type               =       &crypto_blkcipher_type,
741         .cra_module             =       THIS_MODULE,
742         .cra_list               =       LIST_HEAD_INIT(
743                                                 cbc_des3_192_alg.cra_list),
744         .cra_u                  =       {
745                 .blkcipher = {
746                         .min_keysize            =       DES3_192_KEY_SIZE,
747                         .max_keysize            =       DES3_192_KEY_SIZE,
748                         .ivsize                 =       DES3_192_BLOCK_SIZE,
749                         .setkey                 =       des3_192_setkey,
750                         .encrypt                =       cbc_des3_192_encrypt,
751                         .decrypt                =       cbc_des3_192_decrypt,
752                 }
753         }
754 };
755
756 static int init(void)
757 {
758         int ret = 0;
759
760         if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
761             !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) ||
762             !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
763                 return -ENOSYS;
764
765         ret = crypto_register_alg(&des_alg);
766         if (ret)
767                 goto des_err;
768         ret = crypto_register_alg(&ecb_des_alg);
769         if (ret)
770                 goto ecb_des_err;
771         ret = crypto_register_alg(&cbc_des_alg);
772         if (ret)
773                 goto cbc_des_err;
774
775         ret = crypto_register_alg(&des3_128_alg);
776         if (ret)
777                 goto des3_128_err;
778         ret = crypto_register_alg(&ecb_des3_128_alg);
779         if (ret)
780                 goto ecb_des3_128_err;
781         ret = crypto_register_alg(&cbc_des3_128_alg);
782         if (ret)
783                 goto cbc_des3_128_err;
784
785         ret = crypto_register_alg(&des3_192_alg);
786         if (ret)
787                 goto des3_192_err;
788         ret = crypto_register_alg(&ecb_des3_192_alg);
789         if (ret)
790                 goto ecb_des3_192_err;
791         ret = crypto_register_alg(&cbc_des3_192_alg);
792         if (ret)
793                 goto cbc_des3_192_err;
794
795 out:
796         return ret;
797
798 cbc_des3_192_err:
799         crypto_unregister_alg(&ecb_des3_192_alg);
800 ecb_des3_192_err:
801         crypto_unregister_alg(&des3_192_alg);
802 des3_192_err:
803         crypto_unregister_alg(&cbc_des3_128_alg);
804 cbc_des3_128_err:
805         crypto_unregister_alg(&ecb_des3_128_alg);
806 ecb_des3_128_err:
807         crypto_unregister_alg(&des3_128_alg);
808 des3_128_err:
809         crypto_unregister_alg(&cbc_des_alg);
810 cbc_des_err:
811         crypto_unregister_alg(&ecb_des_alg);
812 ecb_des_err:
813         crypto_unregister_alg(&des_alg);
814 des_err:
815         goto out;
816 }
817
818 static void __exit fini(void)
819 {
820         crypto_unregister_alg(&cbc_des3_192_alg);
821         crypto_unregister_alg(&ecb_des3_192_alg);
822         crypto_unregister_alg(&des3_192_alg);
823         crypto_unregister_alg(&cbc_des3_128_alg);
824         crypto_unregister_alg(&ecb_des3_128_alg);
825         crypto_unregister_alg(&des3_128_alg);
826         crypto_unregister_alg(&cbc_des_alg);
827         crypto_unregister_alg(&ecb_des_alg);
828         crypto_unregister_alg(&des_alg);
829 }
830
831 module_init(init);
832 module_exit(fini);
833
834 MODULE_ALIAS("des");
835 MODULE_ALIAS("des3_ede");
836
837 MODULE_LICENSE("GPL");
838 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");