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