- if (out == NULL && c->mgm_state == mgm_associated_data) {
- if (n == 0) {
- grasshopper_encrypt_block(&c->c.encrypt_round_keys, c->mgm_iv,
- &c->partial_buffer, &c->c.buffer);
- memcpy(c->mgm_iv, &c->partial_buffer, GRASSHOPPER_BLOCK_SIZE);
- hexdump(stderr, "Tag", c->tag, 16);
- }
-
- if (rest_len != 0) {
- /* Finalize partial_data */
- if (inl + rest_len < GRASSHOPPER_BLOCK_SIZE) {
- memcpy(c->mgm_partial_buffer.b+rest_len, current_in, inl);
- n += inl;
- EVP_CIPHER_CTX_set_num(ctx, n);
- return 1;
- } else {
- memcpy(c->mgm_partial_buffer.b+rest_len, current_in, GRASSHOPPER_BLOCK_SIZE - rest_len);
-
- grasshopper_encrypt_block(&c->c.encrypt_round_keys, c->mgm_iv,
- &h, &c->c.buffer);
- inc_counter(c->mgm_iv->b, 8);
-
- hexdump(stderr, "Hnext", h.b, 16);
- hexdump(stderr, "Adata", c->mgm_partial_buffer.b, 16);
- /* Galois multiply Hi * Ai */
- gf128_mul_uint64(tmp.q, h.q, c->mgm_partial_buffer.q);
-
- /* XOR to c->tag */
- grasshopper_plus128(&h, (grasshopper_w128_t *)c->tag, &tmp);
- grasshopper_copy128((grasshopper_w128_t *)c->tag, &h);
- hexdump(stderr, "Tag", c->tag, 16);
-
- current_in += GRASSHOPPER_BLOCK_SIZE - rest_len;
- inl -= (GRASSHOPPER_BLOCK_SIZE - rest_len);
- n += GRASSHOPPER_BLOCK_SIZE - rest_len;
- }
- }
-
- while (inl >= GRASSHOPPER_BLOCK_SIZE) {
- currentInputBlock = (grasshopper_w128_t *) current_in;
+ if (out == NULL && c->mgm_state == mgm_associated_data) {
+ if (n == 0) {
+ grasshopper_encrypt_block(&c->c.encrypt_round_keys, c->mgm_iv,
+ &c->partial_buffer, &c->c.buffer);
+ memcpy(c->mgm_iv, &c->partial_buffer, GRASSHOPPER_BLOCK_SIZE);
+ hexdump(stderr, "Tag", c->tag, 16);
+ }
+
+ if (rest_len != 0) {
+ /* Finalize partial_data */
+ if (inl + rest_len < GRASSHOPPER_BLOCK_SIZE) {
+ memcpy(c->mgm_partial_buffer.b + rest_len, current_in, inl);
+ n += inl;
+ EVP_CIPHER_CTX_set_num(ctx, n);
+ return 1;
+ } else {
+ memcpy(c->mgm_partial_buffer.b + rest_len, current_in,
+ GRASSHOPPER_BLOCK_SIZE - rest_len);
+
+ grasshopper_encrypt_block(&c->c.encrypt_round_keys, c->mgm_iv,
+ &h, &c->c.buffer);
+ inc_counter(c->mgm_iv->b, 8);
+
+ hexdump(stderr, "Hnext", h.b, 16);
+ hexdump(stderr, "Adata", c->mgm_partial_buffer.b, 16);
+ /* Galois multiply Hi * Ai */
+ gf128_mul_uint64(tmp.q, h.q, c->mgm_partial_buffer.q);
+
+ /* XOR to c->tag */
+ grasshopper_plus128(&h, (grasshopper_w128_t *) c->tag, &tmp);
+ grasshopper_copy128((grasshopper_w128_t *) c->tag, &h);
+ hexdump(stderr, "Tag", c->tag, 16);
+
+ current_in += GRASSHOPPER_BLOCK_SIZE - rest_len;
+ inl -= (GRASSHOPPER_BLOCK_SIZE - rest_len);
+ n += GRASSHOPPER_BLOCK_SIZE - rest_len;
+ }
+ }
+
+ while (inl >= GRASSHOPPER_BLOCK_SIZE) {
+ currentInputBlock = (grasshopper_w128_t *) current_in;
+
+ grasshopper_encrypt_block(&c->c.encrypt_round_keys, c->mgm_iv,
+ &h, &c->c.buffer);
+ inc_counter(c->mgm_iv->b, 8);
+
+ hexdump(stderr, "Hnext", h.b, 16);
+ hexdump(stderr, "Adata", currentInputBlock->b, 16);
+ /* Galois multiply */
+ gf128_mul_uint64(tmp.q, h.q, currentInputBlock->q);
+
+ /* XOR to c->tag */
+ grasshopper_plus128(&h, (grasshopper_w128_t *) c->tag, &tmp);
+ grasshopper_copy128((grasshopper_w128_t *) c->tag, &h);
+ hexdump(stderr, "Tag", c->tag, 16);
+
+ current_in += GRASSHOPPER_BLOCK_SIZE;
+ inl -= GRASSHOPPER_BLOCK_SIZE;
+ n += GRASSHOPPER_BLOCK_SIZE;
+ }
+
+ if (inl > 0) {
+ memcpy(c->mgm_partial_buffer.b, current_in, inl);
+ n += inl;
+ }
+
+ EVP_CIPHER_CTX_set_num(ctx, n);
+ return 1;
+ }
+
+ if (out == NULL && in != NULL && inl != 0 && c->mgm_state == mgm_main_data) {
+ GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_DO_MGM, GOST_R_BAD_ORDER);
+ return 0;
+ }
+
+ if (out != NULL && c->mgm_state == mgm_associated_data) {
+ memset(c->mgm_partial_buffer.b + rest_len, 0,
+ GRASSHOPPER_BLOCK_SIZE - rest_len);