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