Commit 7bac4f5b authored by Martin Schwidefsky's avatar Martin Schwidefsky

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: default avatarHarald Freudenberger <freude@linux.vnet.ibm.com>
Signed-off-by: default avatarMartin Schwidefsky <schwidefsky@de.ibm.com>
parent 69c0e360
This diff is collapsed.
...@@ -85,57 +85,46 @@ static struct crypto_alg des_alg = { ...@@ -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) struct blkcipher_walk *walk)
{ {
int ret = blkcipher_walk_virt(desc, walk); struct s390_des_ctx *ctx = 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) >= 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_km(fc, ctx->key, walk->dst.virt.addr,
u8 *in = walk->src.virt.addr; walk->src.virt.addr, n);
ret = blkcipher_walk_done(desc, walk, nbytes - n);
cpacf_km(func, key, out, in, n);
nbytes &= DES_BLOCK_SIZE - 1;
ret = blkcipher_walk_done(desc, walk, nbytes);
} }
return ret; return ret;
} }
static int cbc_desall_crypt(struct blkcipher_desc *desc, long func, static int cbc_desall_crypt(struct blkcipher_desc *desc, unsigned long fc,
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;
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, ...@@ -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, ...@@ -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 = { ...@@ -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)
return ret;
if (spin_trylock(&ctrblk_lock)) locked = spin_trylock(&ctrblk_lock);
ctrptr = ctrblk;
memcpy(ctrptr, walk->iv, DES_BLOCK_SIZE); ret = blkcipher_walk_virt_block(desc, walk, 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, ...@@ -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, ...@@ -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 = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment