]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_grasshopper_cipher.c
CI: copy GitHub Actions files from master
[openssl-gost/engine.git] / gost_grasshopper_cipher.c
1 /*
2  * Maxim Tishkov 2016
3  * Copyright (c) 2020 Vitaly Chikunov <vt@altlinux.org>
4  * This file is distributed under the same license as OpenSSL
5  */
6
7 #include "gost_grasshopper_cipher.h"
8 #include "gost_grasshopper_defines.h"
9 #include "gost_grasshopper_math.h"
10 #include "gost_grasshopper_core.h"
11 #include "gost_gost2015.h"
12
13 #include <openssl/evp.h>
14 #include <openssl/rand.h>
15 #include <openssl/err.h>
16
17 #include <string.h>
18
19 #include "gost_lcl.h"
20 #include "e_gost_err.h"
21
22 enum GRASSHOPPER_CIPHER_TYPE {
23     GRASSHOPPER_CIPHER_ECB = 0,
24     GRASSHOPPER_CIPHER_CBC,
25     GRASSHOPPER_CIPHER_OFB,
26     GRASSHOPPER_CIPHER_CFB,
27     GRASSHOPPER_CIPHER_CTR,
28     GRASSHOPPER_CIPHER_CTRACPKM,
29     GRASSHOPPER_CIPHER_CTRACPKMOMAC,
30     GRASSHOPPER_CIPHER_MGM,
31 };
32
33 static GOST_cipher grasshopper_template_cipher = {
34     .block_size = GRASSHOPPER_BLOCK_SIZE,
35     .key_len = GRASSHOPPER_KEY_SIZE,
36     .flags = EVP_CIPH_RAND_KEY |
37         EVP_CIPH_ALWAYS_CALL_INIT,
38     .cleanup = gost_grasshopper_cipher_cleanup,
39     .ctx_size = sizeof(gost_grasshopper_cipher_ctx),
40     .set_asn1_parameters = gost_grasshopper_set_asn1_parameters,
41     .get_asn1_parameters = gost_grasshopper_get_asn1_parameters,
42     .ctrl = gost_grasshopper_cipher_ctl,
43 };
44
45 GOST_cipher grasshopper_ecb_cipher = {
46     .nid = NID_grasshopper_ecb,
47     .template = &grasshopper_template_cipher,
48     .flags = EVP_CIPH_ECB_MODE,
49     .init = gost_grasshopper_cipher_init_ecb,
50     .do_cipher = gost_grasshopper_cipher_do_ecb,
51 };
52
53 GOST_cipher grasshopper_cbc_cipher = {
54     .nid = NID_grasshopper_cbc,
55     .template = &grasshopper_template_cipher,
56     .iv_len = 16,
57     .flags = EVP_CIPH_CBC_MODE |
58         EVP_CIPH_CUSTOM_IV,
59     .init = gost_grasshopper_cipher_init_cbc,
60     .do_cipher = gost_grasshopper_cipher_do_cbc,
61 };
62
63 GOST_cipher grasshopper_ofb_cipher = {
64     .nid = NID_grasshopper_ofb,
65     .template = &grasshopper_template_cipher,
66     .block_size = 1,
67     .iv_len = 16,
68     .flags = EVP_CIPH_OFB_MODE |
69         EVP_CIPH_NO_PADDING |
70         EVP_CIPH_CUSTOM_IV,
71     .init = gost_grasshopper_cipher_init_ofb,
72     .do_cipher = gost_grasshopper_cipher_do_ofb,
73 };
74
75 GOST_cipher grasshopper_cfb_cipher = {
76     .nid = NID_grasshopper_cfb,
77     .template = &grasshopper_template_cipher,
78     .block_size = 1,
79     .iv_len = 16,
80     .flags = EVP_CIPH_CFB_MODE |
81         EVP_CIPH_NO_PADDING |
82         EVP_CIPH_CUSTOM_IV,
83     .init = gost_grasshopper_cipher_init_cfb,
84     .do_cipher = gost_grasshopper_cipher_do_cfb,
85 };
86
87 GOST_cipher grasshopper_ctr_cipher = {
88     .nid = NID_grasshopper_ctr,
89     .template = &grasshopper_template_cipher,
90     .block_size = 1,
91     .iv_len = 8,
92     .flags = EVP_CIPH_CTR_MODE |
93         EVP_CIPH_NO_PADDING |
94         EVP_CIPH_CUSTOM_IV,
95     .init = gost_grasshopper_cipher_init_ctr,
96     .do_cipher = gost_grasshopper_cipher_do_ctr,
97     .ctx_size = sizeof(gost_grasshopper_cipher_ctx_ctr),
98 };
99
100 GOST_cipher grasshopper_ctr_acpkm_cipher = {
101     .nid = NID_kuznyechik_ctr_acpkm,
102     .template = &grasshopper_template_cipher,
103     .block_size = 1,
104     .iv_len = 8,
105     .flags = EVP_CIPH_CTR_MODE |
106         EVP_CIPH_NO_PADDING |
107         EVP_CIPH_CUSTOM_IV,
108     .init = gost_grasshopper_cipher_init_ctracpkm,
109     .do_cipher = gost_grasshopper_cipher_do_ctracpkm,
110     .ctx_size = sizeof(gost_grasshopper_cipher_ctx_ctr),
111 };
112
113 GOST_cipher grasshopper_ctr_acpkm_omac_cipher = {
114     .nid = NID_kuznyechik_ctr_acpkm_omac,
115     .template = &grasshopper_template_cipher,
116     .block_size = 1,
117     .iv_len = 8,
118     .flags = EVP_CIPH_CTR_MODE |
119         EVP_CIPH_NO_PADDING |
120         EVP_CIPH_CUSTOM_IV |
121         EVP_CIPH_FLAG_CUSTOM_CIPHER |
122         EVP_CIPH_FLAG_CIPHER_WITH_MAC |
123         EVP_CIPH_CUSTOM_COPY,
124     .init = gost_grasshopper_cipher_init_ctracpkm_omac,
125     .do_cipher = gost_grasshopper_cipher_do_ctracpkm_omac,
126     .ctx_size = sizeof(gost_grasshopper_cipher_ctx_ctr),
127 };
128
129 GOST_cipher grasshopper_mgm_cipher = {
130     .nid = NID_kuznyechik_mgm,
131     .template = &grasshopper_template_cipher,
132     .block_size = 1,
133     .iv_len = 16,
134     .flags = EVP_CIPH_NO_PADDING |
135         EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | 
136         EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER,
137     .cleanup = gost_grasshopper_mgm_cleanup,
138     .ctrl = gost_grasshopper_mgm_ctrl,
139     .init = gost_grasshopper_cipher_init_mgm,
140     .do_cipher = gost_grasshopper_cipher_do_mgm,
141     .ctx_size = sizeof(gost_mgm_ctx)
142 };
143
144 /* first 256 bit of D from draft-irtf-cfrg-re-keying-12 */
145 static const unsigned char ACPKM_D_2018[] = {
146     0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /*  64 bit */
147     0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 128 bit */
148     0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
149     0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 256 bit */
150 };
151
152 static void acpkm_next(gost_grasshopper_cipher_ctx * c)
153 {
154     unsigned char newkey[GRASSHOPPER_KEY_SIZE];
155     const int J = GRASSHOPPER_KEY_SIZE / GRASSHOPPER_BLOCK_SIZE;
156     int n;
157
158     for (n = 0; n < J; n++) {
159         const unsigned char *D_n = &ACPKM_D_2018[n * GRASSHOPPER_BLOCK_SIZE];
160
161         grasshopper_encrypt_block(&c->encrypt_round_keys,
162                                   (grasshopper_w128_t *) D_n,
163                                   (grasshopper_w128_t *) & newkey[n *
164                                                                   GRASSHOPPER_BLOCK_SIZE],
165                                   &c->buffer);
166     }
167     gost_grasshopper_cipher_key(c, newkey);
168 }
169
170 /* Set 256 bit  key into context */
171 static GRASSHOPPER_INLINE void
172 gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx * c, const uint8_t *k)
173 {
174     int i;
175     for (i = 0; i < 2; i++) {
176         grasshopper_copy128(&c->key.k.k[i],
177                             (const grasshopper_w128_t *)(k + i * 16));
178     }
179
180     grasshopper_set_encrypt_key(&c->encrypt_round_keys, &c->key);
181     grasshopper_set_decrypt_key(&c->decrypt_round_keys, &c->key);
182 }
183
184 /* Set master 256-bit key to be used in TLSTREE calculation into context */
185 static GRASSHOPPER_INLINE void
186 gost_grasshopper_master_key(gost_grasshopper_cipher_ctx * c, const uint8_t *k)
187 {
188     int i;
189     for (i = 0; i < 2; i++) {
190         grasshopper_copy128(&c->master_key.k.k[i],
191                             (const grasshopper_w128_t *)(k + i * 16));
192     }
193 }
194
195 /* Cleans up key from context */
196 static GRASSHOPPER_INLINE void
197 gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx * c)
198 {
199     int i;
200     for (i = 0; i < 2; i++) {
201         grasshopper_zero128(&c->key.k.k[i]);
202         grasshopper_zero128(&c->master_key.k.k[i]);
203     }
204     for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
205         grasshopper_zero128(&c->encrypt_round_keys.k[i]);
206     }
207     for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
208         grasshopper_zero128(&c->decrypt_round_keys.k[i]);
209     }
210     grasshopper_zero128(&c->buffer);
211 }
212
213 static GRASSHOPPER_INLINE void
214 gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx * c)
215 {
216     gost_grasshopper_cipher_ctx_ctr *ctx =
217         (gost_grasshopper_cipher_ctx_ctr *) c;
218
219     if (ctx->omac_ctx)
220         EVP_MD_CTX_free(ctx->omac_ctx);
221
222     grasshopper_zero128(&ctx->partial_buffer);
223 }
224
225 static int gost_grasshopper_cipher_init(EVP_CIPHER_CTX *ctx,
226                                  const unsigned char *key,
227                                  const unsigned char *iv, int enc)
228 {
229     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
230
231     if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
232         EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
233         if (enc && c->type == GRASSHOPPER_CIPHER_CTRACPKM) {
234             gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
235             if (init_zero_kdf_seed(ctr->kdf_seed) == 0)
236                 return -1;
237         }
238     }
239
240     if (key != NULL) {
241         gost_grasshopper_cipher_key(c, key);
242         gost_grasshopper_master_key(c, key);
243     }
244
245     if (iv != NULL) {
246         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
247                EVP_CIPHER_CTX_iv_length(ctx));
248     }
249
250     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
251            EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
252
253     grasshopper_zero128(&c->buffer);
254
255     return 1;
256 }
257
258 static GRASSHOPPER_INLINE int
259 gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX *ctx, const unsigned char
260                                  *key, const unsigned char
261                                  *iv, int enc)
262 {
263     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
264     c->type = GRASSHOPPER_CIPHER_ECB;
265     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
266 }
267
268 static GRASSHOPPER_INLINE int
269 gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char
270                                  *key, const unsigned char
271                                  *iv, int enc)
272 {
273     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
274     c->type = GRASSHOPPER_CIPHER_CBC;
275     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
276 }
277
278 static GRASSHOPPER_INLINE
279 int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX *ctx, const unsigned char
280                                      *key, const unsigned char
281                                      *iv, int enc)
282 {
283     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
284     c->type = GRASSHOPPER_CIPHER_OFB;
285     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
286 }
287
288 static GRASSHOPPER_INLINE int
289 gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX *ctx, const unsigned char
290                                  *key, const unsigned char
291                                  *iv, int enc)
292 {
293     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
294     c->type = GRASSHOPPER_CIPHER_CFB;
295     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
296 }
297
298 static GRASSHOPPER_INLINE int
299 gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX *ctx, const unsigned char
300                                  *key, const unsigned char
301                                  *iv, int enc)
302 {
303     gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
304
305     c->c.type = GRASSHOPPER_CIPHER_CTR;
306     EVP_CIPHER_CTX_set_num(ctx, 0);
307
308     grasshopper_zero128(&c->partial_buffer);
309
310     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
311 }
312
313 static GRASSHOPPER_INLINE int
314 gost_grasshopper_cipher_init_ctracpkm(EVP_CIPHER_CTX
315                                       *ctx, const unsigned
316                                       char *key, const unsigned
317                                       char *iv, int enc)
318 {
319     gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
320
321     /* NB: setting type makes EVP do_cipher callback useless */
322     c->c.type = GRASSHOPPER_CIPHER_CTRACPKM;
323     EVP_CIPHER_CTX_set_num(ctx, 0);
324     c->section_size = 4096;
325
326     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
327 }
328
329 static GRASSHOPPER_INLINE int
330 gost_grasshopper_cipher_init_ctracpkm_omac(EVP_CIPHER_CTX
331                                            *ctx, const unsigned
332                                            char *key, const unsigned
333                                            char *iv, int enc)
334 {
335     gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
336
337     /* NB: setting type makes EVP do_cipher callback useless */
338     c->c.type = GRASSHOPPER_CIPHER_CTRACPKMOMAC;
339     EVP_CIPHER_CTX_set_num(ctx, 0);
340     c->section_size = 4096;
341
342     if (key) {
343         unsigned char cipher_key[32];
344         c->omac_ctx = EVP_MD_CTX_new();
345
346         if (c->omac_ctx == NULL) {
347             GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_INIT_CTRACPKM_OMAC, ERR_R_MALLOC_FAILURE);
348             return 0;
349         }
350
351         if (gost2015_acpkm_omac_init(NID_kuznyechik_mac, enc, key,
352            c->omac_ctx, cipher_key, c->kdf_seed) != 1) {
353             EVP_MD_CTX_free(c->omac_ctx);
354             c->omac_ctx = NULL;
355             return 0;
356         }
357
358         return gost_grasshopper_cipher_init(ctx, cipher_key, iv, enc);
359     }
360
361     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
362 }
363
364 // TODO: const *in, const *c
365 void gost_grasshopper_encrypt_wrap(unsigned char *in, unsigned char *out,
366                    gost_grasshopper_cipher_ctx *c) {
367     grasshopper_encrypt_block(&c->encrypt_round_keys,
368                               (grasshopper_w128_t *) in,
369                               (grasshopper_w128_t *) out,
370                               &c->buffer);
371 }
372
373
374
375 /* ----------------------------------------------------------------------------------------------- */
376 /*! Функция реализует операцию умножения двух элементов конечного поля \f$ \mathbb F_{2^{128}}\f$,
377     порожденного неприводимым многочленом
378     \f$ f(x) = x^{128} + x^7 + x^2 + x + 1 \in \mathbb F_2[x]\f$. Для умножения используется
379     простейшая реализация, основанная на приведении по модулю после каждого шага алгоритма.        */
380 /* ----------------------------------------------------------------------------------------------- */
381 static void gf128_mul_uint64 (uint64_t *result, uint64_t *arg1, uint64_t *arg2)
382 {
383         int i = 0;
384         register uint64_t t, X0, X1;
385         uint64_t Z0 = 0, Z1 = 0;
386  
387 #ifdef L_ENDIAN
388         X0 = BSWAP64(*(arg1 + 1));
389         X1 = BSWAP64(*arg1);
390 #else
391         X0 = *(arg1 + 1);
392         X1 = *arg1;
393 #endif
394
395         //first 64 bits of arg1
396 #ifdef L_ENDIAN
397         t = BSWAP64(*(arg2 + 1));
398 #else
399         t = *(arg2 + 1);
400 #endif
401
402         for (i = 0; i < 64; i++) {
403                 if (t & 0x1) {
404                         Z0 ^= X0;
405                         Z1 ^= X1;
406                 }
407                 t >>= 1;
408                 if (X1 & 0x8000000000000000) {
409                         X1 <<= 1;
410                         X1 ^= X0>>63;
411                         X0 <<= 1;
412                         X0 ^= 0x87;
413                 }
414                 else {
415                         X1 <<= 1;
416                         X1 ^= X0>>63;
417                         X0 <<= 1;
418                 }
419         }
420
421         //second 64 bits of arg2
422 #ifdef L_ENDIAN
423         t = BSWAP64(*arg2);
424 #else
425         t = *arg2;
426 #endif
427
428         for (i = 0; i < 63; i++) {
429                 if (t & 0x1) {
430                         Z0 ^= X0;
431                         Z1 ^= X1;
432                 }
433                 t >>= 1;
434                 if (X1 & 0x8000000000000000) {
435                         X1 <<= 1;
436                         X1 ^= X0>>63;
437                         X0 <<= 1;
438                         X0 ^= 0x87;
439                 }
440                 else {
441                         X1 <<= 1;
442                         X1 ^= X0>>63;
443                         X0 <<= 1;
444                 }
445         }
446
447         if (t & 0x1) {
448                 Z0 ^= X0;
449                 Z1 ^= X1;
450         }
451
452 #ifdef L_ENDIAN
453         result[0] = BSWAP64(Z1);
454         result[1] = BSWAP64(Z0);
455 #else
456         result[0] = Z1;
457         result[1] = Z0;
458 #endif
459 }
460
461 static GRASSHOPPER_INLINE int
462 gost_grasshopper_cipher_init_mgm(EVP_CIPHER_CTX *ctx, const unsigned char *key,
463                                  const unsigned char *iv, int enc)
464 {
465     gost_mgm_ctx *mctx = 
466         (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
467     int bl;
468
469     if (!iv && !key)
470         return 1;
471     if (key) {
472         bl = EVP_CIPHER_CTX_iv_length(ctx);
473         gost_grasshopper_cipher_key(&mctx->ks.gh_ks, key);
474         gost_mgm128_init(&mctx->mgm, &mctx->ks, 
475                          (block128_f) gost_grasshopper_encrypt_wrap, gf128_mul_uint64, bl);
476         
477         /*
478          * If we have an iv can set it directly, otherwise use saved IV.
479          */
480         if (iv == NULL && mctx->iv_set)
481             iv = mctx->iv;
482         if (iv) {
483             if (gost_mgm128_setiv(&mctx->mgm, iv, mctx->ivlen) != 1)
484                 return 0;
485             mctx->iv_set = 1;
486         }
487         mctx->key_set = 1;
488     } else {
489         /* If key set use IV, otherwise copy */
490         if (mctx->key_set) {
491             if (gost_mgm128_setiv(&mctx->mgm, iv, mctx->ivlen) != 1)
492                 return 0;
493         }
494         else
495             memcpy(mctx->iv, iv, mctx->ivlen);
496         mctx->iv_set = 1;
497     }
498     return 1;
499 }
500
501 static int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
502                                           const unsigned char *in, size_t inl)
503 {
504     gost_grasshopper_cipher_ctx *c =
505         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
506     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
507     const unsigned char *current_in = in;
508     unsigned char *current_out = out;
509     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
510     size_t i;
511
512     for (i = 0; i < blocks;
513          i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
514          GRASSHOPPER_BLOCK_SIZE) {
515         if (encrypting) {
516             grasshopper_encrypt_block(&c->encrypt_round_keys,
517                                       (grasshopper_w128_t *) current_in,
518                                       (grasshopper_w128_t *) current_out,
519                                       &c->buffer);
520         } else {
521             grasshopper_decrypt_block(&c->decrypt_round_keys,
522                                       (grasshopper_w128_t *) current_in,
523                                       (grasshopper_w128_t *) current_out,
524                                       &c->buffer);
525         }
526     }
527
528     return 1;
529 }
530
531 static int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
532                                           const unsigned char *in, size_t inl)
533 {
534     gost_grasshopper_cipher_ctx *c =
535         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
536     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
537     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
538     const unsigned char *current_in = in;
539     unsigned char *current_out = out;
540     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
541     size_t i;
542     grasshopper_w128_t *currentBlock;
543
544     currentBlock = (grasshopper_w128_t *) iv;
545
546     for (i = 0; i < blocks;
547          i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
548          GRASSHOPPER_BLOCK_SIZE) {
549         grasshopper_w128_t *currentInputBlock = (grasshopper_w128_t *) current_in;
550         grasshopper_w128_t *currentOutputBlock = (grasshopper_w128_t *) current_out;
551         if (encrypting) {
552             grasshopper_append128(currentBlock, currentInputBlock);
553             grasshopper_encrypt_block(&c->encrypt_round_keys, currentBlock,
554                                       currentOutputBlock, &c->buffer);
555             grasshopper_copy128(currentBlock, currentOutputBlock);
556         } else {
557             grasshopper_w128_t tmp;
558
559             grasshopper_copy128(&tmp, currentInputBlock);
560             grasshopper_decrypt_block(&c->decrypt_round_keys,
561                                       currentInputBlock, currentOutputBlock,
562                                       &c->buffer);
563             grasshopper_append128(currentOutputBlock, currentBlock);
564             grasshopper_copy128(currentBlock, &tmp);
565         }
566     }
567
568     return 1;
569 }
570
571 void inc_counter(unsigned char *counter, size_t counter_bytes)
572 {
573     unsigned int n = counter_bytes;
574
575     do {
576         unsigned char c;
577         --n;
578         c = counter[n];
579         ++c;
580         counter[n] = c;
581         if (c)
582             return;
583     } while (n);
584 }
585
586 /* increment counter (128-bit int) by 1 */
587 static void ctr128_inc(unsigned char *counter)
588 {
589     inc_counter(counter, 16);
590 }
591
592 static int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
593                                           const unsigned char *in, size_t inl)
594 {
595     gost_grasshopper_cipher_ctx_ctr *c = (gost_grasshopper_cipher_ctx_ctr *)
596         EVP_CIPHER_CTX_get_cipher_data(ctx);
597     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
598     const unsigned char *current_in = in;
599     unsigned char *current_out = out;
600     grasshopper_w128_t *currentInputBlock;
601     grasshopper_w128_t *currentOutputBlock;
602     unsigned int n = EVP_CIPHER_CTX_num(ctx);
603     size_t lasted = inl;
604     size_t i;
605     size_t blocks;
606     grasshopper_w128_t *iv_buffer;
607     grasshopper_w128_t tmp;
608
609     while (n && lasted) {
610         *(current_out++) = *(current_in++) ^ c->partial_buffer.b[n];
611         --lasted;
612         n = (n + 1) % GRASSHOPPER_BLOCK_SIZE;
613     }
614     EVP_CIPHER_CTX_set_num(ctx, n);
615     blocks = lasted / GRASSHOPPER_BLOCK_SIZE;
616
617     iv_buffer = (grasshopper_w128_t *) iv;
618
619     // full parts
620     for (i = 0; i < blocks; i++) {
621         currentInputBlock = (grasshopper_w128_t *) current_in;
622         currentOutputBlock = (grasshopper_w128_t *) current_out;
623         grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
624                                   &c->partial_buffer, &c->c.buffer);
625         grasshopper_plus128(&tmp, &c->partial_buffer, currentInputBlock);
626         grasshopper_copy128(currentOutputBlock, &tmp);
627         ctr128_inc(iv_buffer->b);
628         current_in += GRASSHOPPER_BLOCK_SIZE;
629         current_out += GRASSHOPPER_BLOCK_SIZE;
630         lasted -= GRASSHOPPER_BLOCK_SIZE;
631     }
632
633     if (lasted > 0) {
634         currentInputBlock = (grasshopper_w128_t *) current_in;
635         currentOutputBlock = (grasshopper_w128_t *) current_out;
636         grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
637                                   &c->partial_buffer, &c->c.buffer);
638         for (i = 0; i < lasted; i++) {
639             currentOutputBlock->b[i] =
640                 c->partial_buffer.b[i] ^ currentInputBlock->b[i];
641         }
642         EVP_CIPHER_CTX_set_num(ctx, i);
643         ctr128_inc(iv_buffer->b);
644     }
645
646     return inl;
647 }
648
649 #define GRASSHOPPER_BLOCK_MASK (GRASSHOPPER_BLOCK_SIZE - 1)
650 static inline void apply_acpkm_grasshopper(gost_grasshopper_cipher_ctx_ctr *
651                                            ctx, unsigned int *num)
652 {
653     if (!ctx->section_size || (*num < ctx->section_size))
654         return;
655     acpkm_next(&ctx->c);
656     *num &= GRASSHOPPER_BLOCK_MASK;
657 }
658
659 /* If meshing is not configured via ctrl (setting section_size)
660  * this function works exactly like plain ctr */
661 static int gost_grasshopper_cipher_do_ctracpkm(EVP_CIPHER_CTX *ctx,
662                                                unsigned char *out,
663                                                const unsigned char *in,
664                                                size_t inl)
665 {
666     gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
667     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
668     unsigned int num = EVP_CIPHER_CTX_num(ctx);
669     size_t blocks, i, lasted = inl;
670     grasshopper_w128_t tmp;
671
672     while ((num & GRASSHOPPER_BLOCK_MASK) && lasted) {
673         *out++ = *in++ ^ c->partial_buffer.b[num & GRASSHOPPER_BLOCK_MASK];
674         --lasted;
675         num++;
676     }
677     blocks = lasted / GRASSHOPPER_BLOCK_SIZE;
678
679     // full parts
680     for (i = 0; i < blocks; i++) {
681         apply_acpkm_grasshopper(c, &num);
682         grasshopper_encrypt_block(&c->c.encrypt_round_keys,
683                                   (grasshopper_w128_t *) iv,
684                                   (grasshopper_w128_t *) & c->partial_buffer,
685                                   &c->c.buffer);
686         grasshopper_plus128(&tmp, &c->partial_buffer,
687                             (grasshopper_w128_t *) in);
688         grasshopper_copy128((grasshopper_w128_t *) out, &tmp);
689         ctr128_inc(iv);
690         in += GRASSHOPPER_BLOCK_SIZE;
691         out += GRASSHOPPER_BLOCK_SIZE;
692         num += GRASSHOPPER_BLOCK_SIZE;
693         lasted -= GRASSHOPPER_BLOCK_SIZE;
694     }
695
696     // last part
697     if (lasted > 0) {
698         apply_acpkm_grasshopper(c, &num);
699         grasshopper_encrypt_block(&c->c.encrypt_round_keys,
700                                   (grasshopper_w128_t *) iv,
701                                   &c->partial_buffer, &c->c.buffer);
702         for (i = 0; i < lasted; i++)
703             out[i] = c->partial_buffer.b[i] ^ in[i];
704         ctr128_inc(iv);
705         num += lasted;
706     }
707     EVP_CIPHER_CTX_set_num(ctx, num);
708
709     return inl;
710 }
711
712 static int gost_grasshopper_cipher_do_ctracpkm_omac(EVP_CIPHER_CTX *ctx,
713                                                     unsigned char *out,
714                                                     const unsigned char *in,
715                                                     size_t inl)
716 {
717     int result;
718     gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
719     /* As in and out can be the same pointer, process unencrypted here */
720     if (EVP_CIPHER_CTX_encrypting(ctx))
721         EVP_DigestSignUpdate(c->omac_ctx, in, inl);
722
723     if (in == NULL && inl == 0) { /* Final call */
724         return gost2015_final_call(ctx, c->omac_ctx, KUZNYECHIK_MAC_MAX_SIZE, c->tag, gost_grasshopper_cipher_do_ctracpkm);
725     }
726
727     if (in == NULL) {
728         GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_DO_CTRACPKM_OMAC, ERR_R_EVP_LIB);
729         return -1;
730     }
731     result = gost_grasshopper_cipher_do_ctracpkm(ctx, out, in, inl);
732
733     /* As in and out can be the same pointer, process decrypted here */
734     if (!EVP_CIPHER_CTX_encrypting(ctx))
735         EVP_DigestSignUpdate(c->omac_ctx, out, inl);
736
737     return result;
738 }
739
740
741
742 static int gost_grasshopper_cipher_do_mgm(EVP_CIPHER_CTX *ctx, unsigned char *out,
743                                    const unsigned char *in, size_t len)
744 {
745     gost_mgm_ctx *mctx = 
746         (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
747     int enc = EVP_CIPHER_CTX_encrypting(ctx);
748
749     /* If not set up, return error */
750     if (!mctx->key_set) {
751         GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_DO_MGM,
752                 GOST_R_BAD_ORDER);
753         return -1;
754     }
755
756     if (!mctx->iv_set) {
757         GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_DO_MGM,
758                 GOST_R_BAD_ORDER);
759         return -1;
760     }
761     if (in) {
762         if (out == NULL) {
763             if (gost_mgm128_aad(&mctx->mgm, in, len))
764                 return -1;            
765         } else if (enc) {
766             if (gost_mgm128_encrypt(&mctx->mgm, in, out, len))
767                 return -1;
768         } else {
769             if (gost_mgm128_decrypt(&mctx->mgm, in, out, len))
770                 return -1;
771         }
772         return len;
773     } else {
774         if (!enc) {
775             if (mctx->taglen < 0)
776                 return -1;
777             if (gost_mgm128_finish(&mctx->mgm,
778                                    EVP_CIPHER_CTX_buf_noconst(ctx),
779                                    mctx->taglen) != 0)
780                 return -1;
781             mctx->iv_set = 0;
782             return 0;
783         }
784         gost_mgm128_tag(&mctx->mgm, EVP_CIPHER_CTX_buf_noconst(ctx), 16);
785         mctx->taglen = 16;
786         /* Don't reuse the IV */
787         mctx->iv_set = 0;
788         return 0;
789     }
790
791 }
792
793 /*
794  * Fixed 128-bit IV implementation make shift regiser redundant.
795  */
796 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx * ctx,
797                                       grasshopper_w128_t * iv,
798                                       grasshopper_w128_t * buf)
799 {
800     grasshopper_w128_t tmp;
801     memcpy(&tmp, iv, 16);
802     grasshopper_encrypt_block(&ctx->encrypt_round_keys, &tmp,
803                               buf, &ctx->buffer);
804     memcpy(iv, buf, 16);
805 }
806
807 static int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX *ctx, unsigned char *out,
808                                           const unsigned char *in, size_t inl)
809 {
810     gost_grasshopper_cipher_ctx *c = (gost_grasshopper_cipher_ctx *)
811         EVP_CIPHER_CTX_get_cipher_data(ctx);
812     const unsigned char *in_ptr = in;
813     unsigned char *out_ptr = out;
814     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
815     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
816     int num = EVP_CIPHER_CTX_num(ctx);
817     size_t i = 0;
818     size_t j;
819
820     /* process partial block if any */
821     if (num > 0) {
822         for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
823              j++, i++, in_ptr++, out_ptr++) {
824             *out_ptr = buf[j] ^ (*in_ptr);
825         }
826         if (j == GRASSHOPPER_BLOCK_SIZE) {
827             EVP_CIPHER_CTX_set_num(ctx, 0);
828         } else {
829             EVP_CIPHER_CTX_set_num(ctx, (int)j);
830             return 1;
831         }
832     }
833
834     for (; i + GRASSHOPPER_BLOCK_SIZE <
835          inl;
836          i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
837          GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
838         /*
839          * block cipher current iv
840          */
841         /* Encrypt */
842         gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
843                                   (grasshopper_w128_t *) buf);
844
845         /*
846          * xor next block of input text with it and output it
847          */
848         /*
849          * output this block
850          */
851         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
852             out_ptr[j] = buf[j] ^ in_ptr[j];
853         }
854     }
855
856     /* Process rest of buffer */
857     if (i < inl) {
858         gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
859                                   (grasshopper_w128_t *) buf);
860         for (j = 0; i < inl; j++, i++) {
861             out_ptr[j] = buf[j] ^ in_ptr[j];
862         }
863         EVP_CIPHER_CTX_set_num(ctx, (int)j);
864     } else {
865         EVP_CIPHER_CTX_set_num(ctx, 0);
866     }
867
868     return 1;
869 }
870
871 static int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
872                                           const unsigned char *in, size_t inl)
873 {
874     gost_grasshopper_cipher_ctx *c =
875         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
876     const unsigned char *in_ptr = in;
877     unsigned char *out_ptr = out;
878     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
879     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
880     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
881     int num = EVP_CIPHER_CTX_num(ctx);
882     size_t i = 0;
883     size_t j = 0;
884
885     /* process partial block if any */
886     if (num > 0) {
887         for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
888              j++, i++, in_ptr++, out_ptr++) {
889             if (!encrypting) {
890                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
891             }
892             *out_ptr = buf[j] ^ (*in_ptr);
893             if (encrypting) {
894                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
895             }
896         }
897         if (j == GRASSHOPPER_BLOCK_SIZE) {
898             memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE, GRASSHOPPER_BLOCK_SIZE);
899             EVP_CIPHER_CTX_set_num(ctx, 0);
900         } else {
901             EVP_CIPHER_CTX_set_num(ctx, (int)j);
902             return 1;
903         }
904     }
905
906     for (; i + GRASSHOPPER_BLOCK_SIZE <
907          inl;
908          i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
909          GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
910         /*
911          * block cipher current iv
912          */
913         grasshopper_encrypt_block(&c->encrypt_round_keys,
914                                   (grasshopper_w128_t *) iv,
915                                   (grasshopper_w128_t *) buf, &c->buffer);
916         /*
917          * xor next block of input text with it and output it
918          */
919         /*
920          * output this block
921          */
922         if (!encrypting) {
923             memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
924         }
925         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
926             out_ptr[j] = buf[j] ^ in_ptr[j];
927         }
928         /* Encrypt */
929         /* Next iv is next block of cipher text */
930         if (encrypting) {
931             memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
932         }
933     }
934
935     /* Process rest of buffer */
936     if (i < inl) {
937         grasshopper_encrypt_block(&c->encrypt_round_keys,
938                                   (grasshopper_w128_t *) iv,
939                                   (grasshopper_w128_t *) buf, &c->buffer);
940         if (!encrypting) {
941             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
942         }
943         for (j = 0; i < inl; j++, i++) {
944             out_ptr[j] = buf[j] ^ in_ptr[j];
945         }
946         EVP_CIPHER_CTX_set_num(ctx, (int)j);
947         if (encrypting) {
948             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
949         }
950     } else {
951         EVP_CIPHER_CTX_set_num(ctx, 0);
952     }
953
954     return 1;
955 }
956
957 static int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX *ctx)
958 {
959     gost_grasshopper_cipher_ctx *c =
960         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
961
962     if (!c)
963         return 1;
964
965     if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE)
966         gost_grasshopper_cipher_destroy_ctr(c);
967
968     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
969
970     return 1;
971 }
972
973 static int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
974 {
975     if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE) {
976         gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
977
978         /* CMS implies 256kb section_size */
979         ctr->section_size = 256*1024;
980
981         return gost2015_set_asn1_params(params,
982                EVP_CIPHER_CTX_original_iv(ctx), 8, ctr->kdf_seed);
983     }
984     return 0;
985 }
986
987 static GRASSHOPPER_INLINE int
988 gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
989 {
990     if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE) {
991         gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
992
993         int iv_len = 16;
994         unsigned char iv[16];
995
996         if (gost2015_get_asn1_params(params, 16, iv, 8, ctr->kdf_seed) == 0) {
997             return 0;
998         }
999
1000         memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, iv_len);
1001         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, iv_len);
1002
1003         /* CMS implies 256kb section_size */
1004         ctr->section_size = 256*1024;
1005         return 1;
1006     }
1007     return 0;
1008 }
1009
1010 static int gost_grasshopper_mgm_cleanup(EVP_CIPHER_CTX *c)
1011 {
1012     gost_mgm_ctx *mctx = 
1013         (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(c);
1014     if (mctx == NULL)
1015         return 0;
1016     gost_grasshopper_cipher_destroy(&mctx->ks.gh_ks);
1017     OPENSSL_cleanse(&mctx->mgm, sizeof(mctx->mgm));
1018     if (mctx->iv != EVP_CIPHER_CTX_iv_noconst(c))
1019         OPENSSL_free(mctx->iv);
1020     return 1;
1021 }
1022
1023 static int gost_grasshopper_mgm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1024 {
1025     gost_mgm_ctx *mctx = 
1026         (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(c);
1027     unsigned char *buf, *iv;
1028     int ivlen, enc;
1029
1030     switch (type) {
1031     case EVP_CTRL_INIT:
1032         ivlen = EVP_CIPHER_iv_length(EVP_CIPHER_CTX_cipher(c));
1033         iv = EVP_CIPHER_CTX_iv_noconst(c);
1034         mctx->key_set = 0;
1035         mctx->iv_set = 0;
1036         mctx->ivlen = ivlen;
1037         mctx->iv = iv;
1038         mctx->taglen = -1;
1039         return 1;
1040     
1041     case EVP_CTRL_GET_IVLEN:
1042         *(int *)ptr = mctx->ivlen;
1043         return 1;
1044
1045     case EVP_CTRL_AEAD_SET_IVLEN:
1046         if (arg <= 0)
1047             return 0;
1048         if ((arg > EVP_MAX_IV_LENGTH) && (arg > mctx->ivlen)) {
1049             // TODO: Allocate memory for IV or set error
1050             return 0;
1051         }
1052         mctx->ivlen = arg;
1053         return 1;
1054
1055     case EVP_CTRL_AEAD_SET_TAG:
1056         buf = EVP_CIPHER_CTX_buf_noconst(c);
1057         enc = EVP_CIPHER_CTX_encrypting(c);
1058         if (arg <= 0 || arg != 16 || enc) {
1059             GOSTerr(GOST_F_GOST_GRASSHOPPER_MGM_CTRL,
1060                     GOST_R_INVALID_TAG_LENGTH);
1061             return 0;
1062         }
1063         memcpy(buf, ptr, arg);
1064         mctx->taglen = arg;    
1065         return 1;
1066
1067     case EVP_CTRL_AEAD_GET_TAG:
1068         buf = EVP_CIPHER_CTX_buf_noconst(c);
1069         enc = EVP_CIPHER_CTX_encrypting(c);
1070         if (arg <= 0 || arg > 16 || !enc || mctx->taglen < 0) {
1071             GOSTerr(GOST_F_GOST_GRASSHOPPER_MGM_CTRL,
1072                     GOST_R_INVALID_TAG_LENGTH);
1073             return 0;
1074         }
1075         memcpy(ptr, buf, arg);
1076         return 1;
1077
1078     default:
1079         return -1;
1080     }
1081 }
1082
1083 static int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1084 {
1085     switch (type) {
1086     case EVP_CTRL_RAND_KEY:{
1087             if (RAND_priv_bytes
1088                 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
1089                 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR);
1090                 return -1;
1091             }
1092             break;
1093         }
1094     case EVP_CTRL_KEY_MESH:{
1095             gost_grasshopper_cipher_ctx_ctr *c =
1096                 EVP_CIPHER_CTX_get_cipher_data(ctx);
1097             if ((c->c.type != GRASSHOPPER_CIPHER_CTRACPKM &&
1098                 c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
1099                 || (arg == 0)
1100                || (arg % GRASSHOPPER_BLOCK_SIZE))
1101                 return -1;
1102             c->section_size = arg;
1103             break;
1104         }
1105 #ifdef EVP_CTRL_TLS1_2_TLSTREE
1106     case EVP_CTRL_TLS1_2_TLSTREE:
1107         {
1108           unsigned char newkey[32];
1109           int mode = EVP_CIPHER_CTX_mode(ctx);
1110           static const unsigned char zeroseq[8];
1111           gost_grasshopper_cipher_ctx_ctr *ctr_ctx = NULL;
1112           gost_grasshopper_cipher_ctx *c = NULL;
1113
1114           unsigned char adjusted_iv[16];
1115           unsigned char seq[8];
1116           int j, carry;
1117           if (mode != EVP_CIPH_CTR_MODE)
1118             return -1;
1119
1120           ctr_ctx = (gost_grasshopper_cipher_ctx_ctr *)
1121             EVP_CIPHER_CTX_get_cipher_data(ctx);
1122           c = &(ctr_ctx->c);
1123
1124           memcpy(seq, ptr, 8);
1125           if (EVP_CIPHER_CTX_encrypting(ctx)) {
1126             /*
1127              * OpenSSL increments seq after mac calculation.
1128              * As we have Mac-Then-Encrypt, we need decrement it here on encryption
1129              * to derive the key correctly.
1130              * */
1131             if (memcmp(seq, zeroseq, 8) != 0)
1132             {
1133               for(j=7; j>=0; j--)
1134               {
1135                 if (seq[j] != 0) {seq[j]--; break;}
1136                 else seq[j]  = 0xFF;
1137               }
1138             }
1139           }
1140           if (gost_tlstree(NID_grasshopper_cbc, c->master_key.k.b, newkey,
1141                 (const unsigned char *)seq) > 0) {
1142             memset(adjusted_iv, 0, 16);
1143             memcpy(adjusted_iv, EVP_CIPHER_CTX_original_iv(ctx), 8);
1144             for(j=7,carry=0; j>=0; j--)
1145             {
1146               int adj_byte = adjusted_iv[j]+seq[j]+carry;
1147               carry = (adj_byte > 255) ? 1 : 0;
1148               adjusted_iv[j] = adj_byte & 0xFF;
1149             }
1150             EVP_CIPHER_CTX_set_num(ctx, 0);
1151             memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), adjusted_iv, 16);
1152
1153             gost_grasshopper_cipher_key(c, newkey);
1154             return 1;
1155           }
1156         }
1157         return -1;
1158 #endif
1159 #if 0
1160     case EVP_CTRL_AEAD_GET_TAG:
1161     case EVP_CTRL_AEAD_SET_TAG:
1162         {
1163             int taglen = arg;
1164             unsigned char *tag = ptr;
1165
1166             gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1167             if (c->c.type != GRASSHOPPER_CIPHER_MGM)
1168                 return -1;
1169
1170             if (taglen > KUZNYECHIK_MAC_MAX_SIZE) {
1171                 CRYPTOCOMerr(CRYPTOCOM_F_GOST_GRASSHOPPER_CIPHER_CTL,
1172                         CRYPTOCOM_R_INVALID_TAG_LENGTH);
1173                 return -1;
1174             }
1175
1176             if (type == EVP_CTRL_AEAD_GET_TAG)
1177                 memcpy(tag, c->final_tag, taglen);
1178             else
1179                 memcpy(c->final_tag, tag, taglen);
1180
1181             return 1;
1182         }
1183 #endif
1184     case EVP_CTRL_PROCESS_UNPROTECTED:
1185     {
1186       STACK_OF(X509_ATTRIBUTE) *x = ptr;
1187       gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1188
1189       if (c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
1190         return -1;
1191
1192       return gost2015_process_unprotected_attributes(x, arg, KUZNYECHIK_MAC_MAX_SIZE, c->tag);
1193     }
1194     return 1;
1195     case EVP_CTRL_COPY: {
1196         EVP_CIPHER_CTX *out = ptr;
1197
1198         gost_grasshopper_cipher_ctx_ctr *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
1199         gost_grasshopper_cipher_ctx_ctr *in_cctx  = EVP_CIPHER_CTX_get_cipher_data(ctx);
1200
1201         if (in_cctx->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
1202             return -1;
1203
1204         if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
1205             out_cctx->omac_ctx = EVP_MD_CTX_new();
1206             if (out_cctx->omac_ctx == NULL) {
1207                 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, ERR_R_MALLOC_FAILURE);
1208                 return -1;
1209             }
1210         }
1211         return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
1212     }
1213     default:
1214         GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL,
1215                 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
1216         return -1;
1217     }
1218     return 1;
1219 }
1220
1221 /* Called directly by CMAC_ACPKM_Init() */
1222 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctracpkm()
1223 {
1224     return GOST_init_cipher(&grasshopper_ctr_acpkm_cipher);
1225 }
1226 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */