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 /* set/get cipher parameters */
47 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
48 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
49 /* Control function */
50 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
52 static int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
53 const unsigned char *iv, int enc);
54 static int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
55 const unsigned char *iv, int enc);
56 /* Handles block of data in CBC mode */
57 static int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
58 const unsigned char *in, size_t inl);
59 static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
60 const unsigned char *in, size_t inl);
62 static int magma_cipher_do_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, unsigned char *out,
63 const unsigned char *in, size_t inl);
65 /* set/get cipher parameters */
66 static int magma_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
67 static int magma_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
68 /* Control function */
69 static int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
70 static int magma_cipher_ctl_acpkm_omac(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
73 * Single level template accessor.
74 * Note: that you cannot template 0 value.
76 #define TPL(st,field) ( \
77 ((st)->field) ?: TPL_VAL(st,field) \
80 #define TPL_VAL(st,field) ( \
81 ((st)->template ? (st)->template->field : 0) \
84 EVP_CIPHER *GOST_init_cipher(GOST_cipher *c)
90 if (!(cipher = EVP_CIPHER_meth_new(c->nid, TPL(c, block_size), TPL(c, key_len)))
91 || !EVP_CIPHER_meth_set_iv_length(cipher, TPL(c, iv_len))
92 || !EVP_CIPHER_meth_set_flags(cipher, c->flags | TPL_VAL(c, flags))
93 || !EVP_CIPHER_meth_set_init(cipher, TPL(c, init))
94 || !EVP_CIPHER_meth_set_do_cipher(cipher, TPL(c, do_cipher))
95 || !EVP_CIPHER_meth_set_cleanup(cipher, TPL(c, cleanup))
96 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, TPL(c, ctx_size))
97 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, TPL(c, set_asn1_parameters))
98 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, TPL(c, get_asn1_parameters))
99 || !EVP_CIPHER_meth_set_ctrl(cipher, TPL(c, ctrl))) {
100 EVP_CIPHER_meth_free(cipher);
107 void GOST_deinit_cipher(GOST_cipher *c)
110 EVP_CIPHER_meth_free(c->cipher);
115 static GOST_cipher gost_template_cipher = {
119 .flags = EVP_CIPH_CUSTOM_IV |
121 EVP_CIPH_ALWAYS_CALL_INIT,
122 .cleanup = gost_cipher_cleanup,
123 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
124 .set_asn1_parameters = gost89_set_asn1_parameters,
125 .get_asn1_parameters = gost89_get_asn1_parameters,
126 .ctrl = gost_cipher_ctl,
129 GOST_cipher Gost28147_89_cipher = {
130 .nid = NID_id_Gost28147_89,
131 .template = &gost_template_cipher,
133 .flags = EVP_CIPH_CFB_MODE |
135 .init = gost_cipher_init,
136 .do_cipher = gost_cipher_do_cfb,
139 GOST_cipher Gost28147_89_cbc_cipher = {
140 .nid = NID_gost89_cbc,
141 .template = &gost_template_cipher,
142 .flags = EVP_CIPH_CBC_MODE,
143 .init = gost_cipher_init_cbc,
144 .do_cipher = gost_cipher_do_cbc,
147 GOST_cipher Gost28147_89_cnt_cipher = {
148 .nid = NID_gost89_cnt,
149 .template = &gost_template_cipher,
151 .flags = EVP_CIPH_OFB_MODE |
153 .init = gost_cipher_init_cpa,
154 .do_cipher = gost_cipher_do_cnt,
157 GOST_cipher Gost28147_89_cnt_12_cipher = {
158 .nid = NID_gost89_cnt_12,
159 .template = &gost_template_cipher,
161 .flags = EVP_CIPH_OFB_MODE |
163 .init = gost_cipher_init_cp_12,
164 .do_cipher = gost_cipher_do_cnt,
167 static GOST_cipher magma_template_cipher = {
171 .flags = EVP_CIPH_CUSTOM_IV |
173 EVP_CIPH_ALWAYS_CALL_INIT,
174 .cleanup = gost_cipher_cleanup,
175 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
176 .set_asn1_parameters = magma_set_asn1_parameters,
177 .get_asn1_parameters = magma_get_asn1_parameters,
178 .do_cipher = magma_cipher_do_ctr,
179 .ctrl = magma_cipher_ctl,
182 GOST_cipher magma_ctr_cipher = {
183 .nid = NID_magma_ctr,
184 .template = &magma_template_cipher,
187 .flags = EVP_CIPH_CTR_MODE |
189 .init = magma_cipher_init,
192 GOST_cipher magma_ctr_acpkm_cipher = {
193 .nid = NID_magma_ctr_acpkm,
194 .template = &magma_template_cipher,
197 .flags = EVP_CIPH_CTR_MODE |
199 .init = magma_cipher_init,
202 GOST_cipher magma_ctr_acpkm_omac_cipher = {
203 .nid = NID_magma_ctr_acpkm_omac,
204 .template = &magma_template_cipher,
207 .flags = EVP_CIPH_CTR_MODE |
208 EVP_CIPH_NO_PADDING |
209 EVP_CIPH_CUSTOM_COPY |
210 EVP_CIPH_FLAG_CUSTOM_CIPHER |
211 EVP_CIPH_FLAG_CIPHER_WITH_MAC,
212 .init = magma_cipher_init_ctr_acpkm_omac,
213 .do_cipher = magma_cipher_do_ctr_acpkm_omac,
214 .ctrl = magma_cipher_ctl_acpkm_omac,
217 GOST_cipher magma_cbc_cipher = {
218 .nid = NID_magma_cbc,
219 .template = &gost_template_cipher,
220 .flags = EVP_CIPH_CBC_MODE,
221 .init = magma_cipher_init,
222 .do_cipher = magma_cipher_do_cbc,
225 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
226 /* Init functions which set specific parameters */
227 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
228 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx);
229 /* process block of data */
230 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
231 /* Return computed value */
232 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
234 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
235 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
236 /* Control function, knows how to set MAC key.*/
237 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
239 static EVP_MD *_hidden_Gost28147_89_MAC_md = NULL;
240 static EVP_MD *_hidden_Gost28147_89_12_MAC_md = NULL;
242 EVP_MD *imit_gost_cpa(void)
244 if (_hidden_Gost28147_89_MAC_md == NULL) {
247 if ((md = EVP_MD_meth_new(NID_id_Gost28147_89_MAC, NID_undef)) == NULL
248 || !EVP_MD_meth_set_result_size(md, 4)
249 || !EVP_MD_meth_set_input_blocksize(md, 8)
250 || !EVP_MD_meth_set_app_datasize(md,
251 sizeof(struct ossl_gost_imit_ctx))
252 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_XOF)
253 || !EVP_MD_meth_set_init(md, gost_imit_init_cpa)
254 || !EVP_MD_meth_set_update(md, gost_imit_update)
255 || !EVP_MD_meth_set_final(md, gost_imit_final)
256 || !EVP_MD_meth_set_copy(md, gost_imit_copy)
257 || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
258 || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
259 EVP_MD_meth_free(md);
262 _hidden_Gost28147_89_MAC_md = md;
264 return _hidden_Gost28147_89_MAC_md;
267 void imit_gost_cpa_destroy(void)
269 EVP_MD_meth_free(_hidden_Gost28147_89_MAC_md);
270 _hidden_Gost28147_89_MAC_md = NULL;
273 EVP_MD *imit_gost_cp_12(void)
275 if (_hidden_Gost28147_89_12_MAC_md == NULL) {
278 if ((md = EVP_MD_meth_new(NID_gost_mac_12, NID_undef)) == NULL
279 || !EVP_MD_meth_set_result_size(md, 4)
280 || !EVP_MD_meth_set_input_blocksize(md, 8)
281 || !EVP_MD_meth_set_app_datasize(md,
282 sizeof(struct ossl_gost_imit_ctx))
283 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_XOF)
284 || !EVP_MD_meth_set_init(md, gost_imit_init_cp_12)
285 || !EVP_MD_meth_set_update(md, gost_imit_update)
286 || !EVP_MD_meth_set_final(md, gost_imit_final)
287 || !EVP_MD_meth_set_copy(md, gost_imit_copy)
288 || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
289 || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
290 EVP_MD_meth_free(md);
293 _hidden_Gost28147_89_12_MAC_md = md;
295 return _hidden_Gost28147_89_12_MAC_md;
298 void imit_gost_cp_12_destroy(void)
300 EVP_MD_meth_free(_hidden_Gost28147_89_12_MAC_md);
301 _hidden_Gost28147_89_12_MAC_md = NULL;
305 * Correspondence between gost parameter OIDs and substitution blocks
306 * NID field is filed by register_gost_NID function in engine.c
307 * upon engine initialization
310 struct gost_cipher_info gost_cipher_list[] = {
317 * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
319 {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
321 {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
323 {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
325 {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
327 {NID_id_tc26_gost_28147_param_Z, &Gost28147_TC26ParamSetZ, 1},
328 {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
333 * get encryption parameters from crypto network settings FIXME For now we
334 * use environment var CRYPT_PARAMS as place to store these settings.
335 * Actually, it is better to use engine control command, read from
336 * configuration file to set them
338 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
341 struct gost_cipher_info *param;
343 const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
344 if (!params || !strlen(params)) {
346 for (i = 0; gost_cipher_list[i].nid != NID_undef; i++)
347 if (gost_cipher_list[i].nid == NID_id_tc26_gost_28147_param_Z)
348 return &gost_cipher_list[i];
349 return &gost_cipher_list[0];
352 nid = OBJ_txt2nid(params);
353 if (nid == NID_undef) {
354 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
355 GOST_R_INVALID_CIPHER_PARAM_OID);
356 ERR_add_error_data(3, "Unsupported CRYPT_PARAMS='",
357 params, "' specified in environment or in config");
361 nid = OBJ_obj2nid(obj);
363 for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
365 if (!param->sblock) {
366 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
372 /* Sets cipher param from paramset NID. */
373 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
375 const struct gost_cipher_info *param;
376 param = get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
380 c->paramNID = param->nid;
381 c->key_meshing = param->key_meshing;
383 gost_init(&(c->cctx), param->sblock);
387 /* Initializes EVP_CIPHER_CTX by paramset NID */
388 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
389 const unsigned char *key,
390 const unsigned char *iv, int enc,
391 int paramNID, int mode)
393 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
394 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
395 if (!gost_cipher_set_param(c, paramNID))
397 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
400 gost_key(&(c->cctx), key);
402 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
403 EVP_CIPHER_CTX_iv_length(ctx));
405 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
406 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
410 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
411 const unsigned char *key,
412 const unsigned char *iv,
413 gost_subst_block * block)
415 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
416 gost_init(&(c->cctx), block);
420 gost_key(&(c->cctx), key);
422 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
423 EVP_CIPHER_CTX_iv_length(ctx));
425 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
426 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
430 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
431 const unsigned char *iv, int enc)
433 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
436 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
437 const unsigned char *key,
438 const unsigned char *iv, int enc)
440 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
443 /* Initializes EVP_CIPHER_CTX with default values */
444 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
445 const unsigned char *iv, int enc)
447 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
451 /* Initializes EVP_CIPHER_CTX with default values */
452 int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
453 const unsigned char *iv, int enc)
455 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
459 /* Initializes EVP_CIPHER_CTX with default values */
460 int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
461 const unsigned char *iv, int enc)
463 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
464 /* FIXME this is just initializtion check */
465 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
466 if (!gost_cipher_set_param(c, NID_id_tc26_gost_28147_param_Z))
468 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
471 if (init_zero_kdf_seed(c->kdf_seed) == 0)
477 magma_key(&(c->cctx), key);
479 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
480 EVP_CIPHER_CTX_iv_length(ctx));
482 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
483 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
485 if (EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm
486 || EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm_omac) {
487 c->key_meshing = 1024;
495 /* Initializes EVP_CIPHER_CTX with default values */
496 int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
497 const unsigned char *iv, int enc)
500 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
501 unsigned char cipher_key[32];
502 c->omac_ctx = EVP_MD_CTX_new();
504 if (c->omac_ctx == NULL) {
505 GOSTerr(GOST_F_MAGMA_CIPHER_INIT_CTR_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
509 if (gost2015_acpkm_omac_init(NID_magma_mac, enc, key,
510 c->omac_ctx, cipher_key, c->kdf_seed) != 1) {
511 EVP_MD_CTX_free(c->omac_ctx);
516 return magma_cipher_init(ctx, cipher_key, iv, enc);
519 return magma_cipher_init(ctx, key, iv, enc);
523 * Wrapper around gostcrypt function from gost89.c which perform key meshing
526 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
528 struct ossl_gost_cipher_ctx *c = ctx;
529 assert(c->count % 8 == 0 && c->count <= 1024);
530 if (c->key_meshing && c->count == 1024) {
531 cryptopro_key_meshing(&(c->cctx), iv);
533 gostcrypt(&(c->cctx), iv, buf);
534 c->count = c->count % 1024 + 8;
537 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
539 struct ossl_gost_cipher_ctx *c = ctx;
541 unsigned char buf1[8];
542 assert(c->count % 8 == 0 && c->count <= 1024);
543 if (c->key_meshing && c->count == 1024) {
544 cryptopro_key_meshing(&(c->cctx), iv);
547 gostcrypt(&(c->cctx), iv, buf1);
551 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
553 buf1[0] = (unsigned char)(g & 0xff);
554 buf1[1] = (unsigned char)((g >> 8) & 0xff);
555 buf1[2] = (unsigned char)((g >> 16) & 0xff);
556 buf1[3] = (unsigned char)((g >> 24) & 0xff);
557 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
560 if (go > g) /* overflow */
562 buf1[4] = (unsigned char)(g & 0xff);
563 buf1[5] = (unsigned char)((g >> 8) & 0xff);
564 buf1[6] = (unsigned char)((g >> 16) & 0xff);
565 buf1[7] = (unsigned char)((g >> 24) & 0xff);
567 gostcrypt(&(c->cctx), buf1, buf);
568 c->count = c->count % 1024 + 8;
571 /* GOST encryption in CBC mode */
572 int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
573 const unsigned char *in, size_t inl)
576 const unsigned char *in_ptr = in;
577 unsigned char *out_ptr = out;
579 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
580 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
581 if (EVP_CIPHER_CTX_encrypting(ctx)) {
584 for (i = 0; i < 8; i++) {
585 b[i] = iv[i] ^ in_ptr[i];
587 gostcrypt(&(c->cctx), b, out_ptr);
588 memcpy(iv, out_ptr, 8);
595 gostdecrypt(&(c->cctx), in_ptr, b);
596 for (i = 0; i < 8; i++) {
597 out_ptr[i] = iv[i] ^ b[i];
599 memcpy(iv, in_ptr, 8);
608 /* MAGMA encryption in CBC mode */
609 int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
610 const unsigned char *in, size_t inl)
614 const unsigned char *in_ptr = in;
615 unsigned char *out_ptr = out;
617 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
618 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
619 if (EVP_CIPHER_CTX_encrypting(ctx)) {
622 for (i = 0; i < 8; i++) {
623 b[7 - i] = iv[i] ^ in_ptr[i];
625 gostcrypt(&(c->cctx), b, d);
627 for (i = 0; i < 8; i++) {
628 out_ptr[7 - i] = d[i];
630 memcpy(iv, out_ptr, 8);
637 for (i = 0; i < 8; i++) {
638 d[7 - i] = in_ptr[i];
640 gostdecrypt(&(c->cctx), d, b);
641 memcpy(d, in_ptr, 8);
642 for (i = 0; i < 8; i++) {
643 out_ptr[i] = iv[i] ^ b[7 - i];
654 /* increment counter (64-bit int) by 1 */
655 static void ctr64_inc(unsigned char *counter)
657 inc_counter(counter, 8);
660 /* MAGMA encryption in CTR mode */
661 static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
662 const unsigned char *in, size_t inl)
664 const unsigned char *in_ptr = in;
665 unsigned char *out_ptr = out;
668 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
669 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
670 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
672 /* Process partial blocks */
673 if (EVP_CIPHER_CTX_num(ctx)) {
674 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
675 j++, i++, in_ptr++, out_ptr++) {
676 *out_ptr = buf[7 - j] ^ (*in_ptr);
679 EVP_CIPHER_CTX_set_num(ctx, 0);
681 EVP_CIPHER_CTX_set_num(ctx, j);
686 /* Process full blocks */
687 for (; i + 8 <= inl; i += 8, in_ptr += 8, out_ptr += 8) {
688 for (j = 0; j < 8; j++) {
691 gostcrypt(&(c->cctx), b, buf);
692 for (j = 0; j < 8; j++) {
693 out_ptr[j] = buf[7 - j] ^ in_ptr[j];
697 if (c->key_meshing && (c->count % c->key_meshing == 0))
698 acpkm_magma_key_meshing(&(c->cctx));
701 /* Process the rest of plaintext */
703 for (j = 0; j < 8; j++) {
706 gostcrypt(&(c->cctx), b, buf);
708 for (j = 0; i < inl; j++, i++) {
709 out_ptr[j] = buf[7 - j] ^ in_ptr[j];
714 if (c->key_meshing && (c->count % c->key_meshing == 0))
715 acpkm_magma_key_meshing(&(c->cctx));
717 EVP_CIPHER_CTX_set_num(ctx, j);
719 EVP_CIPHER_CTX_set_num(ctx, 0);
725 /* MAGMA encryption in CTR mode */
726 static int magma_cipher_do_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, unsigned char *out,
727 const unsigned char *in, size_t inl)
729 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
731 if (in == NULL && inl == 0) /* Final call */
732 return gost2015_final_call(ctx, c->omac_ctx, MAGMA_MAC_MAX_SIZE, c->tag, magma_cipher_do_ctr);
737 /* As in and out can be the same pointer, process unencrypted here */
738 if (EVP_CIPHER_CTX_encrypting(ctx))
739 EVP_DigestSignUpdate(c->omac_ctx, in, inl);
741 if (magma_cipher_do_ctr(ctx, out, in, inl) != inl)
744 /* As in and out can be the same pointer, process decrypted here */
745 if (!EVP_CIPHER_CTX_encrypting(ctx))
746 EVP_DigestSignUpdate(c->omac_ctx, out, inl);
750 /* GOST encryption in CFB mode */
751 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
752 const unsigned char *in, size_t inl)
754 const unsigned char *in_ptr = in;
755 unsigned char *out_ptr = out;
758 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
759 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
760 /* process partial block if any */
761 if (EVP_CIPHER_CTX_num(ctx)) {
762 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
763 j++, i++, in_ptr++, out_ptr++) {
764 if (!EVP_CIPHER_CTX_encrypting(ctx))
765 buf[j + 8] = *in_ptr;
766 *out_ptr = buf[j] ^ (*in_ptr);
767 if (EVP_CIPHER_CTX_encrypting(ctx))
768 buf[j + 8] = *out_ptr;
771 memcpy(iv, buf + 8, 8);
772 EVP_CIPHER_CTX_set_num(ctx, 0);
774 EVP_CIPHER_CTX_set_num(ctx, j);
779 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
781 * block cipher current iv
783 gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
785 * xor next block of input text with it and output it
790 if (!EVP_CIPHER_CTX_encrypting(ctx))
791 memcpy(iv, in_ptr, 8);
792 for (j = 0; j < 8; j++) {
793 out_ptr[j] = buf[j] ^ in_ptr[j];
796 /* Next iv is next block of cipher text */
797 if (EVP_CIPHER_CTX_encrypting(ctx))
798 memcpy(iv, out_ptr, 8);
800 /* Process rest of buffer */
802 gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
803 if (!EVP_CIPHER_CTX_encrypting(ctx))
804 memcpy(buf + 8, in_ptr, inl - i);
805 for (j = 0; i < inl; j++, i++) {
806 out_ptr[j] = buf[j] ^ in_ptr[j];
808 EVP_CIPHER_CTX_set_num(ctx, j);
809 if (EVP_CIPHER_CTX_encrypting(ctx))
810 memcpy(buf + 8, out_ptr, j);
812 EVP_CIPHER_CTX_set_num(ctx, 0);
817 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
818 const unsigned char *in, size_t inl)
820 const unsigned char *in_ptr = in;
821 unsigned char *out_ptr = out;
824 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
825 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
826 /* process partial block if any */
827 if (EVP_CIPHER_CTX_num(ctx)) {
828 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
829 j++, i++, in_ptr++, out_ptr++) {
830 *out_ptr = buf[j] ^ (*in_ptr);
833 EVP_CIPHER_CTX_set_num(ctx, 0);
835 EVP_CIPHER_CTX_set_num(ctx, j);
840 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
842 * block cipher current iv
845 gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
847 * xor next block of input text with it and output it
852 for (j = 0; j < 8; j++) {
853 out_ptr[j] = buf[j] ^ in_ptr[j];
856 /* Process rest of buffer */
858 gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
859 for (j = 0; i < inl; j++, i++) {
860 out_ptr[j] = buf[j] ^ in_ptr[j];
862 EVP_CIPHER_CTX_set_num(ctx, j);
864 EVP_CIPHER_CTX_set_num(ctx, 0);
869 /* Cleaning up of EVP_CIPHER_CTX */
870 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
872 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
873 EVP_MD_CTX_free(c->omac_ctx);
874 gost_destroy(&(c->cctx));
875 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
879 /* Control function for gost cipher */
880 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
883 case EVP_CTRL_RAND_KEY:
886 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
887 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
892 case EVP_CTRL_PBE_PRF_NID:
894 const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
895 int nid = NID_id_tc26_hmac_gost_3411_2012_512;
898 if (!strcmp("md_gost12_256", params))
899 nid = NID_id_tc26_hmac_gost_3411_2012_256;
900 else if (!strcmp("md_gost12_512", params))
901 nid = NID_id_tc26_hmac_gost_3411_2012_512;
902 else if (!strcmp("md_gost94", params))
903 nid = NID_id_HMACGostR3411_94;
911 case EVP_CTRL_SET_SBOX:
913 struct ossl_gost_cipher_ctx *c =
914 EVP_CIPHER_CTX_get_cipher_data(ctx);
927 nid = OBJ_txt2nid(ptr);
928 if (nid == NID_undef) {
932 cur_meshing = c->key_meshing;
933 ret = gost_cipher_set_param(c, nid);
934 c->key_meshing = cur_meshing;
939 case EVP_CTRL_KEY_MESH:
941 struct ossl_gost_cipher_ctx *c =
942 EVP_CIPHER_CTX_get_cipher_data(ctx);
952 c->key_meshing = arg;
956 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
962 /* Control function for gost cipher */
963 int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
966 case EVP_CTRL_RAND_KEY:
968 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
969 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
973 case EVP_CTRL_KEY_MESH:
975 struct ossl_gost_cipher_ctx *c =
976 EVP_CIPHER_CTX_get_cipher_data(ctx);
986 c->key_meshing = arg;
990 GOSTerr(GOST_F_MAGMA_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
996 static int magma_cipher_ctl_acpkm_omac(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1000 case EVP_CTRL_PROCESS_UNPROTECTED:
1002 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1003 STACK_OF(X509_ATTRIBUTE) *x = ptr;
1004 return gost2015_process_unprotected_attributes(x, arg, MAGMA_MAC_MAX_SIZE, c->tag);
1006 case EVP_CTRL_COPY: {
1007 EVP_CIPHER_CTX *out = ptr;
1008 struct ossl_gost_cipher_ctx *in_cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
1009 struct ossl_gost_cipher_ctx *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
1011 if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
1012 out_cctx->omac_ctx = EVP_MD_CTX_new();
1013 if (out_cctx->omac_ctx == NULL) {
1014 GOSTerr(GOST_F_MAGMA_CIPHER_CTL_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
1018 return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
1021 return magma_cipher_ctl(ctx, type, arg, ptr);
1026 /* Set cipher parameters from ASN1 structure */
1027 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1030 unsigned char *buf = NULL;
1031 unsigned char *p = NULL;
1032 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1033 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
1034 ASN1_OCTET_STRING *os = NULL;
1036 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1039 if (!ASN1_OCTET_STRING_set
1040 (gcp->iv, EVP_CIPHER_CTX_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx))) {
1041 GOST_CIPHER_PARAMS_free(gcp);
1042 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1045 ASN1_OBJECT_free(gcp->enc_param_set);
1046 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
1048 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
1049 p = buf = OPENSSL_malloc(len);
1051 GOST_CIPHER_PARAMS_free(gcp);
1052 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1055 i2d_GOST_CIPHER_PARAMS(gcp, &p);
1056 GOST_CIPHER_PARAMS_free(gcp);
1058 os = ASN1_OCTET_STRING_new();
1060 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
1062 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1067 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
1071 /* Store parameters into ASN1 structure */
1072 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1075 GOST_CIPHER_PARAMS *gcp = NULL;
1077 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1080 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
1084 p = params->value.sequence->data;
1086 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
1087 params->value.sequence->length);
1089 len = gcp->iv->length;
1090 if (len != EVP_CIPHER_CTX_iv_length(ctx)) {
1091 GOST_CIPHER_PARAMS_free(gcp);
1092 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
1096 nid = OBJ_obj2nid(gcp->enc_param_set);
1097 if (nid == NID_undef) {
1098 GOST_CIPHER_PARAMS_free(gcp);
1099 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
1100 GOST_R_INVALID_CIPHER_PARAM_OID);
1104 if (!gost_cipher_set_param(c, nid)) {
1105 GOST_CIPHER_PARAMS_free(gcp);
1108 /*XXX missing non-const accessor */
1109 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), gcp->iv->data,
1110 EVP_CIPHER_CTX_iv_length(ctx));
1112 GOST_CIPHER_PARAMS_free(gcp);
1117 #define MAGMA_UKM_LEN 12
1118 static int magma_set_asn1_parameters (EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1120 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1121 c->key_meshing = 8192;
1123 return gost2015_set_asn1_params(params, EVP_CIPHER_CTX_original_iv(ctx), 4,
1127 static int magma_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1129 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1130 unsigned char iv[16];
1132 c->key_meshing = 8192;
1134 if (gost2015_get_asn1_params(params, MAGMA_UKM_LEN, iv, 4, c->kdf_seed) < 0)
1137 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, sizeof(iv));
1138 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, sizeof(iv));
1139 /* Key meshing 8 kb*/
1140 c->key_meshing = 8192;
1145 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
1147 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1148 memset(c->buffer, 0, sizeof(c->buffer));
1149 memset(c->partial_block, 0, sizeof(c->partial_block));
1154 gost_init(&(c->cctx), block);
1158 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
1160 return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
1163 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
1165 return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
1168 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
1169 const unsigned char *data)
1172 * We are using NULL for iv because CryptoPro doesn't interpret
1173 * internal state of MAC algorithm as iv during keymeshing (but does
1174 * initialize internal state from iv in key transport
1176 assert(c->count % 8 == 0 && c->count <= 1024);
1177 if (c->key_meshing && c->count == 1024) {
1178 cryptopro_key_meshing(&(c->cctx), NULL);
1180 mac_block(&(c->cctx), c->buffer, data);
1181 c->count = c->count % 1024 + 8;
1184 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
1186 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1187 const unsigned char *p = data;
1188 size_t bytes = count;
1189 if (!(c->key_set)) {
1190 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
1193 if (c->bytes_left) {
1195 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
1196 c->partial_block[i] = *p;
1199 mac_block_mesh(c, c->partial_block);
1206 mac_block_mesh(c, p);
1211 memcpy(c->partial_block, p, bytes);
1213 c->bytes_left = bytes;
1217 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
1219 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1221 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
1224 if (c->count == 0 && c->bytes_left) {
1225 unsigned char buffer[8];
1226 memset(buffer, 0, 8);
1227 gost_imit_update(ctx, buffer, 8);
1229 if (c->bytes_left) {
1231 for (i = c->bytes_left; i < 8; i++) {
1232 c->partial_block[i] = 0;
1234 mac_block_mesh(c, c->partial_block);
1236 get_mac(c->buffer, 8 * c->dgst_size, md);
1240 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
1243 case EVP_MD_CTRL_KEY_LEN:
1244 *((unsigned int *)(ptr)) = 32;
1246 case EVP_MD_CTRL_SET_KEY:
1248 struct ossl_gost_imit_ctx *gost_imit_ctx = EVP_MD_CTX_md_data(ctx);
1250 if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
1251 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
1254 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
1257 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
1258 if (key->mac_param_nid != NID_undef) {
1259 const struct gost_cipher_info *param =
1260 get_encryption_params(OBJ_nid2obj(key->mac_param_nid));
1261 if (param == NULL) {
1262 GOSTerr(GOST_F_GOST_IMIT_CTRL,
1263 GOST_R_INVALID_MAC_PARAMS);
1266 gost_init(&(gost_imit_ctx->cctx), param->sblock);
1268 gost_key(&(gost_imit_ctx->cctx), key->key);
1269 gost_imit_ctx->key_set = 1;
1272 } else if (arg == 32) {
1273 gost_key(&(gost_imit_ctx->cctx), ptr);
1274 gost_imit_ctx->key_set = 1;
1277 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
1280 case EVP_MD_CTRL_XOF_LEN:
1282 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1283 if (arg < 1 || arg > 8) {
1284 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
1296 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1298 if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
1299 memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
1300 sizeof(struct ossl_gost_imit_ctx));
1305 /* Clean up imit ctx */
1306 int gost_imit_cleanup(EVP_MD_CTX *ctx)
1308 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
1311 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */