1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * OpenSSL interface to GOST 28147-89 cipher functions *
7 * Requires OpenSSL 0.9.9 for compilation *
8 **********************************************************************/
11 #include <openssl/err.h>
12 #include <openssl/rand.h>
13 #include "e_gost_err.h"
16 #if !defined(CCGOST_DEBUG) && !defined(DEBUG)
23 static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
24 const unsigned char *iv, int enc);
25 static int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
26 const unsigned char *iv, int enc);
27 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
28 const unsigned char *iv, int enc);
29 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
30 const unsigned char *key,
31 const unsigned char *iv, int enc);
32 /* Handles block of data in CFB mode */
33 static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
34 const unsigned char *in, size_t inl);
35 /* Handles block of data in CBC mode */
36 static int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
37 const unsigned char *in, size_t inl);
38 /* Handles block of data in CNT mode */
39 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
40 const unsigned char *in, size_t inl);
41 /* Cleanup function */
42 static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
43 /* set/get cipher parameters */
44 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
45 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
46 /* Control function */
47 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
49 EVP_CIPHER cipher_gost = {
54 EVP_CIPH_CFB_MODE | EVP_CIPH_NO_PADDING |
55 EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
59 sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
60 gost89_set_asn1_parameters,
61 gost89_get_asn1_parameters,
66 EVP_CIPHER cipher_gost_cbc =
78 EVP_CIPH_CUSTOM_IV| EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
82 sizeof(struct ossl_gost_cipher_ctx),/* ctx_size */
83 gost89_set_asn1_parameters,
84 gost89_get_asn1_parameters,
89 EVP_CIPHER cipher_gost_cpacnt = {
94 EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
95 EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
99 sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
100 gost89_set_asn1_parameters,
101 gost89_get_asn1_parameters,
106 EVP_CIPHER cipher_gost_cpcnt_12 = {
107 #ifdef NID_gost89_cnt_12
116 EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
117 EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
118 gost_cipher_init_cp_12,
121 sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
122 gost89_set_asn1_parameters,
123 gost89_get_asn1_parameters,
128 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
129 /* Init functions which set specific parameters */
130 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
131 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx);
132 /* process block of data */
133 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
134 /* Return computed value */
135 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
137 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
138 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
139 /* Control function, knows how to set MAC key.*/
140 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
142 EVP_MD imit_gost_cpa = {
143 NID_id_Gost28147_89_MAC,
156 sizeof(struct ossl_gost_imit_ctx),
160 EVP_MD imit_gost_cp_12 = {
161 #ifdef NID_gost_mac_12
170 gost_imit_init_cp_12,
179 sizeof(struct ossl_gost_imit_ctx),
184 * Correspondence between gost parameter OIDs and substitution blocks
185 * NID field is filed by register_gost_NID function in engine.c
186 * upon engine initialization
189 struct gost_cipher_info gost_cipher_list[] = {
196 * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
198 {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
200 {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
202 {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
204 {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
206 {NID_undef/*,NID_id_tc26_gost_28147_param_Z*/, &Gost28147_TC26ParamSetZ, 1},
207 {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
212 * get encryption parameters from crypto network settings FIXME For now we
213 * use environment var CRYPT_PARAMS as place to store these settings.
214 * Actually, it is better to use engine control command, read from
215 * configuration file to set them
217 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
220 struct gost_cipher_info *param;
222 const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
223 if (!params || !strlen(params))
224 return &gost_cipher_list[4];
226 nid = OBJ_txt2nid(params);
227 if (nid == NID_undef) {
228 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
229 GOST_R_INVALID_CIPHER_PARAM_OID);
233 nid = OBJ_obj2nid(obj);
235 for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
237 if (!param->sblock) {
238 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
244 /* Sets cipher param from paramset NID. */
245 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
247 const struct gost_cipher_info *param;
249 get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
253 c->paramNID = param->nid;
254 c->key_meshing = param->key_meshing;
256 gost_init(&(c->cctx), param->sblock);
260 /* Initializes EVP_CIPHER_CTX by paramset NID */
261 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
262 const unsigned char *key,
263 const unsigned char *iv, int enc,
264 int paramNID, int mode)
266 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
267 if (ctx->app_data == NULL) {
268 if (!gost_cipher_set_param(c, paramNID))
270 ctx->app_data = ctx->cipher_data;
273 gost_key(&(c->cctx), key);
275 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
276 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
280 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
281 const unsigned char *key,
282 const unsigned char *iv,
283 gost_subst_block * block)
285 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
286 gost_init(&(c->cctx), block);
290 gost_key(&(c->cctx), key);
292 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
293 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
297 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
298 const unsigned char *iv, int enc)
300 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
303 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
304 const unsigned char *key,
305 const unsigned char *iv, int enc)
307 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
310 /* Initializes EVP_CIPHER_CTX with default values */
311 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
312 const unsigned char *iv, int enc)
314 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
318 /* Initializes EVP_CIPHER_CTX with default values */
319 int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
320 const unsigned char *iv, int enc)
322 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
328 * Wrapper around gostcrypt function from gost89.c which perform key meshing
331 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
333 struct ossl_gost_cipher_ctx *c = ctx;
334 assert(c->count % 8 == 0 && c->count <= 1024);
335 if (c->key_meshing && c->count == 1024) {
336 cryptopro_key_meshing(&(c->cctx), iv);
338 gostcrypt(&(c->cctx), iv, buf);
339 c->count = c->count % 1024 + 8;
342 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
344 struct ossl_gost_cipher_ctx *c = ctx;
346 unsigned char buf1[8];
347 assert(c->count % 8 == 0 && c->count <= 1024);
348 if (c->key_meshing && c->count == 1024) {
349 cryptopro_key_meshing(&(c->cctx), iv);
352 gostcrypt(&(c->cctx), iv, buf1);
356 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
358 buf1[0] = (unsigned char)(g & 0xff);
359 buf1[1] = (unsigned char)((g >> 8) & 0xff);
360 buf1[2] = (unsigned char)((g >> 16) & 0xff);
361 buf1[3] = (unsigned char)((g >> 24) & 0xff);
362 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
365 if (go > g) /* overflow */
367 buf1[4] = (unsigned char)(g & 0xff);
368 buf1[5] = (unsigned char)((g >> 8) & 0xff);
369 buf1[6] = (unsigned char)((g >> 16) & 0xff);
370 buf1[7] = (unsigned char)((g >> 24) & 0xff);
372 gostcrypt(&(c->cctx), buf1, buf);
373 c->count = c->count % 1024 + 8;
376 /* GOST encryptoon in CBC mode */
377 int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
378 const unsigned char *in, size_t inl)
380 OPENSSL_assert(inl % 8 ==0);
382 const unsigned char *in_ptr=in;
383 unsigned char *out_ptr=out;
385 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
392 b[i]=ctx->iv[i]^in_ptr[i];
394 gostcrypt(&(c->cctx),b,out_ptr);
395 memcpy(ctx->iv,out_ptr,8);
404 gostdecrypt(&(c->cctx),in_ptr,b);
407 out_ptr[i]=ctx->iv[i]^b[i];
409 memcpy(ctx->iv,in_ptr,8);
417 /* GOST encryption in CFB mode */
418 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
419 const unsigned char *in, size_t inl)
421 const unsigned char *in_ptr = in;
422 unsigned char *out_ptr = out;
425 /* process partial block if any */
427 for (j = ctx->num, i = 0; j < 8 && i < inl;
428 j++, i++, in_ptr++, out_ptr++) {
430 ctx->buf[j + 8] = *in_ptr;
431 *out_ptr = ctx->buf[j] ^ (*in_ptr);
433 ctx->buf[j + 8] = *out_ptr;
436 memcpy(ctx->iv, ctx->buf + 8, 8);
444 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
446 * block cipher current iv
448 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
450 * xor next block of input text with it and output it
456 memcpy(ctx->iv, in_ptr, 8);
457 for (j = 0; j < 8; j++) {
458 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
461 /* Next iv is next block of cipher text */
463 memcpy(ctx->iv, out_ptr, 8);
465 /* Process rest of buffer */
467 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
469 memcpy(ctx->buf + 8, in_ptr, inl - i);
470 for (j = 0; i < inl; j++, i++) {
471 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
475 memcpy(ctx->buf + 8, out_ptr, j);
482 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
483 const unsigned char *in, size_t inl)
485 const unsigned char *in_ptr = in;
486 unsigned char *out_ptr = out;
489 /* process partial block if any */
491 for (j = ctx->num, i = 0; j < 8 && i < inl;
492 j++, i++, in_ptr++, out_ptr++) {
493 *out_ptr = ctx->buf[j] ^ (*in_ptr);
503 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
505 * block cipher current iv
508 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
510 * xor next block of input text with it and output it
515 for (j = 0; j < 8; j++) {
516 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
519 /* Process rest of buffer */
521 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
522 for (j = 0; i < inl; j++, i++) {
523 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
532 /* Cleaning up of EVP_CIPHER_CTX */
533 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
535 gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
536 ctx->app_data = NULL;
540 /* Control function for gost cipher */
541 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
546 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
550 return gost_cipher_set_param(c, arg);
552 case EVP_CTRL_RAND_KEY:
554 if (RAND_bytes((unsigned char *)ptr, ctx->key_len) <= 0) {
555 GOSTerr(GOST_F_GOST_CIPHER_CTL,
556 GOST_R_RANDOM_GENERATOR_ERROR);
561 case EVP_CTRL_PBE_PRF_NID:
563 const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
564 int nid = NID_id_tc26_hmac_gost_3411_2012_512;
567 if (!strcmp("md_gost12_256", params))
568 nid = NID_id_tc26_hmac_gost_3411_2012_256;
569 else if (!strcmp("md_gost12_512", params))
570 nid = NID_id_tc26_hmac_gost_3411_2012_512;
571 else if (!strcmp("md_gost94", params))
572 nid = NID_id_HMACGostR3411_94;
581 GOSTerr(GOST_F_GOST_CIPHER_CTL,
582 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
588 /* Set cipher parameters from ASN1 structure */
589 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
592 unsigned char *buf = NULL;
593 unsigned char *p = NULL;
594 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
595 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
596 ASN1_OCTET_STRING *os = NULL;
598 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
601 if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) {
602 GOST_CIPHER_PARAMS_free(gcp);
603 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
606 ASN1_OBJECT_free(gcp->enc_param_set);
607 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
609 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
610 p = buf = OPENSSL_malloc(len);
612 GOST_CIPHER_PARAMS_free(gcp);
613 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
616 i2d_GOST_CIPHER_PARAMS(gcp, &p);
617 GOST_CIPHER_PARAMS_free(gcp);
619 os = ASN1_OCTET_STRING_new();
621 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
623 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
628 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
632 /* Store parameters into ASN1 structure */
633 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
637 GOST_CIPHER_PARAMS *gcp = NULL;
639 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
642 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
646 p = params->value.sequence->data;
648 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
649 params->value.sequence->length);
651 len = gcp->iv->length;
652 if (len != ctx->cipher->iv_len) {
653 GOST_CIPHER_PARAMS_free(gcp);
654 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
658 nid = OBJ_obj2nid(gcp->enc_param_set);
659 if (nid == NID_undef) {
660 GOST_CIPHER_PARAMS_free(gcp);
661 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
662 GOST_R_INVALID_CIPHER_PARAM_OID);
666 if (!gost_cipher_set_param(c, nid)) {
667 GOST_CIPHER_PARAMS_free(gcp);
670 memcpy(ctx->oiv, gcp->iv->data, len);
672 GOST_CIPHER_PARAMS_free(gcp);
677 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
679 struct ossl_gost_imit_ctx *c = ctx->md_data;
680 memset(c->buffer, 0, sizeof(c->buffer));
681 memset(c->partial_block, 0, sizeof(c->partial_block));
686 gost_init(&(c->cctx), block);
690 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
692 return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
695 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
697 return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
700 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
701 const unsigned char *data)
703 unsigned char buffer[8];
705 * We are using local buffer for iv because CryptoPro doesn't interpret
706 * internal state of MAC algorithm as iv during keymeshing (but does
707 * initialize internal state from iv in key transport
709 assert(c->count % 8 == 0 && c->count <= 1024);
710 if (c->key_meshing && c->count == 1024) {
711 cryptopro_key_meshing(&(c->cctx), buffer);
713 mac_block(&(c->cctx), c->buffer, data);
714 c->count = c->count % 1024 + 8;
717 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
719 struct ossl_gost_imit_ctx *c = ctx->md_data;
720 const unsigned char *p = data;
721 size_t bytes = count, i;
723 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
727 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
728 c->partial_block[i] = *p;
731 mac_block_mesh(c, c->partial_block);
738 mac_block_mesh(c, p);
743 memcpy(c->partial_block, p, bytes);
745 c->bytes_left = bytes;
749 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
751 struct ossl_gost_imit_ctx *c = ctx->md_data;
753 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
756 if (c->count == 0 && c->bytes_left) {
757 unsigned char buffer[8];
758 memset(buffer, 0, 8);
759 gost_imit_update(ctx, buffer, 8);
763 for (i = c->bytes_left; i < 8; i++) {
764 c->partial_block[i] = 0;
766 mac_block_mesh(c, c->partial_block);
768 get_mac(c->buffer, 8 * c->dgst_size, md);
772 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
775 case EVP_MD_CTRL_KEY_LEN:
776 *((unsigned int *)(ptr)) = 32;
778 case EVP_MD_CTRL_SET_KEY:
781 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
785 gost_key(&(((struct ossl_gost_imit_ctx *)(ctx->md_data))->cctx),
787 ((struct ossl_gost_imit_ctx *)(ctx->md_data))->key_set = 1;
791 case EVP_MD_CTRL_MAC_LEN:
793 if (arg < 1 || arg > 8) {
794 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
797 struct ossl_gost_imit_ctx *c = ctx->md_data;
807 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
809 memcpy(to->md_data, from->md_data, sizeof(struct ossl_gost_imit_ctx));
813 /* Clean up imit ctx */
814 int gost_imit_cleanup(EVP_MD_CTX *ctx)
816 memset(ctx->md_data, 0, sizeof(struct ossl_gost_imit_ctx));