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 if (ret
< 0 || ret
!= n
)
100 nbytes
&= DES_BLOCK_SIZE
- 1;
101 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
107 static int cbc_desall_crypt(struct blkcipher_desc
*desc
, long func
,
108 u8
*iv
, struct blkcipher_walk
*walk
)
110 int ret
= blkcipher_walk_virt(desc
, walk
);
111 unsigned int nbytes
= walk
->nbytes
;
116 memcpy(iv
, walk
->iv
, DES_BLOCK_SIZE
);
118 /* only use complete blocks */
119 unsigned int n
= nbytes
& ~(DES_BLOCK_SIZE
- 1);
120 u8
*out
= walk
->dst
.virt
.addr
;
121 u8
*in
= walk
->src
.virt
.addr
;
123 ret
= crypt_s390_kmc(func
, iv
, out
, in
, n
);
124 if (ret
< 0 || ret
!= n
)
127 nbytes
&= DES_BLOCK_SIZE
- 1;
128 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
129 } while ((nbytes
= walk
->nbytes
));
130 memcpy(walk
->iv
, iv
, DES_BLOCK_SIZE
);
136 static int ecb_des_encrypt(struct blkcipher_desc
*desc
,
137 struct scatterlist
*dst
, struct scatterlist
*src
,
140 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
141 struct blkcipher_walk walk
;
143 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
144 return ecb_desall_crypt(desc
, KM_DEA_ENCRYPT
, ctx
->key
, &walk
);
147 static int ecb_des_decrypt(struct blkcipher_desc
*desc
,
148 struct scatterlist
*dst
, struct scatterlist
*src
,
151 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
152 struct blkcipher_walk walk
;
154 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
155 return ecb_desall_crypt(desc
, KM_DEA_DECRYPT
, ctx
->key
, &walk
);
158 static struct crypto_alg ecb_des_alg
= {
159 .cra_name
= "ecb(des)",
160 .cra_driver_name
= "ecb-des-s390",
161 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
162 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
163 .cra_blocksize
= DES_BLOCK_SIZE
,
164 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
165 .cra_type
= &crypto_blkcipher_type
,
166 .cra_module
= THIS_MODULE
,
169 .min_keysize
= DES_KEY_SIZE
,
170 .max_keysize
= DES_KEY_SIZE
,
171 .setkey
= des_setkey
,
172 .encrypt
= ecb_des_encrypt
,
173 .decrypt
= ecb_des_decrypt
,
178 static int cbc_des_encrypt(struct blkcipher_desc
*desc
,
179 struct scatterlist
*dst
, struct scatterlist
*src
,
182 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
183 struct blkcipher_walk walk
;
185 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
186 return cbc_desall_crypt(desc
, KMC_DEA_ENCRYPT
, ctx
->iv
, &walk
);
189 static int cbc_des_decrypt(struct blkcipher_desc
*desc
,
190 struct scatterlist
*dst
, struct scatterlist
*src
,
193 struct s390_des_ctx
*ctx
= 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_DECRYPT
, ctx
->iv
, &walk
);
200 static struct crypto_alg cbc_des_alg
= {
201 .cra_name
= "cbc(des)",
202 .cra_driver_name
= "cbc-des-s390",
203 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
204 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
205 .cra_blocksize
= DES_BLOCK_SIZE
,
206 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
207 .cra_type
= &crypto_blkcipher_type
,
208 .cra_module
= THIS_MODULE
,
211 .min_keysize
= DES_KEY_SIZE
,
212 .max_keysize
= DES_KEY_SIZE
,
213 .ivsize
= DES_BLOCK_SIZE
,
214 .setkey
= des_setkey
,
215 .encrypt
= cbc_des_encrypt
,
216 .decrypt
= cbc_des_decrypt
,
224 * For DES-EDE3, there is no known need to reject weak or
225 * complementation keys. Any weakness is obviated by the use of
228 * However, if the first two or last two independent 64-bit keys are
229 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
230 * same as DES. Implementers MUST reject keys that exhibit this
234 static int des3_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
235 unsigned int key_len
)
237 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
238 u32
*flags
= &tfm
->crt_flags
;
240 if (!(memcmp(key
, &key
[DES_KEY_SIZE
], DES_KEY_SIZE
) &&
241 memcmp(&key
[DES_KEY_SIZE
], &key
[DES_KEY_SIZE
* 2],
243 (*flags
& CRYPTO_TFM_REQ_WEAK_KEY
)) {
244 *flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
247 memcpy(ctx
->key
, key
, key_len
);
251 static void des3_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
253 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
255 crypt_s390_km(KM_TDEA_192_ENCRYPT
, ctx
->key
, dst
, src
, DES_BLOCK_SIZE
);
258 static void des3_decrypt(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_DECRYPT
, ctx
->key
, dst
, src
, DES_BLOCK_SIZE
);
265 static struct crypto_alg des3_alg
= {
266 .cra_name
= "des3_ede",
267 .cra_driver_name
= "des3_ede-s390",
268 .cra_priority
= CRYPT_S390_PRIORITY
,
269 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
270 .cra_blocksize
= DES_BLOCK_SIZE
,
271 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
272 .cra_module
= THIS_MODULE
,
275 .cia_min_keysize
= DES3_KEY_SIZE
,
276 .cia_max_keysize
= DES3_KEY_SIZE
,
277 .cia_setkey
= des3_setkey
,
278 .cia_encrypt
= des3_encrypt
,
279 .cia_decrypt
= des3_decrypt
,
284 static int ecb_des3_encrypt(struct blkcipher_desc
*desc
,
285 struct scatterlist
*dst
, struct scatterlist
*src
,
288 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
289 struct blkcipher_walk walk
;
291 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
292 return ecb_desall_crypt(desc
, KM_TDEA_192_ENCRYPT
, ctx
->key
, &walk
);
295 static int ecb_des3_decrypt(struct blkcipher_desc
*desc
,
296 struct scatterlist
*dst
, struct scatterlist
*src
,
299 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
300 struct blkcipher_walk walk
;
302 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
303 return ecb_desall_crypt(desc
, KM_TDEA_192_DECRYPT
, ctx
->key
, &walk
);
306 static struct crypto_alg ecb_des3_alg
= {
307 .cra_name
= "ecb(des3_ede)",
308 .cra_driver_name
= "ecb-des3_ede-s390",
309 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
310 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
311 .cra_blocksize
= DES_BLOCK_SIZE
,
312 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
313 .cra_type
= &crypto_blkcipher_type
,
314 .cra_module
= THIS_MODULE
,
317 .min_keysize
= DES3_KEY_SIZE
,
318 .max_keysize
= DES3_KEY_SIZE
,
319 .setkey
= des3_setkey
,
320 .encrypt
= ecb_des3_encrypt
,
321 .decrypt
= ecb_des3_decrypt
,
326 static int cbc_des3_encrypt(struct blkcipher_desc
*desc
,
327 struct scatterlist
*dst
, struct scatterlist
*src
,
330 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
331 struct blkcipher_walk walk
;
333 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
334 return cbc_desall_crypt(desc
, KMC_TDEA_192_ENCRYPT
, ctx
->iv
, &walk
);
337 static int cbc_des3_decrypt(struct blkcipher_desc
*desc
,
338 struct scatterlist
*dst
, struct scatterlist
*src
,
341 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
342 struct blkcipher_walk walk
;
344 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
345 return cbc_desall_crypt(desc
, KMC_TDEA_192_DECRYPT
, ctx
->iv
, &walk
);
348 static struct crypto_alg cbc_des3_alg
= {
349 .cra_name
= "cbc(des3_ede)",
350 .cra_driver_name
= "cbc-des3_ede-s390",
351 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
352 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
353 .cra_blocksize
= DES_BLOCK_SIZE
,
354 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
355 .cra_type
= &crypto_blkcipher_type
,
356 .cra_module
= THIS_MODULE
,
359 .min_keysize
= DES3_KEY_SIZE
,
360 .max_keysize
= DES3_KEY_SIZE
,
361 .ivsize
= DES_BLOCK_SIZE
,
362 .setkey
= des3_setkey
,
363 .encrypt
= cbc_des3_encrypt
,
364 .decrypt
= cbc_des3_decrypt
,
369 static int ctr_desall_crypt(struct blkcipher_desc
*desc
, long func
,
370 struct s390_des_ctx
*ctx
, struct blkcipher_walk
*walk
)
372 int ret
= blkcipher_walk_virt_block(desc
, walk
, DES_BLOCK_SIZE
);
373 unsigned int i
, n
, nbytes
;
374 u8 buf
[DES_BLOCK_SIZE
];
377 memcpy(ctrblk
, walk
->iv
, DES_BLOCK_SIZE
);
378 while ((nbytes
= walk
->nbytes
) >= DES_BLOCK_SIZE
) {
379 out
= walk
->dst
.virt
.addr
;
380 in
= walk
->src
.virt
.addr
;
381 while (nbytes
>= DES_BLOCK_SIZE
) {
382 /* align to block size, max. PAGE_SIZE */
383 n
= (nbytes
> PAGE_SIZE
) ? PAGE_SIZE
:
384 nbytes
& ~(DES_BLOCK_SIZE
- 1);
385 for (i
= DES_BLOCK_SIZE
; i
< n
; i
+= DES_BLOCK_SIZE
) {
386 memcpy(ctrblk
+ i
, ctrblk
+ i
- DES_BLOCK_SIZE
,
388 crypto_inc(ctrblk
+ i
, DES_BLOCK_SIZE
);
390 ret
= crypt_s390_kmctr(func
, ctx
->key
, out
, in
, n
, ctrblk
);
391 if (ret
< 0 || ret
!= n
)
393 if (n
> DES_BLOCK_SIZE
)
394 memcpy(ctrblk
, ctrblk
+ n
- DES_BLOCK_SIZE
,
396 crypto_inc(ctrblk
, DES_BLOCK_SIZE
);
401 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
404 /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
406 out
= walk
->dst
.virt
.addr
;
407 in
= walk
->src
.virt
.addr
;
408 ret
= crypt_s390_kmctr(func
, ctx
->key
, buf
, in
,
409 DES_BLOCK_SIZE
, ctrblk
);
410 if (ret
< 0 || ret
!= DES_BLOCK_SIZE
)
412 memcpy(out
, buf
, nbytes
);
413 crypto_inc(ctrblk
, DES_BLOCK_SIZE
);
414 ret
= blkcipher_walk_done(desc
, walk
, 0);
416 memcpy(walk
->iv
, ctrblk
, DES_BLOCK_SIZE
);
420 static int ctr_des_encrypt(struct blkcipher_desc
*desc
,
421 struct scatterlist
*dst
, struct scatterlist
*src
,
424 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
425 struct blkcipher_walk walk
;
427 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
428 return ctr_desall_crypt(desc
, KMCTR_DEA_ENCRYPT
, ctx
, &walk
);
431 static int ctr_des_decrypt(struct blkcipher_desc
*desc
,
432 struct scatterlist
*dst
, struct scatterlist
*src
,
435 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
436 struct blkcipher_walk walk
;
438 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
439 return ctr_desall_crypt(desc
, KMCTR_DEA_DECRYPT
, ctx
, &walk
);
442 static struct crypto_alg ctr_des_alg
= {
443 .cra_name
= "ctr(des)",
444 .cra_driver_name
= "ctr-des-s390",
445 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
446 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
448 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
449 .cra_type
= &crypto_blkcipher_type
,
450 .cra_module
= THIS_MODULE
,
453 .min_keysize
= DES_KEY_SIZE
,
454 .max_keysize
= DES_KEY_SIZE
,
455 .ivsize
= DES_BLOCK_SIZE
,
456 .setkey
= des_setkey
,
457 .encrypt
= ctr_des_encrypt
,
458 .decrypt
= ctr_des_decrypt
,
463 static int ctr_des3_encrypt(struct blkcipher_desc
*desc
,
464 struct scatterlist
*dst
, struct scatterlist
*src
,
467 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
468 struct blkcipher_walk walk
;
470 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
471 return ctr_desall_crypt(desc
, KMCTR_TDEA_192_ENCRYPT
, ctx
, &walk
);
474 static int ctr_des3_decrypt(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_DECRYPT
, ctx
, &walk
);
485 static struct crypto_alg ctr_des3_alg
= {
486 .cra_name
= "ctr(des3_ede)",
487 .cra_driver_name
= "ctr-des3_ede-s390",
488 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
489 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
491 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
492 .cra_type
= &crypto_blkcipher_type
,
493 .cra_module
= THIS_MODULE
,
496 .min_keysize
= DES3_KEY_SIZE
,
497 .max_keysize
= DES3_KEY_SIZE
,
498 .ivsize
= DES_BLOCK_SIZE
,
499 .setkey
= des3_setkey
,
500 .encrypt
= ctr_des3_encrypt
,
501 .decrypt
= ctr_des3_decrypt
,
506 static int __init
des_s390_init(void)
510 if (!crypt_s390_func_available(KM_DEA_ENCRYPT
, CRYPT_S390_MSA
) ||
511 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT
, CRYPT_S390_MSA
))
514 ret
= crypto_register_alg(&des_alg
);
517 ret
= crypto_register_alg(&ecb_des_alg
);
520 ret
= crypto_register_alg(&cbc_des_alg
);
523 ret
= crypto_register_alg(&des3_alg
);
526 ret
= crypto_register_alg(&ecb_des3_alg
);
529 ret
= crypto_register_alg(&cbc_des3_alg
);
533 if (crypt_s390_func_available(KMCTR_DEA_ENCRYPT
,
534 CRYPT_S390_MSA
| CRYPT_S390_MSA4
) &&
535 crypt_s390_func_available(KMCTR_TDEA_192_ENCRYPT
,
536 CRYPT_S390_MSA
| CRYPT_S390_MSA4
)) {
537 ret
= crypto_register_alg(&ctr_des_alg
);
540 ret
= crypto_register_alg(&ctr_des3_alg
);
543 ctrblk
= (u8
*) __get_free_page(GFP_KERNEL
);
553 crypto_unregister_alg(&ctr_des3_alg
);
555 crypto_unregister_alg(&ctr_des_alg
);
557 crypto_unregister_alg(&cbc_des3_alg
);
559 crypto_unregister_alg(&ecb_des3_alg
);
561 crypto_unregister_alg(&des3_alg
);
563 crypto_unregister_alg(&cbc_des_alg
);
565 crypto_unregister_alg(&ecb_des_alg
);
567 crypto_unregister_alg(&des_alg
);
572 static void __exit
des_s390_exit(void)
575 crypto_unregister_alg(&ctr_des_alg
);
576 crypto_unregister_alg(&ctr_des3_alg
);
577 free_page((unsigned long) ctrblk
);
579 crypto_unregister_alg(&cbc_des3_alg
);
580 crypto_unregister_alg(&ecb_des3_alg
);
581 crypto_unregister_alg(&des3_alg
);
582 crypto_unregister_alg(&cbc_des_alg
);
583 crypto_unregister_alg(&ecb_des_alg
);
584 crypto_unregister_alg(&des_alg
);
587 module_init(des_s390_init
);
588 module_exit(des_s390_exit
);
591 MODULE_ALIAS("des3_ede");
593 MODULE_LICENSE("GPL");
594 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");