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