4 * s390 implementation of the DES Cipher Algorithm.
6 * Copyright IBM Corp. 2003,2011
7 * Author(s): Thomas Spatzier
8 * Jan Glauber (jan.glauber@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 <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/crypto.h>
20 #include <crypto/algapi.h>
21 #include <crypto/des.h>
23 #include "crypt_s390.h"
25 #define DES3_KEY_SIZE (3 * DES_KEY_SIZE)
30 u8 iv[DES_BLOCK_SIZE];
31 u8 key[DES3_KEY_SIZE];
34 static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
37 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
38 u32 *flags = &tfm->crt_flags;
39 u32 tmp[DES_EXPKEY_WORDS];
41 /* check for weak keys */
42 if (!des_ekey(tmp, key) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
43 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
47 memcpy(ctx->key, key, key_len);
51 static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
53 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
55 crypt_s390_km(KM_DEA_ENCRYPT, ctx->key, out, in, DES_BLOCK_SIZE);
58 static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
60 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
62 crypt_s390_km(KM_DEA_DECRYPT, ctx->key, out, in, DES_BLOCK_SIZE);
65 static struct crypto_alg des_alg = {
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),
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,
85 static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
86 u8 *key, struct blkcipher_walk *walk)
88 int ret = blkcipher_walk_virt(desc, walk);
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;
97 ret = crypt_s390_km(func, key, out, in, n);
98 if (ret < 0 || ret != n)
101 nbytes &= DES_BLOCK_SIZE - 1;
102 ret = blkcipher_walk_done(desc, walk, nbytes);
108 static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
109 struct blkcipher_walk *walk)
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;
115 u8 iv[DES_BLOCK_SIZE];
116 u8 key[DES3_KEY_SIZE];
122 memcpy(param.iv, walk->iv, DES_BLOCK_SIZE);
123 memcpy(param.key, ctx->key, DES3_KEY_SIZE);
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;
130 ret = crypt_s390_kmc(func, ¶m, out, in, n);
131 if (ret < 0 || ret != n)
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);
143 static int ecb_des_encrypt(struct blkcipher_desc *desc,
144 struct scatterlist *dst, struct scatterlist *src,
147 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
148 struct blkcipher_walk walk;
150 blkcipher_walk_init(&walk, dst, src, nbytes);
151 return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, ctx->key, &walk);
154 static int ecb_des_decrypt(struct blkcipher_desc *desc,
155 struct scatterlist *dst, struct scatterlist *src,
158 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
159 struct blkcipher_walk walk;
161 blkcipher_walk_init(&walk, dst, src, nbytes);
162 return ecb_desall_crypt(desc, KM_DEA_DECRYPT, ctx->key, &walk);
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),
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,
186 static int cbc_des_encrypt(struct blkcipher_desc *desc,
187 struct scatterlist *dst, struct scatterlist *src,
190 struct blkcipher_walk walk;
192 blkcipher_walk_init(&walk, dst, src, nbytes);
193 return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, &walk);
196 static int cbc_des_decrypt(struct blkcipher_desc *desc,
197 struct scatterlist *dst, struct scatterlist *src,
200 struct blkcipher_walk walk;
202 blkcipher_walk_init(&walk, dst, src, nbytes);
203 return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, &walk);
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),
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,
231 * For DES-EDE3, there is no known need to reject weak or
232 * complementation keys. Any weakness is obviated by the use of
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
241 static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
242 unsigned int key_len)
244 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
245 u32 *flags = &tfm->crt_flags;
247 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
248 memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
250 (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
251 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
254 memcpy(ctx->key, key, key_len);
258 static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
260 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
262 crypt_s390_km(KM_TDEA_192_ENCRYPT, ctx->key, dst, src, DES_BLOCK_SIZE);
265 static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
267 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
269 crypt_s390_km(KM_TDEA_192_DECRYPT, ctx->key, dst, src, DES_BLOCK_SIZE);
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),
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,
292 static int ecb_des3_encrypt(struct blkcipher_desc *desc,
293 struct scatterlist *dst, struct scatterlist *src,
296 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
297 struct blkcipher_walk walk;
299 blkcipher_walk_init(&walk, dst, src, nbytes);
300 return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, ctx->key, &walk);
303 static int ecb_des3_decrypt(struct blkcipher_desc *desc,
304 struct scatterlist *dst, struct scatterlist *src,
307 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
308 struct blkcipher_walk walk;
310 blkcipher_walk_init(&walk, dst, src, nbytes);
311 return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, ctx->key, &walk);
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),
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,
336 static int cbc_des3_encrypt(struct blkcipher_desc *desc,
337 struct scatterlist *dst, struct scatterlist *src,
340 struct blkcipher_walk walk;
342 blkcipher_walk_init(&walk, dst, src, nbytes);
343 return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, &walk);
346 static int cbc_des3_decrypt(struct blkcipher_desc *desc,
347 struct scatterlist *dst, struct scatterlist *src,
350 struct blkcipher_walk walk;
352 blkcipher_walk_init(&walk, dst, src, nbytes);
353 return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, &walk);
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),
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,
379 static int ctr_desall_crypt(struct blkcipher_desc *desc, long func,
380 struct s390_des_ctx *ctx, struct blkcipher_walk *walk)
382 int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
383 unsigned int i, n, nbytes;
384 u8 buf[DES_BLOCK_SIZE];
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,
398 crypto_inc(ctrblk + i, DES_BLOCK_SIZE);
400 ret = crypt_s390_kmctr(func, ctx->key, out, in, n, ctrblk);
401 if (ret < 0 || ret != n)
403 if (n > DES_BLOCK_SIZE)
404 memcpy(ctrblk, ctrblk + n - DES_BLOCK_SIZE,
406 crypto_inc(ctrblk, DES_BLOCK_SIZE);
411 ret = blkcipher_walk_done(desc, walk, nbytes);
414 /* final block may be < DES_BLOCK_SIZE, copy only 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)
422 memcpy(out, buf, nbytes);
423 crypto_inc(ctrblk, DES_BLOCK_SIZE);
424 ret = blkcipher_walk_done(desc, walk, 0);
426 memcpy(walk->iv, ctrblk, DES_BLOCK_SIZE);
430 static int ctr_des_encrypt(struct blkcipher_desc *desc,
431 struct scatterlist *dst, struct scatterlist *src,
434 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
435 struct blkcipher_walk walk;
437 blkcipher_walk_init(&walk, dst, src, nbytes);
438 return ctr_desall_crypt(desc, KMCTR_DEA_ENCRYPT, ctx, &walk);
441 static int ctr_des_decrypt(struct blkcipher_desc *desc,
442 struct scatterlist *dst, struct scatterlist *src,
445 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
446 struct blkcipher_walk walk;
448 blkcipher_walk_init(&walk, dst, src, nbytes);
449 return ctr_desall_crypt(desc, KMCTR_DEA_DECRYPT, ctx, &walk);
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,
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),
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,
474 static int ctr_des3_encrypt(struct blkcipher_desc *desc,
475 struct scatterlist *dst, struct scatterlist *src,
478 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
479 struct blkcipher_walk walk;
481 blkcipher_walk_init(&walk, dst, src, nbytes);
482 return ctr_desall_crypt(desc, KMCTR_TDEA_192_ENCRYPT, ctx, &walk);
485 static int ctr_des3_decrypt(struct blkcipher_desc *desc,
486 struct scatterlist *dst, struct scatterlist *src,
489 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
490 struct blkcipher_walk walk;
492 blkcipher_walk_init(&walk, dst, src, nbytes);
493 return ctr_desall_crypt(desc, KMCTR_TDEA_192_DECRYPT, ctx, &walk);
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,
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),
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,
518 static int __init des_s390_init(void)
522 if (!crypt_s390_func_available(KM_DEA_ENCRYPT, CRYPT_S390_MSA) ||
523 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT, CRYPT_S390_MSA))
526 ret = crypto_register_alg(&des_alg);
529 ret = crypto_register_alg(&ecb_des_alg);
532 ret = crypto_register_alg(&cbc_des_alg);
535 ret = crypto_register_alg(&des3_alg);
538 ret = crypto_register_alg(&ecb_des3_alg);
541 ret = crypto_register_alg(&cbc_des3_alg);
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);
552 ret = crypto_register_alg(&ctr_des3_alg);
555 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
565 crypto_unregister_alg(&ctr_des3_alg);
567 crypto_unregister_alg(&ctr_des_alg);
569 crypto_unregister_alg(&cbc_des3_alg);
571 crypto_unregister_alg(&ecb_des3_alg);
573 crypto_unregister_alg(&des3_alg);
575 crypto_unregister_alg(&cbc_des_alg);
577 crypto_unregister_alg(&ecb_des_alg);
579 crypto_unregister_alg(&des_alg);
584 static void __exit des_s390_exit(void)
587 crypto_unregister_alg(&ctr_des_alg);
588 crypto_unregister_alg(&ctr_des3_alg);
589 free_page((unsigned long) ctrblk);
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);
599 module_init(des_s390_init);
600 module_exit(des_s390_exit);
603 MODULE_ALIAS("des3_ede");
605 MODULE_LICENSE("GPL");
606 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");