]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_crypt.c
Merge https://github.com/gost-engine/engine
[openssl-gost/engine.git] / 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_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
26                                 const unsigned char *iv, int enc);
27 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
28                                 const unsigned char *iv, int enc);
29 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
30                                   const unsigned char *key,
31                                   const unsigned char *iv, int enc);
32 /* Handles block of data in CFB mode */
33 static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
34                               const unsigned char *in, size_t inl);
35 /* Handles block of data in CBC mode */
36 static int  gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
37                                const unsigned char *in, size_t inl);
38 /* Handles block of data in CNT mode */
39 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
40                               const unsigned char *in, size_t inl);
41 /* Cleanup function */
42 static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
43 /* set/get cipher parameters */
44 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
45 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
46 /* Control function */
47 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
48
49 EVP_CIPHER cipher_gost = {
50     NID_id_Gost28147_89,
51     1,                          /* block_size */
52     32,                         /* key_size */
53     8,                          /* iv_len */
54     EVP_CIPH_CFB_MODE | EVP_CIPH_NO_PADDING |
55         EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
56     gost_cipher_init,
57     gost_cipher_do_cfb,
58     gost_cipher_cleanup,
59     sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
60     gost89_set_asn1_parameters,
61     gost89_get_asn1_parameters,
62     gost_cipher_ctl,
63     NULL,
64 };
65
66 EVP_CIPHER cipher_gost_cbc =
67     {
68     NID_gost89_cbc,
69     8,/*block_size*/
70     32,/*key_size*/
71     8,/*iv_len */
72     EVP_CIPH_CBC_MODE|
73     EVP_CIPH_CUSTOM_IV| EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
74     gost_cipher_init_cbc,
75     gost_cipher_do_cbc,
76     gost_cipher_cleanup,
77     sizeof(struct ossl_gost_cipher_ctx),/* ctx_size */
78     gost89_set_asn1_parameters,
79     gost89_get_asn1_parameters,
80     gost_cipher_ctl,
81     NULL,
82     };
83
84 EVP_CIPHER cipher_gost_cpacnt = {
85     NID_gost89_cnt,
86     1,                          /* block_size */
87     32,                         /* key_size */
88     8,                          /* iv_len */
89     EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
90         EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
91     gost_cipher_init_cpa,
92     gost_cipher_do_cnt,
93     gost_cipher_cleanup,
94     sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
95     gost89_set_asn1_parameters,
96     gost89_get_asn1_parameters,
97     gost_cipher_ctl,
98     NULL,
99 };
100
101 EVP_CIPHER cipher_gost_cpcnt_12 = {
102     NID_gost89_cnt_12,
103     1,                          /* block_size */
104     32,                         /* key_size */
105     8,                          /* iv_len */
106     EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
107         EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
108     gost_cipher_init_cp_12,
109     gost_cipher_do_cnt,
110     gost_cipher_cleanup,
111     sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
112     gost89_set_asn1_parameters,
113     gost89_get_asn1_parameters,
114     gost_cipher_ctl,
115     NULL,
116 };
117
118 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
119 /* Init functions which set specific parameters */
120 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
121 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx);
122 /* process block of data */
123 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
124 /* Return computed value */
125 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
126 /* Copies context */
127 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
128 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
129 /* Control function, knows how to set MAC key.*/
130 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
131
132 EVP_MD imit_gost_cpa = {
133     NID_id_Gost28147_89_MAC,
134     NID_undef,
135     4,
136     0,
137     gost_imit_init_cpa,
138     gost_imit_update,
139     gost_imit_final,
140     gost_imit_copy,
141     gost_imit_cleanup,
142     NULL,
143     NULL,
144     {0, 0, 0, 0, 0},
145     8,
146     sizeof(struct ossl_gost_imit_ctx),
147     gost_imit_ctrl
148 };
149
150 EVP_MD imit_gost_cp_12 = {
151     NID_gost_mac_12,
152     NID_undef,
153     4,
154     0,
155     gost_imit_init_cp_12,
156     gost_imit_update,
157     gost_imit_final,
158     gost_imit_copy,
159     gost_imit_cleanup,
160     NULL,
161     NULL,
162     {0, 0, 0, 0, 0},
163     8,
164     sizeof(struct ossl_gost_imit_ctx),
165     gost_imit_ctrl
166 };
167
168 /*
169  * Correspondence between gost parameter OIDs and substitution blocks
170  * NID field is filed by register_gost_NID function in engine.c
171  * upon engine initialization
172  */
173
174 struct gost_cipher_info gost_cipher_list[] = {
175     /*- NID *//*
176      * Subst block
177      *//*
178      * Key meshing
179      */
180     /*
181      * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
182      */
183     {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
184      1},
185     {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
186      1},
187     {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
188      1},
189     {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
190      1},
191     {NID_id_tc26_gost_28147_param_Z, &Gost28147_TC26ParamSetZ, 1},
192     {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
193     {NID_undef, NULL, 0}
194 };
195
196 /*
197  * get encryption parameters from crypto network settings FIXME For now we
198  * use environment var CRYPT_PARAMS as place to store these settings.
199  * Actually, it is better to use engine control command, read from
200  * configuration file to set them
201  */
202 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
203 {
204     int nid;
205     struct gost_cipher_info *param;
206     if (!obj) {
207         const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
208         if (!params || !strlen(params)) {
209             int i;
210             for (i = 0; gost_cipher_list[i].nid != NID_undef; i++)
211                 if (gost_cipher_list[i].nid == NID_id_tc26_gost_28147_param_Z)
212                     return &gost_cipher_list[i];
213             return &gost_cipher_list[0];
214         }
215
216         nid = OBJ_txt2nid(params);
217         if (nid == NID_undef) {
218             GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
219                     GOST_R_INVALID_CIPHER_PARAM_OID);
220             return NULL;
221         }
222     } else {
223         nid = OBJ_obj2nid(obj);
224     }
225     for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
226          param++) ;
227     if (!param->sblock) {
228         GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
229         return NULL;
230     }
231     return param;
232 }
233
234 /* Sets cipher param from paramset NID. */
235 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
236 {
237     const struct gost_cipher_info *param;
238     param =
239         get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
240     if (!param)
241         return 0;
242
243     c->paramNID = param->nid;
244     c->key_meshing = param->key_meshing;
245     c->count = 0;
246     gost_init(&(c->cctx), param->sblock);
247     return 1;
248 }
249
250 /* Initializes EVP_CIPHER_CTX by paramset NID */
251 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
252                                   const unsigned char *key,
253                                   const unsigned char *iv, int enc,
254                                   int paramNID, int mode)
255 {
256     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
257     if (ctx->app_data == NULL) {
258         if (!gost_cipher_set_param(c, paramNID))
259             return 0;
260         ctx->app_data = ctx->cipher_data;
261     }
262     if (key)
263         gost_key(&(c->cctx), key);
264     if (iv)
265         memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
266     memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
267     return 1;
268 }
269
270 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
271                                 const unsigned char *key,
272                                 const unsigned char *iv,
273                                 gost_subst_block * block)
274 {
275     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
276     gost_init(&(c->cctx), block);
277     c->key_meshing = 1;
278     c->count = 0;
279     if (key)
280         gost_key(&(c->cctx), key);
281     if (iv)
282         memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
283     memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
284     return 1;
285 }
286
287 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
288                                 const unsigned char *iv, int enc)
289 {
290     return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
291 }
292
293 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
294                                   const unsigned char *key,
295                                   const unsigned char *iv, int enc)
296 {
297     return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
298 }
299
300 /* Initializes EVP_CIPHER_CTX with default values */
301 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
302                      const unsigned char *iv, int enc)
303 {
304     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
305                                   EVP_CIPH_CFB_MODE);
306 }
307
308 /* Initializes EVP_CIPHER_CTX with default values */
309 int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
310                          const unsigned char *iv, int enc)
311 {
312     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
313                                   EVP_CIPH_CBC_MODE);
314 }
315
316
317 /*
318  * Wrapper around gostcrypt function from gost89.c which perform key meshing
319  * when nesseccary
320  */
321 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
322 {
323     struct ossl_gost_cipher_ctx *c = ctx;
324     assert(c->count % 8 == 0 && c->count <= 1024);
325     if (c->key_meshing && c->count == 1024) {
326         cryptopro_key_meshing(&(c->cctx), iv);
327     }
328     gostcrypt(&(c->cctx), iv, buf);
329     c->count = c->count % 1024 + 8;
330 }
331
332 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
333 {
334     struct ossl_gost_cipher_ctx *c = ctx;
335     word32 g, go;
336     unsigned char buf1[8];
337     assert(c->count % 8 == 0 && c->count <= 1024);
338     if (c->key_meshing && c->count == 1024) {
339         cryptopro_key_meshing(&(c->cctx), iv);
340     }
341     if (c->count == 0) {
342         gostcrypt(&(c->cctx), iv, buf1);
343     } else {
344         memcpy(buf1, iv, 8);
345     }
346     g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
347     g += 0x01010101;
348     buf1[0] = (unsigned char)(g & 0xff);
349     buf1[1] = (unsigned char)((g >> 8) & 0xff);
350     buf1[2] = (unsigned char)((g >> 16) & 0xff);
351     buf1[3] = (unsigned char)((g >> 24) & 0xff);
352     g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
353     go = g;
354     g += 0x01010104;
355     if (go > g)                 /* overflow */
356         g++;
357     buf1[4] = (unsigned char)(g & 0xff);
358     buf1[5] = (unsigned char)((g >> 8) & 0xff);
359     buf1[6] = (unsigned char)((g >> 16) & 0xff);
360     buf1[7] = (unsigned char)((g >> 24) & 0xff);
361     memcpy(iv, buf1, 8);
362     gostcrypt(&(c->cctx), buf1, buf);
363     c->count = c->count % 1024 + 8;
364 }
365
366 /* GOST encryptoon in CBC mode */
367 int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
368     const unsigned char *in, size_t inl)
369     {
370     OPENSSL_assert(inl % 8 ==0);
371     unsigned char b[8];
372     const unsigned char *in_ptr=in;
373     unsigned char *out_ptr=out;
374     int i;
375     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
376     if (ctx->encrypt)
377         {
378         while(inl>0)
379             {
380             for (i=0;i<8;i++)
381                {
382                 b[i]=ctx->iv[i]^in_ptr[i];
383                 }
384             gostcrypt(&(c->cctx),b,out_ptr);
385             memcpy(ctx->iv,out_ptr,8);
386             out_ptr+=8;
387             in_ptr+=8;
388             inl-=8;
389             }
390         }
391     else
392         {
393         while (inl>0) {
394             gostdecrypt(&(c->cctx),in_ptr,b);
395             for (i=0;i<8;i++)
396                 {
397                 out_ptr[i]=ctx->iv[i]^b[i];
398                 }
399             memcpy(ctx->iv,in_ptr,8);
400             out_ptr+=8;
401             in_ptr+=8;
402             inl-=8;
403             }
404         }
405     return 1;
406     }
407 /* GOST encryption in CFB mode */
408 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
409                        const unsigned char *in, size_t inl)
410 {
411     const unsigned char *in_ptr = in;
412     unsigned char *out_ptr = out;
413     size_t i = 0;
414     size_t j = 0;
415 /* process partial block if any */
416     if (ctx->num) {
417         for (j = ctx->num, i = 0; j < 8 && i < inl;
418              j++, i++, in_ptr++, out_ptr++) {
419             if (!ctx->encrypt)
420                 ctx->buf[j + 8] = *in_ptr;
421             *out_ptr = ctx->buf[j] ^ (*in_ptr);
422             if (ctx->encrypt)
423                 ctx->buf[j + 8] = *out_ptr;
424         }
425         if (j == 8) {
426             memcpy(ctx->iv, ctx->buf + 8, 8);
427             ctx->num = 0;
428         } else {
429             ctx->num = j;
430             return 1;
431         }
432     }
433
434     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
435         /*
436          * block cipher current iv
437          */
438         gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
439         /*
440          * xor next block of input text with it and output it
441          */
442         /*
443          * output this block
444          */
445         if (!ctx->encrypt)
446             memcpy(ctx->iv, in_ptr, 8);
447         for (j = 0; j < 8; j++) {
448             out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
449         }
450         /* Encrypt */
451         /* Next iv is next block of cipher text */
452         if (ctx->encrypt)
453             memcpy(ctx->iv, out_ptr, 8);
454     }
455 /* Process rest of buffer */
456     if (i < inl) {
457         gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
458         if (!ctx->encrypt)
459             memcpy(ctx->buf + 8, in_ptr, inl - i);
460         for (j = 0; i < inl; j++, i++) {
461             out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
462         }
463         ctx->num = j;
464         if (ctx->encrypt)
465             memcpy(ctx->buf + 8, out_ptr, j);
466     } else {
467         ctx->num = 0;
468     }
469     return 1;
470 }
471
472 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
473                               const unsigned char *in, size_t inl)
474 {
475     const unsigned char *in_ptr = in;
476     unsigned char *out_ptr = out;
477     size_t i = 0;
478     size_t j;
479 /* process partial block if any */
480     if (ctx->num) {
481         for (j = ctx->num, i = 0; j < 8 && i < inl;
482              j++, i++, in_ptr++, out_ptr++) {
483             *out_ptr = ctx->buf[j] ^ (*in_ptr);
484         }
485         if (j == 8) {
486             ctx->num = 0;
487         } else {
488             ctx->num = j;
489             return 1;
490         }
491     }
492
493     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
494         /*
495          * block cipher current iv
496          */
497         /* Encrypt */
498         gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
499         /*
500          * xor next block of input text with it and output it
501          */
502         /*
503          * output this block
504          */
505         for (j = 0; j < 8; j++) {
506             out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
507         }
508     }
509 /* Process rest of buffer */
510     if (i < inl) {
511         gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
512         for (j = 0; i < inl; j++, i++) {
513             out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
514         }
515         ctx->num = j;
516     } else {
517         ctx->num = 0;
518     }
519     return 1;
520 }
521
522 /* Cleaning up of EVP_CIPHER_CTX */
523 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
524 {
525     gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
526     ctx->app_data = NULL;
527     return 1;
528 }
529
530 /* Control function for gost cipher */
531 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
532 {
533     switch (type) {
534     case EVP_CTRL_INIT:
535         {
536             struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
537             if (c == NULL) {
538                 return -1;
539             }
540             return gost_cipher_set_param(c, arg);
541         }
542     case EVP_CTRL_RAND_KEY:
543         {
544             if (RAND_bytes((unsigned char *)ptr, ctx->key_len) <= 0) {
545                 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
546                 return -1;
547             }
548             break;
549         }
550     case EVP_CTRL_PBE_PRF_NID:
551         if (ptr) {
552             const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
553             int nid = NID_id_tc26_hmac_gost_3411_2012_512;
554
555             if (params) {
556                 if (!strcmp("md_gost12_256", params))
557                     nid = NID_id_tc26_hmac_gost_3411_2012_256;
558                 else if (!strcmp("md_gost12_512", params))
559                     nid = NID_id_tc26_hmac_gost_3411_2012_512;
560                 else if (!strcmp("md_gost94", params))
561                     nid = NID_id_HMACGostR3411_94;
562             }
563             *((int *)ptr) = nid;
564             return 1;
565         } else {
566             return 0;
567         }
568
569     default:
570         GOSTerr(GOST_F_GOST_CIPHER_CTL,
571                 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
572         return -1;
573     }
574     return 1;
575 }
576
577 /* Set cipher parameters from ASN1 structure */
578 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
579 {
580     int len = 0;
581     unsigned char *buf = NULL;
582     unsigned char *p = NULL;
583     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
584     GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
585     ASN1_OCTET_STRING *os = NULL;
586     if (!gcp) {
587         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
588         return 0;
589     }
590     if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) {
591         GOST_CIPHER_PARAMS_free(gcp);
592         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
593         return 0;
594     }
595     ASN1_OBJECT_free(gcp->enc_param_set);
596     gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
597
598     len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
599     p = buf = OPENSSL_malloc(len);
600     if (!buf) {
601         GOST_CIPHER_PARAMS_free(gcp);
602         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
603         return 0;
604     }
605     i2d_GOST_CIPHER_PARAMS(gcp, &p);
606     GOST_CIPHER_PARAMS_free(gcp);
607
608     os = ASN1_OCTET_STRING_new();
609
610     if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
611         OPENSSL_free(buf);
612         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
613         return 0;
614     }
615     OPENSSL_free(buf);
616
617     ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
618     return 1;
619 }
620
621 /* Store parameters into ASN1 structure */
622 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
623 {
624     int ret = -1;
625     int len;
626     GOST_CIPHER_PARAMS *gcp = NULL;
627     unsigned char *p;
628     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
629     int nid;
630
631     if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
632         return ret;
633     }
634
635     p = params->value.sequence->data;
636
637     gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
638                                  params->value.sequence->length);
639
640     len = gcp->iv->length;
641     if (len != ctx->cipher->iv_len) {
642         GOST_CIPHER_PARAMS_free(gcp);
643         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
644         return -1;
645     }
646
647     nid = OBJ_obj2nid(gcp->enc_param_set);
648     if (nid == NID_undef) {
649         GOST_CIPHER_PARAMS_free(gcp);
650         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
651                 GOST_R_INVALID_CIPHER_PARAM_OID);
652         return -1;
653     }
654
655     if (!gost_cipher_set_param(c, nid)) {
656         GOST_CIPHER_PARAMS_free(gcp);
657         return -1;
658     }
659     memcpy(ctx->oiv, gcp->iv->data, len);
660
661     GOST_CIPHER_PARAMS_free(gcp);
662
663     return 1;
664 }
665
666 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
667 {
668     struct ossl_gost_imit_ctx *c = ctx->md_data;
669     memset(c->buffer, 0, sizeof(c->buffer));
670     memset(c->partial_block, 0, sizeof(c->partial_block));
671     c->count = 0;
672     c->bytes_left = 0;
673     c->key_meshing = 1;
674     c->dgst_size = 4;
675     gost_init(&(c->cctx), block);
676     return 1;
677 }
678
679 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
680 {
681     return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
682 }
683
684 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
685 {
686     return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
687 }
688
689 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
690                            const unsigned char *data)
691 {
692     unsigned char buffer[8];
693     /*
694      * We are using local buffer for iv because CryptoPro doesn't interpret
695      * internal state of MAC algorithm as iv during keymeshing (but does
696      * initialize internal state from iv in key transport
697      */
698     assert(c->count % 8 == 0 && c->count <= 1024);
699     if (c->key_meshing && c->count == 1024) {
700         cryptopro_key_meshing(&(c->cctx), buffer);
701     }
702     mac_block(&(c->cctx), c->buffer, data);
703     c->count = c->count % 1024 + 8;
704 }
705
706 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
707 {
708     struct ossl_gost_imit_ctx *c = ctx->md_data;
709     const unsigned char *p = data;
710     size_t bytes = count, i;
711     if (!(c->key_set)) {
712         GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
713         return 0;
714     }
715     if (c->bytes_left) {
716         for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
717             c->partial_block[i] = *p;
718         }
719         if (i == 8) {
720             mac_block_mesh(c, c->partial_block);
721         } else {
722             c->bytes_left = i;
723             return 1;
724         }
725     }
726     while (bytes > 8) {
727         mac_block_mesh(c, p);
728         p += 8;
729         bytes -= 8;
730     }
731     if (bytes > 0) {
732         memcpy(c->partial_block, p, bytes);
733     }
734     c->bytes_left = bytes;
735     return 1;
736 }
737
738 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
739 {
740     struct ossl_gost_imit_ctx *c = ctx->md_data;
741     if (!c->key_set) {
742         GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
743         return 0;
744     }
745     if (c->count == 0 && c->bytes_left) {
746         unsigned char buffer[8];
747         memset(buffer, 0, 8);
748         gost_imit_update(ctx, buffer, 8);
749     }
750     if (c->bytes_left) {
751         int i;
752         for (i = c->bytes_left; i < 8; i++) {
753             c->partial_block[i] = 0;
754         }
755         mac_block_mesh(c, c->partial_block);
756     }
757     get_mac(c->buffer, 8 * c->dgst_size, md);
758     return 1;
759 }
760
761 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
762 {
763     switch (type) {
764     case EVP_MD_CTRL_KEY_LEN:
765         *((unsigned int *)(ptr)) = 32;
766         return 1;
767     case EVP_MD_CTRL_SET_KEY:
768         {
769                                     struct ossl_gost_imit_ctx *gost_imit_ctx = ctx->md_data;
770
771                                     if (ctx->digest->init(ctx) <= 0) {
772                                         GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
773                                         return 0;
774                                     }
775                                           ctx->flags |= EVP_MD_CTX_FLAG_NO_INIT;
776
777             if (arg == 0) {
778                                                     struct gost_mac_key *key = (struct gost_mac_key*) ptr;
779                                                                 if (key->mac_param_nid != NID_undef) {
780                                                                         const struct gost_cipher_info *param = get_encryption_params(OBJ_nid2obj(key->mac_param_nid));
781                                                                         if (param == NULL)
782                                                                         {
783                     GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_PARAMS);
784                     return 0;
785                                                                         }
786                                                                         gost_init(&(gost_imit_ctx->cctx), param->sblock);
787                                                                 }
788                                                                 gost_key(&(gost_imit_ctx->cctx), key->key);
789                 gost_imit_ctx->key_set = 1;
790
791                                                                 return 1;
792             }
793                                                 else if (arg == 32)
794                                                 {
795             gost_key(&(gost_imit_ctx->cctx), ptr);
796             gost_imit_ctx->key_set = 1;
797             return 1;
798                                                 }
799             GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
800             return 0;
801         }
802     case EVP_MD_CTRL_MAC_LEN:
803         {
804             struct ossl_gost_imit_ctx *c = ctx->md_data;
805             if (arg < 1 || arg > 8) {
806                 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
807                 return 0;
808             }
809             c->dgst_size=arg;
810             return 1;
811         }
812
813     default:
814         return 0;
815     }
816 }
817
818 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
819 {
820     memcpy(to->md_data, from->md_data, sizeof(struct ossl_gost_imit_ctx));
821     return 1;
822 }
823
824 /* Clean up imit ctx */
825 int gost_imit_cleanup(EVP_MD_CTX *ctx)
826 {
827     memset(ctx->md_data, 0, sizeof(struct ossl_gost_imit_ctx));
828     return 1;
829 }