s390/crypto: simplify CPACF encryption / decryption functions

The double while loops of the CTR mode encryption / decryption functions
are overly complex for little gain. Simplify the functions to a single
while loop at the cost of an additional memcpy of a few bytes for every
4K page worth of data.
Adapt the other crypto functions to make them all look alike.

Reviewed-by: Harald Freudenberger <freude@linux.vnet.ibm.com>
Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
This commit is contained in:
Martin Schwidefsky 2016-08-15 15:17:52 +02:00
parent 69c0e360f9
commit 7bac4f5b8e
2 changed files with 143 additions and 221 deletions

View File

@ -46,14 +46,6 @@ struct s390_aes_ctx {
} fallback; } fallback;
}; };
struct pcc_param {
u8 key[32];
u8 tweak[16];
u8 block[16];
u8 bit[16];
u8 xts[16];
};
struct s390_xts_ctx { struct s390_xts_ctx {
u8 key[32]; u8 key[32];
u8 pcc_key[32]; u8 pcc_key[32];
@ -248,22 +240,20 @@ static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
return 0; return 0;
} }
static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param, static int ecb_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
struct blkcipher_walk *walk) struct blkcipher_walk *walk)
{ {
int ret = blkcipher_walk_virt(desc, walk); struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
unsigned int nbytes; unsigned int nbytes, n;
int ret;
while ((nbytes = walk->nbytes)) { ret = blkcipher_walk_virt(desc, walk);
while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
/* only use complete blocks */ /* only use complete blocks */
unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1); n = nbytes & ~(AES_BLOCK_SIZE - 1);
u8 *out = walk->dst.virt.addr; cpacf_km(sctx->fc | modifier, sctx->key,
u8 *in = walk->src.virt.addr; walk->dst.virt.addr, walk->src.virt.addr, n);
ret = blkcipher_walk_done(desc, walk, nbytes - n);
cpacf_km(func, param, out, in, n);
nbytes &= AES_BLOCK_SIZE - 1;
ret = blkcipher_walk_done(desc, walk, nbytes);
} }
return ret; return ret;
@ -280,7 +270,7 @@ static int ecb_aes_encrypt(struct blkcipher_desc *desc,
return fallback_blk_enc(desc, dst, src, nbytes); return fallback_blk_enc(desc, dst, src, nbytes);
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return ecb_aes_crypt(desc, sctx->fc, sctx->key, &walk); return ecb_aes_crypt(desc, 0, &walk);
} }
static int ecb_aes_decrypt(struct blkcipher_desc *desc, static int ecb_aes_decrypt(struct blkcipher_desc *desc,
@ -294,7 +284,7 @@ static int ecb_aes_decrypt(struct blkcipher_desc *desc,
return fallback_blk_dec(desc, dst, src, nbytes); return fallback_blk_dec(desc, dst, src, nbytes);
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return ecb_aes_crypt(desc, sctx->fc | CPACF_DECRYPT, sctx->key, &walk); return ecb_aes_crypt(desc, CPACF_DECRYPT, &walk);
} }
static int fallback_init_blk(struct crypto_tfm *tfm) static int fallback_init_blk(struct crypto_tfm *tfm)
@ -366,36 +356,28 @@ static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
return 0; return 0;
} }
static int cbc_aes_crypt(struct blkcipher_desc *desc, long func, static int cbc_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
struct blkcipher_walk *walk) struct blkcipher_walk *walk)
{ {
struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
int ret = blkcipher_walk_virt(desc, walk); unsigned int nbytes, n;
unsigned int nbytes = walk->nbytes; int ret;
struct { struct {
u8 iv[AES_BLOCK_SIZE]; u8 iv[AES_BLOCK_SIZE];
u8 key[AES_MAX_KEY_SIZE]; u8 key[AES_MAX_KEY_SIZE];
} param; } param;
if (!nbytes) ret = blkcipher_walk_virt(desc, walk);
goto out;
memcpy(param.iv, walk->iv, AES_BLOCK_SIZE); memcpy(param.iv, walk->iv, AES_BLOCK_SIZE);
memcpy(param.key, sctx->key, sctx->key_len); memcpy(param.key, sctx->key, sctx->key_len);
do { while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
/* only use complete blocks */ /* only use complete blocks */
unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1); n = nbytes & ~(AES_BLOCK_SIZE - 1);
u8 *out = walk->dst.virt.addr; cpacf_kmc(sctx->fc | modifier, &param,
u8 *in = walk->src.virt.addr; walk->dst.virt.addr, walk->src.virt.addr, n);
ret = blkcipher_walk_done(desc, walk, nbytes - n);
cpacf_kmc(func, &param, out, in, n); }
nbytes &= AES_BLOCK_SIZE - 1;
ret = blkcipher_walk_done(desc, walk, nbytes);
} while ((nbytes = walk->nbytes));
memcpy(walk->iv, param.iv, AES_BLOCK_SIZE); memcpy(walk->iv, param.iv, AES_BLOCK_SIZE);
out:
return ret; return ret;
} }
@ -410,7 +392,7 @@ static int cbc_aes_encrypt(struct blkcipher_desc *desc,
return fallback_blk_enc(desc, dst, src, nbytes); return fallback_blk_enc(desc, dst, src, nbytes);
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return cbc_aes_crypt(desc, sctx->fc, &walk); return cbc_aes_crypt(desc, 0, &walk);
} }
static int cbc_aes_decrypt(struct blkcipher_desc *desc, static int cbc_aes_decrypt(struct blkcipher_desc *desc,
@ -424,7 +406,7 @@ static int cbc_aes_decrypt(struct blkcipher_desc *desc,
return fallback_blk_dec(desc, dst, src, nbytes); return fallback_blk_dec(desc, dst, src, nbytes);
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return cbc_aes_crypt(desc, sctx->fc | CPACF_DECRYPT, &walk); return cbc_aes_crypt(desc, CPACF_DECRYPT, &walk);
} }
static struct crypto_alg cbc_aes_alg = { static struct crypto_alg cbc_aes_alg = {
@ -536,46 +518,43 @@ static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
return 0; return 0;
} }
static int xts_aes_crypt(struct blkcipher_desc *desc, long func, static int xts_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
struct s390_xts_ctx *xts_ctx,
struct blkcipher_walk *walk) struct blkcipher_walk *walk)
{ {
unsigned int offset = xts_ctx->key_len & 0x10; struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
int ret = blkcipher_walk_virt(desc, walk); unsigned int offset, nbytes, n;
unsigned int nbytes = walk->nbytes; int ret;
unsigned int n; struct {
u8 *in, *out; u8 key[32];
struct pcc_param pcc_param; u8 tweak[16];
u8 block[16];
u8 bit[16];
u8 xts[16];
} pcc_param;
struct { struct {
u8 key[32]; u8 key[32];
u8 init[16]; u8 init[16];
} xts_param; } xts_param;
if (!nbytes) ret = blkcipher_walk_virt(desc, walk);
goto out; offset = xts_ctx->key_len & 0x10;
memset(pcc_param.block, 0, sizeof(pcc_param.block)); memset(pcc_param.block, 0, sizeof(pcc_param.block));
memset(pcc_param.bit, 0, sizeof(pcc_param.bit)); memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
memset(pcc_param.xts, 0, sizeof(pcc_param.xts)); memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
memcpy(pcc_param.tweak, walk->iv, sizeof(pcc_param.tweak)); memcpy(pcc_param.tweak, walk->iv, sizeof(pcc_param.tweak));
memcpy(pcc_param.key + offset, xts_ctx->pcc_key, xts_ctx->key_len); memcpy(pcc_param.key + offset, xts_ctx->pcc_key, xts_ctx->key_len);
/* remove decipher modifier bit from 'func' and call PCC */ cpacf_pcc(xts_ctx->fc, pcc_param.key + offset);
cpacf_pcc(func & 0x7f, &pcc_param.key[offset]);
memcpy(xts_param.key + offset, xts_ctx->key, xts_ctx->key_len); memcpy(xts_param.key + offset, xts_ctx->key, xts_ctx->key_len);
memcpy(xts_param.init, pcc_param.xts, 16); memcpy(xts_param.init, pcc_param.xts, 16);
do {
while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
/* only use complete blocks */ /* only use complete blocks */
n = nbytes & ~(AES_BLOCK_SIZE - 1); n = nbytes & ~(AES_BLOCK_SIZE - 1);
out = walk->dst.virt.addr; cpacf_km(xts_ctx->fc | modifier, xts_param.key + offset,
in = walk->src.virt.addr; walk->dst.virt.addr, walk->src.virt.addr, n);
ret = blkcipher_walk_done(desc, walk, nbytes - n);
cpacf_km(func, &xts_param.key[offset], out, in, n); }
nbytes &= AES_BLOCK_SIZE - 1;
ret = blkcipher_walk_done(desc, walk, nbytes);
} while ((nbytes = walk->nbytes));
out:
return ret; return ret;
} }
@ -590,7 +569,7 @@ static int xts_aes_encrypt(struct blkcipher_desc *desc,
return xts_fallback_encrypt(desc, dst, src, nbytes); return xts_fallback_encrypt(desc, dst, src, nbytes);
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return xts_aes_crypt(desc, xts_ctx->fc, xts_ctx, &walk); return xts_aes_crypt(desc, 0, &walk);
} }
static int xts_aes_decrypt(struct blkcipher_desc *desc, static int xts_aes_decrypt(struct blkcipher_desc *desc,
@ -604,7 +583,7 @@ static int xts_aes_decrypt(struct blkcipher_desc *desc,
return xts_fallback_decrypt(desc, dst, src, nbytes); return xts_fallback_decrypt(desc, dst, src, nbytes);
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return xts_aes_crypt(desc, xts_ctx->fc | CPACF_DECRYPT, xts_ctx, &walk); return xts_aes_crypt(desc, CPACF_DECRYPT, &walk);
} }
static int xts_fallback_init(struct crypto_tfm *tfm) static int xts_fallback_init(struct crypto_tfm *tfm)
@ -676,75 +655,58 @@ static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
return 0; return 0;
} }
static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes) static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
{ {
unsigned int i, n; unsigned int i, n;
/* only use complete blocks, max. PAGE_SIZE */ /* only use complete blocks, max. PAGE_SIZE */
memcpy(ctrptr, iv, AES_BLOCK_SIZE);
n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1); n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
for (i = AES_BLOCK_SIZE; i < n; i += AES_BLOCK_SIZE) { for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
memcpy(ctrptr + i, ctrptr + i - AES_BLOCK_SIZE, memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
AES_BLOCK_SIZE); crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
crypto_inc(ctrptr + i, AES_BLOCK_SIZE); ctrptr += AES_BLOCK_SIZE;
} }
return n; return n;
} }
static int ctr_aes_crypt(struct blkcipher_desc *desc, long func, static int ctr_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
struct s390_aes_ctx *sctx, struct blkcipher_walk *walk) struct blkcipher_walk *walk)
{ {
int ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE); struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
u8 buf[AES_BLOCK_SIZE], *ctrptr;
unsigned int n, nbytes; unsigned int n, nbytes;
u8 buf[AES_BLOCK_SIZE], ctrbuf[AES_BLOCK_SIZE]; int ret, locked;
u8 *out, *in, *ctrptr = ctrbuf;
if (!walk->nbytes) locked = spin_trylock(&ctrblk_lock);
return ret;
if (spin_trylock(&ctrblk_lock)) ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
ctrptr = ctrblk;
memcpy(ctrptr, walk->iv, AES_BLOCK_SIZE);
while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) { while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
out = walk->dst.virt.addr; n = AES_BLOCK_SIZE;
in = walk->src.virt.addr; if (nbytes >= 2*AES_BLOCK_SIZE && locked)
while (nbytes >= AES_BLOCK_SIZE) { n = __ctrblk_init(ctrblk, walk->iv, nbytes);
if (ctrptr == ctrblk) ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk->iv;
n = __ctrblk_init(ctrptr, nbytes); cpacf_kmctr(sctx->fc | modifier, sctx->key,
else walk->dst.virt.addr, walk->src.virt.addr,
n = AES_BLOCK_SIZE; n, ctrptr);
cpacf_kmctr(func, sctx->key, out, in, n, ctrptr); if (ctrptr == ctrblk)
if (n > AES_BLOCK_SIZE) memcpy(walk->iv, ctrptr + n - AES_BLOCK_SIZE,
memcpy(ctrptr, ctrptr + n - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
AES_BLOCK_SIZE); crypto_inc(walk->iv, AES_BLOCK_SIZE);
crypto_inc(ctrptr, AES_BLOCK_SIZE); ret = blkcipher_walk_done(desc, walk, nbytes - n);
out += n;
in += n;
nbytes -= n;
}
ret = blkcipher_walk_done(desc, walk, nbytes);
} }
if (ctrptr == ctrblk) { if (locked)
if (nbytes)
memcpy(ctrbuf, ctrptr, AES_BLOCK_SIZE);
else
memcpy(walk->iv, ctrptr, AES_BLOCK_SIZE);
spin_unlock(&ctrblk_lock); spin_unlock(&ctrblk_lock);
} else {
if (!nbytes)
memcpy(walk->iv, ctrptr, AES_BLOCK_SIZE);
}
/* /*
* final block may be < AES_BLOCK_SIZE, copy only nbytes * final block may be < AES_BLOCK_SIZE, copy only nbytes
*/ */
if (nbytes) { if (nbytes) {
out = walk->dst.virt.addr; cpacf_kmctr(sctx->fc | modifier, sctx->key,
in = walk->src.virt.addr; buf, walk->src.virt.addr,
cpacf_kmctr(func, sctx->key, buf, in, AES_BLOCK_SIZE, ctrbuf); AES_BLOCK_SIZE, walk->iv);
memcpy(out, buf, nbytes); memcpy(walk->dst.virt.addr, buf, nbytes);
crypto_inc(ctrbuf, AES_BLOCK_SIZE); crypto_inc(walk->iv, AES_BLOCK_SIZE);
ret = blkcipher_walk_done(desc, walk, 0); ret = blkcipher_walk_done(desc, walk, 0);
memcpy(walk->iv, ctrbuf, AES_BLOCK_SIZE);
} }
return ret; return ret;
@ -761,7 +723,7 @@ static int ctr_aes_encrypt(struct blkcipher_desc *desc,
return fallback_blk_enc(desc, dst, src, nbytes); return fallback_blk_enc(desc, dst, src, nbytes);
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return ctr_aes_crypt(desc, sctx->fc, sctx, &walk); return ctr_aes_crypt(desc, 0, &walk);
} }
static int ctr_aes_decrypt(struct blkcipher_desc *desc, static int ctr_aes_decrypt(struct blkcipher_desc *desc,
@ -775,7 +737,7 @@ static int ctr_aes_decrypt(struct blkcipher_desc *desc,
return fallback_blk_dec(desc, dst, src, nbytes); return fallback_blk_dec(desc, dst, src, nbytes);
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return ctr_aes_crypt(desc, sctx->fc | CPACF_DECRYPT, sctx, &walk); return ctr_aes_crypt(desc, CPACF_DECRYPT, &walk);
} }
static struct crypto_alg ctr_aes_alg = { static struct crypto_alg ctr_aes_alg = {

View File

@ -85,57 +85,46 @@ static struct crypto_alg des_alg = {
} }
}; };
static int ecb_desall_crypt(struct blkcipher_desc *desc, long func, static int ecb_desall_crypt(struct blkcipher_desc *desc, unsigned long fc,
u8 *key, struct blkcipher_walk *walk)
{
int ret = blkcipher_walk_virt(desc, walk);
unsigned int nbytes;
while ((nbytes = walk->nbytes)) {
/* only use complete blocks */
unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
u8 *out = walk->dst.virt.addr;
u8 *in = walk->src.virt.addr;
cpacf_km(func, key, out, in, n);
nbytes &= DES_BLOCK_SIZE - 1;
ret = blkcipher_walk_done(desc, walk, nbytes);
}
return ret;
}
static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
struct blkcipher_walk *walk) struct blkcipher_walk *walk)
{ {
struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
int ret = blkcipher_walk_virt(desc, walk); unsigned int nbytes, n;
unsigned int nbytes = walk->nbytes; int ret;
ret = blkcipher_walk_virt(desc, walk);
while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
/* only use complete blocks */
n = nbytes & ~(DES_BLOCK_SIZE - 1);
cpacf_km(fc, ctx->key, walk->dst.virt.addr,
walk->src.virt.addr, n);
ret = blkcipher_walk_done(desc, walk, nbytes - n);
}
return ret;
}
static int cbc_desall_crypt(struct blkcipher_desc *desc, unsigned long fc,
struct blkcipher_walk *walk)
{
struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
unsigned int nbytes, n;
int ret;
struct { struct {
u8 iv[DES_BLOCK_SIZE]; u8 iv[DES_BLOCK_SIZE];
u8 key[DES3_KEY_SIZE]; u8 key[DES3_KEY_SIZE];
} param; } param;
if (!nbytes) ret = blkcipher_walk_virt(desc, walk);
goto out;
memcpy(param.iv, walk->iv, DES_BLOCK_SIZE); memcpy(param.iv, walk->iv, DES_BLOCK_SIZE);
memcpy(param.key, ctx->key, DES3_KEY_SIZE); memcpy(param.key, ctx->key, DES3_KEY_SIZE);
do { while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
/* only use complete blocks */ /* only use complete blocks */
unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1); n = nbytes & ~(DES_BLOCK_SIZE - 1);
u8 *out = walk->dst.virt.addr; cpacf_kmc(fc, &param, walk->dst.virt.addr,
u8 *in = walk->src.virt.addr; walk->src.virt.addr, n);
ret = blkcipher_walk_done(desc, walk, nbytes - n);
cpacf_kmc(func, &param, out, in, n); }
nbytes &= DES_BLOCK_SIZE - 1;
ret = blkcipher_walk_done(desc, walk, nbytes);
} while ((nbytes = walk->nbytes));
memcpy(walk->iv, param.iv, DES_BLOCK_SIZE); memcpy(walk->iv, param.iv, DES_BLOCK_SIZE);
out:
return ret; return ret;
} }
@ -143,23 +132,20 @@ static int ecb_des_encrypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src, struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes) unsigned int nbytes)
{ {
struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
struct blkcipher_walk walk; struct blkcipher_walk walk;
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return ecb_desall_crypt(desc, CPACF_KM_DEA, ctx->key, &walk); return ecb_desall_crypt(desc, CPACF_KM_DEA, &walk);
} }
static int ecb_des_decrypt(struct blkcipher_desc *desc, static int ecb_des_decrypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src, struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes) unsigned int nbytes)
{ {
struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
struct blkcipher_walk walk; struct blkcipher_walk walk;
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return ecb_desall_crypt(desc, CPACF_KM_DEA | CPACF_DECRYPT, return ecb_desall_crypt(desc, CPACF_KM_DEA | CPACF_DECRYPT, &walk);
ctx->key, &walk);
} }
static struct crypto_alg ecb_des_alg = { static struct crypto_alg ecb_des_alg = {
@ -290,23 +276,21 @@ static int ecb_des3_encrypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src, struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes) unsigned int nbytes)
{ {
struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
struct blkcipher_walk walk; struct blkcipher_walk walk;
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return ecb_desall_crypt(desc, CPACF_KM_TDEA_192, ctx->key, &walk); return ecb_desall_crypt(desc, CPACF_KM_TDEA_192, &walk);
} }
static int ecb_des3_decrypt(struct blkcipher_desc *desc, static int ecb_des3_decrypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src, struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes) unsigned int nbytes)
{ {
struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
struct blkcipher_walk walk; struct blkcipher_walk walk;
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return ecb_desall_crypt(desc, CPACF_KM_TDEA_192 | CPACF_DECRYPT, return ecb_desall_crypt(desc, CPACF_KM_TDEA_192 | CPACF_DECRYPT,
ctx->key, &walk); &walk);
} }
static struct crypto_alg ecb_des3_alg = { static struct crypto_alg ecb_des3_alg = {
@ -371,73 +355,54 @@ static struct crypto_alg cbc_des3_alg = {
} }
}; };
static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes) static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
{ {
unsigned int i, n; unsigned int i, n;
/* align to block size, max. PAGE_SIZE */ /* align to block size, max. PAGE_SIZE */
n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(DES_BLOCK_SIZE - 1); n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(DES_BLOCK_SIZE - 1);
for (i = DES_BLOCK_SIZE; i < n; i += DES_BLOCK_SIZE) { memcpy(ctrptr, iv, DES_BLOCK_SIZE);
memcpy(ctrptr + i, ctrptr + i - DES_BLOCK_SIZE, DES_BLOCK_SIZE); for (i = (n / DES_BLOCK_SIZE) - 1; i > 0; i--) {
crypto_inc(ctrptr + i, DES_BLOCK_SIZE); memcpy(ctrptr + DES_BLOCK_SIZE, ctrptr, DES_BLOCK_SIZE);
crypto_inc(ctrptr + DES_BLOCK_SIZE, DES_BLOCK_SIZE);
ctrptr += DES_BLOCK_SIZE;
} }
return n; return n;
} }
static int ctr_desall_crypt(struct blkcipher_desc *desc, long func, static int ctr_desall_crypt(struct blkcipher_desc *desc, unsigned long fc,
struct s390_des_ctx *ctx,
struct blkcipher_walk *walk) struct blkcipher_walk *walk)
{ {
int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE); struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
u8 buf[DES_BLOCK_SIZE], *ctrptr;
unsigned int n, nbytes; unsigned int n, nbytes;
u8 buf[DES_BLOCK_SIZE], ctrbuf[DES_BLOCK_SIZE]; int ret, locked;
u8 *out, *in, *ctrptr = ctrbuf;
if (!walk->nbytes) locked = spin_trylock(&ctrblk_lock);
return ret;
if (spin_trylock(&ctrblk_lock)) ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
ctrptr = ctrblk;
memcpy(ctrptr, walk->iv, DES_BLOCK_SIZE);
while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) { while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
out = walk->dst.virt.addr; n = DES_BLOCK_SIZE;
in = walk->src.virt.addr; if (nbytes >= 2*DES_BLOCK_SIZE && locked)
while (nbytes >= DES_BLOCK_SIZE) { n = __ctrblk_init(ctrblk, walk->iv, nbytes);
if (ctrptr == ctrblk) ctrptr = (n > DES_BLOCK_SIZE) ? ctrblk : walk->iv;
n = __ctrblk_init(ctrptr, nbytes); cpacf_kmctr(fc, ctx->key, walk->dst.virt.addr,
else walk->src.virt.addr, n, ctrptr);
n = DES_BLOCK_SIZE; if (ctrptr == ctrblk)
cpacf_kmctr(func, ctx->key, out, in, n, ctrptr); memcpy(walk->iv, ctrptr + n - DES_BLOCK_SIZE,
if (n > DES_BLOCK_SIZE) DES_BLOCK_SIZE);
memcpy(ctrptr, ctrptr + n - DES_BLOCK_SIZE, crypto_inc(walk->iv, DES_BLOCK_SIZE);
DES_BLOCK_SIZE); ret = blkcipher_walk_done(desc, walk, nbytes - n);
crypto_inc(ctrptr, DES_BLOCK_SIZE);
out += n;
in += n;
nbytes -= n;
}
ret = blkcipher_walk_done(desc, walk, nbytes);
} }
if (ctrptr == ctrblk) { if (locked)
if (nbytes)
memcpy(ctrbuf, ctrptr, DES_BLOCK_SIZE);
else
memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
spin_unlock(&ctrblk_lock); spin_unlock(&ctrblk_lock);
} else {
if (!nbytes)
memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
}
/* final block may be < DES_BLOCK_SIZE, copy only nbytes */ /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
if (nbytes) { if (nbytes) {
out = walk->dst.virt.addr; cpacf_kmctr(fc, ctx->key, buf, walk->src.virt.addr,
in = walk->src.virt.addr; DES_BLOCK_SIZE, walk->iv);
cpacf_kmctr(func, ctx->key, buf, in, DES_BLOCK_SIZE, ctrbuf); memcpy(walk->dst.virt.addr, buf, nbytes);
memcpy(out, buf, nbytes); crypto_inc(walk->iv, DES_BLOCK_SIZE);
crypto_inc(ctrbuf, DES_BLOCK_SIZE);
ret = blkcipher_walk_done(desc, walk, 0); ret = blkcipher_walk_done(desc, walk, 0);
memcpy(walk->iv, ctrbuf, DES_BLOCK_SIZE);
} }
return ret; return ret;
} }
@ -446,23 +411,20 @@ static int ctr_des_encrypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src, struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes) unsigned int nbytes)
{ {
struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
struct blkcipher_walk walk; struct blkcipher_walk walk;
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return ctr_desall_crypt(desc, CPACF_KMCTR_DEA, ctx, &walk); return ctr_desall_crypt(desc, CPACF_KMCTR_DEA, &walk);
} }
static int ctr_des_decrypt(struct blkcipher_desc *desc, static int ctr_des_decrypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src, struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes) unsigned int nbytes)
{ {
struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
struct blkcipher_walk walk; struct blkcipher_walk walk;
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return ctr_desall_crypt(desc, CPACF_KMCTR_DEA | CPACF_DECRYPT, return ctr_desall_crypt(desc, CPACF_KMCTR_DEA | CPACF_DECRYPT, &walk);
ctx, &walk);
} }
static struct crypto_alg ctr_des_alg = { static struct crypto_alg ctr_des_alg = {
@ -490,23 +452,21 @@ static int ctr_des3_encrypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src, struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes) unsigned int nbytes)
{ {
struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
struct blkcipher_walk walk; struct blkcipher_walk walk;
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192, ctx, &walk); return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192, &walk);
} }
static int ctr_des3_decrypt(struct blkcipher_desc *desc, static int ctr_des3_decrypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src, struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes) unsigned int nbytes)
{ {
struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
struct blkcipher_walk walk; struct blkcipher_walk walk;
blkcipher_walk_init(&walk, dst, src, nbytes); blkcipher_walk_init(&walk, dst, src, nbytes);
return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192 | CPACF_DECRYPT, return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192 | CPACF_DECRYPT,
ctx, &walk); &walk);
} }
static struct crypto_alg ctr_des3_alg = { static struct crypto_alg ctr_des3_alg = {