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