]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_crypt.c
Update INSTALL.md
[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     if (EVP_CIPHER_CTX_nid(ctx) == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm
481      || EVP_CIPHER_CTX_nid(ctx) == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac) {
482        c->key_meshing = 1024;
483     } else {
484        c->key_meshing = 0;
485     }
486
487     return 1;
488 }
489
490 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
491                                 const unsigned char *key,
492                                 const unsigned char *iv,
493                                 gost_subst_block * block)
494 {
495     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
496     gost_init(&(c->cctx), block);
497     c->key_meshing = 1;
498     c->count = 0;
499     if (key)
500         gost_key(&(c->cctx), key);
501     if (iv) {
502         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
503                EVP_CIPHER_CTX_iv_length(ctx));
504     }
505     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
506            EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
507     return 1;
508 }
509
510 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
511                                 const unsigned char *iv, int enc)
512 {
513     return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
514 }
515
516 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
517                                   const unsigned char *key,
518                                   const unsigned char *iv, int enc)
519 {
520     return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
521 }
522
523 /* Initializes EVP_CIPHER_CTX with default values */
524 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
525                      const unsigned char *iv, int enc)
526 {
527     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
528                                   EVP_CIPH_CFB_MODE);
529 }
530
531 /* Initializes EVP_CIPHER_CTX with default values */
532 int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
533                          const unsigned char *iv, int enc)
534 {
535     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
536                                   EVP_CIPH_CBC_MODE);
537 }
538
539 /* Initializes EVP_CIPHER_CTX with default values */
540 int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
541                       const unsigned char *iv, int enc)
542 {
543     return magma_cipher_init_param(ctx, key, iv, enc, NID_undef,
544                                    EVP_CIPH_CBC_MODE);
545 }
546
547 /*
548  * Wrapper around gostcrypt function from gost89.c which perform key meshing
549  * when nesseccary
550  */
551 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
552 {
553     struct ossl_gost_cipher_ctx *c = ctx;
554     assert(c->count % 8 == 0 && c->count <= 1024);
555     if (c->key_meshing && c->count == 1024) {
556         cryptopro_key_meshing(&(c->cctx), iv);
557     }
558     gostcrypt(&(c->cctx), iv, buf);
559     c->count = c->count % 1024 + 8;
560 }
561
562 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
563 {
564     struct ossl_gost_cipher_ctx *c = ctx;
565     word32 g, go;
566     unsigned char buf1[8];
567     assert(c->count % 8 == 0 && c->count <= 1024);
568     if (c->key_meshing && c->count == 1024) {
569         cryptopro_key_meshing(&(c->cctx), iv);
570     }
571     if (c->count == 0) {
572         gostcrypt(&(c->cctx), iv, buf1);
573     } else {
574         memcpy(buf1, iv, 8);
575     }
576     g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
577     g += 0x01010101;
578     buf1[0] = (unsigned char)(g & 0xff);
579     buf1[1] = (unsigned char)((g >> 8) & 0xff);
580     buf1[2] = (unsigned char)((g >> 16) & 0xff);
581     buf1[3] = (unsigned char)((g >> 24) & 0xff);
582     g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
583     go = g;
584     g += 0x01010104;
585     if (go > g)                 /* overflow */
586         g++;
587     buf1[4] = (unsigned char)(g & 0xff);
588     buf1[5] = (unsigned char)((g >> 8) & 0xff);
589     buf1[6] = (unsigned char)((g >> 16) & 0xff);
590     buf1[7] = (unsigned char)((g >> 24) & 0xff);
591     memcpy(iv, buf1, 8);
592     gostcrypt(&(c->cctx), buf1, buf);
593     c->count = c->count % 1024 + 8;
594 }
595
596 /* GOST encryption in CBC mode */
597 int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
598                        const unsigned char *in, size_t inl)
599 {
600     unsigned char b[8];
601     const unsigned char *in_ptr = in;
602     unsigned char *out_ptr = out;
603     int i;
604     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
605     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
606     if (EVP_CIPHER_CTX_encrypting(ctx)) {
607         while (inl > 0) {
608
609             for (i = 0; i < 8; i++) {
610                 b[i] = iv[i] ^ in_ptr[i];
611             }
612             gostcrypt(&(c->cctx), b, out_ptr);
613             memcpy(iv, out_ptr, 8);
614             out_ptr += 8;
615             in_ptr += 8;
616             inl -= 8;
617         }
618     } else {
619         while (inl > 0) {
620             unsigned char tmpiv[8];
621             gostdecrypt(&(c->cctx), in_ptr, b);
622             memcpy(tmpiv, in_ptr, 8);
623             for (i = 0; i < 8; i++) {
624                 out_ptr[i] = iv[i] ^ b[i];
625             }
626             memcpy(iv, tmpiv, 8);
627             out_ptr += 8;
628             in_ptr += 8;
629             inl -= 8;
630         }
631     }
632     return 1;
633 }
634
635 /* MAGMA encryption in CBC mode */
636 int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
637                         const unsigned char *in, size_t inl)
638 {
639     unsigned char b[8];
640     unsigned char d[8];
641     const unsigned char *in_ptr = in;
642     unsigned char *out_ptr = out;
643     int i;
644     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
645     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
646     if (EVP_CIPHER_CTX_encrypting(ctx)) {
647         while (inl > 0) {
648
649             for (i = 0; i < 8; i++) {
650                 b[7 - i] = iv[i] ^ in_ptr[i];
651             }
652             gostcrypt(&(c->cctx), b, d);
653
654             for (i = 0; i < 8; i++) {
655                 out_ptr[7 - i] = d[i];
656             }
657             memcpy(iv, out_ptr, 8);
658             out_ptr += 8;
659             in_ptr += 8;
660             inl -= 8;
661         }
662     } else {
663         while (inl > 0) {
664             for (i = 0; i < 8; i++) {
665                 d[7 - i] = in_ptr[i];
666             }
667             gostdecrypt(&(c->cctx), d, b);
668             for (i = 0; i < 8; i++) {
669                 out_ptr[i] = iv[i] ^ b[7 - i];
670             }
671             memcpy(iv, in_ptr, 8);
672             out_ptr += 8;
673             in_ptr += 8;
674             inl -= 8;
675         }
676     }
677     return 1;
678 }
679
680 /* increment counter (64-bit int) by 1 */
681 static void ctr64_inc(unsigned char *counter)
682 {
683     inc_counter(counter, 8);
684 }
685
686 #define MAGMA_BLOCK_SIZE 8
687 #define MAGMA_BLOCK_MASK (MAGMA_BLOCK_SIZE - 1)
688 static inline void apply_acpkm_magma(struct ossl_gost_cipher_ctx *
689                                            ctx, unsigned int *num)
690 {
691     if (!ctx->key_meshing || (*num < ctx->key_meshing))
692         return;
693     acpkm_magma_key_meshing(&ctx->cctx);
694     *num &= MAGMA_BLOCK_MASK;
695 }
696
697 /* MAGMA encryption in CTR mode */
698 static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
699                                const unsigned char *in, size_t inl)
700 {
701     const unsigned char *in_ptr = in;
702     unsigned char *out_ptr = out;
703     size_t j;
704     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
705     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
706     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
707     unsigned int num = EVP_CIPHER_CTX_num(ctx);
708     size_t blocks, i, lasted = inl;
709     unsigned char b[8];
710 /* Process partial blocks */
711     while ((num & MAGMA_BLOCK_MASK) && lasted) {
712         *out_ptr++ = *in_ptr++ ^ buf[7 - (num & MAGMA_BLOCK_MASK)];
713         --lasted;
714         num++;
715     }
716     blocks = lasted / MAGMA_BLOCK_SIZE;
717
718 /* Process full blocks */
719     for (i = 0; i < blocks; i++) {
720         apply_acpkm_magma(c, &num);
721         for (j = 0; j < 8; j++) {
722             b[7 - j] = iv[j];
723         }
724         gostcrypt(&(c->cctx), b, buf);
725         for (j = 0; j < 8; j++) {
726             out_ptr[j] = buf[7 - j] ^ in_ptr[j];
727         }
728         ctr64_inc(iv);
729         c->count += MAGMA_BLOCK_SIZE;
730         in_ptr += MAGMA_BLOCK_SIZE;
731         out_ptr += MAGMA_BLOCK_SIZE;
732         num += MAGMA_BLOCK_SIZE;
733         lasted -= MAGMA_BLOCK_SIZE;
734     }
735
736 /* Process the rest of plaintext */
737     if (lasted > 0) {
738         apply_acpkm_magma(c, &num);
739         for (j = 0; j < 8; j++) {
740             b[7 - j] = iv[j];
741         }
742         gostcrypt(&(c->cctx), b, buf);
743
744         for (i = 0; i < lasted; i++)
745             out_ptr[i] = buf[7 - i] ^ in_ptr[i];
746         ctr64_inc(iv);
747         c->count += j;
748         num += lasted;
749     }
750     EVP_CIPHER_CTX_set_num(ctx, num);
751
752     return 1;
753 }
754
755 /* GOST encryption in CFB mode */
756 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
757                        const unsigned char *in, size_t inl)
758 {
759     const unsigned char *in_ptr = in;
760     unsigned char *out_ptr = out;
761     size_t i = 0;
762     size_t j = 0;
763     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
764     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
765 /* process partial block if any */
766     if (EVP_CIPHER_CTX_num(ctx)) {
767         for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
768              j++, i++, in_ptr++, out_ptr++) {
769             if (!EVP_CIPHER_CTX_encrypting(ctx))
770                 buf[j + 8] = *in_ptr;
771             *out_ptr = buf[j] ^ (*in_ptr);
772             if (EVP_CIPHER_CTX_encrypting(ctx))
773                 buf[j + 8] = *out_ptr;
774         }
775         if (j == 8) {
776             memcpy(iv, buf + 8, 8);
777             EVP_CIPHER_CTX_set_num(ctx, 0);
778         } else {
779             EVP_CIPHER_CTX_set_num(ctx, j);
780             return 1;
781         }
782     }
783
784     for (; (inl - i) >= 8; i += 8, in_ptr += 8, out_ptr += 8) {
785         /*
786          * block cipher current iv
787          */
788         gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
789         /*
790          * xor next block of input text with it and output it
791          */
792         /*
793          * output this block
794          */
795         if (!EVP_CIPHER_CTX_encrypting(ctx))
796             memcpy(iv, in_ptr, 8);
797         for (j = 0; j < 8; j++) {
798             out_ptr[j] = buf[j] ^ in_ptr[j];
799         }
800         /* Encrypt */
801         /* Next iv is next block of cipher text */
802         if (EVP_CIPHER_CTX_encrypting(ctx))
803             memcpy(iv, out_ptr, 8);
804     }
805 /* Process rest of buffer */
806     if (i < inl) {
807         gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
808         if (!EVP_CIPHER_CTX_encrypting(ctx))
809             memcpy(buf + 8, in_ptr, inl - i);
810         for (j = 0; i < inl; j++, i++) {
811             out_ptr[j] = buf[j] ^ in_ptr[j];
812         }
813         EVP_CIPHER_CTX_set_num(ctx, j);
814         if (EVP_CIPHER_CTX_encrypting(ctx))
815             memcpy(buf + 8, out_ptr, j);
816     } else {
817         EVP_CIPHER_CTX_set_num(ctx, 0);
818     }
819     return 1;
820 }
821
822 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
823                               const unsigned char *in, size_t inl)
824 {
825     const unsigned char *in_ptr = in;
826     unsigned char *out_ptr = out;
827     size_t i = 0;
828     size_t j;
829     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
830     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
831 /* process partial block if any */
832     if (EVP_CIPHER_CTX_num(ctx)) {
833         for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
834              j++, i++, in_ptr++, out_ptr++) {
835             *out_ptr = buf[j] ^ (*in_ptr);
836         }
837         if (j == 8) {
838             EVP_CIPHER_CTX_set_num(ctx, 0);
839         } else {
840             EVP_CIPHER_CTX_set_num(ctx, j);
841             return 1;
842         }
843     }
844
845     for (; (inl - i) >= 8; i += 8, in_ptr += 8, out_ptr += 8) {
846         /*
847          * block cipher current iv
848          */
849         /* Encrypt */
850         gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
851         /*
852          * xor next block of input text with it and output it
853          */
854         /*
855          * output this block
856          */
857         for (j = 0; j < 8; j++) {
858             out_ptr[j] = buf[j] ^ in_ptr[j];
859         }
860     }
861 /* Process rest of buffer */
862     if (i < inl) {
863         gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
864         for (j = 0; i < inl; j++, i++) {
865             out_ptr[j] = buf[j] ^ in_ptr[j];
866         }
867         EVP_CIPHER_CTX_set_num(ctx, j);
868     } else {
869         EVP_CIPHER_CTX_set_num(ctx, 0);
870     }
871     return 1;
872 }
873
874 /* Cleaning up of EVP_CIPHER_CTX */
875 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
876 {
877     gost_destroy(&((struct ossl_gost_cipher_ctx *)
878                    EVP_CIPHER_CTX_get_cipher_data(ctx))->cctx);
879     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
880     return 1;
881 }
882
883 /* Control function for gost cipher */
884 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
885 {
886     switch (type) {
887 #if 0
888     case EVP_CTRL_INIT:
889         {
890             struct ossl_gost_cipher_ctx *c =
891                 EVP_CIPHER_CTX_get_cipher_data(ctx);
892             if (c == NULL) {
893                 return -1;
894             }
895             return gost_cipher_set_param(c, arg);
896         }
897 #endif
898     case EVP_CTRL_RAND_KEY:
899         {
900             if (RAND_priv_bytes
901                 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
902                 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
903                 return -1;
904             }
905             break;
906         }
907     case EVP_CTRL_PBE_PRF_NID:
908         if (ptr) {
909             const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
910             int nid = NID_id_tc26_hmac_gost_3411_2012_512;
911
912             if (params) {
913                 if (!strcmp("md_gost12_256", params))
914                     nid = NID_id_tc26_hmac_gost_3411_2012_256;
915                 else if (!strcmp("md_gost12_512", params))
916                     nid = NID_id_tc26_hmac_gost_3411_2012_512;
917                 else if (!strcmp("md_gost94", params))
918                     nid = NID_id_HMACGostR3411_94;
919             }
920             *((int *)ptr) = nid;
921             return 1;
922         } else {
923             return 0;
924         }
925
926     case EVP_CTRL_SET_SBOX:
927         if (ptr) {
928             struct ossl_gost_cipher_ctx *c =
929                 EVP_CIPHER_CTX_get_cipher_data(ctx);
930             int nid;
931             int cur_meshing;
932             int ret;
933
934             if (c == NULL) {
935                 return -1;
936             }
937
938             if (c->count != 0) {
939                 return -1;
940             }
941
942             nid = OBJ_txt2nid(ptr);
943             if (nid == NID_undef) {
944                 return 0;
945             }
946
947             cur_meshing = c->key_meshing;
948             ret = gost_cipher_set_param(c, nid);
949             c->key_meshing = cur_meshing;
950             return ret;
951         } else {
952             return 0;
953         }
954     case EVP_CTRL_KEY_MESH:
955         {
956             struct ossl_gost_cipher_ctx *c =
957                 EVP_CIPHER_CTX_get_cipher_data(ctx);
958
959             if (c == NULL) {
960                 return -1;
961             }
962
963             if (c->count != 0) {
964                 return -1;
965             }
966
967             c->key_meshing = arg;
968             return 1;
969         }
970     default:
971         GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
972         return -1;
973     }
974     return 1;
975 }
976
977 /* Set cipher parameters from ASN1 structure */
978 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
979 {
980     int len = 0;
981     unsigned char *buf = NULL;
982     unsigned char *p = NULL;
983     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
984     GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
985     ASN1_OCTET_STRING *os = NULL;
986     if (!gcp) {
987         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
988         return 0;
989     }
990     if (!ASN1_OCTET_STRING_set
991         (gcp->iv, EVP_CIPHER_CTX_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx))) {
992         GOST_CIPHER_PARAMS_free(gcp);
993         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
994         return 0;
995     }
996     ASN1_OBJECT_free(gcp->enc_param_set);
997     gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
998
999     len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
1000     p = buf = OPENSSL_malloc(len);
1001     if (!buf) {
1002         GOST_CIPHER_PARAMS_free(gcp);
1003         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1004         return 0;
1005     }
1006     i2d_GOST_CIPHER_PARAMS(gcp, &p);
1007     GOST_CIPHER_PARAMS_free(gcp);
1008
1009     os = ASN1_OCTET_STRING_new();
1010
1011     if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
1012         OPENSSL_free(buf);
1013         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1014         return 0;
1015     }
1016     OPENSSL_free(buf);
1017
1018     ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
1019     return 1;
1020 }
1021
1022 /* Store parameters into ASN1 structure */
1023 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1024 {
1025     int len;
1026     GOST_CIPHER_PARAMS *gcp = NULL;
1027     unsigned char *p;
1028     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1029     int nid;
1030
1031     if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
1032         return -1;
1033     }
1034
1035     p = params->value.sequence->data;
1036
1037     gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
1038                                  params->value.sequence->length);
1039
1040     len = gcp->iv->length;
1041     if (len != EVP_CIPHER_CTX_iv_length(ctx)) {
1042         GOST_CIPHER_PARAMS_free(gcp);
1043         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
1044         return -1;
1045     }
1046
1047     nid = OBJ_obj2nid(gcp->enc_param_set);
1048     if (nid == NID_undef) {
1049         GOST_CIPHER_PARAMS_free(gcp);
1050         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
1051                 GOST_R_INVALID_CIPHER_PARAM_OID);
1052         return -1;
1053     }
1054
1055     if (!gost_cipher_set_param(c, nid)) {
1056         GOST_CIPHER_PARAMS_free(gcp);
1057         return -1;
1058     }
1059     /*XXX missing non-const accessor */
1060     memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), gcp->iv->data,
1061            EVP_CIPHER_CTX_iv_length(ctx));
1062
1063     GOST_CIPHER_PARAMS_free(gcp);
1064
1065     return 1;
1066 }
1067
1068 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
1069 {
1070     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1071     memset(c->buffer, 0, sizeof(c->buffer));
1072     memset(c->partial_block, 0, sizeof(c->partial_block));
1073     c->count = 0;
1074     c->bytes_left = 0;
1075     c->key_meshing = 1;
1076     c->dgst_size = 4;
1077     gost_init(&(c->cctx), block);
1078     return 1;
1079 }
1080
1081 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
1082 {
1083     return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
1084 }
1085
1086 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
1087 {
1088     return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
1089 }
1090
1091 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
1092                            const unsigned char *data)
1093 {
1094     /*
1095      * We are using NULL for iv because CryptoPro doesn't interpret
1096      * internal state of MAC algorithm as iv during keymeshing (but does
1097      * initialize internal state from iv in key transport
1098      */
1099     assert(c->count % 8 == 0 && c->count <= 1024);
1100     if (c->key_meshing && c->count == 1024) {
1101         cryptopro_key_meshing(&(c->cctx), NULL);
1102     }
1103     mac_block(&(c->cctx), c->buffer, data);
1104     c->count = c->count % 1024 + 8;
1105 }
1106
1107 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
1108 {
1109     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1110     const unsigned char *p = data;
1111     size_t bytes = count;
1112     if (!(c->key_set)) {
1113         GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
1114         return 0;
1115     }
1116     if (c->bytes_left) {
1117         size_t i;
1118         for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
1119             c->partial_block[i] = *p;
1120         }
1121         if (i == 8) {
1122             mac_block_mesh(c, c->partial_block);
1123         } else {
1124             c->bytes_left = i;
1125             return 1;
1126         }
1127     }
1128     while (bytes > 8) {
1129         mac_block_mesh(c, p);
1130         p += 8;
1131         bytes -= 8;
1132     }
1133     if (bytes > 0) {
1134         memcpy(c->partial_block, p, bytes);
1135     }
1136     c->bytes_left = bytes;
1137     return 1;
1138 }
1139
1140 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
1141 {
1142     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1143     if (!c->key_set) {
1144         GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
1145         return 0;
1146     }
1147     if (c->count == 0 && c->bytes_left) {
1148         unsigned char buffer[8];
1149         memset(buffer, 0, 8);
1150         gost_imit_update(ctx, buffer, 8);
1151     }
1152     if (c->bytes_left) {
1153         int i;
1154         for (i = c->bytes_left; i < 8; i++) {
1155             c->partial_block[i] = 0;
1156         }
1157         mac_block_mesh(c, c->partial_block);
1158     }
1159     get_mac(c->buffer, 8 * c->dgst_size, md);
1160     return 1;
1161 }
1162
1163 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
1164 {
1165     switch (type) {
1166     case EVP_MD_CTRL_KEY_LEN:
1167         *((unsigned int *)(ptr)) = 32;
1168         return 1;
1169     case EVP_MD_CTRL_SET_KEY:
1170         {
1171             struct ossl_gost_imit_ctx *gost_imit_ctx = EVP_MD_CTX_md_data(ctx);
1172
1173             if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
1174                 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
1175                 return 0;
1176             }
1177             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
1178
1179             if (arg == 0) {
1180                 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
1181                 if (key->mac_param_nid != NID_undef) {
1182                     const struct gost_cipher_info *param =
1183                         get_encryption_params(OBJ_nid2obj(key->mac_param_nid));
1184                     if (param == NULL) {
1185                         GOSTerr(GOST_F_GOST_IMIT_CTRL,
1186                                 GOST_R_INVALID_MAC_PARAMS);
1187                         return 0;
1188                     }
1189                     gost_init(&(gost_imit_ctx->cctx), param->sblock);
1190                 }
1191                 gost_key(&(gost_imit_ctx->cctx), key->key);
1192                 gost_imit_ctx->key_set = 1;
1193
1194                 return 1;
1195             } else if (arg == 32) {
1196                 gost_key(&(gost_imit_ctx->cctx), ptr);
1197                 gost_imit_ctx->key_set = 1;
1198                 return 1;
1199             }
1200             GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
1201             return 0;
1202         }
1203     case EVP_MD_CTRL_XOF_LEN:
1204         {
1205             struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1206             if (arg < 1 || arg > 8) {
1207                 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
1208                 return 0;
1209             }
1210             c->dgst_size = arg;
1211             return 1;
1212         }
1213
1214     default:
1215         return 0;
1216     }
1217 }
1218
1219 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1220 {
1221     if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
1222         memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
1223                sizeof(struct ossl_gost_imit_ctx));
1224     }
1225     return 1;
1226 }
1227
1228 /* Clean up imit ctx */
1229 int gost_imit_cleanup(EVP_MD_CTX *ctx)
1230 {
1231     memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
1232     return 1;
1233 }