+ 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,
+ &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;
+ }
+
+ // 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];
+ }
+ EVP_CIPHER_CTX_set_num(ctx, i);
+ ctr128_inc(iv_buffer->b);
+ }
+
+ return 1;
+}
+
+#define GRASSHOPPER_BLOCK_MASK (GRASSHOPPER_BLOCK_SIZE - 1)
+static inline void apply_acpkm_grasshopper(gost_grasshopper_cipher_ctx_ctr *
+ ctx, unsigned int *num)
+{
+ if (!ctx->section_size || (*num < ctx->section_size))
+ return;
+ acpkm_next(&ctx->c);
+ *num &= GRASSHOPPER_BLOCK_MASK;
+}
+
+/* If meshing is not configured via ctrl (setting section_size)
+ * this function works exactly like plain ctr */
+int gost_grasshopper_cipher_do_ctracpkm(EVP_CIPHER_CTX *ctx,
+ unsigned char *out,
+ const unsigned char *in, size_t inl)
+{
+ gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
+ unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
+ unsigned int num = EVP_CIPHER_CTX_num(ctx);
+
+ while ((num & GRASSHOPPER_BLOCK_MASK) && inl) {
+ *out++ = *in++ ^ c->partial_buffer.b[num & GRASSHOPPER_BLOCK_MASK];
+ --inl;
+ num++;
+ }
+ 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 *) & 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;
+ num += GRASSHOPPER_BLOCK_SIZE;
+ }
+
+ // last part
+ size_t lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
+ if (lasted > 0) {
+ apply_acpkm_grasshopper(c, &num);
+ grasshopper_encrypt_block(&c->c.encrypt_round_keys,
+ (grasshopper_w128_t *) iv,
+ &c->partial_buffer, &c->c.buffer);
+ for (i = 0; i < lasted; i++)
+ out[i] = c->partial_buffer.b[i] ^ in[i];
+ ctr128_inc(iv);
+ num += lasted;
+ }
+ EVP_CIPHER_CTX_set_num(ctx, num);
+
+ return 1;
+}
+
+/* ----------------------------------------------------------------------------------------------- */
+/*! Функция реализует операцию умножения двух элементов конечного поля \f$ \mathbb F_{2^{128}}\f$,
+ порожденного неприводимым многочленом
+ \f$ f(x) = x^{128} + x^7 + x^2 + x + 1 \in \mathbb F_2[x]\f$. Для умножения используется
+ простейшая реализация, основанная на приведении по модулю после каждого шага алгоритма. */
+/* ----------------------------------------------------------------------------------------------- */
+static void gf128_mul_uint64(uint64_t *z, uint64_t *x, uint64_t *y)
+{
+ int i = 0, n = 0;
+ uint64_t t, s0, s1;
+
+ BUF_reverse((unsigned char *)x, NULL, 16);
+ BUF_reverse((unsigned char *)y, NULL, 16);
+
+#ifdef L_ENDIAN
+ s0 = x[0];
+ s1 = x[1];
+#else
+ s0 = bswap_64(x[0]);
+ s1 = bswap_64(x[1]);
+#endif
+
+ memset(z, 0, sizeof(uint64_t)*2);
+
+ /* lower half */
+#ifdef L_ENDIAN
+ t = y[0];
+#else
+ t = bswap_64(y[0]);
+#endif
+
+ for( i = 0; i < 64; i++ ) {
+ if( t&0x1 ) { z[0] ^= s0; z[1] ^= s1; }
+ t >>= 1;
+ n = s1 >> 63;
+ s1 <<= 1; s1 ^= ( s0 >> 63 ); s0 <<= 1;
+ if(n) s0 ^= 0x87;
+ }
+
+ /* upper half */
+#ifdef L_ENDIAN
+ t = y[1];
+#else
+ t = bswap_64(y[1]);
+#endif
+
+ for( i = 0; i < 63; i++ ) {
+ if( t&0x1 ) { z[0] ^= s0; z[1] ^= s1; }
+ t >>= 1;
+ n = s1 >> 63;
+ s1 <<= 1; s1 ^= ( s0 >> 63 ); s0 <<= 1;
+ if( n ) s0 ^= 0x87;
+ }
+
+ if( t&0x1 ) {
+ z[0] ^= s0;
+ z[1] ^= s1;
+ }
+#ifndef L_ENDIAN
+ z[0] = bswap_64(z[0]);
+ z[1] = bswap_64(z[1]);
+#endif
+ BUF_reverse((unsigned char *)z, NULL, 16);
+}
+
+static void hexdump(FILE *f, const char *title, const unsigned char *s, int l)
+{
+ int n = 0;
+
+ fprintf(f, "%s", title);
+ for (; n < l; ++n) {
+ if ((n % 16) == 0)
+ fprintf(f, "\n%04x", n);
+ fprintf(f, " %02x", s[n]);
+ }
+ fprintf(f, "\n");
+}
+
+int gost_grasshopper_cipher_do_mgm(EVP_CIPHER_CTX *ctx, unsigned char *out,
+ const unsigned char *in, size_t inl)
+{
+ gost_grasshopper_cipher_ctx_mgm *c = (gost_grasshopper_cipher_ctx_mgm *)
+ 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;
+
+ size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
+ int rest_len = n % GRASSHOPPER_BLOCK_SIZE;
+ grasshopper_w128_t h;
+
+ grasshopper_w128_t *iv_buffer = (grasshopper_w128_t *) iv;
+ grasshopper_w128_t tmp;
+
+/* ======== Here we deal with associated data =========== */
+ 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);
+
+ 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, "Padded 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);
+
+ /* We finish processing associated data */
+ /* Pad rest of mgm_partial_buffer */
+ /* Process last block */
+ c->ad_length = n;
+ n = 0;
+ EVP_CIPHER_CTX_set_num(ctx, 0);
+ c->mgm_state = mgm_main_data;
+
+ fprintf(stderr, "============= Deal with main data\n");
+ }
+
+/* ======== Here we deal with main data =========== */
+ if (n == 0) {
+ /* actual IV derived from nonce */
+ grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
+ &c->partial_buffer, &c->c.buffer);
+ memcpy(iv, c->partial_buffer.b, GRASSHOPPER_BLOCK_SIZE);
+ //hexdump(stderr, "Y1", iv, 16);
+ }
+
+ while (rest_len && inl) {
+ *(current_out++) = *(current_in++) ^ c->partial_buffer.b[rest_len];
+ --inl;
+ n++;
+ rest_len++;
+ if (rest_len == GRASSHOPPER_BLOCK_SIZE)
+ {
+ rest_len = 0;
+ }
+ }
+ EVP_CIPHER_CTX_set_num(ctx, n);