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