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);
482 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
483 EVP_CIPHER_CTX_iv_length(ctx));
485 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
486 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
488 if (EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm
489 || EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm_omac) {
490 c->key_meshing = 1024;
498 /* Initializes EVP_CIPHER_CTX with default values */
499 static int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
500 const unsigned char *iv, int enc)
503 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
504 unsigned char cipher_key[32];
505 c->omac_ctx = EVP_MD_CTX_new();
507 if (c->omac_ctx == NULL) {
508 GOSTerr(GOST_F_MAGMA_CIPHER_INIT_CTR_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
512 if (gost2015_acpkm_omac_init(NID_magma_mac, enc, key,
513 c->omac_ctx, cipher_key, c->kdf_seed) != 1) {
514 EVP_MD_CTX_free(c->omac_ctx);
519 return magma_cipher_init(ctx, cipher_key, iv, enc);
522 return magma_cipher_init(ctx, key, iv, enc);
525 void gost_magma_encrypt_wrap(unsigned char *in, unsigned char *out,
526 struct ossl_gost_cipher_ctx *c) {
530 for (i = 0; i < 8; i++) {
533 gostcrypt(&(c->cctx), b, d);
534 for (i = 0; i < 8; i++) {
539 /* ----------------------------------------------------------------------------------------------- */
540 /*! Функция реализует операцию умножения двух элементов конечного поля \f$ \mathbb F_{2^{64}}\f$,
541 порожденного неприводимым многочленом
542 \f$ f(x) = x^{64} + x^4 + x^3 + x + 1 \in \mathbb F_2[x]\f$. Для умножения используется
543 простейшая реализация, основанная на приведении по модулю после каждого шага алгоритма. */
544 /* ----------------------------------------------------------------------------------------------- */
545 static void gf64_mul (uint64_t *result, uint64_t *arg1, uint64_t *arg2)
548 register uint64_t t, X0;
558 t = BSWAP64(*(arg2));
563 for (i = 0; i < 63; i++) {
568 if (X0 & 0x8000000000000000) {
582 *(result) = BSWAP64(Z0);
588 static int gost_magma_cipher_init_mgm(EVP_CIPHER_CTX *ctx, const unsigned char *key,
589 const unsigned char *iv, int enc)
592 (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
598 bl = EVP_CIPHER_CTX_iv_length(ctx);
599 if (!gost_cipher_set_param(&mctx->ks.g_ks, NID_id_tc26_gost_28147_param_Z))
601 magma_key(&(mctx->ks.g_ks.cctx), key);
602 gost_mgm128_init(&mctx->mgm, &mctx->ks,
603 (block128_f) gost_magma_encrypt_wrap, gf64_mul, bl);
606 * If we have an iv can set it directly, otherwise use saved IV.
608 if (iv == NULL && mctx->iv_set)
611 if (gost_mgm128_setiv(&mctx->mgm, iv, mctx->ivlen) != 1)
617 /* If key set use IV, otherwise copy */
619 if (gost_mgm128_setiv(&mctx->mgm, iv, mctx->ivlen) != 1)
623 memcpy(mctx->iv, iv, mctx->ivlen);
630 * Wrapper around gostcrypt function from gost89.c which perform key meshing
633 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
635 struct ossl_gost_cipher_ctx *c = ctx;
636 assert(c->count % 8 == 0 && c->count <= 1024);
637 if (c->key_meshing && c->count == 1024) {
638 cryptopro_key_meshing(&(c->cctx), iv);
640 gostcrypt(&(c->cctx), iv, buf);
641 c->count = c->count % 1024 + 8;
644 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
646 struct ossl_gost_cipher_ctx *c = ctx;
648 unsigned char buf1[8];
649 assert(c->count % 8 == 0 && c->count <= 1024);
650 if (c->key_meshing && c->count == 1024) {
651 cryptopro_key_meshing(&(c->cctx), iv);
654 gostcrypt(&(c->cctx), iv, buf1);
658 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
660 buf1[0] = (unsigned char)(g & 0xff);
661 buf1[1] = (unsigned char)((g >> 8) & 0xff);
662 buf1[2] = (unsigned char)((g >> 16) & 0xff);
663 buf1[3] = (unsigned char)((g >> 24) & 0xff);
664 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
667 if (go > g) /* overflow */
669 buf1[4] = (unsigned char)(g & 0xff);
670 buf1[5] = (unsigned char)((g >> 8) & 0xff);
671 buf1[6] = (unsigned char)((g >> 16) & 0xff);
672 buf1[7] = (unsigned char)((g >> 24) & 0xff);
674 gostcrypt(&(c->cctx), buf1, buf);
675 c->count = c->count % 1024 + 8;
678 /* GOST encryption in CBC mode */
679 static int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
680 const unsigned char *in, size_t inl)
683 const unsigned char *in_ptr = in;
684 unsigned char *out_ptr = out;
686 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
687 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
688 if (EVP_CIPHER_CTX_encrypting(ctx)) {
691 for (i = 0; i < 8; i++) {
692 b[i] = iv[i] ^ in_ptr[i];
694 gostcrypt(&(c->cctx), b, out_ptr);
695 memcpy(iv, out_ptr, 8);
702 unsigned char tmpiv[8];
703 gostdecrypt(&(c->cctx), in_ptr, b);
704 memcpy(tmpiv, in_ptr, 8);
705 for (i = 0; i < 8; i++) {
706 out_ptr[i] = iv[i] ^ b[i];
708 memcpy(iv, tmpiv, 8);
717 /* MAGMA encryption in CBC mode */
718 static int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
719 const unsigned char *in, size_t inl)
723 const unsigned char *in_ptr = in;
724 unsigned char *out_ptr = out;
726 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
727 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
728 if (EVP_CIPHER_CTX_encrypting(ctx)) {
731 for (i = 0; i < 8; i++) {
732 b[7 - i] = iv[i] ^ in_ptr[i];
734 gostcrypt(&(c->cctx), b, d);
736 for (i = 0; i < 8; i++) {
737 out_ptr[7 - i] = d[i];
739 memcpy(iv, out_ptr, 8);
746 for (i = 0; i < 8; i++) {
747 d[7 - i] = in_ptr[i];
749 gostdecrypt(&(c->cctx), d, b);
750 memcpy(d, in_ptr, 8);
751 for (i = 0; i < 8; i++) {
752 out_ptr[i] = iv[i] ^ b[7 - i];
763 /* increment counter (64-bit int) by 1 */
764 static void ctr64_inc(unsigned char *counter)
766 inc_counter(counter, 8);
769 #define MAGMA_BLOCK_SIZE 8
770 #define MAGMA_BLOCK_MASK (MAGMA_BLOCK_SIZE - 1)
771 static inline void apply_acpkm_magma(struct ossl_gost_cipher_ctx *
772 ctx, unsigned int *num)
774 if (!ctx->key_meshing || (*num < ctx->key_meshing))
776 acpkm_magma_key_meshing(&ctx->cctx);
777 *num &= MAGMA_BLOCK_MASK;
780 /* MAGMA encryption in CTR mode */
781 static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
782 const unsigned char *in, size_t inl)
784 const unsigned char *in_ptr = in;
785 unsigned char *out_ptr = out;
787 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
788 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
789 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
790 unsigned int num = EVP_CIPHER_CTX_num(ctx);
791 size_t blocks, i, lasted = inl;
793 /* Process partial blocks */
794 while ((num & MAGMA_BLOCK_MASK) && lasted) {
795 *out_ptr++ = *in_ptr++ ^ buf[7 - (num & MAGMA_BLOCK_MASK)];
799 blocks = lasted / MAGMA_BLOCK_SIZE;
801 /* Process full blocks */
802 for (i = 0; i < blocks; i++) {
803 apply_acpkm_magma(c, &num);
804 for (j = 0; j < 8; j++) {
807 gostcrypt(&(c->cctx), b, buf);
808 for (j = 0; j < 8; j++) {
809 out_ptr[j] = buf[7 - j] ^ in_ptr[j];
812 c->count += MAGMA_BLOCK_SIZE;
813 in_ptr += MAGMA_BLOCK_SIZE;
814 out_ptr += MAGMA_BLOCK_SIZE;
815 num += MAGMA_BLOCK_SIZE;
816 lasted -= MAGMA_BLOCK_SIZE;
819 /* Process the rest of plaintext */
821 apply_acpkm_magma(c, &num);
822 for (j = 0; j < 8; j++) {
825 gostcrypt(&(c->cctx), b, buf);
827 for (i = 0; i < lasted; i++)
828 out_ptr[i] = buf[7 - i] ^ in_ptr[i];
833 EVP_CIPHER_CTX_set_num(ctx, num);
838 /* MAGMA encryption in CTR mode */
839 static int magma_cipher_do_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, unsigned char *out,
840 const unsigned char *in, size_t inl)
842 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
844 if (in == NULL && inl == 0) /* Final call */
845 return gost2015_final_call(ctx, c->omac_ctx, MAGMA_MAC_MAX_SIZE, c->tag, magma_cipher_do_ctr);
850 /* As in and out can be the same pointer, process unencrypted here */
851 if (EVP_CIPHER_CTX_encrypting(ctx))
852 EVP_DigestSignUpdate(c->omac_ctx, in, inl);
854 if (magma_cipher_do_ctr(ctx, out, in, inl) != inl)
857 /* As in and out can be the same pointer, process decrypted here */
858 if (!EVP_CIPHER_CTX_encrypting(ctx))
859 EVP_DigestSignUpdate(c->omac_ctx, out, inl);
864 static int gost_magma_cipher_do_mgm(EVP_CIPHER_CTX *ctx, unsigned char *out,
865 const unsigned char *in, size_t len)
868 (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
869 int enc = EVP_CIPHER_CTX_encrypting(ctx);
871 /* If not set up, return error */
872 if (!mctx->key_set) {
873 GOSTerr(GOST_F_GOST_MAGMA_CIPHER_DO_MGM,
879 GOSTerr(GOST_F_GOST_MAGMA_CIPHER_DO_MGM,
885 if (gost_mgm128_aad(&mctx->mgm, in, len))
888 if (gost_mgm128_encrypt(&mctx->mgm, in, out, len))
891 if (gost_mgm128_decrypt(&mctx->mgm, in, out, len))
897 if (mctx->taglen < 0)
899 if (gost_mgm128_finish(&mctx->mgm,
900 EVP_CIPHER_CTX_buf_noconst(ctx),
906 gost_mgm128_tag(&mctx->mgm, EVP_CIPHER_CTX_buf_noconst(ctx), 8);
908 /* Don't reuse the IV */
915 /* GOST encryption in CFB mode */
916 static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
917 const unsigned char *in, size_t inl)
919 const unsigned char *in_ptr = in;
920 unsigned char *out_ptr = out;
923 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
924 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
925 /* process partial block if any */
926 if (EVP_CIPHER_CTX_num(ctx)) {
927 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
928 j++, i++, in_ptr++, out_ptr++) {
929 if (!EVP_CIPHER_CTX_encrypting(ctx))
930 buf[j + 8] = *in_ptr;
931 *out_ptr = buf[j] ^ (*in_ptr);
932 if (EVP_CIPHER_CTX_encrypting(ctx))
933 buf[j + 8] = *out_ptr;
936 memcpy(iv, buf + 8, 8);
937 EVP_CIPHER_CTX_set_num(ctx, 0);
939 EVP_CIPHER_CTX_set_num(ctx, j);
944 for (; (inl - i) >= 8; i += 8, in_ptr += 8, out_ptr += 8) {
946 * block cipher current iv
948 gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
950 * xor next block of input text with it and output it
955 if (!EVP_CIPHER_CTX_encrypting(ctx))
956 memcpy(iv, in_ptr, 8);
957 for (j = 0; j < 8; j++) {
958 out_ptr[j] = buf[j] ^ in_ptr[j];
961 /* Next iv is next block of cipher text */
962 if (EVP_CIPHER_CTX_encrypting(ctx))
963 memcpy(iv, out_ptr, 8);
965 /* Process rest of buffer */
967 gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
968 if (!EVP_CIPHER_CTX_encrypting(ctx))
969 memcpy(buf + 8, in_ptr, inl - i);
970 for (j = 0; i < inl; j++, i++) {
971 out_ptr[j] = buf[j] ^ in_ptr[j];
973 EVP_CIPHER_CTX_set_num(ctx, j);
974 if (EVP_CIPHER_CTX_encrypting(ctx))
975 memcpy(buf + 8, out_ptr, j);
977 EVP_CIPHER_CTX_set_num(ctx, 0);
982 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
983 const unsigned char *in, size_t inl)
985 const unsigned char *in_ptr = in;
986 unsigned char *out_ptr = out;
989 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
990 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
991 /* process partial block if any */
992 if (EVP_CIPHER_CTX_num(ctx)) {
993 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
994 j++, i++, in_ptr++, out_ptr++) {
995 *out_ptr = buf[j] ^ (*in_ptr);
998 EVP_CIPHER_CTX_set_num(ctx, 0);
1000 EVP_CIPHER_CTX_set_num(ctx, j);
1005 for (; (inl - i) >= 8; i += 8, in_ptr += 8, out_ptr += 8) {
1007 * block cipher current iv
1010 gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
1012 * xor next block of input text with it and output it
1017 for (j = 0; j < 8; j++) {
1018 out_ptr[j] = buf[j] ^ in_ptr[j];
1021 /* Process rest of buffer */
1023 gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
1024 for (j = 0; i < inl; j++, i++) {
1025 out_ptr[j] = buf[j] ^ in_ptr[j];
1027 EVP_CIPHER_CTX_set_num(ctx, j);
1029 EVP_CIPHER_CTX_set_num(ctx, 0);
1034 /* Cleaning up of EVP_CIPHER_CTX */
1035 static int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
1037 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1038 EVP_MD_CTX_free(c->omac_ctx);
1039 gost_destroy(&(c->cctx));
1040 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
1044 static int gost_magma_mgm_cleanup(EVP_CIPHER_CTX *c)
1046 gost_mgm_ctx *mctx =
1047 (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(c);
1050 gost_destroy(&mctx->ks.g_ks.cctx);
1051 OPENSSL_cleanse(&mctx->mgm, sizeof(mctx->mgm));
1052 EVP_CIPHER_CTX_set_app_data(c, NULL);
1056 static int gost_magma_mgm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1058 gost_mgm_ctx *mctx =
1059 (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(c);
1060 unsigned char *buf, *iv;
1065 ivlen = EVP_CIPHER_iv_length(EVP_CIPHER_CTX_cipher(c));
1066 iv = EVP_CIPHER_CTX_iv_noconst(c);
1069 mctx->ivlen = ivlen;
1074 case EVP_CTRL_GET_IVLEN:
1075 *(int *)ptr = mctx->ivlen;
1078 case EVP_CTRL_AEAD_SET_IVLEN:
1081 if ((arg > EVP_MAX_IV_LENGTH) && (arg > mctx->ivlen)) {
1082 // TODO: Allocate memory for IV or set error
1088 case EVP_CTRL_AEAD_SET_TAG:
1089 buf = EVP_CIPHER_CTX_buf_noconst(c);
1090 enc = EVP_CIPHER_CTX_encrypting(c);
1091 if (arg <= 0 || arg != 8 || enc) {
1092 GOSTerr(GOST_F_GOST_MAGMA_MGM_CTRL,
1093 GOST_R_INVALID_TAG_LENGTH);
1096 memcpy(buf, ptr, arg);
1100 case EVP_CTRL_AEAD_GET_TAG:
1101 buf = EVP_CIPHER_CTX_buf_noconst(c);
1102 enc = EVP_CIPHER_CTX_encrypting(c);
1103 if (arg <= 0 || arg > 8 || !enc || mctx->taglen < 0) {
1104 GOSTerr(GOST_F_GOST_MAGMA_MGM_CTRL,
1105 GOST_R_INVALID_TAG_LENGTH);
1108 memcpy(ptr, buf, arg);
1116 /* Control function for gost cipher */
1117 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1120 case EVP_CTRL_RAND_KEY:
1123 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
1124 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
1129 case EVP_CTRL_PBE_PRF_NID:
1131 const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
1132 int nid = NID_id_tc26_hmac_gost_3411_2012_512;
1135 if (!strcmp("md_gost12_256", params))
1136 nid = NID_id_tc26_hmac_gost_3411_2012_256;
1137 else if (!strcmp("md_gost12_512", params))
1138 nid = NID_id_tc26_hmac_gost_3411_2012_512;
1139 else if (!strcmp("md_gost94", params))
1140 nid = NID_id_HMACGostR3411_94;
1142 *((int *)ptr) = nid;
1148 case EVP_CTRL_SET_SBOX:
1150 struct ossl_gost_cipher_ctx *c =
1151 EVP_CIPHER_CTX_get_cipher_data(ctx);
1160 if (c->count != 0) {
1164 nid = OBJ_txt2nid(ptr);
1165 if (nid == NID_undef) {
1169 cur_meshing = c->key_meshing;
1170 ret = gost_cipher_set_param(c, nid);
1171 c->key_meshing = cur_meshing;
1176 case EVP_CTRL_KEY_MESH:
1178 struct ossl_gost_cipher_ctx *c =
1179 EVP_CIPHER_CTX_get_cipher_data(ctx);
1185 if (c->count != 0) {
1189 c->key_meshing = arg;
1193 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
1199 /* Control function for gost cipher */
1200 static int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1203 case EVP_CTRL_RAND_KEY:
1205 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
1206 GOSTerr(GOST_F_MAGMA_CIPHER_CTL, GOST_R_RNG_ERROR);
1210 case EVP_CTRL_KEY_MESH:
1212 struct ossl_gost_cipher_ctx *c =
1213 EVP_CIPHER_CTX_get_cipher_data(ctx);
1219 if (c->count != 0) {
1223 c->key_meshing = arg;
1227 GOSTerr(GOST_F_MAGMA_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
1233 static int magma_cipher_ctl_acpkm_omac(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1237 case EVP_CTRL_PROCESS_UNPROTECTED:
1239 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1240 STACK_OF(X509_ATTRIBUTE) *x = ptr;
1241 return gost2015_process_unprotected_attributes(x, arg, MAGMA_MAC_MAX_SIZE, c->tag);
1243 case EVP_CTRL_COPY: {
1244 EVP_CIPHER_CTX *out = ptr;
1245 struct ossl_gost_cipher_ctx *in_cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
1246 struct ossl_gost_cipher_ctx *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
1248 if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
1249 out_cctx->omac_ctx = EVP_MD_CTX_new();
1250 if (out_cctx->omac_ctx == NULL) {
1251 GOSTerr(GOST_F_MAGMA_CIPHER_CTL_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
1255 return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
1258 return magma_cipher_ctl(ctx, type, arg, ptr);
1263 /* Set cipher parameters from ASN1 structure */
1264 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1267 unsigned char *buf = NULL;
1268 unsigned char *p = NULL;
1269 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1270 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
1271 ASN1_OCTET_STRING *os = NULL;
1273 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1276 if (!ASN1_OCTET_STRING_set
1277 (gcp->iv, EVP_CIPHER_CTX_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx))) {
1278 GOST_CIPHER_PARAMS_free(gcp);
1279 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1282 ASN1_OBJECT_free(gcp->enc_param_set);
1283 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
1285 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
1286 p = buf = OPENSSL_malloc(len);
1288 GOST_CIPHER_PARAMS_free(gcp);
1289 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1292 i2d_GOST_CIPHER_PARAMS(gcp, &p);
1293 GOST_CIPHER_PARAMS_free(gcp);
1295 os = ASN1_OCTET_STRING_new();
1297 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
1299 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1304 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
1308 /* Store parameters into ASN1 structure */
1309 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1312 GOST_CIPHER_PARAMS *gcp = NULL;
1314 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1317 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
1321 p = params->value.sequence->data;
1323 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
1324 params->value.sequence->length);
1326 len = gcp->iv->length;
1327 if (len != EVP_CIPHER_CTX_iv_length(ctx)) {
1328 GOST_CIPHER_PARAMS_free(gcp);
1329 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
1333 nid = OBJ_obj2nid(gcp->enc_param_set);
1334 if (nid == NID_undef) {
1335 GOST_CIPHER_PARAMS_free(gcp);
1336 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
1337 GOST_R_INVALID_CIPHER_PARAM_OID);
1341 if (!gost_cipher_set_param(c, nid)) {
1342 GOST_CIPHER_PARAMS_free(gcp);
1345 /*XXX missing non-const accessor */
1346 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), gcp->iv->data,
1347 EVP_CIPHER_CTX_iv_length(ctx));
1349 GOST_CIPHER_PARAMS_free(gcp);
1354 #define MAGMA_UKM_LEN 12
1355 static int magma_set_asn1_parameters (EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1357 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1358 c->key_meshing = 8192;
1360 return gost2015_set_asn1_params(params, EVP_CIPHER_CTX_original_iv(ctx), 4,
1364 static int magma_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1366 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1367 unsigned char iv[16];
1369 c->key_meshing = 8192;
1371 if (gost2015_get_asn1_params(params, MAGMA_UKM_LEN, iv, 4, c->kdf_seed) < 0)
1374 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, sizeof(iv));
1375 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, sizeof(iv));
1376 /* Key meshing 8 kb*/
1377 c->key_meshing = 8192;
1382 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
1384 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1385 memset(c->buffer, 0, sizeof(c->buffer));
1386 memset(c->partial_block, 0, sizeof(c->partial_block));
1391 gost_init(&(c->cctx), block);
1395 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
1397 return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
1400 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
1402 return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
1405 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
1406 const unsigned char *data)
1409 * We are using NULL for iv because CryptoPro doesn't interpret
1410 * internal state of MAC algorithm as iv during keymeshing (but does
1411 * initialize internal state from iv in key transport
1413 assert(c->count % 8 == 0 && c->count <= 1024);
1414 if (c->key_meshing && c->count == 1024) {
1415 cryptopro_key_meshing(&(c->cctx), NULL);
1417 mac_block(&(c->cctx), c->buffer, data);
1418 c->count = c->count % 1024 + 8;
1421 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
1423 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1424 const unsigned char *p = data;
1425 size_t bytes = count;
1426 if (!(c->key_set)) {
1427 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
1430 if (c->bytes_left) {
1432 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
1433 c->partial_block[i] = *p;
1436 mac_block_mesh(c, c->partial_block);
1443 mac_block_mesh(c, p);
1448 memcpy(c->partial_block, p, bytes);
1450 c->bytes_left = bytes;
1454 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
1456 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1458 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
1461 if (c->count == 0 && c->bytes_left) {
1462 unsigned char buffer[8];
1463 memset(buffer, 0, 8);
1464 gost_imit_update(ctx, buffer, 8);
1466 if (c->bytes_left) {
1468 for (i = c->bytes_left; i < 8; i++) {
1469 c->partial_block[i] = 0;
1471 mac_block_mesh(c, c->partial_block);
1473 get_mac(c->buffer, 8 * c->dgst_size, md);
1477 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
1480 case EVP_MD_CTRL_KEY_LEN:
1481 *((unsigned int *)(ptr)) = 32;
1483 case EVP_MD_CTRL_SET_KEY:
1485 struct ossl_gost_imit_ctx *gost_imit_ctx = EVP_MD_CTX_md_data(ctx);
1487 if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
1488 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
1491 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
1494 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
1495 if (key->mac_param_nid != NID_undef) {
1496 const struct gost_cipher_info *param =
1497 get_encryption_params(OBJ_nid2obj(key->mac_param_nid));
1498 if (param == NULL) {
1499 GOSTerr(GOST_F_GOST_IMIT_CTRL,
1500 GOST_R_INVALID_MAC_PARAMS);
1503 gost_init(&(gost_imit_ctx->cctx), param->sblock);
1505 gost_key(&(gost_imit_ctx->cctx), key->key);
1506 gost_imit_ctx->key_set = 1;
1509 } else if (arg == 32) {
1510 gost_key(&(gost_imit_ctx->cctx), ptr);
1511 gost_imit_ctx->key_set = 1;
1514 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
1517 case EVP_MD_CTRL_XOF_LEN:
1519 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1520 if (arg < 1 || arg > 8) {
1521 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
1533 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1535 if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
1536 memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
1537 sizeof(struct ossl_gost_imit_ctx));
1542 /* Clean up imit ctx */
1543 static int gost_imit_cleanup(EVP_MD_CTX *ctx)
1545 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
1548 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */