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