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)
28 static DEFINE_SPINLOCK(ctrblk_lock
);
31 u8 iv
[DES_BLOCK_SIZE
];
32 u8 key
[DES3_KEY_SIZE
];
35 static int des_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
38 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
39 u32
*flags
= &tfm
->crt_flags
;
40 u32 tmp
[DES_EXPKEY_WORDS
];
42 /* check for weak keys */
43 if (!des_ekey(tmp
, key
) && (*flags
& CRYPTO_TFM_REQ_WEAK_KEY
)) {
44 *flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
48 memcpy(ctx
->key
, key
, key_len
);
52 static void des_encrypt(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
54 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
56 crypt_s390_km(KM_DEA_ENCRYPT
, ctx
->key
, out
, in
, DES_BLOCK_SIZE
);
59 static void des_decrypt(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
61 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
63 crypt_s390_km(KM_DEA_DECRYPT
, ctx
->key
, out
, in
, DES_BLOCK_SIZE
);
66 static struct crypto_alg des_alg
= {
68 .cra_driver_name
= "des-s390",
69 .cra_priority
= CRYPT_S390_PRIORITY
,
70 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
71 .cra_blocksize
= DES_BLOCK_SIZE
,
72 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
73 .cra_module
= THIS_MODULE
,
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
,
176 .min_keysize
= DES_KEY_SIZE
,
177 .max_keysize
= DES_KEY_SIZE
,
178 .setkey
= des_setkey
,
179 .encrypt
= ecb_des_encrypt
,
180 .decrypt
= ecb_des_decrypt
,
185 static int cbc_des_encrypt(struct blkcipher_desc
*desc
,
186 struct scatterlist
*dst
, struct scatterlist
*src
,
189 struct blkcipher_walk walk
;
191 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
192 return cbc_desall_crypt(desc
, KMC_DEA_ENCRYPT
, &walk
);
195 static int cbc_des_decrypt(struct blkcipher_desc
*desc
,
196 struct scatterlist
*dst
, struct scatterlist
*src
,
199 struct blkcipher_walk walk
;
201 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
202 return cbc_desall_crypt(desc
, KMC_DEA_DECRYPT
, &walk
);
205 static struct crypto_alg cbc_des_alg
= {
206 .cra_name
= "cbc(des)",
207 .cra_driver_name
= "cbc-des-s390",
208 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
209 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
210 .cra_blocksize
= DES_BLOCK_SIZE
,
211 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
212 .cra_type
= &crypto_blkcipher_type
,
213 .cra_module
= THIS_MODULE
,
216 .min_keysize
= DES_KEY_SIZE
,
217 .max_keysize
= DES_KEY_SIZE
,
218 .ivsize
= DES_BLOCK_SIZE
,
219 .setkey
= des_setkey
,
220 .encrypt
= cbc_des_encrypt
,
221 .decrypt
= cbc_des_decrypt
,
229 * For DES-EDE3, there is no known need to reject weak or
230 * complementation keys. Any weakness is obviated by the use of
233 * However, if the first two or last two independent 64-bit keys are
234 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
235 * same as DES. Implementers MUST reject keys that exhibit this
239 static int des3_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
240 unsigned int key_len
)
242 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
243 u32
*flags
= &tfm
->crt_flags
;
245 if (!(crypto_memneq(key
, &key
[DES_KEY_SIZE
], DES_KEY_SIZE
) &&
246 crypto_memneq(&key
[DES_KEY_SIZE
], &key
[DES_KEY_SIZE
* 2],
248 (*flags
& CRYPTO_TFM_REQ_WEAK_KEY
)) {
249 *flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
252 memcpy(ctx
->key
, key
, key_len
);
256 static void des3_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
258 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
260 crypt_s390_km(KM_TDEA_192_ENCRYPT
, ctx
->key
, dst
, src
, DES_BLOCK_SIZE
);
263 static void des3_decrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
265 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
267 crypt_s390_km(KM_TDEA_192_DECRYPT
, ctx
->key
, dst
, src
, DES_BLOCK_SIZE
);
270 static struct crypto_alg des3_alg
= {
271 .cra_name
= "des3_ede",
272 .cra_driver_name
= "des3_ede-s390",
273 .cra_priority
= CRYPT_S390_PRIORITY
,
274 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
275 .cra_blocksize
= DES_BLOCK_SIZE
,
276 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
277 .cra_module
= THIS_MODULE
,
280 .cia_min_keysize
= DES3_KEY_SIZE
,
281 .cia_max_keysize
= DES3_KEY_SIZE
,
282 .cia_setkey
= des3_setkey
,
283 .cia_encrypt
= des3_encrypt
,
284 .cia_decrypt
= des3_decrypt
,
289 static int ecb_des3_encrypt(struct blkcipher_desc
*desc
,
290 struct scatterlist
*dst
, struct scatterlist
*src
,
293 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
294 struct blkcipher_walk walk
;
296 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
297 return ecb_desall_crypt(desc
, KM_TDEA_192_ENCRYPT
, ctx
->key
, &walk
);
300 static int ecb_des3_decrypt(struct blkcipher_desc
*desc
,
301 struct scatterlist
*dst
, struct scatterlist
*src
,
304 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
305 struct blkcipher_walk walk
;
307 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
308 return ecb_desall_crypt(desc
, KM_TDEA_192_DECRYPT
, ctx
->key
, &walk
);
311 static struct crypto_alg ecb_des3_alg
= {
312 .cra_name
= "ecb(des3_ede)",
313 .cra_driver_name
= "ecb-des3_ede-s390",
314 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
315 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
316 .cra_blocksize
= DES_BLOCK_SIZE
,
317 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
318 .cra_type
= &crypto_blkcipher_type
,
319 .cra_module
= THIS_MODULE
,
322 .min_keysize
= DES3_KEY_SIZE
,
323 .max_keysize
= DES3_KEY_SIZE
,
324 .setkey
= des3_setkey
,
325 .encrypt
= ecb_des3_encrypt
,
326 .decrypt
= ecb_des3_decrypt
,
331 static int cbc_des3_encrypt(struct blkcipher_desc
*desc
,
332 struct scatterlist
*dst
, struct scatterlist
*src
,
335 struct blkcipher_walk walk
;
337 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
338 return cbc_desall_crypt(desc
, KMC_TDEA_192_ENCRYPT
, &walk
);
341 static int cbc_des3_decrypt(struct blkcipher_desc
*desc
,
342 struct scatterlist
*dst
, struct scatterlist
*src
,
345 struct blkcipher_walk walk
;
347 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
348 return cbc_desall_crypt(desc
, KMC_TDEA_192_DECRYPT
, &walk
);
351 static struct crypto_alg cbc_des3_alg
= {
352 .cra_name
= "cbc(des3_ede)",
353 .cra_driver_name
= "cbc-des3_ede-s390",
354 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
355 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
356 .cra_blocksize
= DES_BLOCK_SIZE
,
357 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
358 .cra_type
= &crypto_blkcipher_type
,
359 .cra_module
= THIS_MODULE
,
362 .min_keysize
= DES3_KEY_SIZE
,
363 .max_keysize
= DES3_KEY_SIZE
,
364 .ivsize
= DES_BLOCK_SIZE
,
365 .setkey
= des3_setkey
,
366 .encrypt
= cbc_des3_encrypt
,
367 .decrypt
= cbc_des3_decrypt
,
372 static unsigned int __ctrblk_init(u8
*ctrptr
, unsigned int nbytes
)
376 /* align to block size, max. PAGE_SIZE */
377 n
= (nbytes
> PAGE_SIZE
) ? PAGE_SIZE
: nbytes
& ~(DES_BLOCK_SIZE
- 1);
378 for (i
= DES_BLOCK_SIZE
; i
< n
; i
+= DES_BLOCK_SIZE
) {
379 memcpy(ctrptr
+ i
, ctrptr
+ i
- DES_BLOCK_SIZE
, DES_BLOCK_SIZE
);
380 crypto_inc(ctrptr
+ i
, DES_BLOCK_SIZE
);
385 static int ctr_desall_crypt(struct blkcipher_desc
*desc
, long func
,
386 struct s390_des_ctx
*ctx
,
387 struct blkcipher_walk
*walk
)
389 int ret
= blkcipher_walk_virt_block(desc
, walk
, DES_BLOCK_SIZE
);
390 unsigned int n
, nbytes
;
391 u8 buf
[DES_BLOCK_SIZE
], ctrbuf
[DES_BLOCK_SIZE
];
392 u8
*out
, *in
, *ctrptr
= ctrbuf
;
397 if (spin_trylock(&ctrblk_lock
))
400 memcpy(ctrptr
, walk
->iv
, DES_BLOCK_SIZE
);
401 while ((nbytes
= walk
->nbytes
) >= DES_BLOCK_SIZE
) {
402 out
= walk
->dst
.virt
.addr
;
403 in
= walk
->src
.virt
.addr
;
404 while (nbytes
>= DES_BLOCK_SIZE
) {
405 if (ctrptr
== ctrblk
)
406 n
= __ctrblk_init(ctrptr
, nbytes
);
409 ret
= crypt_s390_kmctr(func
, ctx
->key
, out
, in
,
411 if (ret
< 0 || ret
!= n
) {
412 if (ctrptr
== ctrblk
)
413 spin_unlock(&ctrblk_lock
);
416 if (n
> DES_BLOCK_SIZE
)
417 memcpy(ctrptr
, ctrptr
+ n
- DES_BLOCK_SIZE
,
419 crypto_inc(ctrptr
, DES_BLOCK_SIZE
);
424 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
426 if (ctrptr
== ctrblk
) {
428 memcpy(ctrbuf
, ctrptr
, DES_BLOCK_SIZE
);
430 memcpy(walk
->iv
, ctrptr
, DES_BLOCK_SIZE
);
431 spin_unlock(&ctrblk_lock
);
433 /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
435 out
= walk
->dst
.virt
.addr
;
436 in
= walk
->src
.virt
.addr
;
437 ret
= crypt_s390_kmctr(func
, ctx
->key
, buf
, in
,
438 DES_BLOCK_SIZE
, ctrbuf
);
439 if (ret
< 0 || ret
!= DES_BLOCK_SIZE
)
441 memcpy(out
, buf
, nbytes
);
442 crypto_inc(ctrbuf
, DES_BLOCK_SIZE
);
443 ret
= blkcipher_walk_done(desc
, walk
, 0);
444 memcpy(walk
->iv
, ctrbuf
, DES_BLOCK_SIZE
);
449 static int ctr_des_encrypt(struct blkcipher_desc
*desc
,
450 struct scatterlist
*dst
, struct scatterlist
*src
,
453 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
454 struct blkcipher_walk walk
;
456 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
457 return ctr_desall_crypt(desc
, KMCTR_DEA_ENCRYPT
, ctx
, &walk
);
460 static int ctr_des_decrypt(struct blkcipher_desc
*desc
,
461 struct scatterlist
*dst
, struct scatterlist
*src
,
464 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
465 struct blkcipher_walk walk
;
467 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
468 return ctr_desall_crypt(desc
, KMCTR_DEA_DECRYPT
, ctx
, &walk
);
471 static struct crypto_alg ctr_des_alg
= {
472 .cra_name
= "ctr(des)",
473 .cra_driver_name
= "ctr-des-s390",
474 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
475 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
477 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
478 .cra_type
= &crypto_blkcipher_type
,
479 .cra_module
= THIS_MODULE
,
482 .min_keysize
= DES_KEY_SIZE
,
483 .max_keysize
= DES_KEY_SIZE
,
484 .ivsize
= DES_BLOCK_SIZE
,
485 .setkey
= des_setkey
,
486 .encrypt
= ctr_des_encrypt
,
487 .decrypt
= ctr_des_decrypt
,
492 static int ctr_des3_encrypt(struct blkcipher_desc
*desc
,
493 struct scatterlist
*dst
, struct scatterlist
*src
,
496 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
497 struct blkcipher_walk walk
;
499 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
500 return ctr_desall_crypt(desc
, KMCTR_TDEA_192_ENCRYPT
, ctx
, &walk
);
503 static int ctr_des3_decrypt(struct blkcipher_desc
*desc
,
504 struct scatterlist
*dst
, struct scatterlist
*src
,
507 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
508 struct blkcipher_walk walk
;
510 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
511 return ctr_desall_crypt(desc
, KMCTR_TDEA_192_DECRYPT
, ctx
, &walk
);
514 static struct crypto_alg ctr_des3_alg
= {
515 .cra_name
= "ctr(des3_ede)",
516 .cra_driver_name
= "ctr-des3_ede-s390",
517 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
518 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
520 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
521 .cra_type
= &crypto_blkcipher_type
,
522 .cra_module
= THIS_MODULE
,
525 .min_keysize
= DES3_KEY_SIZE
,
526 .max_keysize
= DES3_KEY_SIZE
,
527 .ivsize
= DES_BLOCK_SIZE
,
528 .setkey
= des3_setkey
,
529 .encrypt
= ctr_des3_encrypt
,
530 .decrypt
= ctr_des3_decrypt
,
535 static int __init
des_s390_init(void)
539 if (!crypt_s390_func_available(KM_DEA_ENCRYPT
, CRYPT_S390_MSA
) ||
540 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT
, CRYPT_S390_MSA
))
543 ret
= crypto_register_alg(&des_alg
);
546 ret
= crypto_register_alg(&ecb_des_alg
);
549 ret
= crypto_register_alg(&cbc_des_alg
);
552 ret
= crypto_register_alg(&des3_alg
);
555 ret
= crypto_register_alg(&ecb_des3_alg
);
558 ret
= crypto_register_alg(&cbc_des3_alg
);
562 if (crypt_s390_func_available(KMCTR_DEA_ENCRYPT
,
563 CRYPT_S390_MSA
| CRYPT_S390_MSA4
) &&
564 crypt_s390_func_available(KMCTR_TDEA_192_ENCRYPT
,
565 CRYPT_S390_MSA
| CRYPT_S390_MSA4
)) {
566 ret
= crypto_register_alg(&ctr_des_alg
);
569 ret
= crypto_register_alg(&ctr_des3_alg
);
572 ctrblk
= (u8
*) __get_free_page(GFP_KERNEL
);
582 crypto_unregister_alg(&ctr_des3_alg
);
584 crypto_unregister_alg(&ctr_des_alg
);
586 crypto_unregister_alg(&cbc_des3_alg
);
588 crypto_unregister_alg(&ecb_des3_alg
);
590 crypto_unregister_alg(&des3_alg
);
592 crypto_unregister_alg(&cbc_des_alg
);
594 crypto_unregister_alg(&ecb_des_alg
);
596 crypto_unregister_alg(&des_alg
);
601 static void __exit
des_s390_exit(void)
604 crypto_unregister_alg(&ctr_des_alg
);
605 crypto_unregister_alg(&ctr_des3_alg
);
606 free_page((unsigned long) ctrblk
);
608 crypto_unregister_alg(&cbc_des3_alg
);
609 crypto_unregister_alg(&ecb_des3_alg
);
610 crypto_unregister_alg(&des3_alg
);
611 crypto_unregister_alg(&cbc_des_alg
);
612 crypto_unregister_alg(&ecb_des_alg
);
613 crypto_unregister_alg(&des_alg
);
616 module_init(des_s390_init
);
617 module_exit(des_s390_exit
);
620 MODULE_ALIAS("des3_ede");
622 MODULE_LICENSE("GPL");
623 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");