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