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