]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_grasshopper_cipher.c
Merge pull request #83 from vt-alt/in-places
[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     GRASSHOPPER_CIPHER_CTRACPKM,
30 };
31
32 static EVP_CIPHER *gost_grasshopper_ciphers[6] = {
33     [GRASSHOPPER_CIPHER_ECB] = NULL,
34     [GRASSHOPPER_CIPHER_CBC] = NULL,
35     [GRASSHOPPER_CIPHER_OFB] = NULL,
36     [GRASSHOPPER_CIPHER_CFB] = NULL,
37     [GRASSHOPPER_CIPHER_CTR] = NULL,
38     [GRASSHOPPER_CIPHER_CTRACPKM] = NULL,
39 };
40
41 static GRASSHOPPER_INLINE void
42     gost_grasshopper_cipher_destroy_ofb(gost_grasshopper_cipher_ctx * c);
43 static GRASSHOPPER_INLINE void
44     gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx * c);
45
46 struct GRASSHOPPER_CIPHER_PARAMS {
47     int nid;
48     grasshopper_init_cipher_func init_cipher;
49     grasshopper_do_cipher_func do_cipher;
50     grasshopper_destroy_cipher_func destroy_cipher;
51     int block_size;
52     int ctx_size;
53     int iv_size;
54     bool padding;
55 };
56
57 static struct GRASSHOPPER_CIPHER_PARAMS gost_cipher_params[6] = {
58     [GRASSHOPPER_CIPHER_ECB] = {
59                                 NID_grasshopper_ecb,
60                                 gost_grasshopper_cipher_init_ecb,
61                                 gost_grasshopper_cipher_do_ecb,
62                                 NULL,
63                                 16,
64                                 sizeof(gost_grasshopper_cipher_ctx),
65                                 0,
66                                 true}
67     ,
68     [GRASSHOPPER_CIPHER_CBC] = {
69                                 NID_grasshopper_cbc,
70                                 gost_grasshopper_cipher_init_cbc,
71                                 gost_grasshopper_cipher_do_cbc,
72                                 NULL,
73                                 16,
74                                 sizeof(gost_grasshopper_cipher_ctx),
75                                 16,
76                                 true}
77     ,
78     [GRASSHOPPER_CIPHER_OFB] = {
79                                 NID_grasshopper_ofb,
80                                 gost_grasshopper_cipher_init_ofb,
81                                 gost_grasshopper_cipher_do_ofb,
82                                 gost_grasshopper_cipher_destroy_ofb,
83                                 1,
84                                 sizeof(gost_grasshopper_cipher_ctx_ofb),
85                                 16,
86                                 false}
87     ,
88     [GRASSHOPPER_CIPHER_CFB] = {
89                                 NID_grasshopper_cfb,
90                                 gost_grasshopper_cipher_init_cfb,
91                                 gost_grasshopper_cipher_do_cfb,
92                                 NULL,
93                                 1,
94                                 sizeof(gost_grasshopper_cipher_ctx),
95                                 16,
96                                 false}
97     ,
98     [GRASSHOPPER_CIPHER_CTR] = {
99                                 NID_grasshopper_ctr,
100                                 gost_grasshopper_cipher_init_ctr,
101                                 gost_grasshopper_cipher_do_ctr,
102                                 gost_grasshopper_cipher_destroy_ctr,
103                                 1,
104                                 sizeof(gost_grasshopper_cipher_ctx_ctr),
105                                 /* IV size is set to match full block, to make it responsibility of
106                                  * user to assign correct values (IV || 0), and to make naive context
107                                  * copy possible (for software such as openssh) */
108                                 16,
109                                 false}
110     ,
111     [GRASSHOPPER_CIPHER_CTRACPKM] = {
112                                      NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm,
113                                      gost_grasshopper_cipher_init_ctracpkm,
114                                      gost_grasshopper_cipher_do_ctracpkm,
115                                      gost_grasshopper_cipher_destroy_ctr,
116                                      1,
117                                      sizeof
118                                      (gost_grasshopper_cipher_ctx_ctr),
119                                      16,
120                                      false}
121     ,
122 };
123
124 /* first 256 bit of D from draft-irtf-cfrg-re-keying-12 */
125 static const unsigned char ACPKM_D_2018[] = {
126     0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /*  64 bit */
127     0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 128 bit */
128     0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
129     0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 256 bit */
130 };
131
132 static void acpkm_next(gost_grasshopper_cipher_ctx * c) {
133     unsigned char newkey[GRASSHOPPER_KEY_SIZE];
134     const int J = GRASSHOPPER_KEY_SIZE / GRASSHOPPER_BLOCK_SIZE;
135     int n;
136
137     for (n = 0; n < J; n++) {
138         const unsigned char *D_n =
139             &ACPKM_D_2018[n * GRASSHOPPER_BLOCK_SIZE];
140
141         grasshopper_encrypt_block(&c->encrypt_round_keys,
142                                   (grasshopper_w128_t *) D_n,
143                                   (grasshopper_w128_t *) & newkey[n *
144                                                                   GRASSHOPPER_BLOCK_SIZE],
145                                   &c->buffer);
146     }
147     gost_grasshopper_cipher_key(c, newkey);
148 }
149
150 /* Set 256 bit  key into context */
151 GRASSHOPPER_INLINE void
152     gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx * c,
153                                 const uint8_t *k) {
154     int i;
155     for (i = 0; i < 2; i++) {
156         grasshopper_copy128(&c->key.k.k[i],
157                             (const grasshopper_w128_t *)(k + i * 16));
158     }
159     grasshopper_set_encrypt_key(&c->encrypt_round_keys, &c->key);
160     grasshopper_set_decrypt_key(&c->decrypt_round_keys, &c->key);
161 }
162
163 /* Cleans up key from context */
164 GRASSHOPPER_INLINE void
165     gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx * c) {
166     int i;
167     for (i = 0; i < 2; i++) {
168         grasshopper_zero128(&c->key.k.k[i]);
169     }
170     for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
171         grasshopper_zero128(&c->encrypt_round_keys.k[i]);
172     }
173     for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
174         grasshopper_zero128(&c->decrypt_round_keys.k[i]);
175     }
176     grasshopper_zero128(&c->buffer);
177 }
178
179 static GRASSHOPPER_INLINE void
180     gost_grasshopper_cipher_destroy_ofb(gost_grasshopper_cipher_ctx * c) {
181     gost_grasshopper_cipher_ctx_ofb *ctx =
182         (gost_grasshopper_cipher_ctx_ofb *) c;
183
184     grasshopper_zero128(&ctx->buffer1);
185 }
186
187 static GRASSHOPPER_INLINE void
188     gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx * c) {
189     gost_grasshopper_cipher_ctx_ctr *ctx =
190         (gost_grasshopper_cipher_ctx_ctr *) c;
191
192     grasshopper_zero128(&ctx->partial_buffer);
193 }
194
195 int gost_grasshopper_cipher_init(EVP_CIPHER_CTX *ctx,
196                                  const unsigned char *key,
197                                  const unsigned char *iv, int enc) {
198     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
199
200     if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
201         EVP_CIPHER_CTX_set_app_data(ctx,
202                                     EVP_CIPHER_CTX_get_cipher_data(ctx));
203     }
204
205     if (key != NULL) {
206         gost_grasshopper_cipher_key(c, key);
207     }
208
209     if (iv != NULL) {
210         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
211                EVP_CIPHER_CTX_iv_length(ctx));
212     }
213
214     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
215            EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
216
217     grasshopper_zero128(&c->buffer);
218
219     return 1;
220 }
221
222 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX *ctx,
223                                                         const unsigned char
224                                                         *key,
225                                                         const unsigned char
226                                                         *iv, int enc) {
227     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
228     c->type = GRASSHOPPER_CIPHER_ECB;
229     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
230 }
231
232 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX *ctx,
233                                                         const unsigned char
234                                                         *key,
235                                                         const unsigned char
236                                                         *iv, int enc) {
237     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
238     c->type = GRASSHOPPER_CIPHER_CBC;
239     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
240 }
241
242 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX *ctx,
243                                                         const unsigned char
244                                                         *key,
245                                                         const unsigned char
246                                                         *iv, int enc) {
247     gost_grasshopper_cipher_ctx_ofb *c =
248         EVP_CIPHER_CTX_get_cipher_data(ctx);
249
250     c->c.type = GRASSHOPPER_CIPHER_OFB;
251
252     grasshopper_zero128(&c->buffer1);
253
254     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
255 }
256
257 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX *ctx,
258                                                         const unsigned char
259                                                         *key,
260                                                         const unsigned char
261                                                         *iv, int enc) {
262     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
263     c->type = GRASSHOPPER_CIPHER_CFB;
264     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
265 }
266
267 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX *ctx,
268                                                         const unsigned char
269                                                         *key,
270                                                         const unsigned char
271                                                         *iv, int enc) {
272     gost_grasshopper_cipher_ctx_ctr *c =
273         EVP_CIPHER_CTX_get_cipher_data(ctx);
274
275     c->c.type = GRASSHOPPER_CIPHER_CTR;
276     EVP_CIPHER_CTX_set_num(ctx, 0);
277
278     grasshopper_zero128(&c->partial_buffer);
279
280     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
281 }
282
283 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctracpkm(EVP_CIPHER_CTX
284                                                              *ctx,
285                                                              const unsigned
286                                                              char *key,
287                                                              const unsigned
288                                                              char *iv,
289                                                              int enc) {
290     gost_grasshopper_cipher_ctx_ctr *c =
291         EVP_CIPHER_CTX_get_cipher_data(ctx);
292
293     /* NB: setting type makes EVP do_cipher callback useless */
294     c->c.type = GRASSHOPPER_CIPHER_CTRACPKM;
295     EVP_CIPHER_CTX_set_num(ctx, 0);
296     c->section_size = 4096;
297
298     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
299 }
300
301 GRASSHOPPER_INLINE int gost_grasshopper_cipher_do(EVP_CIPHER_CTX *ctx,
302                                                   unsigned char *out,
303                                                   const unsigned char *in,
304                                                   size_t inl) {
305     gost_grasshopper_cipher_ctx *c =
306         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
307     struct GRASSHOPPER_CIPHER_PARAMS *params = &gost_cipher_params[c->type];
308
309     return params->do_cipher(ctx, out, in, inl);
310 }
311
312 int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
313                                    const unsigned char *in, size_t inl) {
314     gost_grasshopper_cipher_ctx *c =
315         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
316     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
317     const unsigned char *current_in = in;
318     unsigned char *current_out = out;
319     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
320     size_t i;
321
322     for (i = 0; i < blocks;
323          i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
324          GRASSHOPPER_BLOCK_SIZE) {
325         if (encrypting) {
326             grasshopper_encrypt_block(&c->encrypt_round_keys,
327                                       (grasshopper_w128_t *) current_in,
328                                       (grasshopper_w128_t *) current_out,
329                                       &c->buffer);
330         } else {
331             grasshopper_decrypt_block(&c->decrypt_round_keys,
332                                       (grasshopper_w128_t *) current_in,
333                                       (grasshopper_w128_t *) current_out,
334                                       &c->buffer);
335         }
336     }
337
338     return 1;
339 }
340
341 int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
342                                    const unsigned char *in, size_t inl) {
343     gost_grasshopper_cipher_ctx *c =
344         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
345     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
346     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
347     const unsigned char *current_in = in;
348     unsigned char *current_out = out;
349     grasshopper_w128_t *currentInputBlock;
350     grasshopper_w128_t *currentOutputBlock;
351     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
352     size_t i;
353     grasshopper_w128_t *currentBlock;
354
355     currentBlock = (grasshopper_w128_t *) iv;
356
357     for (i = 0; i < blocks;
358          i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
359          GRASSHOPPER_BLOCK_SIZE) {
360         currentInputBlock = (grasshopper_w128_t *) current_in;
361         currentOutputBlock = (grasshopper_w128_t *) current_out;
362         if (encrypting) {
363             grasshopper_append128(currentBlock, currentInputBlock);
364             grasshopper_encrypt_block(&c->encrypt_round_keys, currentBlock,
365                                       currentOutputBlock, &c->buffer);
366             grasshopper_copy128(currentBlock, currentOutputBlock);
367         } else {
368             grasshopper_w128_t tmp;
369
370             grasshopper_copy128(&tmp, currentInputBlock);
371             grasshopper_decrypt_block(&c->decrypt_round_keys,
372                                       currentInputBlock, currentOutputBlock,
373                                       &c->buffer);
374             grasshopper_append128(currentOutputBlock, currentBlock);
375             grasshopper_copy128(currentBlock, &tmp);
376         }
377     }
378
379     return 1;
380 }
381
382 void inc_counter(unsigned char *counter, size_t counter_bytes) {
383     unsigned char c;
384     unsigned int n = counter_bytes;
385
386     do {
387         --n;
388         c = counter[n];
389         ++c;
390         counter[n] = c;
391         if (c)
392             return;
393     } while (n);
394 }
395
396 /* increment counter (128-bit int) by 1 */
397 static void ctr128_inc(unsigned char *counter) {
398     inc_counter(counter, 16);
399 }
400
401 int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
402                                    const unsigned char *in, size_t inl) {
403     gost_grasshopper_cipher_ctx_ctr *c =
404         (gost_grasshopper_cipher_ctx_ctr *)
405         EVP_CIPHER_CTX_get_cipher_data(ctx);
406     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
407     const unsigned char *current_in = in;
408     unsigned char *current_out = out;
409     grasshopper_w128_t *currentInputBlock;
410     grasshopper_w128_t *currentOutputBlock;
411     unsigned int n = EVP_CIPHER_CTX_num(ctx);
412     size_t lasted;
413     size_t i;
414
415     while (n && inl) {
416         *(current_out++) = *(current_in++) ^ c->partial_buffer.b[n];
417         --inl;
418         n = (n + 1) % GRASSHOPPER_BLOCK_SIZE;
419     }
420     EVP_CIPHER_CTX_set_num(ctx, n);
421     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
422
423     grasshopper_w128_t *iv_buffer = (grasshopper_w128_t *) iv;
424     grasshopper_w128_t tmp;
425
426     // full parts
427     for (i = 0; i < blocks; i++) {
428         currentInputBlock = (grasshopper_w128_t *) current_in;
429         currentOutputBlock = (grasshopper_w128_t *) current_out;
430         grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
431                                   &c->partial_buffer, &c->c.buffer);
432         grasshopper_plus128(&tmp, &c->partial_buffer, currentInputBlock);
433         grasshopper_copy128(currentOutputBlock, &tmp);
434         ctr128_inc(iv_buffer->b);
435         current_in += GRASSHOPPER_BLOCK_SIZE;
436         current_out += GRASSHOPPER_BLOCK_SIZE;
437     }
438
439     // last part
440     lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
441     if (lasted > 0) {
442         currentInputBlock = (grasshopper_w128_t *) current_in;
443         currentOutputBlock = (grasshopper_w128_t *) current_out;
444         grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
445                                   &c->partial_buffer, &c->c.buffer);
446         for (i = 0; i < lasted; i++) {
447             currentOutputBlock->b[i] =
448                 c->partial_buffer.b[i] ^ currentInputBlock->b[i];
449         }
450         EVP_CIPHER_CTX_set_num(ctx, i);
451         ctr128_inc(iv_buffer->b);
452     }
453
454     return 1;
455 }
456
457 #define GRASSHOPPER_BLOCK_MASK (GRASSHOPPER_BLOCK_SIZE - 1)
458 static inline void apply_acpkm_grasshopper(gost_grasshopper_cipher_ctx_ctr *
459                                            ctx, unsigned int *num) {
460     if (!ctx->section_size || (*num < ctx->section_size))
461         return;
462     acpkm_next(&ctx->c);
463     *num &= GRASSHOPPER_BLOCK_MASK;
464 }
465
466 /* If meshing is not configured via ctrl (setting section_size)
467  * this function works exactly like plain ctr */
468 int gost_grasshopper_cipher_do_ctracpkm(EVP_CIPHER_CTX *ctx,
469                                         unsigned char *out,
470                                         const unsigned char *in,
471                                         size_t inl) {
472     gost_grasshopper_cipher_ctx_ctr *c =
473         EVP_CIPHER_CTX_get_cipher_data(ctx);
474     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
475     unsigned int num = EVP_CIPHER_CTX_num(ctx);
476
477     while ((num & GRASSHOPPER_BLOCK_MASK) && inl) {
478         *out++ = *in++ ^ c->partial_buffer.b[num & GRASSHOPPER_BLOCK_MASK];
479         --inl;
480         num++;
481     }
482     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
483     size_t i;
484     grasshopper_w128_t tmp;
485
486     // full parts
487     for (i = 0; i < blocks; i++) {
488         apply_acpkm_grasshopper(c, &num);
489         grasshopper_encrypt_block(&c->c.encrypt_round_keys,
490                                   (grasshopper_w128_t *) iv,
491                                   (grasshopper_w128_t *) &c->partial_buffer, &c->c.buffer);
492         grasshopper_plus128(&tmp, &c->partial_buffer, (grasshopper_w128_t *) in);
493         grasshopper_copy128((grasshopper_w128_t *) out, &tmp);
494         ctr128_inc(iv);
495         in += GRASSHOPPER_BLOCK_SIZE;
496         out += GRASSHOPPER_BLOCK_SIZE;
497         num += GRASSHOPPER_BLOCK_SIZE;
498     }
499
500     // last part
501     size_t lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
502     if (lasted > 0) {
503         apply_acpkm_grasshopper(c, &num);
504         grasshopper_encrypt_block(&c->c.encrypt_round_keys,
505                                   (grasshopper_w128_t *) iv,
506                                   &c->partial_buffer, &c->c.buffer);
507         for (i = 0; i < lasted; i++)
508             out[i] = c->partial_buffer.b[i] ^ in[i];
509         ctr128_inc(iv);
510         num += lasted;
511     }
512     EVP_CIPHER_CTX_set_num(ctx, num);
513
514     return 1;
515 }
516
517 /*
518  * Fixed 128-bit IV implementation make shift regiser redundant.
519  */
520 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx_ofb * ctx,
521                                       grasshopper_w128_t * iv,
522                                       grasshopper_w128_t * buf) {
523     memcpy(&ctx->buffer1, iv, 16);
524     grasshopper_encrypt_block(&ctx->c.encrypt_round_keys, &ctx->buffer1,
525                               buf, &ctx->c.buffer);
526     memcpy(iv, buf, 16);
527 }
528
529 int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX *ctx, unsigned char *out,
530                                    const unsigned char *in, size_t inl) {
531     gost_grasshopper_cipher_ctx_ofb *c =
532         (gost_grasshopper_cipher_ctx_ofb *)
533         EVP_CIPHER_CTX_get_cipher_data(ctx);
534     const unsigned char *in_ptr = in;
535     unsigned char *out_ptr = out;
536     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
537     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
538     int num = EVP_CIPHER_CTX_num(ctx);
539     size_t i = 0;
540     size_t j;
541
542     /* process partial block if any */
543     if (num > 0) {
544         for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
545              j++, i++, in_ptr++, out_ptr++) {
546             *out_ptr = buf[j] ^ (*in_ptr);
547         }
548         if (j == GRASSHOPPER_BLOCK_SIZE) {
549             EVP_CIPHER_CTX_set_num(ctx, 0);
550         } else {
551             EVP_CIPHER_CTX_set_num(ctx, (int)j);
552             return 1;
553         }
554     }
555
556     for (; i + GRASSHOPPER_BLOCK_SIZE <
557          inl;
558          i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
559          GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
560         /*
561          * block cipher current iv
562          */
563         /* Encrypt */
564         gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
565                                   (grasshopper_w128_t *) buf);
566
567         /*
568          * xor next block of input text with it and output it
569          */
570         /*
571          * output this block
572          */
573         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
574             out_ptr[j] = buf[j] ^ in_ptr[j];
575         }
576     }
577
578     /* Process rest of buffer */
579     if (i < inl) {
580         gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
581                                   (grasshopper_w128_t *) buf);
582         for (j = 0; i < inl; j++, i++) {
583             out_ptr[j] = buf[j] ^ in_ptr[j];
584         }
585         EVP_CIPHER_CTX_set_num(ctx, (int)j);
586     } else {
587         EVP_CIPHER_CTX_set_num(ctx, 0);
588     }
589
590     return 1;
591 }
592
593 int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
594                                    const unsigned char *in, size_t inl) {
595     gost_grasshopper_cipher_ctx *c =
596         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
597     const unsigned char *in_ptr = in;
598     unsigned char *out_ptr = out;
599     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
600     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
601     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
602     int num = EVP_CIPHER_CTX_num(ctx);
603     size_t i = 0;
604     size_t j = 0;
605
606     /* process partial block if any */
607     if (num > 0) {
608         for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
609              j++, i++, in_ptr++, out_ptr++) {
610             if (!encrypting) {
611                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
612             }
613             *out_ptr = buf[j] ^ (*in_ptr);
614             if (encrypting) {
615                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
616             }
617         }
618         if (j == GRASSHOPPER_BLOCK_SIZE) {
619             memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE,
620                    GRASSHOPPER_BLOCK_SIZE);
621             EVP_CIPHER_CTX_set_num(ctx, 0);
622         } else {
623             EVP_CIPHER_CTX_set_num(ctx, (int)j);
624             return 1;
625         }
626     }
627
628     for (; i + GRASSHOPPER_BLOCK_SIZE <
629          inl;
630          i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
631          GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
632         /*
633          * block cipher current iv
634          */
635         grasshopper_encrypt_block(&c->encrypt_round_keys,
636                                   (grasshopper_w128_t *) iv,
637                                   (grasshopper_w128_t *) buf, &c->buffer);
638         /*
639          * xor next block of input text with it and output it
640          */
641         /*
642          * output this block
643          */
644         if (!encrypting) {
645             memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
646         }
647         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
648             out_ptr[j] = buf[j] ^ in_ptr[j];
649         }
650         /* Encrypt */
651         /* Next iv is next block of cipher text */
652         if (encrypting) {
653             memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
654         }
655     }
656
657     /* Process rest of buffer */
658     if (i < inl) {
659         grasshopper_encrypt_block(&c->encrypt_round_keys,
660                                   (grasshopper_w128_t *) iv,
661                                   (grasshopper_w128_t *) buf, &c->buffer);
662         if (!encrypting) {
663             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
664         }
665         for (j = 0; i < inl; j++, i++) {
666             out_ptr[j] = buf[j] ^ in_ptr[j];
667         }
668         EVP_CIPHER_CTX_set_num(ctx, (int)j);
669         if (encrypting) {
670             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
671         }
672     } else {
673         EVP_CIPHER_CTX_set_num(ctx, 0);
674     }
675
676     return 1;
677 }
678
679 int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX *ctx) {
680     gost_grasshopper_cipher_ctx *c =
681         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
682
683     if (!c)
684         return 1;
685
686     struct GRASSHOPPER_CIPHER_PARAMS *params = &gost_cipher_params[c->type];
687
688     gost_grasshopper_cipher_destroy(c);
689     if (params->destroy_cipher != NULL) {
690         params->destroy_cipher(c);
691     }
692
693     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
694
695     return 1;
696 }
697
698 int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX *ctx,
699                                          ASN1_TYPE *params) {
700     int len = 0;
701     unsigned char *buf = NULL;
702     ASN1_OCTET_STRING *os = NULL;
703
704     os = ASN1_OCTET_STRING_new();
705
706     if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
707         OPENSSL_free(buf);
708         GOSTerr(GOST_F_GOST_GRASSHOPPER_SET_ASN1_PARAMETERS,
709                 ERR_R_MALLOC_FAILURE);
710         return 0;
711     }
712     OPENSSL_free(buf);
713
714     ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
715     return 1;
716 }
717
718 GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX
719                                                             *ctx,
720                                                             ASN1_TYPE
721                                                             *params) {
722     int ret = -1;
723
724     if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
725         return ret;
726     }
727
728     return 1;
729 }
730
731 int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg,
732                                 void *ptr) {
733     switch (type) {
734     case EVP_CTRL_RAND_KEY:{
735             if (RAND_bytes
736                 ((unsigned char *)ptr,
737                  EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
738                 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL,
739                         GOST_R_RNG_ERROR);
740                 return -1;
741             }
742             break;
743         }
744     case EVP_CTRL_KEY_MESH:{
745             gost_grasshopper_cipher_ctx_ctr *c =
746                 EVP_CIPHER_CTX_get_cipher_data(ctx);
747             if (c->c.type != GRASSHOPPER_CIPHER_CTRACPKM || !arg
748                 || (arg % GRASSHOPPER_BLOCK_SIZE))
749                 return -1;
750             c->section_size = arg;
751             break;
752         }
753     default:
754         GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL,
755                 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
756         return -1;
757     }
758     return 1;
759 }
760
761 GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_create(int
762                                                               cipher_type,
763                                                               int
764                                                               block_size) {
765     return EVP_CIPHER_meth_new(cipher_type, block_size /* block_size */ ,
766                                GRASSHOPPER_KEY_SIZE /* key_size */ );
767 }
768
769 const int cipher_gost_grasshopper_setup(EVP_CIPHER *cipher, uint8_t mode,
770                                         int iv_size, bool padding) {
771     return EVP_CIPHER_meth_set_iv_length(cipher, iv_size)
772         && EVP_CIPHER_meth_set_flags(cipher,
773                                      (unsigned long)(mode |
774                                                      ((!padding) ?
775                                                       EVP_CIPH_NO_PADDING :
776                                                       0) | ((iv_size >
777                                                              0) ?
778                                                             EVP_CIPH_CUSTOM_IV
779                                                             : 0) |
780                                                      EVP_CIPH_RAND_KEY |
781                                                      EVP_CIPH_ALWAYS_CALL_INIT)
782         )
783         && EVP_CIPHER_meth_set_cleanup(cipher,
784                                        gost_grasshopper_cipher_cleanup)
785         && EVP_CIPHER_meth_set_set_asn1_params(cipher,
786                                                gost_grasshopper_set_asn1_parameters)
787         && EVP_CIPHER_meth_set_get_asn1_params(cipher,
788                                                gost_grasshopper_get_asn1_parameters)
789         && EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl)
790         && EVP_CIPHER_meth_set_do_cipher(cipher,
791                                          gost_grasshopper_cipher_do);
792 }
793
794 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper(uint8_t mode,
795                                                              uint8_t num) {
796     EVP_CIPHER **cipher;
797     struct GRASSHOPPER_CIPHER_PARAMS *params;
798
799     cipher = &gost_grasshopper_ciphers[num];
800
801     if (*cipher == NULL) {
802         params = &gost_cipher_params[num];
803
804         int nid = params->nid;
805         grasshopper_init_cipher_func init_cipher = params->init_cipher;
806         int block_size = params->block_size;
807         int ctx_size = params->ctx_size;
808         int iv_size = params->iv_size;
809         bool padding = params->padding;
810
811         *cipher = cipher_gost_grasshopper_create(nid, block_size);
812         if (*cipher == NULL) {
813             return NULL;
814         }
815
816         if (!cipher_gost_grasshopper_setup(*cipher, mode, iv_size, padding)
817             || !EVP_CIPHER_meth_set_init(*cipher, init_cipher)
818             || !EVP_CIPHER_meth_set_impl_ctx_size(*cipher, ctx_size)) {
819             EVP_CIPHER_meth_free(*cipher);
820             *cipher = NULL;
821         }
822     }
823
824     return *cipher;
825 }
826
827 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ecb() {
828     return cipher_gost_grasshopper(EVP_CIPH_ECB_MODE,
829                                    GRASSHOPPER_CIPHER_ECB);
830 }
831
832 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_cbc() {
833     return cipher_gost_grasshopper(EVP_CIPH_CBC_MODE,
834                                    GRASSHOPPER_CIPHER_CBC);
835 }
836
837 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ofb() {
838     return cipher_gost_grasshopper(EVP_CIPH_OFB_MODE,
839                                    GRASSHOPPER_CIPHER_OFB);
840 }
841
842 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_cfb() {
843     return cipher_gost_grasshopper(EVP_CIPH_CFB_MODE,
844                                    GRASSHOPPER_CIPHER_CFB);
845 }
846
847 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctr() {
848     return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE,
849                                    GRASSHOPPER_CIPHER_CTR);
850 }
851
852 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctracpkm() {
853     return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE,
854                                    GRASSHOPPER_CIPHER_CTRACPKM);
855 }
856
857 void cipher_gost_grasshopper_destroy(void) {
858     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB]);
859     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB] = NULL;
860     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC]);
861     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC] = NULL;
862     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB]);
863     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB] = NULL;
864     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB]);
865     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB] = NULL;
866     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR]);
867     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR] = NULL;
868     EVP_CIPHER_meth_free(gost_grasshopper_ciphers
869                          [GRASSHOPPER_CIPHER_CTRACPKM]);
870     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKM] = NULL;
871 }
872
873 #if defined(__cplusplus)
874 }
875 #endif