]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_grasshopper_cipher.c
Delete .travis.yml
[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_grasshopper_master_key(&mctx->ks.gh_ks, key);
475         gost_mgm128_init(&mctx->mgm, &mctx->ks, 
476                          (block128_f) gost_grasshopper_encrypt_wrap, gf128_mul_uint64, bl);
477         
478         /*
479          * If we have an iv can set it directly, otherwise use saved IV.
480          */
481         if (iv == NULL && mctx->iv_set)
482             iv = mctx->iv;
483         if (iv) {
484             if (gost_mgm128_setiv(&mctx->mgm, iv, mctx->ivlen) != 1)
485                 return 0;
486             mctx->iv_set = 1;
487         }
488         mctx->key_set = 1;
489     } else {
490         /* If key set use IV, otherwise copy */
491         if (mctx->key_set) {
492             if (gost_mgm128_setiv(&mctx->mgm, iv, mctx->ivlen) != 1)
493                 return 0;
494         }
495         else
496             memcpy(mctx->iv, iv, mctx->ivlen);
497         mctx->iv_set = 1;
498     }
499     return 1;
500 }
501
502 static int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
503                                           const unsigned char *in, size_t inl)
504 {
505     gost_grasshopper_cipher_ctx *c =
506         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
507     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
508     const unsigned char *current_in = in;
509     unsigned char *current_out = out;
510     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
511     size_t i;
512
513     for (i = 0; i < blocks;
514          i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
515          GRASSHOPPER_BLOCK_SIZE) {
516         if (encrypting) {
517             grasshopper_encrypt_block(&c->encrypt_round_keys,
518                                       (grasshopper_w128_t *) current_in,
519                                       (grasshopper_w128_t *) current_out,
520                                       &c->buffer);
521         } else {
522             grasshopper_decrypt_block(&c->decrypt_round_keys,
523                                       (grasshopper_w128_t *) current_in,
524                                       (grasshopper_w128_t *) current_out,
525                                       &c->buffer);
526         }
527     }
528
529     return 1;
530 }
531
532 static int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
533                                           const unsigned char *in, size_t inl)
534 {
535     gost_grasshopper_cipher_ctx *c =
536         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
537     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
538     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
539     const unsigned char *current_in = in;
540     unsigned char *current_out = out;
541     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
542     size_t i;
543     grasshopper_w128_t *currentBlock;
544
545     currentBlock = (grasshopper_w128_t *) iv;
546
547     for (i = 0; i < blocks;
548          i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
549          GRASSHOPPER_BLOCK_SIZE) {
550         grasshopper_w128_t *currentInputBlock = (grasshopper_w128_t *) current_in;
551         grasshopper_w128_t *currentOutputBlock = (grasshopper_w128_t *) current_out;
552         if (encrypting) {
553             grasshopper_append128(currentBlock, currentInputBlock);
554             grasshopper_encrypt_block(&c->encrypt_round_keys, currentBlock,
555                                       currentOutputBlock, &c->buffer);
556             grasshopper_copy128(currentBlock, currentOutputBlock);
557         } else {
558             grasshopper_w128_t tmp;
559
560             grasshopper_copy128(&tmp, currentInputBlock);
561             grasshopper_decrypt_block(&c->decrypt_round_keys,
562                                       currentInputBlock, currentOutputBlock,
563                                       &c->buffer);
564             grasshopper_append128(currentOutputBlock, currentBlock);
565             grasshopper_copy128(currentBlock, &tmp);
566         }
567     }
568
569     return 1;
570 }
571
572 void inc_counter(unsigned char *counter, size_t counter_bytes)
573 {
574     unsigned int n = counter_bytes;
575
576     do {
577         unsigned char c;
578         --n;
579         c = counter[n];
580         ++c;
581         counter[n] = c;
582         if (c)
583             return;
584     } while (n);
585 }
586
587 /* increment counter (128-bit int) by 1 */
588 static void ctr128_inc(unsigned char *counter)
589 {
590     inc_counter(counter, 16);
591 }
592
593 static int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
594                                           const unsigned char *in, size_t inl)
595 {
596     gost_grasshopper_cipher_ctx_ctr *c = (gost_grasshopper_cipher_ctx_ctr *)
597         EVP_CIPHER_CTX_get_cipher_data(ctx);
598     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
599     const unsigned char *current_in = in;
600     unsigned char *current_out = out;
601     grasshopper_w128_t *currentInputBlock;
602     grasshopper_w128_t *currentOutputBlock;
603     unsigned int n = EVP_CIPHER_CTX_num(ctx);
604     size_t lasted = inl;
605     size_t i;
606     size_t blocks;
607     grasshopper_w128_t *iv_buffer;
608     grasshopper_w128_t tmp;
609
610     while (n && lasted) {
611         *(current_out++) = *(current_in++) ^ c->partial_buffer.b[n];
612         --lasted;
613         n = (n + 1) % GRASSHOPPER_BLOCK_SIZE;
614     }
615     EVP_CIPHER_CTX_set_num(ctx, n);
616     blocks = lasted / GRASSHOPPER_BLOCK_SIZE;
617
618     iv_buffer = (grasshopper_w128_t *) iv;
619
620     // full parts
621     for (i = 0; i < blocks; i++) {
622         currentInputBlock = (grasshopper_w128_t *) current_in;
623         currentOutputBlock = (grasshopper_w128_t *) current_out;
624         grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
625                                   &c->partial_buffer, &c->c.buffer);
626         grasshopper_plus128(&tmp, &c->partial_buffer, currentInputBlock);
627         grasshopper_copy128(currentOutputBlock, &tmp);
628         ctr128_inc(iv_buffer->b);
629         current_in += GRASSHOPPER_BLOCK_SIZE;
630         current_out += GRASSHOPPER_BLOCK_SIZE;
631         lasted -= GRASSHOPPER_BLOCK_SIZE;
632     }
633
634     if (lasted > 0) {
635         currentInputBlock = (grasshopper_w128_t *) current_in;
636         currentOutputBlock = (grasshopper_w128_t *) current_out;
637         grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
638                                   &c->partial_buffer, &c->c.buffer);
639         for (i = 0; i < lasted; i++) {
640             currentOutputBlock->b[i] =
641                 c->partial_buffer.b[i] ^ currentInputBlock->b[i];
642         }
643         EVP_CIPHER_CTX_set_num(ctx, i);
644         ctr128_inc(iv_buffer->b);
645     }
646
647     return inl;
648 }
649
650 #define GRASSHOPPER_BLOCK_MASK (GRASSHOPPER_BLOCK_SIZE - 1)
651 static inline void apply_acpkm_grasshopper(gost_grasshopper_cipher_ctx_ctr *
652                                            ctx, unsigned int *num)
653 {
654     if (!ctx->section_size || (*num < ctx->section_size))
655         return;
656     acpkm_next(&ctx->c);
657     *num &= GRASSHOPPER_BLOCK_MASK;
658 }
659
660 /* If meshing is not configured via ctrl (setting section_size)
661  * this function works exactly like plain ctr */
662 static int gost_grasshopper_cipher_do_ctracpkm(EVP_CIPHER_CTX *ctx,
663                                                unsigned char *out,
664                                                const unsigned char *in,
665                                                size_t inl)
666 {
667     gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
668     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
669     unsigned int num = EVP_CIPHER_CTX_num(ctx);
670     size_t blocks, i, lasted = inl;
671     grasshopper_w128_t tmp;
672
673     while ((num & GRASSHOPPER_BLOCK_MASK) && lasted) {
674         *out++ = *in++ ^ c->partial_buffer.b[num & GRASSHOPPER_BLOCK_MASK];
675         --lasted;
676         num++;
677     }
678     blocks = lasted / GRASSHOPPER_BLOCK_SIZE;
679
680     // full parts
681     for (i = 0; i < blocks; i++) {
682         apply_acpkm_grasshopper(c, &num);
683         grasshopper_encrypt_block(&c->c.encrypt_round_keys,
684                                   (grasshopper_w128_t *) iv,
685                                   (grasshopper_w128_t *) & c->partial_buffer,
686                                   &c->c.buffer);
687         grasshopper_plus128(&tmp, &c->partial_buffer,
688                             (grasshopper_w128_t *) in);
689         grasshopper_copy128((grasshopper_w128_t *) out, &tmp);
690         ctr128_inc(iv);
691         in += GRASSHOPPER_BLOCK_SIZE;
692         out += GRASSHOPPER_BLOCK_SIZE;
693         num += GRASSHOPPER_BLOCK_SIZE;
694         lasted -= GRASSHOPPER_BLOCK_SIZE;
695     }
696
697     // last part
698     if (lasted > 0) {
699         apply_acpkm_grasshopper(c, &num);
700         grasshopper_encrypt_block(&c->c.encrypt_round_keys,
701                                   (grasshopper_w128_t *) iv,
702                                   &c->partial_buffer, &c->c.buffer);
703         for (i = 0; i < lasted; i++)
704             out[i] = c->partial_buffer.b[i] ^ in[i];
705         ctr128_inc(iv);
706         num += lasted;
707     }
708     EVP_CIPHER_CTX_set_num(ctx, num);
709
710     return inl;
711 }
712
713 static int gost_grasshopper_cipher_do_ctracpkm_omac(EVP_CIPHER_CTX *ctx,
714                                                     unsigned char *out,
715                                                     const unsigned char *in,
716                                                     size_t inl)
717 {
718     int result;
719     gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
720     /* As in and out can be the same pointer, process unencrypted here */
721     if (EVP_CIPHER_CTX_encrypting(ctx))
722         EVP_DigestSignUpdate(c->omac_ctx, in, inl);
723
724     if (in == NULL && inl == 0) { /* Final call */
725         return gost2015_final_call(ctx, c->omac_ctx, KUZNYECHIK_MAC_MAX_SIZE, c->tag, gost_grasshopper_cipher_do_ctracpkm);
726     }
727
728     if (in == NULL) {
729         GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_DO_CTRACPKM_OMAC, ERR_R_EVP_LIB);
730         return -1;
731     }
732     result = gost_grasshopper_cipher_do_ctracpkm(ctx, out, in, inl);
733
734     /* As in and out can be the same pointer, process decrypted here */
735     if (!EVP_CIPHER_CTX_encrypting(ctx))
736         EVP_DigestSignUpdate(c->omac_ctx, out, inl);
737
738     return result;
739 }
740
741
742
743 static int gost_grasshopper_cipher_do_mgm(EVP_CIPHER_CTX *ctx, unsigned char *out,
744                                    const unsigned char *in, size_t len)
745 {
746     gost_mgm_ctx *mctx = 
747         (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
748     int enc = EVP_CIPHER_CTX_encrypting(ctx);
749
750     /* If not set up, return error */
751     if (!mctx->key_set) {
752         GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_DO_MGM,
753                 GOST_R_BAD_ORDER);
754         return -1;
755     }
756
757     if (!mctx->iv_set) {
758         GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_DO_MGM,
759                 GOST_R_BAD_ORDER);
760         return -1;
761     }
762     if (in) {
763         if (out == NULL) {
764             if (gost_mgm128_aad(&mctx->mgm, in, len))
765                 return -1;            
766         } else if (enc) {
767             if (gost_mgm128_encrypt(&mctx->mgm, in, out, len))
768                 return -1;
769         } else {
770             if (gost_mgm128_decrypt(&mctx->mgm, in, out, len))
771                 return -1;
772         }
773         return len;
774     } else {
775         if (!enc) {
776             if (mctx->taglen < 0)
777                 return -1;
778             if (gost_mgm128_finish(&mctx->mgm,
779                                    EVP_CIPHER_CTX_buf_noconst(ctx),
780                                    mctx->taglen) != 0)
781                 return -1;
782             mctx->iv_set = 0;
783             return 0;
784         }
785         gost_mgm128_tag(&mctx->mgm, EVP_CIPHER_CTX_buf_noconst(ctx), 16);
786         mctx->taglen = 16;
787         /* Don't reuse the IV */
788         mctx->iv_set = 0;
789         return 0;
790     }
791
792 }
793
794 /*
795  * Fixed 128-bit IV implementation make shift regiser redundant.
796  */
797 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx * ctx,
798                                       grasshopper_w128_t * iv,
799                                       grasshopper_w128_t * buf)
800 {
801     grasshopper_w128_t tmp;
802     memcpy(&tmp, iv, 16);
803     grasshopper_encrypt_block(&ctx->encrypt_round_keys, &tmp,
804                               buf, &ctx->buffer);
805     memcpy(iv, buf, 16);
806 }
807
808 static int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX *ctx, unsigned char *out,
809                                           const unsigned char *in, size_t inl)
810 {
811     gost_grasshopper_cipher_ctx *c = (gost_grasshopper_cipher_ctx *)
812         EVP_CIPHER_CTX_get_cipher_data(ctx);
813     const unsigned char *in_ptr = in;
814     unsigned char *out_ptr = out;
815     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
816     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
817     int num = EVP_CIPHER_CTX_num(ctx);
818     size_t i = 0;
819     size_t j;
820
821     /* process partial block if any */
822     if (num > 0) {
823         for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
824              j++, i++, in_ptr++, out_ptr++) {
825             *out_ptr = buf[j] ^ (*in_ptr);
826         }
827         if (j == GRASSHOPPER_BLOCK_SIZE) {
828             EVP_CIPHER_CTX_set_num(ctx, 0);
829         } else {
830             EVP_CIPHER_CTX_set_num(ctx, (int)j);
831             return 1;
832         }
833     }
834
835     for (; i + GRASSHOPPER_BLOCK_SIZE <
836          inl;
837          i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
838          GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
839         /*
840          * block cipher current iv
841          */
842         /* Encrypt */
843         gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
844                                   (grasshopper_w128_t *) buf);
845
846         /*
847          * xor next block of input text with it and output it
848          */
849         /*
850          * output this block
851          */
852         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
853             out_ptr[j] = buf[j] ^ in_ptr[j];
854         }
855     }
856
857     /* Process rest of buffer */
858     if (i < inl) {
859         gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
860                                   (grasshopper_w128_t *) buf);
861         for (j = 0; i < inl; j++, i++) {
862             out_ptr[j] = buf[j] ^ in_ptr[j];
863         }
864         EVP_CIPHER_CTX_set_num(ctx, (int)j);
865     } else {
866         EVP_CIPHER_CTX_set_num(ctx, 0);
867     }
868
869     return 1;
870 }
871
872 static int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
873                                           const unsigned char *in, size_t inl)
874 {
875     gost_grasshopper_cipher_ctx *c =
876         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
877     const unsigned char *in_ptr = in;
878     unsigned char *out_ptr = out;
879     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
880     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
881     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
882     int num = EVP_CIPHER_CTX_num(ctx);
883     size_t i = 0;
884     size_t j = 0;
885
886     /* process partial block if any */
887     if (num > 0) {
888         for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
889              j++, i++, in_ptr++, out_ptr++) {
890             if (!encrypting) {
891                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
892             }
893             *out_ptr = buf[j] ^ (*in_ptr);
894             if (encrypting) {
895                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
896             }
897         }
898         if (j == GRASSHOPPER_BLOCK_SIZE) {
899             memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE, GRASSHOPPER_BLOCK_SIZE);
900             EVP_CIPHER_CTX_set_num(ctx, 0);
901         } else {
902             EVP_CIPHER_CTX_set_num(ctx, (int)j);
903             return 1;
904         }
905     }
906
907     for (; i + GRASSHOPPER_BLOCK_SIZE <
908          inl;
909          i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
910          GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
911         /*
912          * block cipher current iv
913          */
914         grasshopper_encrypt_block(&c->encrypt_round_keys,
915                                   (grasshopper_w128_t *) iv,
916                                   (grasshopper_w128_t *) buf, &c->buffer);
917         /*
918          * xor next block of input text with it and output it
919          */
920         /*
921          * output this block
922          */
923         if (!encrypting) {
924             memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
925         }
926         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
927             out_ptr[j] = buf[j] ^ in_ptr[j];
928         }
929         /* Encrypt */
930         /* Next iv is next block of cipher text */
931         if (encrypting) {
932             memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
933         }
934     }
935
936     /* Process rest of buffer */
937     if (i < inl) {
938         grasshopper_encrypt_block(&c->encrypt_round_keys,
939                                   (grasshopper_w128_t *) iv,
940                                   (grasshopper_w128_t *) buf, &c->buffer);
941         if (!encrypting) {
942             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
943         }
944         for (j = 0; i < inl; j++, i++) {
945             out_ptr[j] = buf[j] ^ in_ptr[j];
946         }
947         EVP_CIPHER_CTX_set_num(ctx, (int)j);
948         if (encrypting) {
949             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
950         }
951     } else {
952         EVP_CIPHER_CTX_set_num(ctx, 0);
953     }
954
955     return 1;
956 }
957
958 static int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX *ctx)
959 {
960     gost_grasshopper_cipher_ctx *c =
961         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
962
963     if (!c)
964         return 1;
965
966     if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE)
967         gost_grasshopper_cipher_destroy_ctr(c);
968
969     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
970
971     return 1;
972 }
973
974 static int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
975 {
976     if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE) {
977         gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
978
979         /* CMS implies 256kb section_size */
980         ctr->section_size = 256*1024;
981
982         return gost2015_set_asn1_params(params,
983                EVP_CIPHER_CTX_original_iv(ctx), 8, ctr->kdf_seed);
984     }
985     return 0;
986 }
987
988 static GRASSHOPPER_INLINE int
989 gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
990 {
991     if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE) {
992         gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
993
994         int iv_len = 16;
995         unsigned char iv[16];
996
997         if (gost2015_get_asn1_params(params, 16, iv, 8, ctr->kdf_seed) == 0) {
998             return 0;
999         }
1000
1001         memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, iv_len);
1002         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, iv_len);
1003
1004         /* CMS implies 256kb section_size */
1005         ctr->section_size = 256*1024;
1006         return 1;
1007     }
1008     return 0;
1009 }
1010
1011 static int gost_grasshopper_mgm_cleanup(EVP_CIPHER_CTX *c)
1012 {
1013     gost_mgm_ctx *mctx = 
1014         (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(c);
1015     if (mctx == NULL)
1016         return 0;
1017     gost_grasshopper_cipher_destroy(&mctx->ks.gh_ks);
1018     OPENSSL_cleanse(&mctx->mgm, sizeof(mctx->mgm));
1019     if (mctx->iv != EVP_CIPHER_CTX_iv_noconst(c))
1020         OPENSSL_free(mctx->iv);
1021     return 1;
1022 }
1023
1024 static int gost_grasshopper_mgm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1025 {
1026     gost_mgm_ctx *mctx = 
1027         (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(c);
1028     unsigned char *buf, *iv;
1029     int ivlen, enc;
1030
1031     switch (type) {
1032     case EVP_CTRL_INIT:
1033         ivlen = EVP_CIPHER_iv_length(EVP_CIPHER_CTX_cipher(c));
1034         iv = EVP_CIPHER_CTX_iv_noconst(c);
1035         mctx->key_set = 0;
1036         mctx->iv_set = 0;
1037         mctx->ivlen = ivlen;
1038         mctx->iv = iv;
1039         mctx->taglen = -1;
1040         mctx->tlstree_mode = TLSTREE_MODE_NONE;
1041         return 1;
1042     
1043     case EVP_CTRL_GET_IVLEN:
1044         *(int *)ptr = mctx->ivlen;
1045         return 1;
1046
1047     case EVP_CTRL_AEAD_SET_IVLEN:
1048         if (arg <= 0)
1049             return 0;
1050         if ((arg > EVP_MAX_IV_LENGTH) && (arg > mctx->ivlen)) {
1051             // TODO: Allocate memory for IV or set error
1052             return 0;
1053         }
1054         mctx->ivlen = arg;
1055         return 1;
1056
1057     case EVP_CTRL_AEAD_SET_TAG:
1058         buf = EVP_CIPHER_CTX_buf_noconst(c);
1059         enc = EVP_CIPHER_CTX_encrypting(c);
1060         if (arg <= 0 || arg != 16 || enc) {
1061             GOSTerr(GOST_F_GOST_GRASSHOPPER_MGM_CTRL,
1062                     GOST_R_INVALID_TAG_LENGTH);
1063             return 0;
1064         }
1065         memcpy(buf, ptr, arg);
1066         mctx->taglen = arg;    
1067         return 1;
1068
1069     case EVP_CTRL_AEAD_GET_TAG:
1070         buf = EVP_CIPHER_CTX_buf_noconst(c);
1071         enc = EVP_CIPHER_CTX_encrypting(c);
1072         if (arg <= 0 || arg > 16 || !enc || mctx->taglen < 0) {
1073             GOSTerr(GOST_F_GOST_GRASSHOPPER_MGM_CTRL,
1074                     GOST_R_INVALID_TAG_LENGTH);
1075             return 0;
1076         }
1077         memcpy(ptr, buf, arg);
1078         return 1;
1079
1080     case EVP_CTRL_SET_TLSTREE_PARAMS:
1081         if (strcmp((char *)ptr, "short") == 0)
1082             mctx->tlstree_mode = TLSTREE_MODE_S;
1083         else if (strcmp((char *)ptr, "long") == 0)
1084             mctx->tlstree_mode = TLSTREE_MODE_L;
1085         else {
1086             // TODO: set err
1087             return 0;
1088         }
1089         return 1;
1090
1091     case EVP_CTRL_TLSTREE:
1092         {
1093             unsigned char newkey[32];
1094             if (gost_tlstree(NID_kuznyechik_mgm,
1095                     mctx->ks.gh_ks.master_key.k.b, newkey,
1096                     (const unsigned char *)ptr, mctx->tlstree_mode)
1097                   > 0) {
1098                gost_grasshopper_cipher_key(&mctx->ks.gh_ks, newkey);
1099                memset(newkey, 0, sizeof(newkey));
1100
1101                return 1;
1102             }
1103         }
1104         return -1;
1105
1106     default:
1107         return -1;
1108     }
1109 }
1110
1111 static int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1112 {
1113     switch (type) {
1114     case EVP_CTRL_RAND_KEY:{
1115             if (RAND_priv_bytes
1116                 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
1117                 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR);
1118                 return -1;
1119             }
1120             break;
1121         }
1122     case EVP_CTRL_KEY_MESH:{
1123             gost_grasshopper_cipher_ctx_ctr *c =
1124                 EVP_CIPHER_CTX_get_cipher_data(ctx);
1125             if ((c->c.type != GRASSHOPPER_CIPHER_CTRACPKM &&
1126                 c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
1127                 || (arg == 0)
1128                || (arg % GRASSHOPPER_BLOCK_SIZE))
1129                 return -1;
1130             c->section_size = arg;
1131             break;
1132         }
1133     case EVP_CTRL_TLSTREE:
1134         {
1135           unsigned char newkey[32];
1136           int mode = EVP_CIPHER_CTX_mode(ctx);
1137           gost_grasshopper_cipher_ctx_ctr *ctr_ctx = NULL;
1138           gost_grasshopper_cipher_ctx *c = NULL;
1139
1140           unsigned char adjusted_iv[16];
1141           unsigned char seq[8];
1142           int j, carry, decrement_arg;
1143           if (mode != EVP_CIPH_CTR_MODE)
1144             return -1;
1145
1146           ctr_ctx = (gost_grasshopper_cipher_ctx_ctr *)
1147             EVP_CIPHER_CTX_get_cipher_data(ctx);
1148           c = &(ctr_ctx->c);
1149
1150           /*
1151            * 'arg' parameter indicates what we should do with sequence value.
1152            * 
1153            * When function called, seq is incremented after MAC calculation.
1154            * In ETM mode, we use seq 'as is' in the ctrl-function (arg = 0)
1155            * Otherwise we have to decrease it in the implementation (arg = 1).
1156            */
1157           memcpy(seq, ptr, 8);
1158           decrement_arg = arg;
1159           if (!decrement_sequence(seq, decrement_arg))
1160           {
1161               GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_CTRL_CALL_FAILED);
1162               return -1;
1163           }
1164
1165           if (gost_tlstree(NID_grasshopper_cbc, c->master_key.k.b, newkey,
1166                 (const unsigned char *)seq, TLSTREE_MODE_NONE) > 0) {
1167             memset(adjusted_iv, 0, 16);
1168             memcpy(adjusted_iv, EVP_CIPHER_CTX_original_iv(ctx), 8);
1169             for(j=7,carry=0; j>=0; j--)
1170             {
1171               int adj_byte = adjusted_iv[j]+seq[j]+carry;
1172               carry = (adj_byte > 255) ? 1 : 0;
1173               adjusted_iv[j] = adj_byte & 0xFF;
1174             }
1175             EVP_CIPHER_CTX_set_num(ctx, 0);
1176             memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), adjusted_iv, 16);
1177
1178             gost_grasshopper_cipher_key(c, newkey);
1179             return 1;
1180           }
1181         }
1182         return -1;
1183 #if 0
1184     case EVP_CTRL_AEAD_GET_TAG:
1185     case EVP_CTRL_AEAD_SET_TAG:
1186         {
1187             int taglen = arg;
1188             unsigned char *tag = ptr;
1189
1190             gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1191             if (c->c.type != GRASSHOPPER_CIPHER_MGM)
1192                 return -1;
1193
1194             if (taglen > KUZNYECHIK_MAC_MAX_SIZE) {
1195                 CRYPTOCOMerr(CRYPTOCOM_F_GOST_GRASSHOPPER_CIPHER_CTL,
1196                         CRYPTOCOM_R_INVALID_TAG_LENGTH);
1197                 return -1;
1198             }
1199
1200             if (type == EVP_CTRL_AEAD_GET_TAG)
1201                 memcpy(tag, c->final_tag, taglen);
1202             else
1203                 memcpy(c->final_tag, tag, taglen);
1204
1205             return 1;
1206         }
1207 #endif
1208     case EVP_CTRL_PROCESS_UNPROTECTED:
1209     {
1210       STACK_OF(X509_ATTRIBUTE) *x = ptr;
1211       gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1212
1213       if (c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
1214         return -1;
1215
1216       return gost2015_process_unprotected_attributes(x, arg, KUZNYECHIK_MAC_MAX_SIZE, c->tag);
1217     }
1218     return 1;
1219     case EVP_CTRL_COPY: {
1220         EVP_CIPHER_CTX *out = ptr;
1221
1222         gost_grasshopper_cipher_ctx_ctr *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
1223         gost_grasshopper_cipher_ctx_ctr *in_cctx  = EVP_CIPHER_CTX_get_cipher_data(ctx);
1224
1225         if (in_cctx->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
1226             return -1;
1227
1228         if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
1229             out_cctx->omac_ctx = EVP_MD_CTX_new();
1230             if (out_cctx->omac_ctx == NULL) {
1231                 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, ERR_R_MALLOC_FAILURE);
1232                 return -1;
1233             }
1234         }
1235         return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
1236     }
1237     default:
1238         GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL,
1239                 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
1240         return -1;
1241     }
1242     return 1;
1243 }
1244
1245 /* Called directly by CMAC_ACPKM_Init() */
1246 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctracpkm()
1247 {
1248     return GOST_init_cipher(&grasshopper_ctr_acpkm_cipher);
1249 }
1250 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */