From 90b482aa1eef35d4f4b0189bd37b4a1486042ae6 Mon Sep 17 00:00:00 2001 From: Karel Miko Date: Tue, 25 Apr 2017 17:21:35 +0200 Subject: [PATCH] stream/chacha - improved counter increment --- src/stream/chacha/chacha_crypt.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/stream/chacha/chacha_crypt.c b/src/stream/chacha/chacha_crypt.c index de42a10..30b5da7 100644 --- a/src/stream/chacha/chacha_crypt.c +++ b/src/stream/chacha/chacha_crypt.c @@ -71,8 +71,14 @@ int chacha_crypt(chacha_state *st, const unsigned char *in, unsigned long inlen, } for (;;) { _chacha_block(buf, st->input, st->rounds); - /* increment the counter */ - if (!++st->input[12] && !++st->input[13] && !++st->input[14]) { ++st->input[15]; } + if (st->ivlen == 8) { + /* IV-64bit, increment 64bit counter */ + if (0 == ++st->input[12] && 0 == ++st->input[13]) return CRYPT_OVERFLOW; + } + else { + /* IV-96bit, increment 32bit counter */ + if (0 == ++st->input[12]) return CRYPT_OVERFLOW; + } if (inlen <= 64) { for (i = 0; i < inlen; ++i) out[i] = in[i] ^ buf[i]; st->ksleft = 64 - inlen;