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/cpufeature.h>
20 #include <linux/crypto.h>
21 #include <linux/fips.h>
22 #include <crypto/algapi.h>
23 #include <crypto/des.h>
24 #include <asm/cpacf.h>
26 #define DES3_KEY_SIZE (3 * DES_KEY_SIZE)
29 static DEFINE_SPINLOCK(ctrblk_lock
);
31 static cpacf_mask_t km_functions
, kmc_functions
, kmctr_functions
;
34 u8 iv
[DES_BLOCK_SIZE
];
35 u8 key
[DES3_KEY_SIZE
];
38 static int des_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
41 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
42 u32 tmp
[DES_EXPKEY_WORDS
];
44 /* check for weak keys */
45 if (!des_ekey(tmp
, key
) &&
46 (tfm
->crt_flags
& CRYPTO_TFM_REQ_WEAK_KEY
)) {
47 tfm
->crt_flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
51 memcpy(ctx
->key
, key
, key_len
);
55 static void des_encrypt(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
57 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
59 cpacf_km(CPACF_KM_DEA
, ctx
->key
, out
, in
, DES_BLOCK_SIZE
);
62 static void des_decrypt(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
64 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
66 cpacf_km(CPACF_KM_DEA
| CPACF_DECRYPT
,
67 ctx
->key
, out
, in
, DES_BLOCK_SIZE
);
70 static struct crypto_alg des_alg
= {
72 .cra_driver_name
= "des-s390",
74 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
75 .cra_blocksize
= DES_BLOCK_SIZE
,
76 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
77 .cra_module
= THIS_MODULE
,
80 .cia_min_keysize
= DES_KEY_SIZE
,
81 .cia_max_keysize
= DES_KEY_SIZE
,
82 .cia_setkey
= des_setkey
,
83 .cia_encrypt
= des_encrypt
,
84 .cia_decrypt
= des_decrypt
,
89 static int ecb_desall_crypt(struct blkcipher_desc
*desc
, unsigned long fc
,
90 struct blkcipher_walk
*walk
)
92 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
93 unsigned int nbytes
, n
;
96 ret
= blkcipher_walk_virt(desc
, walk
);
97 while ((nbytes
= walk
->nbytes
) >= DES_BLOCK_SIZE
) {
98 /* only use complete blocks */
99 n
= nbytes
& ~(DES_BLOCK_SIZE
- 1);
100 cpacf_km(fc
, ctx
->key
, walk
->dst
.virt
.addr
,
101 walk
->src
.virt
.addr
, n
);
102 ret
= blkcipher_walk_done(desc
, walk
, nbytes
- n
);
107 static int cbc_desall_crypt(struct blkcipher_desc
*desc
, unsigned long fc
,
108 struct blkcipher_walk
*walk
)
110 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
111 unsigned int nbytes
, n
;
114 u8 iv
[DES_BLOCK_SIZE
];
115 u8 key
[DES3_KEY_SIZE
];
118 ret
= blkcipher_walk_virt(desc
, walk
);
119 memcpy(param
.iv
, walk
->iv
, DES_BLOCK_SIZE
);
120 memcpy(param
.key
, ctx
->key
, DES3_KEY_SIZE
);
121 while ((nbytes
= walk
->nbytes
) >= DES_BLOCK_SIZE
) {
122 /* only use complete blocks */
123 n
= nbytes
& ~(DES_BLOCK_SIZE
- 1);
124 cpacf_kmc(fc
, ¶m
, walk
->dst
.virt
.addr
,
125 walk
->src
.virt
.addr
, n
);
126 ret
= blkcipher_walk_done(desc
, walk
, nbytes
- n
);
128 memcpy(walk
->iv
, param
.iv
, DES_BLOCK_SIZE
);
132 static int ecb_des_encrypt(struct blkcipher_desc
*desc
,
133 struct scatterlist
*dst
, struct scatterlist
*src
,
136 struct blkcipher_walk walk
;
138 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
139 return ecb_desall_crypt(desc
, CPACF_KM_DEA
, &walk
);
142 static int ecb_des_decrypt(struct blkcipher_desc
*desc
,
143 struct scatterlist
*dst
, struct scatterlist
*src
,
146 struct blkcipher_walk walk
;
148 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
149 return ecb_desall_crypt(desc
, CPACF_KM_DEA
| CPACF_DECRYPT
, &walk
);
152 static struct crypto_alg ecb_des_alg
= {
153 .cra_name
= "ecb(des)",
154 .cra_driver_name
= "ecb-des-s390",
155 .cra_priority
= 400, /* combo: des + ecb */
156 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
157 .cra_blocksize
= DES_BLOCK_SIZE
,
158 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
159 .cra_type
= &crypto_blkcipher_type
,
160 .cra_module
= THIS_MODULE
,
163 .min_keysize
= DES_KEY_SIZE
,
164 .max_keysize
= DES_KEY_SIZE
,
165 .setkey
= des_setkey
,
166 .encrypt
= ecb_des_encrypt
,
167 .decrypt
= ecb_des_decrypt
,
172 static int cbc_des_encrypt(struct blkcipher_desc
*desc
,
173 struct scatterlist
*dst
, struct scatterlist
*src
,
176 struct blkcipher_walk walk
;
178 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
179 return cbc_desall_crypt(desc
, CPACF_KMC_DEA
, &walk
);
182 static int cbc_des_decrypt(struct blkcipher_desc
*desc
,
183 struct scatterlist
*dst
, struct scatterlist
*src
,
186 struct blkcipher_walk walk
;
188 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
189 return cbc_desall_crypt(desc
, CPACF_KMC_DEA
| CPACF_DECRYPT
, &walk
);
192 static struct crypto_alg cbc_des_alg
= {
193 .cra_name
= "cbc(des)",
194 .cra_driver_name
= "cbc-des-s390",
195 .cra_priority
= 400, /* combo: des + cbc */
196 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
197 .cra_blocksize
= DES_BLOCK_SIZE
,
198 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
199 .cra_type
= &crypto_blkcipher_type
,
200 .cra_module
= THIS_MODULE
,
203 .min_keysize
= DES_KEY_SIZE
,
204 .max_keysize
= DES_KEY_SIZE
,
205 .ivsize
= DES_BLOCK_SIZE
,
206 .setkey
= des_setkey
,
207 .encrypt
= cbc_des_encrypt
,
208 .decrypt
= cbc_des_decrypt
,
216 * For DES-EDE3, there is no known need to reject weak or
217 * complementation keys. Any weakness is obviated by the use of
220 * However, if the first two or last two independent 64-bit keys are
221 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
222 * same as DES. Implementers MUST reject keys that exhibit this
225 * In fips mode additinally check for all 3 keys are unique.
228 static int des3_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
229 unsigned int key_len
)
231 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
233 if (!(crypto_memneq(key
, &key
[DES_KEY_SIZE
], DES_KEY_SIZE
) &&
234 crypto_memneq(&key
[DES_KEY_SIZE
], &key
[DES_KEY_SIZE
* 2],
236 (tfm
->crt_flags
& CRYPTO_TFM_REQ_WEAK_KEY
)) {
237 tfm
->crt_flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
241 /* in fips mode, ensure k1 != k2 and k2 != k3 and k1 != k3 */
243 !(crypto_memneq(key
, &key
[DES_KEY_SIZE
], DES_KEY_SIZE
) &&
244 crypto_memneq(&key
[DES_KEY_SIZE
], &key
[DES_KEY_SIZE
* 2],
246 crypto_memneq(key
, &key
[DES_KEY_SIZE
* 2], DES_KEY_SIZE
))) {
247 tfm
->crt_flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
251 memcpy(ctx
->key
, key
, key_len
);
255 static void des3_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
257 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
259 cpacf_km(CPACF_KM_TDEA_192
, ctx
->key
, dst
, src
, DES_BLOCK_SIZE
);
262 static void des3_decrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
264 struct s390_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
266 cpacf_km(CPACF_KM_TDEA_192
| CPACF_DECRYPT
,
267 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",
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 blkcipher_walk walk
;
295 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
296 return ecb_desall_crypt(desc
, CPACF_KM_TDEA_192
, &walk
);
299 static int ecb_des3_decrypt(struct blkcipher_desc
*desc
,
300 struct scatterlist
*dst
, struct scatterlist
*src
,
303 struct blkcipher_walk walk
;
305 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
306 return ecb_desall_crypt(desc
, CPACF_KM_TDEA_192
| CPACF_DECRYPT
,
310 static struct crypto_alg ecb_des3_alg
= {
311 .cra_name
= "ecb(des3_ede)",
312 .cra_driver_name
= "ecb-des3_ede-s390",
313 .cra_priority
= 400, /* combo: des3 + ecb */
314 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
315 .cra_blocksize
= DES_BLOCK_SIZE
,
316 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
317 .cra_type
= &crypto_blkcipher_type
,
318 .cra_module
= THIS_MODULE
,
321 .min_keysize
= DES3_KEY_SIZE
,
322 .max_keysize
= DES3_KEY_SIZE
,
323 .setkey
= des3_setkey
,
324 .encrypt
= ecb_des3_encrypt
,
325 .decrypt
= ecb_des3_decrypt
,
330 static int cbc_des3_encrypt(struct blkcipher_desc
*desc
,
331 struct scatterlist
*dst
, struct scatterlist
*src
,
334 struct blkcipher_walk walk
;
336 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
337 return cbc_desall_crypt(desc
, CPACF_KMC_TDEA_192
, &walk
);
340 static int cbc_des3_decrypt(struct blkcipher_desc
*desc
,
341 struct scatterlist
*dst
, struct scatterlist
*src
,
344 struct blkcipher_walk walk
;
346 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
347 return cbc_desall_crypt(desc
, CPACF_KMC_TDEA_192
| CPACF_DECRYPT
,
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
= 400, /* combo: des3 + cbc */
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
, u8
*iv
, unsigned int nbytes
)
376 /* align to block size, max. PAGE_SIZE */
377 n
= (nbytes
> PAGE_SIZE
) ? PAGE_SIZE
: nbytes
& ~(DES_BLOCK_SIZE
- 1);
378 memcpy(ctrptr
, iv
, DES_BLOCK_SIZE
);
379 for (i
= (n
/ DES_BLOCK_SIZE
) - 1; i
> 0; i
--) {
380 memcpy(ctrptr
+ DES_BLOCK_SIZE
, ctrptr
, DES_BLOCK_SIZE
);
381 crypto_inc(ctrptr
+ DES_BLOCK_SIZE
, DES_BLOCK_SIZE
);
382 ctrptr
+= DES_BLOCK_SIZE
;
387 static int ctr_desall_crypt(struct blkcipher_desc
*desc
, unsigned long fc
,
388 struct blkcipher_walk
*walk
)
390 struct s390_des_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
391 u8 buf
[DES_BLOCK_SIZE
], *ctrptr
;
392 unsigned int n
, nbytes
;
395 locked
= spin_trylock(&ctrblk_lock
);
397 ret
= blkcipher_walk_virt_block(desc
, walk
, DES_BLOCK_SIZE
);
398 while ((nbytes
= walk
->nbytes
) >= DES_BLOCK_SIZE
) {
400 if (nbytes
>= 2*DES_BLOCK_SIZE
&& locked
)
401 n
= __ctrblk_init(ctrblk
, walk
->iv
, nbytes
);
402 ctrptr
= (n
> DES_BLOCK_SIZE
) ? ctrblk
: walk
->iv
;
403 cpacf_kmctr(fc
, ctx
->key
, walk
->dst
.virt
.addr
,
404 walk
->src
.virt
.addr
, n
, ctrptr
);
405 if (ctrptr
== ctrblk
)
406 memcpy(walk
->iv
, ctrptr
+ n
- DES_BLOCK_SIZE
,
408 crypto_inc(walk
->iv
, DES_BLOCK_SIZE
);
409 ret
= blkcipher_walk_done(desc
, walk
, nbytes
- n
);
412 spin_unlock(&ctrblk_lock
);
413 /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
415 cpacf_kmctr(fc
, ctx
->key
, buf
, walk
->src
.virt
.addr
,
416 DES_BLOCK_SIZE
, walk
->iv
);
417 memcpy(walk
->dst
.virt
.addr
, buf
, nbytes
);
418 crypto_inc(walk
->iv
, DES_BLOCK_SIZE
);
419 ret
= blkcipher_walk_done(desc
, walk
, 0);
424 static int ctr_des_encrypt(struct blkcipher_desc
*desc
,
425 struct scatterlist
*dst
, struct scatterlist
*src
,
428 struct blkcipher_walk walk
;
430 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
431 return ctr_desall_crypt(desc
, CPACF_KMCTR_DEA
, &walk
);
434 static int ctr_des_decrypt(struct blkcipher_desc
*desc
,
435 struct scatterlist
*dst
, struct scatterlist
*src
,
438 struct blkcipher_walk walk
;
440 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
441 return ctr_desall_crypt(desc
, CPACF_KMCTR_DEA
| CPACF_DECRYPT
, &walk
);
444 static struct crypto_alg ctr_des_alg
= {
445 .cra_name
= "ctr(des)",
446 .cra_driver_name
= "ctr-des-s390",
447 .cra_priority
= 400, /* combo: des + ctr */
448 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
450 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
451 .cra_type
= &crypto_blkcipher_type
,
452 .cra_module
= THIS_MODULE
,
455 .min_keysize
= DES_KEY_SIZE
,
456 .max_keysize
= DES_KEY_SIZE
,
457 .ivsize
= DES_BLOCK_SIZE
,
458 .setkey
= des_setkey
,
459 .encrypt
= ctr_des_encrypt
,
460 .decrypt
= ctr_des_decrypt
,
465 static int ctr_des3_encrypt(struct blkcipher_desc
*desc
,
466 struct scatterlist
*dst
, struct scatterlist
*src
,
469 struct blkcipher_walk walk
;
471 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
472 return ctr_desall_crypt(desc
, CPACF_KMCTR_TDEA_192
, &walk
);
475 static int ctr_des3_decrypt(struct blkcipher_desc
*desc
,
476 struct scatterlist
*dst
, struct scatterlist
*src
,
479 struct blkcipher_walk walk
;
481 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
482 return ctr_desall_crypt(desc
, CPACF_KMCTR_TDEA_192
| CPACF_DECRYPT
,
486 static struct crypto_alg ctr_des3_alg
= {
487 .cra_name
= "ctr(des3_ede)",
488 .cra_driver_name
= "ctr-des3_ede-s390",
489 .cra_priority
= 400, /* combo: des3 + ede */
490 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
492 .cra_ctxsize
= sizeof(struct s390_des_ctx
),
493 .cra_type
= &crypto_blkcipher_type
,
494 .cra_module
= THIS_MODULE
,
497 .min_keysize
= DES3_KEY_SIZE
,
498 .max_keysize
= DES3_KEY_SIZE
,
499 .ivsize
= DES_BLOCK_SIZE
,
500 .setkey
= des3_setkey
,
501 .encrypt
= ctr_des3_encrypt
,
502 .decrypt
= ctr_des3_decrypt
,
507 static struct crypto_alg
*des_s390_algs_ptr
[8];
508 static int des_s390_algs_num
;
510 static int des_s390_register_alg(struct crypto_alg
*alg
)
514 ret
= crypto_register_alg(alg
);
516 des_s390_algs_ptr
[des_s390_algs_num
++] = alg
;
520 static void des_s390_exit(void)
522 while (des_s390_algs_num
--)
523 crypto_unregister_alg(des_s390_algs_ptr
[des_s390_algs_num
]);
525 free_page((unsigned long) ctrblk
);
528 static int __init
des_s390_init(void)
532 /* Query available functions for KM, KMC and KMCTR */
533 cpacf_query(CPACF_KM
, &km_functions
);
534 cpacf_query(CPACF_KMC
, &kmc_functions
);
535 cpacf_query(CPACF_KMCTR
, &kmctr_functions
);
537 if (cpacf_test_func(&km_functions
, CPACF_KM_DEA
)) {
538 ret
= des_s390_register_alg(&des_alg
);
541 ret
= des_s390_register_alg(&ecb_des_alg
);
545 if (cpacf_test_func(&kmc_functions
, CPACF_KMC_DEA
)) {
546 ret
= des_s390_register_alg(&cbc_des_alg
);
550 if (cpacf_test_func(&km_functions
, CPACF_KM_TDEA_192
)) {
551 ret
= des_s390_register_alg(&des3_alg
);
554 ret
= des_s390_register_alg(&ecb_des3_alg
);
558 if (cpacf_test_func(&kmc_functions
, CPACF_KMC_TDEA_192
)) {
559 ret
= des_s390_register_alg(&cbc_des3_alg
);
564 if (cpacf_test_func(&kmctr_functions
, CPACF_KMCTR_DEA
) ||
565 cpacf_test_func(&kmctr_functions
, CPACF_KMCTR_TDEA_192
)) {
566 ctrblk
= (u8
*) __get_free_page(GFP_KERNEL
);
573 if (cpacf_test_func(&kmctr_functions
, CPACF_KMCTR_DEA
)) {
574 ret
= des_s390_register_alg(&ctr_des_alg
);
578 if (cpacf_test_func(&kmctr_functions
, CPACF_KMCTR_TDEA_192
)) {
579 ret
= des_s390_register_alg(&ctr_des3_alg
);
590 module_cpu_feature_match(MSA
, des_s390_init
);
591 module_exit(des_s390_exit
);
593 MODULE_ALIAS_CRYPTO("des");
594 MODULE_ALIAS_CRYPTO("des3_ede");
596 MODULE_LICENSE("GPL");
597 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");