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