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