4 * s390 implementation of the DES Cipher Algorithm.
6 * Copyright (c) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Thomas Spatzier (tspat@de.ibm.com)
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.
17 #include <crypto/algapi.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
21 #include "crypt_s390.h"
22 #include "crypto_des.h"
24 #define DES_BLOCK_SIZE 8
25 #define DES_KEY_SIZE 8
27 #define DES3_128_KEY_SIZE (2 * DES_KEY_SIZE)
28 #define DES3_128_BLOCK_SIZE DES_BLOCK_SIZE
30 #define DES3_192_KEY_SIZE (3 * DES_KEY_SIZE)
31 #define DES3_192_BLOCK_SIZE DES_BLOCK_SIZE
33 struct crypt_s390_des_ctx {
34 u8 iv[DES_BLOCK_SIZE];
38 struct crypt_s390_des3_128_ctx {
39 u8 iv[DES_BLOCK_SIZE];
40 u8 key[DES3_128_KEY_SIZE];
43 struct crypt_s390_des3_192_ctx {
44 u8 iv[DES_BLOCK_SIZE];
45 u8 key[DES3_192_KEY_SIZE];
48 static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
51 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
52 u32 *flags = &tfm->crt_flags;
55 /* test if key is valid (not a weak key) */
56 ret = crypto_des_check_key(key, keylen, flags);
58 memcpy(dctx->key, key, keylen);
62 static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
64 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
66 crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
69 static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
71 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
73 crypt_s390_km(KM_DEA_DECRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
76 static struct crypto_alg des_alg = {
78 .cra_driver_name = "des-s390",
79 .cra_priority = CRYPT_S390_PRIORITY,
80 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
81 .cra_blocksize = DES_BLOCK_SIZE,
82 .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
83 .cra_module = THIS_MODULE,
84 .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
87 .cia_min_keysize = DES_KEY_SIZE,
88 .cia_max_keysize = DES_KEY_SIZE,
89 .cia_setkey = des_setkey,
90 .cia_encrypt = des_encrypt,
91 .cia_decrypt = des_decrypt,
96 static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
97 void *param, struct blkcipher_walk *walk)
99 int ret = blkcipher_walk_virt(desc, walk);
102 while ((nbytes = walk->nbytes)) {
103 /* only use complete blocks */
104 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
105 u8 *out = walk->dst.virt.addr;
106 u8 *in = walk->src.virt.addr;
108 ret = crypt_s390_km(func, param, out, in, n);
109 BUG_ON((ret < 0) || (ret != n));
111 nbytes &= DES_BLOCK_SIZE - 1;
112 ret = blkcipher_walk_done(desc, walk, nbytes);
118 static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
119 void *param, struct blkcipher_walk *walk)
121 int ret = blkcipher_walk_virt(desc, walk);
122 unsigned int nbytes = walk->nbytes;
127 memcpy(param, walk->iv, DES_BLOCK_SIZE);
129 /* only use complete blocks */
130 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
131 u8 *out = walk->dst.virt.addr;
132 u8 *in = walk->src.virt.addr;
134 ret = crypt_s390_kmc(func, param, out, in, n);
135 BUG_ON((ret < 0) || (ret != n));
137 nbytes &= DES_BLOCK_SIZE - 1;
138 ret = blkcipher_walk_done(desc, walk, nbytes);
139 } while ((nbytes = walk->nbytes));
140 memcpy(walk->iv, param, DES_BLOCK_SIZE);
146 static int ecb_des_encrypt(struct blkcipher_desc *desc,
147 struct scatterlist *dst, struct scatterlist *src,
150 struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
151 struct blkcipher_walk walk;
153 blkcipher_walk_init(&walk, dst, src, nbytes);
154 return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, sctx->key, &walk);
157 static int ecb_des_decrypt(struct blkcipher_desc *desc,
158 struct scatterlist *dst, struct scatterlist *src,
161 struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
162 struct blkcipher_walk walk;
164 blkcipher_walk_init(&walk, dst, src, nbytes);
165 return ecb_desall_crypt(desc, KM_DEA_DECRYPT, sctx->key, &walk);
168 static struct crypto_alg ecb_des_alg = {
169 .cra_name = "ecb(des)",
170 .cra_driver_name = "ecb-des-s390",
171 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
172 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
173 .cra_blocksize = DES_BLOCK_SIZE,
174 .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
175 .cra_type = &crypto_blkcipher_type,
176 .cra_module = THIS_MODULE,
177 .cra_list = LIST_HEAD_INIT(ecb_des_alg.cra_list),
180 .min_keysize = DES_KEY_SIZE,
181 .max_keysize = DES_KEY_SIZE,
182 .setkey = des_setkey,
183 .encrypt = ecb_des_encrypt,
184 .decrypt = ecb_des_decrypt,
189 static int cbc_des_encrypt(struct blkcipher_desc *desc,
190 struct scatterlist *dst, struct scatterlist *src,
193 struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
194 struct blkcipher_walk walk;
196 blkcipher_walk_init(&walk, dst, src, nbytes);
197 return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, sctx->iv, &walk);
200 static int cbc_des_decrypt(struct blkcipher_desc *desc,
201 struct scatterlist *dst, struct scatterlist *src,
204 struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
205 struct blkcipher_walk walk;
207 blkcipher_walk_init(&walk, dst, src, nbytes);
208 return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, sctx->iv, &walk);
211 static struct crypto_alg cbc_des_alg = {
212 .cra_name = "cbc(des)",
213 .cra_driver_name = "cbc-des-s390",
214 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
215 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
216 .cra_blocksize = DES_BLOCK_SIZE,
217 .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
218 .cra_type = &crypto_blkcipher_type,
219 .cra_module = THIS_MODULE,
220 .cra_list = LIST_HEAD_INIT(cbc_des_alg.cra_list),
223 .min_keysize = DES_KEY_SIZE,
224 .max_keysize = DES_KEY_SIZE,
225 .ivsize = DES_BLOCK_SIZE,
226 .setkey = des_setkey,
227 .encrypt = cbc_des_encrypt,
228 .decrypt = cbc_des_decrypt,
236 * For DES-EDE3, there is no known need to reject weak or
237 * complementation keys. Any weakness is obviated by the use of
240 * However, if the two independent 64-bit keys are equal,
241 * then the DES3 operation is simply the same as DES.
242 * Implementers MUST reject keys that exhibit this property.
245 static int des3_128_setkey(struct crypto_tfm *tfm, const u8 *key,
249 struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
250 const u8 *temp_key = key;
251 u32 *flags = &tfm->crt_flags;
253 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
254 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
257 for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
258 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
262 memcpy(dctx->key, key, keylen);
266 static void des3_128_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
268 struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
270 crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
271 DES3_128_BLOCK_SIZE);
274 static void des3_128_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
276 struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
278 crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
279 DES3_128_BLOCK_SIZE);
282 static struct crypto_alg des3_128_alg = {
283 .cra_name = "des3_ede128",
284 .cra_driver_name = "des3_ede128-s390",
285 .cra_priority = CRYPT_S390_PRIORITY,
286 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
287 .cra_blocksize = DES3_128_BLOCK_SIZE,
288 .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
289 .cra_module = THIS_MODULE,
290 .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list),
293 .cia_min_keysize = DES3_128_KEY_SIZE,
294 .cia_max_keysize = DES3_128_KEY_SIZE,
295 .cia_setkey = des3_128_setkey,
296 .cia_encrypt = des3_128_encrypt,
297 .cia_decrypt = des3_128_decrypt,
302 static int ecb_des3_128_encrypt(struct blkcipher_desc *desc,
303 struct scatterlist *dst,
304 struct scatterlist *src, unsigned int nbytes)
306 struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
307 struct blkcipher_walk walk;
309 blkcipher_walk_init(&walk, dst, src, nbytes);
310 return ecb_desall_crypt(desc, KM_TDEA_128_ENCRYPT, sctx->key, &walk);
313 static int ecb_des3_128_decrypt(struct blkcipher_desc *desc,
314 struct scatterlist *dst,
315 struct scatterlist *src, unsigned int nbytes)
317 struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
318 struct blkcipher_walk walk;
320 blkcipher_walk_init(&walk, dst, src, nbytes);
321 return ecb_desall_crypt(desc, KM_TDEA_128_DECRYPT, sctx->key, &walk);
324 static struct crypto_alg ecb_des3_128_alg = {
325 .cra_name = "ecb(des3_ede128)",
326 .cra_driver_name = "ecb-des3_ede128-s390",
327 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
328 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
329 .cra_blocksize = DES3_128_BLOCK_SIZE,
330 .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
331 .cra_type = &crypto_blkcipher_type,
332 .cra_module = THIS_MODULE,
333 .cra_list = LIST_HEAD_INIT(
334 ecb_des3_128_alg.cra_list),
337 .min_keysize = DES3_128_KEY_SIZE,
338 .max_keysize = DES3_128_KEY_SIZE,
339 .setkey = des3_128_setkey,
340 .encrypt = ecb_des3_128_encrypt,
341 .decrypt = ecb_des3_128_decrypt,
346 static int cbc_des3_128_encrypt(struct blkcipher_desc *desc,
347 struct scatterlist *dst,
348 struct scatterlist *src, unsigned int nbytes)
350 struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
351 struct blkcipher_walk walk;
353 blkcipher_walk_init(&walk, dst, src, nbytes);
354 return cbc_desall_crypt(desc, KMC_TDEA_128_ENCRYPT, sctx->iv, &walk);
357 static int cbc_des3_128_decrypt(struct blkcipher_desc *desc,
358 struct scatterlist *dst,
359 struct scatterlist *src, unsigned int nbytes)
361 struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
362 struct blkcipher_walk walk;
364 blkcipher_walk_init(&walk, dst, src, nbytes);
365 return cbc_desall_crypt(desc, KMC_TDEA_128_DECRYPT, sctx->iv, &walk);
368 static struct crypto_alg cbc_des3_128_alg = {
369 .cra_name = "cbc(des3_ede128)",
370 .cra_driver_name = "cbc-des3_ede128-s390",
371 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
372 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
373 .cra_blocksize = DES3_128_BLOCK_SIZE,
374 .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
375 .cra_type = &crypto_blkcipher_type,
376 .cra_module = THIS_MODULE,
377 .cra_list = LIST_HEAD_INIT(
378 cbc_des3_128_alg.cra_list),
381 .min_keysize = DES3_128_KEY_SIZE,
382 .max_keysize = DES3_128_KEY_SIZE,
383 .ivsize = DES3_128_BLOCK_SIZE,
384 .setkey = des3_128_setkey,
385 .encrypt = cbc_des3_128_encrypt,
386 .decrypt = cbc_des3_128_decrypt,
394 * For DES-EDE3, there is no known need to reject weak or
395 * complementation keys. Any weakness is obviated by the use of
398 * However, if the first two or last two independent 64-bit keys are
399 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
400 * same as DES. Implementers MUST reject keys that exhibit this
404 static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
408 struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
409 const u8 *temp_key = key;
410 u32 *flags = &tfm->crt_flags;
412 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
413 memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
416 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
419 for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
420 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
424 memcpy(dctx->key, key, keylen);
428 static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
430 struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
432 crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
433 DES3_192_BLOCK_SIZE);
436 static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
438 struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
440 crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
441 DES3_192_BLOCK_SIZE);
444 static struct crypto_alg des3_192_alg = {
445 .cra_name = "des3_ede",
446 .cra_driver_name = "des3_ede-s390",
447 .cra_priority = CRYPT_S390_PRIORITY,
448 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
449 .cra_blocksize = DES3_192_BLOCK_SIZE,
450 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
451 .cra_module = THIS_MODULE,
452 .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
455 .cia_min_keysize = DES3_192_KEY_SIZE,
456 .cia_max_keysize = DES3_192_KEY_SIZE,
457 .cia_setkey = des3_192_setkey,
458 .cia_encrypt = des3_192_encrypt,
459 .cia_decrypt = des3_192_decrypt,
464 static int ecb_des3_192_encrypt(struct blkcipher_desc *desc,
465 struct scatterlist *dst,
466 struct scatterlist *src, unsigned int nbytes)
468 struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
469 struct blkcipher_walk walk;
471 blkcipher_walk_init(&walk, dst, src, nbytes);
472 return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, sctx->key, &walk);
475 static int ecb_des3_192_decrypt(struct blkcipher_desc *desc,
476 struct scatterlist *dst,
477 struct scatterlist *src, unsigned int nbytes)
479 struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
480 struct blkcipher_walk walk;
482 blkcipher_walk_init(&walk, dst, src, nbytes);
483 return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, sctx->key, &walk);
486 static struct crypto_alg ecb_des3_192_alg = {
487 .cra_name = "ecb(des3_ede)",
488 .cra_driver_name = "ecb-des3_ede-s390",
489 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
490 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
491 .cra_blocksize = DES3_192_BLOCK_SIZE,
492 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
493 .cra_type = &crypto_blkcipher_type,
494 .cra_module = THIS_MODULE,
495 .cra_list = LIST_HEAD_INIT(
496 ecb_des3_192_alg.cra_list),
499 .min_keysize = DES3_192_KEY_SIZE,
500 .max_keysize = DES3_192_KEY_SIZE,
501 .setkey = des3_192_setkey,
502 .encrypt = ecb_des3_192_encrypt,
503 .decrypt = ecb_des3_192_decrypt,
508 static int cbc_des3_192_encrypt(struct blkcipher_desc *desc,
509 struct scatterlist *dst,
510 struct scatterlist *src, unsigned int nbytes)
512 struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
513 struct blkcipher_walk walk;
515 blkcipher_walk_init(&walk, dst, src, nbytes);
516 return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, sctx->iv, &walk);
519 static int cbc_des3_192_decrypt(struct blkcipher_desc *desc,
520 struct scatterlist *dst,
521 struct scatterlist *src, unsigned int nbytes)
523 struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
524 struct blkcipher_walk walk;
526 blkcipher_walk_init(&walk, dst, src, nbytes);
527 return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, sctx->iv, &walk);
530 static struct crypto_alg cbc_des3_192_alg = {
531 .cra_name = "cbc(des3_ede)",
532 .cra_driver_name = "cbc-des3_ede-s390",
533 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
534 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
535 .cra_blocksize = DES3_192_BLOCK_SIZE,
536 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
537 .cra_type = &crypto_blkcipher_type,
538 .cra_module = THIS_MODULE,
539 .cra_list = LIST_HEAD_INIT(
540 cbc_des3_192_alg.cra_list),
543 .min_keysize = DES3_192_KEY_SIZE,
544 .max_keysize = DES3_192_KEY_SIZE,
545 .ivsize = DES3_192_BLOCK_SIZE,
546 .setkey = des3_192_setkey,
547 .encrypt = cbc_des3_192_encrypt,
548 .decrypt = cbc_des3_192_decrypt,
553 static int init(void)
557 if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
558 !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) ||
559 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
562 ret = crypto_register_alg(&des_alg);
565 ret = crypto_register_alg(&ecb_des_alg);
568 ret = crypto_register_alg(&cbc_des_alg);
572 ret = crypto_register_alg(&des3_128_alg);
575 ret = crypto_register_alg(&ecb_des3_128_alg);
577 goto ecb_des3_128_err;
578 ret = crypto_register_alg(&cbc_des3_128_alg);
580 goto cbc_des3_128_err;
582 ret = crypto_register_alg(&des3_192_alg);
585 ret = crypto_register_alg(&ecb_des3_192_alg);
587 goto ecb_des3_192_err;
588 ret = crypto_register_alg(&cbc_des3_192_alg);
590 goto cbc_des3_192_err;
596 crypto_unregister_alg(&ecb_des3_192_alg);
598 crypto_unregister_alg(&des3_192_alg);
600 crypto_unregister_alg(&cbc_des3_128_alg);
602 crypto_unregister_alg(&ecb_des3_128_alg);
604 crypto_unregister_alg(&des3_128_alg);
606 crypto_unregister_alg(&cbc_des_alg);
608 crypto_unregister_alg(&ecb_des_alg);
610 crypto_unregister_alg(&des_alg);
615 static void __exit fini(void)
617 crypto_unregister_alg(&cbc_des3_192_alg);
618 crypto_unregister_alg(&ecb_des3_192_alg);
619 crypto_unregister_alg(&des3_192_alg);
620 crypto_unregister_alg(&cbc_des3_128_alg);
621 crypto_unregister_alg(&ecb_des3_128_alg);
622 crypto_unregister_alg(&des3_128_alg);
623 crypto_unregister_alg(&cbc_des_alg);
624 crypto_unregister_alg(&ecb_des_alg);
625 crypto_unregister_alg(&des_alg);
632 MODULE_ALIAS("des3_ede");
634 MODULE_LICENSE("GPL");
635 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");