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