]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_grasshopper_cipher.c
CTR encryption update
[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 = inl;
466     size_t i;
467     size_t blocks;
468     grasshopper_w128_t *iv_buffer;
469     grasshopper_w128_t tmp;
470
471     while (n && lasted) {
472         *(current_out++) = *(current_in++) ^ c->partial_buffer.b[n];
473         --lasted;
474         n = (n + 1) % GRASSHOPPER_BLOCK_SIZE;
475     }
476     EVP_CIPHER_CTX_set_num(ctx, n);
477     blocks = lasted / 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                                 lasted -= GRASSHOPPER_BLOCK_SIZE;
493     }
494
495     if (lasted > 0) {
496         currentInputBlock = (grasshopper_w128_t *) current_in;
497         currentOutputBlock = (grasshopper_w128_t *) current_out;
498         grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
499                                   &c->partial_buffer, &c->c.buffer);
500         for (i = 0; i < lasted; i++) {
501             currentOutputBlock->b[i] =
502                 c->partial_buffer.b[i] ^ currentInputBlock->b[i];
503         }
504         EVP_CIPHER_CTX_set_num(ctx, i);
505         ctr128_inc(iv_buffer->b);
506     }
507
508     return inl;
509 }
510
511 #define GRASSHOPPER_BLOCK_MASK (GRASSHOPPER_BLOCK_SIZE - 1)
512 static inline void apply_acpkm_grasshopper(gost_grasshopper_cipher_ctx_ctr *
513                                            ctx, unsigned int *num)
514 {
515     if (!ctx->section_size || (*num < ctx->section_size))
516         return;
517     acpkm_next(&ctx->c);
518     *num &= GRASSHOPPER_BLOCK_MASK;
519 }
520
521 /* If meshing is not configured via ctrl (setting section_size)
522  * this function works exactly like plain ctr */
523 int gost_grasshopper_cipher_do_ctracpkm(EVP_CIPHER_CTX *ctx,
524                                         unsigned char *out,
525                                         const unsigned char *in, size_t inl)
526 {
527     gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
528     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
529     unsigned int num = EVP_CIPHER_CTX_num(ctx);
530     size_t blocks, i, lasted = inl;
531     grasshopper_w128_t tmp;
532
533     while ((num & GRASSHOPPER_BLOCK_MASK) && lasted) {
534         *out++ = *in++ ^ c->partial_buffer.b[num & GRASSHOPPER_BLOCK_MASK];
535         --lasted;
536         num++;
537     }
538     blocks = lasted / GRASSHOPPER_BLOCK_SIZE;
539
540     // full parts
541     for (i = 0; i < blocks; i++) {
542         apply_acpkm_grasshopper(c, &num);
543         grasshopper_encrypt_block(&c->c.encrypt_round_keys,
544                                   (grasshopper_w128_t *) iv,
545                                   (grasshopper_w128_t *) & c->partial_buffer,
546                                   &c->c.buffer);
547         grasshopper_plus128(&tmp, &c->partial_buffer,
548                             (grasshopper_w128_t *) in);
549         grasshopper_copy128((grasshopper_w128_t *) out, &tmp);
550         ctr128_inc(iv);
551         in += GRASSHOPPER_BLOCK_SIZE;
552         out += GRASSHOPPER_BLOCK_SIZE;
553         num += GRASSHOPPER_BLOCK_SIZE;
554                                 lasted -= GRASSHOPPER_BLOCK_SIZE;
555     }
556
557     // last part
558     if (lasted > 0) {
559         apply_acpkm_grasshopper(c, &num);
560         grasshopper_encrypt_block(&c->c.encrypt_round_keys,
561                                   (grasshopper_w128_t *) iv,
562                                   &c->partial_buffer, &c->c.buffer);
563         for (i = 0; i < lasted; i++)
564             out[i] = c->partial_buffer.b[i] ^ in[i];
565         ctr128_inc(iv);
566         num += lasted;
567     }
568     EVP_CIPHER_CTX_set_num(ctx, num);
569
570     return inl;
571 }
572
573 int gost_grasshopper_cipher_do_ctracpkm_omac(EVP_CIPHER_CTX *ctx,
574                                         unsigned char *out,
575                                         const unsigned char *in, size_t inl)
576 {
577         int result;
578   gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
579         /* As in and out can be the same pointer, process unencrypted here */
580         if (EVP_CIPHER_CTX_encrypting(ctx))
581                 EVP_DigestSignUpdate(c->omac_ctx, in, inl);
582
583         if (in == NULL && inl == 0) { /* Final call */
584                 return gost2015_final_call(ctx, c->omac_ctx, KUZNYECHIK_MAC_MAX_SIZE, c->tag, gost_grasshopper_cipher_do_ctracpkm);
585         }
586
587         result = gost_grasshopper_cipher_do_ctracpkm(ctx, out, in, inl);
588
589         /* As in and out can be the same pointer, process decrypted here */
590         if (!EVP_CIPHER_CTX_encrypting(ctx))
591                 EVP_DigestSignUpdate(c->omac_ctx, out, inl);
592
593         return result;
594 }
595 /*
596  * Fixed 128-bit IV implementation make shift regiser redundant.
597  */
598 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx * ctx,
599                                       grasshopper_w128_t * iv,
600                                       grasshopper_w128_t * buf)
601 {
602     grasshopper_w128_t tmp;
603     memcpy(&tmp, iv, 16);
604     grasshopper_encrypt_block(&ctx->encrypt_round_keys, &tmp,
605                               buf, &ctx->buffer);
606     memcpy(iv, buf, 16);
607 }
608
609 int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX *ctx, unsigned char *out,
610                                    const unsigned char *in, size_t inl)
611 {
612     gost_grasshopper_cipher_ctx *c = (gost_grasshopper_cipher_ctx *)
613         EVP_CIPHER_CTX_get_cipher_data(ctx);
614     const unsigned char *in_ptr = in;
615     unsigned char *out_ptr = out;
616     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
617     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
618     int num = EVP_CIPHER_CTX_num(ctx);
619     size_t i = 0;
620     size_t j;
621
622     /* process partial block if any */
623     if (num > 0) {
624         for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
625              j++, i++, in_ptr++, out_ptr++) {
626             *out_ptr = buf[j] ^ (*in_ptr);
627         }
628         if (j == GRASSHOPPER_BLOCK_SIZE) {
629             EVP_CIPHER_CTX_set_num(ctx, 0);
630         } else {
631             EVP_CIPHER_CTX_set_num(ctx, (int)j);
632             return 1;
633         }
634     }
635
636     for (; i + GRASSHOPPER_BLOCK_SIZE <
637          inl;
638          i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
639          GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
640         /*
641          * block cipher current iv
642          */
643         /* Encrypt */
644         gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
645                                   (grasshopper_w128_t *) buf);
646
647         /*
648          * xor next block of input text with it and output it
649          */
650         /*
651          * output this block
652          */
653         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
654             out_ptr[j] = buf[j] ^ in_ptr[j];
655         }
656     }
657
658     /* Process rest of buffer */
659     if (i < inl) {
660         gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
661                                   (grasshopper_w128_t *) buf);
662         for (j = 0; i < inl; j++, i++) {
663             out_ptr[j] = buf[j] ^ in_ptr[j];
664         }
665         EVP_CIPHER_CTX_set_num(ctx, (int)j);
666     } else {
667         EVP_CIPHER_CTX_set_num(ctx, 0);
668     }
669
670     return 1;
671 }
672
673 int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
674                                    const unsigned char *in, size_t inl)
675 {
676     gost_grasshopper_cipher_ctx *c =
677         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
678     const unsigned char *in_ptr = in;
679     unsigned char *out_ptr = out;
680     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
681     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
682     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
683     int num = EVP_CIPHER_CTX_num(ctx);
684     size_t i = 0;
685     size_t j = 0;
686
687     /* process partial block if any */
688     if (num > 0) {
689         for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
690              j++, i++, in_ptr++, out_ptr++) {
691             if (!encrypting) {
692                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
693             }
694             *out_ptr = buf[j] ^ (*in_ptr);
695             if (encrypting) {
696                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
697             }
698         }
699         if (j == GRASSHOPPER_BLOCK_SIZE) {
700             memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE, GRASSHOPPER_BLOCK_SIZE);
701             EVP_CIPHER_CTX_set_num(ctx, 0);
702         } else {
703             EVP_CIPHER_CTX_set_num(ctx, (int)j);
704             return 1;
705         }
706     }
707
708     for (; i + GRASSHOPPER_BLOCK_SIZE <
709          inl;
710          i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
711          GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
712         /*
713          * block cipher current iv
714          */
715         grasshopper_encrypt_block(&c->encrypt_round_keys,
716                                   (grasshopper_w128_t *) iv,
717                                   (grasshopper_w128_t *) buf, &c->buffer);
718         /*
719          * xor next block of input text with it and output it
720          */
721         /*
722          * output this block
723          */
724         if (!encrypting) {
725             memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
726         }
727         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
728             out_ptr[j] = buf[j] ^ in_ptr[j];
729         }
730         /* Encrypt */
731         /* Next iv is next block of cipher text */
732         if (encrypting) {
733             memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
734         }
735     }
736
737     /* Process rest of buffer */
738     if (i < inl) {
739         grasshopper_encrypt_block(&c->encrypt_round_keys,
740                                   (grasshopper_w128_t *) iv,
741                                   (grasshopper_w128_t *) buf, &c->buffer);
742         if (!encrypting) {
743             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
744         }
745         for (j = 0; i < inl; j++, i++) {
746             out_ptr[j] = buf[j] ^ in_ptr[j];
747         }
748         EVP_CIPHER_CTX_set_num(ctx, (int)j);
749         if (encrypting) {
750             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
751         }
752     } else {
753         EVP_CIPHER_CTX_set_num(ctx, 0);
754     }
755
756     return 1;
757 }
758
759 int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX *ctx)
760 {
761     struct GRASSHOPPER_CIPHER_PARAMS *params;
762     gost_grasshopper_cipher_ctx *c =
763         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
764
765     if (!c)
766         return 1;
767
768     params = &gost_cipher_params[c->type];
769
770     gost_grasshopper_cipher_destroy(c);
771     if (params->destroy_cipher != NULL) {
772         params->destroy_cipher(c);
773     }
774
775     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
776
777     return 1;
778 }
779
780 int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
781 {
782         if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE) {
783                 gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
784
785                 /* CMS implies 256kb section_size */
786                 ctr->section_size = 256*1024;
787
788                 return gost2015_set_asn1_params(params, EVP_CIPHER_CTX_original_iv(ctx), 8,
789                                 ctr->kdf_seed);
790         }
791         return 0;
792 }
793
794 GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX
795                                                             *ctx, ASN1_TYPE
796                                                             *params)
797 {
798         if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE) {
799                 gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
800
801                 int iv_len = 16;
802                 unsigned char iv[16];
803
804                 if (gost2015_get_asn1_params(params, 16, iv, 8, ctr->kdf_seed) == 0) {
805                         return 0;
806                 }
807
808                 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, iv_len);
809                 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, iv_len);
810
811                 /* CMS implies 256kb section_size */
812                 ctr->section_size = 256*1024;
813                 return 1;
814         }
815         return 0;
816 }
817
818 int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg,
819                                 void *ptr)
820 {
821     switch (type) {
822     case EVP_CTRL_RAND_KEY:{
823             if (RAND_priv_bytes
824                 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
825                 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR);
826                 return -1;
827             }
828             break;
829         }
830     case EVP_CTRL_KEY_MESH:{
831             gost_grasshopper_cipher_ctx_ctr *c =
832                 EVP_CIPHER_CTX_get_cipher_data(ctx);
833             if ((c->c.type != GRASSHOPPER_CIPHER_CTRACPKM &&
834                                                     c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
835                                                     || (arg == 0)
836                 || (arg % GRASSHOPPER_BLOCK_SIZE))
837                 return -1;
838             c->section_size = arg;
839             break;
840         }
841 #ifdef EVP_CTRL_TLS1_2_TLSTREE
842     case EVP_CTRL_TLS1_2_TLSTREE:
843         {
844           unsigned char newkey[32];
845           int mode = EVP_CIPHER_CTX_mode(ctx);
846           static const unsigned char zeroseq[8];
847           gost_grasshopper_cipher_ctx_ctr *ctr_ctx = NULL;
848           gost_grasshopper_cipher_ctx *c = NULL;
849
850           unsigned char adjusted_iv[16];
851           unsigned char seq[8];
852           int j, carry;
853           if (mode != EVP_CIPH_CTR_MODE)
854             return -1;
855
856           ctr_ctx = (gost_grasshopper_cipher_ctx_ctr *)
857             EVP_CIPHER_CTX_get_cipher_data(ctx);
858           c = &(ctr_ctx->c);
859
860           memcpy(seq, ptr, 8);
861           if (EVP_CIPHER_CTX_encrypting(ctx)) {
862             /*
863              * OpenSSL increments seq after mac calculation.
864              * As we have Mac-Then-Encrypt, we need decrement it here on encryption
865              * to derive the key correctly.
866              * */
867             if (memcmp(seq, zeroseq, 8) != 0)
868             {
869               for(j=7; j>=0; j--)
870               {
871                 if (seq[j] != 0) {seq[j]--; break;}
872                 else seq[j]  = 0xFF;
873               }
874             }
875           }
876           if (gost_tlstree(NID_grasshopper_cbc, c->master_key.k.b, newkey,
877                 (const unsigned char *)seq) > 0) {
878             memset(adjusted_iv, 0, 16);
879             memcpy(adjusted_iv, EVP_CIPHER_CTX_original_iv(ctx), 8);
880             for(j=7,carry=0; j>=0; j--)
881             {
882               int adj_byte = adjusted_iv[j]+seq[j]+carry;
883               carry = (adj_byte > 255) ? 1 : 0;
884               adjusted_iv[j] = adj_byte & 0xFF;
885             }
886             EVP_CIPHER_CTX_set_num(ctx, 0);
887             memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), adjusted_iv, 16);
888
889             gost_grasshopper_cipher_key(c, newkey);
890             return 1;
891           }
892         }
893         return -1;
894 #endif
895 #if 0
896     case EVP_CTRL_AEAD_GET_TAG:
897     case EVP_CTRL_AEAD_SET_TAG:
898         {
899             int taglen = arg;
900             unsigned char *tag = ptr;
901
902             gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
903             if (c->c.type != GRASSHOPPER_CIPHER_MGM)
904                 return -1;
905
906             if (taglen > KUZNYECHIK_MAC_MAX_SIZE) {
907                 CRYPTOCOMerr(CRYPTOCOM_F_GOST_GRASSHOPPER_CIPHER_CTL,
908                         CRYPTOCOM_R_INVALID_TAG_LENGTH);
909                 return -1;
910             }
911
912             if (type == EVP_CTRL_AEAD_GET_TAG)
913                 memcpy(tag, c->final_tag, taglen);
914             else
915                 memcpy(c->final_tag, tag, taglen);
916
917             return 1;
918         }
919 #endif
920                 case EVP_CTRL_PROCESS_UNPROTECTED:
921                                 {
922                                         gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
923                                         ASN1_OBJECT *cmsmacobj = NULL;
924                                         if (c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
925                                                 return -1;
926           cmsmacobj = OBJ_txt2obj(OID_GOST_CMS_MAC, 1);
927                                         if (cmsmacobj == NULL) {
928                                                 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, ERR_R_MALLOC_FAILURE);
929                                                 return -1;
930                                         }
931                                         if (arg == 0) /*Decrypting*/ {
932                                                 STACK_OF(X509_ATTRIBUTE) *x = ptr;
933                                                 ASN1_OCTET_STRING *osExpectedMac = X509at_get0_data_by_OBJ(x,
934                                                                 cmsmacobj, -3, V_ASN1_OCTET_STRING);
935                                                 ASN1_OBJECT_free(cmsmacobj);
936
937                                                 if (ptr == NULL || osExpectedMac ==NULL || osExpectedMac->length != KUZNYECHIK_MAC_MAX_SIZE)
938                                                         return -1;
939
940                                                 memcpy(c->tag, osExpectedMac->data, osExpectedMac->length);
941                                                 return 1;
942                                         } else {
943                                                 STACK_OF(X509_ATTRIBUTE) *x = ptr;
944                                                 return (X509at_add1_attr_by_OBJ(&x, cmsmacobj,
945                                                                         V_ASN1_OCTET_STRING, c->tag, KUZNYECHIK_MAC_MAX_SIZE) == NULL) ? -1 : 1;
946                                         }
947                                 }
948                         return 1;
949     case EVP_CTRL_COPY: {
950                         EVP_CIPHER_CTX *out = ptr;
951
952       gost_grasshopper_cipher_ctx_ctr *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
953       gost_grasshopper_cipher_ctx_ctr *in_cctx  = EVP_CIPHER_CTX_get_cipher_data(ctx);
954
955       if (in_cctx->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
956           return -1;
957
958                         if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
959                                 out_cctx->omac_ctx = EVP_MD_CTX_new();
960                                 if (out_cctx->omac_ctx == NULL) {
961                                         GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, ERR_R_MALLOC_FAILURE);
962                                         return -1;
963                                 }
964                         }
965                         return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
966                 }
967     default:
968         GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL,
969                 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
970         return -1;
971     }
972     return 1;
973 }
974
975 GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_create(int
976                                                               cipher_type, int
977                                                               block_size)
978 {
979     return EVP_CIPHER_meth_new(cipher_type, block_size /* block_size */ ,
980                                GRASSHOPPER_KEY_SIZE /* key_size */ );
981 }
982
983 const int cipher_gost_grasshopper_setup(EVP_CIPHER *cipher, uint8_t mode,
984                                         int iv_size, bool padding, int extra_flags)
985 {
986         unsigned long flags = (unsigned long)(mode
987                  | ((!padding) ?  EVP_CIPH_NO_PADDING : 0)
988                                          | ((iv_size >  0) ?  EVP_CIPH_CUSTOM_IV : 0)
989                                          | EVP_CIPH_RAND_KEY |  EVP_CIPH_ALWAYS_CALL_INIT
990                                          | extra_flags);
991
992     return EVP_CIPHER_meth_set_iv_length(cipher, iv_size)
993         && EVP_CIPHER_meth_set_flags(cipher, flags)
994         && EVP_CIPHER_meth_set_cleanup(cipher, gost_grasshopper_cipher_cleanup)
995         && EVP_CIPHER_meth_set_set_asn1_params(cipher,
996                                                gost_grasshopper_set_asn1_parameters)
997         && EVP_CIPHER_meth_set_get_asn1_params(cipher,
998                                                gost_grasshopper_get_asn1_parameters)
999         && EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl)
1000         && EVP_CIPHER_meth_set_do_cipher(cipher, gost_grasshopper_cipher_do);
1001 }
1002
1003 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper(uint8_t mode,
1004                                                              uint8_t num)
1005 {
1006     EVP_CIPHER **cipher;
1007     struct GRASSHOPPER_CIPHER_PARAMS *params;
1008
1009     cipher = &gost_grasshopper_ciphers[num];
1010
1011     if (*cipher == NULL) {
1012         grasshopper_init_cipher_func init_cipher;
1013         int nid, block_size, ctx_size, iv_size, extra_flags;
1014         bool padding;
1015
1016         params = &gost_cipher_params[num];
1017
1018         nid = params->nid;
1019         init_cipher = params->init_cipher;
1020         block_size = params->block_size;
1021         ctx_size = params->ctx_size;
1022         iv_size = params->iv_size;
1023         padding = params->padding;
1024                                 extra_flags = params->extra_flags;
1025
1026         *cipher = cipher_gost_grasshopper_create(nid, block_size);
1027         if (*cipher == NULL) {
1028             return NULL;
1029         }
1030
1031         if (!cipher_gost_grasshopper_setup(*cipher, mode, iv_size, padding, extra_flags)
1032             || !EVP_CIPHER_meth_set_init(*cipher, init_cipher)
1033             || !EVP_CIPHER_meth_set_impl_ctx_size(*cipher, ctx_size)) {
1034             EVP_CIPHER_meth_free(*cipher);
1035             *cipher = NULL;
1036         }
1037     }
1038
1039     return *cipher;
1040 }
1041
1042 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ecb()
1043 {
1044     return cipher_gost_grasshopper(EVP_CIPH_ECB_MODE, GRASSHOPPER_CIPHER_ECB);
1045 }
1046
1047 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_cbc()
1048 {
1049     return cipher_gost_grasshopper(EVP_CIPH_CBC_MODE, GRASSHOPPER_CIPHER_CBC);
1050 }
1051
1052 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ofb()
1053 {
1054     return cipher_gost_grasshopper(EVP_CIPH_OFB_MODE, GRASSHOPPER_CIPHER_OFB);
1055 }
1056
1057 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_cfb()
1058 {
1059     return cipher_gost_grasshopper(EVP_CIPH_CFB_MODE, GRASSHOPPER_CIPHER_CFB);
1060 }
1061
1062 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctr()
1063 {
1064     return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTR);
1065 }
1066
1067 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctracpkm()
1068 {
1069     return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE,
1070                                    GRASSHOPPER_CIPHER_CTRACPKM);
1071 }
1072
1073 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctracpkm_omac()
1074 {
1075     return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE,
1076                                    GRASSHOPPER_CIPHER_CTRACPKMOMAC);
1077 }
1078
1079 void cipher_gost_grasshopper_destroy(void)
1080 {
1081     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB]);
1082     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB] = NULL;
1083     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC]);
1084     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC] = NULL;
1085     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB]);
1086     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB] = NULL;
1087     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB]);
1088     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB] = NULL;
1089     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR]);
1090     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR] = NULL;
1091     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKM]);
1092     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKM] = NULL;
1093     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKMOMAC]);
1094     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKMOMAC] = NULL;
1095 }