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