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