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