]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_crypt.c
Finalized magma-cbc
[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 static int magma_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
50                                 const unsigned char *iv, int enc);
51 /* Handles block of data in CBC mode */
52 static int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
53                               const unsigned char *in, size_t inl);
54 static EVP_CIPHER *_hidden_Gost28147_89_cipher = NULL;
55 const EVP_CIPHER *cipher_gost(void)
56 {
57     if (_hidden_Gost28147_89_cipher == NULL
58         && ((_hidden_Gost28147_89_cipher =
59              EVP_CIPHER_meth_new(NID_id_Gost28147_89, 1 /* block_size */ ,
60                                  32 /* key_size */ )) == NULL
61             || !EVP_CIPHER_meth_set_iv_length(_hidden_Gost28147_89_cipher, 8)
62             || !EVP_CIPHER_meth_set_flags(_hidden_Gost28147_89_cipher,
63                                           EVP_CIPH_CFB_MODE |
64                                           EVP_CIPH_NO_PADDING |
65                                           EVP_CIPH_CUSTOM_IV |
66                                           EVP_CIPH_RAND_KEY |
67                                           EVP_CIPH_ALWAYS_CALL_INIT)
68             || !EVP_CIPHER_meth_set_init(_hidden_Gost28147_89_cipher,
69                                          gost_cipher_init)
70             || !EVP_CIPHER_meth_set_do_cipher(_hidden_Gost28147_89_cipher,
71                                               gost_cipher_do_cfb)
72             || !EVP_CIPHER_meth_set_cleanup(_hidden_Gost28147_89_cipher,
73                                             gost_cipher_cleanup)
74             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_Gost28147_89_cipher,
75                                                   sizeof(struct
76                                                          ossl_gost_cipher_ctx))
77             ||
78             !EVP_CIPHER_meth_set_set_asn1_params(_hidden_Gost28147_89_cipher,
79                                                  gost89_set_asn1_parameters)
80             ||
81             !EVP_CIPHER_meth_set_get_asn1_params(_hidden_Gost28147_89_cipher,
82                                                  gost89_get_asn1_parameters)
83             || !EVP_CIPHER_meth_set_ctrl(_hidden_Gost28147_89_cipher,
84                                          gost_cipher_ctl))) {
85         EVP_CIPHER_meth_free(_hidden_Gost28147_89_cipher);
86         _hidden_Gost28147_89_cipher = NULL;
87     }
88     return _hidden_Gost28147_89_cipher;
89 }
90
91 static EVP_CIPHER *_hidden_Gost28147_89_cbc = NULL;
92 const EVP_CIPHER *cipher_gost_cbc(void)
93 {
94     if (_hidden_Gost28147_89_cbc == NULL
95         && ((_hidden_Gost28147_89_cbc =
96              EVP_CIPHER_meth_new(NID_gost89_cbc, 8 /* block_size */ ,
97                                  32 /* key_size */ )) == NULL
98             || !EVP_CIPHER_meth_set_iv_length(_hidden_Gost28147_89_cbc, 8)
99             || !EVP_CIPHER_meth_set_flags(_hidden_Gost28147_89_cbc,
100                                           EVP_CIPH_CBC_MODE |
101                                           EVP_CIPH_CUSTOM_IV |
102                                           EVP_CIPH_RAND_KEY |
103                                           EVP_CIPH_ALWAYS_CALL_INIT)
104             || !EVP_CIPHER_meth_set_init(_hidden_Gost28147_89_cbc,
105                                          gost_cipher_init_cbc)
106             || !EVP_CIPHER_meth_set_do_cipher(_hidden_Gost28147_89_cbc,
107                                               gost_cipher_do_cbc)
108             || !EVP_CIPHER_meth_set_cleanup(_hidden_Gost28147_89_cbc,
109                                             gost_cipher_cleanup)
110             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_Gost28147_89_cbc,
111                                                   sizeof(struct
112                                                          ossl_gost_cipher_ctx))
113             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_Gost28147_89_cbc,
114                                                     gost89_set_asn1_parameters)
115             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_Gost28147_89_cbc,
116                                                     gost89_get_asn1_parameters)
117             || !EVP_CIPHER_meth_set_ctrl(_hidden_Gost28147_89_cbc,
118                                          gost_cipher_ctl))) {
119         EVP_CIPHER_meth_free(_hidden_Gost28147_89_cbc);
120         _hidden_Gost28147_89_cbc = NULL;
121     }
122     return _hidden_Gost28147_89_cbc;
123 }
124
125 static EVP_CIPHER *_hidden_gost89_cnt = NULL;
126 const EVP_CIPHER *cipher_gost_cpacnt(void)
127 {
128     if (_hidden_gost89_cnt == NULL
129         && ((_hidden_gost89_cnt =
130              EVP_CIPHER_meth_new(NID_gost89_cnt, 1 /* block_size */ ,
131                                  32 /* key_size */ )) == NULL
132             || !EVP_CIPHER_meth_set_iv_length(_hidden_gost89_cnt, 8)
133             || !EVP_CIPHER_meth_set_flags(_hidden_gost89_cnt,
134                                           EVP_CIPH_OFB_MODE |
135                                           EVP_CIPH_NO_PADDING |
136                                           EVP_CIPH_CUSTOM_IV |
137                                           EVP_CIPH_RAND_KEY |
138                                           EVP_CIPH_ALWAYS_CALL_INIT)
139             || !EVP_CIPHER_meth_set_init(_hidden_gost89_cnt,
140                                          gost_cipher_init_cpa)
141             || !EVP_CIPHER_meth_set_do_cipher(_hidden_gost89_cnt,
142                                               gost_cipher_do_cnt)
143             || !EVP_CIPHER_meth_set_cleanup(_hidden_gost89_cnt,
144                                             gost_cipher_cleanup)
145             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_gost89_cnt,
146                                                   sizeof(struct
147                                                          ossl_gost_cipher_ctx))
148             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_gost89_cnt,
149                                                     gost89_set_asn1_parameters)
150             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_gost89_cnt,
151                                                     gost89_get_asn1_parameters)
152             || !EVP_CIPHER_meth_set_ctrl(_hidden_gost89_cnt,
153                                          gost_cipher_ctl))) {
154         EVP_CIPHER_meth_free(_hidden_gost89_cnt);
155         _hidden_gost89_cnt = NULL;
156     }
157     return _hidden_gost89_cnt;
158 }
159
160 static EVP_CIPHER *_hidden_gost89_cnt_12 = NULL;
161 const EVP_CIPHER *cipher_gost_cpcnt_12(void)
162 {
163     if (_hidden_gost89_cnt_12 == NULL
164         && ((_hidden_gost89_cnt_12 =
165              EVP_CIPHER_meth_new(NID_gost89_cnt_12, 1 /* block_size */ ,
166                                  32 /* key_size */ )) == NULL
167             || !EVP_CIPHER_meth_set_iv_length(_hidden_gost89_cnt_12, 8)
168             || !EVP_CIPHER_meth_set_flags(_hidden_gost89_cnt_12,
169                                           EVP_CIPH_OFB_MODE |
170                                           EVP_CIPH_NO_PADDING |
171                                           EVP_CIPH_CUSTOM_IV |
172                                           EVP_CIPH_RAND_KEY |
173                                           EVP_CIPH_ALWAYS_CALL_INIT)
174             || !EVP_CIPHER_meth_set_init(_hidden_gost89_cnt_12,
175                                          gost_cipher_init_cp_12)
176             || !EVP_CIPHER_meth_set_do_cipher(_hidden_gost89_cnt_12,
177                                               gost_cipher_do_cnt)
178             || !EVP_CIPHER_meth_set_cleanup(_hidden_gost89_cnt_12,
179                                             gost_cipher_cleanup)
180             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_gost89_cnt_12,
181                                                   sizeof(struct
182                                                          ossl_gost_cipher_ctx))
183             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_gost89_cnt_12,
184                                                     gost89_set_asn1_parameters)
185             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_gost89_cnt_12,
186                                                     gost89_get_asn1_parameters)
187             || !EVP_CIPHER_meth_set_ctrl(_hidden_gost89_cnt_12,
188                                          gost_cipher_ctl))) {
189         EVP_CIPHER_meth_free(_hidden_gost89_cnt_12);
190         _hidden_gost89_cnt_12 = NULL;
191     }
192     return _hidden_gost89_cnt_12;
193 }
194
195 static EVP_CIPHER *_hidden_magma_cbc = NULL;
196 const EVP_CIPHER *cipher_magma_cbc(void)
197 {
198     if (_hidden_magma_cbc == NULL
199         && ((_hidden_magma_cbc =
200              EVP_CIPHER_meth_new(NID_magma_cbc, 8 /* block_size */ ,
201                                  32 /* key_size */ )) == NULL
202             || !EVP_CIPHER_meth_set_iv_length(_hidden_magma_cbc, 8)
203             || !EVP_CIPHER_meth_set_flags(_hidden_magma_cbc,
204                                           EVP_CIPH_CBC_MODE |
205                                           EVP_CIPH_CUSTOM_IV |
206                                           EVP_CIPH_RAND_KEY |
207                                           EVP_CIPH_ALWAYS_CALL_INIT)
208             || !EVP_CIPHER_meth_set_init(_hidden_magma_cbc,
209                                          magma_cipher_init_cbc)
210             || !EVP_CIPHER_meth_set_do_cipher(_hidden_magma_cbc,
211                                               magma_cipher_do_cbc)
212             || !EVP_CIPHER_meth_set_cleanup(_hidden_magma_cbc,
213                                             gost_cipher_cleanup)
214             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_magma_cbc,
215                                                   sizeof(struct
216                                                          ossl_gost_cipher_ctx))
217             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_magma_cbc,
218                                                     gost89_set_asn1_parameters)
219             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_magma_cbc,
220                                                     gost89_get_asn1_parameters)
221             || !EVP_CIPHER_meth_set_ctrl(_hidden_magma_cbc,
222                                          gost_cipher_ctl))) {
223         EVP_CIPHER_meth_free(_hidden_magma_cbc);
224         _hidden_magma_cbc = NULL;
225     }
226     return _hidden_magma_cbc;
227 }
228
229 void cipher_gost_destroy(void)
230 {
231     EVP_CIPHER_meth_free(_hidden_Gost28147_89_cipher);
232     _hidden_Gost28147_89_cipher = NULL;
233     EVP_CIPHER_meth_free(_hidden_gost89_cnt);
234     _hidden_gost89_cnt = NULL;
235     EVP_CIPHER_meth_free(_hidden_Gost28147_89_cbc);
236     _hidden_Gost28147_89_cbc = NULL;
237     EVP_CIPHER_meth_free(_hidden_gost89_cnt_12);
238     _hidden_gost89_cnt_12 = NULL;
239 }
240
241 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
242 /* Init functions which set specific parameters */
243 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
244 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx);
245 /* process block of data */
246 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
247 /* Return computed value */
248 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
249 /* Copies context */
250 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
251 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
252 /* Control function, knows how to set MAC key.*/
253 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
254
255 static EVP_MD *_hidden_Gost28147_89_MAC_md = NULL;
256 static EVP_MD *_hidden_Gost28147_89_12_MAC_md = NULL;
257
258 EVP_MD *imit_gost_cpa(void)
259 {
260     if (_hidden_Gost28147_89_MAC_md == NULL) {
261         EVP_MD *md;
262
263         if ((md = EVP_MD_meth_new(NID_id_Gost28147_89_MAC, NID_undef)) == NULL
264             || !EVP_MD_meth_set_result_size(md, 4)
265             || !EVP_MD_meth_set_input_blocksize(md, 8)
266             || !EVP_MD_meth_set_app_datasize(md,
267                                              sizeof(struct
268                                                     ossl_gost_imit_ctx))
269             || !EVP_MD_meth_set_flags(md, 0)
270             || !EVP_MD_meth_set_init(md, gost_imit_init_cpa)
271             || !EVP_MD_meth_set_update(md, gost_imit_update)
272             || !EVP_MD_meth_set_final(md, gost_imit_final)
273             || !EVP_MD_meth_set_copy(md, gost_imit_copy)
274             || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
275             || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
276             EVP_MD_meth_free(md);
277             md = NULL;
278         }
279         _hidden_Gost28147_89_MAC_md = md;
280     }
281     return _hidden_Gost28147_89_MAC_md;
282 }
283
284 void imit_gost_cpa_destroy(void)
285 {
286     EVP_MD_meth_free(_hidden_Gost28147_89_MAC_md);
287     _hidden_Gost28147_89_MAC_md = NULL;
288 }
289
290 EVP_MD *imit_gost_cp_12(void)
291 {
292     if (_hidden_Gost28147_89_12_MAC_md == NULL) {
293         EVP_MD *md;
294
295         if ((md = EVP_MD_meth_new(NID_gost_mac_12, NID_undef)) == NULL
296             || !EVP_MD_meth_set_result_size(md, 4)
297             || !EVP_MD_meth_set_input_blocksize(md, 8)
298             || !EVP_MD_meth_set_app_datasize(md,
299                                              sizeof(struct
300                                                     ossl_gost_imit_ctx))
301             || !EVP_MD_meth_set_flags(md, 0)
302             || !EVP_MD_meth_set_init(md, gost_imit_init_cp_12)
303             || !EVP_MD_meth_set_update(md, gost_imit_update)
304             || !EVP_MD_meth_set_final(md, gost_imit_final)
305             || !EVP_MD_meth_set_copy(md, gost_imit_copy)
306             || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
307             || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
308             EVP_MD_meth_free(md);
309             md = NULL;
310         }
311         _hidden_Gost28147_89_12_MAC_md = md;
312     }
313     return _hidden_Gost28147_89_12_MAC_md;
314 }
315
316 void imit_gost_cp_12_destroy(void)
317 {
318     EVP_MD_meth_free(_hidden_Gost28147_89_12_MAC_md);
319     _hidden_Gost28147_89_12_MAC_md = NULL;
320 }
321
322 /*
323  * Correspondence between gost parameter OIDs and substitution blocks
324  * NID field is filed by register_gost_NID function in engine.c
325  * upon engine initialization
326  */
327
328 struct gost_cipher_info gost_cipher_list[] = {
329     /*- NID *//*
330      * Subst block
331      *//*
332      * Key meshing
333      */
334     /*
335      * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
336      */
337     {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
338      1},
339     {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
340      1},
341     {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
342      1},
343     {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
344      1},
345     {NID_id_tc26_gost_28147_param_Z, &Gost28147_TC26ParamSetZ, 1},
346     {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
347     {NID_undef, NULL, 0}
348 };
349
350 /*
351  * get encryption parameters from crypto network settings FIXME For now we
352  * use environment var CRYPT_PARAMS as place to store these settings.
353  * Actually, it is better to use engine control command, read from
354  * configuration file to set them
355  */
356 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
357 {
358     int nid;
359     struct gost_cipher_info *param;
360     if (!obj) {
361         const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
362         if (!params || !strlen(params)) {
363             int i;
364             for (i = 0; gost_cipher_list[i].nid != NID_undef; i++)
365                 if (gost_cipher_list[i].nid == NID_id_tc26_gost_28147_param_Z)
366                     return &gost_cipher_list[i];
367             return &gost_cipher_list[0];
368         }
369
370         nid = OBJ_txt2nid(params);
371         if (nid == NID_undef) {
372             GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
373                     GOST_R_INVALID_CIPHER_PARAM_OID);
374             return NULL;
375         }
376     } else {
377         nid = OBJ_obj2nid(obj);
378     }
379     for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
380          param++) ;
381     if (!param->sblock) {
382         GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
383         return NULL;
384     }
385     return param;
386 }
387
388 /* Sets cipher param from paramset NID. */
389 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
390 {
391     const struct gost_cipher_info *param;
392     param =
393         get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
394     if (!param)
395         return 0;
396
397     c->paramNID = param->nid;
398     c->key_meshing = param->key_meshing;
399     c->count = 0;
400     gost_init(&(c->cctx), param->sblock);
401     return 1;
402 }
403
404 /* Initializes EVP_CIPHER_CTX by paramset NID */
405 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
406                                   const unsigned char *key,
407                                   const unsigned char *iv, int enc,
408                                   int paramNID, int mode)
409 {
410     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
411     if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
412         if (!gost_cipher_set_param(c, paramNID))
413             return 0;
414         EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
415     }
416     if (key)
417         gost_key(&(c->cctx), key);
418     if (iv) {
419         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
420                EVP_CIPHER_CTX_iv_length(ctx));
421     }
422     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
423            EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
424     return 1;
425 }
426
427 static int magma_cipher_init_param(EVP_CIPHER_CTX *ctx,
428                                   const unsigned char *key,
429                                   const unsigned char *iv, int enc,
430                                   int paramNID, int mode)
431 {
432     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
433     if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
434         if (!gost_cipher_set_param(c, NID_id_tc26_gost_28147_param_Z))
435             return 0;
436         EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
437     }
438     if (key)
439         magma_key(&(c->cctx), key);
440     if (iv) {
441         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
442                EVP_CIPHER_CTX_iv_length(ctx));
443     }
444     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
445            EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
446     return 1;
447 }
448
449 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
450                                 const unsigned char *key,
451                                 const unsigned char *iv,
452                                 gost_subst_block * block)
453 {
454     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
455     gost_init(&(c->cctx), block);
456     c->key_meshing = 1;
457     c->count = 0;
458     if (key)
459         gost_key(&(c->cctx), key);
460     if (iv) {
461         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
462                EVP_CIPHER_CTX_iv_length(ctx));
463     }
464     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
465            EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
466     return 1;
467 }
468
469 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
470                                 const unsigned char *iv, int enc)
471 {
472     return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
473 }
474
475 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
476                                   const unsigned char *key,
477                                   const unsigned char *iv, int enc)
478 {
479     return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
480 }
481
482 /* Initializes EVP_CIPHER_CTX with default values */
483 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
484                      const unsigned char *iv, int enc)
485 {
486     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
487                                   EVP_CIPH_CFB_MODE);
488 }
489
490 /* Initializes EVP_CIPHER_CTX with default values */
491 int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
492                          const unsigned char *iv, int enc)
493 {
494     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
495                                   EVP_CIPH_CBC_MODE);
496 }
497
498 /* Initializes EVP_CIPHER_CTX with default values */
499 int magma_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
500                          const unsigned char *iv, int enc)
501 {
502     return magma_cipher_init_param(ctx, key, iv, enc, NID_undef,
503                                   EVP_CIPH_CBC_MODE);
504 }
505
506 /*
507  * Wrapper around gostcrypt function from gost89.c which perform key meshing
508  * when nesseccary
509  */
510 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
511 {
512     struct ossl_gost_cipher_ctx *c = ctx;
513     assert(c->count % 8 == 0 && c->count <= 1024);
514     if (c->key_meshing && c->count == 1024) {
515         cryptopro_key_meshing(&(c->cctx), iv);
516     }
517     gostcrypt(&(c->cctx), iv, buf);
518     c->count = c->count % 1024 + 8;
519 }
520
521 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
522 {
523     struct ossl_gost_cipher_ctx *c = ctx;
524     word32 g, go;
525     unsigned char buf1[8];
526     assert(c->count % 8 == 0 && c->count <= 1024);
527     if (c->key_meshing && c->count == 1024) {
528         cryptopro_key_meshing(&(c->cctx), iv);
529     }
530     if (c->count == 0) {
531         gostcrypt(&(c->cctx), iv, buf1);
532     } else {
533         memcpy(buf1, iv, 8);
534     }
535     g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
536     g += 0x01010101;
537     buf1[0] = (unsigned char)(g & 0xff);
538     buf1[1] = (unsigned char)((g >> 8) & 0xff);
539     buf1[2] = (unsigned char)((g >> 16) & 0xff);
540     buf1[3] = (unsigned char)((g >> 24) & 0xff);
541     g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
542     go = g;
543     g += 0x01010104;
544     if (go > g)                 /* overflow */
545         g++;
546     buf1[4] = (unsigned char)(g & 0xff);
547     buf1[5] = (unsigned char)((g >> 8) & 0xff);
548     buf1[6] = (unsigned char)((g >> 16) & 0xff);
549     buf1[7] = (unsigned char)((g >> 24) & 0xff);
550     memcpy(iv, buf1, 8);
551     gostcrypt(&(c->cctx), buf1, buf);
552     c->count = c->count % 1024 + 8;
553 }
554
555 /* GOST encryption in CBC mode */
556 int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
557                        const unsigned char *in, size_t inl)
558 {
559     unsigned char b[8];
560     const unsigned char *in_ptr = in;
561     unsigned char *out_ptr = out;
562     int i;
563     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
564     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
565     if (EVP_CIPHER_CTX_encrypting(ctx)) {
566         while (inl > 0) {
567
568             for (i = 0; i < 8; i++) {
569                 b[i] = iv[i] ^ in_ptr[i];
570             }
571             gostcrypt(&(c->cctx), b, out_ptr);
572             memcpy(iv, out_ptr, 8);
573             out_ptr += 8;
574             in_ptr += 8;
575             inl -= 8;
576         }
577     } else {
578         while (inl > 0) {
579             gostdecrypt(&(c->cctx), in_ptr, b);
580             for (i = 0; i < 8; i++) {
581                 out_ptr[i] = iv[i] ^ b[i];
582             }
583             memcpy(iv, in_ptr, 8);
584             out_ptr += 8;
585             in_ptr += 8;
586             inl -= 8;
587         }
588     }
589     return 1;
590 }
591
592 /* GOST encryption in CBC mode */
593 int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
594                        const unsigned char *in, size_t inl)
595 {
596     unsigned char b[8];
597                 unsigned char d[8];
598     const unsigned char *in_ptr = in;
599     unsigned char *out_ptr = out;
600     int i;
601     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
602     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
603     if (EVP_CIPHER_CTX_encrypting(ctx)) {
604         while (inl > 0) {
605
606             for (i = 0; i < 8; i++) {
607                 b[7-i] = iv[i] ^ in_ptr[i];
608             }
609             gostcrypt(&(c->cctx), b, d);
610
611             for (i = 0; i < 8; i++) {
612                 out_ptr[7-i] = d[i];
613             }
614             memcpy(iv, out_ptr, 8);
615             out_ptr += 8;
616             in_ptr += 8;
617             inl -= 8;
618         }
619     } else {
620         while (inl > 0) {
621             for (i = 0; i < 8; i++) {
622                 d[7-i] = in_ptr[i];
623             }
624             gostdecrypt(&(c->cctx), d, b);
625             for (i = 0; i < 8; i++) {
626                 out_ptr[i] = iv[i] ^ b[7-i];
627             }
628             memcpy(iv, in_ptr, 8);
629             out_ptr += 8;
630             in_ptr += 8;
631             inl -= 8;
632         }
633     }
634     return 1;
635 }
636
637 /* GOST encryption in CFB mode */
638 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
639                        const unsigned char *in, size_t inl)
640 {
641     const unsigned char *in_ptr = in;
642     unsigned char *out_ptr = out;
643     size_t i = 0;
644     size_t j = 0;
645     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
646     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
647 /* process partial block if any */
648     if (EVP_CIPHER_CTX_num(ctx)) {
649         for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
650              j++, i++, in_ptr++, out_ptr++) {
651             if (!EVP_CIPHER_CTX_encrypting(ctx))
652                 buf[j + 8] = *in_ptr;
653             *out_ptr = buf[j] ^ (*in_ptr);
654             if (EVP_CIPHER_CTX_encrypting(ctx))
655                 buf[j + 8] = *out_ptr;
656         }
657         if (j == 8) {
658             memcpy(iv, buf + 8, 8);
659             EVP_CIPHER_CTX_set_num(ctx, 0);
660         } else {
661             EVP_CIPHER_CTX_set_num(ctx, j);
662             return 1;
663         }
664     }
665
666     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
667         /*
668          * block cipher current iv
669          */
670         gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
671         /*
672          * xor next block of input text with it and output it
673          */
674         /*
675          * output this block
676          */
677         if (!EVP_CIPHER_CTX_encrypting(ctx))
678             memcpy(iv, in_ptr, 8);
679         for (j = 0; j < 8; j++) {
680             out_ptr[j] = buf[j] ^ in_ptr[j];
681         }
682         /* Encrypt */
683         /* Next iv is next block of cipher text */
684         if (EVP_CIPHER_CTX_encrypting(ctx))
685             memcpy(iv, out_ptr, 8);
686     }
687 /* Process rest of buffer */
688     if (i < inl) {
689         gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
690         if (!EVP_CIPHER_CTX_encrypting(ctx))
691             memcpy(buf + 8, in_ptr, inl - i);
692         for (j = 0; i < inl; j++, i++) {
693             out_ptr[j] = buf[j] ^ in_ptr[j];
694         }
695         EVP_CIPHER_CTX_set_num(ctx, j);
696         if (EVP_CIPHER_CTX_encrypting(ctx))
697             memcpy(buf + 8, out_ptr, j);
698     } else {
699         EVP_CIPHER_CTX_set_num(ctx, 0);
700     }
701     return 1;
702 }
703
704 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
705                               const unsigned char *in, size_t inl)
706 {
707     const unsigned char *in_ptr = in;
708     unsigned char *out_ptr = out;
709     size_t i = 0;
710     size_t j;
711     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
712     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
713 /* process partial block if any */
714     if (EVP_CIPHER_CTX_num(ctx)) {
715         for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
716              j++, i++, in_ptr++, out_ptr++) {
717             *out_ptr = buf[j] ^ (*in_ptr);
718         }
719         if (j == 8) {
720             EVP_CIPHER_CTX_set_num(ctx, 0);
721         } else {
722             EVP_CIPHER_CTX_set_num(ctx, j);
723             return 1;
724         }
725     }
726
727     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
728         /*
729          * block cipher current iv
730          */
731         /* Encrypt */
732         gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
733         /*
734          * xor next block of input text with it and output it
735          */
736         /*
737          * output this block
738          */
739         for (j = 0; j < 8; j++) {
740             out_ptr[j] = buf[j] ^ in_ptr[j];
741         }
742     }
743 /* Process rest of buffer */
744     if (i < inl) {
745         gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
746         for (j = 0; i < inl; j++, i++) {
747             out_ptr[j] = buf[j] ^ in_ptr[j];
748         }
749         EVP_CIPHER_CTX_set_num(ctx, j);
750     } else {
751         EVP_CIPHER_CTX_set_num(ctx, 0);
752     }
753     return 1;
754 }
755
756 /* Cleaning up of EVP_CIPHER_CTX */
757 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
758 {
759     gost_destroy(&
760                  ((struct ossl_gost_cipher_ctx *)
761                   EVP_CIPHER_CTX_get_cipher_data(ctx))->cctx);
762     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
763     return 1;
764 }
765
766 /* Control function for gost cipher */
767 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
768 {
769     switch (type) {
770 #if 0
771     case EVP_CTRL_INIT:
772         {
773             struct ossl_gost_cipher_ctx *c =
774                 EVP_CIPHER_CTX_get_cipher_data(ctx);
775             if (c == NULL) {
776                 return -1;
777             }
778             return gost_cipher_set_param(c, arg);
779         }
780 #endif
781     case EVP_CTRL_RAND_KEY:
782         {
783             if (RAND_bytes
784                 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
785                 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
786                 return -1;
787             }
788             break;
789         }
790     case EVP_CTRL_PBE_PRF_NID:
791         if (ptr) {
792             const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
793             int nid = NID_id_tc26_hmac_gost_3411_2012_512;
794
795             if (params) {
796                 if (!strcmp("md_gost12_256", params))
797                     nid = NID_id_tc26_hmac_gost_3411_2012_256;
798                 else if (!strcmp("md_gost12_512", params))
799                     nid = NID_id_tc26_hmac_gost_3411_2012_512;
800                 else if (!strcmp("md_gost94", params))
801                     nid = NID_id_HMACGostR3411_94;
802             }
803             *((int *)ptr) = nid;
804             return 1;
805         } else {
806             return 0;
807         }
808
809     case EVP_CTRL_SET_SBOX:
810         if (ptr) {
811             struct ossl_gost_cipher_ctx *c =
812                 EVP_CIPHER_CTX_get_cipher_data(ctx);
813             int nid;
814             int cur_meshing;
815             int ret;
816
817             if (c == NULL) {
818                 return -1;
819             }
820
821             if (c->count != 0) {
822                 return -1;
823             }
824
825             nid = OBJ_txt2nid(ptr);
826             if (nid == NID_undef) {
827                 return 0;
828             }
829
830             cur_meshing = c->key_meshing;
831             ret = gost_cipher_set_param(c, nid);
832             c->key_meshing = cur_meshing;
833             return ret;
834         } else {
835             return 0;
836         }
837     case EVP_CTRL_KEY_MESH:
838         {
839             struct ossl_gost_cipher_ctx *c =
840                 EVP_CIPHER_CTX_get_cipher_data(ctx);
841
842             if (c == NULL) {
843                 return -1;
844             }
845
846             if (c->count != 0) {
847                 return -1;
848             }
849
850             c->key_meshing = arg;
851             return 1;
852         }
853     default:
854         GOSTerr(GOST_F_GOST_CIPHER_CTL,
855                 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
856         return -1;
857     }
858     return 1;
859 }
860
861 /* Set cipher parameters from ASN1 structure */
862 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
863 {
864     int len = 0;
865     unsigned char *buf = NULL;
866     unsigned char *p = NULL;
867     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
868     GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
869     ASN1_OCTET_STRING *os = NULL;
870     if (!gcp) {
871         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
872         return 0;
873     }
874     if (!ASN1_OCTET_STRING_set
875         (gcp->iv, EVP_CIPHER_CTX_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx))) {
876         GOST_CIPHER_PARAMS_free(gcp);
877         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
878         return 0;
879     }
880     ASN1_OBJECT_free(gcp->enc_param_set);
881     gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
882
883     len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
884     p = buf = OPENSSL_malloc(len);
885     if (!buf) {
886         GOST_CIPHER_PARAMS_free(gcp);
887         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
888         return 0;
889     }
890     i2d_GOST_CIPHER_PARAMS(gcp, &p);
891     GOST_CIPHER_PARAMS_free(gcp);
892
893     os = ASN1_OCTET_STRING_new();
894
895     if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
896         OPENSSL_free(buf);
897         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
898         return 0;
899     }
900     OPENSSL_free(buf);
901
902     ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
903     return 1;
904 }
905
906 /* Store parameters into ASN1 structure */
907 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
908 {
909     int ret = -1;
910     int len;
911     GOST_CIPHER_PARAMS *gcp = NULL;
912     unsigned char *p;
913     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
914     int nid;
915
916     if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
917         return ret;
918     }
919
920     p = params->value.sequence->data;
921
922     gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
923                                  params->value.sequence->length);
924
925     len = gcp->iv->length;
926     if (len != EVP_CIPHER_CTX_iv_length(ctx)) {
927         GOST_CIPHER_PARAMS_free(gcp);
928         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
929         return -1;
930     }
931
932     nid = OBJ_obj2nid(gcp->enc_param_set);
933     if (nid == NID_undef) {
934         GOST_CIPHER_PARAMS_free(gcp);
935         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
936                 GOST_R_INVALID_CIPHER_PARAM_OID);
937         return -1;
938     }
939
940     if (!gost_cipher_set_param(c, nid)) {
941         GOST_CIPHER_PARAMS_free(gcp);
942         return -1;
943     }
944     /*XXX missing non-const accessor */
945     memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), gcp->iv->data,
946            EVP_CIPHER_CTX_iv_length(ctx));
947
948     GOST_CIPHER_PARAMS_free(gcp);
949
950     return 1;
951 }
952
953 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
954 {
955     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
956     memset(c->buffer, 0, sizeof(c->buffer));
957     memset(c->partial_block, 0, sizeof(c->partial_block));
958     c->count = 0;
959     c->bytes_left = 0;
960     c->key_meshing = 1;
961     c->dgst_size = 4;
962     gost_init(&(c->cctx), block);
963     return 1;
964 }
965
966 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
967 {
968     return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
969 }
970
971 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
972 {
973     return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
974 }
975
976 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
977                            const unsigned char *data)
978 {
979     unsigned char buffer[8];
980     /*
981      * We are using local buffer for iv because CryptoPro doesn't interpret
982      * internal state of MAC algorithm as iv during keymeshing (but does
983      * initialize internal state from iv in key transport
984      */
985     assert(c->count % 8 == 0 && c->count <= 1024);
986     if (c->key_meshing && c->count == 1024) {
987         cryptopro_key_meshing(&(c->cctx), buffer);
988     }
989     mac_block(&(c->cctx), c->buffer, data);
990     c->count = c->count % 1024 + 8;
991 }
992
993 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
994 {
995     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
996     const unsigned char *p = data;
997     size_t bytes = count, i;
998     if (!(c->key_set)) {
999         GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
1000         return 0;
1001     }
1002     if (c->bytes_left) {
1003         for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
1004             c->partial_block[i] = *p;
1005         }
1006         if (i == 8) {
1007             mac_block_mesh(c, c->partial_block);
1008         } else {
1009             c->bytes_left = i;
1010             return 1;
1011         }
1012     }
1013     while (bytes > 8) {
1014         mac_block_mesh(c, p);
1015         p += 8;
1016         bytes -= 8;
1017     }
1018     if (bytes > 0) {
1019         memcpy(c->partial_block, p, bytes);
1020     }
1021     c->bytes_left = bytes;
1022     return 1;
1023 }
1024
1025 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
1026 {
1027     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1028     if (!c->key_set) {
1029         GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
1030         return 0;
1031     }
1032     if (c->count == 0 && c->bytes_left) {
1033         unsigned char buffer[8];
1034         memset(buffer, 0, 8);
1035         gost_imit_update(ctx, buffer, 8);
1036     }
1037     if (c->bytes_left) {
1038         int i;
1039         for (i = c->bytes_left; i < 8; i++) {
1040             c->partial_block[i] = 0;
1041         }
1042         mac_block_mesh(c, c->partial_block);
1043     }
1044     get_mac(c->buffer, 8 * c->dgst_size, md);
1045     return 1;
1046 }
1047
1048 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
1049 {
1050     switch (type) {
1051     case EVP_MD_CTRL_KEY_LEN:
1052         *((unsigned int *)(ptr)) = 32;
1053         return 1;
1054     case EVP_MD_CTRL_SET_KEY:
1055         {
1056             struct ossl_gost_imit_ctx *gost_imit_ctx =
1057                 EVP_MD_CTX_md_data(ctx);
1058
1059             if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
1060                 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
1061                 return 0;
1062             }
1063             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
1064
1065             if (arg == 0) {
1066                 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
1067                 if (key->mac_param_nid != NID_undef) {
1068                     const struct gost_cipher_info *param =
1069                         get_encryption_params(OBJ_nid2obj
1070                                               (key->mac_param_nid));
1071                     if (param == NULL) {
1072                         GOSTerr(GOST_F_GOST_IMIT_CTRL,
1073                                 GOST_R_INVALID_MAC_PARAMS);
1074                         return 0;
1075                     }
1076                     gost_init(&(gost_imit_ctx->cctx), param->sblock);
1077                 }
1078                 gost_key(&(gost_imit_ctx->cctx), key->key);
1079                 gost_imit_ctx->key_set = 1;
1080
1081                 return 1;
1082             } else if (arg == 32) {
1083                 gost_key(&(gost_imit_ctx->cctx), ptr);
1084                 gost_imit_ctx->key_set = 1;
1085                 return 1;
1086             }
1087             GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
1088             return 0;
1089         }
1090     case EVP_MD_CTRL_MAC_LEN:
1091         {
1092             struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1093             if (arg < 1 || arg > 8) {
1094                 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
1095                 return 0;
1096             }
1097             c->dgst_size = arg;
1098             return 1;
1099         }
1100
1101     default:
1102         return 0;
1103     }
1104 }
1105
1106 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1107 {
1108     if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
1109         memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
1110                sizeof(struct ossl_gost_imit_ctx));
1111     }
1112     return 1;
1113 }
1114
1115 /* Clean up imit ctx */
1116 int gost_imit_cleanup(EVP_MD_CTX *ctx)
1117 {
1118     memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
1119     return 1;
1120 }