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