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