-int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
- const unsigned char* in, size_t inl) {
- gost_grasshopper_cipher_ctx_ctr* c = (gost_grasshopper_cipher_ctx_ctr*) EVP_CIPHER_CTX_get_cipher_data(ctx);
- unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
- const unsigned char* current_in = in;
- unsigned char* current_out = out;
- grasshopper_w128_t* currentInputBlock;
- grasshopper_w128_t* currentOutputBlock;
- unsigned int n = EVP_CIPHER_CTX_num(ctx);
- size_t lasted;
- size_t i;
-
- while (n && inl) {
- *(current_out++) = *(current_in++) ^ c->partial_buffer.b[n];
- --inl;
- n = (n + 1) % GRASSHOPPER_BLOCK_SIZE;
- }
- EVP_CIPHER_CTX_set_num(ctx, n);
- size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
-
- grasshopper_w128_t* iv_buffer = (grasshopper_w128_t*) iv;
-
- // 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);
- ctr128_inc(iv_buffer->b);
- current_in += GRASSHOPPER_BLOCK_SIZE;
- current_out += GRASSHOPPER_BLOCK_SIZE;
- }
-
- // last part
- lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
- if (lasted > 0) {
- currentInputBlock = (grasshopper_w128_t*) current_in;
- currentOutputBlock = (grasshopper_w128_t*) current_out;
- grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer, &c->partial_buffer, &c->c.buffer);
- for (i = 0; i < lasted; i++) {
- currentOutputBlock->b[i] = c->partial_buffer.b[i] ^ currentInputBlock->b[i];
+ int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
+ const unsigned char *in, size_t inl) {
+ gost_grasshopper_cipher_ctx_ctr *c =
+ (gost_grasshopper_cipher_ctx_ctr *)
+ EVP_CIPHER_CTX_get_cipher_data(ctx);
+ unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
+ const unsigned char *current_in = in;
+ unsigned char *current_out = out;
+ grasshopper_w128_t *currentInputBlock;
+ grasshopper_w128_t *currentOutputBlock;
+ unsigned int n = EVP_CIPHER_CTX_num(ctx);
+ size_t lasted;
+ size_t i;
+
+ while (n && inl) {
+ *(current_out++) = *(current_in++) ^ c->partial_buffer.b[n];
+ --inl;
+ n = (n + 1) % GRASSHOPPER_BLOCK_SIZE;
+ }
+ EVP_CIPHER_CTX_set_num(ctx, n);
+ size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
+
+ grasshopper_w128_t *iv_buffer = (grasshopper_w128_t *) iv;
+
+ // 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);
+ ctr128_inc(iv_buffer->b);
+ current_in += GRASSHOPPER_BLOCK_SIZE;
+ current_out += GRASSHOPPER_BLOCK_SIZE;