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