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);
72 EVP_CIPHER *GOST_init_cipher(GOST_cipher *c)
78 if (!(cipher = EVP_CIPHER_meth_new(c->nid, c->block_size, c->key_len))
79 || !EVP_CIPHER_meth_set_iv_length(cipher, c->iv_len)
80 || !EVP_CIPHER_meth_set_flags(cipher, c->flags)
81 || !EVP_CIPHER_meth_set_init(cipher, c->init)
82 || !EVP_CIPHER_meth_set_do_cipher(cipher, c->do_cipher)
83 || !EVP_CIPHER_meth_set_cleanup(cipher, c->cleanup)
84 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, c->ctx_size)
85 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, c->set_asn1_parameters)
86 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, c->get_asn1_parameters)
87 || !EVP_CIPHER_meth_set_ctrl(cipher, c->ctrl)) {
88 EVP_CIPHER_meth_free(cipher);
95 void GOST_deinit_cipher(GOST_cipher *c)
98 EVP_CIPHER_meth_free(c->cipher);
103 GOST_cipher Gost28147_89_cipher = {
104 .nid = NID_id_Gost28147_89,
108 .flags = EVP_CIPH_CFB_MODE |
109 EVP_CIPH_NO_PADDING |
112 EVP_CIPH_ALWAYS_CALL_INIT,
113 .init = gost_cipher_init,
114 .do_cipher = gost_cipher_do_cfb,
115 .cleanup = gost_cipher_cleanup,
116 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
117 .set_asn1_parameters = gost89_set_asn1_parameters,
118 .get_asn1_parameters = gost89_get_asn1_parameters,
119 .ctrl = gost_cipher_ctl,
122 GOST_cipher Gost28147_89_cbc_cipher = {
123 .nid = NID_gost89_cbc,
127 .flags = EVP_CIPH_CBC_MODE |
130 EVP_CIPH_ALWAYS_CALL_INIT,
131 .init = gost_cipher_init_cbc,
132 .do_cipher = gost_cipher_do_cbc,
133 .cleanup = gost_cipher_cleanup,
134 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
135 .set_asn1_parameters = gost89_set_asn1_parameters,
136 .get_asn1_parameters = gost89_get_asn1_parameters,
137 .ctrl = gost_cipher_ctl,
140 GOST_cipher Gost28147_89_cnt_cipher = {
141 .nid = NID_gost89_cnt,
145 .flags = EVP_CIPH_OFB_MODE |
146 EVP_CIPH_NO_PADDING |
149 EVP_CIPH_ALWAYS_CALL_INIT,
150 .init = gost_cipher_init_cpa,
151 .do_cipher = gost_cipher_do_cnt,
152 .cleanup = gost_cipher_cleanup,
153 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
154 .set_asn1_parameters = gost89_set_asn1_parameters,
155 .get_asn1_parameters = gost89_get_asn1_parameters,
156 .ctrl = gost_cipher_ctl,
159 GOST_cipher Gost28147_89_cnt_12_cipher = {
160 .nid = NID_gost89_cnt_12,
164 .flags = EVP_CIPH_OFB_MODE |
165 EVP_CIPH_NO_PADDING |
168 EVP_CIPH_ALWAYS_CALL_INIT,
169 .init = gost_cipher_init_cp_12,
170 .do_cipher = gost_cipher_do_cnt,
171 .cleanup = gost_cipher_cleanup,
172 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
173 .set_asn1_parameters = gost89_set_asn1_parameters,
174 .get_asn1_parameters = gost89_get_asn1_parameters,
175 .ctrl = gost_cipher_ctl,
178 GOST_cipher magma_ctr_cipher = {
179 .nid = NID_magma_ctr,
183 .flags = EVP_CIPH_CTR_MODE |
184 EVP_CIPH_NO_PADDING |
187 EVP_CIPH_ALWAYS_CALL_INIT,
188 .init = magma_cipher_init,
189 .do_cipher = magma_cipher_do_ctr,
190 .cleanup = gost_cipher_cleanup,
191 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
192 .set_asn1_parameters = gost89_set_asn1_parameters,
193 .get_asn1_parameters = gost89_get_asn1_parameters,
194 .ctrl = magma_cipher_ctl,
197 GOST_cipher magma_ctr_acpkm_cipher = {
198 .nid = NID_magma_ctr_acpkm,
202 .flags = EVP_CIPH_CTR_MODE |
203 EVP_CIPH_NO_PADDING |
206 EVP_CIPH_ALWAYS_CALL_INIT,
207 .init = magma_cipher_init,
208 .do_cipher = magma_cipher_do_ctr,
209 .cleanup = gost_cipher_cleanup,
210 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
211 .set_asn1_parameters = gost89_set_asn1_parameters,
212 .get_asn1_parameters = gost89_get_asn1_parameters,
213 .ctrl = magma_cipher_ctl,
216 GOST_cipher magma_ctr_acpkm_omac_cipher = {
217 .nid = NID_magma_ctr_acpkm_omac,
221 .flags = EVP_CIPH_CTR_MODE |
222 EVP_CIPH_NO_PADDING |
225 EVP_CIPH_ALWAYS_CALL_INIT |
226 EVP_CIPH_CUSTOM_COPY |
227 EVP_CIPH_FLAG_CUSTOM_CIPHER |
228 EVP_CIPH_FLAG_CIPHER_WITH_MAC,
229 .init = magma_cipher_init_ctr_acpkm_omac,
230 .do_cipher = magma_cipher_do_ctr_acpkm_omac,
231 .cleanup = gost_cipher_cleanup,
232 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
233 .set_asn1_parameters = gost89_set_asn1_parameters,
234 .get_asn1_parameters = gost89_get_asn1_parameters,
235 .ctrl = magma_cipher_ctl_acpkm_omac,
238 GOST_cipher magma_cbc_cipher = {
239 .nid = NID_magma_cbc,
243 .flags = EVP_CIPH_CBC_MODE |
244 EVP_CIPH_NO_PADDING |
247 EVP_CIPH_ALWAYS_CALL_INIT,
248 .init = magma_cipher_init,
249 .do_cipher = magma_cipher_do_cbc,
250 .cleanup = gost_cipher_cleanup,
251 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
252 .set_asn1_parameters = gost89_set_asn1_parameters,
253 .get_asn1_parameters = gost89_get_asn1_parameters,
254 .ctrl = magma_cipher_ctl,
257 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
258 /* Init functions which set specific parameters */
259 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
260 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx);
261 /* process block of data */
262 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
263 /* Return computed value */
264 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
266 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
267 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
268 /* Control function, knows how to set MAC key.*/
269 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
271 static EVP_MD *_hidden_Gost28147_89_MAC_md = NULL;
272 static EVP_MD *_hidden_Gost28147_89_12_MAC_md = NULL;
274 EVP_MD *imit_gost_cpa(void)
276 if (_hidden_Gost28147_89_MAC_md == NULL) {
279 if ((md = EVP_MD_meth_new(NID_id_Gost28147_89_MAC, NID_undef)) == NULL
280 || !EVP_MD_meth_set_result_size(md, 4)
281 || !EVP_MD_meth_set_input_blocksize(md, 8)
282 || !EVP_MD_meth_set_app_datasize(md,
283 sizeof(struct ossl_gost_imit_ctx))
284 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_XOF)
285 || !EVP_MD_meth_set_init(md, gost_imit_init_cpa)
286 || !EVP_MD_meth_set_update(md, gost_imit_update)
287 || !EVP_MD_meth_set_final(md, gost_imit_final)
288 || !EVP_MD_meth_set_copy(md, gost_imit_copy)
289 || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
290 || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
291 EVP_MD_meth_free(md);
294 _hidden_Gost28147_89_MAC_md = md;
296 return _hidden_Gost28147_89_MAC_md;
299 void imit_gost_cpa_destroy(void)
301 EVP_MD_meth_free(_hidden_Gost28147_89_MAC_md);
302 _hidden_Gost28147_89_MAC_md = NULL;
305 EVP_MD *imit_gost_cp_12(void)
307 if (_hidden_Gost28147_89_12_MAC_md == NULL) {
310 if ((md = EVP_MD_meth_new(NID_gost_mac_12, NID_undef)) == NULL
311 || !EVP_MD_meth_set_result_size(md, 4)
312 || !EVP_MD_meth_set_input_blocksize(md, 8)
313 || !EVP_MD_meth_set_app_datasize(md,
314 sizeof(struct ossl_gost_imit_ctx))
315 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_XOF)
316 || !EVP_MD_meth_set_init(md, gost_imit_init_cp_12)
317 || !EVP_MD_meth_set_update(md, gost_imit_update)
318 || !EVP_MD_meth_set_final(md, gost_imit_final)
319 || !EVP_MD_meth_set_copy(md, gost_imit_copy)
320 || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
321 || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
322 EVP_MD_meth_free(md);
325 _hidden_Gost28147_89_12_MAC_md = md;
327 return _hidden_Gost28147_89_12_MAC_md;
330 void imit_gost_cp_12_destroy(void)
332 EVP_MD_meth_free(_hidden_Gost28147_89_12_MAC_md);
333 _hidden_Gost28147_89_12_MAC_md = NULL;
337 * Correspondence between gost parameter OIDs and substitution blocks
338 * NID field is filed by register_gost_NID function in engine.c
339 * upon engine initialization
342 struct gost_cipher_info gost_cipher_list[] = {
349 * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
351 {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
353 {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
355 {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
357 {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
359 {NID_id_tc26_gost_28147_param_Z, &Gost28147_TC26ParamSetZ, 1},
360 {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
365 * get encryption parameters from crypto network settings FIXME For now we
366 * use environment var CRYPT_PARAMS as place to store these settings.
367 * Actually, it is better to use engine control command, read from
368 * configuration file to set them
370 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
373 struct gost_cipher_info *param;
375 const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
376 if (!params || !strlen(params)) {
378 for (i = 0; gost_cipher_list[i].nid != NID_undef; i++)
379 if (gost_cipher_list[i].nid == NID_id_tc26_gost_28147_param_Z)
380 return &gost_cipher_list[i];
381 return &gost_cipher_list[0];
384 nid = OBJ_txt2nid(params);
385 if (nid == NID_undef) {
386 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
387 GOST_R_INVALID_CIPHER_PARAM_OID);
388 ERR_add_error_data(3, "Unsupported CRYPT_PARAMS='",
389 params, "' specified in environment or in config");
393 nid = OBJ_obj2nid(obj);
395 for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
397 if (!param->sblock) {
398 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
404 /* Sets cipher param from paramset NID. */
405 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
407 const struct gost_cipher_info *param;
408 param = get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
412 c->paramNID = param->nid;
413 c->key_meshing = param->key_meshing;
415 gost_init(&(c->cctx), param->sblock);
419 /* Initializes EVP_CIPHER_CTX by paramset NID */
420 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
421 const unsigned char *key,
422 const unsigned char *iv, int enc,
423 int paramNID, int mode)
425 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
426 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
427 if (!gost_cipher_set_param(c, paramNID))
429 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
432 gost_key(&(c->cctx), key);
434 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
435 EVP_CIPHER_CTX_iv_length(ctx));
437 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
438 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
442 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
443 const unsigned char *key,
444 const unsigned char *iv,
445 gost_subst_block * block)
447 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
448 gost_init(&(c->cctx), block);
452 gost_key(&(c->cctx), key);
454 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
455 EVP_CIPHER_CTX_iv_length(ctx));
457 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
458 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
462 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
463 const unsigned char *iv, int enc)
465 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
468 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
469 const unsigned char *key,
470 const unsigned char *iv, int enc)
472 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
475 /* Initializes EVP_CIPHER_CTX with default values */
476 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
477 const unsigned char *iv, int enc)
479 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
483 /* Initializes EVP_CIPHER_CTX with default values */
484 int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
485 const unsigned char *iv, int enc)
487 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
491 /* Initializes EVP_CIPHER_CTX with default values */
492 int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
493 const unsigned char *iv, int enc)
495 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
496 /* FIXME this is just initializtion check */
497 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
498 if (!gost_cipher_set_param(c, NID_id_tc26_gost_28147_param_Z))
500 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
503 if (init_zero_kdf_seed(c->kdf_seed) == 0)
509 magma_key(&(c->cctx), key);
511 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
512 EVP_CIPHER_CTX_iv_length(ctx));
514 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
515 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
517 if (EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm
518 || EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm_omac) {
519 c->key_meshing = 1024;
527 /* Initializes EVP_CIPHER_CTX with default values */
528 int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
529 const unsigned char *iv, int enc)
532 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
533 unsigned char cipher_key[32];
534 c->omac_ctx = EVP_MD_CTX_new();
536 if (c->omac_ctx == NULL) {
537 GOSTerr(GOST_F_MAGMA_CIPHER_INIT_CTR_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
541 if (gost2015_acpkm_omac_init(NID_magma_mac, enc, key,
542 c->omac_ctx, cipher_key, c->kdf_seed) != 1) {
543 EVP_MD_CTX_free(c->omac_ctx);
548 return magma_cipher_init(ctx, cipher_key, iv, enc);
551 return magma_cipher_init(ctx, key, iv, enc);
555 * Wrapper around gostcrypt function from gost89.c which perform key meshing
558 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
560 struct ossl_gost_cipher_ctx *c = ctx;
561 assert(c->count % 8 == 0 && c->count <= 1024);
562 if (c->key_meshing && c->count == 1024) {
563 cryptopro_key_meshing(&(c->cctx), iv);
565 gostcrypt(&(c->cctx), iv, buf);
566 c->count = c->count % 1024 + 8;
569 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
571 struct ossl_gost_cipher_ctx *c = ctx;
573 unsigned char buf1[8];
574 assert(c->count % 8 == 0 && c->count <= 1024);
575 if (c->key_meshing && c->count == 1024) {
576 cryptopro_key_meshing(&(c->cctx), iv);
579 gostcrypt(&(c->cctx), iv, buf1);
583 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
585 buf1[0] = (unsigned char)(g & 0xff);
586 buf1[1] = (unsigned char)((g >> 8) & 0xff);
587 buf1[2] = (unsigned char)((g >> 16) & 0xff);
588 buf1[3] = (unsigned char)((g >> 24) & 0xff);
589 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
592 if (go > g) /* overflow */
594 buf1[4] = (unsigned char)(g & 0xff);
595 buf1[5] = (unsigned char)((g >> 8) & 0xff);
596 buf1[6] = (unsigned char)((g >> 16) & 0xff);
597 buf1[7] = (unsigned char)((g >> 24) & 0xff);
599 gostcrypt(&(c->cctx), buf1, buf);
600 c->count = c->count % 1024 + 8;
603 /* GOST encryption in CBC mode */
604 int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
605 const unsigned char *in, size_t inl)
608 const unsigned char *in_ptr = in;
609 unsigned char *out_ptr = out;
611 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
612 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
613 if (EVP_CIPHER_CTX_encrypting(ctx)) {
616 for (i = 0; i < 8; i++) {
617 b[i] = iv[i] ^ in_ptr[i];
619 gostcrypt(&(c->cctx), b, out_ptr);
620 memcpy(iv, out_ptr, 8);
627 gostdecrypt(&(c->cctx), in_ptr, b);
628 for (i = 0; i < 8; i++) {
629 out_ptr[i] = iv[i] ^ b[i];
631 memcpy(iv, in_ptr, 8);
640 /* MAGMA encryption in CBC mode */
641 int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
642 const unsigned char *in, size_t inl)
646 const unsigned char *in_ptr = in;
647 unsigned char *out_ptr = out;
649 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
650 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
651 if (EVP_CIPHER_CTX_encrypting(ctx)) {
654 for (i = 0; i < 8; i++) {
655 b[7 - i] = iv[i] ^ in_ptr[i];
657 gostcrypt(&(c->cctx), b, d);
659 for (i = 0; i < 8; i++) {
660 out_ptr[7 - i] = d[i];
662 memcpy(iv, out_ptr, 8);
669 for (i = 0; i < 8; i++) {
670 d[7 - i] = in_ptr[i];
672 gostdecrypt(&(c->cctx), d, b);
673 memcpy(d, in_ptr, 8);
674 for (i = 0; i < 8; i++) {
675 out_ptr[i] = iv[i] ^ b[7 - i];
686 /* increment counter (64-bit int) by 1 */
687 static void ctr64_inc(unsigned char *counter)
689 inc_counter(counter, 8);
692 /* MAGMA encryption in CTR mode */
693 static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
694 const unsigned char *in, size_t inl)
696 const unsigned char *in_ptr = in;
697 unsigned char *out_ptr = out;
700 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
701 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
702 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
704 /* Process partial blocks */
705 if (EVP_CIPHER_CTX_num(ctx)) {
706 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
707 j++, i++, in_ptr++, out_ptr++) {
708 *out_ptr = buf[7 - j] ^ (*in_ptr);
711 EVP_CIPHER_CTX_set_num(ctx, 0);
713 EVP_CIPHER_CTX_set_num(ctx, j);
718 /* Process full blocks */
719 for (; i + 8 <= inl; i += 8, in_ptr += 8, out_ptr += 8) {
720 for (j = 0; j < 8; j++) {
723 gostcrypt(&(c->cctx), b, buf);
724 for (j = 0; j < 8; j++) {
725 out_ptr[j] = buf[7 - j] ^ in_ptr[j];
729 if (c->key_meshing && (c->count % c->key_meshing == 0))
730 acpkm_magma_key_meshing(&(c->cctx));
733 /* Process the rest of plaintext */
735 for (j = 0; j < 8; j++) {
738 gostcrypt(&(c->cctx), b, buf);
740 for (j = 0; i < inl; j++, i++) {
741 out_ptr[j] = buf[7 - j] ^ in_ptr[j];
746 if (c->key_meshing && (c->count % c->key_meshing == 0))
747 acpkm_magma_key_meshing(&(c->cctx));
749 EVP_CIPHER_CTX_set_num(ctx, j);
751 EVP_CIPHER_CTX_set_num(ctx, 0);
757 /* MAGMA encryption in CTR mode */
758 static int magma_cipher_do_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, unsigned char *out,
759 const unsigned char *in, size_t inl)
761 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
763 if (in == NULL && inl == 0) /* Final call */
764 return gost2015_final_call(ctx, c->omac_ctx, MAGMA_MAC_MAX_SIZE, c->tag, magma_cipher_do_ctr);
769 /* As in and out can be the same pointer, process unencrypted here */
770 if (EVP_CIPHER_CTX_encrypting(ctx))
771 EVP_DigestSignUpdate(c->omac_ctx, in, inl);
773 if (magma_cipher_do_ctr(ctx, out, in, inl) != inl)
776 /* As in and out can be the same pointer, process decrypted here */
777 if (!EVP_CIPHER_CTX_encrypting(ctx))
778 EVP_DigestSignUpdate(c->omac_ctx, out, inl);
782 /* GOST encryption in CFB mode */
783 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
784 const unsigned char *in, size_t inl)
786 const unsigned char *in_ptr = in;
787 unsigned char *out_ptr = out;
790 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
791 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
792 /* process partial block if any */
793 if (EVP_CIPHER_CTX_num(ctx)) {
794 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
795 j++, i++, in_ptr++, out_ptr++) {
796 if (!EVP_CIPHER_CTX_encrypting(ctx))
797 buf[j + 8] = *in_ptr;
798 *out_ptr = buf[j] ^ (*in_ptr);
799 if (EVP_CIPHER_CTX_encrypting(ctx))
800 buf[j + 8] = *out_ptr;
803 memcpy(iv, buf + 8, 8);
804 EVP_CIPHER_CTX_set_num(ctx, 0);
806 EVP_CIPHER_CTX_set_num(ctx, j);
811 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
813 * block cipher current iv
815 gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
817 * xor next block of input text with it and output it
822 if (!EVP_CIPHER_CTX_encrypting(ctx))
823 memcpy(iv, in_ptr, 8);
824 for (j = 0; j < 8; j++) {
825 out_ptr[j] = buf[j] ^ in_ptr[j];
828 /* Next iv is next block of cipher text */
829 if (EVP_CIPHER_CTX_encrypting(ctx))
830 memcpy(iv, out_ptr, 8);
832 /* Process rest of buffer */
834 gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
835 if (!EVP_CIPHER_CTX_encrypting(ctx))
836 memcpy(buf + 8, in_ptr, inl - i);
837 for (j = 0; i < inl; j++, i++) {
838 out_ptr[j] = buf[j] ^ in_ptr[j];
840 EVP_CIPHER_CTX_set_num(ctx, j);
841 if (EVP_CIPHER_CTX_encrypting(ctx))
842 memcpy(buf + 8, out_ptr, j);
844 EVP_CIPHER_CTX_set_num(ctx, 0);
849 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
850 const unsigned char *in, size_t inl)
852 const unsigned char *in_ptr = in;
853 unsigned char *out_ptr = out;
856 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
857 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
858 /* process partial block if any */
859 if (EVP_CIPHER_CTX_num(ctx)) {
860 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
861 j++, i++, in_ptr++, out_ptr++) {
862 *out_ptr = buf[j] ^ (*in_ptr);
865 EVP_CIPHER_CTX_set_num(ctx, 0);
867 EVP_CIPHER_CTX_set_num(ctx, j);
872 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
874 * block cipher current iv
877 gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
879 * xor next block of input text with it and output it
884 for (j = 0; j < 8; j++) {
885 out_ptr[j] = buf[j] ^ in_ptr[j];
888 /* Process rest of buffer */
890 gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
891 for (j = 0; i < inl; j++, i++) {
892 out_ptr[j] = buf[j] ^ in_ptr[j];
894 EVP_CIPHER_CTX_set_num(ctx, j);
896 EVP_CIPHER_CTX_set_num(ctx, 0);
901 /* Cleaning up of EVP_CIPHER_CTX */
902 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
904 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
905 EVP_MD_CTX_free(c->omac_ctx);
906 gost_destroy(&(c->cctx));
907 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
911 /* Control function for gost cipher */
912 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
915 case EVP_CTRL_RAND_KEY:
918 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
919 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
924 case EVP_CTRL_PBE_PRF_NID:
926 const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
927 int nid = NID_id_tc26_hmac_gost_3411_2012_512;
930 if (!strcmp("md_gost12_256", params))
931 nid = NID_id_tc26_hmac_gost_3411_2012_256;
932 else if (!strcmp("md_gost12_512", params))
933 nid = NID_id_tc26_hmac_gost_3411_2012_512;
934 else if (!strcmp("md_gost94", params))
935 nid = NID_id_HMACGostR3411_94;
943 case EVP_CTRL_SET_SBOX:
945 struct ossl_gost_cipher_ctx *c =
946 EVP_CIPHER_CTX_get_cipher_data(ctx);
959 nid = OBJ_txt2nid(ptr);
960 if (nid == NID_undef) {
964 cur_meshing = c->key_meshing;
965 ret = gost_cipher_set_param(c, nid);
966 c->key_meshing = cur_meshing;
971 case EVP_CTRL_KEY_MESH:
973 struct ossl_gost_cipher_ctx *c =
974 EVP_CIPHER_CTX_get_cipher_data(ctx);
984 c->key_meshing = arg;
988 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
994 /* Control function for gost cipher */
995 int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
998 case EVP_CTRL_RAND_KEY:
1000 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
1001 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
1005 case EVP_CTRL_KEY_MESH:
1007 struct ossl_gost_cipher_ctx *c =
1008 EVP_CIPHER_CTX_get_cipher_data(ctx);
1014 if (c->count != 0) {
1018 c->key_meshing = arg;
1022 GOSTerr(GOST_F_MAGMA_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
1028 static int magma_cipher_ctl_acpkm_omac(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1032 case EVP_CTRL_PROCESS_UNPROTECTED:
1034 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1035 STACK_OF(X509_ATTRIBUTE) *x = ptr;
1036 return gost2015_process_unprotected_attributes(x, arg, MAGMA_MAC_MAX_SIZE, c->tag);
1038 case EVP_CTRL_COPY: {
1039 EVP_CIPHER_CTX *out = ptr;
1040 struct ossl_gost_cipher_ctx *in_cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
1041 struct ossl_gost_cipher_ctx *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
1043 if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
1044 out_cctx->omac_ctx = EVP_MD_CTX_new();
1045 if (out_cctx->omac_ctx == NULL) {
1046 GOSTerr(GOST_F_MAGMA_CIPHER_CTL_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
1050 return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
1053 return magma_cipher_ctl(ctx, type, arg, ptr);
1058 /* Set cipher parameters from ASN1 structure */
1059 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1062 unsigned char *buf = NULL;
1063 unsigned char *p = NULL;
1064 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1065 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
1066 ASN1_OCTET_STRING *os = NULL;
1068 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1071 if (!ASN1_OCTET_STRING_set
1072 (gcp->iv, EVP_CIPHER_CTX_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx))) {
1073 GOST_CIPHER_PARAMS_free(gcp);
1074 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1077 ASN1_OBJECT_free(gcp->enc_param_set);
1078 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
1080 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
1081 p = buf = OPENSSL_malloc(len);
1083 GOST_CIPHER_PARAMS_free(gcp);
1084 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1087 i2d_GOST_CIPHER_PARAMS(gcp, &p);
1088 GOST_CIPHER_PARAMS_free(gcp);
1090 os = ASN1_OCTET_STRING_new();
1092 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
1094 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1099 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
1103 /* Store parameters into ASN1 structure */
1104 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1107 GOST_CIPHER_PARAMS *gcp = NULL;
1109 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1112 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
1116 p = params->value.sequence->data;
1118 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
1119 params->value.sequence->length);
1121 len = gcp->iv->length;
1122 if (len != EVP_CIPHER_CTX_iv_length(ctx)) {
1123 GOST_CIPHER_PARAMS_free(gcp);
1124 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
1128 nid = OBJ_obj2nid(gcp->enc_param_set);
1129 if (nid == NID_undef) {
1130 GOST_CIPHER_PARAMS_free(gcp);
1131 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
1132 GOST_R_INVALID_CIPHER_PARAM_OID);
1136 if (!gost_cipher_set_param(c, nid)) {
1137 GOST_CIPHER_PARAMS_free(gcp);
1140 /*XXX missing non-const accessor */
1141 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), gcp->iv->data,
1142 EVP_CIPHER_CTX_iv_length(ctx));
1144 GOST_CIPHER_PARAMS_free(gcp);
1149 #define MAGMA_UKM_LEN 12
1150 static int magma_set_asn1_parameters (EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1152 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1153 c->key_meshing = 8192;
1155 return gost2015_set_asn1_params(params, EVP_CIPHER_CTX_original_iv(ctx), 4,
1159 static int magma_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1161 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1162 unsigned char iv[16];
1164 c->key_meshing = 8192;
1166 if (gost2015_get_asn1_params(params, MAGMA_UKM_LEN, iv, 4, c->kdf_seed) < 0)
1169 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, sizeof(iv));
1170 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, sizeof(iv));
1171 /* Key meshing 8 kb*/
1172 c->key_meshing = 8192;
1177 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
1179 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1180 memset(c->buffer, 0, sizeof(c->buffer));
1181 memset(c->partial_block, 0, sizeof(c->partial_block));
1186 gost_init(&(c->cctx), block);
1190 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
1192 return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
1195 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
1197 return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
1200 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
1201 const unsigned char *data)
1204 * We are using NULL for iv because CryptoPro doesn't interpret
1205 * internal state of MAC algorithm as iv during keymeshing (but does
1206 * initialize internal state from iv in key transport
1208 assert(c->count % 8 == 0 && c->count <= 1024);
1209 if (c->key_meshing && c->count == 1024) {
1210 cryptopro_key_meshing(&(c->cctx), NULL);
1212 mac_block(&(c->cctx), c->buffer, data);
1213 c->count = c->count % 1024 + 8;
1216 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
1218 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1219 const unsigned char *p = data;
1220 size_t bytes = count;
1221 if (!(c->key_set)) {
1222 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
1225 if (c->bytes_left) {
1227 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
1228 c->partial_block[i] = *p;
1231 mac_block_mesh(c, c->partial_block);
1238 mac_block_mesh(c, p);
1243 memcpy(c->partial_block, p, bytes);
1245 c->bytes_left = bytes;
1249 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
1251 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1253 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
1256 if (c->count == 0 && c->bytes_left) {
1257 unsigned char buffer[8];
1258 memset(buffer, 0, 8);
1259 gost_imit_update(ctx, buffer, 8);
1261 if (c->bytes_left) {
1263 for (i = c->bytes_left; i < 8; i++) {
1264 c->partial_block[i] = 0;
1266 mac_block_mesh(c, c->partial_block);
1268 get_mac(c->buffer, 8 * c->dgst_size, md);
1272 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
1275 case EVP_MD_CTRL_KEY_LEN:
1276 *((unsigned int *)(ptr)) = 32;
1278 case EVP_MD_CTRL_SET_KEY:
1280 struct ossl_gost_imit_ctx *gost_imit_ctx = EVP_MD_CTX_md_data(ctx);
1282 if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
1283 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
1286 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
1289 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
1290 if (key->mac_param_nid != NID_undef) {
1291 const struct gost_cipher_info *param =
1292 get_encryption_params(OBJ_nid2obj(key->mac_param_nid));
1293 if (param == NULL) {
1294 GOSTerr(GOST_F_GOST_IMIT_CTRL,
1295 GOST_R_INVALID_MAC_PARAMS);
1298 gost_init(&(gost_imit_ctx->cctx), param->sblock);
1300 gost_key(&(gost_imit_ctx->cctx), key->key);
1301 gost_imit_ctx->key_set = 1;
1304 } else if (arg == 32) {
1305 gost_key(&(gost_imit_ctx->cctx), ptr);
1306 gost_imit_ctx->key_set = 1;
1309 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
1312 case EVP_MD_CTRL_XOF_LEN:
1314 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1315 if (arg < 1 || arg > 8) {
1316 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
1328 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1330 if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
1331 memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
1332 sizeof(struct ossl_gost_imit_ctx));
1337 /* Clean up imit ctx */
1338 int gost_imit_cleanup(EVP_MD_CTX *ctx)
1340 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
1343 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */