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