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