]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/s390/crypto/des_s390.c
Merge tag 'mmc-v4.15-2' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc
[mirror_ubuntu-bionic-kernel.git] / arch / s390 / crypto / des_s390.c
CommitLineData
20a884f5 1// SPDX-License-Identifier: GPL-2.0+
1da177e4
LT
2/*
3 * Cryptographic API.
4 *
c1e26e1e 5 * s390 implementation of the DES Cipher Algorithm.
1da177e4 6 *
a53c8fab 7 * Copyright IBM Corp. 2003, 2011
86aa9fc2
JG
8 * Author(s): Thomas Spatzier
9 * Jan Glauber (jan.glauber@de.ibm.com)
1da177e4 10 */
a9e62fad 11
1da177e4
LT
12#include <linux/init.h>
13#include <linux/module.h>
d05377c1 14#include <linux/cpufeature.h>
1efbd15c 15#include <linux/crypto.h>
f3d3584f 16#include <linux/fips.h>
1efbd15c
JG
17#include <crypto/algapi.h>
18#include <crypto/des.h>
c7d4d259 19#include <asm/cpacf.h>
1da177e4 20
98971f84 21#define DES3_KEY_SIZE (3 * DES_KEY_SIZE)
1da177e4 22
0200f3ec 23static u8 *ctrblk;
ee97dc7d 24static DEFINE_SPINLOCK(ctrblk_lock);
0200f3ec 25
69c0e360
MS
26static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
27
98971f84 28struct s390_des_ctx {
1da177e4 29 u8 iv[DES_BLOCK_SIZE];
98971f84 30 u8 key[DES3_KEY_SIZE];
1da177e4
LT
31};
32
6c2bb98b 33static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
98971f84 34 unsigned int key_len)
1da177e4 35{
98971f84 36 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
1efbd15c 37 u32 tmp[DES_EXPKEY_WORDS];
1da177e4 38
1efbd15c 39 /* check for weak keys */
69c0e360
MS
40 if (!des_ekey(tmp, key) &&
41 (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
42 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
1efbd15c
JG
43 return -EINVAL;
44 }
45
98971f84 46 memcpy(ctx->key, key, key_len);
1efbd15c 47 return 0;
1da177e4
LT
48}
49
6c2bb98b 50static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1da177e4 51{
98971f84 52 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4 53
edc63a37 54 cpacf_km(CPACF_KM_DEA, ctx->key, out, in, DES_BLOCK_SIZE);
1da177e4
LT
55}
56
6c2bb98b 57static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1da177e4 58{
98971f84 59 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4 60
edc63a37
MS
61 cpacf_km(CPACF_KM_DEA | CPACF_DECRYPT,
62 ctx->key, out, in, DES_BLOCK_SIZE);
b8dc6038
JG
63}
64
1da177e4
LT
65static struct crypto_alg des_alg = {
66 .cra_name = "des",
65b75c36 67 .cra_driver_name = "des-s390",
c7d4d259 68 .cra_priority = 300,
1da177e4
LT
69 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
70 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 71 .cra_ctxsize = sizeof(struct s390_des_ctx),
1da177e4 72 .cra_module = THIS_MODULE,
c1357833
JG
73 .cra_u = {
74 .cipher = {
75 .cia_min_keysize = DES_KEY_SIZE,
76 .cia_max_keysize = DES_KEY_SIZE,
77 .cia_setkey = des_setkey,
78 .cia_encrypt = des_encrypt,
b8dc6038 79 .cia_decrypt = des_decrypt,
c1357833
JG
80 }
81 }
1da177e4
LT
82};
83
7bac4f5b
MS
84static int ecb_desall_crypt(struct blkcipher_desc *desc, unsigned long fc,
85 struct blkcipher_walk *walk)
a9e62fad 86{
7bac4f5b
MS
87 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
88 unsigned int nbytes, n;
89 int ret;
a9e62fad 90
7bac4f5b
MS
91 ret = blkcipher_walk_virt(desc, walk);
92 while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
a9e62fad 93 /* only use complete blocks */
7bac4f5b
MS
94 n = nbytes & ~(DES_BLOCK_SIZE - 1);
95 cpacf_km(fc, ctx->key, walk->dst.virt.addr,
96 walk->src.virt.addr, n);
97 ret = blkcipher_walk_done(desc, walk, nbytes - n);
a9e62fad 98 }
a9e62fad
HX
99 return ret;
100}
101
7bac4f5b 102static int cbc_desall_crypt(struct blkcipher_desc *desc, unsigned long fc,
adc3fcf1 103 struct blkcipher_walk *walk)
a9e62fad 104{
adc3fcf1 105 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
7bac4f5b
MS
106 unsigned int nbytes, n;
107 int ret;
adc3fcf1
HF
108 struct {
109 u8 iv[DES_BLOCK_SIZE];
110 u8 key[DES3_KEY_SIZE];
111 } param;
a9e62fad 112
7bac4f5b 113 ret = blkcipher_walk_virt(desc, walk);
adc3fcf1
HF
114 memcpy(param.iv, walk->iv, DES_BLOCK_SIZE);
115 memcpy(param.key, ctx->key, DES3_KEY_SIZE);
7bac4f5b 116 while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
a9e62fad 117 /* only use complete blocks */
7bac4f5b
MS
118 n = nbytes & ~(DES_BLOCK_SIZE - 1);
119 cpacf_kmc(fc, &param, walk->dst.virt.addr,
120 walk->src.virt.addr, n);
121 ret = blkcipher_walk_done(desc, walk, nbytes - n);
122 }
adc3fcf1 123 memcpy(walk->iv, param.iv, DES_BLOCK_SIZE);
a9e62fad
HX
124 return ret;
125}
126
127static int ecb_des_encrypt(struct blkcipher_desc *desc,
128 struct scatterlist *dst, struct scatterlist *src,
129 unsigned int nbytes)
130{
a9e62fad
HX
131 struct blkcipher_walk walk;
132
133 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 134 return ecb_desall_crypt(desc, CPACF_KM_DEA, &walk);
a9e62fad
HX
135}
136
137static int ecb_des_decrypt(struct blkcipher_desc *desc,
138 struct scatterlist *dst, struct scatterlist *src,
139 unsigned int nbytes)
140{
a9e62fad
HX
141 struct blkcipher_walk walk;
142
143 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 144 return ecb_desall_crypt(desc, CPACF_KM_DEA | CPACF_DECRYPT, &walk);
a9e62fad
HX
145}
146
147static struct crypto_alg ecb_des_alg = {
148 .cra_name = "ecb(des)",
149 .cra_driver_name = "ecb-des-s390",
c7d4d259 150 .cra_priority = 400, /* combo: des + ecb */
a9e62fad
HX
151 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
152 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 153 .cra_ctxsize = sizeof(struct s390_des_ctx),
a9e62fad
HX
154 .cra_type = &crypto_blkcipher_type,
155 .cra_module = THIS_MODULE,
a9e62fad
HX
156 .cra_u = {
157 .blkcipher = {
158 .min_keysize = DES_KEY_SIZE,
159 .max_keysize = DES_KEY_SIZE,
160 .setkey = des_setkey,
161 .encrypt = ecb_des_encrypt,
162 .decrypt = ecb_des_decrypt,
163 }
164 }
165};
166
167static int cbc_des_encrypt(struct blkcipher_desc *desc,
168 struct scatterlist *dst, struct scatterlist *src,
169 unsigned int nbytes)
170{
a9e62fad
HX
171 struct blkcipher_walk walk;
172
173 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37 174 return cbc_desall_crypt(desc, CPACF_KMC_DEA, &walk);
a9e62fad
HX
175}
176
177static int cbc_des_decrypt(struct blkcipher_desc *desc,
178 struct scatterlist *dst, struct scatterlist *src,
179 unsigned int nbytes)
180{
a9e62fad
HX
181 struct blkcipher_walk walk;
182
183 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37 184 return cbc_desall_crypt(desc, CPACF_KMC_DEA | CPACF_DECRYPT, &walk);
a9e62fad
HX
185}
186
187static struct crypto_alg cbc_des_alg = {
188 .cra_name = "cbc(des)",
189 .cra_driver_name = "cbc-des-s390",
c7d4d259 190 .cra_priority = 400, /* combo: des + cbc */
a9e62fad
HX
191 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
192 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 193 .cra_ctxsize = sizeof(struct s390_des_ctx),
a9e62fad
HX
194 .cra_type = &crypto_blkcipher_type,
195 .cra_module = THIS_MODULE,
a9e62fad
HX
196 .cra_u = {
197 .blkcipher = {
198 .min_keysize = DES_KEY_SIZE,
199 .max_keysize = DES_KEY_SIZE,
200 .ivsize = DES_BLOCK_SIZE,
201 .setkey = des_setkey,
202 .encrypt = cbc_des_encrypt,
203 .decrypt = cbc_des_decrypt,
204 }
205 }
206};
207
1da177e4
LT
208/*
209 * RFC2451:
210 *
211 * For DES-EDE3, there is no known need to reject weak or
212 * complementation keys. Any weakness is obviated by the use of
213 * multiple keys.
214 *
215 * However, if the first two or last two independent 64-bit keys are
216 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
217 * same as DES. Implementers MUST reject keys that exhibit this
218 * property.
219 *
f3d3584f
MR
220 * In fips mode additinally check for all 3 keys are unique.
221 *
1da177e4 222 */
98971f84
JG
223static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
224 unsigned int key_len)
1da177e4 225{
98971f84 226 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4 227
fed28611
DB
228 if (!(crypto_memneq(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
229 crypto_memneq(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
230 DES_KEY_SIZE)) &&
69c0e360
MS
231 (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
232 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
1da177e4
LT
233 return -EINVAL;
234 }
f3d3584f
MR
235
236 /* in fips mode, ensure k1 != k2 and k2 != k3 and k1 != k3 */
237 if (fips_enabled &&
238 !(crypto_memneq(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
239 crypto_memneq(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
240 DES_KEY_SIZE) &&
241 crypto_memneq(key, &key[DES_KEY_SIZE * 2], DES_KEY_SIZE))) {
242 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
243 return -EINVAL;
244 }
245
98971f84 246 memcpy(ctx->key, key, key_len);
1da177e4
LT
247 return 0;
248}
249
98971f84 250static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
1da177e4 251{
98971f84 252 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4 253
edc63a37 254 cpacf_km(CPACF_KM_TDEA_192, ctx->key, dst, src, DES_BLOCK_SIZE);
1da177e4
LT
255}
256
98971f84 257static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
1da177e4 258{
98971f84 259 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4 260
edc63a37
MS
261 cpacf_km(CPACF_KM_TDEA_192 | CPACF_DECRYPT,
262 ctx->key, dst, src, DES_BLOCK_SIZE);
1da177e4
LT
263}
264
98971f84 265static struct crypto_alg des3_alg = {
1da177e4 266 .cra_name = "des3_ede",
65b75c36 267 .cra_driver_name = "des3_ede-s390",
c7d4d259 268 .cra_priority = 300,
1da177e4 269 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
1efbd15c 270 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 271 .cra_ctxsize = sizeof(struct s390_des_ctx),
1da177e4 272 .cra_module = THIS_MODULE,
c1357833
JG
273 .cra_u = {
274 .cipher = {
98971f84
JG
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,
c1357833
JG
280 }
281 }
1da177e4
LT
282};
283
98971f84
JG
284static int ecb_des3_encrypt(struct blkcipher_desc *desc,
285 struct scatterlist *dst, struct scatterlist *src,
286 unsigned int nbytes)
a9e62fad 287{
a9e62fad
HX
288 struct blkcipher_walk walk;
289
290 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 291 return ecb_desall_crypt(desc, CPACF_KM_TDEA_192, &walk);
a9e62fad
HX
292}
293
98971f84
JG
294static int ecb_des3_decrypt(struct blkcipher_desc *desc,
295 struct scatterlist *dst, struct scatterlist *src,
296 unsigned int nbytes)
a9e62fad 297{
a9e62fad
HX
298 struct blkcipher_walk walk;
299
300 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37 301 return ecb_desall_crypt(desc, CPACF_KM_TDEA_192 | CPACF_DECRYPT,
7bac4f5b 302 &walk);
a9e62fad
HX
303}
304
98971f84 305static struct crypto_alg ecb_des3_alg = {
a9e62fad
HX
306 .cra_name = "ecb(des3_ede)",
307 .cra_driver_name = "ecb-des3_ede-s390",
c7d4d259 308 .cra_priority = 400, /* combo: des3 + ecb */
a9e62fad 309 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1efbd15c 310 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 311 .cra_ctxsize = sizeof(struct s390_des_ctx),
a9e62fad
HX
312 .cra_type = &crypto_blkcipher_type,
313 .cra_module = THIS_MODULE,
a9e62fad
HX
314 .cra_u = {
315 .blkcipher = {
98971f84
JG
316 .min_keysize = DES3_KEY_SIZE,
317 .max_keysize = DES3_KEY_SIZE,
318 .setkey = des3_setkey,
319 .encrypt = ecb_des3_encrypt,
320 .decrypt = ecb_des3_decrypt,
a9e62fad
HX
321 }
322 }
323};
324
98971f84
JG
325static int cbc_des3_encrypt(struct blkcipher_desc *desc,
326 struct scatterlist *dst, struct scatterlist *src,
327 unsigned int nbytes)
a9e62fad 328{
a9e62fad
HX
329 struct blkcipher_walk walk;
330
331 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37 332 return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192, &walk);
a9e62fad
HX
333}
334
98971f84
JG
335static int cbc_des3_decrypt(struct blkcipher_desc *desc,
336 struct scatterlist *dst, struct scatterlist *src,
337 unsigned int nbytes)
a9e62fad 338{
a9e62fad
HX
339 struct blkcipher_walk walk;
340
341 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37
MS
342 return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192 | CPACF_DECRYPT,
343 &walk);
a9e62fad
HX
344}
345
98971f84 346static struct crypto_alg cbc_des3_alg = {
a9e62fad
HX
347 .cra_name = "cbc(des3_ede)",
348 .cra_driver_name = "cbc-des3_ede-s390",
c7d4d259 349 .cra_priority = 400, /* combo: des3 + cbc */
a9e62fad 350 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1efbd15c 351 .cra_blocksize = DES_BLOCK_SIZE,
98971f84 352 .cra_ctxsize = sizeof(struct s390_des_ctx),
a9e62fad
HX
353 .cra_type = &crypto_blkcipher_type,
354 .cra_module = THIS_MODULE,
a9e62fad
HX
355 .cra_u = {
356 .blkcipher = {
98971f84
JG
357 .min_keysize = DES3_KEY_SIZE,
358 .max_keysize = DES3_KEY_SIZE,
1efbd15c 359 .ivsize = DES_BLOCK_SIZE,
98971f84
JG
360 .setkey = des3_setkey,
361 .encrypt = cbc_des3_encrypt,
362 .decrypt = cbc_des3_decrypt,
a9e62fad
HX
363 }
364 }
365};
366
7bac4f5b 367static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
ee97dc7d
HF
368{
369 unsigned int i, n;
370
371 /* align to block size, max. PAGE_SIZE */
372 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(DES_BLOCK_SIZE - 1);
7bac4f5b
MS
373 memcpy(ctrptr, iv, DES_BLOCK_SIZE);
374 for (i = (n / DES_BLOCK_SIZE) - 1; i > 0; i--) {
375 memcpy(ctrptr + DES_BLOCK_SIZE, ctrptr, DES_BLOCK_SIZE);
376 crypto_inc(ctrptr + DES_BLOCK_SIZE, DES_BLOCK_SIZE);
377 ctrptr += DES_BLOCK_SIZE;
ee97dc7d
HF
378 }
379 return n;
380}
381
7bac4f5b 382static int ctr_desall_crypt(struct blkcipher_desc *desc, unsigned long fc,
ee97dc7d 383 struct blkcipher_walk *walk)
0200f3ec 384{
7bac4f5b
MS
385 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
386 u8 buf[DES_BLOCK_SIZE], *ctrptr;
ee97dc7d 387 unsigned int n, nbytes;
7bac4f5b 388 int ret, locked;
0200f3ec 389
7bac4f5b 390 locked = spin_trylock(&ctrblk_lock);
ee97dc7d 391
7bac4f5b 392 ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
0200f3ec 393 while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
7bac4f5b
MS
394 n = DES_BLOCK_SIZE;
395 if (nbytes >= 2*DES_BLOCK_SIZE && locked)
396 n = __ctrblk_init(ctrblk, walk->iv, nbytes);
397 ctrptr = (n > DES_BLOCK_SIZE) ? ctrblk : walk->iv;
398 cpacf_kmctr(fc, ctx->key, walk->dst.virt.addr,
399 walk->src.virt.addr, n, ctrptr);
400 if (ctrptr == ctrblk)
401 memcpy(walk->iv, ctrptr + n - DES_BLOCK_SIZE,
402 DES_BLOCK_SIZE);
403 crypto_inc(walk->iv, DES_BLOCK_SIZE);
404 ret = blkcipher_walk_done(desc, walk, nbytes - n);
0200f3ec 405 }
7bac4f5b 406 if (locked)
ee97dc7d 407 spin_unlock(&ctrblk_lock);
0200f3ec
GS
408 /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
409 if (nbytes) {
7bac4f5b
MS
410 cpacf_kmctr(fc, ctx->key, buf, walk->src.virt.addr,
411 DES_BLOCK_SIZE, walk->iv);
412 memcpy(walk->dst.virt.addr, buf, nbytes);
413 crypto_inc(walk->iv, DES_BLOCK_SIZE);
0200f3ec
GS
414 ret = blkcipher_walk_done(desc, walk, 0);
415 }
0200f3ec
GS
416 return ret;
417}
418
419static int ctr_des_encrypt(struct blkcipher_desc *desc,
420 struct scatterlist *dst, struct scatterlist *src,
421 unsigned int nbytes)
422{
0200f3ec
GS
423 struct blkcipher_walk walk;
424
425 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 426 return ctr_desall_crypt(desc, CPACF_KMCTR_DEA, &walk);
0200f3ec
GS
427}
428
429static int ctr_des_decrypt(struct blkcipher_desc *desc,
430 struct scatterlist *dst, struct scatterlist *src,
431 unsigned int nbytes)
432{
0200f3ec
GS
433 struct blkcipher_walk walk;
434
435 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 436 return ctr_desall_crypt(desc, CPACF_KMCTR_DEA | CPACF_DECRYPT, &walk);
0200f3ec
GS
437}
438
439static struct crypto_alg ctr_des_alg = {
440 .cra_name = "ctr(des)",
441 .cra_driver_name = "ctr-des-s390",
c7d4d259 442 .cra_priority = 400, /* combo: des + ctr */
0200f3ec
GS
443 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
444 .cra_blocksize = 1,
445 .cra_ctxsize = sizeof(struct s390_des_ctx),
446 .cra_type = &crypto_blkcipher_type,
447 .cra_module = THIS_MODULE,
0200f3ec
GS
448 .cra_u = {
449 .blkcipher = {
450 .min_keysize = DES_KEY_SIZE,
451 .max_keysize = DES_KEY_SIZE,
452 .ivsize = DES_BLOCK_SIZE,
453 .setkey = des_setkey,
454 .encrypt = ctr_des_encrypt,
455 .decrypt = ctr_des_decrypt,
456 }
457 }
458};
459
460static int ctr_des3_encrypt(struct blkcipher_desc *desc,
461 struct scatterlist *dst, struct scatterlist *src,
462 unsigned int nbytes)
463{
0200f3ec
GS
464 struct blkcipher_walk walk;
465
466 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 467 return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192, &walk);
0200f3ec
GS
468}
469
470static int ctr_des3_decrypt(struct blkcipher_desc *desc,
471 struct scatterlist *dst, struct scatterlist *src,
472 unsigned int nbytes)
473{
0200f3ec
GS
474 struct blkcipher_walk walk;
475
476 blkcipher_walk_init(&walk, dst, src, nbytes);
edc63a37 477 return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192 | CPACF_DECRYPT,
7bac4f5b 478 &walk);
0200f3ec
GS
479}
480
481static struct crypto_alg ctr_des3_alg = {
482 .cra_name = "ctr(des3_ede)",
483 .cra_driver_name = "ctr-des3_ede-s390",
c7d4d259 484 .cra_priority = 400, /* combo: des3 + ede */
0200f3ec
GS
485 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
486 .cra_blocksize = 1,
487 .cra_ctxsize = sizeof(struct s390_des_ctx),
488 .cra_type = &crypto_blkcipher_type,
489 .cra_module = THIS_MODULE,
0200f3ec
GS
490 .cra_u = {
491 .blkcipher = {
492 .min_keysize = DES3_KEY_SIZE,
493 .max_keysize = DES3_KEY_SIZE,
494 .ivsize = DES_BLOCK_SIZE,
495 .setkey = des3_setkey,
496 .encrypt = ctr_des3_encrypt,
497 .decrypt = ctr_des3_decrypt,
498 }
499 }
500};
501
d863d594
MS
502static struct crypto_alg *des_s390_algs_ptr[8];
503static int des_s390_algs_num;
504
505static int des_s390_register_alg(struct crypto_alg *alg)
506{
507 int ret;
508
509 ret = crypto_register_alg(alg);
510 if (!ret)
511 des_s390_algs_ptr[des_s390_algs_num++] = alg;
512 return ret;
513}
514
515static void des_s390_exit(void)
516{
517 while (des_s390_algs_num--)
518 crypto_unregister_alg(des_s390_algs_ptr[des_s390_algs_num]);
519 if (ctrblk)
520 free_page((unsigned long) ctrblk);
521}
522
98971f84 523static int __init des_s390_init(void)
1da177e4 524{
80d663a4 525 int ret;
1da177e4 526
69c0e360
MS
527 /* Query available functions for KM, KMC and KMCTR */
528 cpacf_query(CPACF_KM, &km_functions);
529 cpacf_query(CPACF_KMC, &kmc_functions);
530 cpacf_query(CPACF_KMCTR, &kmctr_functions);
531
532 if (cpacf_test_func(&km_functions, CPACF_KM_DEA)) {
533 ret = des_s390_register_alg(&des_alg);
534 if (ret)
535 goto out_err;
536 ret = des_s390_register_alg(&ecb_des_alg);
537 if (ret)
538 goto out_err;
539 }
540 if (cpacf_test_func(&kmc_functions, CPACF_KMC_DEA)) {
541 ret = des_s390_register_alg(&cbc_des_alg);
542 if (ret)
543 goto out_err;
544 }
545 if (cpacf_test_func(&km_functions, CPACF_KM_TDEA_192)) {
546 ret = des_s390_register_alg(&des3_alg);
547 if (ret)
548 goto out_err;
549 ret = des_s390_register_alg(&ecb_des3_alg);
550 if (ret)
551 goto out_err;
552 }
553 if (cpacf_test_func(&kmc_functions, CPACF_KMC_TDEA_192)) {
554 ret = des_s390_register_alg(&cbc_des3_alg);
555 if (ret)
556 goto out_err;
557 }
558
559 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA) ||
560 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) {
0200f3ec
GS
561 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
562 if (!ctrblk) {
563 ret = -ENOMEM;
d863d594 564 goto out_err;
0200f3ec 565 }
69c0e360
MS
566 }
567
568 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA)) {
d863d594
MS
569 ret = des_s390_register_alg(&ctr_des_alg);
570 if (ret)
571 goto out_err;
69c0e360
MS
572 }
573 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) {
d863d594
MS
574 ret = des_s390_register_alg(&ctr_des3_alg);
575 if (ret)
576 goto out_err;
0200f3ec 577 }
1da177e4 578
d863d594
MS
579 return 0;
580out_err:
581 des_s390_exit();
582 return ret;
1da177e4
LT
583}
584
d05377c1 585module_cpu_feature_match(MSA, des_s390_init);
1efbd15c 586module_exit(des_s390_exit);
1da177e4 587
5d26a105
KC
588MODULE_ALIAS_CRYPTO("des");
589MODULE_ALIAS_CRYPTO("des3_ede");
1da177e4
LT
590
591MODULE_LICENSE("GPL");
592MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");