]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_crypt.c
Openssl 1.1.0 has separate constants for managing key meshing and sbox values.
[openssl-gost/engine.git] / gost_crypt.c
1 /**********************************************************************
2  *                          gost_crypt.c                              *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *       OpenSSL interface to GOST 28147-89 cipher functions          *
7  *          Requires OpenSSL 0.9.9 for compilation                    *
8  **********************************************************************/
9 #include <string.h>
10 #include "gost89.h"
11 #include <openssl/err.h>
12 #include <openssl/rand.h>
13 #include "e_gost_err.h"
14 #include "gost_lcl.h"
15
16 #if !defined(CCGOST_DEBUG) && !defined(DEBUG)
17 # ifndef NDEBUG
18 #  define NDEBUG
19 # endif
20 #endif
21 #include <assert.h>
22
23 static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
24                             const unsigned char *iv, int enc);
25 static int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
26                                 const unsigned char *iv, int enc);
27 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
28                                 const unsigned char *iv, int enc);
29 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
30                                   const unsigned char *key,
31                                   const unsigned char *iv, int enc);
32 /* Handles block of data in CFB mode */
33 static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
34                               const unsigned char *in, size_t inl);
35 /* Handles block of data in CBC mode */
36 static int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
37                               const unsigned char *in, size_t inl);
38 /* Handles block of data in CNT mode */
39 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
40                               const unsigned char *in, size_t inl);
41 /* Cleanup function */
42 static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
43 /* set/get cipher parameters */
44 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
45 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
46 /* Control function */
47 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
48
49 EVP_CIPHER cipher_gost = {
50     NID_id_Gost28147_89,
51     1,                          /* block_size */
52     32,                         /* key_size */
53     8,                          /* iv_len */
54     EVP_CIPH_CFB_MODE | EVP_CIPH_NO_PADDING |
55         EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
56     gost_cipher_init,
57     gost_cipher_do_cfb,
58     gost_cipher_cleanup,
59     sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
60     gost89_set_asn1_parameters,
61     gost89_get_asn1_parameters,
62     gost_cipher_ctl,
63     NULL,
64 };
65
66 EVP_CIPHER cipher_gost_cbc = {
67     NID_gost89_cbc,
68     8,                          /*block_size */
69     32,                         /*key_size */
70     8,                          /*iv_len */
71     EVP_CIPH_CBC_MODE |
72         EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
73     gost_cipher_init_cbc,
74     gost_cipher_do_cbc,
75     gost_cipher_cleanup,
76     sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
77     gost89_set_asn1_parameters,
78     gost89_get_asn1_parameters,
79     gost_cipher_ctl,
80     NULL,
81 };
82
83 EVP_CIPHER cipher_gost_cpacnt = {
84     NID_gost89_cnt,
85     1,                          /* block_size */
86     32,                         /* key_size */
87     8,                          /* iv_len */
88     EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
89         EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
90     gost_cipher_init_cpa,
91     gost_cipher_do_cnt,
92     gost_cipher_cleanup,
93     sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
94     gost89_set_asn1_parameters,
95     gost89_get_asn1_parameters,
96     gost_cipher_ctl,
97     NULL,
98 };
99
100 EVP_CIPHER cipher_gost_cpcnt_12 = {
101     NID_gost89_cnt_12,
102     1,                          /* block_size */
103     32,                         /* key_size */
104     8,                          /* iv_len */
105     EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
106         EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
107     gost_cipher_init_cp_12,
108     gost_cipher_do_cnt,
109     gost_cipher_cleanup,
110     sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
111     gost89_set_asn1_parameters,
112     gost89_get_asn1_parameters,
113     gost_cipher_ctl,
114     NULL,
115 };
116
117 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
118 /* Init functions which set specific parameters */
119 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
120 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx);
121 /* process block of data */
122 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
123 /* Return computed value */
124 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
125 /* Copies context */
126 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
127 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
128 /* Control function, knows how to set MAC key.*/
129 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
130
131 static EVP_MD *_hidden_Gost28147_89_MAC_md = NULL;
132 static EVP_MD *_hidden_Gost28147_89_12_MAC_md = NULL;
133
134 EVP_MD *imit_gost_cpa(void)
135 {
136     if (_hidden_Gost28147_89_MAC_md == NULL) {
137         EVP_MD *md;
138
139         if ((md = EVP_MD_meth_new(NID_id_Gost28147_89_MAC, NID_undef)) == NULL
140             || !EVP_MD_meth_set_result_size(md, 4)
141             || !EVP_MD_meth_set_input_blocksize(md, 8)
142             || !EVP_MD_meth_set_app_datasize(md,
143                                              sizeof(struct ossl_gost_imit_ctx))
144             || !EVP_MD_meth_set_flags(md, 0)
145             || !EVP_MD_meth_set_init(md, gost_imit_init_cpa)
146             || !EVP_MD_meth_set_update(md, gost_imit_update)
147             || !EVP_MD_meth_set_final(md, gost_imit_final)
148             || !EVP_MD_meth_set_copy(md, gost_imit_copy)
149             || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
150             || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
151             EVP_MD_meth_free(md);
152             md = NULL;
153         }
154         _hidden_Gost28147_89_MAC_md = md;
155     }
156     return _hidden_Gost28147_89_MAC_md;
157 }
158
159 void imit_gost_cpa_destroy(void)
160 {
161     EVP_MD_meth_free(_hidden_Gost28147_89_MAC_md);
162     _hidden_Gost28147_89_MAC_md = NULL;
163 }
164
165 EVP_MD *imit_gost_cp_12(void)
166 {
167     if (_hidden_Gost28147_89_12_MAC_md == NULL) {
168         EVP_MD *md;
169
170         if ((md = EVP_MD_meth_new(NID_gost_mac_12, NID_undef)) == NULL
171             || !EVP_MD_meth_set_result_size(md, 4)
172             || !EVP_MD_meth_set_input_blocksize(md, 8)
173             || !EVP_MD_meth_set_app_datasize(md,
174                                              sizeof(struct ossl_gost_imit_ctx))
175             || !EVP_MD_meth_set_flags(md, 0)
176             || !EVP_MD_meth_set_init(md, gost_imit_init_cp_12)
177             || !EVP_MD_meth_set_update(md, gost_imit_update)
178             || !EVP_MD_meth_set_final(md, gost_imit_final)
179             || !EVP_MD_meth_set_copy(md, gost_imit_copy)
180             || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
181             || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
182             EVP_MD_meth_free(md);
183             md = NULL;
184         }
185         _hidden_Gost28147_89_12_MAC_md = md;
186     }
187     return _hidden_Gost28147_89_12_MAC_md;
188 }
189
190 void imit_gost_cp_12_destroy(void)
191 {
192     EVP_MD_meth_free(_hidden_Gost28147_89_12_MAC_md);
193     _hidden_Gost28147_89_12_MAC_md = NULL;
194 }
195
196 /*
197  * Correspondence between gost parameter OIDs and substitution blocks
198  * NID field is filed by register_gost_NID function in engine.c
199  * upon engine initialization
200  */
201
202 struct gost_cipher_info gost_cipher_list[] = {
203     /*- NID *//*
204      * Subst block
205      *//*
206      * Key meshing
207      */
208     /*
209      * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
210      */
211     {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
212      1},
213     {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
214      1},
215     {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
216      1},
217     {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
218      1},
219     {NID_id_tc26_gost_28147_param_Z, &Gost28147_TC26ParamSetZ, 1},
220     {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
221     {NID_undef, NULL, 0}
222 };
223
224 /*
225  * get encryption parameters from crypto network settings FIXME For now we
226  * use environment var CRYPT_PARAMS as place to store these settings.
227  * Actually, it is better to use engine control command, read from
228  * configuration file to set them
229  */
230 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
231 {
232     int nid;
233     struct gost_cipher_info *param;
234     if (!obj) {
235         const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
236         if (!params || !strlen(params)) {
237             int i;
238             for (i = 0; gost_cipher_list[i].nid != NID_undef; i++)
239                 if (gost_cipher_list[i].nid == NID_id_tc26_gost_28147_param_Z)
240                     return &gost_cipher_list[i];
241             return &gost_cipher_list[0];
242         }
243
244         nid = OBJ_txt2nid(params);
245         if (nid == NID_undef) {
246             GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
247                     GOST_R_INVALID_CIPHER_PARAM_OID);
248             return NULL;
249         }
250     } else {
251         nid = OBJ_obj2nid(obj);
252     }
253     for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
254          param++) ;
255     if (!param->sblock) {
256         GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
257         return NULL;
258     }
259     return param;
260 }
261
262 /* Sets cipher param from paramset NID. */
263 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
264 {
265     const struct gost_cipher_info *param;
266     param =
267         get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
268     if (!param)
269         return 0;
270
271     c->paramNID = param->nid;
272     c->key_meshing = param->key_meshing;
273     c->count = 0;
274     gost_init(&(c->cctx), param->sblock);
275     return 1;
276 }
277
278 /* Initializes EVP_CIPHER_CTX by paramset NID */
279 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
280                                   const unsigned char *key,
281                                   const unsigned char *iv, int enc,
282                                   int paramNID, int mode)
283 {
284     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
285     if (ctx->app_data == NULL) {
286         if (!gost_cipher_set_param(c, paramNID))
287             return 0;
288         ctx->app_data = ctx->cipher_data;
289     }
290     if (key)
291         gost_key(&(c->cctx), key);
292     if (iv)
293         memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
294     memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
295     return 1;
296 }
297
298 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
299                                 const unsigned char *key,
300                                 const unsigned char *iv,
301                                 gost_subst_block * block)
302 {
303     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
304     gost_init(&(c->cctx), block);
305     c->key_meshing = 1;
306     c->count = 0;
307     if (key)
308         gost_key(&(c->cctx), key);
309     if (iv)
310         memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
311     memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
312     return 1;
313 }
314
315 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
316                                 const unsigned char *iv, int enc)
317 {
318     return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
319 }
320
321 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
322                                   const unsigned char *key,
323                                   const unsigned char *iv, int enc)
324 {
325     return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
326 }
327
328 /* Initializes EVP_CIPHER_CTX with default values */
329 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
330                      const unsigned char *iv, int enc)
331 {
332     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
333                                   EVP_CIPH_CFB_MODE);
334 }
335
336 /* Initializes EVP_CIPHER_CTX with default values */
337 int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
338                          const unsigned char *iv, int enc)
339 {
340     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
341                                   EVP_CIPH_CBC_MODE);
342 }
343
344 /*
345  * Wrapper around gostcrypt function from gost89.c which perform key meshing
346  * when nesseccary
347  */
348 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
349 {
350     struct ossl_gost_cipher_ctx *c = ctx;
351     assert(c->count % 8 == 0 && c->count <= 1024);
352     if (c->key_meshing && c->count == 1024) {
353         cryptopro_key_meshing(&(c->cctx), iv);
354     }
355     gostcrypt(&(c->cctx), iv, buf);
356     c->count = c->count % 1024 + 8;
357 }
358
359 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
360 {
361     struct ossl_gost_cipher_ctx *c = ctx;
362     word32 g, go;
363     unsigned char buf1[8];
364     assert(c->count % 8 == 0 && c->count <= 1024);
365     if (c->key_meshing && c->count == 1024) {
366         cryptopro_key_meshing(&(c->cctx), iv);
367     }
368     if (c->count == 0) {
369         gostcrypt(&(c->cctx), iv, buf1);
370     } else {
371         memcpy(buf1, iv, 8);
372     }
373     g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
374     g += 0x01010101;
375     buf1[0] = (unsigned char)(g & 0xff);
376     buf1[1] = (unsigned char)((g >> 8) & 0xff);
377     buf1[2] = (unsigned char)((g >> 16) & 0xff);
378     buf1[3] = (unsigned char)((g >> 24) & 0xff);
379     g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
380     go = g;
381     g += 0x01010104;
382     if (go > g)                 /* overflow */
383         g++;
384     buf1[4] = (unsigned char)(g & 0xff);
385     buf1[5] = (unsigned char)((g >> 8) & 0xff);
386     buf1[6] = (unsigned char)((g >> 16) & 0xff);
387     buf1[7] = (unsigned char)((g >> 24) & 0xff);
388     memcpy(iv, buf1, 8);
389     gostcrypt(&(c->cctx), buf1, buf);
390     c->count = c->count % 1024 + 8;
391 }
392
393 /* GOST encryptoon in CBC mode */
394 int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
395                        const unsigned char *in, size_t inl)
396 {
397     OPENSSL_assert(inl % 8 == 0);
398     unsigned char b[8];
399     const unsigned char *in_ptr = in;
400     unsigned char *out_ptr = out;
401     int i;
402     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
403     if (ctx->encrypt) {
404         while (inl > 0) {
405             for (i = 0; i < 8; i++) {
406                 b[i] = ctx->iv[i] ^ in_ptr[i];
407             }
408             gostcrypt(&(c->cctx), b, out_ptr);
409             memcpy(ctx->iv, out_ptr, 8);
410             out_ptr += 8;
411             in_ptr += 8;
412             inl -= 8;
413         }
414     } else {
415         while (inl > 0) {
416             gostdecrypt(&(c->cctx), in_ptr, b);
417             for (i = 0; i < 8; i++) {
418                 out_ptr[i] = ctx->iv[i] ^ b[i];
419             }
420             memcpy(ctx->iv, in_ptr, 8);
421             out_ptr += 8;
422             in_ptr += 8;
423             inl -= 8;
424         }
425     }
426     return 1;
427 }
428
429 /* GOST encryption in CFB mode */
430 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
431                        const unsigned char *in, size_t inl)
432 {
433     const unsigned char *in_ptr = in;
434     unsigned char *out_ptr = out;
435     size_t i = 0;
436     size_t j = 0;
437 /* process partial block if any */
438     if (ctx->num) {
439         for (j = ctx->num, i = 0; j < 8 && i < inl;
440              j++, i++, in_ptr++, out_ptr++) {
441             if (!ctx->encrypt)
442                 ctx->buf[j + 8] = *in_ptr;
443             *out_ptr = ctx->buf[j] ^ (*in_ptr);
444             if (ctx->encrypt)
445                 ctx->buf[j + 8] = *out_ptr;
446         }
447         if (j == 8) {
448             memcpy(ctx->iv, ctx->buf + 8, 8);
449             ctx->num = 0;
450         } else {
451             ctx->num = j;
452             return 1;
453         }
454     }
455
456     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
457         /*
458          * block cipher current iv
459          */
460         gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
461         /*
462          * xor next block of input text with it and output it
463          */
464         /*
465          * output this block
466          */
467         if (!ctx->encrypt)
468             memcpy(ctx->iv, in_ptr, 8);
469         for (j = 0; j < 8; j++) {
470             out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
471         }
472         /* Encrypt */
473         /* Next iv is next block of cipher text */
474         if (ctx->encrypt)
475             memcpy(ctx->iv, out_ptr, 8);
476     }
477 /* Process rest of buffer */
478     if (i < inl) {
479         gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
480         if (!ctx->encrypt)
481             memcpy(ctx->buf + 8, in_ptr, inl - i);
482         for (j = 0; i < inl; j++, i++) {
483             out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
484         }
485         ctx->num = j;
486         if (ctx->encrypt)
487             memcpy(ctx->buf + 8, out_ptr, j);
488     } else {
489         ctx->num = 0;
490     }
491     return 1;
492 }
493
494 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
495                               const unsigned char *in, size_t inl)
496 {
497     const unsigned char *in_ptr = in;
498     unsigned char *out_ptr = out;
499     size_t i = 0;
500     size_t j;
501 /* process partial block if any */
502     if (ctx->num) {
503         for (j = ctx->num, i = 0; j < 8 && i < inl;
504              j++, i++, in_ptr++, out_ptr++) {
505             *out_ptr = ctx->buf[j] ^ (*in_ptr);
506         }
507         if (j == 8) {
508             ctx->num = 0;
509         } else {
510             ctx->num = j;
511             return 1;
512         }
513     }
514
515     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
516         /*
517          * block cipher current iv
518          */
519         /* Encrypt */
520         gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
521         /*
522          * xor next block of input text with it and output it
523          */
524         /*
525          * output this block
526          */
527         for (j = 0; j < 8; j++) {
528             out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
529         }
530     }
531 /* Process rest of buffer */
532     if (i < inl) {
533         gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
534         for (j = 0; i < inl; j++, i++) {
535             out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
536         }
537         ctx->num = j;
538     } else {
539         ctx->num = 0;
540     }
541     return 1;
542 }
543
544 /* Cleaning up of EVP_CIPHER_CTX */
545 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
546 {
547     gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
548     ctx->app_data = NULL;
549     return 1;
550 }
551
552 /* Control function for gost cipher */
553 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
554 {
555     switch (type) {
556 #if 0
557     case EVP_CTRL_INIT:
558         {
559             struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
560             if (c == NULL) {
561                 return -1;
562             }
563             return gost_cipher_set_param(c, arg);
564         }
565 #endif
566     case EVP_CTRL_RAND_KEY:
567         {
568             if (RAND_bytes((unsigned char *)ptr, ctx->key_len) <= 0) {
569                 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
570                 return -1;
571             }
572             break;
573         }
574     case EVP_CTRL_PBE_PRF_NID:
575         if (ptr) {
576             const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
577             int nid = NID_id_tc26_hmac_gost_3411_2012_512;
578
579             if (params) {
580                 if (!strcmp("md_gost12_256", params))
581                     nid = NID_id_tc26_hmac_gost_3411_2012_256;
582                 else if (!strcmp("md_gost12_512", params))
583                     nid = NID_id_tc26_hmac_gost_3411_2012_512;
584                 else if (!strcmp("md_gost94", params))
585                     nid = NID_id_HMACGostR3411_94;
586             }
587             *((int *)ptr) = nid;
588             return 1;
589         } else {
590             return 0;
591         }
592
593     case EVP_CTRL_SET_SBOX:
594         if (ptr) {
595             struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
596             int nid;
597             int cur_meshing;
598             int ret;
599
600             if (c == NULL) {
601                 return -1;
602             }
603
604             if (c->count != 0) {
605                 return -1;
606             }
607
608             nid = OBJ_txt2nid(ptr);
609             if (nid == NID_undef) {
610                 return 0;
611             }
612
613             cur_meshing = c->key_meshing;
614             ret = gost_cipher_set_param(c, nid);
615             c->key_meshing = cur_meshing;
616             return ret;
617         } else {
618           return 0;
619         }
620     case EVP_CTRL_KEY_MESH:
621         {
622             struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
623
624             if (c == NULL) {
625                 return -1;
626             }
627
628             if (c->count != 0) {
629                 return -1;
630             }
631
632             c->key_meshing = arg;
633             return 1;
634         }
635     default:
636         GOSTerr(GOST_F_GOST_CIPHER_CTL,
637                 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
638         return -1;
639     }
640     return 1;
641 }
642
643 /* Set cipher parameters from ASN1 structure */
644 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
645 {
646     int len = 0;
647     unsigned char *buf = NULL;
648     unsigned char *p = NULL;
649     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
650     GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
651     ASN1_OCTET_STRING *os = NULL;
652     if (!gcp) {
653         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
654         return 0;
655     }
656     if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) {
657         GOST_CIPHER_PARAMS_free(gcp);
658         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
659         return 0;
660     }
661     ASN1_OBJECT_free(gcp->enc_param_set);
662     gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
663
664     len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
665     p = buf = OPENSSL_malloc(len);
666     if (!buf) {
667         GOST_CIPHER_PARAMS_free(gcp);
668         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
669         return 0;
670     }
671     i2d_GOST_CIPHER_PARAMS(gcp, &p);
672     GOST_CIPHER_PARAMS_free(gcp);
673
674     os = ASN1_OCTET_STRING_new();
675
676     if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
677         OPENSSL_free(buf);
678         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
679         return 0;
680     }
681     OPENSSL_free(buf);
682
683     ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
684     return 1;
685 }
686
687 /* Store parameters into ASN1 structure */
688 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
689 {
690     int ret = -1;
691     int len;
692     GOST_CIPHER_PARAMS *gcp = NULL;
693     unsigned char *p;
694     struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
695     int nid;
696
697     if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
698         return ret;
699     }
700
701     p = params->value.sequence->data;
702
703     gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
704                                  params->value.sequence->length);
705
706     len = gcp->iv->length;
707     if (len != ctx->cipher->iv_len) {
708         GOST_CIPHER_PARAMS_free(gcp);
709         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
710         return -1;
711     }
712
713     nid = OBJ_obj2nid(gcp->enc_param_set);
714     if (nid == NID_undef) {
715         GOST_CIPHER_PARAMS_free(gcp);
716         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
717                 GOST_R_INVALID_CIPHER_PARAM_OID);
718         return -1;
719     }
720
721     if (!gost_cipher_set_param(c, nid)) {
722         GOST_CIPHER_PARAMS_free(gcp);
723         return -1;
724     }
725     memcpy(ctx->oiv, gcp->iv->data, len);
726
727     GOST_CIPHER_PARAMS_free(gcp);
728
729     return 1;
730 }
731
732 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
733 {
734     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
735     memset(c->buffer, 0, sizeof(c->buffer));
736     memset(c->partial_block, 0, sizeof(c->partial_block));
737     c->count = 0;
738     c->bytes_left = 0;
739     c->key_meshing = 1;
740     c->dgst_size = 4;
741     gost_init(&(c->cctx), block);
742     return 1;
743 }
744
745 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
746 {
747     return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
748 }
749
750 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
751 {
752     return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
753 }
754
755 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
756                            const unsigned char *data)
757 {
758     unsigned char buffer[8];
759     /*
760      * We are using local buffer for iv because CryptoPro doesn't interpret
761      * internal state of MAC algorithm as iv during keymeshing (but does
762      * initialize internal state from iv in key transport
763      */
764     assert(c->count % 8 == 0 && c->count <= 1024);
765     if (c->key_meshing && c->count == 1024) {
766         cryptopro_key_meshing(&(c->cctx), buffer);
767     }
768     mac_block(&(c->cctx), c->buffer, data);
769     c->count = c->count % 1024 + 8;
770 }
771
772 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
773 {
774     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
775     const unsigned char *p = data;
776     size_t bytes = count, i;
777     if (!(c->key_set)) {
778         GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
779         return 0;
780     }
781     if (c->bytes_left) {
782         for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
783             c->partial_block[i] = *p;
784         }
785         if (i == 8) {
786             mac_block_mesh(c, c->partial_block);
787         } else {
788             c->bytes_left = i;
789             return 1;
790         }
791     }
792     while (bytes > 8) {
793         mac_block_mesh(c, p);
794         p += 8;
795         bytes -= 8;
796     }
797     if (bytes > 0) {
798         memcpy(c->partial_block, p, bytes);
799     }
800     c->bytes_left = bytes;
801     return 1;
802 }
803
804 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
805 {
806     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
807     if (!c->key_set) {
808         GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
809         return 0;
810     }
811     if (c->count == 0 && c->bytes_left) {
812         unsigned char buffer[8];
813         memset(buffer, 0, 8);
814         gost_imit_update(ctx, buffer, 8);
815     }
816     if (c->bytes_left) {
817         int i;
818         for (i = c->bytes_left; i < 8; i++) {
819             c->partial_block[i] = 0;
820         }
821         mac_block_mesh(c, c->partial_block);
822     }
823     get_mac(c->buffer, 8 * c->dgst_size, md);
824     return 1;
825 }
826
827 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
828 {
829     switch (type) {
830     case EVP_MD_CTRL_KEY_LEN:
831         *((unsigned int *)(ptr)) = 32;
832         return 1;
833     case EVP_MD_CTRL_SET_KEY:
834         {
835             struct ossl_gost_imit_ctx *gost_imit_ctx = EVP_MD_CTX_md_data(ctx);
836
837             if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx))(ctx) <= 0) {
838                 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
839                 return 0;
840             }
841             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
842
843             if (arg == 0) {
844                 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
845                 if (key->mac_param_nid != NID_undef) {
846                     const struct gost_cipher_info *param =
847                         get_encryption_params(OBJ_nid2obj
848                                               (key->mac_param_nid));
849                     if (param == NULL) {
850                         GOSTerr(GOST_F_GOST_IMIT_CTRL,
851                                 GOST_R_INVALID_MAC_PARAMS);
852                         return 0;
853                     }
854                     gost_init(&(gost_imit_ctx->cctx), param->sblock);
855                 }
856                 gost_key(&(gost_imit_ctx->cctx), key->key);
857                 gost_imit_ctx->key_set = 1;
858
859                 return 1;
860             } else if (arg == 32) {
861                 gost_key(&(gost_imit_ctx->cctx), ptr);
862                 gost_imit_ctx->key_set = 1;
863                 return 1;
864             }
865             GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
866             return 0;
867         }
868     case EVP_MD_CTRL_MAC_LEN:
869         {
870             struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
871             if (arg < 1 || arg > 8) {
872                 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
873                 return 0;
874             }
875             c->dgst_size = arg;
876             return 1;
877         }
878
879     default:
880         return 0;
881     }
882 }
883
884 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
885 {
886     if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
887         memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
888                sizeof(struct ossl_gost_imit_ctx));
889     }
890     return 1;
891 }
892
893 /* Clean up imit ctx */
894 int gost_imit_cleanup(EVP_MD_CTX *ctx)
895 {
896     memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
897     return 1;
898 }