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