Adapt cipher implementations to opaque EVP_CIPHER_CTX
[openssl.git] / engines / ccgost / gost_crypt.c
1 /**********************************************************************
2  *                          gost_crypt.c                              *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *       OpenSSL interface to GOST 28147-89 cipher functions          *
7  *          Requires OpenSSL 0.9.9 for compilation                    *
8  **********************************************************************/
9 #include <string.h>
10 #include "gost89.h"
11 #include <openssl/err.h>
12 #include <openssl/rand.h>
13 #include "e_gost_err.h"
14 #include "gost_lcl.h"
15
16 #if !defined(CCGOST_DEBUG) && !defined(DEBUG)
17 # ifndef NDEBUG
18 #  define NDEBUG
19 # endif
20 #endif
21 #include <assert.h>
22
23 static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
24                             const unsigned char *iv, int enc);
25 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
26                                 const unsigned char *iv, int enc);
27 /* Handles block of data in CFB mode */
28 static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
29                               const unsigned char *in, size_t inl);
30 /* Handles block of data in CNT mode */
31 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
32                               const unsigned char *in, size_t inl);
33 /* Cleanup function */
34 static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
35 /* set/get cipher parameters */
36 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
37 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
38 /* Control function */
39 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
40
41 EVP_CIPHER cipher_gost = {
42     NID_id_Gost28147_89,
43     1,                          /* block_size */
44     32,                         /* key_size */
45     8,                          /* iv_len */
46     EVP_CIPH_CFB_MODE | EVP_CIPH_NO_PADDING |
47         EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
48     gost_cipher_init,
49     gost_cipher_do_cfb,
50     gost_cipher_cleanup,
51     sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
52     gost89_set_asn1_parameters,
53     gost89_get_asn1_parameters,
54     gost_cipher_ctl,
55     NULL,
56 };
57
58 EVP_CIPHER cipher_gost_cpacnt = {
59     NID_gost89_cnt,
60     1,                          /* block_size */
61     32,                         /* key_size */
62     8,                          /* iv_len */
63     EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
64         EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
65     gost_cipher_init_cpa,
66     gost_cipher_do_cnt,
67     gost_cipher_cleanup,
68     sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
69     gost89_set_asn1_parameters,
70     gost89_get_asn1_parameters,
71     gost_cipher_ctl,
72     NULL,
73 };
74
75 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
76 /* Init functions which set specific parameters */
77 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
78 /* process block of data */
79 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
80 /* Return computed value */
81 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
82 /* Copies context */
83 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
84 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
85 /* Control function, knows how to set MAC key.*/
86 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
87
88 static EVP_MD *_hidden_Gost28147_89_MAC_md = NULL;
89 EVP_MD *imit_gost_cpa(void)
90 {
91
92     if (_hidden_Gost28147_89_MAC_md == NULL) {
93         EVP_MD *md;
94
95         if ((md = EVP_MD_meth_new(NID_id_Gost28147_89_MAC, NID_undef)) == NULL
96             || !EVP_MD_meth_set_result_size(md, 4)
97             || !EVP_MD_meth_set_input_blocksize(md, 8)
98             || !EVP_MD_meth_set_app_datasize(md,
99                                              sizeof(struct ossl_gost_imit_ctx))
100             || !EVP_MD_meth_set_flags(md, 0)
101             || !EVP_MD_meth_set_init(md, gost_imit_init_cpa)
102             || !EVP_MD_meth_set_update(md, gost_imit_update)
103             || !EVP_MD_meth_set_final(md, gost_imit_final)
104             || !EVP_MD_meth_set_copy(md, gost_imit_copy)
105             || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
106             || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
107             EVP_MD_meth_free(md);
108             md = NULL;
109         }
110         _hidden_Gost28147_89_MAC_md = md;
111     }
112     return _hidden_Gost28147_89_MAC_md;
113 }
114 void imit_gost_cpa_destroy(void)
115 {
116     EVP_MD_meth_free(_hidden_Gost28147_89_MAC_md);
117     _hidden_Gost28147_89_MAC_md = NULL;
118 }
119
120 /*
121  * Correspondence between gost parameter OIDs and substitution blocks
122  * NID field is filed by register_gost_NID function in engine.c
123  * upon engine initialization
124  */
125
126 struct gost_cipher_info gost_cipher_list[] = {
127     /*- NID *//*
128      * Subst block
129      *//*
130      * Key meshing
131      */
132     /*
133      * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
134      */
135     {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
136      1},
137     {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
138      1},
139     {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
140      1},
141     {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
142      1},
143     {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
144     {NID_undef, NULL, 0}
145 };
146
147 /*
148  * get encryption parameters from crypto network settings FIXME For now we
149  * use environment var CRYPT_PARAMS as place to store these settings.
150  * Actually, it is better to use engine control command, read from
151  * configuration file to set them
152  */
153 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
154 {
155     int nid;
156     struct gost_cipher_info *param;
157     if (!obj) {
158         const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
159         if (!params || !strlen(params))
160             return &gost_cipher_list[1];
161
162         nid = OBJ_txt2nid(params);
163         if (nid == NID_undef) {
164             GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
165                     GOST_R_INVALID_CIPHER_PARAM_OID);
166             return NULL;
167         }
168     } else {
169         nid = OBJ_obj2nid(obj);
170     }
171     for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
172          param++) ;
173     if (!param->sblock) {
174         GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
175         return NULL;
176     }
177     return param;
178 }
179
180 /* Sets cipher param from paramset NID. */
181 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
182 {
183     const struct gost_cipher_info *param;
184     param =
185         get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
186     if (!param)
187         return 0;
188
189     c->paramNID = param->nid;
190     c->key_meshing = param->key_meshing;
191     c->count = 0;
192     gost_init(&(c->cctx), param->sblock);
193     return 1;
194 }
195
196 /* Initializes EVP_CIPHER_CTX by paramset NID */
197 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
198                                   const unsigned char *key,
199                                   const unsigned char *iv, int enc,
200                                   int paramNID, int mode)
201 {
202     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_cipher_data(ctx);
203     if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
204         if (!gost_cipher_set_param(c, paramNID))
205             return 0;
206         EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_cipher_data(ctx));
207     }
208     if (key)
209         gost_key(&(c->cctx), key);
210     if (iv)
211         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
212                EVP_CIPHER_CTX_iv_length(ctx));
213     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
214            EVP_CIPHER_CTX_original_iv(ctx),
215            EVP_CIPHER_CTX_iv_length(ctx));
216     return 1;
217 }
218
219 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
220                                 const unsigned char *iv, int enc)
221 {
222     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_cipher_data(ctx);
223     gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
224     c->key_meshing = 1;
225     c->count = 0;
226     if (key)
227         gost_key(&(c->cctx), key);
228     if (iv)
229         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
230                EVP_CIPHER_CTX_iv_length(ctx));
231     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
232            EVP_CIPHER_CTX_original_iv(ctx),
233            EVP_CIPHER_CTX_iv_length(ctx));
234     return 1;
235 }
236
237 /* Initializes EVP_CIPHER_CTX with default values */
238 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
239                      const unsigned char *iv, int enc)
240 {
241     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
242                                   EVP_CIPH_CFB_MODE);
243 }
244
245 /*
246  * Wrapper around gostcrypt function from gost89.c which perform key meshing
247  * when nesseccary
248  */
249 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
250 {
251     struct ossl_gost_cipher_ctx *c = ctx;
252     assert(c->count % 8 == 0 && c->count <= 1024);
253     if (c->key_meshing && c->count == 1024) {
254         cryptopro_key_meshing(&(c->cctx), iv);
255     }
256     gostcrypt(&(c->cctx), iv, buf);
257     c->count = c->count % 1024 + 8;
258 }
259
260 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
261 {
262     struct ossl_gost_cipher_ctx *c = ctx;
263     word32 g, go;
264     unsigned char buf1[8];
265     assert(c->count % 8 == 0 && c->count <= 1024);
266     if (c->key_meshing && c->count == 1024) {
267         cryptopro_key_meshing(&(c->cctx), iv);
268     }
269     if (c->count == 0) {
270         gostcrypt(&(c->cctx), iv, buf1);
271     } else {
272         memcpy(buf1, iv, 8);
273     }
274     g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
275     g += 0x01010101;
276     buf1[0] = (unsigned char)(g & 0xff);
277     buf1[1] = (unsigned char)((g >> 8) & 0xff);
278     buf1[2] = (unsigned char)((g >> 16) & 0xff);
279     buf1[3] = (unsigned char)((g >> 24) & 0xff);
280     g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
281     go = g;
282     g += 0x01010104;
283     if (go > g)                 /* overflow */
284         g++;
285     buf1[4] = (unsigned char)(g & 0xff);
286     buf1[5] = (unsigned char)((g >> 8) & 0xff);
287     buf1[6] = (unsigned char)((g >> 16) & 0xff);
288     buf1[7] = (unsigned char)((g >> 24) & 0xff);
289     memcpy(iv, buf1, 8);
290     gostcrypt(&(c->cctx), buf1, buf);
291     c->count = c->count % 1024 + 8;
292 }
293
294 /* GOST encryption in CFB mode */
295 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
296                        const unsigned char *in, size_t inl)
297 {
298     const unsigned char *in_ptr = in;
299     unsigned char *out_ptr = out;
300     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
301     size_t i = 0;
302     size_t j = 0;
303 /* process partial block if any */
304     if (EVP_CIPHER_CTX_num(ctx)) {
305         for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
306              j++, i++, in_ptr++, out_ptr++) {
307             if (!EVP_CIPHER_CTX_encrypting(ctx))
308                 buf[j + 8] = *in_ptr;
309             *out_ptr = buf[j] ^ (*in_ptr);
310             if (EVP_CIPHER_CTX_encrypting(ctx))
311                 buf[j + 8] = *out_ptr;
312         }
313         if (j == 8) {
314             memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), buf + 8, 8);
315             EVP_CIPHER_CTX_set_num(ctx, 0);
316         } else {
317             EVP_CIPHER_CTX_set_num(ctx, j);
318             return 1;
319         }
320     }
321
322     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
323         /*
324          * block cipher current iv
325          */
326         gost_crypt_mesh(EVP_CIPHER_CTX_cipher_data(ctx),
327                         EVP_CIPHER_CTX_iv_noconst(ctx), buf);
328         /*
329          * xor next block of input text with it and output it
330          */
331         /*
332          * output this block
333          */
334         if (!EVP_CIPHER_CTX_encrypting(ctx))
335             memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), in_ptr, 8);
336         for (j = 0; j < 8; j++) {
337             out_ptr[j] = buf[j] ^ in_ptr[j];
338         }
339         /* Encrypt */
340         /* Next iv is next block of cipher text */
341         if (EVP_CIPHER_CTX_encrypting(ctx))
342             memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), out_ptr, 8);
343     }
344 /* Process rest of buffer */
345     if (i < inl) {
346         gost_crypt_mesh(EVP_CIPHER_CTX_cipher_data(ctx),
347                         EVP_CIPHER_CTX_iv_noconst(ctx), buf);
348         if (!EVP_CIPHER_CTX_encrypting(ctx))
349             memcpy(buf + 8, in_ptr, inl - i);
350         for (j = 0; i < inl; j++, i++) {
351             out_ptr[j] = buf[j] ^ in_ptr[j];
352         }
353         EVP_CIPHER_CTX_set_num(ctx, j);
354         if (EVP_CIPHER_CTX_encrypting(ctx))
355             memcpy(buf + 8, out_ptr, j);
356     } else {
357         EVP_CIPHER_CTX_set_num(ctx, 0);
358     }
359     return 1;
360 }
361
362 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
363                               const unsigned char *in, size_t inl)
364 {
365     const unsigned char *in_ptr = in;
366     unsigned char *out_ptr = out;
367     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
368     size_t i = 0;
369     size_t j;
370 /* process partial block if any */
371     if (EVP_CIPHER_CTX_num(ctx)) {
372         for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
373              j++, i++, in_ptr++, out_ptr++) {
374             *out_ptr = buf[j] ^ (*in_ptr);
375         }
376         if (j == 8) {
377             EVP_CIPHER_CTX_set_num(ctx, 0);
378         } else {
379             EVP_CIPHER_CTX_set_num(ctx, j);
380             return 1;
381         }
382     }
383
384     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
385         /*
386          * block cipher current iv
387          */
388         /* Encrypt */
389         gost_cnt_next(EVP_CIPHER_CTX_cipher_data(ctx),
390                       EVP_CIPHER_CTX_iv_noconst(ctx), buf);
391         /*
392          * xor next block of input text with it and output it
393          */
394         /*
395          * output this block
396          */
397         for (j = 0; j < 8; j++) {
398             out_ptr[j] = buf[j] ^ in_ptr[j];
399         }
400     }
401 /* Process rest of buffer */
402     if (i < inl) {
403         gost_cnt_next(EVP_CIPHER_CTX_cipher_data(ctx),
404                       EVP_CIPHER_CTX_iv_noconst(ctx), buf);
405         for (j = 0; i < inl; j++, i++) {
406             out_ptr[j] = buf[j] ^ in_ptr[j];
407         }
408         EVP_CIPHER_CTX_set_num(ctx, j);
409     } else {
410         EVP_CIPHER_CTX_set_num(ctx, 0);
411     }
412     return 1;
413 }
414
415 /* Cleaning up of EVP_CIPHER_CTX */
416 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
417 {
418     gost_destroy(&((struct ossl_gost_cipher_ctx *)
419                    EVP_CIPHER_CTX_cipher_data(ctx))->cctx);
420     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
421     return 1;
422 }
423
424 /* Control function for gost cipher */
425 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
426 {
427     switch (type) {
428     case EVP_CTRL_RAND_KEY:
429         {
430             if (RAND_bytes((unsigned char *)ptr,
431                            EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
432                 GOSTerr(GOST_F_GOST_CIPHER_CTL,
433                         GOST_R_RANDOM_GENERATOR_ERROR);
434                 return -1;
435             }
436             break;
437         }
438     case EVP_CTRL_PBE_PRF_NID:
439         if (ptr) {
440             *((int *)ptr) = NID_id_HMACGostR3411_94;
441             return 1;
442         } else {
443             return 0;
444         }
445
446     default:
447         GOSTerr(GOST_F_GOST_CIPHER_CTL,
448                 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
449         return -1;
450     }
451     return 1;
452 }
453
454 /* Set cipher parameters from ASN1 structure */
455 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
456 {
457     int len = 0;
458     unsigned char *buf = NULL;
459     unsigned char *p = NULL;
460     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_cipher_data(ctx);
461     GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
462     ASN1_OCTET_STRING *os = NULL;
463     if (!gcp) {
464         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
465         return 0;
466     }
467     if (!ASN1_OCTET_STRING_set(gcp->iv, EVP_CIPHER_CTX_iv(ctx),
468                                EVP_CIPHER_CTX_iv_length(ctx))) {
469         GOST_CIPHER_PARAMS_free(gcp);
470         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
471         return 0;
472     }
473     ASN1_OBJECT_free(gcp->enc_param_set);
474     gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
475
476     len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
477     p = buf = OPENSSL_malloc(len);
478     if (!buf) {
479         GOST_CIPHER_PARAMS_free(gcp);
480         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
481         return 0;
482     }
483     i2d_GOST_CIPHER_PARAMS(gcp, &p);
484     GOST_CIPHER_PARAMS_free(gcp);
485
486     os = ASN1_OCTET_STRING_new();
487
488     if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
489         OPENSSL_free(buf);
490         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
491         return 0;
492     }
493     OPENSSL_free(buf);
494
495     ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
496     return 1;
497 }
498
499 /* Store parameters into ASN1 structure */
500 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
501 {
502     int ret = -1;
503     int len;
504     GOST_CIPHER_PARAMS *gcp = NULL;
505     unsigned char *p;
506     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_cipher_data(ctx);
507     if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
508         return ret;
509     }
510
511     p = params->value.sequence->data;
512
513     gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
514                                  params->value.sequence->length);
515
516     len = gcp->iv->length;
517     if (len != EVP_CIPHER_CTX_iv_length(ctx)) {
518         GOST_CIPHER_PARAMS_free(gcp);
519         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
520         return -1;
521     }
522     if (!gost_cipher_set_param(c, OBJ_obj2nid(gcp->enc_param_set))) {
523         GOST_CIPHER_PARAMS_free(gcp);
524         return -1;
525     }
526     memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), gcp->iv->data,
527            EVP_CIPHER_CTX_iv_length(ctx));
528
529     GOST_CIPHER_PARAMS_free(gcp);
530
531     return 1;
532 }
533
534 int gost_imit_init_cpa(EVP_MD_CTX *ctx)
535 {
536     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
537     memset(c->buffer, 0, sizeof(c->buffer));
538     memset(c->partial_block, 0, sizeof(c->partial_block));
539     c->count = 0;
540     c->bytes_left = 0;
541     c->key_meshing = 1;
542     gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
543     return 1;
544 }
545
546 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
547                            const unsigned char *data)
548 {
549     unsigned char buffer[8];
550     /*
551      * We are using local buffer for iv because CryptoPro doesn't interpret
552      * internal state of MAC algorithm as iv during keymeshing (but does
553      * initialize internal state from iv in key transport
554      */
555     assert(c->count % 8 == 0 && c->count <= 1024);
556     if (c->key_meshing && c->count == 1024) {
557         cryptopro_key_meshing(&(c->cctx), buffer);
558     }
559     mac_block(&(c->cctx), c->buffer, data);
560     c->count = c->count % 1024 + 8;
561 }
562
563 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
564 {
565     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
566     const unsigned char *p = data;
567     size_t bytes = count, i;
568     if (!(c->key_set)) {
569         GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
570         return 0;
571     }
572     if (c->bytes_left) {
573         for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
574             c->partial_block[i] = *p;
575         }
576         if (i == 8) {
577             mac_block_mesh(c, c->partial_block);
578         } else {
579             c->bytes_left = i;
580             return 1;
581         }
582     }
583     while (bytes > 8) {
584         mac_block_mesh(c, p);
585         p += 8;
586         bytes -= 8;
587     }
588     if (bytes > 0) {
589         memcpy(c->partial_block, p, bytes);
590     }
591     c->bytes_left = bytes;
592     return 1;
593 }
594
595 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
596 {
597     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
598     if (!c->key_set) {
599         GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
600         return 0;
601     }
602     if (c->count == 0 && c->bytes_left) {
603         unsigned char buffer[8];
604         memset(buffer, 0, 8);
605         gost_imit_update(ctx, buffer, 8);
606     }
607     if (c->bytes_left) {
608         int i;
609         for (i = c->bytes_left; i < 8; i++) {
610             c->partial_block[i] = 0;
611         }
612         mac_block_mesh(c, c->partial_block);
613     }
614     get_mac(c->buffer, 32, md);
615     return 1;
616 }
617
618 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
619 {
620     switch (type) {
621     case EVP_MD_CTRL_KEY_LEN:
622         *((unsigned int *)(ptr)) = 32;
623         return 1;
624     case EVP_MD_CTRL_SET_KEY:
625         {
626             if (arg != 32) {
627                 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
628                 return 0;
629             }
630
631             gost_key(&(((struct ossl_gost_imit_ctx *)(EVP_MD_CTX_md_data(ctx)))->cctx),
632                      ptr);
633             ((struct ossl_gost_imit_ctx *)(EVP_MD_CTX_md_data(ctx)))->key_set = 1;
634             return 1;
635
636         }
637     default:
638         return 0;
639     }
640 }
641
642 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
643 {
644     memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
645            sizeof(struct ossl_gost_imit_ctx));
646     return 1;
647 }
648
649 /* Clean up imit ctx */
650 int gost_imit_cleanup(EVP_MD_CTX *ctx)
651 {
652     memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
653     return 1;
654 }