Merge branches 'for-4.8/alps', 'for-4.8/apple', 'for-4.8/i2c-hid', 'for-4.8/uhid...
[deliverable/linux.git] / arch / s390 / crypto / des_s390.c
CommitLineData
1da177e4
LT
1/*
2 * Cryptographic API.
3 *
c1e26e1e 4 * s390 implementation of the DES Cipher Algorithm.
1da177e4 5 *
a53c8fab 6 * Copyright IBM Corp. 2003, 2011
86aa9fc2
JG
7 * Author(s): Thomas Spatzier
8 * Jan Glauber (jan.glauber@de.ibm.com)
1da177e4
LT
9 *
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.
14 *
15 */
a9e62fad 16
1da177e4
LT
17#include <linux/init.h>
18#include <linux/module.h>
d05377c1 19#include <linux/cpufeature.h>
1efbd15c
JG
20#include <linux/crypto.h>
21#include <crypto/algapi.h>
22#include <crypto/des.h>
c7d4d259 23#include <asm/cpacf.h>
1da177e4 24
98971f84 25#define DES3_KEY_SIZE (3 * DES_KEY_SIZE)
1da177e4 26
0200f3ec 27static u8 *ctrblk;
ee97dc7d 28static DEFINE_SPINLOCK(ctrblk_lock);
0200f3ec 29
98971f84 30struct s390_des_ctx {
1da177e4 31 u8 iv[DES_BLOCK_SIZE];
98971f84 32 u8 key[DES3_KEY_SIZE];
1da177e4
LT
33};
34
6c2bb98b 35static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
98971f84 36 unsigned int key_len)
1da177e4 37{
98971f84 38 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
560c06ae 39 u32 *flags = &tfm->crt_flags;
1efbd15c 40 u32 tmp[DES_EXPKEY_WORDS];
1da177e4 41
1efbd15c
JG
42 /* check for weak keys */
43 if (!des_ekey(tmp, key) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
44 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
45 return -EINVAL;
46 }
47
98971f84 48 memcpy(ctx->key, key, key_len);
1efbd15c 49 return 0;
1da177e4
LT
50}
51
6c2bb98b 52static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1da177e4 53{
98971f84 54 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4 55
c7d4d259 56 cpacf_km(CPACF_KM_DEA_ENC, ctx->key, out, in, DES_BLOCK_SIZE);
1da177e4
LT
57}
58
6c2bb98b 59static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1da177e4 60{
98971f84 61 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4 62
c7d4d259 63 cpacf_km(CPACF_KM_DEA_DEC, ctx->key, out, in, DES_BLOCK_SIZE);
b8dc6038
JG
64}
65
1da177e4
LT
66static struct crypto_alg des_alg = {
67 .cra_name = "des",
65b75c36 68 .cra_driver_name = "des-s390",
c7d4d259 69 .cra_priority = 300,
1da177e4
LT
70 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
71 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 72 .cra_ctxsize = sizeof(struct s390_des_ctx),
1da177e4 73 .cra_module = THIS_MODULE,
c1357833
JG
74 .cra_u = {
75 .cipher = {
76 .cia_min_keysize = DES_KEY_SIZE,
77 .cia_max_keysize = DES_KEY_SIZE,
78 .cia_setkey = des_setkey,
79 .cia_encrypt = des_encrypt,
b8dc6038 80 .cia_decrypt = des_decrypt,
c1357833
JG
81 }
82 }
1da177e4
LT
83};
84
a9e62fad 85static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
98971f84 86 u8 *key, struct blkcipher_walk *walk)
a9e62fad
HX
87{
88 int ret = blkcipher_walk_virt(desc, walk);
89 unsigned int nbytes;
90
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;
96
c7d4d259 97 ret = cpacf_km(func, key, out, in, n);
36eb2caa
JG
98 if (ret < 0 || ret != n)
99 return -EIO;
a9e62fad
HX
100
101 nbytes &= DES_BLOCK_SIZE - 1;
102 ret = blkcipher_walk_done(desc, walk, nbytes);
103 }
104
105 return ret;
106}
107
108static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
adc3fcf1 109 struct blkcipher_walk *walk)
a9e62fad 110{
adc3fcf1 111 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
a9e62fad
HX
112 int ret = blkcipher_walk_virt(desc, walk);
113 unsigned int nbytes = walk->nbytes;
adc3fcf1
HF
114 struct {
115 u8 iv[DES_BLOCK_SIZE];
116 u8 key[DES3_KEY_SIZE];
117 } param;
a9e62fad
HX
118
119 if (!nbytes)
120 goto out;
121
adc3fcf1
HF
122 memcpy(param.iv, walk->iv, DES_BLOCK_SIZE);
123 memcpy(param.key, ctx->key, DES3_KEY_SIZE);
a9e62fad
HX
124 do {
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;
129
c7d4d259 130 ret = cpacf_kmc(func, &param, out, in, n);
36eb2caa
JG
131 if (ret < 0 || ret != n)
132 return -EIO;
a9e62fad
HX
133
134 nbytes &= DES_BLOCK_SIZE - 1;
135 ret = blkcipher_walk_done(desc, walk, nbytes);
136 } while ((nbytes = walk->nbytes));
adc3fcf1 137 memcpy(walk->iv, param.iv, DES_BLOCK_SIZE);
a9e62fad
HX
138
139out:
140 return ret;
141}
142
143static int ecb_des_encrypt(struct blkcipher_desc *desc,
144 struct scatterlist *dst, struct scatterlist *src,
145 unsigned int nbytes)
146{
98971f84 147 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
a9e62fad
HX
148 struct blkcipher_walk walk;
149
150 blkcipher_walk_init(&walk, dst, src, nbytes);
c7d4d259 151 return ecb_desall_crypt(desc, CPACF_KM_DEA_ENC, ctx->key, &walk);
a9e62fad
HX
152}
153
154static int ecb_des_decrypt(struct blkcipher_desc *desc,
155 struct scatterlist *dst, struct scatterlist *src,
156 unsigned int nbytes)
157{
98971f84 158 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
a9e62fad
HX
159 struct blkcipher_walk walk;
160
161 blkcipher_walk_init(&walk, dst, src, nbytes);
c7d4d259 162 return ecb_desall_crypt(desc, CPACF_KM_DEA_DEC, ctx->key, &walk);
a9e62fad
HX
163}
164
165static struct crypto_alg ecb_des_alg = {
166 .cra_name = "ecb(des)",
167 .cra_driver_name = "ecb-des-s390",
c7d4d259 168 .cra_priority = 400, /* combo: des + ecb */
a9e62fad
HX
169 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
170 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 171 .cra_ctxsize = sizeof(struct s390_des_ctx),
a9e62fad
HX
172 .cra_type = &crypto_blkcipher_type,
173 .cra_module = THIS_MODULE,
a9e62fad
HX
174 .cra_u = {
175 .blkcipher = {
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,
181 }
182 }
183};
184
185static int cbc_des_encrypt(struct blkcipher_desc *desc,
186 struct scatterlist *dst, struct scatterlist *src,
187 unsigned int nbytes)
188{
a9e62fad
HX
189 struct blkcipher_walk walk;
190
191 blkcipher_walk_init(&walk, dst, src, nbytes);
c7d4d259 192 return cbc_desall_crypt(desc, CPACF_KMC_DEA_ENC, &walk);
a9e62fad
HX
193}
194
195static int cbc_des_decrypt(struct blkcipher_desc *desc,
196 struct scatterlist *dst, struct scatterlist *src,
197 unsigned int nbytes)
198{
a9e62fad
HX
199 struct blkcipher_walk walk;
200
201 blkcipher_walk_init(&walk, dst, src, nbytes);
c7d4d259 202 return cbc_desall_crypt(desc, CPACF_KMC_DEA_DEC, &walk);
a9e62fad
HX
203}
204
205static struct crypto_alg cbc_des_alg = {
206 .cra_name = "cbc(des)",
207 .cra_driver_name = "cbc-des-s390",
c7d4d259 208 .cra_priority = 400, /* combo: des + cbc */
a9e62fad
HX
209 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
210 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 211 .cra_ctxsize = sizeof(struct s390_des_ctx),
a9e62fad
HX
212 .cra_type = &crypto_blkcipher_type,
213 .cra_module = THIS_MODULE,
a9e62fad
HX
214 .cra_u = {
215 .blkcipher = {
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,
222 }
223 }
224};
225
1da177e4
LT
226/*
227 * RFC2451:
228 *
229 * For DES-EDE3, there is no known need to reject weak or
230 * complementation keys. Any weakness is obviated by the use of
231 * multiple keys.
232 *
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
236 * property.
237 *
238 */
98971f84
JG
239static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
240 unsigned int key_len)
1da177e4 241{
98971f84 242 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
560c06ae 243 u32 *flags = &tfm->crt_flags;
1da177e4 244
fed28611
DB
245 if (!(crypto_memneq(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
246 crypto_memneq(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
247 DES_KEY_SIZE)) &&
03b56ce5
JW
248 (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
249 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
1da177e4
LT
250 return -EINVAL;
251 }
98971f84 252 memcpy(ctx->key, key, key_len);
1da177e4
LT
253 return 0;
254}
255
98971f84 256static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
1da177e4 257{
98971f84 258 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4 259
c7d4d259 260 cpacf_km(CPACF_KM_TDEA_192_ENC, ctx->key, dst, src, DES_BLOCK_SIZE);
1da177e4
LT
261}
262
98971f84 263static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
1da177e4 264{
98971f84 265 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4 266
c7d4d259 267 cpacf_km(CPACF_KM_TDEA_192_DEC, ctx->key, dst, src, DES_BLOCK_SIZE);
1da177e4
LT
268}
269
98971f84 270static struct crypto_alg des3_alg = {
1da177e4 271 .cra_name = "des3_ede",
65b75c36 272 .cra_driver_name = "des3_ede-s390",
c7d4d259 273 .cra_priority = 300,
1da177e4 274 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
1efbd15c 275 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 276 .cra_ctxsize = sizeof(struct s390_des_ctx),
1da177e4 277 .cra_module = THIS_MODULE,
c1357833
JG
278 .cra_u = {
279 .cipher = {
98971f84
JG
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,
c1357833
JG
285 }
286 }
1da177e4
LT
287};
288
98971f84
JG
289static int ecb_des3_encrypt(struct blkcipher_desc *desc,
290 struct scatterlist *dst, struct scatterlist *src,
291 unsigned int nbytes)
a9e62fad 292{
98971f84 293 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
a9e62fad
HX
294 struct blkcipher_walk walk;
295
296 blkcipher_walk_init(&walk, dst, src, nbytes);
c7d4d259 297 return ecb_desall_crypt(desc, CPACF_KM_TDEA_192_ENC, ctx->key, &walk);
a9e62fad
HX
298}
299
98971f84
JG
300static int ecb_des3_decrypt(struct blkcipher_desc *desc,
301 struct scatterlist *dst, struct scatterlist *src,
302 unsigned int nbytes)
a9e62fad 303{
98971f84 304 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
a9e62fad
HX
305 struct blkcipher_walk walk;
306
307 blkcipher_walk_init(&walk, dst, src, nbytes);
c7d4d259 308 return ecb_desall_crypt(desc, CPACF_KM_TDEA_192_DEC, ctx->key, &walk);
a9e62fad
HX
309}
310
98971f84 311static struct crypto_alg ecb_des3_alg = {
a9e62fad
HX
312 .cra_name = "ecb(des3_ede)",
313 .cra_driver_name = "ecb-des3_ede-s390",
c7d4d259 314 .cra_priority = 400, /* combo: des3 + ecb */
a9e62fad 315 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1efbd15c 316 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 317 .cra_ctxsize = sizeof(struct s390_des_ctx),
a9e62fad
HX
318 .cra_type = &crypto_blkcipher_type,
319 .cra_module = THIS_MODULE,
a9e62fad
HX
320 .cra_u = {
321 .blkcipher = {
98971f84
JG
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,
a9e62fad
HX
327 }
328 }
329};
330
98971f84
JG
331static int cbc_des3_encrypt(struct blkcipher_desc *desc,
332 struct scatterlist *dst, struct scatterlist *src,
333 unsigned int nbytes)
a9e62fad 334{
a9e62fad
HX
335 struct blkcipher_walk walk;
336
337 blkcipher_walk_init(&walk, dst, src, nbytes);
c7d4d259 338 return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192_ENC, &walk);
a9e62fad
HX
339}
340
98971f84
JG
341static int cbc_des3_decrypt(struct blkcipher_desc *desc,
342 struct scatterlist *dst, struct scatterlist *src,
343 unsigned int nbytes)
a9e62fad 344{
a9e62fad
HX
345 struct blkcipher_walk walk;
346
347 blkcipher_walk_init(&walk, dst, src, nbytes);
c7d4d259 348 return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192_DEC, &walk);
a9e62fad
HX
349}
350
98971f84 351static struct crypto_alg cbc_des3_alg = {
a9e62fad
HX
352 .cra_name = "cbc(des3_ede)",
353 .cra_driver_name = "cbc-des3_ede-s390",
c7d4d259 354 .cra_priority = 400, /* combo: des3 + cbc */
a9e62fad 355 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1efbd15c 356 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 357 .cra_ctxsize = sizeof(struct s390_des_ctx),
a9e62fad
HX
358 .cra_type = &crypto_blkcipher_type,
359 .cra_module = THIS_MODULE,
a9e62fad
HX
360 .cra_u = {
361 .blkcipher = {
98971f84
JG
362 .min_keysize = DES3_KEY_SIZE,
363 .max_keysize = DES3_KEY_SIZE,
1efbd15c 364 .ivsize = DES_BLOCK_SIZE,
98971f84
JG
365 .setkey = des3_setkey,
366 .encrypt = cbc_des3_encrypt,
367 .decrypt = cbc_des3_decrypt,
a9e62fad
HX
368 }
369 }
370};
371
ee97dc7d
HF
372static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes)
373{
374 unsigned int i, n;
375
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);
381 }
382 return n;
383}
384
0200f3ec 385static int ctr_desall_crypt(struct blkcipher_desc *desc, long func,
ee97dc7d
HF
386 struct s390_des_ctx *ctx,
387 struct blkcipher_walk *walk)
0200f3ec
GS
388{
389 int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
ee97dc7d
HF
390 unsigned int n, nbytes;
391 u8 buf[DES_BLOCK_SIZE], ctrbuf[DES_BLOCK_SIZE];
392 u8 *out, *in, *ctrptr = ctrbuf;
393
394 if (!walk->nbytes)
395 return ret;
0200f3ec 396
ee97dc7d
HF
397 if (spin_trylock(&ctrblk_lock))
398 ctrptr = ctrblk;
399
400 memcpy(ctrptr, walk->iv, DES_BLOCK_SIZE);
0200f3ec
GS
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) {
ee97dc7d
HF
405 if (ctrptr == ctrblk)
406 n = __ctrblk_init(ctrptr, nbytes);
407 else
408 n = DES_BLOCK_SIZE;
c7d4d259 409 ret = cpacf_kmctr(func, ctx->key, out, in, n, ctrptr);
ee97dc7d
HF
410 if (ret < 0 || ret != n) {
411 if (ctrptr == ctrblk)
412 spin_unlock(&ctrblk_lock);
36eb2caa 413 return -EIO;
ee97dc7d 414 }
0200f3ec 415 if (n > DES_BLOCK_SIZE)
ee97dc7d 416 memcpy(ctrptr, ctrptr + n - DES_BLOCK_SIZE,
0200f3ec 417 DES_BLOCK_SIZE);
ee97dc7d 418 crypto_inc(ctrptr, DES_BLOCK_SIZE);
0200f3ec
GS
419 out += n;
420 in += n;
421 nbytes -= n;
422 }
423 ret = blkcipher_walk_done(desc, walk, nbytes);
424 }
ee97dc7d
HF
425 if (ctrptr == ctrblk) {
426 if (nbytes)
427 memcpy(ctrbuf, ctrptr, DES_BLOCK_SIZE);
428 else
429 memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
430 spin_unlock(&ctrblk_lock);
3901c112
HF
431 } else {
432 if (!nbytes)
433 memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
ee97dc7d 434 }
0200f3ec
GS
435 /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
436 if (nbytes) {
437 out = walk->dst.virt.addr;
438 in = walk->src.virt.addr;
c7d4d259
MS
439 ret = cpacf_kmctr(func, ctx->key, buf, in,
440 DES_BLOCK_SIZE, ctrbuf);
36eb2caa
JG
441 if (ret < 0 || ret != DES_BLOCK_SIZE)
442 return -EIO;
0200f3ec 443 memcpy(out, buf, nbytes);
ee97dc7d 444 crypto_inc(ctrbuf, DES_BLOCK_SIZE);
0200f3ec 445 ret = blkcipher_walk_done(desc, walk, 0);
ee97dc7d 446 memcpy(walk->iv, ctrbuf, DES_BLOCK_SIZE);
0200f3ec 447 }
0200f3ec
GS
448 return ret;
449}
450
451static int ctr_des_encrypt(struct blkcipher_desc *desc,
452 struct scatterlist *dst, struct scatterlist *src,
453 unsigned int nbytes)
454{
455 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
456 struct blkcipher_walk walk;
457
458 blkcipher_walk_init(&walk, dst, src, nbytes);
c7d4d259 459 return ctr_desall_crypt(desc, CPACF_KMCTR_DEA_ENC, ctx, &walk);
0200f3ec
GS
460}
461
462static int ctr_des_decrypt(struct blkcipher_desc *desc,
463 struct scatterlist *dst, struct scatterlist *src,
464 unsigned int nbytes)
465{
466 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
467 struct blkcipher_walk walk;
468
469 blkcipher_walk_init(&walk, dst, src, nbytes);
c7d4d259 470 return ctr_desall_crypt(desc, CPACF_KMCTR_DEA_DEC, ctx, &walk);
0200f3ec
GS
471}
472
473static struct crypto_alg ctr_des_alg = {
474 .cra_name = "ctr(des)",
475 .cra_driver_name = "ctr-des-s390",
c7d4d259 476 .cra_priority = 400, /* combo: des + ctr */
0200f3ec
GS
477 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
478 .cra_blocksize = 1,
479 .cra_ctxsize = sizeof(struct s390_des_ctx),
480 .cra_type = &crypto_blkcipher_type,
481 .cra_module = THIS_MODULE,
0200f3ec
GS
482 .cra_u = {
483 .blkcipher = {
484 .min_keysize = DES_KEY_SIZE,
485 .max_keysize = DES_KEY_SIZE,
486 .ivsize = DES_BLOCK_SIZE,
487 .setkey = des_setkey,
488 .encrypt = ctr_des_encrypt,
489 .decrypt = ctr_des_decrypt,
490 }
491 }
492};
493
494static int ctr_des3_encrypt(struct blkcipher_desc *desc,
495 struct scatterlist *dst, struct scatterlist *src,
496 unsigned int nbytes)
497{
498 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
499 struct blkcipher_walk walk;
500
501 blkcipher_walk_init(&walk, dst, src, nbytes);
c7d4d259 502 return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192_ENC, ctx, &walk);
0200f3ec
GS
503}
504
505static int ctr_des3_decrypt(struct blkcipher_desc *desc,
506 struct scatterlist *dst, struct scatterlist *src,
507 unsigned int nbytes)
508{
509 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
510 struct blkcipher_walk walk;
511
512 blkcipher_walk_init(&walk, dst, src, nbytes);
c7d4d259 513 return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192_DEC, ctx, &walk);
0200f3ec
GS
514}
515
516static struct crypto_alg ctr_des3_alg = {
517 .cra_name = "ctr(des3_ede)",
518 .cra_driver_name = "ctr-des3_ede-s390",
c7d4d259 519 .cra_priority = 400, /* combo: des3 + ede */
0200f3ec
GS
520 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
521 .cra_blocksize = 1,
522 .cra_ctxsize = sizeof(struct s390_des_ctx),
523 .cra_type = &crypto_blkcipher_type,
524 .cra_module = THIS_MODULE,
0200f3ec
GS
525 .cra_u = {
526 .blkcipher = {
527 .min_keysize = DES3_KEY_SIZE,
528 .max_keysize = DES3_KEY_SIZE,
529 .ivsize = DES_BLOCK_SIZE,
530 .setkey = des3_setkey,
531 .encrypt = ctr_des3_encrypt,
532 .decrypt = ctr_des3_decrypt,
533 }
534 }
535};
536
98971f84 537static int __init des_s390_init(void)
1da177e4 538{
80d663a4 539 int ret;
1da177e4 540
c7d4d259
MS
541 if (!cpacf_query(CPACF_KM, CPACF_KM_DEA_ENC) ||
542 !cpacf_query(CPACF_KM, CPACF_KM_TDEA_192_ENC))
86aa9fc2 543 return -EOPNOTSUPP;
1da177e4 544
a9e62fad
HX
545 ret = crypto_register_alg(&des_alg);
546 if (ret)
547 goto des_err;
548 ret = crypto_register_alg(&ecb_des_alg);
549 if (ret)
550 goto ecb_des_err;
551 ret = crypto_register_alg(&cbc_des_alg);
552 if (ret)
553 goto cbc_des_err;
98971f84 554 ret = crypto_register_alg(&des3_alg);
a9e62fad 555 if (ret)
98971f84
JG
556 goto des3_err;
557 ret = crypto_register_alg(&ecb_des3_alg);
a9e62fad 558 if (ret)
98971f84
JG
559 goto ecb_des3_err;
560 ret = crypto_register_alg(&cbc_des3_alg);
a9e62fad 561 if (ret)
98971f84 562 goto cbc_des3_err;
0200f3ec 563
c7d4d259
MS
564 if (cpacf_query(CPACF_KMCTR, CPACF_KMCTR_DEA_ENC) &&
565 cpacf_query(CPACF_KMCTR, CPACF_KMCTR_TDEA_192_ENC)) {
0200f3ec
GS
566 ret = crypto_register_alg(&ctr_des_alg);
567 if (ret)
568 goto ctr_des_err;
569 ret = crypto_register_alg(&ctr_des3_alg);
570 if (ret)
571 goto ctr_des3_err;
572 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
573 if (!ctrblk) {
574 ret = -ENOMEM;
575 goto ctr_mem_err;
576 }
577 }
a9e62fad
HX
578out:
579 return ret;
580
0200f3ec
GS
581ctr_mem_err:
582 crypto_unregister_alg(&ctr_des3_alg);
583ctr_des3_err:
584 crypto_unregister_alg(&ctr_des_alg);
585ctr_des_err:
586 crypto_unregister_alg(&cbc_des3_alg);
98971f84
JG
587cbc_des3_err:
588 crypto_unregister_alg(&ecb_des3_alg);
589ecb_des3_err:
590 crypto_unregister_alg(&des3_alg);
591des3_err:
a9e62fad
HX
592 crypto_unregister_alg(&cbc_des_alg);
593cbc_des_err:
594 crypto_unregister_alg(&ecb_des_alg);
595ecb_des_err:
596 crypto_unregister_alg(&des_alg);
597des_err:
598 goto out;
1da177e4
LT
599}
600
1efbd15c 601static void __exit des_s390_exit(void)
1da177e4 602{
0200f3ec
GS
603 if (ctrblk) {
604 crypto_unregister_alg(&ctr_des_alg);
605 crypto_unregister_alg(&ctr_des3_alg);
606 free_page((unsigned long) ctrblk);
607 }
98971f84
JG
608 crypto_unregister_alg(&cbc_des3_alg);
609 crypto_unregister_alg(&ecb_des3_alg);
610 crypto_unregister_alg(&des3_alg);
a9e62fad
HX
611 crypto_unregister_alg(&cbc_des_alg);
612 crypto_unregister_alg(&ecb_des_alg);
1da177e4
LT
613 crypto_unregister_alg(&des_alg);
614}
615
d05377c1 616module_cpu_feature_match(MSA, des_s390_init);
1efbd15c 617module_exit(des_s390_exit);
1da177e4 618
5d26a105
KC
619MODULE_ALIAS_CRYPTO("des");
620MODULE_ALIAS_CRYPTO("des3_ede");
1da177e4
LT
621
622MODULE_LICENSE("GPL");
623MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");
This page took 0.96679 seconds and 5 git commands to generate.