]>
Commit | Line | Data |
---|---|---|
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> | |
1efbd15c JG |
19 | #include <linux/crypto.h> |
20 | #include <crypto/algapi.h> | |
21 | #include <crypto/des.h> | |
c1357833 | 22 | |
c1e26e1e | 23 | #include "crypt_s390.h" |
1da177e4 | 24 | |
98971f84 | 25 | #define DES3_KEY_SIZE (3 * DES_KEY_SIZE) |
1da177e4 | 26 | |
0200f3ec GS |
27 | static u8 *ctrblk; |
28 | ||
98971f84 | 29 | struct s390_des_ctx { |
1da177e4 | 30 | u8 iv[DES_BLOCK_SIZE]; |
98971f84 | 31 | u8 key[DES3_KEY_SIZE]; |
1da177e4 LT |
32 | }; |
33 | ||
6c2bb98b | 34 | static int des_setkey(struct crypto_tfm *tfm, const u8 *key, |
98971f84 | 35 | unsigned int key_len) |
1da177e4 | 36 | { |
98971f84 | 37 | struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm); |
560c06ae | 38 | u32 *flags = &tfm->crt_flags; |
1efbd15c | 39 | u32 tmp[DES_EXPKEY_WORDS]; |
1da177e4 | 40 | |
1efbd15c JG |
41 | /* check for weak keys */ |
42 | if (!des_ekey(tmp, key) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) { | |
43 | *flags |= CRYPTO_TFM_RES_WEAK_KEY; | |
44 | return -EINVAL; | |
45 | } | |
46 | ||
98971f84 | 47 | memcpy(ctx->key, key, key_len); |
1efbd15c | 48 | return 0; |
1da177e4 LT |
49 | } |
50 | ||
6c2bb98b | 51 | static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
1da177e4 | 52 | { |
98971f84 | 53 | struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm); |
1da177e4 | 54 | |
98971f84 | 55 | crypt_s390_km(KM_DEA_ENCRYPT, ctx->key, out, in, DES_BLOCK_SIZE); |
1da177e4 LT |
56 | } |
57 | ||
6c2bb98b | 58 | static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
1da177e4 | 59 | { |
98971f84 | 60 | struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm); |
1da177e4 | 61 | |
98971f84 | 62 | crypt_s390_km(KM_DEA_DECRYPT, ctx->key, out, in, DES_BLOCK_SIZE); |
b8dc6038 JG |
63 | } |
64 | ||
1da177e4 LT |
65 | static struct crypto_alg des_alg = { |
66 | .cra_name = "des", | |
65b75c36 HX |
67 | .cra_driver_name = "des-s390", |
68 | .cra_priority = CRYPT_S390_PRIORITY, | |
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 | ||
a9e62fad | 84 | static int ecb_desall_crypt(struct blkcipher_desc *desc, long func, |
98971f84 | 85 | u8 *key, struct blkcipher_walk *walk) |
a9e62fad HX |
86 | { |
87 | int ret = blkcipher_walk_virt(desc, walk); | |
88 | unsigned int nbytes; | |
89 | ||
90 | while ((nbytes = walk->nbytes)) { | |
91 | /* only use complete blocks */ | |
92 | unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1); | |
93 | u8 *out = walk->dst.virt.addr; | |
94 | u8 *in = walk->src.virt.addr; | |
95 | ||
98971f84 | 96 | ret = crypt_s390_km(func, key, out, in, n); |
36eb2caa JG |
97 | if (ret < 0 || ret != n) |
98 | return -EIO; | |
a9e62fad HX |
99 | |
100 | nbytes &= DES_BLOCK_SIZE - 1; | |
101 | ret = blkcipher_walk_done(desc, walk, nbytes); | |
102 | } | |
103 | ||
104 | return ret; | |
105 | } | |
106 | ||
107 | static int cbc_desall_crypt(struct blkcipher_desc *desc, long func, | |
98971f84 | 108 | u8 *iv, struct blkcipher_walk *walk) |
a9e62fad HX |
109 | { |
110 | int ret = blkcipher_walk_virt(desc, walk); | |
111 | unsigned int nbytes = walk->nbytes; | |
112 | ||
113 | if (!nbytes) | |
114 | goto out; | |
115 | ||
98971f84 | 116 | memcpy(iv, walk->iv, DES_BLOCK_SIZE); |
a9e62fad HX |
117 | do { |
118 | /* only use complete blocks */ | |
119 | unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1); | |
120 | u8 *out = walk->dst.virt.addr; | |
121 | u8 *in = walk->src.virt.addr; | |
122 | ||
98971f84 | 123 | ret = crypt_s390_kmc(func, iv, out, in, n); |
36eb2caa JG |
124 | if (ret < 0 || ret != n) |
125 | return -EIO; | |
a9e62fad HX |
126 | |
127 | nbytes &= DES_BLOCK_SIZE - 1; | |
128 | ret = blkcipher_walk_done(desc, walk, nbytes); | |
129 | } while ((nbytes = walk->nbytes)); | |
98971f84 | 130 | memcpy(walk->iv, iv, DES_BLOCK_SIZE); |
a9e62fad HX |
131 | |
132 | out: | |
133 | return ret; | |
134 | } | |
135 | ||
136 | static int ecb_des_encrypt(struct blkcipher_desc *desc, | |
137 | struct scatterlist *dst, struct scatterlist *src, | |
138 | unsigned int nbytes) | |
139 | { | |
98971f84 | 140 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
a9e62fad HX |
141 | struct blkcipher_walk walk; |
142 | ||
143 | blkcipher_walk_init(&walk, dst, src, nbytes); | |
98971f84 | 144 | return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, ctx->key, &walk); |
a9e62fad HX |
145 | } |
146 | ||
147 | static int ecb_des_decrypt(struct blkcipher_desc *desc, | |
148 | struct scatterlist *dst, struct scatterlist *src, | |
149 | unsigned int nbytes) | |
150 | { | |
98971f84 | 151 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
a9e62fad HX |
152 | struct blkcipher_walk walk; |
153 | ||
154 | blkcipher_walk_init(&walk, dst, src, nbytes); | |
98971f84 | 155 | return ecb_desall_crypt(desc, KM_DEA_DECRYPT, ctx->key, &walk); |
a9e62fad HX |
156 | } |
157 | ||
158 | static struct crypto_alg ecb_des_alg = { | |
159 | .cra_name = "ecb(des)", | |
160 | .cra_driver_name = "ecb-des-s390", | |
161 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, | |
162 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, | |
163 | .cra_blocksize = DES_BLOCK_SIZE, | |
98971f84 | 164 | .cra_ctxsize = sizeof(struct s390_des_ctx), |
a9e62fad HX |
165 | .cra_type = &crypto_blkcipher_type, |
166 | .cra_module = THIS_MODULE, | |
a9e62fad HX |
167 | .cra_u = { |
168 | .blkcipher = { | |
169 | .min_keysize = DES_KEY_SIZE, | |
170 | .max_keysize = DES_KEY_SIZE, | |
171 | .setkey = des_setkey, | |
172 | .encrypt = ecb_des_encrypt, | |
173 | .decrypt = ecb_des_decrypt, | |
174 | } | |
175 | } | |
176 | }; | |
177 | ||
178 | static int cbc_des_encrypt(struct blkcipher_desc *desc, | |
179 | struct scatterlist *dst, struct scatterlist *src, | |
180 | unsigned int nbytes) | |
181 | { | |
98971f84 | 182 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
a9e62fad HX |
183 | struct blkcipher_walk walk; |
184 | ||
185 | blkcipher_walk_init(&walk, dst, src, nbytes); | |
98971f84 | 186 | return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, ctx->iv, &walk); |
a9e62fad HX |
187 | } |
188 | ||
189 | static int cbc_des_decrypt(struct blkcipher_desc *desc, | |
190 | struct scatterlist *dst, struct scatterlist *src, | |
191 | unsigned int nbytes) | |
192 | { | |
98971f84 | 193 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
a9e62fad HX |
194 | struct blkcipher_walk walk; |
195 | ||
196 | blkcipher_walk_init(&walk, dst, src, nbytes); | |
98971f84 | 197 | return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, ctx->iv, &walk); |
a9e62fad HX |
198 | } |
199 | ||
200 | static struct crypto_alg cbc_des_alg = { | |
201 | .cra_name = "cbc(des)", | |
202 | .cra_driver_name = "cbc-des-s390", | |
203 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, | |
204 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, | |
205 | .cra_blocksize = DES_BLOCK_SIZE, | |
98971f84 | 206 | .cra_ctxsize = sizeof(struct s390_des_ctx), |
a9e62fad HX |
207 | .cra_type = &crypto_blkcipher_type, |
208 | .cra_module = THIS_MODULE, | |
a9e62fad HX |
209 | .cra_u = { |
210 | .blkcipher = { | |
211 | .min_keysize = DES_KEY_SIZE, | |
212 | .max_keysize = DES_KEY_SIZE, | |
213 | .ivsize = DES_BLOCK_SIZE, | |
214 | .setkey = des_setkey, | |
215 | .encrypt = cbc_des_encrypt, | |
216 | .decrypt = cbc_des_decrypt, | |
217 | } | |
218 | } | |
219 | }; | |
220 | ||
1da177e4 LT |
221 | /* |
222 | * RFC2451: | |
223 | * | |
224 | * For DES-EDE3, there is no known need to reject weak or | |
225 | * complementation keys. Any weakness is obviated by the use of | |
226 | * multiple keys. | |
227 | * | |
228 | * However, if the first two or last two independent 64-bit keys are | |
229 | * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the | |
230 | * same as DES. Implementers MUST reject keys that exhibit this | |
231 | * property. | |
232 | * | |
233 | */ | |
98971f84 JG |
234 | static int des3_setkey(struct crypto_tfm *tfm, const u8 *key, |
235 | unsigned int key_len) | |
1da177e4 | 236 | { |
98971f84 | 237 | struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm); |
560c06ae | 238 | u32 *flags = &tfm->crt_flags; |
1da177e4 | 239 | |
fed28611 DB |
240 | if (!(crypto_memneq(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) && |
241 | crypto_memneq(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2], | |
242 | DES_KEY_SIZE)) && | |
03b56ce5 JW |
243 | (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) { |
244 | *flags |= CRYPTO_TFM_RES_WEAK_KEY; | |
1da177e4 LT |
245 | return -EINVAL; |
246 | } | |
98971f84 | 247 | memcpy(ctx->key, key, key_len); |
1da177e4 LT |
248 | return 0; |
249 | } | |
250 | ||
98971f84 | 251 | static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
1da177e4 | 252 | { |
98971f84 | 253 | struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm); |
1da177e4 | 254 | |
98971f84 | 255 | crypt_s390_km(KM_TDEA_192_ENCRYPT, ctx->key, dst, src, DES_BLOCK_SIZE); |
1da177e4 LT |
256 | } |
257 | ||
98971f84 | 258 | static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
1da177e4 | 259 | { |
98971f84 | 260 | struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm); |
1da177e4 | 261 | |
98971f84 | 262 | crypt_s390_km(KM_TDEA_192_DECRYPT, ctx->key, dst, src, DES_BLOCK_SIZE); |
1da177e4 LT |
263 | } |
264 | ||
98971f84 | 265 | static struct crypto_alg des3_alg = { |
1da177e4 | 266 | .cra_name = "des3_ede", |
65b75c36 HX |
267 | .cra_driver_name = "des3_ede-s390", |
268 | .cra_priority = CRYPT_S390_PRIORITY, | |
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 |
284 | static int ecb_des3_encrypt(struct blkcipher_desc *desc, |
285 | struct scatterlist *dst, struct scatterlist *src, | |
286 | unsigned int nbytes) | |
a9e62fad | 287 | { |
98971f84 | 288 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
a9e62fad HX |
289 | struct blkcipher_walk walk; |
290 | ||
291 | blkcipher_walk_init(&walk, dst, src, nbytes); | |
98971f84 | 292 | return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, ctx->key, &walk); |
a9e62fad HX |
293 | } |
294 | ||
98971f84 JG |
295 | static int ecb_des3_decrypt(struct blkcipher_desc *desc, |
296 | struct scatterlist *dst, struct scatterlist *src, | |
297 | unsigned int nbytes) | |
a9e62fad | 298 | { |
98971f84 | 299 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
a9e62fad HX |
300 | struct blkcipher_walk walk; |
301 | ||
302 | blkcipher_walk_init(&walk, dst, src, nbytes); | |
98971f84 | 303 | return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, ctx->key, &walk); |
a9e62fad HX |
304 | } |
305 | ||
98971f84 | 306 | static struct crypto_alg ecb_des3_alg = { |
a9e62fad HX |
307 | .cra_name = "ecb(des3_ede)", |
308 | .cra_driver_name = "ecb-des3_ede-s390", | |
309 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, | |
310 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, | |
1efbd15c | 311 | .cra_blocksize = DES_BLOCK_SIZE, |
98971f84 | 312 | .cra_ctxsize = sizeof(struct s390_des_ctx), |
a9e62fad HX |
313 | .cra_type = &crypto_blkcipher_type, |
314 | .cra_module = THIS_MODULE, | |
a9e62fad HX |
315 | .cra_u = { |
316 | .blkcipher = { | |
98971f84 JG |
317 | .min_keysize = DES3_KEY_SIZE, |
318 | .max_keysize = DES3_KEY_SIZE, | |
319 | .setkey = des3_setkey, | |
320 | .encrypt = ecb_des3_encrypt, | |
321 | .decrypt = ecb_des3_decrypt, | |
a9e62fad HX |
322 | } |
323 | } | |
324 | }; | |
325 | ||
98971f84 JG |
326 | static int cbc_des3_encrypt(struct blkcipher_desc *desc, |
327 | struct scatterlist *dst, struct scatterlist *src, | |
328 | unsigned int nbytes) | |
a9e62fad | 329 | { |
98971f84 | 330 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
a9e62fad HX |
331 | struct blkcipher_walk walk; |
332 | ||
333 | blkcipher_walk_init(&walk, dst, src, nbytes); | |
98971f84 | 334 | return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, ctx->iv, &walk); |
a9e62fad HX |
335 | } |
336 | ||
98971f84 JG |
337 | static int cbc_des3_decrypt(struct blkcipher_desc *desc, |
338 | struct scatterlist *dst, struct scatterlist *src, | |
339 | unsigned int nbytes) | |
a9e62fad | 340 | { |
98971f84 | 341 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
a9e62fad HX |
342 | struct blkcipher_walk walk; |
343 | ||
344 | blkcipher_walk_init(&walk, dst, src, nbytes); | |
98971f84 | 345 | return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, ctx->iv, &walk); |
a9e62fad HX |
346 | } |
347 | ||
98971f84 | 348 | static struct crypto_alg cbc_des3_alg = { |
a9e62fad HX |
349 | .cra_name = "cbc(des3_ede)", |
350 | .cra_driver_name = "cbc-des3_ede-s390", | |
351 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, | |
352 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, | |
1efbd15c | 353 | .cra_blocksize = DES_BLOCK_SIZE, |
98971f84 | 354 | .cra_ctxsize = sizeof(struct s390_des_ctx), |
a9e62fad HX |
355 | .cra_type = &crypto_blkcipher_type, |
356 | .cra_module = THIS_MODULE, | |
a9e62fad HX |
357 | .cra_u = { |
358 | .blkcipher = { | |
98971f84 JG |
359 | .min_keysize = DES3_KEY_SIZE, |
360 | .max_keysize = DES3_KEY_SIZE, | |
1efbd15c | 361 | .ivsize = DES_BLOCK_SIZE, |
98971f84 JG |
362 | .setkey = des3_setkey, |
363 | .encrypt = cbc_des3_encrypt, | |
364 | .decrypt = cbc_des3_decrypt, | |
a9e62fad HX |
365 | } |
366 | } | |
367 | }; | |
368 | ||
0200f3ec GS |
369 | static int ctr_desall_crypt(struct blkcipher_desc *desc, long func, |
370 | struct s390_des_ctx *ctx, struct blkcipher_walk *walk) | |
371 | { | |
372 | int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE); | |
373 | unsigned int i, n, nbytes; | |
374 | u8 buf[DES_BLOCK_SIZE]; | |
375 | u8 *out, *in; | |
376 | ||
377 | memcpy(ctrblk, walk->iv, DES_BLOCK_SIZE); | |
378 | while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) { | |
379 | out = walk->dst.virt.addr; | |
380 | in = walk->src.virt.addr; | |
381 | while (nbytes >= DES_BLOCK_SIZE) { | |
382 | /* align to block size, max. PAGE_SIZE */ | |
383 | n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : | |
384 | nbytes & ~(DES_BLOCK_SIZE - 1); | |
385 | for (i = DES_BLOCK_SIZE; i < n; i += DES_BLOCK_SIZE) { | |
386 | memcpy(ctrblk + i, ctrblk + i - DES_BLOCK_SIZE, | |
387 | DES_BLOCK_SIZE); | |
388 | crypto_inc(ctrblk + i, DES_BLOCK_SIZE); | |
389 | } | |
390 | ret = crypt_s390_kmctr(func, ctx->key, out, in, n, ctrblk); | |
36eb2caa JG |
391 | if (ret < 0 || ret != n) |
392 | return -EIO; | |
0200f3ec GS |
393 | if (n > DES_BLOCK_SIZE) |
394 | memcpy(ctrblk, ctrblk + n - DES_BLOCK_SIZE, | |
395 | DES_BLOCK_SIZE); | |
396 | crypto_inc(ctrblk, DES_BLOCK_SIZE); | |
397 | out += n; | |
398 | in += n; | |
399 | nbytes -= n; | |
400 | } | |
401 | ret = blkcipher_walk_done(desc, walk, nbytes); | |
402 | } | |
403 | ||
404 | /* final block may be < DES_BLOCK_SIZE, copy only nbytes */ | |
405 | if (nbytes) { | |
406 | out = walk->dst.virt.addr; | |
407 | in = walk->src.virt.addr; | |
408 | ret = crypt_s390_kmctr(func, ctx->key, buf, in, | |
409 | DES_BLOCK_SIZE, ctrblk); | |
36eb2caa JG |
410 | if (ret < 0 || ret != DES_BLOCK_SIZE) |
411 | return -EIO; | |
0200f3ec GS |
412 | memcpy(out, buf, nbytes); |
413 | crypto_inc(ctrblk, DES_BLOCK_SIZE); | |
414 | ret = blkcipher_walk_done(desc, walk, 0); | |
415 | } | |
416 | memcpy(walk->iv, ctrblk, DES_BLOCK_SIZE); | |
417 | return ret; | |
418 | } | |
419 | ||
420 | static int ctr_des_encrypt(struct blkcipher_desc *desc, | |
421 | struct scatterlist *dst, struct scatterlist *src, | |
422 | unsigned int nbytes) | |
423 | { | |
424 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); | |
425 | struct blkcipher_walk walk; | |
426 | ||
427 | blkcipher_walk_init(&walk, dst, src, nbytes); | |
428 | return ctr_desall_crypt(desc, KMCTR_DEA_ENCRYPT, ctx, &walk); | |
429 | } | |
430 | ||
431 | static int ctr_des_decrypt(struct blkcipher_desc *desc, | |
432 | struct scatterlist *dst, struct scatterlist *src, | |
433 | unsigned int nbytes) | |
434 | { | |
435 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); | |
436 | struct blkcipher_walk walk; | |
437 | ||
438 | blkcipher_walk_init(&walk, dst, src, nbytes); | |
439 | return ctr_desall_crypt(desc, KMCTR_DEA_DECRYPT, ctx, &walk); | |
440 | } | |
441 | ||
442 | static struct crypto_alg ctr_des_alg = { | |
443 | .cra_name = "ctr(des)", | |
444 | .cra_driver_name = "ctr-des-s390", | |
445 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, | |
446 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, | |
447 | .cra_blocksize = 1, | |
448 | .cra_ctxsize = sizeof(struct s390_des_ctx), | |
449 | .cra_type = &crypto_blkcipher_type, | |
450 | .cra_module = THIS_MODULE, | |
0200f3ec GS |
451 | .cra_u = { |
452 | .blkcipher = { | |
453 | .min_keysize = DES_KEY_SIZE, | |
454 | .max_keysize = DES_KEY_SIZE, | |
455 | .ivsize = DES_BLOCK_SIZE, | |
456 | .setkey = des_setkey, | |
457 | .encrypt = ctr_des_encrypt, | |
458 | .decrypt = ctr_des_decrypt, | |
459 | } | |
460 | } | |
461 | }; | |
462 | ||
463 | static int ctr_des3_encrypt(struct blkcipher_desc *desc, | |
464 | struct scatterlist *dst, struct scatterlist *src, | |
465 | unsigned int nbytes) | |
466 | { | |
467 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); | |
468 | struct blkcipher_walk walk; | |
469 | ||
470 | blkcipher_walk_init(&walk, dst, src, nbytes); | |
471 | return ctr_desall_crypt(desc, KMCTR_TDEA_192_ENCRYPT, ctx, &walk); | |
472 | } | |
473 | ||
474 | static int ctr_des3_decrypt(struct blkcipher_desc *desc, | |
475 | struct scatterlist *dst, struct scatterlist *src, | |
476 | unsigned int nbytes) | |
477 | { | |
478 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); | |
479 | struct blkcipher_walk walk; | |
480 | ||
481 | blkcipher_walk_init(&walk, dst, src, nbytes); | |
482 | return ctr_desall_crypt(desc, KMCTR_TDEA_192_DECRYPT, ctx, &walk); | |
483 | } | |
484 | ||
485 | static struct crypto_alg ctr_des3_alg = { | |
486 | .cra_name = "ctr(des3_ede)", | |
487 | .cra_driver_name = "ctr-des3_ede-s390", | |
488 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, | |
489 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, | |
490 | .cra_blocksize = 1, | |
491 | .cra_ctxsize = sizeof(struct s390_des_ctx), | |
492 | .cra_type = &crypto_blkcipher_type, | |
493 | .cra_module = THIS_MODULE, | |
0200f3ec GS |
494 | .cra_u = { |
495 | .blkcipher = { | |
496 | .min_keysize = DES3_KEY_SIZE, | |
497 | .max_keysize = DES3_KEY_SIZE, | |
498 | .ivsize = DES_BLOCK_SIZE, | |
499 | .setkey = des3_setkey, | |
500 | .encrypt = ctr_des3_encrypt, | |
501 | .decrypt = ctr_des3_decrypt, | |
502 | } | |
503 | } | |
504 | }; | |
505 | ||
98971f84 | 506 | static int __init des_s390_init(void) |
1da177e4 | 507 | { |
80d663a4 | 508 | int ret; |
1da177e4 | 509 | |
1822bc90 JG |
510 | if (!crypt_s390_func_available(KM_DEA_ENCRYPT, CRYPT_S390_MSA) || |
511 | !crypt_s390_func_available(KM_TDEA_192_ENCRYPT, CRYPT_S390_MSA)) | |
86aa9fc2 | 512 | return -EOPNOTSUPP; |
1da177e4 | 513 | |
a9e62fad HX |
514 | ret = crypto_register_alg(&des_alg); |
515 | if (ret) | |
516 | goto des_err; | |
517 | ret = crypto_register_alg(&ecb_des_alg); | |
518 | if (ret) | |
519 | goto ecb_des_err; | |
520 | ret = crypto_register_alg(&cbc_des_alg); | |
521 | if (ret) | |
522 | goto cbc_des_err; | |
98971f84 | 523 | ret = crypto_register_alg(&des3_alg); |
a9e62fad | 524 | if (ret) |
98971f84 JG |
525 | goto des3_err; |
526 | ret = crypto_register_alg(&ecb_des3_alg); | |
a9e62fad | 527 | if (ret) |
98971f84 JG |
528 | goto ecb_des3_err; |
529 | ret = crypto_register_alg(&cbc_des3_alg); | |
a9e62fad | 530 | if (ret) |
98971f84 | 531 | goto cbc_des3_err; |
0200f3ec GS |
532 | |
533 | if (crypt_s390_func_available(KMCTR_DEA_ENCRYPT, | |
534 | CRYPT_S390_MSA | CRYPT_S390_MSA4) && | |
535 | crypt_s390_func_available(KMCTR_TDEA_192_ENCRYPT, | |
536 | CRYPT_S390_MSA | CRYPT_S390_MSA4)) { | |
537 | ret = crypto_register_alg(&ctr_des_alg); | |
538 | if (ret) | |
539 | goto ctr_des_err; | |
540 | ret = crypto_register_alg(&ctr_des3_alg); | |
541 | if (ret) | |
542 | goto ctr_des3_err; | |
543 | ctrblk = (u8 *) __get_free_page(GFP_KERNEL); | |
544 | if (!ctrblk) { | |
545 | ret = -ENOMEM; | |
546 | goto ctr_mem_err; | |
547 | } | |
548 | } | |
a9e62fad HX |
549 | out: |
550 | return ret; | |
551 | ||
0200f3ec GS |
552 | ctr_mem_err: |
553 | crypto_unregister_alg(&ctr_des3_alg); | |
554 | ctr_des3_err: | |
555 | crypto_unregister_alg(&ctr_des_alg); | |
556 | ctr_des_err: | |
557 | crypto_unregister_alg(&cbc_des3_alg); | |
98971f84 JG |
558 | cbc_des3_err: |
559 | crypto_unregister_alg(&ecb_des3_alg); | |
560 | ecb_des3_err: | |
561 | crypto_unregister_alg(&des3_alg); | |
562 | des3_err: | |
a9e62fad HX |
563 | crypto_unregister_alg(&cbc_des_alg); |
564 | cbc_des_err: | |
565 | crypto_unregister_alg(&ecb_des_alg); | |
566 | ecb_des_err: | |
567 | crypto_unregister_alg(&des_alg); | |
568 | des_err: | |
569 | goto out; | |
1da177e4 LT |
570 | } |
571 | ||
1efbd15c | 572 | static void __exit des_s390_exit(void) |
1da177e4 | 573 | { |
0200f3ec GS |
574 | if (ctrblk) { |
575 | crypto_unregister_alg(&ctr_des_alg); | |
576 | crypto_unregister_alg(&ctr_des3_alg); | |
577 | free_page((unsigned long) ctrblk); | |
578 | } | |
98971f84 JG |
579 | crypto_unregister_alg(&cbc_des3_alg); |
580 | crypto_unregister_alg(&ecb_des3_alg); | |
581 | crypto_unregister_alg(&des3_alg); | |
a9e62fad HX |
582 | crypto_unregister_alg(&cbc_des_alg); |
583 | crypto_unregister_alg(&ecb_des_alg); | |
1da177e4 LT |
584 | crypto_unregister_alg(&des_alg); |
585 | } | |
586 | ||
9f7819c1 | 587 | module_init(des_s390_init); |
1efbd15c | 588 | module_exit(des_s390_exit); |
1da177e4 LT |
589 | |
590 | MODULE_ALIAS("des"); | |
591 | MODULE_ALIAS("des3_ede"); | |
592 | ||
593 | MODULE_LICENSE("GPL"); | |
594 | MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms"); |