Adapt all engines that need it to opaque EVP_CIPHER
[openssl.git] / engines / ccgost / 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 #include <openssl/evp.h>
16
17 #if !defined(CCGOST_DEBUG) && !defined(DEBUG)
18 # ifndef NDEBUG
19 #  define NDEBUG
20 # endif
21 #endif
22 #include <assert.h>
23
24 static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
25                             const unsigned char *iv, int enc);
26 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
27                                 const unsigned char *iv, int enc);
28 /* Handles block of data in CFB mode */
29 static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
30                               const unsigned char *in, size_t inl);
31 /* Handles block of data in CNT mode */
32 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
33                               const unsigned char *in, size_t inl);
34 /* Cleanup function */
35 static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
36 /* set/get cipher parameters */
37 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
38 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
39 /* Control function */
40 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
41
42 static EVP_CIPHER *_hidden_Gost28147_89_cipher = NULL;
43 const EVP_CIPHER *cipher_gost(void)
44 {
45     if (_hidden_Gost28147_89_cipher == NULL
46         && ((_hidden_Gost28147_89_cipher =
47              EVP_CIPHER_meth_new(NID_id_Gost28147_89,
48                                  1  /* block_size */,
49                                  32 /* key_size */)) == NULL
50             || !EVP_CIPHER_meth_set_iv_length(_hidden_Gost28147_89_cipher, 8)
51             || !EVP_CIPHER_meth_set_flags(_hidden_Gost28147_89_cipher,
52                                           EVP_CIPH_CFB_MODE |
53                                           EVP_CIPH_NO_PADDING |
54                                           EVP_CIPH_CUSTOM_IV |
55                                           EVP_CIPH_RAND_KEY |
56                                           EVP_CIPH_ALWAYS_CALL_INIT)
57             || !EVP_CIPHER_meth_set_init(_hidden_Gost28147_89_cipher,
58                                          gost_cipher_init)
59             || !EVP_CIPHER_meth_set_do_cipher(_hidden_Gost28147_89_cipher,
60                                               gost_cipher_do_cfb)
61             || !EVP_CIPHER_meth_set_cleanup(_hidden_Gost28147_89_cipher,
62                                             gost_cipher_cleanup)
63             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_Gost28147_89_cipher,
64                                                   sizeof(struct ossl_gost_cipher_ctx))
65             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_Gost28147_89_cipher,
66                                                     gost89_set_asn1_parameters)
67             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_Gost28147_89_cipher,
68                                                     gost89_get_asn1_parameters)
69             || !EVP_CIPHER_meth_set_ctrl(_hidden_Gost28147_89_cipher,
70                                          gost_cipher_ctl))) {
71         EVP_CIPHER_meth_free(_hidden_Gost28147_89_cipher);
72         _hidden_Gost28147_89_cipher = NULL;
73     }
74     return _hidden_Gost28147_89_cipher;
75 }
76
77 static EVP_CIPHER *_hidden_gost89_cnt = NULL;
78 const EVP_CIPHER *cipher_gost_cpacnt(void)
79 {
80     if (_hidden_gost89_cnt == NULL
81         && ((_hidden_gost89_cnt =
82              EVP_CIPHER_meth_new(NID_gost89_cnt,
83                                  1  /* block_size */,
84                                  32 /* key_size */)) == NULL
85             || !EVP_CIPHER_meth_set_iv_length(_hidden_gost89_cnt, 8)
86             || !EVP_CIPHER_meth_set_flags(_hidden_gost89_cnt,
87                                           EVP_CIPH_OFB_MODE |
88                                           EVP_CIPH_NO_PADDING |
89                                           EVP_CIPH_CUSTOM_IV |
90                                           EVP_CIPH_RAND_KEY |
91                                           EVP_CIPH_ALWAYS_CALL_INIT)
92             || !EVP_CIPHER_meth_set_init(_hidden_gost89_cnt,
93                                          gost_cipher_init_cpa)
94             || !EVP_CIPHER_meth_set_do_cipher(_hidden_gost89_cnt,
95                                               gost_cipher_do_cnt)
96             || !EVP_CIPHER_meth_set_cleanup(_hidden_gost89_cnt,
97                                             gost_cipher_cleanup)
98             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_gost89_cnt,
99                                                   sizeof(struct ossl_gost_cipher_ctx))
100             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_gost89_cnt,
101                                                     gost89_set_asn1_parameters)
102             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_gost89_cnt,
103                                                     gost89_get_asn1_parameters)
104             || !EVP_CIPHER_meth_set_ctrl(_hidden_gost89_cnt,
105                                          gost_cipher_ctl))) {
106         EVP_CIPHER_meth_free(_hidden_gost89_cnt);
107         _hidden_gost89_cnt = NULL;
108     }
109     return _hidden_gost89_cnt;
110 }
111
112 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
113 /* Init functions which set specific parameters */
114 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
115 /* process block of data */
116 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
117 /* Return computed value */
118 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
119 /* Copies context */
120 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
121 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
122 /* Control function, knows how to set MAC key.*/
123 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
124
125 static EVP_MD *_hidden_Gost28147_89_MAC_md = NULL;
126 const EVP_MD *imit_gost_cpa(void)
127 {
128
129     if (_hidden_Gost28147_89_MAC_md == NULL) {
130         EVP_MD *md;
131
132         if ((md = EVP_MD_meth_new(NID_id_Gost28147_89_MAC, NID_undef)) == NULL
133             || !EVP_MD_meth_set_result_size(md, 4)
134             || !EVP_MD_meth_set_input_blocksize(md, 8)
135             || !EVP_MD_meth_set_app_datasize(md,
136                                              sizeof(struct ossl_gost_imit_ctx))
137             || !EVP_MD_meth_set_flags(md, 0)
138             || !EVP_MD_meth_set_init(md, gost_imit_init_cpa)
139             || !EVP_MD_meth_set_update(md, gost_imit_update)
140             || !EVP_MD_meth_set_final(md, gost_imit_final)
141             || !EVP_MD_meth_set_copy(md, gost_imit_copy)
142             || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
143             || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
144             EVP_MD_meth_free(md);
145             md = NULL;
146         }
147         _hidden_Gost28147_89_MAC_md = md;
148     }
149     return _hidden_Gost28147_89_MAC_md;
150 }
151 void imit_gost_cpa_destroy(void)
152 {
153     EVP_MD_meth_free(_hidden_Gost28147_89_MAC_md);
154     _hidden_Gost28147_89_MAC_md = NULL;
155 }
156
157 /*
158  * Correspondence between gost parameter OIDs and substitution blocks
159  * NID field is filed by register_gost_NID function in engine.c
160  * upon engine initialization
161  */
162
163 struct gost_cipher_info gost_cipher_list[] = {
164     /*- NID *//*
165      * Subst block
166      *//*
167      * Key meshing
168      */
169     /*
170      * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
171      */
172     {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
173      1},
174     {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
175      1},
176     {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
177      1},
178     {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
179      1},
180     {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
181     {NID_undef, NULL, 0}
182 };
183
184 /*
185  * get encryption parameters from crypto network settings FIXME For now we
186  * use environment var CRYPT_PARAMS as place to store these settings.
187  * Actually, it is better to use engine control command, read from
188  * configuration file to set them
189  */
190 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
191 {
192     int nid;
193     struct gost_cipher_info *param;
194     if (!obj) {
195         const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
196         if (!params || !strlen(params))
197             return &gost_cipher_list[1];
198
199         nid = OBJ_txt2nid(params);
200         if (nid == NID_undef) {
201             GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
202                     GOST_R_INVALID_CIPHER_PARAM_OID);
203             return NULL;
204         }
205     } else {
206         nid = OBJ_obj2nid(obj);
207     }
208     for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
209          param++) ;
210     if (!param->sblock) {
211         GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
212         return NULL;
213     }
214     return param;
215 }
216
217 /* Sets cipher param from paramset NID. */
218 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
219 {
220     const struct gost_cipher_info *param;
221     param =
222         get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
223     if (!param)
224         return 0;
225
226     c->paramNID = param->nid;
227     c->key_meshing = param->key_meshing;
228     c->count = 0;
229     gost_init(&(c->cctx), param->sblock);
230     return 1;
231 }
232
233 /* Initializes EVP_CIPHER_CTX by paramset NID */
234 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
235                                   const unsigned char *key,
236                                   const unsigned char *iv, int enc,
237                                   int paramNID, int mode)
238 {
239     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_cipher_data(ctx);
240     if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
241         if (!gost_cipher_set_param(c, paramNID))
242             return 0;
243         EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_cipher_data(ctx));
244     }
245     if (key)
246         gost_key(&(c->cctx), key);
247     if (iv)
248         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
249                EVP_CIPHER_CTX_iv_length(ctx));
250     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
251            EVP_CIPHER_CTX_original_iv(ctx),
252            EVP_CIPHER_CTX_iv_length(ctx));
253     return 1;
254 }
255
256 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
257                                 const unsigned char *iv, int enc)
258 {
259     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_cipher_data(ctx);
260     gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
261     c->key_meshing = 1;
262     c->count = 0;
263     if (key)
264         gost_key(&(c->cctx), key);
265     if (iv)
266         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
267                EVP_CIPHER_CTX_iv_length(ctx));
268     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
269            EVP_CIPHER_CTX_original_iv(ctx),
270            EVP_CIPHER_CTX_iv_length(ctx));
271     return 1;
272 }
273
274 /* Initializes EVP_CIPHER_CTX with default values */
275 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
276                      const unsigned char *iv, int enc)
277 {
278     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
279                                   EVP_CIPH_CFB_MODE);
280 }
281
282 /*
283  * Wrapper around gostcrypt function from gost89.c which perform key meshing
284  * when nesseccary
285  */
286 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
287 {
288     struct ossl_gost_cipher_ctx *c = ctx;
289     assert(c->count % 8 == 0 && c->count <= 1024);
290     if (c->key_meshing && c->count == 1024) {
291         cryptopro_key_meshing(&(c->cctx), iv);
292     }
293     gostcrypt(&(c->cctx), iv, buf);
294     c->count = c->count % 1024 + 8;
295 }
296
297 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
298 {
299     struct ossl_gost_cipher_ctx *c = ctx;
300     word32 g, go;
301     unsigned char buf1[8];
302     assert(c->count % 8 == 0 && c->count <= 1024);
303     if (c->key_meshing && c->count == 1024) {
304         cryptopro_key_meshing(&(c->cctx), iv);
305     }
306     if (c->count == 0) {
307         gostcrypt(&(c->cctx), iv, buf1);
308     } else {
309         memcpy(buf1, iv, 8);
310     }
311     g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
312     g += 0x01010101;
313     buf1[0] = (unsigned char)(g & 0xff);
314     buf1[1] = (unsigned char)((g >> 8) & 0xff);
315     buf1[2] = (unsigned char)((g >> 16) & 0xff);
316     buf1[3] = (unsigned char)((g >> 24) & 0xff);
317     g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
318     go = g;
319     g += 0x01010104;
320     if (go > g)                 /* overflow */
321         g++;
322     buf1[4] = (unsigned char)(g & 0xff);
323     buf1[5] = (unsigned char)((g >> 8) & 0xff);
324     buf1[6] = (unsigned char)((g >> 16) & 0xff);
325     buf1[7] = (unsigned char)((g >> 24) & 0xff);
326     memcpy(iv, buf1, 8);
327     gostcrypt(&(c->cctx), buf1, buf);
328     c->count = c->count % 1024 + 8;
329 }
330
331 /* GOST encryption in CFB mode */
332 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
333                        const unsigned char *in, size_t inl)
334 {
335     const unsigned char *in_ptr = in;
336     unsigned char *out_ptr = out;
337     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
338     size_t i = 0;
339     size_t j = 0;
340 /* process partial block if any */
341     if (EVP_CIPHER_CTX_num(ctx)) {
342         for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
343              j++, i++, in_ptr++, out_ptr++) {
344             if (!EVP_CIPHER_CTX_encrypting(ctx))
345                 buf[j + 8] = *in_ptr;
346             *out_ptr = buf[j] ^ (*in_ptr);
347             if (EVP_CIPHER_CTX_encrypting(ctx))
348                 buf[j + 8] = *out_ptr;
349         }
350         if (j == 8) {
351             memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), buf + 8, 8);
352             EVP_CIPHER_CTX_set_num(ctx, 0);
353         } else {
354             EVP_CIPHER_CTX_set_num(ctx, j);
355             return 1;
356         }
357     }
358
359     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
360         /*
361          * block cipher current iv
362          */
363         gost_crypt_mesh(EVP_CIPHER_CTX_cipher_data(ctx),
364                         EVP_CIPHER_CTX_iv_noconst(ctx), buf);
365         /*
366          * xor next block of input text with it and output it
367          */
368         /*
369          * output this block
370          */
371         if (!EVP_CIPHER_CTX_encrypting(ctx))
372             memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), in_ptr, 8);
373         for (j = 0; j < 8; j++) {
374             out_ptr[j] = buf[j] ^ in_ptr[j];
375         }
376         /* Encrypt */
377         /* Next iv is next block of cipher text */
378         if (EVP_CIPHER_CTX_encrypting(ctx))
379             memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), out_ptr, 8);
380     }
381 /* Process rest of buffer */
382     if (i < inl) {
383         gost_crypt_mesh(EVP_CIPHER_CTX_cipher_data(ctx),
384                         EVP_CIPHER_CTX_iv_noconst(ctx), buf);
385         if (!EVP_CIPHER_CTX_encrypting(ctx))
386             memcpy(buf + 8, in_ptr, inl - i);
387         for (j = 0; i < inl; j++, i++) {
388             out_ptr[j] = buf[j] ^ in_ptr[j];
389         }
390         EVP_CIPHER_CTX_set_num(ctx, j);
391         if (EVP_CIPHER_CTX_encrypting(ctx))
392             memcpy(buf + 8, out_ptr, j);
393     } else {
394         EVP_CIPHER_CTX_set_num(ctx, 0);
395     }
396     return 1;
397 }
398
399 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
400                               const unsigned char *in, size_t inl)
401 {
402     const unsigned char *in_ptr = in;
403     unsigned char *out_ptr = out;
404     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
405     size_t i = 0;
406     size_t j;
407 /* process partial block if any */
408     if (EVP_CIPHER_CTX_num(ctx)) {
409         for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
410              j++, i++, in_ptr++, out_ptr++) {
411             *out_ptr = buf[j] ^ (*in_ptr);
412         }
413         if (j == 8) {
414             EVP_CIPHER_CTX_set_num(ctx, 0);
415         } else {
416             EVP_CIPHER_CTX_set_num(ctx, j);
417             return 1;
418         }
419     }
420
421     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
422         /*
423          * block cipher current iv
424          */
425         /* Encrypt */
426         gost_cnt_next(EVP_CIPHER_CTX_cipher_data(ctx),
427                       EVP_CIPHER_CTX_iv_noconst(ctx), buf);
428         /*
429          * xor next block of input text with it and output it
430          */
431         /*
432          * output this block
433          */
434         for (j = 0; j < 8; j++) {
435             out_ptr[j] = buf[j] ^ in_ptr[j];
436         }
437     }
438 /* Process rest of buffer */
439     if (i < inl) {
440         gost_cnt_next(EVP_CIPHER_CTX_cipher_data(ctx),
441                       EVP_CIPHER_CTX_iv_noconst(ctx), buf);
442         for (j = 0; i < inl; j++, i++) {
443             out_ptr[j] = buf[j] ^ in_ptr[j];
444         }
445         EVP_CIPHER_CTX_set_num(ctx, j);
446     } else {
447         EVP_CIPHER_CTX_set_num(ctx, 0);
448     }
449     return 1;
450 }
451
452 /* Cleaning up of EVP_CIPHER_CTX */
453 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
454 {
455     gost_destroy(&((struct ossl_gost_cipher_ctx *)
456                    EVP_CIPHER_CTX_cipher_data(ctx))->cctx);
457     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
458     return 1;
459 }
460
461 /* Control function for gost cipher */
462 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
463 {
464     switch (type) {
465     case EVP_CTRL_RAND_KEY:
466         {
467             if (RAND_bytes((unsigned char *)ptr,
468                            EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
469                 GOSTerr(GOST_F_GOST_CIPHER_CTL,
470                         GOST_R_RANDOM_GENERATOR_ERROR);
471                 return -1;
472             }
473             break;
474         }
475     case EVP_CTRL_PBE_PRF_NID:
476         if (ptr) {
477             *((int *)ptr) = NID_id_HMACGostR3411_94;
478             return 1;
479         } else {
480             return 0;
481         }
482
483     default:
484         GOSTerr(GOST_F_GOST_CIPHER_CTL,
485                 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
486         return -1;
487     }
488     return 1;
489 }
490
491 /* Set cipher parameters from ASN1 structure */
492 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
493 {
494     int len = 0;
495     unsigned char *buf = NULL;
496     unsigned char *p = NULL;
497     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_cipher_data(ctx);
498     GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
499     ASN1_OCTET_STRING *os = NULL;
500     if (!gcp) {
501         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
502         return 0;
503     }
504     if (!ASN1_OCTET_STRING_set(gcp->iv, EVP_CIPHER_CTX_iv(ctx),
505                                EVP_CIPHER_CTX_iv_length(ctx))) {
506         GOST_CIPHER_PARAMS_free(gcp);
507         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
508         return 0;
509     }
510     ASN1_OBJECT_free(gcp->enc_param_set);
511     gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
512
513     len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
514     p = buf = OPENSSL_malloc(len);
515     if (!buf) {
516         GOST_CIPHER_PARAMS_free(gcp);
517         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
518         return 0;
519     }
520     i2d_GOST_CIPHER_PARAMS(gcp, &p);
521     GOST_CIPHER_PARAMS_free(gcp);
522
523     os = ASN1_OCTET_STRING_new();
524
525     if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
526         OPENSSL_free(buf);
527         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
528         return 0;
529     }
530     OPENSSL_free(buf);
531
532     ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
533     return 1;
534 }
535
536 /* Store parameters into ASN1 structure */
537 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
538 {
539     int ret = -1;
540     int len;
541     GOST_CIPHER_PARAMS *gcp = NULL;
542     unsigned char *p;
543     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_cipher_data(ctx);
544     if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
545         return ret;
546     }
547
548     p = params->value.sequence->data;
549
550     gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
551                                  params->value.sequence->length);
552
553     len = gcp->iv->length;
554     if (len != EVP_CIPHER_CTX_iv_length(ctx)) {
555         GOST_CIPHER_PARAMS_free(gcp);
556         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
557         return -1;
558     }
559     if (!gost_cipher_set_param(c, OBJ_obj2nid(gcp->enc_param_set))) {
560         GOST_CIPHER_PARAMS_free(gcp);
561         return -1;
562     }
563     memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), gcp->iv->data,
564            EVP_CIPHER_CTX_iv_length(ctx));
565
566     GOST_CIPHER_PARAMS_free(gcp);
567
568     return 1;
569 }
570
571 int gost_imit_init_cpa(EVP_MD_CTX *ctx)
572 {
573     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
574     memset(c->buffer, 0, sizeof(c->buffer));
575     memset(c->partial_block, 0, sizeof(c->partial_block));
576     c->count = 0;
577     c->bytes_left = 0;
578     c->key_meshing = 1;
579     gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
580     return 1;
581 }
582
583 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
584                            const unsigned char *data)
585 {
586     unsigned char buffer[8];
587     /*
588      * We are using local buffer for iv because CryptoPro doesn't interpret
589      * internal state of MAC algorithm as iv during keymeshing (but does
590      * initialize internal state from iv in key transport
591      */
592     assert(c->count % 8 == 0 && c->count <= 1024);
593     if (c->key_meshing && c->count == 1024) {
594         cryptopro_key_meshing(&(c->cctx), buffer);
595     }
596     mac_block(&(c->cctx), c->buffer, data);
597     c->count = c->count % 1024 + 8;
598 }
599
600 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
601 {
602     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
603     const unsigned char *p = data;
604     size_t bytes = count, i;
605     if (!(c->key_set)) {
606         GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
607         return 0;
608     }
609     if (c->bytes_left) {
610         for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
611             c->partial_block[i] = *p;
612         }
613         if (i == 8) {
614             mac_block_mesh(c, c->partial_block);
615         } else {
616             c->bytes_left = i;
617             return 1;
618         }
619     }
620     while (bytes > 8) {
621         mac_block_mesh(c, p);
622         p += 8;
623         bytes -= 8;
624     }
625     if (bytes > 0) {
626         memcpy(c->partial_block, p, bytes);
627     }
628     c->bytes_left = bytes;
629     return 1;
630 }
631
632 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
633 {
634     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
635     if (!c->key_set) {
636         GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
637         return 0;
638     }
639     if (c->count == 0 && c->bytes_left) {
640         unsigned char buffer[8];
641         memset(buffer, 0, 8);
642         gost_imit_update(ctx, buffer, 8);
643     }
644     if (c->bytes_left) {
645         int i;
646         for (i = c->bytes_left; i < 8; i++) {
647             c->partial_block[i] = 0;
648         }
649         mac_block_mesh(c, c->partial_block);
650     }
651     get_mac(c->buffer, 32, md);
652     return 1;
653 }
654
655 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
656 {
657     switch (type) {
658     case EVP_MD_CTRL_KEY_LEN:
659         *((unsigned int *)(ptr)) = 32;
660         return 1;
661     case EVP_MD_CTRL_SET_KEY:
662         {
663             if (arg != 32) {
664                 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
665                 return 0;
666             }
667
668             gost_key(&(((struct ossl_gost_imit_ctx *)(EVP_MD_CTX_md_data(ctx)))->cctx),
669                      ptr);
670             ((struct ossl_gost_imit_ctx *)(EVP_MD_CTX_md_data(ctx)))->key_set = 1;
671             return 1;
672
673         }
674     default:
675         return 0;
676     }
677 }
678
679 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
680 {
681     memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
682            sizeof(struct ossl_gost_imit_ctx));
683     return 1;
684 }
685
686 /* Clean up imit ctx */
687 int gost_imit_cleanup(EVP_MD_CTX *ctx)
688 {
689     memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
690     return 1;
691 }