]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_crypt.c
gost_crypt: Add Gost28147_89_cbc_cipher
[openssl-gost/engine.git] / gost_crypt.c
1 /**********************************************************************
2  *             gost_crypt.c - Initialize all ciphers                  *
3  *                                                                    *
4  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
5  *             Copyright (c) 2020 Chikunov Vitaly <vt@altlinux.org>   *
6  *         This file is distributed under the same license as OpenSSL *
7  *                                                                    *
8  *       OpenSSL interface to GOST 28147-89 cipher functions          *
9  *          Requires OpenSSL 0.9.9 for compilation                    *
10  **********************************************************************/
11 #include <string.h>
12 #include "gost89.h"
13 #include <openssl/err.h>
14 #include <openssl/rand.h>
15 #include "e_gost_err.h"
16 #include "gost_lcl.h"
17 #include "gost_gost2015.h"
18
19 #if !defined(CCGOST_DEBUG) && !defined(DEBUG)
20 # ifndef NDEBUG
21 #  define NDEBUG
22 # endif
23 #endif
24 #include <assert.h>
25
26 static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
27                             const unsigned char *iv, int enc);
28 static int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
29                                 const unsigned char *iv, int enc);
30 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
31                                 const unsigned char *iv, int enc);
32 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
33                                   const unsigned char *key,
34                                   const unsigned char *iv, int enc);
35 /* Handles block of data in CFB mode */
36 static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
37                               const unsigned char *in, size_t inl);
38 /* Handles block of data in CBC mode */
39 static int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
40                               const unsigned char *in, size_t inl);
41 /* Handles block of data in CNT mode */
42 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
43                               const unsigned char *in, size_t inl);
44 /* Cleanup function */
45 static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
46 /* set/get cipher parameters */
47 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
48 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
49 /* Control function */
50 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
51
52 static int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
53                              const unsigned char *iv, int enc);
54 static int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
55                              const unsigned char *iv, int enc);
56 /* Handles block of data in CBC mode */
57 static int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
58                                const unsigned char *in, size_t inl);
59 static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
60                                const unsigned char *in, size_t inl);
61
62 static int magma_cipher_do_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, unsigned char *out,
63                                const unsigned char *in, size_t inl);
64
65 /* set/get cipher parameters */
66 static int magma_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
67 static int magma_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
68 /* Control function */
69 static int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
70 static int magma_cipher_ctl_acpkm_omac(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
71
72 EVP_CIPHER *GOST_init_cipher(GOST_cipher *c)
73 {
74     if (c->cipher)
75         return c->cipher;
76
77     EVP_CIPHER *cipher;
78     if (!(cipher = EVP_CIPHER_meth_new(c->nid, c->block_size, c->key_len))
79         || !EVP_CIPHER_meth_set_iv_length(cipher, c->iv_len)
80         || !EVP_CIPHER_meth_set_flags(cipher, c->flags)
81         || !EVP_CIPHER_meth_set_init(cipher, c->init)
82         || !EVP_CIPHER_meth_set_do_cipher(cipher, c->do_cipher)
83         || !EVP_CIPHER_meth_set_cleanup(cipher, c->cleanup)
84         || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, c->ctx_size)
85         || !EVP_CIPHER_meth_set_set_asn1_params(cipher, c->set_asn1_parameters)
86         || !EVP_CIPHER_meth_set_get_asn1_params(cipher, c->get_asn1_parameters)
87         || !EVP_CIPHER_meth_set_ctrl(cipher, c->ctrl)) {
88         EVP_CIPHER_meth_free(cipher);
89         cipher = NULL;
90     }
91     c->cipher = cipher;
92     return c->cipher;
93 }
94
95 void GOST_deinit_cipher(GOST_cipher *c)
96 {
97     if (c->cipher) {
98         EVP_CIPHER_meth_free(c->cipher);
99         c->cipher = NULL;
100     }
101 }
102
103 GOST_cipher Gost28147_89_cipher = {
104     .nid = NID_id_Gost28147_89,
105     .block_size = 1,
106     .key_len = 32,
107     .iv_len = 8,
108     .flags = EVP_CIPH_CFB_MODE |
109         EVP_CIPH_NO_PADDING |
110         EVP_CIPH_CUSTOM_IV |
111         EVP_CIPH_RAND_KEY |
112         EVP_CIPH_ALWAYS_CALL_INIT,
113     .init = gost_cipher_init,
114     .do_cipher = gost_cipher_do_cfb,
115     .cleanup = gost_cipher_cleanup,
116     .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
117     .set_asn1_parameters = gost89_set_asn1_parameters,
118     .get_asn1_parameters = gost89_get_asn1_parameters,
119     .ctrl = gost_cipher_ctl,
120 };
121
122 GOST_cipher Gost28147_89_cbc_cipher = {
123     .nid = NID_gost89_cbc,
124     .block_size = 8,
125     .key_len = 32,
126     .iv_len = 8,
127     .flags = EVP_CIPH_CBC_MODE |
128         EVP_CIPH_CUSTOM_IV |
129         EVP_CIPH_RAND_KEY |
130         EVP_CIPH_ALWAYS_CALL_INIT,
131     .init = gost_cipher_init_cbc,
132     .do_cipher = gost_cipher_do_cbc,
133     .cleanup = gost_cipher_cleanup,
134     .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
135     .set_asn1_parameters = gost89_set_asn1_parameters,
136     .get_asn1_parameters = gost89_get_asn1_parameters,
137     .ctrl = gost_cipher_ctl,
138 };
139
140 static EVP_CIPHER *_hidden_gost89_cnt = NULL;
141 const EVP_CIPHER *cipher_gost_cpacnt(void)
142 {
143     if (_hidden_gost89_cnt == NULL
144         && ((_hidden_gost89_cnt =
145              EVP_CIPHER_meth_new(NID_gost89_cnt, 1 /* block_size */ ,
146                                  32 /* key_size */ )) == NULL
147             || !EVP_CIPHER_meth_set_iv_length(_hidden_gost89_cnt, 8)
148             || !EVP_CIPHER_meth_set_flags(_hidden_gost89_cnt,
149                                           EVP_CIPH_OFB_MODE |
150                                           EVP_CIPH_NO_PADDING |
151                                           EVP_CIPH_CUSTOM_IV |
152                                           EVP_CIPH_RAND_KEY |
153                                           EVP_CIPH_ALWAYS_CALL_INIT)
154             || !EVP_CIPHER_meth_set_init(_hidden_gost89_cnt,
155                                          gost_cipher_init_cpa)
156             || !EVP_CIPHER_meth_set_do_cipher(_hidden_gost89_cnt,
157                                               gost_cipher_do_cnt)
158             || !EVP_CIPHER_meth_set_cleanup(_hidden_gost89_cnt,
159                                             gost_cipher_cleanup)
160             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_gost89_cnt,
161                                                   sizeof(struct
162                                                          ossl_gost_cipher_ctx))
163             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_gost89_cnt,
164                                                     gost89_set_asn1_parameters)
165             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_gost89_cnt,
166                                                     gost89_get_asn1_parameters)
167             || !EVP_CIPHER_meth_set_ctrl(_hidden_gost89_cnt, gost_cipher_ctl))) {
168         EVP_CIPHER_meth_free(_hidden_gost89_cnt);
169         _hidden_gost89_cnt = NULL;
170     }
171     return _hidden_gost89_cnt;
172 }
173
174 static EVP_CIPHER *_hidden_gost89_cnt_12 = NULL;
175 const EVP_CIPHER *cipher_gost_cpcnt_12(void)
176 {
177     if (_hidden_gost89_cnt_12 == NULL
178         && ((_hidden_gost89_cnt_12 =
179              EVP_CIPHER_meth_new(NID_gost89_cnt_12, 1 /* block_size */ ,
180                                  32 /* key_size */ )) == NULL
181             || !EVP_CIPHER_meth_set_iv_length(_hidden_gost89_cnt_12, 8)
182             || !EVP_CIPHER_meth_set_flags(_hidden_gost89_cnt_12,
183                                           EVP_CIPH_OFB_MODE |
184                                           EVP_CIPH_NO_PADDING |
185                                           EVP_CIPH_CUSTOM_IV |
186                                           EVP_CIPH_RAND_KEY |
187                                           EVP_CIPH_ALWAYS_CALL_INIT)
188             || !EVP_CIPHER_meth_set_init(_hidden_gost89_cnt_12,
189                                          gost_cipher_init_cp_12)
190             || !EVP_CIPHER_meth_set_do_cipher(_hidden_gost89_cnt_12,
191                                               gost_cipher_do_cnt)
192             || !EVP_CIPHER_meth_set_cleanup(_hidden_gost89_cnt_12,
193                                             gost_cipher_cleanup)
194             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_gost89_cnt_12,
195                                                   sizeof(struct
196                                                          ossl_gost_cipher_ctx))
197             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_gost89_cnt_12,
198                                                     gost89_set_asn1_parameters)
199             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_gost89_cnt_12,
200                                                     gost89_get_asn1_parameters)
201             || !EVP_CIPHER_meth_set_ctrl(_hidden_gost89_cnt_12,
202                                          gost_cipher_ctl))) {
203         EVP_CIPHER_meth_free(_hidden_gost89_cnt_12);
204         _hidden_gost89_cnt_12 = NULL;
205     }
206     return _hidden_gost89_cnt_12;
207 }
208
209 static EVP_CIPHER *_hidden_magma_ctr = NULL;
210 const EVP_CIPHER *cipher_magma_ctr(void)
211 {
212     if (_hidden_magma_ctr == NULL
213         && ((_hidden_magma_ctr =
214              EVP_CIPHER_meth_new(NID_magma_ctr, 1 /* block_size */ ,
215                                  32 /* key_size */ )) == NULL
216             || !EVP_CIPHER_meth_set_iv_length(_hidden_magma_ctr, 4)
217             || !EVP_CIPHER_meth_set_flags(_hidden_magma_ctr,
218                                           EVP_CIPH_CTR_MODE |
219                                           EVP_CIPH_NO_PADDING |
220                                           EVP_CIPH_CUSTOM_IV |
221                                           EVP_CIPH_RAND_KEY |
222                                           EVP_CIPH_ALWAYS_CALL_INIT)
223             || !EVP_CIPHER_meth_set_init(_hidden_magma_ctr, magma_cipher_init)
224             || !EVP_CIPHER_meth_set_do_cipher(_hidden_magma_ctr,
225                                               magma_cipher_do_ctr)
226             || !EVP_CIPHER_meth_set_cleanup(_hidden_magma_ctr,
227                                             gost_cipher_cleanup)
228             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_magma_ctr,
229                                                   sizeof(struct
230                                                          ossl_gost_cipher_ctx))
231             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_magma_ctr,
232                                                     magma_set_asn1_parameters)
233             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_magma_ctr,
234                                                     magma_get_asn1_parameters)
235             || !EVP_CIPHER_meth_set_ctrl(_hidden_magma_ctr, magma_cipher_ctl))) {
236         EVP_CIPHER_meth_free(_hidden_magma_ctr);
237         _hidden_magma_ctr = NULL;
238     }
239     return _hidden_magma_ctr;
240 }
241
242 static EVP_CIPHER *_hidden_magma_ctr_acpkm = NULL;
243 const EVP_CIPHER *cipher_magma_ctr_acpkm(void)
244 {
245     if (_hidden_magma_ctr_acpkm == NULL
246         && ((_hidden_magma_ctr_acpkm =
247              EVP_CIPHER_meth_new(NID_magma_ctr_acpkm, 1 /* block_size */ ,
248                                  32 /* key_size */ )) == NULL
249             || !EVP_CIPHER_meth_set_iv_length(_hidden_magma_ctr_acpkm, 4)
250             || !EVP_CIPHER_meth_set_flags(_hidden_magma_ctr_acpkm,
251                                           EVP_CIPH_CTR_MODE |
252                                           EVP_CIPH_NO_PADDING |
253                                           EVP_CIPH_CUSTOM_IV |
254                                           EVP_CIPH_RAND_KEY |
255                                           EVP_CIPH_ALWAYS_CALL_INIT)
256             || !EVP_CIPHER_meth_set_init(_hidden_magma_ctr_acpkm, magma_cipher_init)
257             || !EVP_CIPHER_meth_set_do_cipher(_hidden_magma_ctr_acpkm,
258                                               magma_cipher_do_ctr)
259             || !EVP_CIPHER_meth_set_cleanup(_hidden_magma_ctr_acpkm,
260                                             gost_cipher_cleanup)
261             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_magma_ctr_acpkm,
262                                                   sizeof(struct
263                                                          ossl_gost_cipher_ctx))
264             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_magma_ctr_acpkm,
265                                                     magma_set_asn1_parameters)
266             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_magma_ctr_acpkm,
267                                                     magma_get_asn1_parameters)
268
269             || !EVP_CIPHER_meth_set_ctrl(_hidden_magma_ctr_acpkm, magma_cipher_ctl))) {
270         EVP_CIPHER_meth_free(_hidden_magma_ctr_acpkm);
271         _hidden_magma_ctr_acpkm = NULL;
272     }
273     return _hidden_magma_ctr_acpkm;
274 }
275
276 static EVP_CIPHER *_hidden_magma_ctr_acpkm_omac = NULL;
277 const EVP_CIPHER *cipher_magma_ctr_acpkm_omac(void)
278 {
279     if (_hidden_magma_ctr_acpkm_omac == NULL
280         && ((_hidden_magma_ctr_acpkm_omac =
281              EVP_CIPHER_meth_new(NID_magma_ctr_acpkm_omac, 1 /* block_size */ ,
282                                  32 /* key_size */ )) == NULL
283             || !EVP_CIPHER_meth_set_iv_length(_hidden_magma_ctr_acpkm_omac, 4)
284             || !EVP_CIPHER_meth_set_flags(_hidden_magma_ctr_acpkm_omac,
285                                           EVP_CIPH_CTR_MODE |
286                                           EVP_CIPH_NO_PADDING |
287                                           EVP_CIPH_CUSTOM_IV |
288                                           EVP_CIPH_RAND_KEY |
289                                           EVP_CIPH_ALWAYS_CALL_INIT |
290                                                                                                                                                                         EVP_CIPH_CUSTOM_COPY |
291                                                                                                                                                                         EVP_CIPH_FLAG_CUSTOM_CIPHER |
292                                                                                                                                                                         EVP_CIPH_FLAG_CIPHER_WITH_MAC)
293             || !EVP_CIPHER_meth_set_init(_hidden_magma_ctr_acpkm_omac, magma_cipher_init_ctr_acpkm_omac)
294             || !EVP_CIPHER_meth_set_do_cipher(_hidden_magma_ctr_acpkm_omac,
295                                               magma_cipher_do_ctr_acpkm_omac)
296             || !EVP_CIPHER_meth_set_cleanup(_hidden_magma_ctr_acpkm_omac,
297                                             gost_cipher_cleanup)
298             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_magma_ctr_acpkm_omac,
299                                                   sizeof(struct
300                                                          ossl_gost_cipher_ctx))
301             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_magma_ctr_acpkm_omac,
302                                                     magma_set_asn1_parameters)
303             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_magma_ctr_acpkm_omac,
304                                                     magma_get_asn1_parameters)
305             || !EVP_CIPHER_meth_set_ctrl(_hidden_magma_ctr_acpkm_omac, magma_cipher_ctl_acpkm_omac))) {
306         EVP_CIPHER_meth_free(_hidden_magma_ctr_acpkm_omac);
307         _hidden_magma_ctr_acpkm_omac = NULL;
308     }
309     return _hidden_magma_ctr_acpkm_omac;
310 }
311
312 static EVP_CIPHER *_hidden_magma_cbc = NULL;
313 const EVP_CIPHER *cipher_magma_cbc(void)
314 {
315     if (_hidden_magma_cbc == NULL
316         && ((_hidden_magma_cbc =
317              EVP_CIPHER_meth_new(NID_magma_cbc, 8 /* block_size */ ,
318                                  32 /* key_size */ )) == NULL
319             || !EVP_CIPHER_meth_set_iv_length(_hidden_magma_cbc, 8)
320             || !EVP_CIPHER_meth_set_flags(_hidden_magma_cbc,
321                                           EVP_CIPH_CBC_MODE |
322                                           EVP_CIPH_CUSTOM_IV |
323                                           EVP_CIPH_RAND_KEY |
324                                           EVP_CIPH_ALWAYS_CALL_INIT)
325             || !EVP_CIPHER_meth_set_init(_hidden_magma_cbc, magma_cipher_init)
326             || !EVP_CIPHER_meth_set_do_cipher(_hidden_magma_cbc,
327                                               magma_cipher_do_cbc)
328             || !EVP_CIPHER_meth_set_cleanup(_hidden_magma_cbc,
329                                             gost_cipher_cleanup)
330             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_magma_cbc,
331                                                   sizeof(struct
332                                                          ossl_gost_cipher_ctx))
333             || !EVP_CIPHER_meth_set_ctrl(_hidden_magma_cbc, magma_cipher_ctl))) {
334         EVP_CIPHER_meth_free(_hidden_magma_cbc);
335         _hidden_magma_cbc = NULL;
336     }
337     return _hidden_magma_cbc;
338 }
339
340 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
341 /* Init functions which set specific parameters */
342 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
343 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx);
344 /* process block of data */
345 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
346 /* Return computed value */
347 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
348 /* Copies context */
349 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
350 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
351 /* Control function, knows how to set MAC key.*/
352 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
353
354 static EVP_MD *_hidden_Gost28147_89_MAC_md = NULL;
355 static EVP_MD *_hidden_Gost28147_89_12_MAC_md = NULL;
356
357 EVP_MD *imit_gost_cpa(void)
358 {
359     if (_hidden_Gost28147_89_MAC_md == NULL) {
360         EVP_MD *md;
361
362         if ((md = EVP_MD_meth_new(NID_id_Gost28147_89_MAC, NID_undef)) == NULL
363             || !EVP_MD_meth_set_result_size(md, 4)
364             || !EVP_MD_meth_set_input_blocksize(md, 8)
365             || !EVP_MD_meth_set_app_datasize(md,
366                                              sizeof(struct ossl_gost_imit_ctx))
367             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_XOF)
368             || !EVP_MD_meth_set_init(md, gost_imit_init_cpa)
369             || !EVP_MD_meth_set_update(md, gost_imit_update)
370             || !EVP_MD_meth_set_final(md, gost_imit_final)
371             || !EVP_MD_meth_set_copy(md, gost_imit_copy)
372             || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
373             || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
374             EVP_MD_meth_free(md);
375             md = NULL;
376         }
377         _hidden_Gost28147_89_MAC_md = md;
378     }
379     return _hidden_Gost28147_89_MAC_md;
380 }
381
382 void imit_gost_cpa_destroy(void)
383 {
384     EVP_MD_meth_free(_hidden_Gost28147_89_MAC_md);
385     _hidden_Gost28147_89_MAC_md = NULL;
386 }
387
388 EVP_MD *imit_gost_cp_12(void)
389 {
390     if (_hidden_Gost28147_89_12_MAC_md == NULL) {
391         EVP_MD *md;
392
393         if ((md = EVP_MD_meth_new(NID_gost_mac_12, NID_undef)) == NULL
394             || !EVP_MD_meth_set_result_size(md, 4)
395             || !EVP_MD_meth_set_input_blocksize(md, 8)
396             || !EVP_MD_meth_set_app_datasize(md,
397                                              sizeof(struct ossl_gost_imit_ctx))
398             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_XOF)
399             || !EVP_MD_meth_set_init(md, gost_imit_init_cp_12)
400             || !EVP_MD_meth_set_update(md, gost_imit_update)
401             || !EVP_MD_meth_set_final(md, gost_imit_final)
402             || !EVP_MD_meth_set_copy(md, gost_imit_copy)
403             || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
404             || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
405             EVP_MD_meth_free(md);
406             md = NULL;
407         }
408         _hidden_Gost28147_89_12_MAC_md = md;
409     }
410     return _hidden_Gost28147_89_12_MAC_md;
411 }
412
413 void imit_gost_cp_12_destroy(void)
414 {
415     EVP_MD_meth_free(_hidden_Gost28147_89_12_MAC_md);
416     _hidden_Gost28147_89_12_MAC_md = NULL;
417 }
418
419 /*
420  * Correspondence between gost parameter OIDs and substitution blocks
421  * NID field is filed by register_gost_NID function in engine.c
422  * upon engine initialization
423  */
424
425 struct gost_cipher_info gost_cipher_list[] = {
426     /*- NID *//*
427      * Subst block
428      *//*
429      * Key meshing
430      */
431     /*
432      * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
433      */
434     {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
435      1},
436     {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
437      1},
438     {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
439      1},
440     {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
441      1},
442     {NID_id_tc26_gost_28147_param_Z, &Gost28147_TC26ParamSetZ, 1},
443     {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
444     {NID_undef, NULL, 0}
445 };
446
447 /*
448  * get encryption parameters from crypto network settings FIXME For now we
449  * use environment var CRYPT_PARAMS as place to store these settings.
450  * Actually, it is better to use engine control command, read from
451  * configuration file to set them
452  */
453 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
454 {
455     int nid;
456     struct gost_cipher_info *param;
457     if (!obj) {
458         const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
459         if (!params || !strlen(params)) {
460             int i;
461             for (i = 0; gost_cipher_list[i].nid != NID_undef; i++)
462                 if (gost_cipher_list[i].nid == NID_id_tc26_gost_28147_param_Z)
463                     return &gost_cipher_list[i];
464             return &gost_cipher_list[0];
465         }
466
467         nid = OBJ_txt2nid(params);
468         if (nid == NID_undef) {
469             GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
470                     GOST_R_INVALID_CIPHER_PARAM_OID);
471             ERR_add_error_data(3, "Unsupported CRYPT_PARAMS='",
472                 params, "' specified in environment or in config");
473             return NULL;
474         }
475     } else {
476         nid = OBJ_obj2nid(obj);
477     }
478     for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
479          param++) ;
480     if (!param->sblock) {
481         GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
482         return NULL;
483     }
484     return param;
485 }
486
487 /* Sets cipher param from paramset NID. */
488 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
489 {
490     const struct gost_cipher_info *param;
491     param = get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
492     if (!param)
493         return 0;
494
495     c->paramNID = param->nid;
496     c->key_meshing = param->key_meshing;
497     c->count = 0;
498     gost_init(&(c->cctx), param->sblock);
499     return 1;
500 }
501
502 /* Initializes EVP_CIPHER_CTX by paramset NID */
503 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
504                                   const unsigned char *key,
505                                   const unsigned char *iv, int enc,
506                                   int paramNID, int mode)
507 {
508     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
509     if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
510         if (!gost_cipher_set_param(c, paramNID))
511             return 0;
512         EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
513     }
514     if (key)
515         gost_key(&(c->cctx), key);
516     if (iv) {
517         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
518                EVP_CIPHER_CTX_iv_length(ctx));
519     }
520     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
521            EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
522     return 1;
523 }
524
525 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
526                                 const unsigned char *key,
527                                 const unsigned char *iv,
528                                 gost_subst_block * block)
529 {
530     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
531     gost_init(&(c->cctx), block);
532     c->key_meshing = 1;
533     c->count = 0;
534     if (key)
535         gost_key(&(c->cctx), key);
536     if (iv) {
537         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
538                EVP_CIPHER_CTX_iv_length(ctx));
539     }
540     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
541            EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
542     return 1;
543 }
544
545 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
546                                 const unsigned char *iv, int enc)
547 {
548     return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
549 }
550
551 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
552                                   const unsigned char *key,
553                                   const unsigned char *iv, int enc)
554 {
555     return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
556 }
557
558 /* Initializes EVP_CIPHER_CTX with default values */
559 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
560                      const unsigned char *iv, int enc)
561 {
562     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
563                                   EVP_CIPH_CFB_MODE);
564 }
565
566 /* Initializes EVP_CIPHER_CTX with default values */
567 int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
568                          const unsigned char *iv, int enc)
569 {
570     return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
571                                   EVP_CIPH_CBC_MODE);
572 }
573
574 /* Initializes EVP_CIPHER_CTX with default values */
575 int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
576                       const unsigned char *iv, int enc)
577 {
578     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
579     /* FIXME this is just initializtion check */
580     if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
581         if (!gost_cipher_set_param(c, NID_id_tc26_gost_28147_param_Z))
582             return 0;
583         EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
584
585         if (enc) {
586             if (init_zero_kdf_seed(c->kdf_seed) == 0)
587                 return -1;
588         }
589     }
590
591     if (key)
592         magma_key(&(c->cctx), key);
593     if (iv) {
594         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
595                EVP_CIPHER_CTX_iv_length(ctx));
596     }
597     memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
598            EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
599
600     if (EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm
601      || EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm_omac) {
602        c->key_meshing = 1024;
603     } else {
604        c->key_meshing = 0;
605     }
606
607     return 1;
608 }
609
610 /* Initializes EVP_CIPHER_CTX with default values */
611 int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
612                       const unsigned char *iv, int enc)
613 {
614         if (key) {
615     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
616                 unsigned char cipher_key[32];
617                 c->omac_ctx = EVP_MD_CTX_new();
618
619                 if (c->omac_ctx == NULL) {
620                     GOSTerr(GOST_F_MAGMA_CIPHER_INIT_CTR_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
621                                 return 0;
622                 }
623
624                 if (gost2015_acpkm_omac_init(NID_magma_mac, enc, key,
625                                  c->omac_ctx, cipher_key, c->kdf_seed) != 1) {
626                     EVP_MD_CTX_free(c->omac_ctx);
627                                 c->omac_ctx = NULL;
628                     return 0;
629                 }
630
631                 return magma_cipher_init(ctx, cipher_key, iv, enc);
632         }
633
634         return magma_cipher_init(ctx, key, iv, enc);
635 }
636
637 /*
638  * Wrapper around gostcrypt function from gost89.c which perform key meshing
639  * when nesseccary
640  */
641 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
642 {
643     struct ossl_gost_cipher_ctx *c = ctx;
644     assert(c->count % 8 == 0 && c->count <= 1024);
645     if (c->key_meshing && c->count == 1024) {
646         cryptopro_key_meshing(&(c->cctx), iv);
647     }
648     gostcrypt(&(c->cctx), iv, buf);
649     c->count = c->count % 1024 + 8;
650 }
651
652 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
653 {
654     struct ossl_gost_cipher_ctx *c = ctx;
655     word32 g, go;
656     unsigned char buf1[8];
657     assert(c->count % 8 == 0 && c->count <= 1024);
658     if (c->key_meshing && c->count == 1024) {
659         cryptopro_key_meshing(&(c->cctx), iv);
660     }
661     if (c->count == 0) {
662         gostcrypt(&(c->cctx), iv, buf1);
663     } else {
664         memcpy(buf1, iv, 8);
665     }
666     g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
667     g += 0x01010101;
668     buf1[0] = (unsigned char)(g & 0xff);
669     buf1[1] = (unsigned char)((g >> 8) & 0xff);
670     buf1[2] = (unsigned char)((g >> 16) & 0xff);
671     buf1[3] = (unsigned char)((g >> 24) & 0xff);
672     g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
673     go = g;
674     g += 0x01010104;
675     if (go > g)                 /* overflow */
676         g++;
677     buf1[4] = (unsigned char)(g & 0xff);
678     buf1[5] = (unsigned char)((g >> 8) & 0xff);
679     buf1[6] = (unsigned char)((g >> 16) & 0xff);
680     buf1[7] = (unsigned char)((g >> 24) & 0xff);
681     memcpy(iv, buf1, 8);
682     gostcrypt(&(c->cctx), buf1, buf);
683     c->count = c->count % 1024 + 8;
684 }
685
686 /* GOST encryption in CBC mode */
687 int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
688                        const unsigned char *in, size_t inl)
689 {
690     unsigned char b[8];
691     const unsigned char *in_ptr = in;
692     unsigned char *out_ptr = out;
693     int i;
694     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
695     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
696     if (EVP_CIPHER_CTX_encrypting(ctx)) {
697         while (inl > 0) {
698
699             for (i = 0; i < 8; i++) {
700                 b[i] = iv[i] ^ in_ptr[i];
701             }
702             gostcrypt(&(c->cctx), b, out_ptr);
703             memcpy(iv, out_ptr, 8);
704             out_ptr += 8;
705             in_ptr += 8;
706             inl -= 8;
707         }
708     } else {
709         while (inl > 0) {
710             gostdecrypt(&(c->cctx), in_ptr, b);
711             for (i = 0; i < 8; i++) {
712                 out_ptr[i] = iv[i] ^ b[i];
713             }
714             memcpy(iv, in_ptr, 8);
715             out_ptr += 8;
716             in_ptr += 8;
717             inl -= 8;
718         }
719     }
720     return 1;
721 }
722
723 /* MAGMA encryption in CBC mode */
724 int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
725                         const unsigned char *in, size_t inl)
726 {
727     unsigned char b[8];
728     unsigned char d[8];
729     const unsigned char *in_ptr = in;
730     unsigned char *out_ptr = out;
731     int i;
732     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
733     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
734     if (EVP_CIPHER_CTX_encrypting(ctx)) {
735         while (inl > 0) {
736
737             for (i = 0; i < 8; i++) {
738                 b[7 - i] = iv[i] ^ in_ptr[i];
739             }
740             gostcrypt(&(c->cctx), b, d);
741
742             for (i = 0; i < 8; i++) {
743                 out_ptr[7 - i] = d[i];
744             }
745             memcpy(iv, out_ptr, 8);
746             out_ptr += 8;
747             in_ptr += 8;
748             inl -= 8;
749         }
750     } else {
751         while (inl > 0) {
752             for (i = 0; i < 8; i++) {
753                 d[7 - i] = in_ptr[i];
754             }
755             gostdecrypt(&(c->cctx), d, b);
756             memcpy(d, in_ptr, 8);
757             for (i = 0; i < 8; i++) {
758                 out_ptr[i] = iv[i] ^ b[7 - i];
759             }
760             memcpy(iv, d, 8);
761             out_ptr += 8;
762             in_ptr += 8;
763             inl -= 8;
764         }
765     }
766     return 1;
767 }
768
769 /* increment counter (64-bit int) by 1 */
770 static void ctr64_inc(unsigned char *counter)
771 {
772     inc_counter(counter, 8);
773 }
774
775 /* MAGMA encryption in CTR mode */
776 static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
777                                const unsigned char *in, size_t inl)
778 {
779     const unsigned char *in_ptr = in;
780     unsigned char *out_ptr = out;
781     size_t i = 0;
782     size_t j;
783     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
784     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
785     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
786     unsigned char b[8];
787 /* Process partial blocks */
788     if (EVP_CIPHER_CTX_num(ctx)) {
789         for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
790              j++, i++, in_ptr++, out_ptr++) {
791             *out_ptr = buf[7 - j] ^ (*in_ptr);
792         }
793         if (j == 8) {
794             EVP_CIPHER_CTX_set_num(ctx, 0);
795         } else {
796             EVP_CIPHER_CTX_set_num(ctx, j);
797             return inl;
798         }
799     }
800
801 /* Process full blocks */
802     for (; i + 8 <= inl; i += 8, in_ptr += 8, out_ptr += 8) {
803         for (j = 0; j < 8; j++) {
804             b[7 - j] = iv[j];
805         }
806         gostcrypt(&(c->cctx), b, buf);
807         for (j = 0; j < 8; j++) {
808             out_ptr[j] = buf[7 - j] ^ in_ptr[j];
809         }
810         ctr64_inc(iv);
811         c->count += 8;
812         if (c->key_meshing && (c->count % c->key_meshing == 0))
813             acpkm_magma_key_meshing(&(c->cctx));
814     }
815
816 /* Process the rest of plaintext */
817     if (i < inl) {
818         for (j = 0; j < 8; j++) {
819             b[7 - j] = iv[j];
820         }
821         gostcrypt(&(c->cctx), b, buf);
822
823         for (j = 0; i < inl; j++, i++) {
824             out_ptr[j] = buf[7 - j] ^ in_ptr[j];
825         }
826
827         ctr64_inc(iv);
828         c->count += 8;
829         if (c->key_meshing && (c->count % c->key_meshing == 0))
830             acpkm_magma_key_meshing(&(c->cctx));
831
832         EVP_CIPHER_CTX_set_num(ctx, j);
833     } else {
834         EVP_CIPHER_CTX_set_num(ctx, 0);
835     }
836
837     return inl;
838 }
839
840 /* MAGMA encryption in CTR mode */
841 static int magma_cipher_do_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, unsigned char *out,
842                                const unsigned char *in, size_t inl)
843 {
844   struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
845
846         if (in == NULL && inl == 0) /* Final call */
847                 return gost2015_final_call(ctx, c->omac_ctx, MAGMA_MAC_MAX_SIZE, c->tag, magma_cipher_do_ctr);
848
849   if (in == NULL)
850       return -1;
851
852         /* As in and out can be the same pointer, process unencrypted here */
853         if (EVP_CIPHER_CTX_encrypting(ctx))
854                 EVP_DigestSignUpdate(c->omac_ctx, in, inl);
855
856   if (magma_cipher_do_ctr(ctx, out, in, inl) != inl)
857       return -1;
858
859         /* As in and out can be the same pointer, process decrypted here */
860         if (!EVP_CIPHER_CTX_encrypting(ctx))
861                 EVP_DigestSignUpdate(c->omac_ctx, out, inl);
862
863         return inl;
864 }
865 /* GOST encryption in CFB mode */
866 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
867                        const unsigned char *in, size_t inl)
868 {
869     const unsigned char *in_ptr = in;
870     unsigned char *out_ptr = out;
871     size_t i = 0;
872     size_t j = 0;
873     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
874     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
875 /* process partial block if any */
876     if (EVP_CIPHER_CTX_num(ctx)) {
877         for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
878              j++, i++, in_ptr++, out_ptr++) {
879             if (!EVP_CIPHER_CTX_encrypting(ctx))
880                 buf[j + 8] = *in_ptr;
881             *out_ptr = buf[j] ^ (*in_ptr);
882             if (EVP_CIPHER_CTX_encrypting(ctx))
883                 buf[j + 8] = *out_ptr;
884         }
885         if (j == 8) {
886             memcpy(iv, buf + 8, 8);
887             EVP_CIPHER_CTX_set_num(ctx, 0);
888         } else {
889             EVP_CIPHER_CTX_set_num(ctx, j);
890             return 1;
891         }
892     }
893
894     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
895         /*
896          * block cipher current iv
897          */
898         gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
899         /*
900          * xor next block of input text with it and output it
901          */
902         /*
903          * output this block
904          */
905         if (!EVP_CIPHER_CTX_encrypting(ctx))
906             memcpy(iv, in_ptr, 8);
907         for (j = 0; j < 8; j++) {
908             out_ptr[j] = buf[j] ^ in_ptr[j];
909         }
910         /* Encrypt */
911         /* Next iv is next block of cipher text */
912         if (EVP_CIPHER_CTX_encrypting(ctx))
913             memcpy(iv, out_ptr, 8);
914     }
915 /* Process rest of buffer */
916     if (i < inl) {
917         gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
918         if (!EVP_CIPHER_CTX_encrypting(ctx))
919             memcpy(buf + 8, in_ptr, inl - i);
920         for (j = 0; i < inl; j++, i++) {
921             out_ptr[j] = buf[j] ^ in_ptr[j];
922         }
923         EVP_CIPHER_CTX_set_num(ctx, j);
924         if (EVP_CIPHER_CTX_encrypting(ctx))
925             memcpy(buf + 8, out_ptr, j);
926     } else {
927         EVP_CIPHER_CTX_set_num(ctx, 0);
928     }
929     return 1;
930 }
931
932 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
933                               const unsigned char *in, size_t inl)
934 {
935     const unsigned char *in_ptr = in;
936     unsigned char *out_ptr = out;
937     size_t i = 0;
938     size_t j;
939     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
940     unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
941 /* process partial block if any */
942     if (EVP_CIPHER_CTX_num(ctx)) {
943         for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
944              j++, i++, in_ptr++, out_ptr++) {
945             *out_ptr = buf[j] ^ (*in_ptr);
946         }
947         if (j == 8) {
948             EVP_CIPHER_CTX_set_num(ctx, 0);
949         } else {
950             EVP_CIPHER_CTX_set_num(ctx, j);
951             return 1;
952         }
953     }
954
955     for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
956         /*
957          * block cipher current iv
958          */
959         /* Encrypt */
960         gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
961         /*
962          * xor next block of input text with it and output it
963          */
964         /*
965          * output this block
966          */
967         for (j = 0; j < 8; j++) {
968             out_ptr[j] = buf[j] ^ in_ptr[j];
969         }
970     }
971 /* Process rest of buffer */
972     if (i < inl) {
973         gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
974         for (j = 0; i < inl; j++, i++) {
975             out_ptr[j] = buf[j] ^ in_ptr[j];
976         }
977         EVP_CIPHER_CTX_set_num(ctx, j);
978     } else {
979         EVP_CIPHER_CTX_set_num(ctx, 0);
980     }
981     return 1;
982 }
983
984 /* Cleaning up of EVP_CIPHER_CTX */
985 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
986 {
987     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
988                 EVP_MD_CTX_free(c->omac_ctx);
989     gost_destroy(&(c->cctx));
990     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
991     return 1;
992 }
993
994 /* Control function for gost cipher */
995 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
996 {
997     switch (type) {
998     case EVP_CTRL_RAND_KEY:
999         {
1000             if (RAND_priv_bytes
1001                 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
1002                 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
1003                 return -1;
1004             }
1005             break;
1006         }
1007     case EVP_CTRL_PBE_PRF_NID:
1008         if (ptr) {
1009             const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
1010             int nid = NID_id_tc26_hmac_gost_3411_2012_512;
1011
1012             if (params) {
1013                 if (!strcmp("md_gost12_256", params))
1014                     nid = NID_id_tc26_hmac_gost_3411_2012_256;
1015                 else if (!strcmp("md_gost12_512", params))
1016                     nid = NID_id_tc26_hmac_gost_3411_2012_512;
1017                 else if (!strcmp("md_gost94", params))
1018                     nid = NID_id_HMACGostR3411_94;
1019             }
1020             *((int *)ptr) = nid;
1021             return 1;
1022         } else {
1023             return 0;
1024         }
1025
1026     case EVP_CTRL_SET_SBOX:
1027         if (ptr) {
1028             struct ossl_gost_cipher_ctx *c =
1029                 EVP_CIPHER_CTX_get_cipher_data(ctx);
1030             int nid;
1031             int cur_meshing;
1032             int ret;
1033
1034             if (c == NULL) {
1035                 return -1;
1036             }
1037
1038             if (c->count != 0) {
1039                 return -1;
1040             }
1041
1042             nid = OBJ_txt2nid(ptr);
1043             if (nid == NID_undef) {
1044                 return 0;
1045             }
1046
1047             cur_meshing = c->key_meshing;
1048             ret = gost_cipher_set_param(c, nid);
1049             c->key_meshing = cur_meshing;
1050             return ret;
1051         } else {
1052             return 0;
1053         }
1054     case EVP_CTRL_KEY_MESH:
1055         {
1056             struct ossl_gost_cipher_ctx *c =
1057                 EVP_CIPHER_CTX_get_cipher_data(ctx);
1058
1059             if (c == NULL) {
1060                 return -1;
1061             }
1062
1063             if (c->count != 0) {
1064                 return -1;
1065             }
1066
1067             c->key_meshing = arg;
1068             return 1;
1069         }
1070     default:
1071         GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
1072         return -1;
1073     }
1074     return 1;
1075 }
1076
1077 /* Control function for gost cipher */
1078 int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1079 {
1080     switch (type) {
1081     case EVP_CTRL_RAND_KEY:
1082             if (RAND_priv_bytes
1083                 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
1084                 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
1085                 return -1;
1086             }
1087             break;
1088     case EVP_CTRL_KEY_MESH:
1089         {
1090             struct ossl_gost_cipher_ctx *c =
1091                 EVP_CIPHER_CTX_get_cipher_data(ctx);
1092
1093             if (c == NULL) {
1094                 return -1;
1095             }
1096
1097             if (c->count != 0) {
1098                 return -1;
1099             }
1100
1101             c->key_meshing = arg;
1102             return 1;
1103         }
1104     default:
1105         GOSTerr(GOST_F_MAGMA_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
1106         return -1;
1107     }
1108     return 1;
1109 }
1110
1111 static int magma_cipher_ctl_acpkm_omac(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1112 {
1113         switch (type)
1114         {
1115                 case EVP_CTRL_PROCESS_UNPROTECTED:
1116                 {
1117                         struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1118                         STACK_OF(X509_ATTRIBUTE) *x = ptr;
1119       return gost2015_process_unprotected_attributes(x, arg, MAGMA_MAC_MAX_SIZE, c->tag);
1120                 }
1121     case EVP_CTRL_COPY: {
1122                         EVP_CIPHER_CTX *out = ptr;
1123       struct ossl_gost_cipher_ctx *in_cctx  = EVP_CIPHER_CTX_get_cipher_data(ctx);
1124       struct ossl_gost_cipher_ctx *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
1125
1126                         if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
1127                                 out_cctx->omac_ctx = EVP_MD_CTX_new();
1128                                 if (out_cctx->omac_ctx == NULL) {
1129                                         GOSTerr(GOST_F_MAGMA_CIPHER_CTL_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
1130                                         return -1;
1131                                 }
1132                         }
1133                         return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
1134                 }
1135                 default:
1136                         return magma_cipher_ctl(ctx, type, arg, ptr);
1137                         break;
1138         }
1139 }
1140
1141 /* Set cipher parameters from ASN1 structure */
1142 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1143 {
1144     int len = 0;
1145     unsigned char *buf = NULL;
1146     unsigned char *p = NULL;
1147     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1148     GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
1149     ASN1_OCTET_STRING *os = NULL;
1150     if (!gcp) {
1151         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1152         return 0;
1153     }
1154     if (!ASN1_OCTET_STRING_set
1155         (gcp->iv, EVP_CIPHER_CTX_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx))) {
1156         GOST_CIPHER_PARAMS_free(gcp);
1157         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1158         return 0;
1159     }
1160     ASN1_OBJECT_free(gcp->enc_param_set);
1161     gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
1162
1163     len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
1164     p = buf = OPENSSL_malloc(len);
1165     if (!buf) {
1166         GOST_CIPHER_PARAMS_free(gcp);
1167         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1168         return 0;
1169     }
1170     i2d_GOST_CIPHER_PARAMS(gcp, &p);
1171     GOST_CIPHER_PARAMS_free(gcp);
1172
1173     os = ASN1_OCTET_STRING_new();
1174
1175     if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
1176         OPENSSL_free(buf);
1177         GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1178         return 0;
1179     }
1180     OPENSSL_free(buf);
1181
1182     ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
1183     return 1;
1184 }
1185
1186 /* Store parameters into ASN1 structure */
1187 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1188 {
1189     int len;
1190     GOST_CIPHER_PARAMS *gcp = NULL;
1191     unsigned char *p;
1192     struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1193     int nid;
1194
1195     if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
1196         return -1;
1197     }
1198
1199     p = params->value.sequence->data;
1200
1201     gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
1202                                  params->value.sequence->length);
1203
1204     len = gcp->iv->length;
1205     if (len != EVP_CIPHER_CTX_iv_length(ctx)) {
1206         GOST_CIPHER_PARAMS_free(gcp);
1207         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
1208         return -1;
1209     }
1210
1211     nid = OBJ_obj2nid(gcp->enc_param_set);
1212     if (nid == NID_undef) {
1213         GOST_CIPHER_PARAMS_free(gcp);
1214         GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
1215                 GOST_R_INVALID_CIPHER_PARAM_OID);
1216         return -1;
1217     }
1218
1219     if (!gost_cipher_set_param(c, nid)) {
1220         GOST_CIPHER_PARAMS_free(gcp);
1221         return -1;
1222     }
1223     /*XXX missing non-const accessor */
1224     memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), gcp->iv->data,
1225            EVP_CIPHER_CTX_iv_length(ctx));
1226
1227     GOST_CIPHER_PARAMS_free(gcp);
1228
1229     return 1;
1230 }
1231
1232 #define MAGMA_UKM_LEN 12
1233 static int magma_set_asn1_parameters (EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1234 {
1235   struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1236         c->key_meshing = 8192;
1237
1238         return gost2015_set_asn1_params(params, EVP_CIPHER_CTX_original_iv(ctx), 4,
1239                 c->kdf_seed);
1240 }
1241
1242 static int magma_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1243 {
1244   struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1245         unsigned char iv[16];
1246
1247         c->key_meshing = 8192;
1248
1249         if (gost2015_get_asn1_params(params, MAGMA_UKM_LEN, iv, 4, c->kdf_seed) < 0)
1250             return -1;
1251
1252         memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, sizeof(iv));
1253         memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, sizeof(iv));
1254         /* Key meshing 8 kb*/
1255         c->key_meshing = 8192;
1256
1257         return 1;
1258 }
1259
1260 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
1261 {
1262     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1263     memset(c->buffer, 0, sizeof(c->buffer));
1264     memset(c->partial_block, 0, sizeof(c->partial_block));
1265     c->count = 0;
1266     c->bytes_left = 0;
1267     c->key_meshing = 1;
1268     c->dgst_size = 4;
1269     gost_init(&(c->cctx), block);
1270     return 1;
1271 }
1272
1273 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
1274 {
1275     return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
1276 }
1277
1278 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
1279 {
1280     return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
1281 }
1282
1283 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
1284                            const unsigned char *data)
1285 {
1286     /*
1287      * We are using NULL for iv because CryptoPro doesn't interpret
1288      * internal state of MAC algorithm as iv during keymeshing (but does
1289      * initialize internal state from iv in key transport
1290      */
1291     assert(c->count % 8 == 0 && c->count <= 1024);
1292     if (c->key_meshing && c->count == 1024) {
1293         cryptopro_key_meshing(&(c->cctx), NULL);
1294     }
1295     mac_block(&(c->cctx), c->buffer, data);
1296     c->count = c->count % 1024 + 8;
1297 }
1298
1299 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
1300 {
1301     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1302     const unsigned char *p = data;
1303     size_t bytes = count;
1304     if (!(c->key_set)) {
1305         GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
1306         return 0;
1307     }
1308     if (c->bytes_left) {
1309         size_t i;
1310         for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
1311             c->partial_block[i] = *p;
1312         }
1313         if (i == 8) {
1314             mac_block_mesh(c, c->partial_block);
1315         } else {
1316             c->bytes_left = i;
1317             return 1;
1318         }
1319     }
1320     while (bytes > 8) {
1321         mac_block_mesh(c, p);
1322         p += 8;
1323         bytes -= 8;
1324     }
1325     if (bytes > 0) {
1326         memcpy(c->partial_block, p, bytes);
1327     }
1328     c->bytes_left = bytes;
1329     return 1;
1330 }
1331
1332 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
1333 {
1334     struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1335     if (!c->key_set) {
1336         GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
1337         return 0;
1338     }
1339     if (c->count == 0 && c->bytes_left) {
1340         unsigned char buffer[8];
1341         memset(buffer, 0, 8);
1342         gost_imit_update(ctx, buffer, 8);
1343     }
1344     if (c->bytes_left) {
1345         int i;
1346         for (i = c->bytes_left; i < 8; i++) {
1347             c->partial_block[i] = 0;
1348         }
1349         mac_block_mesh(c, c->partial_block);
1350     }
1351     get_mac(c->buffer, 8 * c->dgst_size, md);
1352     return 1;
1353 }
1354
1355 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
1356 {
1357     switch (type) {
1358     case EVP_MD_CTRL_KEY_LEN:
1359         *((unsigned int *)(ptr)) = 32;
1360         return 1;
1361     case EVP_MD_CTRL_SET_KEY:
1362         {
1363             struct ossl_gost_imit_ctx *gost_imit_ctx = EVP_MD_CTX_md_data(ctx);
1364
1365             if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
1366                 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
1367                 return 0;
1368             }
1369             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
1370
1371             if (arg == 0) {
1372                 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
1373                 if (key->mac_param_nid != NID_undef) {
1374                     const struct gost_cipher_info *param =
1375                         get_encryption_params(OBJ_nid2obj(key->mac_param_nid));
1376                     if (param == NULL) {
1377                         GOSTerr(GOST_F_GOST_IMIT_CTRL,
1378                                 GOST_R_INVALID_MAC_PARAMS);
1379                         return 0;
1380                     }
1381                     gost_init(&(gost_imit_ctx->cctx), param->sblock);
1382                 }
1383                 gost_key(&(gost_imit_ctx->cctx), key->key);
1384                 gost_imit_ctx->key_set = 1;
1385
1386                 return 1;
1387             } else if (arg == 32) {
1388                 gost_key(&(gost_imit_ctx->cctx), ptr);
1389                 gost_imit_ctx->key_set = 1;
1390                 return 1;
1391             }
1392             GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
1393             return 0;
1394         }
1395     case EVP_MD_CTRL_XOF_LEN:
1396         {
1397             struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1398             if (arg < 1 || arg > 8) {
1399                 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
1400                 return 0;
1401             }
1402             c->dgst_size = arg;
1403             return 1;
1404         }
1405
1406     default:
1407         return 0;
1408     }
1409 }
1410
1411 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1412 {
1413     if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
1414         memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
1415                sizeof(struct ossl_gost_imit_ctx));
1416     }
1417     return 1;
1418 }
1419
1420 /* Clean up imit ctx */
1421 int gost_imit_cleanup(EVP_MD_CTX *ctx)
1422 {
1423     memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
1424     return 1;
1425 }
1426 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */