1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * OpenSSL interface to GOST 28147-89 cipher functions *
7 * Requires OpenSSL 0.9.9 for compilation *
8 **********************************************************************/
11 #include <openssl/err.h>
12 #include <openssl/rand.h>
13 #include "e_gost_err.h"
15 #include "gost_gost2015.h"
17 #if !defined(CCGOST_DEBUG) && !defined(DEBUG)
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);
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);
60 static int magma_cipher_do_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, unsigned char *out,
61 const unsigned char *in, size_t inl);
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);
70 static EVP_CIPHER *_hidden_Gost28147_89_cipher = NULL;
71 const EVP_CIPHER *cipher_gost(void)
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,
83 EVP_CIPH_ALWAYS_CALL_INIT)
84 || !EVP_CIPHER_meth_set_init(_hidden_Gost28147_89_cipher,
86 || !EVP_CIPHER_meth_set_do_cipher(_hidden_Gost28147_89_cipher,
88 || !EVP_CIPHER_meth_set_cleanup(_hidden_Gost28147_89_cipher,
90 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_Gost28147_89_cipher,
92 ossl_gost_cipher_ctx))
94 !EVP_CIPHER_meth_set_set_asn1_params(_hidden_Gost28147_89_cipher,
95 gost89_set_asn1_parameters)
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,
101 EVP_CIPHER_meth_free(_hidden_Gost28147_89_cipher);
102 _hidden_Gost28147_89_cipher = NULL;
104 return _hidden_Gost28147_89_cipher;
107 static EVP_CIPHER *_hidden_Gost28147_89_cbc = NULL;
108 const EVP_CIPHER *cipher_gost_cbc(void)
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,
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,
124 || !EVP_CIPHER_meth_set_cleanup(_hidden_Gost28147_89_cbc,
126 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_Gost28147_89_cbc,
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,
135 EVP_CIPHER_meth_free(_hidden_Gost28147_89_cbc);
136 _hidden_Gost28147_89_cbc = NULL;
138 return _hidden_Gost28147_89_cbc;
141 static EVP_CIPHER *_hidden_gost89_cnt = NULL;
142 const EVP_CIPHER *cipher_gost_cpacnt(void)
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,
151 EVP_CIPH_NO_PADDING |
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,
159 || !EVP_CIPHER_meth_set_cleanup(_hidden_gost89_cnt,
161 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_gost89_cnt,
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;
172 return _hidden_gost89_cnt;
175 static EVP_CIPHER *_hidden_gost89_cnt_12 = NULL;
176 const EVP_CIPHER *cipher_gost_cpcnt_12(void)
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,
185 EVP_CIPH_NO_PADDING |
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,
193 || !EVP_CIPHER_meth_set_cleanup(_hidden_gost89_cnt_12,
195 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_gost89_cnt_12,
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,
204 EVP_CIPHER_meth_free(_hidden_gost89_cnt_12);
205 _hidden_gost89_cnt_12 = NULL;
207 return _hidden_gost89_cnt_12;
210 static EVP_CIPHER *_hidden_magma_ctr = NULL;
211 const EVP_CIPHER *cipher_magma_ctr(void)
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,
220 EVP_CIPH_NO_PADDING |
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,
227 || !EVP_CIPHER_meth_set_cleanup(_hidden_magma_ctr,
229 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_magma_ctr,
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;
240 return _hidden_magma_ctr;
243 static EVP_CIPHER *_hidden_magma_ctr_acpkm = NULL;
244 const EVP_CIPHER *cipher_magma_ctr_acpkm(void)
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,
253 EVP_CIPH_NO_PADDING |
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,
260 || !EVP_CIPHER_meth_set_cleanup(_hidden_magma_ctr_acpkm,
262 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_magma_ctr_acpkm,
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)
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;
274 return _hidden_magma_ctr_acpkm;
277 static EVP_CIPHER *_hidden_magma_ctr_acpkm_omac = NULL;
278 const EVP_CIPHER *cipher_magma_ctr_acpkm_omac(void)
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,
287 EVP_CIPH_NO_PADDING |
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,
299 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_magma_ctr_acpkm_omac,
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;
310 return _hidden_magma_ctr_acpkm_omac;
313 static EVP_CIPHER *_hidden_magma_cbc = NULL;
314 const EVP_CIPHER *cipher_magma_cbc(void)
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,
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,
329 || !EVP_CIPHER_meth_set_cleanup(_hidden_magma_cbc,
331 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_magma_cbc,
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;
338 return _hidden_magma_cbc;
341 void cipher_gost_destroy(void)
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;
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);
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);
375 static EVP_MD *_hidden_Gost28147_89_MAC_md = NULL;
376 static EVP_MD *_hidden_Gost28147_89_12_MAC_md = NULL;
378 EVP_MD *imit_gost_cpa(void)
380 if (_hidden_Gost28147_89_MAC_md == NULL) {
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);
398 _hidden_Gost28147_89_MAC_md = md;
400 return _hidden_Gost28147_89_MAC_md;
403 void imit_gost_cpa_destroy(void)
405 EVP_MD_meth_free(_hidden_Gost28147_89_MAC_md);
406 _hidden_Gost28147_89_MAC_md = NULL;
409 EVP_MD *imit_gost_cp_12(void)
411 if (_hidden_Gost28147_89_12_MAC_md == NULL) {
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);
429 _hidden_Gost28147_89_12_MAC_md = md;
431 return _hidden_Gost28147_89_12_MAC_md;
434 void imit_gost_cp_12_destroy(void)
436 EVP_MD_meth_free(_hidden_Gost28147_89_12_MAC_md);
437 _hidden_Gost28147_89_12_MAC_md = NULL;
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
446 struct gost_cipher_info gost_cipher_list[] = {
453 * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
455 {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
457 {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
459 {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
461 {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
463 {NID_id_tc26_gost_28147_param_Z, &Gost28147_TC26ParamSetZ, 1},
464 {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
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
474 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
477 struct gost_cipher_info *param;
479 const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
480 if (!params || !strlen(params)) {
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];
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 ERR_add_error_data(3, "Unsupported CRYPT_PARAMS='",
493 params, "' specified in environment or in config");
497 nid = OBJ_obj2nid(obj);
499 for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
501 if (!param->sblock) {
502 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
508 /* Sets cipher param from paramset NID. */
509 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
511 const struct gost_cipher_info *param;
512 param = get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
516 c->paramNID = param->nid;
517 c->key_meshing = param->key_meshing;
519 gost_init(&(c->cctx), param->sblock);
523 /* Initializes EVP_CIPHER_CTX by paramset NID */
524 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
525 const unsigned char *key,
526 const unsigned char *iv, int enc,
527 int paramNID, int mode)
529 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
530 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
531 if (!gost_cipher_set_param(c, paramNID))
533 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
536 gost_key(&(c->cctx), key);
538 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
539 EVP_CIPHER_CTX_iv_length(ctx));
541 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
542 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
546 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
547 const unsigned char *key,
548 const unsigned char *iv,
549 gost_subst_block * block)
551 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
552 gost_init(&(c->cctx), block);
556 gost_key(&(c->cctx), key);
558 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
559 EVP_CIPHER_CTX_iv_length(ctx));
561 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
562 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
566 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
567 const unsigned char *iv, int enc)
569 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
572 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
573 const unsigned char *key,
574 const unsigned char *iv, int enc)
576 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
579 /* Initializes EVP_CIPHER_CTX with default values */
580 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
581 const unsigned char *iv, int enc)
583 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
587 /* Initializes EVP_CIPHER_CTX with default values */
588 int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
589 const unsigned char *iv, int enc)
591 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
595 /* Initializes EVP_CIPHER_CTX with default values */
596 int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
597 const unsigned char *iv, int enc)
599 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
600 /* FIXME this is just initializtion check */
601 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
602 if (!gost_cipher_set_param(c, NID_id_tc26_gost_28147_param_Z))
604 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
607 if (init_zero_kdf_seed(c->kdf_seed) == 0)
613 magma_key(&(c->cctx), key);
615 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
616 EVP_CIPHER_CTX_iv_length(ctx));
618 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
619 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
621 if (EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm
622 || EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm_omac) {
623 c->key_meshing = 1024;
631 /* Initializes EVP_CIPHER_CTX with default values */
632 int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
633 const unsigned char *iv, int enc)
636 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
637 unsigned char cipher_key[32];
638 c->omac_ctx = EVP_MD_CTX_new();
640 if (c->omac_ctx == NULL) {
641 GOSTerr(GOST_F_MAGMA_CIPHER_INIT_CTR_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
645 if (gost2015_acpkm_omac_init(NID_magma_mac, enc, key,
646 c->omac_ctx, cipher_key, c->kdf_seed) != 1) {
647 EVP_MD_CTX_free(c->omac_ctx);
652 return magma_cipher_init(ctx, cipher_key, iv, enc);
655 return magma_cipher_init(ctx, key, iv, enc);
659 * Wrapper around gostcrypt function from gost89.c which perform key meshing
662 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
664 struct ossl_gost_cipher_ctx *c = ctx;
665 assert(c->count % 8 == 0 && c->count <= 1024);
666 if (c->key_meshing && c->count == 1024) {
667 cryptopro_key_meshing(&(c->cctx), iv);
669 gostcrypt(&(c->cctx), iv, buf);
670 c->count = c->count % 1024 + 8;
673 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
675 struct ossl_gost_cipher_ctx *c = ctx;
677 unsigned char buf1[8];
678 assert(c->count % 8 == 0 && c->count <= 1024);
679 if (c->key_meshing && c->count == 1024) {
680 cryptopro_key_meshing(&(c->cctx), iv);
683 gostcrypt(&(c->cctx), iv, buf1);
687 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
689 buf1[0] = (unsigned char)(g & 0xff);
690 buf1[1] = (unsigned char)((g >> 8) & 0xff);
691 buf1[2] = (unsigned char)((g >> 16) & 0xff);
692 buf1[3] = (unsigned char)((g >> 24) & 0xff);
693 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
696 if (go > g) /* overflow */
698 buf1[4] = (unsigned char)(g & 0xff);
699 buf1[5] = (unsigned char)((g >> 8) & 0xff);
700 buf1[6] = (unsigned char)((g >> 16) & 0xff);
701 buf1[7] = (unsigned char)((g >> 24) & 0xff);
703 gostcrypt(&(c->cctx), buf1, buf);
704 c->count = c->count % 1024 + 8;
707 /* GOST encryption in CBC mode */
708 int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
709 const unsigned char *in, size_t inl)
712 const unsigned char *in_ptr = in;
713 unsigned char *out_ptr = out;
715 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
716 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
717 if (EVP_CIPHER_CTX_encrypting(ctx)) {
720 for (i = 0; i < 8; i++) {
721 b[i] = iv[i] ^ in_ptr[i];
723 gostcrypt(&(c->cctx), b, out_ptr);
724 memcpy(iv, out_ptr, 8);
731 gostdecrypt(&(c->cctx), in_ptr, b);
732 for (i = 0; i < 8; i++) {
733 out_ptr[i] = iv[i] ^ b[i];
735 memcpy(iv, in_ptr, 8);
744 /* MAGMA encryption in CBC mode */
745 int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
746 const unsigned char *in, size_t inl)
750 const unsigned char *in_ptr = in;
751 unsigned char *out_ptr = out;
753 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
754 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
755 if (EVP_CIPHER_CTX_encrypting(ctx)) {
758 for (i = 0; i < 8; i++) {
759 b[7 - i] = iv[i] ^ in_ptr[i];
761 gostcrypt(&(c->cctx), b, d);
763 for (i = 0; i < 8; i++) {
764 out_ptr[7 - i] = d[i];
766 memcpy(iv, out_ptr, 8);
773 for (i = 0; i < 8; i++) {
774 d[7 - i] = in_ptr[i];
776 gostdecrypt(&(c->cctx), d, b);
777 memcpy(d, in_ptr, 8);
778 for (i = 0; i < 8; i++) {
779 out_ptr[i] = iv[i] ^ b[7 - i];
790 /* increment counter (64-bit int) by 1 */
791 static void ctr64_inc(unsigned char *counter)
793 inc_counter(counter, 8);
796 /* MAGMA encryption in CTR mode */
797 static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
798 const unsigned char *in, size_t inl)
800 const unsigned char *in_ptr = in;
801 unsigned char *out_ptr = out;
804 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
805 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
806 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
808 /* Process partial blocks */
809 if (EVP_CIPHER_CTX_num(ctx)) {
810 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
811 j++, i++, in_ptr++, out_ptr++) {
812 *out_ptr = buf[7 - j] ^ (*in_ptr);
815 EVP_CIPHER_CTX_set_num(ctx, 0);
817 EVP_CIPHER_CTX_set_num(ctx, j);
822 /* Process full blocks */
823 for (; i + 8 <= inl; i += 8, in_ptr += 8, out_ptr += 8) {
824 for (j = 0; j < 8; j++) {
827 gostcrypt(&(c->cctx), b, buf);
828 for (j = 0; j < 8; j++) {
829 out_ptr[j] = buf[7 - j] ^ in_ptr[j];
833 if (c->key_meshing && (c->count % c->key_meshing == 0))
834 acpkm_magma_key_meshing(&(c->cctx));
837 /* Process the rest of plaintext */
839 for (j = 0; j < 8; j++) {
842 gostcrypt(&(c->cctx), b, buf);
844 for (j = 0; i < inl; j++, i++) {
845 out_ptr[j] = buf[7 - j] ^ in_ptr[j];
850 if (c->key_meshing && (c->count % c->key_meshing == 0))
851 acpkm_magma_key_meshing(&(c->cctx));
853 EVP_CIPHER_CTX_set_num(ctx, j);
855 EVP_CIPHER_CTX_set_num(ctx, 0);
861 /* MAGMA encryption in CTR mode */
862 static int magma_cipher_do_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, unsigned char *out,
863 const unsigned char *in, size_t inl)
865 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
867 if (in == NULL && inl == 0) /* Final call */
868 return gost2015_final_call(ctx, c->omac_ctx, MAGMA_MAC_MAX_SIZE, c->tag, magma_cipher_do_ctr);
873 /* As in and out can be the same pointer, process unencrypted here */
874 if (EVP_CIPHER_CTX_encrypting(ctx))
875 EVP_DigestSignUpdate(c->omac_ctx, in, inl);
877 if (magma_cipher_do_ctr(ctx, out, in, inl) != inl)
880 /* As in and out can be the same pointer, process decrypted here */
881 if (!EVP_CIPHER_CTX_encrypting(ctx))
882 EVP_DigestSignUpdate(c->omac_ctx, out, inl);
886 /* GOST encryption in CFB mode */
887 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
888 const unsigned char *in, size_t inl)
890 const unsigned char *in_ptr = in;
891 unsigned char *out_ptr = out;
894 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
895 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
896 /* process partial block if any */
897 if (EVP_CIPHER_CTX_num(ctx)) {
898 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
899 j++, i++, in_ptr++, out_ptr++) {
900 if (!EVP_CIPHER_CTX_encrypting(ctx))
901 buf[j + 8] = *in_ptr;
902 *out_ptr = buf[j] ^ (*in_ptr);
903 if (EVP_CIPHER_CTX_encrypting(ctx))
904 buf[j + 8] = *out_ptr;
907 memcpy(iv, buf + 8, 8);
908 EVP_CIPHER_CTX_set_num(ctx, 0);
910 EVP_CIPHER_CTX_set_num(ctx, j);
915 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
917 * block cipher current iv
919 gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
921 * xor next block of input text with it and output it
926 if (!EVP_CIPHER_CTX_encrypting(ctx))
927 memcpy(iv, in_ptr, 8);
928 for (j = 0; j < 8; j++) {
929 out_ptr[j] = buf[j] ^ in_ptr[j];
932 /* Next iv is next block of cipher text */
933 if (EVP_CIPHER_CTX_encrypting(ctx))
934 memcpy(iv, out_ptr, 8);
936 /* Process rest of buffer */
938 gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
939 if (!EVP_CIPHER_CTX_encrypting(ctx))
940 memcpy(buf + 8, in_ptr, inl - i);
941 for (j = 0; i < inl; j++, i++) {
942 out_ptr[j] = buf[j] ^ in_ptr[j];
944 EVP_CIPHER_CTX_set_num(ctx, j);
945 if (EVP_CIPHER_CTX_encrypting(ctx))
946 memcpy(buf + 8, out_ptr, j);
948 EVP_CIPHER_CTX_set_num(ctx, 0);
953 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
954 const unsigned char *in, size_t inl)
956 const unsigned char *in_ptr = in;
957 unsigned char *out_ptr = out;
960 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
961 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
962 /* process partial block if any */
963 if (EVP_CIPHER_CTX_num(ctx)) {
964 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
965 j++, i++, in_ptr++, out_ptr++) {
966 *out_ptr = buf[j] ^ (*in_ptr);
969 EVP_CIPHER_CTX_set_num(ctx, 0);
971 EVP_CIPHER_CTX_set_num(ctx, j);
976 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
978 * block cipher current iv
981 gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
983 * xor next block of input text with it and output it
988 for (j = 0; j < 8; j++) {
989 out_ptr[j] = buf[j] ^ in_ptr[j];
992 /* Process rest of buffer */
994 gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
995 for (j = 0; i < inl; j++, i++) {
996 out_ptr[j] = buf[j] ^ in_ptr[j];
998 EVP_CIPHER_CTX_set_num(ctx, j);
1000 EVP_CIPHER_CTX_set_num(ctx, 0);
1005 /* Cleaning up of EVP_CIPHER_CTX */
1006 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
1008 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1009 EVP_MD_CTX_free(c->omac_ctx);
1010 gost_destroy(&(c->cctx));
1011 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
1015 /* Control function for gost cipher */
1016 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1019 case EVP_CTRL_RAND_KEY:
1022 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
1023 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
1028 case EVP_CTRL_PBE_PRF_NID:
1030 const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
1031 int nid = NID_id_tc26_hmac_gost_3411_2012_512;
1034 if (!strcmp("md_gost12_256", params))
1035 nid = NID_id_tc26_hmac_gost_3411_2012_256;
1036 else if (!strcmp("md_gost12_512", params))
1037 nid = NID_id_tc26_hmac_gost_3411_2012_512;
1038 else if (!strcmp("md_gost94", params))
1039 nid = NID_id_HMACGostR3411_94;
1041 *((int *)ptr) = nid;
1047 case EVP_CTRL_SET_SBOX:
1049 struct ossl_gost_cipher_ctx *c =
1050 EVP_CIPHER_CTX_get_cipher_data(ctx);
1059 if (c->count != 0) {
1063 nid = OBJ_txt2nid(ptr);
1064 if (nid == NID_undef) {
1068 cur_meshing = c->key_meshing;
1069 ret = gost_cipher_set_param(c, nid);
1070 c->key_meshing = cur_meshing;
1075 case EVP_CTRL_KEY_MESH:
1077 struct ossl_gost_cipher_ctx *c =
1078 EVP_CIPHER_CTX_get_cipher_data(ctx);
1084 if (c->count != 0) {
1088 c->key_meshing = arg;
1092 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
1098 /* Control function for gost cipher */
1099 int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1102 case EVP_CTRL_RAND_KEY:
1104 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
1105 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
1109 case EVP_CTRL_KEY_MESH:
1111 struct ossl_gost_cipher_ctx *c =
1112 EVP_CIPHER_CTX_get_cipher_data(ctx);
1118 if (c->count != 0) {
1122 c->key_meshing = arg;
1126 GOSTerr(GOST_F_MAGMA_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
1132 static int magma_cipher_ctl_acpkm_omac(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1136 case EVP_CTRL_PROCESS_UNPROTECTED:
1138 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1139 STACK_OF(X509_ATTRIBUTE) *x = ptr;
1140 return gost2015_process_unprotected_attributes(x, arg, MAGMA_MAC_MAX_SIZE, c->tag);
1142 case EVP_CTRL_COPY: {
1143 EVP_CIPHER_CTX *out = ptr;
1144 struct ossl_gost_cipher_ctx *in_cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
1145 struct ossl_gost_cipher_ctx *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
1147 if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
1148 out_cctx->omac_ctx = EVP_MD_CTX_new();
1149 if (out_cctx->omac_ctx == NULL) {
1150 GOSTerr(GOST_F_MAGMA_CIPHER_CTL_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
1154 return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
1157 return magma_cipher_ctl(ctx, type, arg, ptr);
1162 /* Set cipher parameters from ASN1 structure */
1163 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1166 unsigned char *buf = NULL;
1167 unsigned char *p = NULL;
1168 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1169 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
1170 ASN1_OCTET_STRING *os = NULL;
1172 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1175 if (!ASN1_OCTET_STRING_set
1176 (gcp->iv, EVP_CIPHER_CTX_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx))) {
1177 GOST_CIPHER_PARAMS_free(gcp);
1178 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1181 ASN1_OBJECT_free(gcp->enc_param_set);
1182 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
1184 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
1185 p = buf = OPENSSL_malloc(len);
1187 GOST_CIPHER_PARAMS_free(gcp);
1188 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1191 i2d_GOST_CIPHER_PARAMS(gcp, &p);
1192 GOST_CIPHER_PARAMS_free(gcp);
1194 os = ASN1_OCTET_STRING_new();
1196 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
1198 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1203 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
1207 /* Store parameters into ASN1 structure */
1208 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1211 GOST_CIPHER_PARAMS *gcp = NULL;
1213 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1216 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
1220 p = params->value.sequence->data;
1222 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
1223 params->value.sequence->length);
1225 len = gcp->iv->length;
1226 if (len != EVP_CIPHER_CTX_iv_length(ctx)) {
1227 GOST_CIPHER_PARAMS_free(gcp);
1228 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
1232 nid = OBJ_obj2nid(gcp->enc_param_set);
1233 if (nid == NID_undef) {
1234 GOST_CIPHER_PARAMS_free(gcp);
1235 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
1236 GOST_R_INVALID_CIPHER_PARAM_OID);
1240 if (!gost_cipher_set_param(c, nid)) {
1241 GOST_CIPHER_PARAMS_free(gcp);
1244 /*XXX missing non-const accessor */
1245 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), gcp->iv->data,
1246 EVP_CIPHER_CTX_iv_length(ctx));
1248 GOST_CIPHER_PARAMS_free(gcp);
1253 #define MAGMA_UKM_LEN 12
1254 static int magma_set_asn1_parameters (EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1256 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1257 c->key_meshing = 8192;
1259 return gost2015_set_asn1_params(params, EVP_CIPHER_CTX_original_iv(ctx), 4,
1263 static int magma_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1265 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1266 unsigned char iv[16];
1268 c->key_meshing = 8192;
1270 if (gost2015_get_asn1_params(params, MAGMA_UKM_LEN, iv, 4, c->kdf_seed) < 0)
1273 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, sizeof(iv));
1274 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, sizeof(iv));
1275 /* Key meshing 8 kb*/
1276 c->key_meshing = 8192;
1281 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
1283 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1284 memset(c->buffer, 0, sizeof(c->buffer));
1285 memset(c->partial_block, 0, sizeof(c->partial_block));
1290 gost_init(&(c->cctx), block);
1294 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
1296 return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
1299 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
1301 return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
1304 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
1305 const unsigned char *data)
1308 * We are using NULL for iv because CryptoPro doesn't interpret
1309 * internal state of MAC algorithm as iv during keymeshing (but does
1310 * initialize internal state from iv in key transport
1312 assert(c->count % 8 == 0 && c->count <= 1024);
1313 if (c->key_meshing && c->count == 1024) {
1314 cryptopro_key_meshing(&(c->cctx), NULL);
1316 mac_block(&(c->cctx), c->buffer, data);
1317 c->count = c->count % 1024 + 8;
1320 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
1322 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1323 const unsigned char *p = data;
1324 size_t bytes = count;
1325 if (!(c->key_set)) {
1326 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
1329 if (c->bytes_left) {
1331 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
1332 c->partial_block[i] = *p;
1335 mac_block_mesh(c, c->partial_block);
1342 mac_block_mesh(c, p);
1347 memcpy(c->partial_block, p, bytes);
1349 c->bytes_left = bytes;
1353 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
1355 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1357 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
1360 if (c->count == 0 && c->bytes_left) {
1361 unsigned char buffer[8];
1362 memset(buffer, 0, 8);
1363 gost_imit_update(ctx, buffer, 8);
1365 if (c->bytes_left) {
1367 for (i = c->bytes_left; i < 8; i++) {
1368 c->partial_block[i] = 0;
1370 mac_block_mesh(c, c->partial_block);
1372 get_mac(c->buffer, 8 * c->dgst_size, md);
1376 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
1379 case EVP_MD_CTRL_KEY_LEN:
1380 *((unsigned int *)(ptr)) = 32;
1382 case EVP_MD_CTRL_SET_KEY:
1384 struct ossl_gost_imit_ctx *gost_imit_ctx = EVP_MD_CTX_md_data(ctx);
1386 if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
1387 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
1390 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
1393 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
1394 if (key->mac_param_nid != NID_undef) {
1395 const struct gost_cipher_info *param =
1396 get_encryption_params(OBJ_nid2obj(key->mac_param_nid));
1397 if (param == NULL) {
1398 GOSTerr(GOST_F_GOST_IMIT_CTRL,
1399 GOST_R_INVALID_MAC_PARAMS);
1402 gost_init(&(gost_imit_ctx->cctx), param->sblock);
1404 gost_key(&(gost_imit_ctx->cctx), key->key);
1405 gost_imit_ctx->key_set = 1;
1408 } else if (arg == 32) {
1409 gost_key(&(gost_imit_ctx->cctx), ptr);
1410 gost_imit_ctx->key_set = 1;
1413 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
1416 case EVP_MD_CTRL_XOF_LEN:
1418 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1419 if (arg < 1 || arg > 8) {
1420 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
1432 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1434 if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
1435 memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
1436 sizeof(struct ossl_gost_imit_ctx));
1441 /* Clean up imit ctx */
1442 int gost_imit_cleanup(EVP_MD_CTX *ctx)
1444 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));