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