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) ? ((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)
89 /* Some sanity checking. */
90 int flags = c->flags | TPL_VAL(c, flags);
91 int block_size = TPL(c, block_size);
92 switch (flags & EVP_CIPH_MODE) {
93 case EVP_CIPH_CTR_MODE:
94 case EVP_CIPH_CFB_MODE:
95 case EVP_CIPH_OFB_MODE:
96 OPENSSL_assert(block_size == 1);
97 OPENSSL_assert(flags & EVP_CIPH_NO_PADDING);
100 OPENSSL_assert(block_size != 1);
101 OPENSSL_assert(!(flags & EVP_CIPH_NO_PADDING));
105 OPENSSL_assert(flags & EVP_CIPH_CUSTOM_IV);
107 OPENSSL_assert(!(flags & EVP_CIPH_CUSTOM_IV));
110 if (!(cipher = EVP_CIPHER_meth_new(c->nid, block_size, TPL(c, key_len)))
111 || !EVP_CIPHER_meth_set_iv_length(cipher, TPL(c, iv_len))
112 || !EVP_CIPHER_meth_set_flags(cipher, flags)
113 || !EVP_CIPHER_meth_set_init(cipher, TPL(c, init))
114 || !EVP_CIPHER_meth_set_do_cipher(cipher, TPL(c, do_cipher))
115 || !EVP_CIPHER_meth_set_cleanup(cipher, TPL(c, cleanup))
116 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, TPL(c, ctx_size))
117 || !EVP_CIPHER_meth_set_set_asn1_params(cipher, TPL(c, set_asn1_parameters))
118 || !EVP_CIPHER_meth_set_get_asn1_params(cipher, TPL(c, get_asn1_parameters))
119 || !EVP_CIPHER_meth_set_ctrl(cipher, TPL(c, ctrl))) {
120 EVP_CIPHER_meth_free(cipher);
127 void GOST_deinit_cipher(GOST_cipher *c)
130 EVP_CIPHER_meth_free(c->cipher);
135 static GOST_cipher gost_template_cipher = {
139 .flags = EVP_CIPH_CUSTOM_IV |
141 EVP_CIPH_ALWAYS_CALL_INIT,
142 .cleanup = gost_cipher_cleanup,
143 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
144 .set_asn1_parameters = gost89_set_asn1_parameters,
145 .get_asn1_parameters = gost89_get_asn1_parameters,
146 .ctrl = gost_cipher_ctl,
149 GOST_cipher Gost28147_89_cipher = {
150 .nid = NID_id_Gost28147_89,
151 .template = &gost_template_cipher,
153 .flags = EVP_CIPH_CFB_MODE |
155 .init = gost_cipher_init,
156 .do_cipher = gost_cipher_do_cfb,
159 GOST_cipher Gost28147_89_cbc_cipher = {
160 .nid = NID_gost89_cbc,
161 .template = &gost_template_cipher,
162 .flags = EVP_CIPH_CBC_MODE,
163 .init = gost_cipher_init_cbc,
164 .do_cipher = gost_cipher_do_cbc,
167 GOST_cipher Gost28147_89_cnt_cipher = {
168 .nid = NID_gost89_cnt,
169 .template = &gost_template_cipher,
171 .flags = EVP_CIPH_OFB_MODE |
173 .init = gost_cipher_init_cpa,
174 .do_cipher = gost_cipher_do_cnt,
177 GOST_cipher Gost28147_89_cnt_12_cipher = {
178 .nid = NID_gost89_cnt_12,
179 .template = &gost_template_cipher,
181 .flags = EVP_CIPH_OFB_MODE |
183 .init = gost_cipher_init_cp_12,
184 .do_cipher = gost_cipher_do_cnt,
187 static GOST_cipher magma_template_cipher = {
191 .flags = EVP_CIPH_CUSTOM_IV |
193 EVP_CIPH_ALWAYS_CALL_INIT,
194 .cleanup = gost_cipher_cleanup,
195 .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
196 .set_asn1_parameters = magma_set_asn1_parameters,
197 .get_asn1_parameters = magma_get_asn1_parameters,
198 .do_cipher = magma_cipher_do_ctr,
199 .ctrl = magma_cipher_ctl,
202 GOST_cipher magma_ctr_cipher = {
203 .nid = NID_magma_ctr,
204 .template = &magma_template_cipher,
207 .flags = EVP_CIPH_CTR_MODE |
209 .init = magma_cipher_init,
212 GOST_cipher magma_ctr_acpkm_cipher = {
213 .nid = NID_magma_ctr_acpkm,
214 .template = &magma_template_cipher,
217 .flags = EVP_CIPH_CTR_MODE |
219 .init = magma_cipher_init,
222 GOST_cipher magma_ctr_acpkm_omac_cipher = {
223 .nid = NID_magma_ctr_acpkm_omac,
224 .template = &magma_template_cipher,
227 .flags = EVP_CIPH_CTR_MODE |
228 EVP_CIPH_NO_PADDING |
229 EVP_CIPH_CUSTOM_COPY |
230 EVP_CIPH_FLAG_CUSTOM_CIPHER |
231 EVP_CIPH_FLAG_CIPHER_WITH_MAC,
232 .init = magma_cipher_init_ctr_acpkm_omac,
233 .do_cipher = magma_cipher_do_ctr_acpkm_omac,
234 .ctrl = magma_cipher_ctl_acpkm_omac,
237 GOST_cipher magma_cbc_cipher = {
238 .nid = NID_magma_cbc,
239 .template = &gost_template_cipher,
240 .flags = EVP_CIPH_CBC_MODE,
241 .init = magma_cipher_init,
242 .do_cipher = magma_cipher_do_cbc,
245 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
246 /* Init functions which set specific parameters */
247 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
248 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx);
249 /* process block of data */
250 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
251 /* Return computed value */
252 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
254 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
255 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
256 /* Control function, knows how to set MAC key.*/
257 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
259 GOST_digest Gost28147_89_MAC_digest = {
260 .nid = NID_id_Gost28147_89_MAC,
262 .input_blocksize = 8,
263 .app_datasize = sizeof(struct ossl_gost_imit_ctx),
264 .flags = EVP_MD_FLAG_XOF,
265 .init = gost_imit_init_cpa,
266 .update = gost_imit_update,
267 .final = gost_imit_final,
268 .copy = gost_imit_copy,
269 .cleanup = gost_imit_cleanup,
270 .ctrl = gost_imit_ctrl,
273 GOST_digest Gost28147_89_mac_12_digest = {
274 .nid = NID_gost_mac_12,
276 .input_blocksize = 8,
277 .app_datasize = sizeof(struct ossl_gost_imit_ctx),
278 .flags = EVP_MD_FLAG_XOF,
279 .init = gost_imit_init_cp_12,
280 .update = gost_imit_update,
281 .final = gost_imit_final,
282 .copy = gost_imit_copy,
283 .cleanup = gost_imit_cleanup,
284 .ctrl = gost_imit_ctrl,
288 * Correspondence between gost parameter OIDs and substitution blocks
289 * NID field is filed by register_gost_NID function in engine.c
290 * upon engine initialization
293 static struct gost_cipher_info gost_cipher_list[] = {
300 * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
302 {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
304 {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
306 {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
308 {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
310 {NID_id_tc26_gost_28147_param_Z, &Gost28147_TC26ParamSetZ, 1},
311 {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
316 * get encryption parameters from crypto network settings FIXME For now we
317 * use environment var CRYPT_PARAMS as place to store these settings.
318 * Actually, it is better to use engine control command, read from
319 * configuration file to set them
321 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
324 struct gost_cipher_info *param;
326 const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
327 if (!params || !strlen(params)) {
329 for (i = 0; gost_cipher_list[i].nid != NID_undef; i++)
330 if (gost_cipher_list[i].nid == NID_id_tc26_gost_28147_param_Z)
331 return &gost_cipher_list[i];
332 return &gost_cipher_list[0];
335 nid = OBJ_txt2nid(params);
336 if (nid == NID_undef) {
337 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
338 GOST_R_INVALID_CIPHER_PARAM_OID);
339 ERR_add_error_data(3, "Unsupported CRYPT_PARAMS='",
340 params, "' specified in environment or in config");
344 nid = OBJ_obj2nid(obj);
346 for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
348 if (!param->sblock) {
349 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
355 /* Sets cipher param from paramset NID. */
356 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
358 const struct gost_cipher_info *param;
359 param = get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
363 c->paramNID = param->nid;
364 c->key_meshing = param->key_meshing;
366 gost_init(&(c->cctx), param->sblock);
370 /* Initializes EVP_CIPHER_CTX by paramset NID */
371 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
372 const unsigned char *key,
373 const unsigned char *iv, int enc,
374 int paramNID, int mode)
376 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
377 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
378 if (!gost_cipher_set_param(c, paramNID))
380 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
383 gost_key(&(c->cctx), key);
385 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
386 EVP_CIPHER_CTX_iv_length(ctx));
388 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
389 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
393 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
394 const unsigned char *key,
395 const unsigned char *iv,
396 gost_subst_block * block)
398 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
399 gost_init(&(c->cctx), block);
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_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
414 const unsigned char *iv, int enc)
416 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
419 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
420 const unsigned char *key,
421 const unsigned char *iv, int enc)
423 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
426 /* Initializes EVP_CIPHER_CTX with default values */
427 static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
428 const unsigned char *iv, int enc)
430 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
434 /* Initializes EVP_CIPHER_CTX with default values */
435 static int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
436 const unsigned char *iv, int enc)
438 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
442 /* Initializes EVP_CIPHER_CTX with default values */
443 static int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
444 const unsigned char *iv, int enc)
446 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
447 /* FIXME this is just initializtion check */
448 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
449 if (!gost_cipher_set_param(c, NID_id_tc26_gost_28147_param_Z))
451 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
454 if (init_zero_kdf_seed(c->kdf_seed) == 0)
460 magma_key(&(c->cctx), key);
461 magma_master_key(&(c->cctx), key);
464 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
465 EVP_CIPHER_CTX_iv_length(ctx));
467 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
468 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
470 if (EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm
471 || EVP_CIPHER_CTX_nid(ctx) == NID_magma_ctr_acpkm_omac) {
472 c->key_meshing = 1024;
480 /* Initializes EVP_CIPHER_CTX with default values */
481 static int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
482 const unsigned char *iv, int enc)
485 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
486 unsigned char cipher_key[32];
487 c->omac_ctx = EVP_MD_CTX_new();
489 if (c->omac_ctx == NULL) {
490 GOSTerr(GOST_F_MAGMA_CIPHER_INIT_CTR_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
494 if (gost2015_acpkm_omac_init(NID_magma_mac, enc, key,
495 c->omac_ctx, cipher_key, c->kdf_seed) != 1) {
496 EVP_MD_CTX_free(c->omac_ctx);
501 return magma_cipher_init(ctx, cipher_key, iv, enc);
504 return magma_cipher_init(ctx, key, iv, enc);
508 * Wrapper around gostcrypt function from gost89.c which perform key meshing
511 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
513 struct ossl_gost_cipher_ctx *c = ctx;
514 assert(c->count % 8 == 0 && c->count <= 1024);
515 if (c->key_meshing && c->count == 1024) {
516 cryptopro_key_meshing(&(c->cctx), iv);
518 gostcrypt(&(c->cctx), iv, buf);
519 c->count = c->count % 1024 + 8;
522 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
524 struct ossl_gost_cipher_ctx *c = ctx;
526 unsigned char buf1[8];
527 assert(c->count % 8 == 0 && c->count <= 1024);
528 if (c->key_meshing && c->count == 1024) {
529 cryptopro_key_meshing(&(c->cctx), iv);
532 gostcrypt(&(c->cctx), iv, buf1);
536 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
538 buf1[0] = (unsigned char)(g & 0xff);
539 buf1[1] = (unsigned char)((g >> 8) & 0xff);
540 buf1[2] = (unsigned char)((g >> 16) & 0xff);
541 buf1[3] = (unsigned char)((g >> 24) & 0xff);
542 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
545 if (go > g) /* overflow */
547 buf1[4] = (unsigned char)(g & 0xff);
548 buf1[5] = (unsigned char)((g >> 8) & 0xff);
549 buf1[6] = (unsigned char)((g >> 16) & 0xff);
550 buf1[7] = (unsigned char)((g >> 24) & 0xff);
552 gostcrypt(&(c->cctx), buf1, buf);
553 c->count = c->count % 1024 + 8;
556 /* GOST encryption in CBC mode */
557 static int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
558 const unsigned char *in, size_t inl)
561 const unsigned char *in_ptr = in;
562 unsigned char *out_ptr = out;
564 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
565 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
566 if (EVP_CIPHER_CTX_encrypting(ctx)) {
569 for (i = 0; i < 8; i++) {
570 b[i] = iv[i] ^ in_ptr[i];
572 gostcrypt(&(c->cctx), b, out_ptr);
573 memcpy(iv, out_ptr, 8);
580 unsigned char tmpiv[8];
581 gostdecrypt(&(c->cctx), in_ptr, b);
582 memcpy(tmpiv, in_ptr, 8);
583 for (i = 0; i < 8; i++) {
584 out_ptr[i] = iv[i] ^ b[i];
586 memcpy(iv, tmpiv, 8);
595 /* MAGMA encryption in CBC mode */
596 static int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
597 const unsigned char *in, size_t inl)
601 const unsigned char *in_ptr = in;
602 unsigned char *out_ptr = out;
604 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
605 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
606 if (EVP_CIPHER_CTX_encrypting(ctx)) {
609 for (i = 0; i < 8; i++) {
610 out_ptr[i] = iv[i] ^ in_ptr[i];
612 magmacrypt(&(c->cctx), out_ptr, out_ptr);
613 memcpy(iv, out_ptr, 8);
620 magmadecrypt(&(c->cctx), in_ptr, b);
621 memcpy(d, in_ptr, 8);
622 for (i = 0; i < 8; i++) {
623 out_ptr[i] = iv[i] ^ b[i];
634 /* increment counter (64-bit int) by 1 */
635 static void ctr64_inc(unsigned char *counter)
637 inc_counter(counter, 8);
640 #define MAGMA_BLOCK_SIZE 8
641 #define MAGMA_BLOCK_MASK (MAGMA_BLOCK_SIZE - 1)
642 static inline void apply_acpkm_magma(struct ossl_gost_cipher_ctx *
643 ctx, unsigned int *num)
645 if (!ctx->key_meshing || (*num < (unsigned int)ctx->key_meshing))
647 acpkm_magma_key_meshing(&ctx->cctx);
648 *num &= MAGMA_BLOCK_MASK;
651 /* MAGMA encryption in CTR mode */
652 static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
653 const unsigned char *in, size_t inl)
655 const unsigned char *in_ptr = in;
656 unsigned char *out_ptr = out;
658 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
659 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
660 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
661 unsigned int num = EVP_CIPHER_CTX_num(ctx);
662 size_t blocks, i, lasted = inl;
663 /* Process partial blocks */
664 while ((num & MAGMA_BLOCK_MASK) && lasted) {
665 *out_ptr++ = *in_ptr++ ^ buf[num & MAGMA_BLOCK_MASK];
669 blocks = lasted / MAGMA_BLOCK_SIZE;
671 /* Process full blocks */
672 for (i = 0; i < blocks; i++) {
673 apply_acpkm_magma(c, &num);
674 magmacrypt(&(c->cctx), iv, buf);
675 for (j = 0; j < 8; j++) {
676 out_ptr[j] = buf[j] ^ in_ptr[j];
679 c->count += MAGMA_BLOCK_SIZE;
680 in_ptr += MAGMA_BLOCK_SIZE;
681 out_ptr += MAGMA_BLOCK_SIZE;
682 num += MAGMA_BLOCK_SIZE;
683 lasted -= MAGMA_BLOCK_SIZE;
686 /* Process the rest of plaintext */
688 apply_acpkm_magma(c, &num);
689 magmacrypt(&(c->cctx), iv, buf);
691 for (i = 0; i < lasted; i++)
692 out_ptr[i] = buf[i] ^ in_ptr[i];
697 EVP_CIPHER_CTX_set_num(ctx, num);
702 /* MAGMA encryption in CTR mode */
703 static int magma_cipher_do_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, unsigned char *out,
704 const unsigned char *in, size_t inl)
706 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
708 if (in == NULL && inl == 0) /* Final call */
709 return gost2015_final_call(ctx, c->omac_ctx, MAGMA_MAC_MAX_SIZE, c->tag, magma_cipher_do_ctr);
714 /* As in and out can be the same pointer, process unencrypted here */
715 if (EVP_CIPHER_CTX_encrypting(ctx))
716 EVP_DigestSignUpdate(c->omac_ctx, in, inl);
718 if (magma_cipher_do_ctr(ctx, out, in, inl) != inl)
721 /* As in and out can be the same pointer, process decrypted here */
722 if (!EVP_CIPHER_CTX_encrypting(ctx))
723 EVP_DigestSignUpdate(c->omac_ctx, out, inl);
727 /* GOST encryption in CFB mode */
728 static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
729 const unsigned char *in, size_t inl)
731 const unsigned char *in_ptr = in;
732 unsigned char *out_ptr = out;
735 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
736 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
737 /* process partial block if any */
738 if (EVP_CIPHER_CTX_num(ctx)) {
739 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
740 j++, i++, in_ptr++, out_ptr++) {
741 if (!EVP_CIPHER_CTX_encrypting(ctx))
742 buf[j + 8] = *in_ptr;
743 *out_ptr = buf[j] ^ (*in_ptr);
744 if (EVP_CIPHER_CTX_encrypting(ctx))
745 buf[j + 8] = *out_ptr;
748 memcpy(iv, buf + 8, 8);
749 EVP_CIPHER_CTX_set_num(ctx, 0);
751 EVP_CIPHER_CTX_set_num(ctx, j);
756 for (; (inl - i) >= 8; i += 8, in_ptr += 8, out_ptr += 8) {
758 * block cipher current iv
760 gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
762 * xor next block of input text with it and output it
767 if (!EVP_CIPHER_CTX_encrypting(ctx))
768 memcpy(iv, in_ptr, 8);
769 for (j = 0; j < 8; j++) {
770 out_ptr[j] = buf[j] ^ in_ptr[j];
773 /* Next iv is next block of cipher text */
774 if (EVP_CIPHER_CTX_encrypting(ctx))
775 memcpy(iv, out_ptr, 8);
777 /* Process rest of buffer */
779 gost_crypt_mesh(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
780 if (!EVP_CIPHER_CTX_encrypting(ctx))
781 memcpy(buf + 8, in_ptr, inl - i);
782 for (j = 0; i < inl; j++, i++) {
783 out_ptr[j] = buf[j] ^ in_ptr[j];
785 EVP_CIPHER_CTX_set_num(ctx, j);
786 if (EVP_CIPHER_CTX_encrypting(ctx))
787 memcpy(buf + 8, out_ptr, j);
789 EVP_CIPHER_CTX_set_num(ctx, 0);
794 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
795 const unsigned char *in, size_t inl)
797 const unsigned char *in_ptr = in;
798 unsigned char *out_ptr = out;
801 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
802 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
803 /* process partial block if any */
804 if (EVP_CIPHER_CTX_num(ctx)) {
805 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
806 j++, i++, in_ptr++, out_ptr++) {
807 *out_ptr = buf[j] ^ (*in_ptr);
810 EVP_CIPHER_CTX_set_num(ctx, 0);
812 EVP_CIPHER_CTX_set_num(ctx, j);
817 for (; (inl - i) >= 8; i += 8, in_ptr += 8, out_ptr += 8) {
819 * block cipher current iv
822 gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
824 * xor next block of input text with it and output it
829 for (j = 0; j < 8; j++) {
830 out_ptr[j] = buf[j] ^ in_ptr[j];
833 /* Process rest of buffer */
835 gost_cnt_next(EVP_CIPHER_CTX_get_cipher_data(ctx), iv, buf);
836 for (j = 0; i < inl; j++, i++) {
837 out_ptr[j] = buf[j] ^ in_ptr[j];
839 EVP_CIPHER_CTX_set_num(ctx, j);
841 EVP_CIPHER_CTX_set_num(ctx, 0);
846 /* Cleaning up of EVP_CIPHER_CTX */
847 static int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
849 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
850 EVP_MD_CTX_free(c->omac_ctx);
851 gost_destroy(&(c->cctx));
852 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
856 /* Control function for gost cipher */
857 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
860 case EVP_CTRL_RAND_KEY:
863 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
864 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
869 case EVP_CTRL_PBE_PRF_NID:
871 const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
872 int nid = NID_id_tc26_hmac_gost_3411_2012_512;
875 if (!strcmp("md_gost12_256", params))
876 nid = NID_id_tc26_hmac_gost_3411_2012_256;
877 else if (!strcmp("md_gost12_512", params))
878 nid = NID_id_tc26_hmac_gost_3411_2012_512;
879 else if (!strcmp("md_gost94", params))
880 nid = NID_id_HMACGostR3411_94;
888 case EVP_CTRL_SET_SBOX:
890 struct ossl_gost_cipher_ctx *c =
891 EVP_CIPHER_CTX_get_cipher_data(ctx);
904 nid = OBJ_txt2nid(ptr);
905 if (nid == NID_undef) {
909 cur_meshing = c->key_meshing;
910 ret = gost_cipher_set_param(c, nid);
911 c->key_meshing = cur_meshing;
916 case EVP_CTRL_KEY_MESH:
918 struct ossl_gost_cipher_ctx *c =
919 EVP_CIPHER_CTX_get_cipher_data(ctx);
929 c->key_meshing = arg;
933 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
939 /* Decrement 8-byte sequence if needed */
940 int decrement_sequence(unsigned char *seq, int decrement) {
941 if (decrement < 0 || decrement > 1)
946 for (j = 7; j >= 0; j--)
960 /* Control function for gost cipher */
961 static int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
964 case EVP_CTRL_RAND_KEY:
966 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
967 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
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;
987 case EVP_CTRL_TLSTREE:
989 unsigned char newkey[32];
990 int mode = EVP_CIPHER_CTX_mode(ctx);
991 struct ossl_gost_cipher_ctx *ctr_ctx = NULL;
994 unsigned char adjusted_iv[8];
995 unsigned char seq[8];
996 int j, carry, decrement_arg;
997 if (mode != EVP_CIPH_CTR_MODE)
1000 ctr_ctx = (struct ossl_gost_cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
1001 c = &(ctr_ctx->cctx);
1004 * 'arg' parameter indicates what we should do with sequence value.
1006 * When function called, seq is incremented after MAC calculation.
1007 * In ETM mode, we use seq 'as is' in the ctrl-function (arg = 0)
1008 * Otherwise we have to decrease it in the implementation (arg = 1).
1010 memcpy(seq, ptr, 8);
1011 decrement_arg = arg;
1012 if(!decrement_sequence(seq, decrement_arg)) {
1013 GOSTerr(GOST_F_MAGMA_CIPHER_CTL, GOST_R_CTRL_CALL_FAILED);
1017 if (gost_tlstree(NID_magma_cbc, (const unsigned char *)c->master_key, newkey,
1018 (const unsigned char *)seq) > 0) {
1019 memset(adjusted_iv, 0, 8);
1020 memcpy(adjusted_iv, EVP_CIPHER_CTX_original_iv(ctx), 4);
1021 for (j = 3, carry = 0; j >= 0; j--)
1023 int adj_byte = adjusted_iv[j] + seq[j+4] + carry;
1024 carry = (adj_byte > 255) ? 1 : 0;
1025 adjusted_iv[j] = adj_byte & 0xFF;
1027 EVP_CIPHER_CTX_set_num(ctx, 0);
1028 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), adjusted_iv, 8);
1030 magma_key(c, newkey);
1036 GOSTerr(GOST_F_MAGMA_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
1042 static int magma_cipher_ctl_acpkm_omac(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1046 case EVP_CTRL_PROCESS_UNPROTECTED:
1048 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1049 STACK_OF(X509_ATTRIBUTE) *x = ptr;
1050 return gost2015_process_unprotected_attributes(x, arg, MAGMA_MAC_MAX_SIZE, c->tag);
1052 case EVP_CTRL_COPY: {
1053 EVP_CIPHER_CTX *out = ptr;
1054 struct ossl_gost_cipher_ctx *in_cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
1055 struct ossl_gost_cipher_ctx *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
1057 if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
1058 out_cctx->omac_ctx = EVP_MD_CTX_new();
1059 if (out_cctx->omac_ctx == NULL) {
1060 GOSTerr(GOST_F_MAGMA_CIPHER_CTL_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
1064 return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
1067 return magma_cipher_ctl(ctx, type, arg, ptr);
1072 /* Set cipher parameters from ASN1 structure */
1073 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1076 unsigned char *buf = NULL;
1077 unsigned char *p = NULL;
1078 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1079 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
1080 ASN1_OCTET_STRING *os = NULL;
1082 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1085 if (!ASN1_OCTET_STRING_set
1086 (gcp->iv, EVP_CIPHER_CTX_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx))) {
1087 GOST_CIPHER_PARAMS_free(gcp);
1088 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1091 ASN1_OBJECT_free(gcp->enc_param_set);
1092 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
1094 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
1095 p = buf = OPENSSL_malloc(len);
1097 GOST_CIPHER_PARAMS_free(gcp);
1098 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1101 i2d_GOST_CIPHER_PARAMS(gcp, &p);
1102 GOST_CIPHER_PARAMS_free(gcp);
1104 os = ASN1_OCTET_STRING_new();
1106 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
1108 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
1113 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
1117 /* Store parameters into ASN1 structure */
1118 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1121 GOST_CIPHER_PARAMS *gcp = NULL;
1123 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1126 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
1130 p = params->value.sequence->data;
1132 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
1133 params->value.sequence->length);
1135 len = gcp->iv->length;
1136 if (len != EVP_CIPHER_CTX_iv_length(ctx)) {
1137 GOST_CIPHER_PARAMS_free(gcp);
1138 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
1142 nid = OBJ_obj2nid(gcp->enc_param_set);
1143 if (nid == NID_undef) {
1144 GOST_CIPHER_PARAMS_free(gcp);
1145 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
1146 GOST_R_INVALID_CIPHER_PARAM_OID);
1150 if (!gost_cipher_set_param(c, nid)) {
1151 GOST_CIPHER_PARAMS_free(gcp);
1154 /*XXX missing non-const accessor */
1155 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), gcp->iv->data,
1156 EVP_CIPHER_CTX_iv_length(ctx));
1158 GOST_CIPHER_PARAMS_free(gcp);
1163 #define MAGMA_UKM_LEN 12
1164 static int magma_set_asn1_parameters (EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1166 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1167 c->key_meshing = 8192;
1169 return gost2015_set_asn1_params(params, EVP_CIPHER_CTX_original_iv(ctx), 4,
1173 static int magma_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
1175 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
1176 unsigned char iv[16];
1178 c->key_meshing = 8192;
1180 if (gost2015_get_asn1_params(params, MAGMA_UKM_LEN, iv, 4, c->kdf_seed) < 0)
1183 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, sizeof(iv));
1184 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, sizeof(iv));
1185 /* Key meshing 8 kb*/
1186 c->key_meshing = 8192;
1191 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
1193 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1194 memset(c->buffer, 0, sizeof(c->buffer));
1195 memset(c->partial_block, 0, sizeof(c->partial_block));
1200 gost_init(&(c->cctx), block);
1204 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
1206 return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
1209 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
1211 return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
1214 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
1215 const unsigned char *data)
1218 * We are using NULL for iv because CryptoPro doesn't interpret
1219 * internal state of MAC algorithm as iv during keymeshing (but does
1220 * initialize internal state from iv in key transport
1222 assert(c->count % 8 == 0 && c->count <= 1024);
1223 if (c->key_meshing && c->count == 1024) {
1224 cryptopro_key_meshing(&(c->cctx), NULL);
1226 mac_block(&(c->cctx), c->buffer, data);
1227 c->count = c->count % 1024 + 8;
1230 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
1232 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1233 const unsigned char *p = data;
1234 size_t bytes = count;
1235 if (!(c->key_set)) {
1236 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
1239 if (c->bytes_left) {
1241 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
1242 c->partial_block[i] = *p;
1245 mac_block_mesh(c, c->partial_block);
1252 mac_block_mesh(c, p);
1257 memcpy(c->partial_block, p, bytes);
1259 c->bytes_left = bytes;
1263 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
1265 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1267 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
1270 if (c->count == 0 && c->bytes_left) {
1271 unsigned char buffer[8];
1272 memset(buffer, 0, 8);
1273 gost_imit_update(ctx, buffer, 8);
1275 if (c->bytes_left) {
1277 for (i = c->bytes_left; i < 8; i++) {
1278 c->partial_block[i] = 0;
1280 mac_block_mesh(c, c->partial_block);
1282 get_mac(c->buffer, 8 * c->dgst_size, md);
1286 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
1289 case EVP_MD_CTRL_KEY_LEN:
1290 *((unsigned int *)(ptr)) = 32;
1292 case EVP_MD_CTRL_SET_KEY:
1294 struct ossl_gost_imit_ctx *gost_imit_ctx = EVP_MD_CTX_md_data(ctx);
1296 if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
1297 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
1300 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
1303 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
1304 if (key->mac_param_nid != NID_undef) {
1305 const struct gost_cipher_info *param =
1306 get_encryption_params(OBJ_nid2obj(key->mac_param_nid));
1307 if (param == NULL) {
1308 GOSTerr(GOST_F_GOST_IMIT_CTRL,
1309 GOST_R_INVALID_MAC_PARAMS);
1312 gost_init(&(gost_imit_ctx->cctx), param->sblock);
1314 gost_key(&(gost_imit_ctx->cctx), key->key);
1315 gost_imit_ctx->key_set = 1;
1318 } else if (arg == 32) {
1319 gost_key(&(gost_imit_ctx->cctx), ptr);
1320 gost_imit_ctx->key_set = 1;
1323 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
1326 case EVP_MD_CTRL_XOF_LEN:
1328 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1329 if (arg < 1 || arg > 8) {
1330 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
1342 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1344 if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
1345 memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
1346 sizeof(struct ossl_gost_imit_ctx));
1351 /* Clean up imit ctx */
1352 static int gost_imit_cleanup(EVP_MD_CTX *ctx)
1354 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
1357 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */