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