|
25 | 25 | #define DES3_KEY_SIZE (3 * DES_KEY_SIZE) |
26 | 26 |
|
27 | 27 | static u8 *ctrblk; |
| 28 | +static DEFINE_SPINLOCK(ctrblk_lock); |
28 | 29 |
|
29 | 30 | struct s390_des_ctx { |
30 | 31 | u8 iv[DES_BLOCK_SIZE]; |
@@ -368,54 +369,80 @@ static struct crypto_alg cbc_des3_alg = { |
368 | 369 | } |
369 | 370 | }; |
370 | 371 |
|
| 372 | +static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes) |
| 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); |
| 378 | + for (i = DES_BLOCK_SIZE; i < n; i += DES_BLOCK_SIZE) { |
| 379 | + memcpy(ctrptr + i, ctrptr + i - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
| 380 | + crypto_inc(ctrptr + i, DES_BLOCK_SIZE); |
| 381 | + } |
| 382 | + return n; |
| 383 | +} |
| 384 | + |
371 | 385 | static int ctr_desall_crypt(struct blkcipher_desc *desc, long func, |
372 | | - struct s390_des_ctx *ctx, struct blkcipher_walk *walk) |
| 386 | + struct s390_des_ctx *ctx, |
| 387 | + struct blkcipher_walk *walk) |
373 | 388 | { |
374 | 389 | 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; |
| 390 | + unsigned int n, nbytes; |
| 391 | + u8 buf[DES_BLOCK_SIZE], ctrbuf[DES_BLOCK_SIZE]; |
| 392 | + u8 *out, *in, *ctrptr = ctrbuf; |
| 393 | + |
| 394 | + if (!walk->nbytes) |
| 395 | + return ret; |
378 | 396 |
|
379 | | - memcpy(ctrblk, walk->iv, DES_BLOCK_SIZE); |
| 397 | + if (spin_trylock(&ctrblk_lock)) |
| 398 | + ctrptr = ctrblk; |
| 399 | + |
| 400 | + memcpy(ctrptr, walk->iv, DES_BLOCK_SIZE); |
380 | 401 | while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) { |
381 | 402 | out = walk->dst.virt.addr; |
382 | 403 | in = walk->src.virt.addr; |
383 | 404 | 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); |
393 | | - if (ret < 0 || ret != n) |
| 405 | + if (ctrptr == ctrblk) |
| 406 | + n = __ctrblk_init(ctrptr, nbytes); |
| 407 | + else |
| 408 | + n = DES_BLOCK_SIZE; |
| 409 | + ret = crypt_s390_kmctr(func, ctx->key, out, in, |
| 410 | + n, ctrptr); |
| 411 | + if (ret < 0 || ret != n) { |
| 412 | + if (ctrptr == ctrblk) |
| 413 | + spin_unlock(&ctrblk_lock); |
394 | 414 | return -EIO; |
| 415 | + } |
395 | 416 | if (n > DES_BLOCK_SIZE) |
396 | | - memcpy(ctrblk, ctrblk + n - DES_BLOCK_SIZE, |
| 417 | + memcpy(ctrptr, ctrptr + n - DES_BLOCK_SIZE, |
397 | 418 | DES_BLOCK_SIZE); |
398 | | - crypto_inc(ctrblk, DES_BLOCK_SIZE); |
| 419 | + crypto_inc(ctrptr, DES_BLOCK_SIZE); |
399 | 420 | out += n; |
400 | 421 | in += n; |
401 | 422 | nbytes -= n; |
402 | 423 | } |
403 | 424 | ret = blkcipher_walk_done(desc, walk, nbytes); |
404 | 425 | } |
405 | | - |
| 426 | + if (ctrptr == ctrblk) { |
| 427 | + if (nbytes) |
| 428 | + memcpy(ctrbuf, ctrptr, DES_BLOCK_SIZE); |
| 429 | + else |
| 430 | + memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE); |
| 431 | + spin_unlock(&ctrblk_lock); |
| 432 | + } |
406 | 433 | /* final block may be < DES_BLOCK_SIZE, copy only nbytes */ |
407 | 434 | if (nbytes) { |
408 | 435 | out = walk->dst.virt.addr; |
409 | 436 | in = walk->src.virt.addr; |
410 | 437 | ret = crypt_s390_kmctr(func, ctx->key, buf, in, |
411 | | - DES_BLOCK_SIZE, ctrblk); |
| 438 | + DES_BLOCK_SIZE, ctrbuf); |
412 | 439 | if (ret < 0 || ret != DES_BLOCK_SIZE) |
413 | 440 | return -EIO; |
414 | 441 | memcpy(out, buf, nbytes); |
415 | | - crypto_inc(ctrblk, DES_BLOCK_SIZE); |
| 442 | + crypto_inc(ctrbuf, DES_BLOCK_SIZE); |
416 | 443 | ret = blkcipher_walk_done(desc, walk, 0); |
| 444 | + memcpy(walk->iv, ctrbuf, DES_BLOCK_SIZE); |
417 | 445 | } |
418 | | - memcpy(walk->iv, ctrblk, DES_BLOCK_SIZE); |
419 | 446 | return ret; |
420 | 447 | } |
421 | 448 |
|
|
0 commit comments