]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_grasshopper_cipher.c
TLSTREE ctrl
[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
160     grasshopper_set_encrypt_key(&c->encrypt_round_keys, &c->key);
161     grasshopper_set_decrypt_key(&c->decrypt_round_keys, &c->key);
162 }
163
164 /* Set master 256-bit key to be used in TLSTREE calculation into context */
165 GRASSHOPPER_INLINE void
166     gost_grasshopper_master_key(gost_grasshopper_cipher_ctx * c,
167                                 const uint8_t *k) {
168     int i;
169     for (i = 0; i < 2; i++) {
170         grasshopper_copy128(&c->master_key.k.k[i],
171                             (const grasshopper_w128_t *)(k + i * 16));
172     }
173 }
174
175 /* Cleans up key from context */
176 GRASSHOPPER_INLINE void
177     gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx * c) {
178     int i;
179     for (i = 0; i < 2; i++) {
180         grasshopper_zero128(&c->key.k.k[i]);
181                                 grasshopper_zero128(&c->master_key.k.k[i]);
182     }
183     for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
184         grasshopper_zero128(&c->encrypt_round_keys.k[i]);
185     }
186     for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
187         grasshopper_zero128(&c->decrypt_round_keys.k[i]);
188     }
189     grasshopper_zero128(&c->buffer);
190 }
191
192 static GRASSHOPPER_INLINE void
193     gost_grasshopper_cipher_destroy_ofb(gost_grasshopper_cipher_ctx * c) {
194     gost_grasshopper_cipher_ctx_ofb *ctx =
195         (gost_grasshopper_cipher_ctx_ofb *) c;
196
197     grasshopper_zero128(&ctx->buffer1);
198 }
199
200 static GRASSHOPPER_INLINE void
201     gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx * c) {
202     gost_grasshopper_cipher_ctx_ctr *ctx =
203         (gost_grasshopper_cipher_ctx_ctr *) c;
204
205     grasshopper_zero128(&ctx->partial_buffer);
206 }
207
208 int gost_grasshopper_cipher_init(EVP_CIPHER_CTX *ctx,
209                                  const unsigned char *key,
210                                  const unsigned char *iv, int enc) {
211     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
212
213     if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
214         EVP_CIPHER_CTX_set_app_data(ctx,
215                                     EVP_CIPHER_CTX_get_cipher_data(ctx));
216     }
217
218     if (key != NULL) {
219         gost_grasshopper_cipher_key(c, key);
220         gost_grasshopper_master_key(c, key);
221     }
222
223     if (iv != NULL) {
224         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
225                EVP_CIPHER_CTX_iv_length(ctx));
226     }
227
228     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
229            EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
230
231     grasshopper_zero128(&c->buffer);
232
233     return 1;
234 }
235
236 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX *ctx,
237                                                         const unsigned char
238                                                         *key,
239                                                         const unsigned char
240                                                         *iv, int enc) {
241     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
242     c->type = GRASSHOPPER_CIPHER_ECB;
243     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
244 }
245
246 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX *ctx,
247                                                         const unsigned char
248                                                         *key,
249                                                         const unsigned char
250                                                         *iv, int enc) {
251     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
252     c->type = GRASSHOPPER_CIPHER_CBC;
253     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
254 }
255
256 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX *ctx,
257                                                         const unsigned char
258                                                         *key,
259                                                         const unsigned char
260                                                         *iv, int enc) {
261     gost_grasshopper_cipher_ctx_ofb *c =
262         EVP_CIPHER_CTX_get_cipher_data(ctx);
263
264     c->c.type = GRASSHOPPER_CIPHER_OFB;
265
266     grasshopper_zero128(&c->buffer1);
267
268     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
269 }
270
271 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX *ctx,
272                                                         const unsigned char
273                                                         *key,
274                                                         const unsigned char
275                                                         *iv, int enc) {
276     gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
277     c->type = GRASSHOPPER_CIPHER_CFB;
278     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
279 }
280
281 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX *ctx,
282                                                         const unsigned char
283                                                         *key,
284                                                         const unsigned char
285                                                         *iv, int enc) {
286     gost_grasshopper_cipher_ctx_ctr *c =
287         EVP_CIPHER_CTX_get_cipher_data(ctx);
288
289     c->c.type = GRASSHOPPER_CIPHER_CTR;
290     EVP_CIPHER_CTX_set_num(ctx, 0);
291
292     grasshopper_zero128(&c->partial_buffer);
293
294     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
295 }
296
297 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctracpkm(EVP_CIPHER_CTX
298                                                              *ctx,
299                                                              const unsigned
300                                                              char *key,
301                                                              const unsigned
302                                                              char *iv,
303                                                              int enc) {
304     gost_grasshopper_cipher_ctx_ctr *c =
305         EVP_CIPHER_CTX_get_cipher_data(ctx);
306
307     /* NB: setting type makes EVP do_cipher callback useless */
308     c->c.type = GRASSHOPPER_CIPHER_CTRACPKM;
309     EVP_CIPHER_CTX_set_num(ctx, 0);
310     c->section_size = 4096;
311
312     return gost_grasshopper_cipher_init(ctx, key, iv, enc);
313 }
314
315 GRASSHOPPER_INLINE int gost_grasshopper_cipher_do(EVP_CIPHER_CTX *ctx,
316                                                   unsigned char *out,
317                                                   const unsigned char *in,
318                                                   size_t inl) {
319     gost_grasshopper_cipher_ctx *c =
320         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
321     struct GRASSHOPPER_CIPHER_PARAMS *params = &gost_cipher_params[c->type];
322
323     return params->do_cipher(ctx, out, in, inl);
324 }
325
326 int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
327                                    const unsigned char *in, size_t inl) {
328     gost_grasshopper_cipher_ctx *c =
329         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
330     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
331     const unsigned char *current_in = in;
332     unsigned char *current_out = out;
333     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
334     size_t i;
335
336     for (i = 0; i < blocks;
337          i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
338          GRASSHOPPER_BLOCK_SIZE) {
339         if (encrypting) {
340             grasshopper_encrypt_block(&c->encrypt_round_keys,
341                                       (grasshopper_w128_t *) current_in,
342                                       (grasshopper_w128_t *) current_out,
343                                       &c->buffer);
344         } else {
345             grasshopper_decrypt_block(&c->decrypt_round_keys,
346                                       (grasshopper_w128_t *) current_in,
347                                       (grasshopper_w128_t *) current_out,
348                                       &c->buffer);
349         }
350     }
351
352     return 1;
353 }
354
355 int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
356                                    const unsigned char *in, size_t inl) {
357     gost_grasshopper_cipher_ctx *c =
358         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
359     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
360     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
361     const unsigned char *current_in = in;
362     unsigned char *current_out = out;
363     grasshopper_w128_t *currentInputBlock;
364     grasshopper_w128_t *currentOutputBlock;
365     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
366     size_t i;
367     grasshopper_w128_t *currentBlock;
368
369     currentBlock = (grasshopper_w128_t *) iv;
370
371     for (i = 0; i < blocks;
372          i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
373          GRASSHOPPER_BLOCK_SIZE) {
374         currentInputBlock = (grasshopper_w128_t *) current_in;
375         currentOutputBlock = (grasshopper_w128_t *) current_out;
376         if (encrypting) {
377             grasshopper_append128(currentBlock, currentInputBlock);
378             grasshopper_encrypt_block(&c->encrypt_round_keys, currentBlock,
379                                       currentOutputBlock, &c->buffer);
380             grasshopper_copy128(currentBlock, currentOutputBlock);
381         } else {
382             grasshopper_w128_t tmp;
383
384             grasshopper_copy128(&tmp, currentInputBlock);
385             grasshopper_decrypt_block(&c->decrypt_round_keys,
386                                       currentInputBlock, currentOutputBlock,
387                                       &c->buffer);
388             grasshopper_append128(currentOutputBlock, currentBlock);
389             grasshopper_copy128(currentBlock, &tmp);
390         }
391     }
392
393     return 1;
394 }
395
396 void inc_counter(unsigned char *counter, size_t counter_bytes) {
397     unsigned char c;
398     unsigned int n = counter_bytes;
399
400     do {
401         --n;
402         c = counter[n];
403         ++c;
404         counter[n] = c;
405         if (c)
406             return;
407     } while (n);
408 }
409
410 /* increment counter (128-bit int) by 1 */
411 static void ctr128_inc(unsigned char *counter) {
412     inc_counter(counter, 16);
413 }
414
415 int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
416                                    const unsigned char *in, size_t inl) {
417     gost_grasshopper_cipher_ctx_ctr *c =
418         (gost_grasshopper_cipher_ctx_ctr *)
419         EVP_CIPHER_CTX_get_cipher_data(ctx);
420     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
421     const unsigned char *current_in = in;
422     unsigned char *current_out = out;
423     grasshopper_w128_t *currentInputBlock;
424     grasshopper_w128_t *currentOutputBlock;
425     unsigned int n = EVP_CIPHER_CTX_num(ctx);
426     size_t lasted;
427     size_t i;
428
429     while (n && inl) {
430         *(current_out++) = *(current_in++) ^ c->partial_buffer.b[n];
431         --inl;
432         n = (n + 1) % GRASSHOPPER_BLOCK_SIZE;
433     }
434     EVP_CIPHER_CTX_set_num(ctx, n);
435     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
436
437     grasshopper_w128_t *iv_buffer = (grasshopper_w128_t *) iv;
438     grasshopper_w128_t tmp;
439
440     // full parts
441     for (i = 0; i < blocks; i++) {
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         grasshopper_plus128(&tmp, &c->partial_buffer, currentInputBlock);
447         grasshopper_copy128(currentOutputBlock, &tmp);
448         ctr128_inc(iv_buffer->b);
449         current_in += GRASSHOPPER_BLOCK_SIZE;
450         current_out += GRASSHOPPER_BLOCK_SIZE;
451     }
452
453     // last part
454     lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
455     if (lasted > 0) {
456         currentInputBlock = (grasshopper_w128_t *) current_in;
457         currentOutputBlock = (grasshopper_w128_t *) current_out;
458         grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
459                                   &c->partial_buffer, &c->c.buffer);
460         for (i = 0; i < lasted; i++) {
461             currentOutputBlock->b[i] =
462                 c->partial_buffer.b[i] ^ currentInputBlock->b[i];
463         }
464         EVP_CIPHER_CTX_set_num(ctx, i);
465         ctr128_inc(iv_buffer->b);
466     }
467
468     return 1;
469 }
470
471 #define GRASSHOPPER_BLOCK_MASK (GRASSHOPPER_BLOCK_SIZE - 1)
472 static inline void apply_acpkm_grasshopper(gost_grasshopper_cipher_ctx_ctr *
473                                            ctx, unsigned int *num) {
474     if (!ctx->section_size || (*num < ctx->section_size))
475         return;
476     acpkm_next(&ctx->c);
477     *num &= GRASSHOPPER_BLOCK_MASK;
478 }
479
480 /* If meshing is not configured via ctrl (setting section_size)
481  * this function works exactly like plain ctr */
482 int gost_grasshopper_cipher_do_ctracpkm(EVP_CIPHER_CTX *ctx,
483                                         unsigned char *out,
484                                         const unsigned char *in,
485                                         size_t inl) {
486     gost_grasshopper_cipher_ctx_ctr *c =
487         EVP_CIPHER_CTX_get_cipher_data(ctx);
488     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
489     unsigned int num = EVP_CIPHER_CTX_num(ctx);
490
491     while ((num & GRASSHOPPER_BLOCK_MASK) && inl) {
492         *out++ = *in++ ^ c->partial_buffer.b[num & GRASSHOPPER_BLOCK_MASK];
493         --inl;
494         num++;
495     }
496     size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
497     size_t i;
498     grasshopper_w128_t tmp;
499
500     // full parts
501     for (i = 0; i < blocks; i++) {
502         apply_acpkm_grasshopper(c, &num);
503         grasshopper_encrypt_block(&c->c.encrypt_round_keys,
504                                   (grasshopper_w128_t *) iv,
505                                   (grasshopper_w128_t *) &c->partial_buffer, &c->c.buffer);
506         grasshopper_plus128(&tmp, &c->partial_buffer, (grasshopper_w128_t *) in);
507         grasshopper_copy128((grasshopper_w128_t *) out, &tmp);
508         ctr128_inc(iv);
509         in += GRASSHOPPER_BLOCK_SIZE;
510         out += GRASSHOPPER_BLOCK_SIZE;
511         num += GRASSHOPPER_BLOCK_SIZE;
512     }
513
514     // last part
515     size_t lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
516     if (lasted > 0) {
517         apply_acpkm_grasshopper(c, &num);
518         grasshopper_encrypt_block(&c->c.encrypt_round_keys,
519                                   (grasshopper_w128_t *) iv,
520                                   &c->partial_buffer, &c->c.buffer);
521         for (i = 0; i < lasted; i++)
522             out[i] = c->partial_buffer.b[i] ^ in[i];
523         ctr128_inc(iv);
524         num += lasted;
525     }
526     EVP_CIPHER_CTX_set_num(ctx, num);
527
528     return 1;
529 }
530
531 /*
532  * Fixed 128-bit IV implementation make shift regiser redundant.
533  */
534 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx_ofb * ctx,
535                                       grasshopper_w128_t * iv,
536                                       grasshopper_w128_t * buf) {
537     memcpy(&ctx->buffer1, iv, 16);
538     grasshopper_encrypt_block(&ctx->c.encrypt_round_keys, &ctx->buffer1,
539                               buf, &ctx->c.buffer);
540     memcpy(iv, buf, 16);
541 }
542
543 int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX *ctx, unsigned char *out,
544                                    const unsigned char *in, size_t inl) {
545     gost_grasshopper_cipher_ctx_ofb *c =
546         (gost_grasshopper_cipher_ctx_ofb *)
547         EVP_CIPHER_CTX_get_cipher_data(ctx);
548     const unsigned char *in_ptr = in;
549     unsigned char *out_ptr = out;
550     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
551     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
552     int num = EVP_CIPHER_CTX_num(ctx);
553     size_t i = 0;
554     size_t j;
555
556     /* process partial block if any */
557     if (num > 0) {
558         for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
559              j++, i++, in_ptr++, out_ptr++) {
560             *out_ptr = buf[j] ^ (*in_ptr);
561         }
562         if (j == GRASSHOPPER_BLOCK_SIZE) {
563             EVP_CIPHER_CTX_set_num(ctx, 0);
564         } else {
565             EVP_CIPHER_CTX_set_num(ctx, (int)j);
566             return 1;
567         }
568     }
569
570     for (; i + GRASSHOPPER_BLOCK_SIZE <
571          inl;
572          i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
573          GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
574         /*
575          * block cipher current iv
576          */
577         /* Encrypt */
578         gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
579                                   (grasshopper_w128_t *) buf);
580
581         /*
582          * xor next block of input text with it and output it
583          */
584         /*
585          * output this block
586          */
587         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
588             out_ptr[j] = buf[j] ^ in_ptr[j];
589         }
590     }
591
592     /* Process rest of buffer */
593     if (i < inl) {
594         gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
595                                   (grasshopper_w128_t *) buf);
596         for (j = 0; i < inl; j++, i++) {
597             out_ptr[j] = buf[j] ^ in_ptr[j];
598         }
599         EVP_CIPHER_CTX_set_num(ctx, (int)j);
600     } else {
601         EVP_CIPHER_CTX_set_num(ctx, 0);
602     }
603
604     return 1;
605 }
606
607 int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
608                                    const unsigned char *in, size_t inl) {
609     gost_grasshopper_cipher_ctx *c =
610         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
611     const unsigned char *in_ptr = in;
612     unsigned char *out_ptr = out;
613     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
614     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
615     bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
616     int num = EVP_CIPHER_CTX_num(ctx);
617     size_t i = 0;
618     size_t j = 0;
619
620     /* process partial block if any */
621     if (num > 0) {
622         for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
623              j++, i++, in_ptr++, out_ptr++) {
624             if (!encrypting) {
625                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
626             }
627             *out_ptr = buf[j] ^ (*in_ptr);
628             if (encrypting) {
629                 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
630             }
631         }
632         if (j == GRASSHOPPER_BLOCK_SIZE) {
633             memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE,
634                    GRASSHOPPER_BLOCK_SIZE);
635             EVP_CIPHER_CTX_set_num(ctx, 0);
636         } else {
637             EVP_CIPHER_CTX_set_num(ctx, (int)j);
638             return 1;
639         }
640     }
641
642     for (; i + GRASSHOPPER_BLOCK_SIZE <
643          inl;
644          i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
645          GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
646         /*
647          * block cipher current iv
648          */
649         grasshopper_encrypt_block(&c->encrypt_round_keys,
650                                   (grasshopper_w128_t *) iv,
651                                   (grasshopper_w128_t *) buf, &c->buffer);
652         /*
653          * xor next block of input text with it and output it
654          */
655         /*
656          * output this block
657          */
658         if (!encrypting) {
659             memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
660         }
661         for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
662             out_ptr[j] = buf[j] ^ in_ptr[j];
663         }
664         /* Encrypt */
665         /* Next iv is next block of cipher text */
666         if (encrypting) {
667             memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
668         }
669     }
670
671     /* Process rest of buffer */
672     if (i < inl) {
673         grasshopper_encrypt_block(&c->encrypt_round_keys,
674                                   (grasshopper_w128_t *) iv,
675                                   (grasshopper_w128_t *) buf, &c->buffer);
676         if (!encrypting) {
677             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
678         }
679         for (j = 0; i < inl; j++, i++) {
680             out_ptr[j] = buf[j] ^ in_ptr[j];
681         }
682         EVP_CIPHER_CTX_set_num(ctx, (int)j);
683         if (encrypting) {
684             memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
685         }
686     } else {
687         EVP_CIPHER_CTX_set_num(ctx, 0);
688     }
689
690     return 1;
691 }
692
693 int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX *ctx) {
694     gost_grasshopper_cipher_ctx *c =
695         (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
696
697     if (!c)
698         return 1;
699
700     struct GRASSHOPPER_CIPHER_PARAMS *params = &gost_cipher_params[c->type];
701
702     gost_grasshopper_cipher_destroy(c);
703     if (params->destroy_cipher != NULL) {
704         params->destroy_cipher(c);
705     }
706
707     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
708
709     return 1;
710 }
711
712 int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX *ctx,
713                                          ASN1_TYPE *params) {
714     int len = 0;
715     unsigned char *buf = NULL;
716     ASN1_OCTET_STRING *os = NULL;
717
718     os = ASN1_OCTET_STRING_new();
719
720     if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
721         OPENSSL_free(buf);
722         GOSTerr(GOST_F_GOST_GRASSHOPPER_SET_ASN1_PARAMETERS,
723                 ERR_R_MALLOC_FAILURE);
724         return 0;
725     }
726     OPENSSL_free(buf);
727
728     ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
729     return 1;
730 }
731
732 GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX
733                                                             *ctx,
734                                                             ASN1_TYPE
735                                                             *params) {
736     int ret = -1;
737
738     if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
739         return ret;
740     }
741
742     return 1;
743 }
744
745 int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg,
746                                 void *ptr) {
747     switch (type) {
748     case EVP_CTRL_RAND_KEY:{
749             if (RAND_bytes
750                 ((unsigned char *)ptr,
751                  EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
752                 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL,
753                         GOST_R_RNG_ERROR);
754                 return -1;
755             }
756             break;
757         }
758     case EVP_CTRL_KEY_MESH:{
759             gost_grasshopper_cipher_ctx_ctr *c =
760                 EVP_CIPHER_CTX_get_cipher_data(ctx);
761             if (c->c.type != GRASSHOPPER_CIPHER_CTRACPKM || !arg
762                 || (arg % GRASSHOPPER_BLOCK_SIZE))
763                 return -1;
764             c->section_size = arg;
765             break;
766         }
767 #ifdef EVP_CTRL_TLS1_2_TLSTREE
768     case EVP_CTRL_TLS1_2_TLSTREE:
769                         {
770                                 unsigned char newkey[32];
771                                 int mode = EVP_CIPHER_CTX_mode(ctx);
772                                 gost_grasshopper_cipher_ctx_ctr *ctr_ctx = NULL;
773                                 gost_grasshopper_cipher_ctx *c = NULL;
774
775                                 if (mode != EVP_CIPH_CTR_MODE)
776                                         return -1;
777
778                                 ctr_ctx =
779                                 (gost_grasshopper_cipher_ctx_ctr *) EVP_CIPHER_CTX_get_cipher_data(ctx);
780                                 c = &(ctr_ctx->c);
781
782                                 if (gost_tlstree(NID_grasshopper_cbc, c->master_key.k.b, newkey,
783                                                                 (const unsigned char *)ptr) )
784                                         {
785                                                 gost_grasshopper_cipher_key(c, newkey);
786                                                 return 1;
787                                         }
788                         }
789     return -1;
790 #endif
791     default:
792         GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL,
793                 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
794         return -1;
795     }
796     return 1;
797 }
798
799 GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_create(int
800                                                               cipher_type,
801                                                               int
802                                                               block_size) {
803     return EVP_CIPHER_meth_new(cipher_type, block_size /* block_size */ ,
804                                GRASSHOPPER_KEY_SIZE /* key_size */ );
805 }
806
807 const int cipher_gost_grasshopper_setup(EVP_CIPHER *cipher, uint8_t mode,
808                                         int iv_size, bool padding) {
809     return EVP_CIPHER_meth_set_iv_length(cipher, iv_size)
810         && EVP_CIPHER_meth_set_flags(cipher,
811                                      (unsigned long)(mode |
812                                                      ((!padding) ?
813                                                       EVP_CIPH_NO_PADDING :
814                                                       0) | ((iv_size >
815                                                              0) ?
816                                                             EVP_CIPH_CUSTOM_IV
817                                                             : 0) |
818                                                      EVP_CIPH_RAND_KEY |
819                                                      EVP_CIPH_ALWAYS_CALL_INIT)
820         )
821         && EVP_CIPHER_meth_set_cleanup(cipher,
822                                        gost_grasshopper_cipher_cleanup)
823         && EVP_CIPHER_meth_set_set_asn1_params(cipher,
824                                                gost_grasshopper_set_asn1_parameters)
825         && EVP_CIPHER_meth_set_get_asn1_params(cipher,
826                                                gost_grasshopper_get_asn1_parameters)
827         && EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl)
828         && EVP_CIPHER_meth_set_do_cipher(cipher,
829                                          gost_grasshopper_cipher_do);
830 }
831
832 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper(uint8_t mode,
833                                                              uint8_t num) {
834     EVP_CIPHER **cipher;
835     struct GRASSHOPPER_CIPHER_PARAMS *params;
836
837     cipher = &gost_grasshopper_ciphers[num];
838
839     if (*cipher == NULL) {
840         params = &gost_cipher_params[num];
841
842         int nid = params->nid;
843         grasshopper_init_cipher_func init_cipher = params->init_cipher;
844         int block_size = params->block_size;
845         int ctx_size = params->ctx_size;
846         int iv_size = params->iv_size;
847         bool padding = params->padding;
848
849         *cipher = cipher_gost_grasshopper_create(nid, block_size);
850         if (*cipher == NULL) {
851             return NULL;
852         }
853
854         if (!cipher_gost_grasshopper_setup(*cipher, mode, iv_size, padding)
855             || !EVP_CIPHER_meth_set_init(*cipher, init_cipher)
856             || !EVP_CIPHER_meth_set_impl_ctx_size(*cipher, ctx_size)) {
857             EVP_CIPHER_meth_free(*cipher);
858             *cipher = NULL;
859         }
860     }
861
862     return *cipher;
863 }
864
865 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ecb() {
866     return cipher_gost_grasshopper(EVP_CIPH_ECB_MODE,
867                                    GRASSHOPPER_CIPHER_ECB);
868 }
869
870 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_cbc() {
871     return cipher_gost_grasshopper(EVP_CIPH_CBC_MODE,
872                                    GRASSHOPPER_CIPHER_CBC);
873 }
874
875 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ofb() {
876     return cipher_gost_grasshopper(EVP_CIPH_OFB_MODE,
877                                    GRASSHOPPER_CIPHER_OFB);
878 }
879
880 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_cfb() {
881     return cipher_gost_grasshopper(EVP_CIPH_CFB_MODE,
882                                    GRASSHOPPER_CIPHER_CFB);
883 }
884
885 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctr() {
886     return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE,
887                                    GRASSHOPPER_CIPHER_CTR);
888 }
889
890 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctracpkm() {
891     return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE,
892                                    GRASSHOPPER_CIPHER_CTRACPKM);
893 }
894
895 void cipher_gost_grasshopper_destroy(void) {
896     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB]);
897     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB] = NULL;
898     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC]);
899     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC] = NULL;
900     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB]);
901     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB] = NULL;
902     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB]);
903     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB] = NULL;
904     EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR]);
905     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR] = NULL;
906     EVP_CIPHER_meth_free(gost_grasshopper_ciphers
907                          [GRASSHOPPER_CIPHER_CTRACPKM]);
908     gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKM] = NULL;
909 }
910
911 #if defined(__cplusplus)
912 }
913 #endif