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