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