]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_grasshopper_cipher.c
Magma CTR (no meshing), compile-only version.
[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
213     grasshopper_zero128(&c->iv_buffer);
214     grasshopper_zero128(&c->partial_buffer);
215
216     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
217 }
218
219 GRASSHOPPER_INLINE int gost_grasshopper_cipher_do(EVP_CIPHER_CTX* ctx, unsigned char* out,
220                                                          const unsigned char* in, size_t inl) {
221     gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
222     struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
223
224     return params->do_cipher(ctx, out, in, inl);
225 }
226
227 int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX* ctx, unsigned char* out,
228                                           const unsigned char* in, size_t inl) {
229     gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
230     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
231     const unsigned char* current_in = in;
232     unsigned char* current_out = out;
233     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
234     size_t i;
235
236     for (i = 0; i < blocks; i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out += GRASSHOPPER_BLOCK_SIZE) {
237         if (encrypting) {
238             grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) current_in,
239                                       (grasshopper_w128_t*) current_out,
240                                       &c->buffer);
241         } else {
242             grasshopper_decrypt_block(&c->decrypt_round_keys, (grasshopper_w128_t*) current_in,
243                                       (grasshopper_w128_t*) current_out,
244                                       &c->buffer);
245         }
246     }
247
248     return 1;
249 }
250
251 int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX* ctx, unsigned char* out,
252                                           const unsigned char* in, size_t inl) {
253     gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
254     unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
255     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
256     const unsigned char* current_in = in;
257     unsigned char* current_out = out;
258     grasshopper_w128_t* currentInputBlock;
259     grasshopper_w128_t* currentOutputBlock;
260     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
261     size_t i;
262     grasshopper_w128_t* currentBlock;
263
264     currentBlock = (grasshopper_w128_t*) iv;
265
266     for (i = 0; i < blocks; i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out += GRASSHOPPER_BLOCK_SIZE) {
267         currentInputBlock = (grasshopper_w128_t*) current_in;
268         currentOutputBlock = (grasshopper_w128_t*) current_out;
269         if (encrypting) {
270             grasshopper_append128(currentBlock, currentInputBlock);
271             grasshopper_encrypt_block(&c->encrypt_round_keys, currentBlock, currentOutputBlock, &c->buffer);
272             grasshopper_copy128(currentBlock, currentOutputBlock);
273         } else {
274             grasshopper_decrypt_block(&c->decrypt_round_keys, currentInputBlock, currentOutputBlock, &c->buffer);
275             grasshopper_append128(currentOutputBlock, currentBlock);
276             grasshopper_copy128(currentBlock, currentInputBlock);
277         }
278     }
279
280     return 1;
281 }
282
283 void inc_counter(unsigned char* counter, size_t counter_bytes)
284 {
285     unsigned char c;
286     unsigned int n = counter_bytes;
287
288     do {
289         --n;
290         c = counter[n];
291         ++c;
292         counter[n] = c;
293         if (c) return;
294     } while (n);
295 }
296
297 /* increment counter (128-bit int) by 1 */
298 static void ctr128_inc(unsigned char *counter)
299 {
300         inc_counter(counter, 16);
301 }
302
303 int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
304                                           const unsigned char* in, size_t inl) {
305     gost_grasshopper_cipher_ctx_ctr* c = (gost_grasshopper_cipher_ctx_ctr*) EVP_CIPHER_CTX_get_cipher_data(ctx);
306     unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
307     const unsigned char* current_in = in;
308     unsigned char* current_out = out;
309     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
310     grasshopper_w128_t* currentInputBlock;
311     grasshopper_w128_t* currentOutputBlock;
312     size_t lasted;
313     size_t i;
314
315     memcpy(&c->iv_buffer, iv, 8);
316
317     // full parts
318     for (i = 0; i < blocks; i++) {
319         currentInputBlock = (grasshopper_w128_t*) current_in;
320         currentOutputBlock = (grasshopper_w128_t*) current_out;
321         grasshopper_encrypt_block(&c->c.encrypt_round_keys, &c->iv_buffer, currentOutputBlock, &c->c.buffer);
322         grasshopper_append128(currentOutputBlock, currentInputBlock);
323         ctr128_inc(c->iv_buffer.b);
324         current_in += GRASSHOPPER_BLOCK_SIZE;
325         current_out += GRASSHOPPER_BLOCK_SIZE;
326     }
327
328     // last part
329     lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
330     if (lasted > 0) {
331         currentInputBlock = (grasshopper_w128_t*) current_in;
332         currentOutputBlock = (grasshopper_w128_t*) current_out;
333         grasshopper_encrypt_block(&c->c.encrypt_round_keys, &c->iv_buffer, &c->partial_buffer, &c->c.buffer);
334         for (i = 0; i < lasted; i++) {
335             currentOutputBlock->b[i] = c->partial_buffer.b[i] ^ currentInputBlock->b[i];
336         }
337         ctr128_inc(c->iv_buffer.b);
338     }
339
340     return 1;
341 }
342
343 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx_ofb* ctx, grasshopper_w128_t* iv,
344                                       grasshopper_w128_t* buf) {
345     memcpy(&ctx->buffer1, iv, 16);
346     ctx->g = ctx->buffer1.b[0] | (ctx->buffer1.b[1] << 8) | (ctx->buffer1.b[2] << 16) |
347              ((uint32_t) ctx->buffer1.b[3] << 24);
348     ctx->g += 0x01010101;
349     ctx->buffer1.b[0] = (unsigned char) (ctx->g & 0xff);
350     ctx->buffer1.b[1] = (unsigned char) ((ctx->g >> 8) & 0xff);
351     ctx->buffer1.b[2] = (unsigned char) ((ctx->g >> 16) & 0xff);
352     ctx->buffer1.b[3] = (unsigned char) ((ctx->g >> 24) & 0xff);
353     ctx->g = ctx->buffer1.b[4] | (ctx->buffer1.b[5] << 8) | (ctx->buffer1.b[6] << 16) |
354              ((uint32_t) ctx->buffer1.b[7] << 24);
355     ctx->go = ctx->g;
356     ctx->g += 0x01010104;
357     if (ctx->go > ctx->g) {                 /* overflow */
358         ctx->g++;
359     }
360     ctx->buffer1.b[4] = (unsigned char) (ctx->g & 0xff);
361     ctx->buffer1.b[5] = (unsigned char) ((ctx->g >> 8) & 0xff);
362     ctx->buffer1.b[6] = (unsigned char) ((ctx->g >> 16) & 0xff);
363     ctx->buffer1.b[7] = (unsigned char) ((ctx->g >> 24) & 0xff);
364     ctx->g = ctx->buffer1.b[8] | (ctx->buffer1.b[9] << 8) | (ctx->buffer1.b[10] << 16) |
365              ((uint32_t) ctx->buffer1.b[11] << 24);
366     ctx->go = ctx->g;
367     ctx->g += 0x01010107;
368     if (ctx->go > ctx->g) {                 /* overflow */
369         ctx->g++;
370     }
371     ctx->buffer1.b[8] = (unsigned char) (ctx->g & 0xff);
372     ctx->buffer1.b[9] = (unsigned char) ((ctx->g >> 8) & 0xff);
373     ctx->buffer1.b[10] = (unsigned char) ((ctx->g >> 16) & 0xff);
374     ctx->buffer1.b[11] = (unsigned char) ((ctx->g >> 24) & 0xff);
375     ctx->g = ctx->buffer1.b[12] | (ctx->buffer1.b[13] << 8) | (ctx->buffer1.b[14] << 16) |
376              ((uint32_t) ctx->buffer1.b[15] << 24);
377     ctx->go = ctx->g;
378     ctx->g += 0x01010110;
379     if (ctx->go > ctx->g) {                 /* overflow */
380         ctx->g++;
381     }
382     ctx->buffer1.b[12] = (unsigned char) (ctx->g & 0xff);
383     ctx->buffer1.b[13] = (unsigned char) ((ctx->g >> 8) & 0xff);
384     ctx->buffer1.b[14] = (unsigned char) ((ctx->g >> 16) & 0xff);
385     ctx->buffer1.b[15] = (unsigned char) ((ctx->g >> 24) & 0xff);
386     memcpy(iv, &ctx->buffer1, 16);
387     grasshopper_encrypt_block(&ctx->c.encrypt_round_keys, &ctx->buffer1, buf, &ctx->c.buffer);
388 }
389
390 int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX* ctx, unsigned char* out,
391                                           const unsigned char* in, size_t inl) {
392     gost_grasshopper_cipher_ctx_ofb* c = (gost_grasshopper_cipher_ctx_ofb*) EVP_CIPHER_CTX_get_cipher_data(ctx);
393     const unsigned char* in_ptr = in;
394     unsigned char* out_ptr = out;
395     unsigned char* buf = EVP_CIPHER_CTX_buf_noconst(ctx);
396     unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
397     int num = EVP_CIPHER_CTX_num(ctx);
398     size_t i = 0;
399     size_t j;
400
401     /* process partial block if any */
402     if (num > 0) {
403         for (j = (size_t) num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
404              j++, i++, in_ptr++, out_ptr++) {
405             *out_ptr = buf[j] ^ (*in_ptr);
406         }
407         if (j == GRASSHOPPER_BLOCK_SIZE) {
408             EVP_CIPHER_CTX_set_num(ctx, 0);
409         } else {
410             EVP_CIPHER_CTX_set_num(ctx, (int) j);
411             return 1;
412         }
413     }
414
415     for (; i + GRASSHOPPER_BLOCK_SIZE <
416            inl; i += GRASSHOPPER_BLOCK_SIZE, in_ptr += GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
417         /*
418          * block cipher current iv
419          */
420         /* Encrypt */
421         gost_grasshopper_cnt_next(c, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf);
422
423         /*
424          * xor next block of input text with it and output it
425          */
426         /*
427          * output this block
428          */
429         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
430             out_ptr[j] = buf[j] ^ in_ptr[j];
431         }
432     }
433
434     /* Process rest of buffer */
435     if (i < inl) {
436         gost_grasshopper_cnt_next(c, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf);
437         for (j = 0; i < inl; j++, i++) {
438             out_ptr[j] = buf[j] ^ in_ptr[j];
439         }
440         EVP_CIPHER_CTX_set_num(ctx, (int) j);
441     } else {
442         EVP_CIPHER_CTX_set_num(ctx, 0);
443     }
444
445     return 1;
446 }
447
448 int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX* ctx, unsigned char* out,
449                                           const unsigned char* in, size_t inl) {
450     gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
451     const unsigned char* in_ptr = in;
452     unsigned char* out_ptr = out;
453     unsigned char* buf = EVP_CIPHER_CTX_buf_noconst(ctx);
454     unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
455     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
456     int num = EVP_CIPHER_CTX_num(ctx);
457     size_t i = 0;
458     size_t j = 0;
459
460     /* process partial block if any */
461     if (num > 0) {
462         for (j = (size_t) num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl; j++, i++, in_ptr++, out_ptr++) {
463             if (!encrypting) {
464                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
465             }
466             *out_ptr = buf[j] ^ (*in_ptr);
467             if (encrypting) {
468                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
469             }
470         }
471         if (j == GRASSHOPPER_BLOCK_SIZE) {
472             memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE, GRASSHOPPER_BLOCK_SIZE);
473             EVP_CIPHER_CTX_set_num(ctx, 0);
474         } else {
475             EVP_CIPHER_CTX_set_num(ctx, (int) j);
476             return 1;
477         }
478     }
479
480     for (; i + GRASSHOPPER_BLOCK_SIZE <
481            inl; i += GRASSHOPPER_BLOCK_SIZE, in_ptr += GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
482         /*
483          * block cipher current iv
484          */
485         grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf,
486                                   &c->buffer);
487         /*
488          * xor next block of input text with it and output it
489          */
490         /*
491          * output this block
492          */
493         if (!encrypting) {
494             memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
495         }
496         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
497             out_ptr[j] = buf[j] ^ in_ptr[j];
498         }
499         /* Encrypt */
500         /* Next iv is next block of cipher text */
501         if (encrypting) {
502             memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
503         }
504     }
505
506     /* Process rest of buffer */
507     if (i < inl) {
508         grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf,
509                                   &c->buffer);
510         if (!encrypting) {
511             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
512         }
513         for (j = 0; i < inl; j++, i++) {
514             out_ptr[j] = buf[j] ^ in_ptr[j];
515         }
516         EVP_CIPHER_CTX_set_num(ctx, (int) j);
517         if (encrypting) {
518             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
519         }
520     } else {
521         EVP_CIPHER_CTX_set_num(ctx, 0);
522     }
523
524     return 1;
525 }
526
527 int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX* ctx) {
528     gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
529     struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
530
531     gost_grasshopper_cipher_destroy(c);
532     if (params->destroy_cipher != NULL) {
533         params->destroy_cipher(c);
534     }
535
536     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
537
538     return 1;
539 }
540
541 int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
542     int len = 0;
543     unsigned char* buf = NULL;
544     ASN1_OCTET_STRING* os = NULL;
545
546     os = ASN1_OCTET_STRING_new();
547
548     if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
549         OPENSSL_free(buf);
550         GOSTerr(GOST_F_GOST_GRASSHOPPER_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
551         return 0;
552     }
553     OPENSSL_free(buf);
554
555     ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
556     return 1;
557 }
558
559 GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
560     int ret = -1;
561
562     if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
563         return ret;
564     }
565
566     return 1;
567 }
568
569 int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX* ctx, int type, int arg, void* ptr) {
570     switch (type) {
571         case EVP_CTRL_RAND_KEY: {
572             if (RAND_bytes((unsigned char*) ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
573                 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR);
574                 return -1;
575             }
576             break;
577         }
578         default:
579             GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
580             return -1;
581     }
582     return 1;
583 }
584
585 GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_create(int cipher_type, int block_size) {
586     return EVP_CIPHER_meth_new(cipher_type,
587                                block_size  /* block_size */,
588                                GRASSHOPPER_KEY_SIZE /* key_size */);
589 }
590
591 const int cipher_gost_grasshopper_setup(EVP_CIPHER* cipher, uint8_t mode, int iv_size, bool padding) {
592     return EVP_CIPHER_meth_set_iv_length(cipher, iv_size) &&
593            EVP_CIPHER_meth_set_flags(cipher, (unsigned long) (
594                    mode |
595                    ((!padding) ? EVP_CIPH_NO_PADDING : 0) |
596                    ((iv_size > 0) ? EVP_CIPH_CUSTOM_IV : 0) |
597                    EVP_CIPH_RAND_KEY |
598                    EVP_CIPH_ALWAYS_CALL_INIT)
599            ) &&
600            EVP_CIPHER_meth_set_cleanup(cipher, gost_grasshopper_cipher_cleanup) &&
601            EVP_CIPHER_meth_set_set_asn1_params(cipher, gost_grasshopper_set_asn1_parameters) &&
602            EVP_CIPHER_meth_set_get_asn1_params(cipher, gost_grasshopper_get_asn1_parameters) &&
603            EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl) &&
604            EVP_CIPHER_meth_set_do_cipher(cipher, gost_grasshopper_cipher_do);
605 }
606
607 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper(uint8_t mode, uint8_t num) {
608     EVP_CIPHER** cipher;
609     struct GRASSHOPPER_CIPHER_PARAMS* params;
610
611     cipher = &gost_grasshopper_ciphers[num];
612
613     if (*cipher == NULL) {
614         params = &gost_cipher_params[num];
615
616         int nid = params->nid;
617         grasshopper_init_cipher_func init_cipher = params->init_cipher;
618         int block_size = params->block_size;
619         int ctx_size = params->ctx_size;
620         int iv_size = params->iv_size;
621         bool padding = params->padding;
622
623         *cipher = cipher_gost_grasshopper_create(nid, block_size);
624         if (*cipher == NULL) {
625             return NULL;
626         }
627
628         if (!cipher_gost_grasshopper_setup(*cipher, mode, iv_size, padding) ||
629             !EVP_CIPHER_meth_set_init(*cipher, init_cipher) ||
630             !EVP_CIPHER_meth_set_impl_ctx_size(*cipher, ctx_size)) {
631             EVP_CIPHER_meth_free(*cipher);
632             *cipher = NULL;
633         }
634     }
635
636     return *cipher;
637 }
638
639 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ecb() {
640     return cipher_gost_grasshopper(EVP_CIPH_ECB_MODE, GRASSHOPPER_CIPHER_ECB);
641 }
642
643 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_cbc() {
644     return cipher_gost_grasshopper(EVP_CIPH_CBC_MODE, GRASSHOPPER_CIPHER_CBC);
645 }
646
647 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ofb() {
648     return cipher_gost_grasshopper(EVP_CIPH_OFB_MODE, GRASSHOPPER_CIPHER_OFB);
649 }
650
651 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_cfb() {
652     return cipher_gost_grasshopper(EVP_CIPH_CFB_MODE, GRASSHOPPER_CIPHER_CFB);
653 }
654
655 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ctr() {
656     return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTR);
657 }
658
659 #if defined(__cplusplus)
660 }
661 #endif