s390/crypto: simplify init / exit functions
[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
edc63a37 56 cpacf_km(CPACF_KM_DEA, 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
edc63a37
MS
63 cpacf_km(CPACF_KM_DEA | CPACF_DECRYPT,
64 ctx->key, out, in, DES_BLOCK_SIZE);
b8dc6038
JG
65}
66
1da177e4
LT
67static struct crypto_alg des_alg = {
68 .cra_name = "des",
65b75c36 69 .cra_driver_name = "des-s390",
c7d4d259 70 .cra_priority = 300,
1da177e4
LT
71 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
72 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 73 .cra_ctxsize = sizeof(struct s390_des_ctx),
1da177e4 74 .cra_module = THIS_MODULE,
c1357833
JG
75 .cra_u = {
76 .cipher = {
77 .cia_min_keysize = DES_KEY_SIZE,
78 .cia_max_keysize = DES_KEY_SIZE,
79 .cia_setkey = des_setkey,
80 .cia_encrypt = des_encrypt,
b8dc6038 81 .cia_decrypt = des_decrypt,
c1357833
JG
82 }
83 }
1da177e4
LT
84};
85
a9e62fad 86static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
98971f84 87 u8 *key, struct blkcipher_walk *walk)
a9e62fad
HX
88{
89 int ret = blkcipher_walk_virt(desc, walk);
90 unsigned int nbytes;
91
92 while ((nbytes = walk->nbytes)) {
93 /* only use complete blocks */
94 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
95 u8 *out = walk->dst.virt.addr;
96 u8 *in = walk->src.virt.addr;
97
0177db01 98 cpacf_km(func, key, out, in, n);
a9e62fad
HX
99
100 nbytes &= DES_BLOCK_SIZE - 1;
101 ret = blkcipher_walk_done(desc, walk, nbytes);
102 }
103
104 return ret;
105}
106
107static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
adc3fcf1 108 struct blkcipher_walk *walk)
a9e62fad 109{
adc3fcf1 110 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
a9e62fad
HX
111 int ret = blkcipher_walk_virt(desc, walk);
112 unsigned int nbytes = walk->nbytes;
adc3fcf1
HF
113 struct {
114 u8 iv[DES_BLOCK_SIZE];
115 u8 key[DES3_KEY_SIZE];
116 } param;
a9e62fad
HX
117
118 if (!nbytes)
119 goto out;
120
adc3fcf1
HF
121 memcpy(param.iv, walk->iv, DES_BLOCK_SIZE);
122 memcpy(param.key, ctx->key, DES3_KEY_SIZE);
a9e62fad
HX
123 do {
124 /* only use complete blocks */
125 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
126 u8 *out = walk->dst.virt.addr;
127 u8 *in = walk->src.virt.addr;
128
0177db01 129 cpacf_kmc(func, &param, out, in, n);
a9e62fad
HX
130
131 nbytes &= DES_BLOCK_SIZE - 1;
132 ret = blkcipher_walk_done(desc, walk, nbytes);
133 } while ((nbytes = walk->nbytes));
adc3fcf1 134 memcpy(walk->iv, param.iv, DES_BLOCK_SIZE);
a9e62fad
HX
135
136out:
137 return ret;
138}
139
140static int ecb_des_encrypt(struct blkcipher_desc *desc,
141 struct scatterlist *dst, struct scatterlist *src,
142 unsigned int nbytes)
143{
98971f84 144 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
a9e62fad
HX
145 struct blkcipher_walk walk;
146
147 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37 148 return ecb_desall_crypt(desc, CPACF_KM_DEA, ctx->key, &walk);
a9e62fad
HX
149}
150
151static int ecb_des_decrypt(struct blkcipher_desc *desc,
152 struct scatterlist *dst, struct scatterlist *src,
153 unsigned int nbytes)
154{
98971f84 155 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
a9e62fad
HX
156 struct blkcipher_walk walk;
157
158 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37
MS
159 return ecb_desall_crypt(desc, CPACF_KM_DEA | CPACF_DECRYPT,
160 ctx->key, &walk);
a9e62fad
HX
161}
162
163static struct crypto_alg ecb_des_alg = {
164 .cra_name = "ecb(des)",
165 .cra_driver_name = "ecb-des-s390",
c7d4d259 166 .cra_priority = 400, /* combo: des + ecb */
a9e62fad
HX
167 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
168 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 169 .cra_ctxsize = sizeof(struct s390_des_ctx),
a9e62fad
HX
170 .cra_type = &crypto_blkcipher_type,
171 .cra_module = THIS_MODULE,
a9e62fad
HX
172 .cra_u = {
173 .blkcipher = {
174 .min_keysize = DES_KEY_SIZE,
175 .max_keysize = DES_KEY_SIZE,
176 .setkey = des_setkey,
177 .encrypt = ecb_des_encrypt,
178 .decrypt = ecb_des_decrypt,
179 }
180 }
181};
182
183static int cbc_des_encrypt(struct blkcipher_desc *desc,
184 struct scatterlist *dst, struct scatterlist *src,
185 unsigned int nbytes)
186{
a9e62fad
HX
187 struct blkcipher_walk walk;
188
189 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37 190 return cbc_desall_crypt(desc, CPACF_KMC_DEA, &walk);
a9e62fad
HX
191}
192
193static int cbc_des_decrypt(struct blkcipher_desc *desc,
194 struct scatterlist *dst, struct scatterlist *src,
195 unsigned int nbytes)
196{
a9e62fad
HX
197 struct blkcipher_walk walk;
198
199 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37 200 return cbc_desall_crypt(desc, CPACF_KMC_DEA | CPACF_DECRYPT, &walk);
a9e62fad
HX
201}
202
203static struct crypto_alg cbc_des_alg = {
204 .cra_name = "cbc(des)",
205 .cra_driver_name = "cbc-des-s390",
c7d4d259 206 .cra_priority = 400, /* combo: des + cbc */
a9e62fad
HX
207 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
208 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 209 .cra_ctxsize = sizeof(struct s390_des_ctx),
a9e62fad
HX
210 .cra_type = &crypto_blkcipher_type,
211 .cra_module = THIS_MODULE,
a9e62fad
HX
212 .cra_u = {
213 .blkcipher = {
214 .min_keysize = DES_KEY_SIZE,
215 .max_keysize = DES_KEY_SIZE,
216 .ivsize = DES_BLOCK_SIZE,
217 .setkey = des_setkey,
218 .encrypt = cbc_des_encrypt,
219 .decrypt = cbc_des_decrypt,
220 }
221 }
222};
223
1da177e4
LT
224/*
225 * RFC2451:
226 *
227 * For DES-EDE3, there is no known need to reject weak or
228 * complementation keys. Any weakness is obviated by the use of
229 * multiple keys.
230 *
231 * However, if the first two or last two independent 64-bit keys are
232 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
233 * same as DES. Implementers MUST reject keys that exhibit this
234 * property.
235 *
236 */
98971f84
JG
237static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
238 unsigned int key_len)
1da177e4 239{
98971f84 240 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
560c06ae 241 u32 *flags = &tfm->crt_flags;
1da177e4 242
fed28611
DB
243 if (!(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)) &&
03b56ce5
JW
246 (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
247 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
1da177e4
LT
248 return -EINVAL;
249 }
98971f84 250 memcpy(ctx->key, key, key_len);
1da177e4
LT
251 return 0;
252}
253
98971f84 254static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
1da177e4 255{
98971f84 256 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4 257
edc63a37 258 cpacf_km(CPACF_KM_TDEA_192, ctx->key, dst, src, DES_BLOCK_SIZE);
1da177e4
LT
259}
260
98971f84 261static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
1da177e4 262{
98971f84 263 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4 264
edc63a37
MS
265 cpacf_km(CPACF_KM_TDEA_192 | CPACF_DECRYPT,
266 ctx->key, dst, src, DES_BLOCK_SIZE);
1da177e4
LT
267}
268
98971f84 269static struct crypto_alg des3_alg = {
1da177e4 270 .cra_name = "des3_ede",
65b75c36 271 .cra_driver_name = "des3_ede-s390",
c7d4d259 272 .cra_priority = 300,
1da177e4 273 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
1efbd15c 274 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 275 .cra_ctxsize = sizeof(struct s390_des_ctx),
1da177e4 276 .cra_module = THIS_MODULE,
c1357833
JG
277 .cra_u = {
278 .cipher = {
98971f84
JG
279 .cia_min_keysize = DES3_KEY_SIZE,
280 .cia_max_keysize = DES3_KEY_SIZE,
281 .cia_setkey = des3_setkey,
282 .cia_encrypt = des3_encrypt,
283 .cia_decrypt = des3_decrypt,
c1357833
JG
284 }
285 }
1da177e4
LT
286};
287
98971f84
JG
288static int ecb_des3_encrypt(struct blkcipher_desc *desc,
289 struct scatterlist *dst, struct scatterlist *src,
290 unsigned int nbytes)
a9e62fad 291{
98971f84 292 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
a9e62fad
HX
293 struct blkcipher_walk walk;
294
295 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37 296 return ecb_desall_crypt(desc, CPACF_KM_TDEA_192, ctx->key, &walk);
a9e62fad
HX
297}
298
98971f84
JG
299static int ecb_des3_decrypt(struct blkcipher_desc *desc,
300 struct scatterlist *dst, struct scatterlist *src,
301 unsigned int nbytes)
a9e62fad 302{
98971f84 303 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
a9e62fad
HX
304 struct blkcipher_walk walk;
305
306 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37
MS
307 return ecb_desall_crypt(desc, CPACF_KM_TDEA_192 | CPACF_DECRYPT,
308 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);
edc63a37 338 return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192, &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);
edc63a37
MS
348 return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192 | CPACF_DECRYPT,
349 &walk);
a9e62fad
HX
350}
351
98971f84 352static struct crypto_alg cbc_des3_alg = {
a9e62fad
HX
353 .cra_name = "cbc(des3_ede)",
354 .cra_driver_name = "cbc-des3_ede-s390",
c7d4d259 355 .cra_priority = 400, /* combo: des3 + cbc */
a9e62fad 356 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1efbd15c 357 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 358 .cra_ctxsize = sizeof(struct s390_des_ctx),
a9e62fad
HX
359 .cra_type = &crypto_blkcipher_type,
360 .cra_module = THIS_MODULE,
a9e62fad
HX
361 .cra_u = {
362 .blkcipher = {
98971f84
JG
363 .min_keysize = DES3_KEY_SIZE,
364 .max_keysize = DES3_KEY_SIZE,
1efbd15c 365 .ivsize = DES_BLOCK_SIZE,
98971f84
JG
366 .setkey = des3_setkey,
367 .encrypt = cbc_des3_encrypt,
368 .decrypt = cbc_des3_decrypt,
a9e62fad
HX
369 }
370 }
371};
372
ee97dc7d
HF
373static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes)
374{
375 unsigned int i, n;
376
377 /* align to block size, max. PAGE_SIZE */
378 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(DES_BLOCK_SIZE - 1);
379 for (i = DES_BLOCK_SIZE; i < n; i += DES_BLOCK_SIZE) {
380 memcpy(ctrptr + i, ctrptr + i - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
381 crypto_inc(ctrptr + i, DES_BLOCK_SIZE);
382 }
383 return n;
384}
385
0200f3ec 386static int ctr_desall_crypt(struct blkcipher_desc *desc, long func,
ee97dc7d
HF
387 struct s390_des_ctx *ctx,
388 struct blkcipher_walk *walk)
0200f3ec
GS
389{
390 int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
ee97dc7d
HF
391 unsigned int n, nbytes;
392 u8 buf[DES_BLOCK_SIZE], ctrbuf[DES_BLOCK_SIZE];
393 u8 *out, *in, *ctrptr = ctrbuf;
394
395 if (!walk->nbytes)
396 return ret;
0200f3ec 397
ee97dc7d
HF
398 if (spin_trylock(&ctrblk_lock))
399 ctrptr = ctrblk;
400
401 memcpy(ctrptr, walk->iv, DES_BLOCK_SIZE);
0200f3ec
GS
402 while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
403 out = walk->dst.virt.addr;
404 in = walk->src.virt.addr;
405 while (nbytes >= DES_BLOCK_SIZE) {
ee97dc7d
HF
406 if (ctrptr == ctrblk)
407 n = __ctrblk_init(ctrptr, nbytes);
408 else
409 n = DES_BLOCK_SIZE;
0177db01 410 cpacf_kmctr(func, ctx->key, out, in, n, ctrptr);
0200f3ec 411 if (n > DES_BLOCK_SIZE)
ee97dc7d 412 memcpy(ctrptr, ctrptr + n - DES_BLOCK_SIZE,
0200f3ec 413 DES_BLOCK_SIZE);
ee97dc7d 414 crypto_inc(ctrptr, DES_BLOCK_SIZE);
0200f3ec
GS
415 out += n;
416 in += n;
417 nbytes -= n;
418 }
419 ret = blkcipher_walk_done(desc, walk, nbytes);
420 }
ee97dc7d
HF
421 if (ctrptr == ctrblk) {
422 if (nbytes)
423 memcpy(ctrbuf, ctrptr, DES_BLOCK_SIZE);
424 else
425 memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
426 spin_unlock(&ctrblk_lock);
3901c112
HF
427 } else {
428 if (!nbytes)
429 memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
ee97dc7d 430 }
0200f3ec
GS
431 /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
432 if (nbytes) {
433 out = walk->dst.virt.addr;
434 in = walk->src.virt.addr;
0177db01 435 cpacf_kmctr(func, ctx->key, buf, in, DES_BLOCK_SIZE, ctrbuf);
0200f3ec 436 memcpy(out, buf, nbytes);
ee97dc7d 437 crypto_inc(ctrbuf, DES_BLOCK_SIZE);
0200f3ec 438 ret = blkcipher_walk_done(desc, walk, 0);
ee97dc7d 439 memcpy(walk->iv, ctrbuf, DES_BLOCK_SIZE);
0200f3ec 440 }
0200f3ec
GS
441 return ret;
442}
443
444static int ctr_des_encrypt(struct blkcipher_desc *desc,
445 struct scatterlist *dst, struct scatterlist *src,
446 unsigned int nbytes)
447{
448 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
449 struct blkcipher_walk walk;
450
451 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37 452 return ctr_desall_crypt(desc, CPACF_KMCTR_DEA, ctx, &walk);
0200f3ec
GS
453}
454
455static int ctr_des_decrypt(struct blkcipher_desc *desc,
456 struct scatterlist *dst, struct scatterlist *src,
457 unsigned int nbytes)
458{
459 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
460 struct blkcipher_walk walk;
461
462 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37
MS
463 return ctr_desall_crypt(desc, CPACF_KMCTR_DEA | CPACF_DECRYPT,
464 ctx, &walk);
0200f3ec
GS
465}
466
467static struct crypto_alg ctr_des_alg = {
468 .cra_name = "ctr(des)",
469 .cra_driver_name = "ctr-des-s390",
c7d4d259 470 .cra_priority = 400, /* combo: des + ctr */
0200f3ec
GS
471 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
472 .cra_blocksize = 1,
473 .cra_ctxsize = sizeof(struct s390_des_ctx),
474 .cra_type = &crypto_blkcipher_type,
475 .cra_module = THIS_MODULE,
0200f3ec
GS
476 .cra_u = {
477 .blkcipher = {
478 .min_keysize = DES_KEY_SIZE,
479 .max_keysize = DES_KEY_SIZE,
480 .ivsize = DES_BLOCK_SIZE,
481 .setkey = des_setkey,
482 .encrypt = ctr_des_encrypt,
483 .decrypt = ctr_des_decrypt,
484 }
485 }
486};
487
488static int ctr_des3_encrypt(struct blkcipher_desc *desc,
489 struct scatterlist *dst, struct scatterlist *src,
490 unsigned int nbytes)
491{
492 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
493 struct blkcipher_walk walk;
494
495 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37 496 return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192, ctx, &walk);
0200f3ec
GS
497}
498
499static int ctr_des3_decrypt(struct blkcipher_desc *desc,
500 struct scatterlist *dst, struct scatterlist *src,
501 unsigned int nbytes)
502{
503 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
504 struct blkcipher_walk walk;
505
506 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37
MS
507 return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192 | CPACF_DECRYPT,
508 ctx, &walk);
0200f3ec
GS
509}
510
511static struct crypto_alg ctr_des3_alg = {
512 .cra_name = "ctr(des3_ede)",
513 .cra_driver_name = "ctr-des3_ede-s390",
c7d4d259 514 .cra_priority = 400, /* combo: des3 + ede */
0200f3ec
GS
515 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
516 .cra_blocksize = 1,
517 .cra_ctxsize = sizeof(struct s390_des_ctx),
518 .cra_type = &crypto_blkcipher_type,
519 .cra_module = THIS_MODULE,
0200f3ec
GS
520 .cra_u = {
521 .blkcipher = {
522 .min_keysize = DES3_KEY_SIZE,
523 .max_keysize = DES3_KEY_SIZE,
524 .ivsize = DES_BLOCK_SIZE,
525 .setkey = des3_setkey,
526 .encrypt = ctr_des3_encrypt,
527 .decrypt = ctr_des3_decrypt,
528 }
529 }
530};
531
d863d594
MS
532static struct crypto_alg *des_s390_algs_ptr[8];
533static int des_s390_algs_num;
534
535static int des_s390_register_alg(struct crypto_alg *alg)
536{
537 int ret;
538
539 ret = crypto_register_alg(alg);
540 if (!ret)
541 des_s390_algs_ptr[des_s390_algs_num++] = alg;
542 return ret;
543}
544
545static void des_s390_exit(void)
546{
547 while (des_s390_algs_num--)
548 crypto_unregister_alg(des_s390_algs_ptr[des_s390_algs_num]);
549 if (ctrblk)
550 free_page((unsigned long) ctrblk);
551}
552
98971f84 553static int __init des_s390_init(void)
1da177e4 554{
80d663a4 555 int ret;
1da177e4 556
edc63a37
MS
557 if (!cpacf_query(CPACF_KM, CPACF_KM_DEA) ||
558 !cpacf_query(CPACF_KM, CPACF_KM_TDEA_192))
86aa9fc2 559 return -EOPNOTSUPP;
1da177e4 560
d863d594 561 ret = des_s390_register_alg(&des_alg);
a9e62fad 562 if (ret)
d863d594
MS
563 goto out_err;
564 ret = des_s390_register_alg(&ecb_des_alg);
a9e62fad 565 if (ret)
d863d594
MS
566 goto out_err;
567 ret = des_s390_register_alg(&cbc_des_alg);
a9e62fad 568 if (ret)
d863d594
MS
569 goto out_err;
570 ret = des_s390_register_alg(&des3_alg);
a9e62fad 571 if (ret)
d863d594
MS
572 goto out_err;
573 ret = des_s390_register_alg(&ecb_des3_alg);
a9e62fad 574 if (ret)
d863d594
MS
575 goto out_err;
576 ret = des_s390_register_alg(&cbc_des3_alg);
a9e62fad 577 if (ret)
d863d594 578 goto out_err;
0200f3ec 579
edc63a37
MS
580 if (cpacf_query(CPACF_KMCTR, CPACF_KMCTR_DEA) &&
581 cpacf_query(CPACF_KMCTR, CPACF_KMCTR_TDEA_192)) {
0200f3ec
GS
582 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
583 if (!ctrblk) {
584 ret = -ENOMEM;
d863d594 585 goto out_err;
0200f3ec 586 }
d863d594
MS
587 ret = des_s390_register_alg(&ctr_des_alg);
588 if (ret)
589 goto out_err;
590 ret = des_s390_register_alg(&ctr_des3_alg);
591 if (ret)
592 goto out_err;
0200f3ec 593 }
1da177e4 594
d863d594
MS
595 return 0;
596out_err:
597 des_s390_exit();
598 return ret;
1da177e4
LT
599}
600
d05377c1 601module_cpu_feature_match(MSA, des_s390_init);
1efbd15c 602module_exit(des_s390_exit);
1da177e4 603
5d26a105
KC
604MODULE_ALIAS_CRYPTO("des");
605MODULE_ALIAS_CRYPTO("des3_ede");
1da177e4
LT
606
607MODULE_LICENSE("GPL");
608MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");
This page took 0.81102 seconds and 5 git commands to generate.