]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_grasshopper_cipher.c
grasshopper: Fix OFB implementation.
[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 #if defined(__cplusplus)
7 extern "C" {
8 #endif
9
10 #include "gost_grasshopper_cipher.h"
11 #include "gost_grasshopper_defines.h"
12 #include "gost_grasshopper_math.h"
13 #include "gost_grasshopper_core.h"
14
15 #include <openssl/evp.h>
16 #include <openssl/rand.h>
17 #include <openssl/err.h>
18 #include <string.h>
19
20 #include "gost_lcl.h"
21 #include "e_gost_err.h"
22
23 enum GRASSHOPPER_CIPHER_TYPE {
24     GRASSHOPPER_CIPHER_ECB = 0,
25     GRASSHOPPER_CIPHER_CBC,
26     GRASSHOPPER_CIPHER_OFB,
27     GRASSHOPPER_CIPHER_CFB,
28     GRASSHOPPER_CIPHER_CTR
29 };
30
31 static EVP_CIPHER* gost_grasshopper_ciphers[5] = {
32         [GRASSHOPPER_CIPHER_ECB] = NULL,
33         [GRASSHOPPER_CIPHER_CBC] = NULL,
34         [GRASSHOPPER_CIPHER_OFB] = NULL,
35         [GRASSHOPPER_CIPHER_CFB] = NULL,
36         [GRASSHOPPER_CIPHER_CTR] = NULL
37 };
38
39 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ofb(gost_grasshopper_cipher_ctx* c);
40 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx* c);
41
42 struct GRASSHOPPER_CIPHER_PARAMS {
43     int nid;
44     grasshopper_init_cipher_func init_cipher;
45     grasshopper_do_cipher_func do_cipher;
46     grasshopper_destroy_cipher_func destroy_cipher;
47     int block_size;
48     int ctx_size;
49     int iv_size;
50     bool padding;
51 };
52
53 static struct GRASSHOPPER_CIPHER_PARAMS gost_cipher_params[5] = {
54         [GRASSHOPPER_CIPHER_ECB] = {
55                 NID_grasshopper_ecb,
56                 gost_grasshopper_cipher_init_ecb,
57                 gost_grasshopper_cipher_do_ecb,
58                 NULL,
59                 16,
60                 sizeof(gost_grasshopper_cipher_ctx),
61                 0,
62                 true
63         },
64         [GRASSHOPPER_CIPHER_CBC] = {
65                 NID_grasshopper_cbc,
66                 gost_grasshopper_cipher_init_cbc,
67                 gost_grasshopper_cipher_do_cbc,
68                 NULL,
69                 16,
70                 sizeof(gost_grasshopper_cipher_ctx),
71                 16,
72                 true
73         },
74         [GRASSHOPPER_CIPHER_OFB] = {
75                 NID_grasshopper_ofb,
76                 gost_grasshopper_cipher_init_ofb,
77                 gost_grasshopper_cipher_do_ofb,
78                 gost_grasshopper_cipher_destroy_ofb,
79                 1,
80                 sizeof(gost_grasshopper_cipher_ctx_ofb),
81                 16,
82                 false
83         },
84         [GRASSHOPPER_CIPHER_CFB] = {
85                 NID_grasshopper_cfb,
86                 gost_grasshopper_cipher_init_cfb,
87                 gost_grasshopper_cipher_do_cfb,
88                 NULL,
89                 1,
90                 sizeof(gost_grasshopper_cipher_ctx),
91                 16,
92                 false
93         },
94         [GRASSHOPPER_CIPHER_CTR] = {
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         },
104 };
105
106 /* Set 256 bit  key into context */
107 GRASSHOPPER_INLINE void gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx* c, const uint8_t* k) {
108                 int i;
109     for (i = 0; i < 2; i++) {
110         grasshopper_copy128(&c->key.k.k[i], (const grasshopper_w128_t*) (k + i * 16));
111     }
112     grasshopper_set_encrypt_key(&c->encrypt_round_keys, &c->key);
113     grasshopper_set_decrypt_key(&c->decrypt_round_keys, &c->key);
114 }
115
116 /* Cleans up key from context */
117 GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx* c) {
118                 int i;
119     for (i = 0; i < 2; i++) {
120         grasshopper_zero128(&c->key.k.k[i]);
121     }
122     for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
123         grasshopper_zero128(&c->encrypt_round_keys.k[i]);
124     }
125     for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
126         grasshopper_zero128(&c->decrypt_round_keys.k[i]);
127     }
128     grasshopper_zero128(&c->buffer);
129 }
130
131 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ofb(gost_grasshopper_cipher_ctx* c) {
132     gost_grasshopper_cipher_ctx_ofb* ctx = (gost_grasshopper_cipher_ctx_ofb*) c;
133
134     grasshopper_zero128(&ctx->buffer1);
135 }
136
137 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx* c) {
138     gost_grasshopper_cipher_ctx_ctr* ctx = (gost_grasshopper_cipher_ctx_ctr*) c;
139
140     grasshopper_zero128(&ctx->iv_buffer);
141     grasshopper_zero128(&ctx->partial_buffer);
142 }
143
144 int gost_grasshopper_cipher_init(EVP_CIPHER_CTX* ctx, const unsigned char* key,
145                                         const unsigned char* iv, int enc) {
146     gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
147
148     if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
149         EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
150     }
151
152     if (key != NULL) {
153         gost_grasshopper_cipher_key(c, key);
154     }
155
156     if (iv != NULL) {
157         memcpy((unsigned char*) EVP_CIPHER_CTX_original_iv(ctx), iv,
158                EVP_CIPHER_CTX_iv_length(ctx));
159     }
160
161     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
162            EVP_CIPHER_CTX_original_iv(ctx),
163            EVP_CIPHER_CTX_iv_length(ctx));
164
165     grasshopper_zero128(&c->buffer);
166
167     return 1;
168 }
169
170 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
171                                                                const unsigned char* iv,
172                                                                int enc) {
173     gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
174     c->type = GRASSHOPPER_CIPHER_ECB;
175     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
176 }
177
178 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX* ctx, const unsigned char* key,
179                                                                const unsigned char* iv,
180                                                                int enc) {
181     gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
182     c->type = GRASSHOPPER_CIPHER_CBC;
183     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
184 }
185
186 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
187                                                                const unsigned char* iv,
188                                                                int enc) {
189     gost_grasshopper_cipher_ctx_ofb* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
190
191     c->c.type = GRASSHOPPER_CIPHER_OFB;
192
193     grasshopper_zero128(&c->buffer1);
194
195     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
196 }
197
198 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
199                                                                const unsigned char* iv,
200                                                                int enc) {
201     gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
202     c->type = GRASSHOPPER_CIPHER_CFB;
203     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
204 }
205
206 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX* ctx, const unsigned char* key,
207                                                                const unsigned char* iv,
208                                                                int enc) {
209     gost_grasshopper_cipher_ctx_ctr* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
210
211     c->c.type = GRASSHOPPER_CIPHER_CTR;
212     ctx->num = 0;
213
214     grasshopper_zero128(&c->iv_buffer);
215     grasshopper_zero128(&c->partial_buffer);
216
217     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
218 }
219
220 GRASSHOPPER_INLINE int gost_grasshopper_cipher_do(EVP_CIPHER_CTX* ctx, unsigned char* out,
221                                                          const unsigned char* in, size_t inl) {
222     gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
223     struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
224
225     return params->do_cipher(ctx, out, in, inl);
226 }
227
228 int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX* ctx, unsigned char* out,
229                                           const unsigned char* in, size_t inl) {
230     gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
231     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
232     const unsigned char* current_in = in;
233     unsigned char* current_out = out;
234     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
235     size_t i;
236
237     for (i = 0; i < blocks; i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out += GRASSHOPPER_BLOCK_SIZE) {
238         if (encrypting) {
239             grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) current_in,
240                                       (grasshopper_w128_t*) current_out,
241                                       &c->buffer);
242         } else {
243             grasshopper_decrypt_block(&c->decrypt_round_keys, (grasshopper_w128_t*) current_in,
244                                       (grasshopper_w128_t*) current_out,
245                                       &c->buffer);
246         }
247     }
248
249     return 1;
250 }
251
252 int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX* ctx, unsigned char* out,
253                                           const unsigned char* in, size_t inl) {
254     gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
255     unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
256     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
257     const unsigned char* current_in = in;
258     unsigned char* current_out = out;
259     grasshopper_w128_t* currentInputBlock;
260     grasshopper_w128_t* currentOutputBlock;
261     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
262     size_t i;
263     grasshopper_w128_t* currentBlock;
264
265     currentBlock = (grasshopper_w128_t*) iv;
266
267     for (i = 0; i < blocks; i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out += GRASSHOPPER_BLOCK_SIZE) {
268         currentInputBlock = (grasshopper_w128_t*) current_in;
269         currentOutputBlock = (grasshopper_w128_t*) current_out;
270         if (encrypting) {
271             grasshopper_append128(currentBlock, currentInputBlock);
272             grasshopper_encrypt_block(&c->encrypt_round_keys, currentBlock, currentOutputBlock, &c->buffer);
273             grasshopper_copy128(currentBlock, currentOutputBlock);
274         } else {
275             grasshopper_decrypt_block(&c->decrypt_round_keys, currentInputBlock, currentOutputBlock, &c->buffer);
276             grasshopper_append128(currentOutputBlock, currentBlock);
277             grasshopper_copy128(currentBlock, currentInputBlock);
278         }
279     }
280
281     return 1;
282 }
283
284 void inc_counter(unsigned char* counter, size_t counter_bytes)
285 {
286     unsigned char c;
287     unsigned int n = counter_bytes;
288
289     do {
290         --n;
291         c = counter[n];
292         ++c;
293         counter[n] = c;
294         if (c) return;
295     } while (n);
296 }
297
298 /* increment counter (128-bit int) by 1 */
299 static void ctr128_inc(unsigned char *counter)
300 {
301         inc_counter(counter, 16);
302 }
303
304 int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
305                                           const unsigned char* in, size_t inl) {
306     gost_grasshopper_cipher_ctx_ctr* c = (gost_grasshopper_cipher_ctx_ctr*) EVP_CIPHER_CTX_get_cipher_data(ctx);
307     unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
308     const unsigned char* current_in = in;
309     unsigned char* current_out = out;
310     grasshopper_w128_t* currentInputBlock;
311     grasshopper_w128_t* currentOutputBlock;
312     unsigned int n = ctx->num;
313     size_t lasted;
314     size_t i;
315
316     while (n && inl) {
317         *(current_out++) = *(current_in++) ^ c->partial_buffer.b[n];
318         --inl;
319         n = (n + 1) % GRASSHOPPER_BLOCK_SIZE;
320     }
321     ctx->num = n;
322     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
323
324     memcpy(&c->iv_buffer, iv, 8);
325
326     // full parts
327     for (i = 0; i < blocks; i++) {
328         currentInputBlock = (grasshopper_w128_t*) current_in;
329         currentOutputBlock = (grasshopper_w128_t*) current_out;
330         grasshopper_encrypt_block(&c->c.encrypt_round_keys, &c->iv_buffer, currentOutputBlock, &c->c.buffer);
331         grasshopper_append128(currentOutputBlock, currentInputBlock);
332         ctr128_inc(c->iv_buffer.b);
333         current_in += GRASSHOPPER_BLOCK_SIZE;
334         current_out += GRASSHOPPER_BLOCK_SIZE;
335     }
336
337     // last part
338     lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
339     if (lasted > 0) {
340         currentInputBlock = (grasshopper_w128_t*) current_in;
341         currentOutputBlock = (grasshopper_w128_t*) current_out;
342         grasshopper_encrypt_block(&c->c.encrypt_round_keys, &c->iv_buffer, &c->partial_buffer, &c->c.buffer);
343         for (i = 0; i < lasted; i++) {
344             currentOutputBlock->b[i] = c->partial_buffer.b[i] ^ currentInputBlock->b[i];
345         }
346         ctx->num = i;
347         ctr128_inc(c->iv_buffer.b);
348     }
349
350     return 1;
351 }
352
353 /*
354  * Fixed 128-bit IV implementation make shift regiser redundant.
355  */
356 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx_ofb* ctx, grasshopper_w128_t* iv,
357                                       grasshopper_w128_t* buf) {
358     memcpy(&ctx->buffer1, iv, 16);
359     grasshopper_encrypt_block(&ctx->c.encrypt_round_keys, &ctx->buffer1, buf, &ctx->c.buffer);
360     memcpy(iv, buf, 16);
361 }
362
363 int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX* ctx, unsigned char* out,
364                                           const unsigned char* in, size_t inl) {
365     gost_grasshopper_cipher_ctx_ofb* c = (gost_grasshopper_cipher_ctx_ofb*) EVP_CIPHER_CTX_get_cipher_data(ctx);
366     const unsigned char* in_ptr = in;
367     unsigned char* out_ptr = out;
368     unsigned char* buf = EVP_CIPHER_CTX_buf_noconst(ctx);
369     unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
370     int num = EVP_CIPHER_CTX_num(ctx);
371     size_t i = 0;
372     size_t j;
373
374     /* process partial block if any */
375     if (num > 0) {
376         for (j = (size_t) num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
377              j++, i++, in_ptr++, out_ptr++) {
378             *out_ptr = buf[j] ^ (*in_ptr);
379         }
380         if (j == GRASSHOPPER_BLOCK_SIZE) {
381             EVP_CIPHER_CTX_set_num(ctx, 0);
382         } else {
383             EVP_CIPHER_CTX_set_num(ctx, (int) j);
384             return 1;
385         }
386     }
387
388     for (; i + GRASSHOPPER_BLOCK_SIZE <
389            inl; i += GRASSHOPPER_BLOCK_SIZE, in_ptr += GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
390         /*
391          * block cipher current iv
392          */
393         /* Encrypt */
394         gost_grasshopper_cnt_next(c, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf);
395
396         /*
397          * xor next block of input text with it and output it
398          */
399         /*
400          * output this block
401          */
402         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
403             out_ptr[j] = buf[j] ^ in_ptr[j];
404         }
405     }
406
407     /* Process rest of buffer */
408     if (i < inl) {
409         gost_grasshopper_cnt_next(c, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf);
410         for (j = 0; i < inl; j++, i++) {
411             out_ptr[j] = buf[j] ^ in_ptr[j];
412         }
413         EVP_CIPHER_CTX_set_num(ctx, (int) j);
414     } else {
415         EVP_CIPHER_CTX_set_num(ctx, 0);
416     }
417
418     return 1;
419 }
420
421 int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX* ctx, unsigned char* out,
422                                           const unsigned char* in, size_t inl) {
423     gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
424     const unsigned char* in_ptr = in;
425     unsigned char* out_ptr = out;
426     unsigned char* buf = EVP_CIPHER_CTX_buf_noconst(ctx);
427     unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
428     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
429     int num = EVP_CIPHER_CTX_num(ctx);
430     size_t i = 0;
431     size_t j = 0;
432
433     /* process partial block if any */
434     if (num > 0) {
435         for (j = (size_t) num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl; j++, i++, in_ptr++, out_ptr++) {
436             if (!encrypting) {
437                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
438             }
439             *out_ptr = buf[j] ^ (*in_ptr);
440             if (encrypting) {
441                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
442             }
443         }
444         if (j == GRASSHOPPER_BLOCK_SIZE) {
445             memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE, GRASSHOPPER_BLOCK_SIZE);
446             EVP_CIPHER_CTX_set_num(ctx, 0);
447         } else {
448             EVP_CIPHER_CTX_set_num(ctx, (int) j);
449             return 1;
450         }
451     }
452
453     for (; i + GRASSHOPPER_BLOCK_SIZE <
454            inl; i += GRASSHOPPER_BLOCK_SIZE, in_ptr += GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
455         /*
456          * block cipher current iv
457          */
458         grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf,
459                                   &c->buffer);
460         /*
461          * xor next block of input text with it and output it
462          */
463         /*
464          * output this block
465          */
466         if (!encrypting) {
467             memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
468         }
469         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
470             out_ptr[j] = buf[j] ^ in_ptr[j];
471         }
472         /* Encrypt */
473         /* Next iv is next block of cipher text */
474         if (encrypting) {
475             memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
476         }
477     }
478
479     /* Process rest of buffer */
480     if (i < inl) {
481         grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf,
482                                   &c->buffer);
483         if (!encrypting) {
484             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
485         }
486         for (j = 0; i < inl; j++, i++) {
487             out_ptr[j] = buf[j] ^ in_ptr[j];
488         }
489         EVP_CIPHER_CTX_set_num(ctx, (int) j);
490         if (encrypting) {
491             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
492         }
493     } else {
494         EVP_CIPHER_CTX_set_num(ctx, 0);
495     }
496
497     return 1;
498 }
499
500 int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX* ctx) {
501     gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
502     struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
503
504     gost_grasshopper_cipher_destroy(c);
505     if (params->destroy_cipher != NULL) {
506         params->destroy_cipher(c);
507     }
508
509     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
510
511     return 1;
512 }
513
514 int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
515     int len = 0;
516     unsigned char* buf = NULL;
517     ASN1_OCTET_STRING* os = NULL;
518
519     os = ASN1_OCTET_STRING_new();
520
521     if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
522         OPENSSL_free(buf);
523         GOSTerr(GOST_F_GOST_GRASSHOPPER_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
524         return 0;
525     }
526     OPENSSL_free(buf);
527
528     ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
529     return 1;
530 }
531
532 GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
533     int ret = -1;
534
535     if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
536         return ret;
537     }
538
539     return 1;
540 }
541
542 int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX* ctx, int type, int arg, void* ptr) {
543     switch (type) {
544         case EVP_CTRL_RAND_KEY: {
545             if (RAND_bytes((unsigned char*) ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
546                 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR);
547                 return -1;
548             }
549             break;
550         }
551         default:
552             GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
553             return -1;
554     }
555     return 1;
556 }
557
558 GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_create(int cipher_type, int block_size) {
559     return EVP_CIPHER_meth_new(cipher_type,
560                                block_size  /* block_size */,
561                                GRASSHOPPER_KEY_SIZE /* key_size */);
562 }
563
564 const int cipher_gost_grasshopper_setup(EVP_CIPHER* cipher, uint8_t mode, int iv_size, bool padding) {
565     return EVP_CIPHER_meth_set_iv_length(cipher, iv_size) &&
566            EVP_CIPHER_meth_set_flags(cipher, (unsigned long) (
567                    mode |
568                    ((!padding) ? EVP_CIPH_NO_PADDING : 0) |
569                    ((iv_size > 0) ? EVP_CIPH_CUSTOM_IV : 0) |
570                    EVP_CIPH_RAND_KEY |
571                    EVP_CIPH_ALWAYS_CALL_INIT)
572            ) &&
573            EVP_CIPHER_meth_set_cleanup(cipher, gost_grasshopper_cipher_cleanup) &&
574            EVP_CIPHER_meth_set_set_asn1_params(cipher, gost_grasshopper_set_asn1_parameters) &&
575            EVP_CIPHER_meth_set_get_asn1_params(cipher, gost_grasshopper_get_asn1_parameters) &&
576            EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl) &&
577            EVP_CIPHER_meth_set_do_cipher(cipher, gost_grasshopper_cipher_do);
578 }
579
580 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper(uint8_t mode, uint8_t num) {
581     EVP_CIPHER** cipher;
582     struct GRASSHOPPER_CIPHER_PARAMS* params;
583
584     cipher = &gost_grasshopper_ciphers[num];
585
586     if (*cipher == NULL) {
587         params = &gost_cipher_params[num];
588
589         int nid = params->nid;
590         grasshopper_init_cipher_func init_cipher = params->init_cipher;
591         int block_size = params->block_size;
592         int ctx_size = params->ctx_size;
593         int iv_size = params->iv_size;
594         bool padding = params->padding;
595
596         *cipher = cipher_gost_grasshopper_create(nid, block_size);
597         if (*cipher == NULL) {
598             return NULL;
599         }
600
601         if (!cipher_gost_grasshopper_setup(*cipher, mode, iv_size, padding) ||
602             !EVP_CIPHER_meth_set_init(*cipher, init_cipher) ||
603             !EVP_CIPHER_meth_set_impl_ctx_size(*cipher, ctx_size)) {
604             EVP_CIPHER_meth_free(*cipher);
605             *cipher = NULL;
606         }
607     }
608
609     return *cipher;
610 }
611
612 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ecb() {
613     return cipher_gost_grasshopper(EVP_CIPH_ECB_MODE, GRASSHOPPER_CIPHER_ECB);
614 }
615
616 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_cbc() {
617     return cipher_gost_grasshopper(EVP_CIPH_CBC_MODE, GRASSHOPPER_CIPHER_CBC);
618 }
619
620 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ofb() {
621     return cipher_gost_grasshopper(EVP_CIPH_OFB_MODE, GRASSHOPPER_CIPHER_OFB);
622 }
623
624 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_cfb() {
625     return cipher_gost_grasshopper(EVP_CIPH_CFB_MODE, GRASSHOPPER_CIPHER_CFB);
626 }
627
628 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ctr() {
629     return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTR);
630 }
631
632 void cipher_gost_grasshopper_destroy(void)
633 {
634     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB]);
635     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB] = NULL;
636     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC]);
637     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC] = NULL;
638     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB]);
639     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB] = NULL;
640     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB]);
641     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB] = NULL;
642     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR]);
643     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR] = NULL;
644 }
645
646 #if defined(__cplusplus)
647 }
648 #endif