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
,
75 .cia_min_keysize
= DES_KEY_SIZE
,
76 .cia_max_keysize
= DES_KEY_SIZE
,
77 .cia_setkey
= des_setkey
,
78 .cia_encrypt
= des_encrypt
,
79 .cia_decrypt
= des_decrypt
,
84 static int ecb_desall_crypt(struct blkcipher_desc
*desc
, long func
,
85 u8
*key
, struct blkcipher_walk
*walk
)
87 int ret
= blkcipher_walk_virt(desc
, walk
);
90 while ((nbytes
= walk
->nbytes
)) {
91 /* only use complete blocks */
92 unsigned int n
= nbytes
& ~(DES_BLOCK_SIZE
- 1);
93 u8
*out
= walk
->dst
.virt
.addr
;
94 u8
*in
= walk
->src
.virt
.addr
;
96 ret
= crypt_s390_km(func
, key
, out
, in
, n
);
97 BUG_ON((ret
< 0) || (ret
!= n
));
99 nbytes
&= DES_BLOCK_SIZE
- 1;
100 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
106 static int cbc_desall_crypt(struct blkcipher_desc
*desc
, long func
,
107 u8
*iv
, struct blkcipher_walk
*walk
)
109 int ret
= blkcipher_walk_virt(desc
, walk
);
110 unsigned int nbytes
= walk
->nbytes
;
115 memcpy(iv
, walk
->iv
, DES_BLOCK_SIZE
);
117 /* only use complete blocks */
118 unsigned int n
= nbytes
& ~(DES_BLOCK_SIZE
- 1);
119 u8
*out
= walk
->dst
.virt
.addr
;
120 u8
*in
= walk
->src
.virt
.addr
;
122 ret
= crypt_s390_kmc(func
, iv
, out
, in
, n
);
123 BUG_ON((ret
< 0) || (ret
!= n
));
125 nbytes
&= DES_BLOCK_SIZE
- 1;
126 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
127 } while ((nbytes
= walk
->nbytes
));
128 memcpy(walk
->iv
, iv
, DES_BLOCK_SIZE
);
134 static int ecb_des_encrypt(struct blkcipher_desc
*desc
,
135 struct scatterlist
*dst
, struct scatterlist
*src
,
138 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
139 struct blkcipher_walk walk
;
141 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
142 return ecb_desall_crypt(desc
, KM_DEA_ENCRYPT
, ctx
->key
, &walk
);
145 static int ecb_des_decrypt(struct blkcipher_desc
*desc
,
146 struct scatterlist
*dst
, struct scatterlist
*src
,
149 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
150 struct blkcipher_walk walk
;
152 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
153 return ecb_desall_crypt(desc
, KM_DEA_DECRYPT
, ctx
->key
, &walk
);
156 static struct crypto_alg ecb_des_alg
= {
157 .cra_name
= "ecb(des)",
158 .cra_driver_name
= "ecb-des-s390",
159 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
160 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
161 .cra_blocksize
= DES_BLOCK_SIZE
,
162 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
163 .cra_type
= &crypto_blkcipher_type
,
164 .cra_module
= THIS_MODULE
,
167 .min_keysize
= DES_KEY_SIZE
,
168 .max_keysize
= DES_KEY_SIZE
,
169 .setkey
= des_setkey
,
170 .encrypt
= ecb_des_encrypt
,
171 .decrypt
= ecb_des_decrypt
,
176 static int cbc_des_encrypt(struct blkcipher_desc
*desc
,
177 struct scatterlist
*dst
, struct scatterlist
*src
,
180 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
181 struct blkcipher_walk walk
;
183 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
184 return cbc_desall_crypt(desc
, KMC_DEA_ENCRYPT
, ctx
->iv
, &walk
);
187 static int cbc_des_decrypt(struct blkcipher_desc
*desc
,
188 struct scatterlist
*dst
, struct scatterlist
*src
,
191 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
192 struct blkcipher_walk walk
;
194 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
195 return cbc_desall_crypt(desc
, KMC_DEA_DECRYPT
, ctx
->iv
, &walk
);
198 static struct crypto_alg cbc_des_alg
= {
199 .cra_name
= "cbc(des)",
200 .cra_driver_name
= "cbc-des-s390",
201 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
202 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
203 .cra_blocksize
= DES_BLOCK_SIZE
,
204 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
205 .cra_type
= &crypto_blkcipher_type
,
206 .cra_module
= THIS_MODULE
,
209 .min_keysize
= DES_KEY_SIZE
,
210 .max_keysize
= DES_KEY_SIZE
,
211 .ivsize
= DES_BLOCK_SIZE
,
212 .setkey
= des_setkey
,
213 .encrypt
= cbc_des_encrypt
,
214 .decrypt
= cbc_des_decrypt
,
222 * For DES-EDE3, there is no known need to reject weak or
223 * complementation keys. Any weakness is obviated by the use of
226 * However, if the first two or last two independent 64-bit keys are
227 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
228 * same as DES. Implementers MUST reject keys that exhibit this
232 static int des3_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
233 unsigned int key_len
)
235 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
236 u32
*flags
= &tfm
->crt_flags
;
238 if (!(memcmp(key
, &key
[DES_KEY_SIZE
], DES_KEY_SIZE
) &&
239 memcmp(&key
[DES_KEY_SIZE
], &key
[DES_KEY_SIZE
* 2],
241 (*flags
& CRYPTO_TFM_REQ_WEAK_KEY
)) {
242 *flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
245 memcpy(ctx
->key
, key
, key_len
);
249 static void des3_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
251 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
253 crypt_s390_km(KM_TDEA_192_ENCRYPT
, ctx
->key
, dst
, src
, DES_BLOCK_SIZE
);
256 static void des3_decrypt(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_DECRYPT
, ctx
->key
, dst
, src
, DES_BLOCK_SIZE
);
263 static struct crypto_alg des3_alg
= {
264 .cra_name
= "des3_ede",
265 .cra_driver_name
= "des3_ede-s390",
266 .cra_priority
= CRYPT_S390_PRIORITY
,
267 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
268 .cra_blocksize
= DES_BLOCK_SIZE
,
269 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
270 .cra_module
= THIS_MODULE
,
273 .cia_min_keysize
= DES3_KEY_SIZE
,
274 .cia_max_keysize
= DES3_KEY_SIZE
,
275 .cia_setkey
= des3_setkey
,
276 .cia_encrypt
= des3_encrypt
,
277 .cia_decrypt
= des3_decrypt
,
282 static int ecb_des3_encrypt(struct blkcipher_desc
*desc
,
283 struct scatterlist
*dst
, struct scatterlist
*src
,
286 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
287 struct blkcipher_walk walk
;
289 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
290 return ecb_desall_crypt(desc
, KM_TDEA_192_ENCRYPT
, ctx
->key
, &walk
);
293 static int ecb_des3_decrypt(struct blkcipher_desc
*desc
,
294 struct scatterlist
*dst
, struct scatterlist
*src
,
297 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
298 struct blkcipher_walk walk
;
300 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
301 return ecb_desall_crypt(desc
, KM_TDEA_192_DECRYPT
, ctx
->key
, &walk
);
304 static struct crypto_alg ecb_des3_alg
= {
305 .cra_name
= "ecb(des3_ede)",
306 .cra_driver_name
= "ecb-des3_ede-s390",
307 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
308 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
309 .cra_blocksize
= DES_BLOCK_SIZE
,
310 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
311 .cra_type
= &crypto_blkcipher_type
,
312 .cra_module
= THIS_MODULE
,
315 .min_keysize
= DES3_KEY_SIZE
,
316 .max_keysize
= DES3_KEY_SIZE
,
317 .setkey
= des3_setkey
,
318 .encrypt
= ecb_des3_encrypt
,
319 .decrypt
= ecb_des3_decrypt
,
324 static int cbc_des3_encrypt(struct blkcipher_desc
*desc
,
325 struct scatterlist
*dst
, struct scatterlist
*src
,
328 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
329 struct blkcipher_walk walk
;
331 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
332 return cbc_desall_crypt(desc
, KMC_TDEA_192_ENCRYPT
, ctx
->iv
, &walk
);
335 static int cbc_des3_decrypt(struct blkcipher_desc
*desc
,
336 struct scatterlist
*dst
, struct scatterlist
*src
,
339 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
340 struct blkcipher_walk walk
;
342 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
343 return cbc_desall_crypt(desc
, KMC_TDEA_192_DECRYPT
, ctx
->iv
, &walk
);
346 static struct crypto_alg cbc_des3_alg
= {
347 .cra_name
= "cbc(des3_ede)",
348 .cra_driver_name
= "cbc-des3_ede-s390",
349 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
350 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
351 .cra_blocksize
= DES_BLOCK_SIZE
,
352 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
353 .cra_type
= &crypto_blkcipher_type
,
354 .cra_module
= THIS_MODULE
,
357 .min_keysize
= DES3_KEY_SIZE
,
358 .max_keysize
= DES3_KEY_SIZE
,
359 .ivsize
= DES_BLOCK_SIZE
,
360 .setkey
= des3_setkey
,
361 .encrypt
= cbc_des3_encrypt
,
362 .decrypt
= cbc_des3_decrypt
,
367 static int ctr_desall_crypt(struct blkcipher_desc
*desc
, long func
,
368 struct s390_des_ctx
*ctx
, struct blkcipher_walk
*walk
)
370 int ret
= blkcipher_walk_virt_block(desc
, walk
, DES_BLOCK_SIZE
);
371 unsigned int i
, n
, nbytes
;
372 u8 buf
[DES_BLOCK_SIZE
];
375 memcpy(ctrblk
, walk
->iv
, DES_BLOCK_SIZE
);
376 while ((nbytes
= walk
->nbytes
) >= DES_BLOCK_SIZE
) {
377 out
= walk
->dst
.virt
.addr
;
378 in
= walk
->src
.virt
.addr
;
379 while (nbytes
>= DES_BLOCK_SIZE
) {
380 /* align to block size, max. PAGE_SIZE */
381 n
= (nbytes
> PAGE_SIZE
) ? PAGE_SIZE
:
382 nbytes
& ~(DES_BLOCK_SIZE
- 1);
383 for (i
= DES_BLOCK_SIZE
; i
< n
; i
+= DES_BLOCK_SIZE
) {
384 memcpy(ctrblk
+ i
, ctrblk
+ i
- DES_BLOCK_SIZE
,
386 crypto_inc(ctrblk
+ i
, DES_BLOCK_SIZE
);
388 ret
= crypt_s390_kmctr(func
, ctx
->key
, out
, in
, n
, ctrblk
);
389 BUG_ON((ret
< 0) || (ret
!= n
));
390 if (n
> DES_BLOCK_SIZE
)
391 memcpy(ctrblk
, ctrblk
+ n
- DES_BLOCK_SIZE
,
393 crypto_inc(ctrblk
, DES_BLOCK_SIZE
);
398 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
401 /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
403 out
= walk
->dst
.virt
.addr
;
404 in
= walk
->src
.virt
.addr
;
405 ret
= crypt_s390_kmctr(func
, ctx
->key
, buf
, in
,
406 DES_BLOCK_SIZE
, ctrblk
);
407 BUG_ON(ret
< 0 || ret
!= DES_BLOCK_SIZE
);
408 memcpy(out
, buf
, nbytes
);
409 crypto_inc(ctrblk
, DES_BLOCK_SIZE
);
410 ret
= blkcipher_walk_done(desc
, walk
, 0);
412 memcpy(walk
->iv
, ctrblk
, DES_BLOCK_SIZE
);
416 static int ctr_des_encrypt(struct blkcipher_desc
*desc
,
417 struct scatterlist
*dst
, struct scatterlist
*src
,
420 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
421 struct blkcipher_walk walk
;
423 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
424 return ctr_desall_crypt(desc
, KMCTR_DEA_ENCRYPT
, ctx
, &walk
);
427 static int ctr_des_decrypt(struct blkcipher_desc
*desc
,
428 struct scatterlist
*dst
, struct scatterlist
*src
,
431 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
432 struct blkcipher_walk walk
;
434 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
435 return ctr_desall_crypt(desc
, KMCTR_DEA_DECRYPT
, ctx
, &walk
);
438 static struct crypto_alg ctr_des_alg
= {
439 .cra_name
= "ctr(des)",
440 .cra_driver_name
= "ctr-des-s390",
441 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
442 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
444 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
445 .cra_type
= &crypto_blkcipher_type
,
446 .cra_module
= THIS_MODULE
,
449 .min_keysize
= DES_KEY_SIZE
,
450 .max_keysize
= DES_KEY_SIZE
,
451 .ivsize
= DES_BLOCK_SIZE
,
452 .setkey
= des_setkey
,
453 .encrypt
= ctr_des_encrypt
,
454 .decrypt
= ctr_des_decrypt
,
459 static int ctr_des3_encrypt(struct blkcipher_desc
*desc
,
460 struct scatterlist
*dst
, struct scatterlist
*src
,
463 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
464 struct blkcipher_walk walk
;
466 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
467 return ctr_desall_crypt(desc
, KMCTR_TDEA_192_ENCRYPT
, ctx
, &walk
);
470 static int ctr_des3_decrypt(struct blkcipher_desc
*desc
,
471 struct scatterlist
*dst
, struct scatterlist
*src
,
474 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
475 struct blkcipher_walk walk
;
477 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
478 return ctr_desall_crypt(desc
, KMCTR_TDEA_192_DECRYPT
, ctx
, &walk
);
481 static struct crypto_alg ctr_des3_alg
= {
482 .cra_name
= "ctr(des3_ede)",
483 .cra_driver_name
= "ctr-des3_ede-s390",
484 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
485 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
487 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
488 .cra_type
= &crypto_blkcipher_type
,
489 .cra_module
= THIS_MODULE
,
492 .min_keysize
= DES3_KEY_SIZE
,
493 .max_keysize
= DES3_KEY_SIZE
,
494 .ivsize
= DES_BLOCK_SIZE
,
495 .setkey
= des3_setkey
,
496 .encrypt
= ctr_des3_encrypt
,
497 .decrypt
= ctr_des3_decrypt
,
502 static int __init
des_s390_init(void)
506 if (!crypt_s390_func_available(KM_DEA_ENCRYPT
, CRYPT_S390_MSA
) ||
507 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT
, CRYPT_S390_MSA
))
510 ret
= crypto_register_alg(&des_alg
);
513 ret
= crypto_register_alg(&ecb_des_alg
);
516 ret
= crypto_register_alg(&cbc_des_alg
);
519 ret
= crypto_register_alg(&des3_alg
);
522 ret
= crypto_register_alg(&ecb_des3_alg
);
525 ret
= crypto_register_alg(&cbc_des3_alg
);
529 if (crypt_s390_func_available(KMCTR_DEA_ENCRYPT
,
530 CRYPT_S390_MSA
| CRYPT_S390_MSA4
) &&
531 crypt_s390_func_available(KMCTR_TDEA_192_ENCRYPT
,
532 CRYPT_S390_MSA
| CRYPT_S390_MSA4
)) {
533 ret
= crypto_register_alg(&ctr_des_alg
);
536 ret
= crypto_register_alg(&ctr_des3_alg
);
539 ctrblk
= (u8
*) __get_free_page(GFP_KERNEL
);
549 crypto_unregister_alg(&ctr_des3_alg
);
551 crypto_unregister_alg(&ctr_des_alg
);
553 crypto_unregister_alg(&cbc_des3_alg
);
555 crypto_unregister_alg(&ecb_des3_alg
);
557 crypto_unregister_alg(&des3_alg
);
559 crypto_unregister_alg(&cbc_des_alg
);
561 crypto_unregister_alg(&ecb_des_alg
);
563 crypto_unregister_alg(&des_alg
);
568 static void __exit
des_s390_exit(void)
571 crypto_unregister_alg(&ctr_des_alg
);
572 crypto_unregister_alg(&ctr_des3_alg
);
573 free_page((unsigned long) ctrblk
);
575 crypto_unregister_alg(&cbc_des3_alg
);
576 crypto_unregister_alg(&ecb_des3_alg
);
577 crypto_unregister_alg(&des3_alg
);
578 crypto_unregister_alg(&cbc_des_alg
);
579 crypto_unregister_alg(&ecb_des_alg
);
580 crypto_unregister_alg(&des_alg
);
583 module_init(des_s390_init
);
584 module_exit(des_s390_exit
);
587 MODULE_ALIAS("des3_ede");
589 MODULE_LICENSE("GPL");
590 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");