Skip to content

Commit 0971eb0

Browse files
committed
[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: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 06e1a8f commit 0971eb0

1 file changed

Lines changed: 33 additions & 31 deletions

File tree

‎crypto/ctr.c‎

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,42 +59,45 @@ static int crypto_ctr_setkey(struct crypto_tfm *parent, const u8 *key,
5959
return err;
6060
}
6161

62+
static void crypto_ctr_crypt_final(struct blkcipher_walk *walk,
63+
struct crypto_cipher *tfm, u8 *ctrblk,
64+
unsigned int countersize)
65+
{
66+
unsigned int bsize = crypto_cipher_blocksize(tfm);
67+
u8 *keystream = ctrblk + bsize;
68+
u8 *src = walk->src.virt.addr;
69+
u8 *dst = walk->dst.virt.addr;
70+
unsigned int nbytes = walk->nbytes;
71+
72+
crypto_cipher_encrypt_one(tfm, keystream, ctrblk);
73+
crypto_xor(keystream, src, nbytes);
74+
memcpy(dst, keystream, nbytes);
75+
}
76+
6277
static int crypto_ctr_crypt_segment(struct blkcipher_walk *walk,
6378
struct crypto_cipher *tfm, u8 *ctrblk,
6479
unsigned int countersize)
6580
{
6681
void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
6782
crypto_cipher_alg(tfm)->cia_encrypt;
6883
unsigned int bsize = crypto_cipher_blocksize(tfm);
69-
unsigned long alignmask = crypto_cipher_alignmask(tfm) |
70-
(__alignof__(u32) - 1);
71-
u8 ks[bsize + alignmask];
72-
u8 *keystream = (u8 *)ALIGN((unsigned long)ks, alignmask + 1);
7384
u8 *src = walk->src.virt.addr;
7485
u8 *dst = walk->dst.virt.addr;
7586
unsigned int nbytes = walk->nbytes;
7687

7788
do {
7889
/* create keystream */
79-
fn(crypto_cipher_tfm(tfm), keystream, ctrblk);
80-
crypto_xor(keystream, src, min(nbytes, bsize));
81-
82-
/* copy result into dst */
83-
memcpy(dst, keystream, min(nbytes, bsize));
90+
fn(crypto_cipher_tfm(tfm), dst, ctrblk);
91+
crypto_xor(dst, src, bsize);
8492

8593
/* increment counter in counterblock */
8694
crypto_inc(ctrblk + bsize - countersize, countersize);
8795

88-
if (nbytes < bsize)
89-
break;
90-
9196
src += bsize;
9297
dst += bsize;
93-
nbytes -= bsize;
94-
95-
} while (nbytes);
98+
} while ((nbytes -= bsize) >= bsize);
9699

97-
return 0;
100+
return nbytes;
98101
}
99102

100103
static int crypto_ctr_crypt_inplace(struct blkcipher_walk *walk,
@@ -104,30 +107,22 @@ static int crypto_ctr_crypt_inplace(struct blkcipher_walk *walk,
104107
void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
105108
crypto_cipher_alg(tfm)->cia_encrypt;
106109
unsigned int bsize = crypto_cipher_blocksize(tfm);
107-
unsigned long alignmask = crypto_cipher_alignmask(tfm) |
108-
(__alignof__(u32) - 1);
109110
unsigned int nbytes = walk->nbytes;
110111
u8 *src = walk->src.virt.addr;
111-
u8 ks[bsize + alignmask];
112-
u8 *keystream = (u8 *)ALIGN((unsigned long)ks, alignmask + 1);
112+
u8 *keystream = ctrblk + bsize;
113113

114114
do {
115115
/* create keystream */
116116
fn(crypto_cipher_tfm(tfm), keystream, ctrblk);
117-
crypto_xor(src, keystream, min(nbytes, bsize));
117+
crypto_xor(src, keystream, bsize);
118118

119119
/* increment counter in counterblock */
120120
crypto_inc(ctrblk + bsize - countersize, countersize);
121121

122-
if (nbytes < bsize)
123-
break;
124-
125122
src += bsize;
126-
nbytes -= bsize;
123+
} while ((nbytes -= bsize) >= bsize);
127124

128-
} while (nbytes);
129-
130-
return 0;
125+
return nbytes;
131126
}
132127

133128
static int crypto_ctr_crypt(struct blkcipher_desc *desc,
@@ -143,7 +138,7 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc,
143138
crypto_instance_ctx(crypto_tfm_alg_instance(&tfm->base));
144139
unsigned long alignmask = crypto_cipher_alignmask(child) |
145140
(__alignof__(u32) - 1);
146-
u8 cblk[bsize + alignmask];
141+
u8 cblk[bsize * 2 + alignmask];
147142
u8 *counterblk = (u8 *)ALIGN((unsigned long)cblk, alignmask + 1);
148143
int err;
149144

@@ -158,7 +153,7 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc,
158153
/* initialize counter portion of counter block */
159154
crypto_inc(counterblk + bsize - ictx->countersize, ictx->countersize);
160155

161-
while (walk.nbytes) {
156+
while (walk.nbytes >= bsize) {
162157
if (walk.src.virt.addr == walk.dst.virt.addr)
163158
nbytes = crypto_ctr_crypt_inplace(&walk, child,
164159
counterblk,
@@ -170,6 +165,13 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc,
170165

171166
err = blkcipher_walk_done(desc, &walk, nbytes);
172167
}
168+
169+
if (walk.nbytes) {
170+
crypto_ctr_crypt_final(&walk, child, counterblk,
171+
ictx->countersize);
172+
err = blkcipher_walk_done(desc, &walk, 0);
173+
}
174+
173175
return err;
174176
}
175177

@@ -277,7 +279,7 @@ static struct crypto_instance *crypto_ctr_alloc(struct rtattr **tb)
277279
inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
278280
inst->alg.cra_priority = alg->cra_priority;
279281
inst->alg.cra_blocksize = 1;
280-
inst->alg.cra_alignmask = __alignof__(u32) - 1;
282+
inst->alg.cra_alignmask = alg->cra_alignmask | (__alignof__(u32) - 1);
281283
inst->alg.cra_type = &crypto_blkcipher_type;
282284

283285
inst->alg.cra_blkcipher.ivsize = ivsize;

0 commit comments

Comments
 (0)