From 23f3265d293c91a80988dadcb4f7e36b60dbef0a Mon Sep 17 00:00:00 2001 From: Dmitry Belyavskiy Date: Tue, 11 Sep 2018 13:11:29 +0300 Subject: [PATCH] Fixed in-place encryption for CTR and CTR-ACPKM modes --- gost_grasshopper_cipher.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/gost_grasshopper_cipher.c b/gost_grasshopper_cipher.c index 2818e86..0d0eacb 100644 --- a/gost_grasshopper_cipher.c +++ b/gost_grasshopper_cipher.c @@ -418,14 +418,16 @@ int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out, size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE; grasshopper_w128_t *iv_buffer = (grasshopper_w128_t *) iv; + grasshopper_w128_t tmp; // full parts for (i = 0; i < blocks; i++) { currentInputBlock = (grasshopper_w128_t *) current_in; currentOutputBlock = (grasshopper_w128_t *) current_out; grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer, - currentOutputBlock, &c->c.buffer); - grasshopper_append128(currentOutputBlock, currentInputBlock); + &c->partial_buffer, &c->c.buffer); + grasshopper_plus128(&tmp, &c->partial_buffer, currentInputBlock); + grasshopper_copy128(currentOutputBlock, &tmp); ctr128_inc(iv_buffer->b); current_in += GRASSHOPPER_BLOCK_SIZE; current_out += GRASSHOPPER_BLOCK_SIZE; @@ -476,15 +478,16 @@ int gost_grasshopper_cipher_do_ctracpkm(EVP_CIPHER_CTX *ctx, } size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE; size_t i; + grasshopper_w128_t tmp; // full parts for (i = 0; i < blocks; i++) { apply_acpkm_grasshopper(c, &num); grasshopper_encrypt_block(&c->c.encrypt_round_keys, (grasshopper_w128_t *) iv, - (grasshopper_w128_t *) out, &c->c.buffer); - grasshopper_append128((grasshopper_w128_t *) out, - (grasshopper_w128_t *) in); + (grasshopper_w128_t *) &c->partial_buffer, &c->c.buffer); + grasshopper_plus128(&tmp, &c->partial_buffer, (grasshopper_w128_t *) in); + grasshopper_copy128((grasshopper_w128_t *) out, &tmp); ctr128_inc(iv); in += GRASSHOPPER_BLOCK_SIZE; out += GRASSHOPPER_BLOCK_SIZE; -- 2.39.5