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