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