Commit 0971eb0d authored by Herbert Xu's avatar Herbert Xu

[CRYPTO] ctr: Fix multi-page processing

When the data spans across a page boundary, CTR may incorrectly process
a partial block in the middle because the blkcipher walking code may
supply partial blocks in the middle as long as the total length of the
supplied data is more than a block.  CTR is supposed to return any unused
partial block in that case to the walker.

This patch fixes this by doing exactly that, returning partial blocks to
the walker unless we received less than a block-worth of data to start
with.

This also allows us to optimise the bulk of the processing since we no
longer have to worry about partial blocks until the very end.

Thanks to Tan Swee Heng for fixes and actually testing this :)
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 06e1a8f0
...@@ -59,6 +59,21 @@ static int crypto_ctr_setkey(struct crypto_tfm *parent, const u8 *key, ...@@ -59,6 +59,21 @@ static int crypto_ctr_setkey(struct crypto_tfm *parent, const u8 *key,
return err; return err;
} }
static void crypto_ctr_crypt_final(struct blkcipher_walk *walk,
struct crypto_cipher *tfm, u8 *ctrblk,
unsigned int countersize)
{
unsigned int bsize = crypto_cipher_blocksize(tfm);
u8 *keystream = ctrblk + bsize;
u8 *src = walk->src.virt.addr;
u8 *dst = walk->dst.virt.addr;
unsigned int nbytes = walk->nbytes;
crypto_cipher_encrypt_one(tfm, keystream, ctrblk);
crypto_xor(keystream, src, nbytes);
memcpy(dst, keystream, nbytes);
}
static int crypto_ctr_crypt_segment(struct blkcipher_walk *walk, static int crypto_ctr_crypt_segment(struct blkcipher_walk *walk,
struct crypto_cipher *tfm, u8 *ctrblk, struct crypto_cipher *tfm, u8 *ctrblk,
unsigned int countersize) unsigned int countersize)
...@@ -66,35 +81,23 @@ static int crypto_ctr_crypt_segment(struct blkcipher_walk *walk, ...@@ -66,35 +81,23 @@ static int crypto_ctr_crypt_segment(struct blkcipher_walk *walk,
void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
crypto_cipher_alg(tfm)->cia_encrypt; crypto_cipher_alg(tfm)->cia_encrypt;
unsigned int bsize = crypto_cipher_blocksize(tfm); unsigned int bsize = crypto_cipher_blocksize(tfm);
unsigned long alignmask = crypto_cipher_alignmask(tfm) |
(__alignof__(u32) - 1);
u8 ks[bsize + alignmask];
u8 *keystream = (u8 *)ALIGN((unsigned long)ks, alignmask + 1);
u8 *src = walk->src.virt.addr; u8 *src = walk->src.virt.addr;
u8 *dst = walk->dst.virt.addr; u8 *dst = walk->dst.virt.addr;
unsigned int nbytes = walk->nbytes; unsigned int nbytes = walk->nbytes;
do { do {
/* create keystream */ /* create keystream */
fn(crypto_cipher_tfm(tfm), keystream, ctrblk); fn(crypto_cipher_tfm(tfm), dst, ctrblk);
crypto_xor(keystream, src, min(nbytes, bsize)); crypto_xor(dst, src, bsize);
/* copy result into dst */
memcpy(dst, keystream, min(nbytes, bsize));
/* increment counter in counterblock */ /* increment counter in counterblock */
crypto_inc(ctrblk + bsize - countersize, countersize); crypto_inc(ctrblk + bsize - countersize, countersize);
if (nbytes < bsize)
break;
src += bsize; src += bsize;
dst += bsize; dst += bsize;
nbytes -= bsize; } while ((nbytes -= bsize) >= bsize);
} while (nbytes);
return 0; return nbytes;
} }
static int crypto_ctr_crypt_inplace(struct blkcipher_walk *walk, static int crypto_ctr_crypt_inplace(struct blkcipher_walk *walk,
...@@ -104,30 +107,22 @@ static int crypto_ctr_crypt_inplace(struct blkcipher_walk *walk, ...@@ -104,30 +107,22 @@ static int crypto_ctr_crypt_inplace(struct blkcipher_walk *walk,
void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
crypto_cipher_alg(tfm)->cia_encrypt; crypto_cipher_alg(tfm)->cia_encrypt;
unsigned int bsize = crypto_cipher_blocksize(tfm); unsigned int bsize = crypto_cipher_blocksize(tfm);
unsigned long alignmask = crypto_cipher_alignmask(tfm) |
(__alignof__(u32) - 1);
unsigned int nbytes = walk->nbytes; unsigned int nbytes = walk->nbytes;
u8 *src = walk->src.virt.addr; u8 *src = walk->src.virt.addr;
u8 ks[bsize + alignmask]; u8 *keystream = ctrblk + bsize;
u8 *keystream = (u8 *)ALIGN((unsigned long)ks, alignmask + 1);
do { do {
/* create keystream */ /* create keystream */
fn(crypto_cipher_tfm(tfm), keystream, ctrblk); fn(crypto_cipher_tfm(tfm), keystream, ctrblk);
crypto_xor(src, keystream, min(nbytes, bsize)); crypto_xor(src, keystream, bsize);
/* increment counter in counterblock */ /* increment counter in counterblock */
crypto_inc(ctrblk + bsize - countersize, countersize); crypto_inc(ctrblk + bsize - countersize, countersize);
if (nbytes < bsize)
break;
src += bsize; src += bsize;
nbytes -= bsize; } while ((nbytes -= bsize) >= bsize);
} while (nbytes); return nbytes;
return 0;
} }
static int crypto_ctr_crypt(struct blkcipher_desc *desc, static int crypto_ctr_crypt(struct blkcipher_desc *desc,
...@@ -143,7 +138,7 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc, ...@@ -143,7 +138,7 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc,
crypto_instance_ctx(crypto_tfm_alg_instance(&tfm->base)); crypto_instance_ctx(crypto_tfm_alg_instance(&tfm->base));
unsigned long alignmask = crypto_cipher_alignmask(child) | unsigned long alignmask = crypto_cipher_alignmask(child) |
(__alignof__(u32) - 1); (__alignof__(u32) - 1);
u8 cblk[bsize + alignmask]; u8 cblk[bsize * 2 + alignmask];
u8 *counterblk = (u8 *)ALIGN((unsigned long)cblk, alignmask + 1); u8 *counterblk = (u8 *)ALIGN((unsigned long)cblk, alignmask + 1);
int err; int err;
...@@ -158,7 +153,7 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc, ...@@ -158,7 +153,7 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc,
/* initialize counter portion of counter block */ /* initialize counter portion of counter block */
crypto_inc(counterblk + bsize - ictx->countersize, ictx->countersize); crypto_inc(counterblk + bsize - ictx->countersize, ictx->countersize);
while (walk.nbytes) { while (walk.nbytes >= bsize) {
if (walk.src.virt.addr == walk.dst.virt.addr) if (walk.src.virt.addr == walk.dst.virt.addr)
nbytes = crypto_ctr_crypt_inplace(&walk, child, nbytes = crypto_ctr_crypt_inplace(&walk, child,
counterblk, counterblk,
...@@ -170,6 +165,13 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc, ...@@ -170,6 +165,13 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc,
err = blkcipher_walk_done(desc, &walk, nbytes); err = blkcipher_walk_done(desc, &walk, nbytes);
} }
if (walk.nbytes) {
crypto_ctr_crypt_final(&walk, child, counterblk,
ictx->countersize);
err = blkcipher_walk_done(desc, &walk, 0);
}
return err; return err;
} }
...@@ -277,7 +279,7 @@ static struct crypto_instance *crypto_ctr_alloc(struct rtattr **tb) ...@@ -277,7 +279,7 @@ static struct crypto_instance *crypto_ctr_alloc(struct rtattr **tb)
inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER; inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
inst->alg.cra_priority = alg->cra_priority; inst->alg.cra_priority = alg->cra_priority;
inst->alg.cra_blocksize = 1; inst->alg.cra_blocksize = 1;
inst->alg.cra_alignmask = __alignof__(u32) - 1; inst->alg.cra_alignmask = alg->cra_alignmask | (__alignof__(u32) - 1);
inst->alg.cra_type = &crypto_blkcipher_type; inst->alg.cra_type = &crypto_blkcipher_type;
inst->alg.cra_blkcipher.ivsize = ivsize; inst->alg.cra_blkcipher.ivsize = ivsize;
......
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