Linux 4.13.16
[linux/fpc-iii.git] / arch / s390 / crypto / des_s390.c
blob0d296662bbf0aba03dbaa79de182114e315b6184
1 /*
2 * Cryptographic API.
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)
28 static u8 *ctrblk;
29 static DEFINE_SPINLOCK(ctrblk_lock);
31 static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
33 struct s390_des_ctx {
34 u8 iv[DES_BLOCK_SIZE];
35 u8 key[DES3_KEY_SIZE];
38 static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
39 unsigned int key_len)
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;
48 return -EINVAL;
51 memcpy(ctx->key, key, key_len);
52 return 0;
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 = {
71 .cra_name = "des",
72 .cra_driver_name = "des-s390",
73 .cra_priority = 300,
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,
78 .cra_u = {
79 .cipher = {
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;
94 int ret;
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);
104 return ret;
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;
112 int ret;
113 struct {
114 u8 iv[DES_BLOCK_SIZE];
115 u8 key[DES3_KEY_SIZE];
116 } param;
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, &param, 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);
129 return ret;
132 static int ecb_des_encrypt(struct blkcipher_desc *desc,
133 struct scatterlist *dst, struct scatterlist *src,
134 unsigned int nbytes)
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,
144 unsigned int nbytes)
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,
161 .cra_u = {
162 .blkcipher = {
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,
174 unsigned int nbytes)
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,
184 unsigned int nbytes)
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,
201 .cra_u = {
202 .blkcipher = {
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,
214 * RFC2451:
216 * For DES-EDE3, there is no known need to reject weak or
217 * complementation keys. Any weakness is obviated by the use of
218 * multiple keys.
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
223 * property.
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],
235 DES_KEY_SIZE)) &&
236 (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
237 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
238 return -EINVAL;
241 /* in fips mode, ensure k1 != k2 and k2 != k3 and k1 != k3 */
242 if (fips_enabled &&
243 !(crypto_memneq(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
244 crypto_memneq(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
245 DES_KEY_SIZE) &&
246 crypto_memneq(key, &key[DES_KEY_SIZE * 2], DES_KEY_SIZE))) {
247 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
248 return -EINVAL;
251 memcpy(ctx->key, key, key_len);
252 return 0;
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",
273 .cra_priority = 300,
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,
278 .cra_u = {
279 .cipher = {
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,
291 unsigned int nbytes)
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,
301 unsigned int nbytes)
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,
307 &walk);
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,
319 .cra_u = {
320 .blkcipher = {
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,
332 unsigned int nbytes)
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,
342 unsigned int nbytes)
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,
348 &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 = 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,
360 .cra_u = {
361 .blkcipher = {
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)
374 unsigned int i, n;
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;
384 return n;
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;
393 int ret, locked;
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) {
399 n = 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,
407 DES_BLOCK_SIZE);
408 crypto_inc(walk->iv, DES_BLOCK_SIZE);
409 ret = blkcipher_walk_done(desc, walk, nbytes - n);
411 if (locked)
412 spin_unlock(&ctrblk_lock);
413 /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
414 if (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);
421 return ret;
424 static int ctr_des_encrypt(struct blkcipher_desc *desc,
425 struct scatterlist *dst, struct scatterlist *src,
426 unsigned int nbytes)
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,
436 unsigned int nbytes)
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,
449 .cra_blocksize = 1,
450 .cra_ctxsize = sizeof(struct s390_des_ctx),
451 .cra_type = &crypto_blkcipher_type,
452 .cra_module = THIS_MODULE,
453 .cra_u = {
454 .blkcipher = {
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,
467 unsigned int nbytes)
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,
477 unsigned int nbytes)
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,
483 &walk);
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,
491 .cra_blocksize = 1,
492 .cra_ctxsize = sizeof(struct s390_des_ctx),
493 .cra_type = &crypto_blkcipher_type,
494 .cra_module = THIS_MODULE,
495 .cra_u = {
496 .blkcipher = {
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)
512 int ret;
514 ret = crypto_register_alg(alg);
515 if (!ret)
516 des_s390_algs_ptr[des_s390_algs_num++] = alg;
517 return ret;
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]);
524 if (ctrblk)
525 free_page((unsigned long) ctrblk);
528 static int __init des_s390_init(void)
530 int ret;
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);
539 if (ret)
540 goto out_err;
541 ret = des_s390_register_alg(&ecb_des_alg);
542 if (ret)
543 goto out_err;
545 if (cpacf_test_func(&kmc_functions, CPACF_KMC_DEA)) {
546 ret = des_s390_register_alg(&cbc_des_alg);
547 if (ret)
548 goto out_err;
550 if (cpacf_test_func(&km_functions, CPACF_KM_TDEA_192)) {
551 ret = des_s390_register_alg(&des3_alg);
552 if (ret)
553 goto out_err;
554 ret = des_s390_register_alg(&ecb_des3_alg);
555 if (ret)
556 goto out_err;
558 if (cpacf_test_func(&kmc_functions, CPACF_KMC_TDEA_192)) {
559 ret = des_s390_register_alg(&cbc_des3_alg);
560 if (ret)
561 goto out_err;
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);
567 if (!ctrblk) {
568 ret = -ENOMEM;
569 goto out_err;
573 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA)) {
574 ret = des_s390_register_alg(&ctr_des_alg);
575 if (ret)
576 goto out_err;
578 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) {
579 ret = des_s390_register_alg(&ctr_des3_alg);
580 if (ret)
581 goto out_err;
584 return 0;
585 out_err:
586 des_s390_exit();
587 return ret;
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");