Adapt all engines that add new EVP_MDs
[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 = ctx->cipher_data;
203     if (ctx->app_data == NULL) {
204         if (!gost_cipher_set_param(c, paramNID))
205             return 0;
206         ctx->app_data = ctx->cipher_data;
207     }
208     if (key)
209         gost_key(&(c->cctx), key);
210     if (iv)
211         memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
212     memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
213     return 1;
214 }
215
216 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
217                                 const unsigned char *iv, int enc)
218 {
219     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
220     gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
221     c->key_meshing = 1;
222     c->count = 0;
223     if (key)
224         gost_key(&(c->cctx), key);
225     if (iv)
226         memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
227     memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
228     return 1;
229 }
230
231 /* Initializes EVP_CIPHER_CTX with default values */
232 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
233                      const unsigned char *iv, int enc)
234 {
235     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
236                                   EVP_CIPH_CFB_MODE);
237 }
238
239 /*
240  * Wrapper around gostcrypt function from gost89.c which perform key meshing
241  * when nesseccary
242  */
243 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
244 {
245     struct ossl_gost_cipher_ctx *c = ctx;
246     assert(c->count % 8 == 0 && c->count <= 1024);
247     if (c->key_meshing && c->count == 1024) {
248         cryptopro_key_meshing(&(c->cctx), iv);
249     }
250     gostcrypt(&(c->cctx), iv, buf);
251     c->count = c->count % 1024 + 8;
252 }
253
254 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
255 {
256     struct ossl_gost_cipher_ctx *c = ctx;
257     word32 g, go;
258     unsigned char buf1[8];
259     assert(c->count % 8 == 0 && c->count <= 1024);
260     if (c->key_meshing && c->count == 1024) {
261         cryptopro_key_meshing(&(c->cctx), iv);
262     }
263     if (c->count == 0) {
264         gostcrypt(&(c->cctx), iv, buf1);
265     } else {
266         memcpy(buf1, iv, 8);
267     }
268     g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
269     g += 0x01010101;
270     buf1[0] = (unsigned char)(g & 0xff);
271     buf1[1] = (unsigned char)((g >> 8) & 0xff);
272     buf1[2] = (unsigned char)((g >> 16) & 0xff);
273     buf1[3] = (unsigned char)((g >> 24) & 0xff);
274     g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
275     go = g;
276     g += 0x01010104;
277     if (go > g)                 /* overflow */
278         g++;
279     buf1[4] = (unsigned char)(g & 0xff);
280     buf1[5] = (unsigned char)((g >> 8) & 0xff);
281     buf1[6] = (unsigned char)((g >> 16) & 0xff);
282     buf1[7] = (unsigned char)((g >> 24) & 0xff);
283     memcpy(iv, buf1, 8);
284     gostcrypt(&(c->cctx), buf1, buf);
285     c->count = c->count % 1024 + 8;
286 }
287
288 /* GOST encryption in CFB mode */
289 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
290                        const unsigned char *in, size_t inl)
291 {
292     const unsigned char *in_ptr = in;
293     unsigned char *out_ptr = out;
294     size_t i = 0;
295     size_t j = 0;
296 /* process partial block if any */
297     if (ctx->num) {
298         for (j = ctx->num, i = 0; j < 8 && i < inl;
299              j++, i++, in_ptr++, out_ptr++) {
300             if (!ctx->encrypt)
301                 ctx->buf[j + 8] = *in_ptr;
302             *out_ptr = ctx->buf[j] ^ (*in_ptr);
303             if (ctx->encrypt)
304                 ctx->buf[j + 8] = *out_ptr;
305         }
306         if (j == 8) {
307             memcpy(ctx->iv, ctx->buf + 8, 8);
308             ctx->num = 0;
309         } else {
310             ctx->num = j;
311             return 1;
312         }
313     }
314
315     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
316         /*
317          * block cipher current iv
318          */
319         gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
320         /*
321          * xor next block of input text with it and output it
322          */
323         /*
324          * output this block
325          */
326         if (!ctx->encrypt)
327             memcpy(ctx->iv, in_ptr, 8);
328         for (j = 0; j < 8; j++) {
329             out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
330         }
331         /* Encrypt */
332         /* Next iv is next block of cipher text */
333         if (ctx->encrypt)
334             memcpy(ctx->iv, out_ptr, 8);
335     }
336 /* Process rest of buffer */
337     if (i < inl) {
338         gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
339         if (!ctx->encrypt)
340             memcpy(ctx->buf + 8, in_ptr, inl - i);
341         for (j = 0; i < inl; j++, i++) {
342             out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
343         }
344         ctx->num = j;
345         if (ctx->encrypt)
346             memcpy(ctx->buf + 8, out_ptr, j);
347     } else {
348         ctx->num = 0;
349     }
350     return 1;
351 }
352
353 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
354                               const unsigned char *in, size_t inl)
355 {
356     const unsigned char *in_ptr = in;
357     unsigned char *out_ptr = out;
358     size_t i = 0;
359     size_t j;
360 /* process partial block if any */
361     if (ctx->num) {
362         for (j = ctx->num, i = 0; j < 8 && i < inl;
363              j++, i++, in_ptr++, out_ptr++) {
364             *out_ptr = ctx->buf[j] ^ (*in_ptr);
365         }
366         if (j == 8) {
367             ctx->num = 0;
368         } else {
369             ctx->num = j;
370             return 1;
371         }
372     }
373
374     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
375         /*
376          * block cipher current iv
377          */
378         /* Encrypt */
379         gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
380         /*
381          * xor next block of input text with it and output it
382          */
383         /*
384          * output this block
385          */
386         for (j = 0; j < 8; j++) {
387             out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
388         }
389     }
390 /* Process rest of buffer */
391     if (i < inl) {
392         gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
393         for (j = 0; i < inl; j++, i++) {
394             out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
395         }
396         ctx->num = j;
397     } else {
398         ctx->num = 0;
399     }
400     return 1;
401 }
402
403 /* Cleaning up of EVP_CIPHER_CTX */
404 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
405 {
406     gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
407     ctx->app_data = NULL;
408     return 1;
409 }
410
411 /* Control function for gost cipher */
412 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
413 {
414     switch (type) {
415     case EVP_CTRL_RAND_KEY:
416         {
417             if (RAND_bytes((unsigned char *)ptr, ctx->key_len) <= 0) {
418                 GOSTerr(GOST_F_GOST_CIPHER_CTL,
419                         GOST_R_RANDOM_GENERATOR_ERROR);
420                 return -1;
421             }
422             break;
423         }
424     case EVP_CTRL_PBE_PRF_NID:
425         if (ptr) {
426             *((int *)ptr) = NID_id_HMACGostR3411_94;
427             return 1;
428         } else {
429             return 0;
430         }
431
432     default:
433         GOSTerr(GOST_F_GOST_CIPHER_CTL,
434                 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
435         return -1;
436     }
437     return 1;
438 }
439
440 /* Set cipher parameters from ASN1 structure */
441 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
442 {
443     int len = 0;
444     unsigned char *buf = NULL;
445     unsigned char *p = NULL;
446     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
447     GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
448     ASN1_OCTET_STRING *os = NULL;
449     if (!gcp) {
450         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
451         return 0;
452     }
453     if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) {
454         GOST_CIPHER_PARAMS_free(gcp);
455         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
456         return 0;
457     }
458     ASN1_OBJECT_free(gcp->enc_param_set);
459     gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
460
461     len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
462     p = buf = OPENSSL_malloc(len);
463     if (!buf) {
464         GOST_CIPHER_PARAMS_free(gcp);
465         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
466         return 0;
467     }
468     i2d_GOST_CIPHER_PARAMS(gcp, &p);
469     GOST_CIPHER_PARAMS_free(gcp);
470
471     os = ASN1_OCTET_STRING_new();
472
473     if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
474         OPENSSL_free(buf);
475         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
476         return 0;
477     }
478     OPENSSL_free(buf);
479
480     ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
481     return 1;
482 }
483
484 /* Store parameters into ASN1 structure */
485 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
486 {
487     int ret = -1;
488     int len;
489     GOST_CIPHER_PARAMS *gcp = NULL;
490     unsigned char *p;
491     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
492     if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
493         return ret;
494     }
495
496     p = params->value.sequence->data;
497
498     gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
499                                  params->value.sequence->length);
500
501     len = gcp->iv->length;
502     if (len != ctx->cipher->iv_len) {
503         GOST_CIPHER_PARAMS_free(gcp);
504         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
505         return -1;
506     }
507     if (!gost_cipher_set_param(c, OBJ_obj2nid(gcp->enc_param_set))) {
508         GOST_CIPHER_PARAMS_free(gcp);
509         return -1;
510     }
511     memcpy(ctx->oiv, gcp->iv->data, len);
512
513     GOST_CIPHER_PARAMS_free(gcp);
514
515     return 1;
516 }
517
518 int gost_imit_init_cpa(EVP_MD_CTX *ctx)
519 {
520     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
521     memset(c->buffer, 0, sizeof(c->buffer));
522     memset(c->partial_block, 0, sizeof(c->partial_block));
523     c->count = 0;
524     c->bytes_left = 0;
525     c->key_meshing = 1;
526     gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
527     return 1;
528 }
529
530 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
531                            const unsigned char *data)
532 {
533     unsigned char buffer[8];
534     /*
535      * We are using local buffer for iv because CryptoPro doesn't interpret
536      * internal state of MAC algorithm as iv during keymeshing (but does
537      * initialize internal state from iv in key transport
538      */
539     assert(c->count % 8 == 0 && c->count <= 1024);
540     if (c->key_meshing && c->count == 1024) {
541         cryptopro_key_meshing(&(c->cctx), buffer);
542     }
543     mac_block(&(c->cctx), c->buffer, data);
544     c->count = c->count % 1024 + 8;
545 }
546
547 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
548 {
549     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
550     const unsigned char *p = data;
551     size_t bytes = count, i;
552     if (!(c->key_set)) {
553         GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
554         return 0;
555     }
556     if (c->bytes_left) {
557         for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
558             c->partial_block[i] = *p;
559         }
560         if (i == 8) {
561             mac_block_mesh(c, c->partial_block);
562         } else {
563             c->bytes_left = i;
564             return 1;
565         }
566     }
567     while (bytes > 8) {
568         mac_block_mesh(c, p);
569         p += 8;
570         bytes -= 8;
571     }
572     if (bytes > 0) {
573         memcpy(c->partial_block, p, bytes);
574     }
575     c->bytes_left = bytes;
576     return 1;
577 }
578
579 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
580 {
581     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
582     if (!c->key_set) {
583         GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
584         return 0;
585     }
586     if (c->count == 0 && c->bytes_left) {
587         unsigned char buffer[8];
588         memset(buffer, 0, 8);
589         gost_imit_update(ctx, buffer, 8);
590     }
591     if (c->bytes_left) {
592         int i;
593         for (i = c->bytes_left; i < 8; i++) {
594             c->partial_block[i] = 0;
595         }
596         mac_block_mesh(c, c->partial_block);
597     }
598     get_mac(c->buffer, 32, md);
599     return 1;
600 }
601
602 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
603 {
604     switch (type) {
605     case EVP_MD_CTRL_KEY_LEN:
606         *((unsigned int *)(ptr)) = 32;
607         return 1;
608     case EVP_MD_CTRL_SET_KEY:
609         {
610             if (arg != 32) {
611                 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
612                 return 0;
613             }
614
615             gost_key(&(((struct ossl_gost_imit_ctx *)(EVP_MD_CTX_md_data(ctx)))->cctx),
616                      ptr);
617             ((struct ossl_gost_imit_ctx *)(EVP_MD_CTX_md_data(ctx)))->key_set = 1;
618             return 1;
619
620         }
621     default:
622         return 0;
623     }
624 }
625
626 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
627 {
628     memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
629            sizeof(struct ossl_gost_imit_ctx));
630     return 1;
631 }
632
633 /* Clean up imit ctx */
634 int gost_imit_cleanup(EVP_MD_CTX *ctx)
635 {
636     memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
637     return 1;
638 }