Skip to content

Commit 41fdab3

Browse files
Joy Lattenherbertx
authored andcommitted
[CRYPTO] ctr: Add countersize
This patch adds countersize to CTR mode. The template is now ctr(algo,noncesize,ivsize,countersize). For example, ctr(aes,4,8,4) indicates the counterblock will be composed of a salt/nonce that is 4 bytes, an iv that is 8 bytes and the counter is 4 bytes. When noncesize + ivsize < blocksize, CTR initializes the last block - ivsize - noncesize portion of the block to zero. Otherwise the counter block is composed of the IV (and nonce if necessary). If noncesize + ivsize == blocksize, then this indicates that user is passing in entire counterblock. Thus countersize indicates the amount of bytes in counterblock to use as the counter for incrementing. CTR will increment counter portion by 1, and begin encryption with that value. Note that CTR assumes the counter portion of the block that will be incremented is stored in big endian. Signed-off-by: Joy Latten <latten@austin.ibm.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent d3e7480 commit 41fdab3

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

‎crypto/ctr.c‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct ctr_instance_ctx {
2323
struct crypto_spawn alg;
2424
unsigned int noncesize;
2525
unsigned int ivsize;
26+
unsigned int countersize;
2627
};
2728

2829
struct crypto_ctr_ctx {
@@ -186,7 +187,6 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc,
186187
unsigned long alignmask = crypto_cipher_alignmask(child);
187188
u8 cblk[bsize + alignmask];
188189
u8 *counterblk = (u8 *)ALIGN((unsigned long)cblk, alignmask + 1);
189-
unsigned int countersize;
190190
int err;
191191

192192
blkcipher_walk_init(&walk, dst, src, nbytes);
@@ -198,18 +198,18 @@ static int crypto_ctr_crypt(struct blkcipher_desc *desc,
198198
memcpy(counterblk + ictx->noncesize, walk.iv, ictx->ivsize);
199199

200200
/* initialize counter portion of counter block */
201-
countersize = bsize - ictx->noncesize - ictx->ivsize;
202-
ctr_inc_quad(counterblk + (bsize - countersize), countersize);
201+
ctr_inc_quad(counterblk + (bsize - ictx->countersize),
202+
ictx->countersize);
203203

204204
while (walk.nbytes) {
205205
if (walk.src.virt.addr == walk.dst.virt.addr)
206206
nbytes = crypto_ctr_crypt_inplace(&walk, child,
207207
counterblk,
208-
countersize);
208+
ictx->countersize);
209209
else
210210
nbytes = crypto_ctr_crypt_segment(&walk, child,
211211
counterblk,
212-
countersize);
212+
ictx->countersize);
213213

214214
err = blkcipher_walk_done(desc, &walk, nbytes);
215215
}
@@ -251,6 +251,7 @@ static struct crypto_instance *crypto_ctr_alloc(struct rtattr **tb)
251251
struct ctr_instance_ctx *ictx;
252252
unsigned int noncesize;
253253
unsigned int ivsize;
254+
unsigned int countersize;
254255
int err;
255256

256257
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
@@ -270,9 +271,17 @@ static struct crypto_instance *crypto_ctr_alloc(struct rtattr **tb)
270271
if (err)
271272
goto out_put_alg;
272273

273-
/* verify size of nonce + iv + counter */
274+
err = crypto_attr_u32(tb[4], &countersize);
275+
if (err)
276+
goto out_put_alg;
277+
278+
/* verify size of nonce + iv + counter
279+
* counter must be >= 4 bytes.
280+
*/
274281
err = -EINVAL;
275-
if ((noncesize + ivsize) >= alg->cra_blocksize)
282+
if (((noncesize + ivsize + countersize) < alg->cra_blocksize) ||
283+
((noncesize + ivsize) > alg->cra_blocksize) ||
284+
(countersize > alg->cra_blocksize) || (countersize < 4))
276285
goto out_put_alg;
277286

278287
inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
@@ -282,20 +291,21 @@ static struct crypto_instance *crypto_ctr_alloc(struct rtattr **tb)
282291

283292
err = -ENAMETOOLONG;
284293
if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
285-
"ctr(%s,%u,%u)", alg->cra_name, noncesize,
286-
ivsize) >= CRYPTO_MAX_ALG_NAME) {
294+
"ctr(%s,%u,%u,%u)", alg->cra_name, noncesize,
295+
ivsize, countersize) >= CRYPTO_MAX_ALG_NAME) {
287296
goto err_free_inst;
288297
}
289298

290299
if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
291-
"ctr(%s,%u,%u)", alg->cra_driver_name, noncesize,
292-
ivsize) >= CRYPTO_MAX_ALG_NAME) {
300+
"ctr(%s,%u,%u,%u)", alg->cra_driver_name, noncesize,
301+
ivsize, countersize) >= CRYPTO_MAX_ALG_NAME) {
293302
goto err_free_inst;
294303
}
295304

296305
ictx = crypto_instance_ctx(inst);
297306
ictx->noncesize = noncesize;
298307
ictx->ivsize = ivsize;
308+
ictx->countersize = countersize;
299309

300310
err = crypto_init_spawn(&ictx->alg, alg, inst,
301311
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);

‎crypto/tcrypt.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,9 +969,9 @@ static void do_test(void)
969969
AES_XTS_ENC_TEST_VECTORS);
970970
test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
971971
AES_XTS_DEC_TEST_VECTORS);
972-
test_cipher("ctr(aes,4,8)", ENCRYPT, aes_ctr_enc_tv_template,
972+
test_cipher("ctr(aes,4,8,4)", ENCRYPT, aes_ctr_enc_tv_template,
973973
AES_CTR_ENC_TEST_VECTORS);
974-
test_cipher("ctr(aes,4,8)", DECRYPT, aes_ctr_dec_tv_template,
974+
test_cipher("ctr(aes,4,8,4)", DECRYPT, aes_ctr_dec_tv_template,
975975
AES_CTR_DEC_TEST_VECTORS);
976976

977977
//CAST5
@@ -1160,9 +1160,9 @@ static void do_test(void)
11601160
AES_XTS_ENC_TEST_VECTORS);
11611161
test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
11621162
AES_XTS_DEC_TEST_VECTORS);
1163-
test_cipher("ctr(aes,4,8)", ENCRYPT, aes_ctr_enc_tv_template,
1163+
test_cipher("ctr(aes,4,8,4)", ENCRYPT, aes_ctr_enc_tv_template,
11641164
AES_CTR_ENC_TEST_VECTORS);
1165-
test_cipher("ctr(aes,4,8)", DECRYPT, aes_ctr_dec_tv_template,
1165+
test_cipher("ctr(aes,4,8,4)", DECRYPT, aes_ctr_dec_tv_template,
11661166
AES_CTR_DEC_TEST_VECTORS);
11671167
break;
11681168

0 commit comments

Comments
 (0)