1 /**********************************************************************
2 * gost_crypt.c - Initialize all ciphers *
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 *
8 * OpenSSL interface to GOST 28147-89 cipher functions *
9 * Requires OpenSSL 0.9.9 for compilation *
10 **********************************************************************/
13 #include <openssl/err.h>
14 #include <openssl/rand.h>
15 #include "e_gost_err.h"
17 #include "gost_gost2015.h"
19 #if !defined(CCGOST_DEBUG) && !defined(DEBUG)
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 static int gost_magma_mgm_cleanup(EVP_CIPHER_CTX *c);
47 /* set/get cipher parameters */
48 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
49 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
50 /* Control function */
51 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
53 static int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
54 const unsigned char *iv, int enc);
55 static int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
56 const unsigned char *iv, int enc);
57 static int gost_magma_cipher_init_mgm(EVP_CIPHER_CTX *ctx, const unsigned char *key,
58 const unsigned char *iv, int enc);
59 /* Handles block of data in CBC mode */
60 static int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
61 const unsigned char *in, size_t inl);
62 static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
63 const unsigned char *in, size_t inl);
65 static int magma_cipher_do_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, unsigned char *out,
66 const unsigned char *in, size_t inl);
67 static int gost_magma_cipher_do_mgm(EVP_CIPHER_CTX *ctx, unsigned char *out,
68 const unsigned char *in, size_t len);
69 /* set/get cipher parameters */
70 static int magma_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
71 static int magma_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
72 /* Control function */
73 static int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
74 static int magma_cipher_ctl_acpkm_omac(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
75 static int gost_magma_mgm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
78 * Single level template accessor.
79 * Note: that you cannot template 0 value.
81 #define TPL(st,field) ( \
82 ((st)->field) ?: TPL_VAL(st,field) \
85 #define TPL_VAL(st,field) ( \
86 ((st)->template ? (st)->template->field : 0) \
89 EVP_CIPHER *GOST_init_cipher(GOST_cipher *c)
94 /* Some sanity checking. */
95 int flags = c->flags | TPL_VAL(c, flags);
96 int block_size = TPL(c, block_size);
97 switch (flags & EVP_CIPH_MODE) {
98 case EVP_CIPH_CBC_MODE:
99 case EVP_CIPH_ECB_MODE:
100 case EVP_CIPH_WRAP_MODE:
101 OPENSSL_assert(block_size != 1);
102 OPENSSL_assert(!(flags & EVP_CIPH_NO_PADDING));
105 OPENSSL_assert(block_size == 1);
106 OPENSSL_assert(flags & EVP_CIPH_NO_PADDING);
110 OPENSSL_assert(flags & EVP_CIPH_CUSTOM_IV);
112 OPENSSL_assert(!(flags & EVP_CIPH_CUSTOM_IV));
115 if (!(cipher = EVP_CIPHER_meth_new(c->nid, block_size, TPL(c, key_len)))
116 || !EVP_CIPHER_meth_set_iv_length(cipher, TPL(c, iv_len))
117 || !EVP_CIPHER_meth_set_flags(cipher, flags)
118 || !EVP_CIPHER_meth_set_init(cipher, TPL(c, init))
119 || !EVP_CIPHER_meth_set_do_cipher(cipher, TPL(c, do_cipher))
120 || !EVP_CIPHER_meth_set_cleanup(cipher, TPL(c, cleanup))
121 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, TPL(c, ctx_size))
122 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, TPL(c, set_asn1_parameters))
123 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, TPL(c, get_asn1_parameters))
124 || !EVP_CIPHER_meth_set_ctrl(cipher, TPL(c, ctrl))) {
125 EVP_CIPHER_meth_free(cipher);
132 void GOST_deinit_cipher(GOST_cipher *c)
135 EVP_CIPHER_meth_free(c->cipher);
140 static GOST_cipher gost_template_cipher = {
144 .flags = EVP_CIPH_CUSTOM_IV |
146 EVP_CIPH_ALWAYS_CALL_INIT,
147 .cleanup = gost_cipher_cleanup,
148 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
149 .set_asn1_parameters = gost89_set_asn1_parameters,
150 .get_asn1_parameters = gost89_get_asn1_parameters,
151 .ctrl = gost_cipher_ctl,
154 GOST_cipher Gost28147_89_cipher = {
155 .nid = NID_id_Gost28147_89,
156 .template = &gost_template_cipher,
158 .flags = EVP_CIPH_CFB_MODE |
160 .init = gost_cipher_init,
161 .do_cipher = gost_cipher_do_cfb,
164 GOST_cipher Gost28147_89_cbc_cipher = {
165 .nid = NID_gost89_cbc,
166 .template = &gost_template_cipher,
167 .flags = EVP_CIPH_CBC_MODE,
168 .init = gost_cipher_init_cbc,
169 .do_cipher = gost_cipher_do_cbc,
172 GOST_cipher Gost28147_89_cnt_cipher = {
173 .nid = NID_gost89_cnt,
174 .template = &gost_template_cipher,
176 .flags = EVP_CIPH_OFB_MODE |
178 .init = gost_cipher_init_cpa,
179 .do_cipher = gost_cipher_do_cnt,
182 GOST_cipher Gost28147_89_cnt_12_cipher = {
183 .nid = NID_gost89_cnt_12,
184 .template = &gost_template_cipher,
186 .flags = EVP_CIPH_OFB_MODE |
188 .init = gost_cipher_init_cp_12,
189 .do_cipher = gost_cipher_do_cnt,
192 static GOST_cipher magma_template_cipher = {
196 .flags = EVP_CIPH_CUSTOM_IV |
198 EVP_CIPH_ALWAYS_CALL_INIT,
199 .cleanup = gost_cipher_cleanup,
200 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
201 .set_asn1_parameters = magma_set_asn1_parameters,
202 .get_asn1_parameters = magma_get_asn1_parameters,
203 .do_cipher = magma_cipher_do_ctr,
204 .ctrl = magma_cipher_ctl,
207 GOST_cipher magma_ctr_cipher = {
208 .nid = NID_magma_ctr,
209 .template = &magma_template_cipher,
212 .flags = EVP_CIPH_CTR_MODE |
214 .init = magma_cipher_init,
217 GOST_cipher magma_ctr_acpkm_cipher = {
218 .nid = NID_magma_ctr_acpkm,
219 .template = &magma_template_cipher,
222 .flags = EVP_CIPH_CTR_MODE |
224 .init = magma_cipher_init,
227 GOST_cipher magma_ctr_acpkm_omac_cipher = {
228 .nid = NID_magma_ctr_acpkm_omac,
229 .template = &magma_template_cipher,
232 .flags = EVP_CIPH_CTR_MODE |
233 EVP_CIPH_NO_PADDING |
234 EVP_CIPH_CUSTOM_COPY |
235 EVP_CIPH_FLAG_CUSTOM_CIPHER |
236 EVP_CIPH_FLAG_CIPHER_WITH_MAC,
237 .init = magma_cipher_init_ctr_acpkm_omac,
238 .do_cipher = magma_cipher_do_ctr_acpkm_omac,
239 .ctrl = magma_cipher_ctl_acpkm_omac,
242 GOST_cipher magma_mgm_cipher = {
243 .nid = NID_magma_mgm,
244 .template = &magma_template_cipher,
247 .flags = EVP_CIPH_NO_PADDING |
248 EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER |
249 EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER,
250 .init = gost_magma_cipher_init_mgm,
251 .do_cipher = gost_magma_cipher_do_mgm,
252 .ctrl = gost_magma_mgm_ctrl,
253 .cleanup = gost_magma_mgm_cleanup,
254 .ctx_size = sizeof(gost_mgm_ctx)
257 GOST_cipher magma_cbc_cipher = {
258 .nid = NID_magma_cbc,
259 .template = &gost_template_cipher,
260 .flags = EVP_CIPH_CBC_MODE,
261 .init = magma_cipher_init,
262 .do_cipher = magma_cipher_do_cbc,
265 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
266 /* Init functions which set specific parameters */
267 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
268 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx);
269 /* process block of data */
270 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
271 /* Return computed value */
272 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
274 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
275 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
276 /* Control function, knows how to set MAC key.*/
277 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
279 GOST_digest Gost28147_89_MAC_digest = {
280 .nid = NID_id_Gost28147_89_MAC,
282 .input_blocksize = 8,
283 .app_datasize = sizeof(struct ossl_gost_imit_ctx),
284 .flags = EVP_MD_FLAG_XOF,
285 .init = gost_imit_init_cpa,
286 .update = gost_imit_update,
287 .final = gost_imit_final,
288 .copy = gost_imit_copy,
289 .cleanup = gost_imit_cleanup,
290 .ctrl = gost_imit_ctrl,
293 GOST_digest Gost28147_89_mac_12_digest = {
294 .nid = NID_gost_mac_12,
296 .input_blocksize = 8,
297 .app_datasize = sizeof(struct ossl_gost_imit_ctx),
298 .flags = EVP_MD_FLAG_XOF,
299 .init = gost_imit_init_cp_12,
300 .update = gost_imit_update,
301 .final = gost_imit_final,
302 .copy = gost_imit_copy,
303 .cleanup = gost_imit_cleanup,
304 .ctrl = gost_imit_ctrl,
308 * Correspondence between gost parameter OIDs and substitution blocks
309 * NID field is filed by register_gost_NID function in engine.c
310 * upon engine initialization
313 static struct gost_cipher_info gost_cipher_list[] = {
320 * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
322 {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
324 {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
326 {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
328 {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
330 {NID_id_tc26_gost_28147_param_Z, &Gost28147_TC26ParamSetZ, 1},
331 {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
336 * get encryption parameters from crypto network settings FIXME For now we
337 * use environment var CRYPT_PARAMS as place to store these settings.
338 * Actually, it is better to use engine control command, read from
339 * configuration file to set them
341 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
344 struct gost_cipher_info *param;
346 const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
347 if (!params || !strlen(params)) {
349 for (i = 0; gost_cipher_list[i].nid != NID_undef; i++)
350 if (gost_cipher_list[i].nid == NID_id_tc26_gost_28147_param_Z)
351 return &gost_cipher_list[i];
352 return &gost_cipher_list[0];
355 nid = OBJ_txt2nid(params);
356 if (nid == NID_undef) {
357 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
358 GOST_R_INVALID_CIPHER_PARAM_OID);
359 ERR_add_error_data(3, "Unsupported CRYPT_PARAMS='",
360 params, "' specified in environment or in config");
364 nid = OBJ_obj2nid(obj);
366 for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
368 if (!param->sblock) {
369 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
375 /* Sets cipher param from paramset NID. */
376 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
378 const struct gost_cipher_info *param;
379 param = get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
383 c->paramNID = param->nid;
384 c->key_meshing = param->key_meshing;
386 gost_init(&(c->cctx), param->sblock);
390 /* Initializes EVP_CIPHER_CTX by paramset NID */
391 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
392 const unsigned char *key,
393 const unsigned char *iv, int enc,
394 int paramNID, int mode)
396 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
397 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
398 if (!gost_cipher_set_param(c, paramNID))
400 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
403 gost_key(&(c->cctx), key);
405 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
406 EVP_CIPHER_CTX_iv_length(ctx));
408 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
409 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
413 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
414 const unsigned char *key,
415 const unsigned char *iv,
416 gost_subst_block * block)
418 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
419 gost_init(&(c->cctx), block);
423 gost_key(&(c->cctx), key);
425 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
426 EVP_CIPHER_CTX_iv_length(ctx));
428 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
429 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
433 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
434 const unsigned char *iv, int enc)
436 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
439 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
440 const unsigned char *key,
441 const unsigned char *iv, int enc)
443 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
446 /* Initializes EVP_CIPHER_CTX with default values */
447 static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
448 const unsigned char *iv, int enc)
450 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
454 /* Initializes EVP_CIPHER_CTX with default values */
455 static int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
456 const unsigned char *iv, int enc)
458 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
462 /* Initializes EVP_CIPHER_CTX with default values */
463 static int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
464 const unsigned char *iv, int enc)
466 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
467 /* FIXME this is just initializtion check */
468 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
469 if (!gost_cipher_set_param(c, NID_id_tc26_gost_28147_param_Z))
471 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
474 if (init_zero_kdf_seed(c->kdf_seed) == 0)
480 magma_key(&(c->cctx), key);
481 magma_master_key(&(c->cctx), key);
484 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
485 EVP_CIPHER_CTX_iv_length(ctx));
487 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
488 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
490 if (EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm
491 || EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm_omac) {
492 c->key_meshing = 1024;
500 /* Initializes EVP_CIPHER_CTX with default values */
501 static int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
502 const unsigned char *iv, int enc)
505 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
506 unsigned char cipher_key[32];
507 c->omac_ctx = EVP_MD_CTX_new();
509 if (c->omac_ctx == NULL) {
510 GOSTerr(GOST_F_MAGMA_CIPHER_INIT_CTR_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
514 if (gost2015_acpkm_omac_init(NID_magma_mac, enc, key,
515 c->omac_ctx, cipher_key, c->kdf_seed) != 1) {
516 EVP_MD_CTX_free(c->omac_ctx);
521 return magma_cipher_init(ctx, cipher_key, iv, enc);
524 return magma_cipher_init(ctx, key, iv, enc);
527 void gost_magma_encrypt_wrap(unsigned char *in, unsigned char *out,
528 struct ossl_gost_cipher_ctx *c) {
532 for (i = 0; i < 8; i++) {
535 gostcrypt(&(c->cctx), b, d);
536 for (i = 0; i < 8; i++) {
541 /* ----------------------------------------------------------------------------------------------- */
542 /*! Функция реализует операцию умножения двух элементов конечного поля \f$ \mathbb F_{2^{64}}\f$,
543 порожденного неприводимым многочленом
544 \f$ f(x) = x^{64} + x^4 + x^3 + x + 1 \in \mathbb F_2[x]\f$. Для умножения используется
545 простейшая реализация, основанная на приведении по модулю после каждого шага алгоритма. */
546 /* ----------------------------------------------------------------------------------------------- */
547 static void gf64_mul (uint64_t *result, uint64_t *arg1, uint64_t *arg2)
550 register uint64_t t, X0;
560 t = BSWAP64(*(arg2));
565 for (i = 0; i < 63; i++) {
570 if (X0 & 0x8000000000000000) {
584 *(result) = BSWAP64(Z0);
590 static int gost_magma_cipher_init_mgm(EVP_CIPHER_CTX *ctx, const unsigned char *key,
591 const unsigned char *iv, int enc)
594 (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
600 bl = EVP_CIPHER_CTX_iv_length(ctx);
601 if (!gost_cipher_set_param(&mctx->ks.g_ks, NID_id_tc26_gost_28147_param_Z))
603 magma_key(&(mctx->ks.g_ks.cctx), key);
604 magma_master_key(&(mctx->ks.g_ks.cctx), key);
605 gost_mgm128_init(&mctx->mgm, &mctx->ks,
606 (block128_f) gost_magma_encrypt_wrap, gf64_mul, bl);
609 * If we have an iv can set it directly, otherwise use saved IV.
611 if (iv == NULL && mctx->iv_set)
614 if (gost_mgm128_setiv(&mctx->mgm, iv, mctx->ivlen) != 1)
620 /* If key set use IV, otherwise copy */
622 if (gost_mgm128_setiv(&mctx->mgm, iv, mctx->ivlen) != 1)
626 memcpy(mctx->iv, iv, mctx->ivlen);
633 * Wrapper around gostcrypt function from gost89.c which perform key meshing
636 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
638 struct ossl_gost_cipher_ctx *c = ctx;
639 assert(c->count % 8 == 0 && c->count <= 1024);
640 if (c->key_meshing && c->count == 1024) {
641 cryptopro_key_meshing(&(c->cctx), iv);
643 gostcrypt(&(c->cctx), iv, buf);
644 c->count = c->count % 1024 + 8;
647 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
649 struct ossl_gost_cipher_ctx *c = ctx;
651 unsigned char buf1[8];
652 assert(c->count % 8 == 0 && c->count <= 1024);
653 if (c->key_meshing && c->count == 1024) {
654 cryptopro_key_meshing(&(c->cctx), iv);
657 gostcrypt(&(c->cctx), iv, buf1);
661 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
663 buf1[0] = (unsigned char)(g & 0xff);
664 buf1[1] = (unsigned char)((g >> 8) & 0xff);
665 buf1[2] = (unsigned char)((g >> 16) & 0xff);
666 buf1[3] = (unsigned char)((g >> 24) & 0xff);
667 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
670 if (go > g) /* overflow */
672 buf1[4] = (unsigned char)(g & 0xff);
673 buf1[5] = (unsigned char)((g >> 8) & 0xff);
674 buf1[6] = (unsigned char)((g >> 16) & 0xff);
675 buf1[7] = (unsigned char)((g >> 24) & 0xff);
677 gostcrypt(&(c->cctx), buf1, buf);
678 c->count = c->count % 1024 + 8;
681 /* GOST encryption in CBC mode */
682 static int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
683 const unsigned char *in, size_t inl)
686 const unsigned char *in_ptr = in;
687 unsigned char *out_ptr = out;
689 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
690 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
691 if (EVP_CIPHER_CTX_encrypting(ctx)) {
694 for (i = 0; i < 8; i++) {
695 b[i] = iv[i] ^ in_ptr[i];
697 gostcrypt(&(c->cctx), b, out_ptr);
698 memcpy(iv, out_ptr, 8);
705 unsigned char tmpiv[8];
706 gostdecrypt(&(c->cctx), in_ptr, b);
707 memcpy(tmpiv, in_ptr, 8);
708 for (i = 0; i < 8; i++) {
709 out_ptr[i] = iv[i] ^ b[i];
711 memcpy(iv, tmpiv, 8);
720 /* MAGMA encryption in CBC mode */
721 static int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
722 const unsigned char *in, size_t inl)
726 const unsigned char *in_ptr = in;
727 unsigned char *out_ptr = out;
729 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
730 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
731 if (EVP_CIPHER_CTX_encrypting(ctx)) {
734 for (i = 0; i < 8; i++) {
735 b[7 - i] = iv[i] ^ in_ptr[i];
737 gostcrypt(&(c->cctx), b, d);
739 for (i = 0; i < 8; i++) {
740 out_ptr[7 - i] = d[i];
742 memcpy(iv, out_ptr, 8);
749 for (i = 0; i < 8; i++) {
750 d[7 - i] = in_ptr[i];
752 gostdecrypt(&(c->cctx), d, b);
753 memcpy(d, in_ptr, 8);
754 for (i = 0; i < 8; i++) {
755 out_ptr[i] = iv[i] ^ b[7 - i];
766 /* increment counter (64-bit int) by 1 */
767 static void ctr64_inc(unsigned char *counter)
769 inc_counter(counter, 8);
772 #define MAGMA_BLOCK_SIZE 8
773 #define MAGMA_BLOCK_MASK (MAGMA_BLOCK_SIZE - 1)
774 static inline void apply_acpkm_magma(struct ossl_gost_cipher_ctx *
775 ctx, unsigned int *num)
777 if (!ctx->key_meshing || (*num < ctx->key_meshing))
779 acpkm_magma_key_meshing(&ctx->cctx);
780 *num &= MAGMA_BLOCK_MASK;
783 /* MAGMA encryption in CTR mode */
784 static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
785 const unsigned char *in, size_t inl)
787 const unsigned char *in_ptr = in;
788 unsigned char *out_ptr = out;
790 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
791 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
792 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
793 unsigned int num = EVP_CIPHER_CTX_num(ctx);
794 size_t blocks, i, lasted = inl;
796 /* Process partial blocks */
797 while ((num & MAGMA_BLOCK_MASK) && lasted) {
798 *out_ptr++ = *in_ptr++ ^ buf[7 - (num & MAGMA_BLOCK_MASK)];
802 blocks = lasted / MAGMA_BLOCK_SIZE;
804 /* Process full blocks */
805 for (i = 0; i < blocks; i++) {
806 apply_acpkm_magma(c, &num);
807 for (j = 0; j < 8; j++) {
810 gostcrypt(&(c->cctx), b, buf);
811 for (j = 0; j < 8; j++) {
812 out_ptr[j] = buf[7 - j] ^ in_ptr[j];
815 c->count += MAGMA_BLOCK_SIZE;
816 in_ptr += MAGMA_BLOCK_SIZE;
817 out_ptr += MAGMA_BLOCK_SIZE;
818 num += MAGMA_BLOCK_SIZE;
819 lasted -= MAGMA_BLOCK_SIZE;
822 /* Process the rest of plaintext */
824 apply_acpkm_magma(c, &num);
825 for (j = 0; j < 8; j++) {
828 gostcrypt(&(c->cctx), b, buf);
830 for (i = 0; i < lasted; i++)
831 out_ptr[i] = buf[7 - i] ^ in_ptr[i];
836 EVP_CIPHER_CTX_set_num(ctx, num);
841 /* MAGMA encryption in CTR mode */
842 static int magma_cipher_do_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, unsigned char *out,
843 const unsigned char *in, size_t inl)
845 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
847 if (in == NULL && inl == 0) /* Final call */
848 return gost2015_final_call(ctx, c->omac_ctx, MAGMA_MAC_MAX_SIZE, c->tag, magma_cipher_do_ctr);
853 /* As in and out can be the same pointer, process unencrypted here */
854 if (EVP_CIPHER_CTX_encrypting(ctx))
855 EVP_DigestSignUpdate(c->omac_ctx, in, inl);
857 if (magma_cipher_do_ctr(ctx, out, in, inl) != inl)
860 /* As in and out can be the same pointer, process decrypted here */
861 if (!EVP_CIPHER_CTX_encrypting(ctx))
862 EVP_DigestSignUpdate(c->omac_ctx, out, inl);
867 static int gost_magma_cipher_do_mgm(EVP_CIPHER_CTX *ctx, unsigned char *out,
868 const unsigned char *in, size_t len)
871 (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
872 int enc = EVP_CIPHER_CTX_encrypting(ctx);
874 /* If not set up, return error */
875 if (!mctx->key_set) {
876 GOSTerr(GOST_F_GOST_MAGMA_CIPHER_DO_MGM,
882 GOSTerr(GOST_F_GOST_MAGMA_CIPHER_DO_MGM,
888 if (gost_mgm128_aad(&mctx->mgm, in, len))
891 if (gost_mgm128_encrypt(&mctx->mgm, in, out, len))
894 if (gost_mgm128_decrypt(&mctx->mgm, in, out, len))
900 if (mctx->taglen < 0)
902 if (gost_mgm128_finish(&mctx->mgm,
903 EVP_CIPHER_CTX_buf_noconst(ctx),
909 gost_mgm128_tag(&mctx->mgm, EVP_CIPHER_CTX_buf_noconst(ctx), 8);
911 /* Don't reuse the IV */
918 /* GOST encryption in CFB mode */
919 static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
920 const unsigned char *in, size_t inl)
922 const unsigned char *in_ptr = in;
923 unsigned char *out_ptr = out;
926 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
927 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
928 /* process partial block if any */
929 if (EVP_CIPHER_CTX_num(ctx)) {
930 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
931 j++, i++, in_ptr++, out_ptr++) {
932 if (!EVP_CIPHER_CTX_encrypting(ctx))
933 buf[j + 8] = *in_ptr;
934 *out_ptr = buf[j] ^ (*in_ptr);
935 if (EVP_CIPHER_CTX_encrypting(ctx))
936 buf[j + 8] = *out_ptr;
939 memcpy(iv, buf + 8, 8);
940 EVP_CIPHER_CTX_set_num(ctx, 0);
942 EVP_CIPHER_CTX_set_num(ctx, j);
947 for (; (inl - i) >= 8; i += 8, in_ptr += 8, out_ptr += 8) {
949 * block cipher current iv
951 gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
953 * xor next block of input text with it and output it
958 if (!EVP_CIPHER_CTX_encrypting(ctx))
959 memcpy(iv, in_ptr, 8);
960 for (j = 0; j < 8; j++) {
961 out_ptr[j] = buf[j] ^ in_ptr[j];
964 /* Next iv is next block of cipher text */
965 if (EVP_CIPHER_CTX_encrypting(ctx))
966 memcpy(iv, out_ptr, 8);
968 /* Process rest of buffer */
970 gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
971 if (!EVP_CIPHER_CTX_encrypting(ctx))
972 memcpy(buf + 8, in_ptr, inl - i);
973 for (j = 0; i < inl; j++, i++) {
974 out_ptr[j] = buf[j] ^ in_ptr[j];
976 EVP_CIPHER_CTX_set_num(ctx, j);
977 if (EVP_CIPHER_CTX_encrypting(ctx))
978 memcpy(buf + 8, out_ptr, j);
980 EVP_CIPHER_CTX_set_num(ctx, 0);
985 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
986 const unsigned char *in, size_t inl)
988 const unsigned char *in_ptr = in;
989 unsigned char *out_ptr = out;
992 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
993 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
994 /* process partial block if any */
995 if (EVP_CIPHER_CTX_num(ctx)) {
996 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
997 j++, i++, in_ptr++, out_ptr++) {
998 *out_ptr = buf[j] ^ (*in_ptr);
1001 EVP_CIPHER_CTX_set_num(ctx, 0);
1003 EVP_CIPHER_CTX_set_num(ctx, j);
1008 for (; (inl - i) >= 8; i += 8, in_ptr += 8, out_ptr += 8) {
1010 * block cipher current iv
1013 gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
1015 * xor next block of input text with it and output it
1020 for (j = 0; j < 8; j++) {
1021 out_ptr[j] = buf[j] ^ in_ptr[j];
1024 /* Process rest of buffer */
1026 gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
1027 for (j = 0; i < inl; j++, i++) {
1028 out_ptr[j] = buf[j] ^ in_ptr[j];
1030 EVP_CIPHER_CTX_set_num(ctx, j);
1032 EVP_CIPHER_CTX_set_num(ctx, 0);
1037 /* Cleaning up of EVP_CIPHER_CTX */
1038 static int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
1040 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1041 EVP_MD_CTX_free(c->omac_ctx);
1042 gost_destroy(&(c->cctx));
1043 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
1047 static int gost_magma_mgm_cleanup(EVP_CIPHER_CTX *c)
1049 gost_mgm_ctx *mctx =
1050 (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(c);
1053 gost_destroy(&mctx->ks.g_ks.cctx);
1054 OPENSSL_cleanse(&mctx->mgm, sizeof(mctx->mgm));
1055 EVP_CIPHER_CTX_set_app_data(c, NULL);
1059 static int gost_magma_mgm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1061 gost_mgm_ctx *mctx =
1062 (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(c);
1063 unsigned char *buf, *iv;
1068 ivlen = EVP_CIPHER_iv_length(EVP_CIPHER_CTX_cipher(c));
1069 iv = EVP_CIPHER_CTX_iv_noconst(c);
1072 mctx->ivlen = ivlen;
1075 mctx->tlstree_mode = TLSTREE_MODE_NONE;
1078 case EVP_CTRL_GET_IVLEN:
1079 *(int *)ptr = mctx->ivlen;
1082 case EVP_CTRL_AEAD_SET_IVLEN:
1085 if ((arg > EVP_MAX_IV_LENGTH) && (arg > mctx->ivlen)) {
1086 // TODO: Allocate memory for IV or set error
1092 case EVP_CTRL_AEAD_SET_TAG:
1093 buf = EVP_CIPHER_CTX_buf_noconst(c);
1094 enc = EVP_CIPHER_CTX_encrypting(c);
1095 if (arg <= 0 || arg != 8 || enc) {
1096 GOSTerr(GOST_F_GOST_MAGMA_MGM_CTRL,
1097 GOST_R_INVALID_TAG_LENGTH);
1100 memcpy(buf, ptr, arg);
1104 case EVP_CTRL_AEAD_GET_TAG:
1105 buf = EVP_CIPHER_CTX_buf_noconst(c);
1106 enc = EVP_CIPHER_CTX_encrypting(c);
1107 if (arg <= 0 || arg > 8 || !enc || mctx->taglen < 0) {
1108 GOSTerr(GOST_F_GOST_MAGMA_MGM_CTRL,
1109 GOST_R_INVALID_TAG_LENGTH);
1112 memcpy(ptr, buf, arg);
1115 case EVP_CTRL_SET_TLSTREE_PARAMS:
1116 if (strcmp((char *)ptr, "short") == 0)
1117 mctx->tlstree_mode = TLSTREE_MODE_S;
1118 else if (strcmp((char *)ptr, "long") == 0)
1119 mctx->tlstree_mode = TLSTREE_MODE_L;
1126 case EVP_CTRL_TLSTREE:
1128 unsigned char newkey[32];
1129 if (gost_tlstree(NID_magma_mgm,
1130 (const unsigned char *)mctx->ks.g_ks.cctx.master_key,
1131 newkey, (const unsigned char *)ptr, mctx->tlstree_mode)
1133 magma_key(&mctx->ks.g_ks.cctx, newkey);
1134 memset(newkey, 0, sizeof(newkey));
1144 /* Control function for gost cipher */
1145 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1148 case EVP_CTRL_RAND_KEY:
1151 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
1152 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
1157 case EVP_CTRL_PBE_PRF_NID:
1159 const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
1160 int nid = NID_id_tc26_hmac_gost_3411_2012_512;
1163 if (!strcmp("md_gost12_256", params))
1164 nid = NID_id_tc26_hmac_gost_3411_2012_256;
1165 else if (!strcmp("md_gost12_512", params))
1166 nid = NID_id_tc26_hmac_gost_3411_2012_512;
1167 else if (!strcmp("md_gost94", params))
1168 nid = NID_id_HMACGostR3411_94;
1170 *((int *)ptr) = nid;
1176 case EVP_CTRL_SET_SBOX:
1178 struct ossl_gost_cipher_ctx *c =
1179 EVP_CIPHER_CTX_get_cipher_data(ctx);
1188 if (c->count != 0) {
1192 nid = OBJ_txt2nid(ptr);
1193 if (nid == NID_undef) {
1197 cur_meshing = c->key_meshing;
1198 ret = gost_cipher_set_param(c, nid);
1199 c->key_meshing = cur_meshing;
1204 case EVP_CTRL_KEY_MESH:
1206 struct ossl_gost_cipher_ctx *c =
1207 EVP_CIPHER_CTX_get_cipher_data(ctx);
1213 if (c->count != 0) {
1217 c->key_meshing = arg;
1221 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
1227 /* Decrement 8-byte sequence if needed */
1228 int decrement_sequence(unsigned char *seq, int decrement) {
1229 if (decrement < 0 || decrement > 1)
1234 for (j = 7; j >= 0; j--)
1248 /* Control function for gost cipher */
1249 static int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1252 case EVP_CTRL_RAND_KEY:
1254 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
1255 GOSTerr(GOST_F_MAGMA_CIPHER_CTL, GOST_R_RNG_ERROR);
1259 case EVP_CTRL_KEY_MESH:
1261 struct ossl_gost_cipher_ctx *c =
1262 EVP_CIPHER_CTX_get_cipher_data(ctx);
1268 if (c->count != 0) {
1272 c->key_meshing = arg;
1275 case EVP_CTRL_TLSTREE:
1277 unsigned char newkey[32];
1278 int mode = EVP_CIPHER_CTX_mode(ctx);
1279 struct ossl_gost_cipher_ctx *ctr_ctx = NULL;
1282 unsigned char adjusted_iv[8];
1283 unsigned char seq[8];
1284 int j, carry, decrement_arg;
1285 if (mode != EVP_CIPH_CTR_MODE)
1288 ctr_ctx = (struct ossl_gost_cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
1289 c = &(ctr_ctx->cctx);
1292 * 'arg' parameter indicates what we should do with sequence value.
1294 * When function called, seq is incremented after MAC calculation.
1295 * In ETM mode, we use seq 'as is' in the ctrl-function (arg = 0)
1296 * Otherwise we have to decrease it in the implementation (arg = 1).
1298 memcpy(seq, ptr, 8);
1299 decrement_arg = arg;
1300 if(!decrement_sequence(seq, decrement_arg)) {
1301 GOSTerr(GOST_F_MAGMA_CIPHER_CTL, GOST_R_CTRL_CALL_FAILED);
1305 if (gost_tlstree(NID_magma_cbc,
1306 (const unsigned char *)c->master_key, newkey,
1307 (const unsigned char *)seq, TLSTREE_MODE_NONE)
1309 memset(adjusted_iv, 0, 8);
1310 memcpy(adjusted_iv, EVP_CIPHER_CTX_original_iv(ctx), 4);
1311 for (j = 3, carry = 0; j >= 0; j--)
1313 int adj_byte = adjusted_iv[j] + seq[j+4] + carry;
1314 carry = (adj_byte > 255) ? 1 : 0;
1315 adjusted_iv[j] = adj_byte & 0xFF;
1317 EVP_CIPHER_CTX_set_num(ctx, 0);
1318 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), adjusted_iv, 8);
1320 magma_key(c, newkey);
1326 GOSTerr(GOST_F_MAGMA_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
1332 static int magma_cipher_ctl_acpkm_omac(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1336 case EVP_CTRL_PROCESS_UNPROTECTED:
1338 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1339 STACK_OF(X509_ATTRIBUTE) *x = ptr;
1340 return gost2015_process_unprotected_attributes(x, arg, MAGMA_MAC_MAX_SIZE, c->tag);
1342 case EVP_CTRL_COPY: {
1343 EVP_CIPHER_CTX *out = ptr;
1344 struct ossl_gost_cipher_ctx *in_cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
1345 struct ossl_gost_cipher_ctx *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
1347 if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
1348 out_cctx->omac_ctx = EVP_MD_CTX_new();
1349 if (out_cctx->omac_ctx == NULL) {
1350 GOSTerr(GOST_F_MAGMA_CIPHER_CTL_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
1354 return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
1357 return magma_cipher_ctl(ctx, type, arg, ptr);
1362 /* Set cipher parameters from ASN1 structure */
1363 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1366 unsigned char *buf = NULL;
1367 unsigned char *p = NULL;
1368 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1369 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
1370 ASN1_OCTET_STRING *os = NULL;
1372 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1375 if (!ASN1_OCTET_STRING_set
1376 (gcp->iv, EVP_CIPHER_CTX_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx))) {
1377 GOST_CIPHER_PARAMS_free(gcp);
1378 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1381 ASN1_OBJECT_free(gcp->enc_param_set);
1382 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
1384 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
1385 p = buf = OPENSSL_malloc(len);
1387 GOST_CIPHER_PARAMS_free(gcp);
1388 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1391 i2d_GOST_CIPHER_PARAMS(gcp, &p);
1392 GOST_CIPHER_PARAMS_free(gcp);
1394 os = ASN1_OCTET_STRING_new();
1396 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
1398 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1403 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
1407 /* Store parameters into ASN1 structure */
1408 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1411 GOST_CIPHER_PARAMS *gcp = NULL;
1413 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1416 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
1420 p = params->value.sequence->data;
1422 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
1423 params->value.sequence->length);
1425 len = gcp->iv->length;
1426 if (len != EVP_CIPHER_CTX_iv_length(ctx)) {
1427 GOST_CIPHER_PARAMS_free(gcp);
1428 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
1432 nid = OBJ_obj2nid(gcp->enc_param_set);
1433 if (nid == NID_undef) {
1434 GOST_CIPHER_PARAMS_free(gcp);
1435 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
1436 GOST_R_INVALID_CIPHER_PARAM_OID);
1440 if (!gost_cipher_set_param(c, nid)) {
1441 GOST_CIPHER_PARAMS_free(gcp);
1444 /*XXX missing non-const accessor */
1445 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), gcp->iv->data,
1446 EVP_CIPHER_CTX_iv_length(ctx));
1448 GOST_CIPHER_PARAMS_free(gcp);
1453 #define MAGMA_UKM_LEN 12
1454 static int magma_set_asn1_parameters (EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1456 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1457 c->key_meshing = 8192;
1459 return gost2015_set_asn1_params(params, EVP_CIPHER_CTX_original_iv(ctx), 4,
1463 static int magma_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1465 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1466 unsigned char iv[16];
1468 c->key_meshing = 8192;
1470 if (gost2015_get_asn1_params(params, MAGMA_UKM_LEN, iv, 4, c->kdf_seed) < 0)
1473 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, sizeof(iv));
1474 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, sizeof(iv));
1475 /* Key meshing 8 kb*/
1476 c->key_meshing = 8192;
1481 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
1483 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1484 memset(c->buffer, 0, sizeof(c->buffer));
1485 memset(c->partial_block, 0, sizeof(c->partial_block));
1490 gost_init(&(c->cctx), block);
1494 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
1496 return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
1499 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
1501 return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
1504 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
1505 const unsigned char *data)
1508 * We are using NULL for iv because CryptoPro doesn't interpret
1509 * internal state of MAC algorithm as iv during keymeshing (but does
1510 * initialize internal state from iv in key transport
1512 assert(c->count % 8 == 0 && c->count <= 1024);
1513 if (c->key_meshing && c->count == 1024) {
1514 cryptopro_key_meshing(&(c->cctx), NULL);
1516 mac_block(&(c->cctx), c->buffer, data);
1517 c->count = c->count % 1024 + 8;
1520 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
1522 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1523 const unsigned char *p = data;
1524 size_t bytes = count;
1525 if (!(c->key_set)) {
1526 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
1529 if (c->bytes_left) {
1531 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
1532 c->partial_block[i] = *p;
1535 mac_block_mesh(c, c->partial_block);
1542 mac_block_mesh(c, p);
1547 memcpy(c->partial_block, p, bytes);
1549 c->bytes_left = bytes;
1553 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
1555 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1557 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
1560 if (c->count == 0 && c->bytes_left) {
1561 unsigned char buffer[8];
1562 memset(buffer, 0, 8);
1563 gost_imit_update(ctx, buffer, 8);
1565 if (c->bytes_left) {
1567 for (i = c->bytes_left; i < 8; i++) {
1568 c->partial_block[i] = 0;
1570 mac_block_mesh(c, c->partial_block);
1572 get_mac(c->buffer, 8 * c->dgst_size, md);
1576 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
1579 case EVP_MD_CTRL_KEY_LEN:
1580 *((unsigned int *)(ptr)) = 32;
1582 case EVP_MD_CTRL_SET_KEY:
1584 struct ossl_gost_imit_ctx *gost_imit_ctx = EVP_MD_CTX_md_data(ctx);
1586 if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
1587 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
1590 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
1593 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
1594 if (key->mac_param_nid != NID_undef) {
1595 const struct gost_cipher_info *param =
1596 get_encryption_params(OBJ_nid2obj(key->mac_param_nid));
1597 if (param == NULL) {
1598 GOSTerr(GOST_F_GOST_IMIT_CTRL,
1599 GOST_R_INVALID_MAC_PARAMS);
1602 gost_init(&(gost_imit_ctx->cctx), param->sblock);
1604 gost_key(&(gost_imit_ctx->cctx), key->key);
1605 gost_imit_ctx->key_set = 1;
1608 } else if (arg == 32) {
1609 gost_key(&(gost_imit_ctx->cctx), ptr);
1610 gost_imit_ctx->key_set = 1;
1613 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
1616 case EVP_MD_CTRL_XOF_LEN:
1618 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1619 if (arg < 1 || arg > 8) {
1620 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
1632 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1634 if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
1635 memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
1636 sizeof(struct ossl_gost_imit_ctx));
1641 /* Clean up imit ctx */
1642 static int gost_imit_cleanup(EVP_MD_CTX *ctx)
1644 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
1647 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */