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