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 =
73 EVP_CIPH_CUSTOM_IV| EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
77 sizeof(struct ossl_gost_cipher_ctx),/* ctx_size */
78 gost89_set_asn1_parameters,
79 gost89_get_asn1_parameters,
84 EVP_CIPHER cipher_gost_cpacnt = {
89 EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
90 EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
94 sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
95 gost89_set_asn1_parameters,
96 gost89_get_asn1_parameters,
101 EVP_CIPHER cipher_gost_cpcnt_12 = {
106 EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
107 EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
108 gost_cipher_init_cp_12,
111 sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
112 gost89_set_asn1_parameters,
113 gost89_get_asn1_parameters,
118 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
119 /* Init functions which set specific parameters */
120 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
121 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx);
122 /* process block of data */
123 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
124 /* Return computed value */
125 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
127 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
128 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
129 /* Control function, knows how to set MAC key.*/
130 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
132 EVP_MD imit_gost_cpa = {
133 NID_id_Gost28147_89_MAC,
146 sizeof(struct ossl_gost_imit_ctx),
150 EVP_MD imit_gost_cp_12 = {
155 gost_imit_init_cp_12,
164 sizeof(struct ossl_gost_imit_ctx),
169 * Correspondence between gost parameter OIDs and substitution blocks
170 * NID field is filed by register_gost_NID function in engine.c
171 * upon engine initialization
174 struct gost_cipher_info gost_cipher_list[] = {
181 * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
183 {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
185 {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
187 {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
189 {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
191 {NID_id_tc26_gost_28147_param_Z, &Gost28147_TC26ParamSetZ, 1},
192 {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
197 * get encryption parameters from crypto network settings FIXME For now we
198 * use environment var CRYPT_PARAMS as place to store these settings.
199 * Actually, it is better to use engine control command, read from
200 * configuration file to set them
202 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
205 struct gost_cipher_info *param;
207 const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
208 if (!params || !strlen(params)) {
210 for (i = 0; gost_cipher_list[i].nid != NID_undef; i++)
211 if (gost_cipher_list[i].nid == NID_id_tc26_gost_28147_param_Z)
212 return &gost_cipher_list[i];
213 return &gost_cipher_list[0];
216 nid = OBJ_txt2nid(params);
217 if (nid == NID_undef) {
218 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
219 GOST_R_INVALID_CIPHER_PARAM_OID);
223 nid = OBJ_obj2nid(obj);
225 for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
227 if (!param->sblock) {
228 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
234 /* Sets cipher param from paramset NID. */
235 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
237 const struct gost_cipher_info *param;
239 get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
243 c->paramNID = param->nid;
244 c->key_meshing = param->key_meshing;
246 gost_init(&(c->cctx), param->sblock);
250 /* Initializes EVP_CIPHER_CTX by paramset NID */
251 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
252 const unsigned char *key,
253 const unsigned char *iv, int enc,
254 int paramNID, int mode)
256 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
257 if (ctx->app_data == NULL) {
258 if (!gost_cipher_set_param(c, paramNID))
260 ctx->app_data = ctx->cipher_data;
263 gost_key(&(c->cctx), key);
265 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
266 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
270 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
271 const unsigned char *key,
272 const unsigned char *iv,
273 gost_subst_block * block)
275 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
276 gost_init(&(c->cctx), block);
280 gost_key(&(c->cctx), key);
282 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
283 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
287 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
288 const unsigned char *iv, int enc)
290 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
293 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
294 const unsigned char *key,
295 const unsigned char *iv, int enc)
297 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
300 /* Initializes EVP_CIPHER_CTX with default values */
301 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
302 const unsigned char *iv, int enc)
304 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
308 /* Initializes EVP_CIPHER_CTX with default values */
309 int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
310 const unsigned char *iv, int enc)
312 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
318 * Wrapper around gostcrypt function from gost89.c which perform key meshing
321 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
323 struct ossl_gost_cipher_ctx *c = ctx;
324 assert(c->count % 8 == 0 && c->count <= 1024);
325 if (c->key_meshing && c->count == 1024) {
326 cryptopro_key_meshing(&(c->cctx), iv);
328 gostcrypt(&(c->cctx), iv, buf);
329 c->count = c->count % 1024 + 8;
332 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
334 struct ossl_gost_cipher_ctx *c = ctx;
336 unsigned char buf1[8];
337 assert(c->count % 8 == 0 && c->count <= 1024);
338 if (c->key_meshing && c->count == 1024) {
339 cryptopro_key_meshing(&(c->cctx), iv);
342 gostcrypt(&(c->cctx), iv, buf1);
346 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
348 buf1[0] = (unsigned char)(g & 0xff);
349 buf1[1] = (unsigned char)((g >> 8) & 0xff);
350 buf1[2] = (unsigned char)((g >> 16) & 0xff);
351 buf1[3] = (unsigned char)((g >> 24) & 0xff);
352 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
355 if (go > g) /* overflow */
357 buf1[4] = (unsigned char)(g & 0xff);
358 buf1[5] = (unsigned char)((g >> 8) & 0xff);
359 buf1[6] = (unsigned char)((g >> 16) & 0xff);
360 buf1[7] = (unsigned char)((g >> 24) & 0xff);
362 gostcrypt(&(c->cctx), buf1, buf);
363 c->count = c->count % 1024 + 8;
366 /* GOST encryptoon in CBC mode */
367 int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
368 const unsigned char *in, size_t inl)
370 OPENSSL_assert(inl % 8 ==0);
372 const unsigned char *in_ptr=in;
373 unsigned char *out_ptr=out;
375 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
382 b[i]=ctx->iv[i]^in_ptr[i];
384 gostcrypt(&(c->cctx),b,out_ptr);
385 memcpy(ctx->iv,out_ptr,8);
394 gostdecrypt(&(c->cctx),in_ptr,b);
397 out_ptr[i]=ctx->iv[i]^b[i];
399 memcpy(ctx->iv,in_ptr,8);
407 /* GOST encryption in CFB mode */
408 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
409 const unsigned char *in, size_t inl)
411 const unsigned char *in_ptr = in;
412 unsigned char *out_ptr = out;
415 /* process partial block if any */
417 for (j = ctx->num, i = 0; j < 8 && i < inl;
418 j++, i++, in_ptr++, out_ptr++) {
420 ctx->buf[j + 8] = *in_ptr;
421 *out_ptr = ctx->buf[j] ^ (*in_ptr);
423 ctx->buf[j + 8] = *out_ptr;
426 memcpy(ctx->iv, ctx->buf + 8, 8);
434 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
436 * block cipher current iv
438 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
440 * xor next block of input text with it and output it
446 memcpy(ctx->iv, in_ptr, 8);
447 for (j = 0; j < 8; j++) {
448 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
451 /* Next iv is next block of cipher text */
453 memcpy(ctx->iv, out_ptr, 8);
455 /* Process rest of buffer */
457 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
459 memcpy(ctx->buf + 8, in_ptr, inl - i);
460 for (j = 0; i < inl; j++, i++) {
461 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
465 memcpy(ctx->buf + 8, out_ptr, j);
472 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
473 const unsigned char *in, size_t inl)
475 const unsigned char *in_ptr = in;
476 unsigned char *out_ptr = out;
479 /* process partial block if any */
481 for (j = ctx->num, i = 0; j < 8 && i < inl;
482 j++, i++, in_ptr++, out_ptr++) {
483 *out_ptr = ctx->buf[j] ^ (*in_ptr);
493 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
495 * block cipher current iv
498 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
500 * xor next block of input text with it and output it
505 for (j = 0; j < 8; j++) {
506 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
509 /* Process rest of buffer */
511 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
512 for (j = 0; i < inl; j++, i++) {
513 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
522 /* Cleaning up of EVP_CIPHER_CTX */
523 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
525 gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
526 ctx->app_data = NULL;
530 /* Control function for gost cipher */
531 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
536 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
540 return gost_cipher_set_param(c, arg);
542 case EVP_CTRL_RAND_KEY:
544 if (RAND_bytes((unsigned char *)ptr, ctx->key_len) <= 0) {
545 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
550 case EVP_CTRL_PBE_PRF_NID:
552 const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
553 int nid = NID_id_tc26_hmac_gost_3411_2012_512;
556 if (!strcmp("md_gost12_256", params))
557 nid = NID_id_tc26_hmac_gost_3411_2012_256;
558 else if (!strcmp("md_gost12_512", params))
559 nid = NID_id_tc26_hmac_gost_3411_2012_512;
560 else if (!strcmp("md_gost94", params))
561 nid = NID_id_HMACGostR3411_94;
570 GOSTerr(GOST_F_GOST_CIPHER_CTL,
571 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
577 /* Set cipher parameters from ASN1 structure */
578 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
581 unsigned char *buf = NULL;
582 unsigned char *p = NULL;
583 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
584 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
585 ASN1_OCTET_STRING *os = NULL;
587 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
590 if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) {
591 GOST_CIPHER_PARAMS_free(gcp);
592 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
595 ASN1_OBJECT_free(gcp->enc_param_set);
596 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
598 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
599 p = buf = OPENSSL_malloc(len);
601 GOST_CIPHER_PARAMS_free(gcp);
602 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
605 i2d_GOST_CIPHER_PARAMS(gcp, &p);
606 GOST_CIPHER_PARAMS_free(gcp);
608 os = ASN1_OCTET_STRING_new();
610 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
612 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
617 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
621 /* Store parameters into ASN1 structure */
622 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
626 GOST_CIPHER_PARAMS *gcp = NULL;
628 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
631 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
635 p = params->value.sequence->data;
637 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
638 params->value.sequence->length);
640 len = gcp->iv->length;
641 if (len != ctx->cipher->iv_len) {
642 GOST_CIPHER_PARAMS_free(gcp);
643 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
647 nid = OBJ_obj2nid(gcp->enc_param_set);
648 if (nid == NID_undef) {
649 GOST_CIPHER_PARAMS_free(gcp);
650 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
651 GOST_R_INVALID_CIPHER_PARAM_OID);
655 if (!gost_cipher_set_param(c, nid)) {
656 GOST_CIPHER_PARAMS_free(gcp);
659 memcpy(ctx->oiv, gcp->iv->data, len);
661 GOST_CIPHER_PARAMS_free(gcp);
666 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
668 struct ossl_gost_imit_ctx *c = ctx->md_data;
669 memset(c->buffer, 0, sizeof(c->buffer));
670 memset(c->partial_block, 0, sizeof(c->partial_block));
675 gost_init(&(c->cctx), block);
679 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
681 return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
684 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
686 return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
689 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
690 const unsigned char *data)
692 unsigned char buffer[8];
694 * We are using local buffer for iv because CryptoPro doesn't interpret
695 * internal state of MAC algorithm as iv during keymeshing (but does
696 * initialize internal state from iv in key transport
698 assert(c->count % 8 == 0 && c->count <= 1024);
699 if (c->key_meshing && c->count == 1024) {
700 cryptopro_key_meshing(&(c->cctx), buffer);
702 mac_block(&(c->cctx), c->buffer, data);
703 c->count = c->count % 1024 + 8;
706 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
708 struct ossl_gost_imit_ctx *c = ctx->md_data;
709 const unsigned char *p = data;
710 size_t bytes = count, i;
712 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
716 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
717 c->partial_block[i] = *p;
720 mac_block_mesh(c, c->partial_block);
727 mac_block_mesh(c, p);
732 memcpy(c->partial_block, p, bytes);
734 c->bytes_left = bytes;
738 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
740 struct ossl_gost_imit_ctx *c = ctx->md_data;
742 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
745 if (c->count == 0 && c->bytes_left) {
746 unsigned char buffer[8];
747 memset(buffer, 0, 8);
748 gost_imit_update(ctx, buffer, 8);
752 for (i = c->bytes_left; i < 8; i++) {
753 c->partial_block[i] = 0;
755 mac_block_mesh(c, c->partial_block);
757 get_mac(c->buffer, 8 * c->dgst_size, md);
761 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
764 case EVP_MD_CTRL_KEY_LEN:
765 *((unsigned int *)(ptr)) = 32;
767 case EVP_MD_CTRL_SET_KEY:
770 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
774 gost_key(&(((struct ossl_gost_imit_ctx *)(ctx->md_data))->cctx),
776 ((struct ossl_gost_imit_ctx *)(ctx->md_data))->key_set = 1;
780 case EVP_MD_CTRL_MAC_LEN:
782 if (arg < 1 || arg > 8) {
783 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
786 struct ossl_gost_imit_ctx *c = ctx->md_data;
796 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
798 memcpy(to->md_data, from->md_data, sizeof(struct ossl_gost_imit_ctx));
802 /* Clean up imit ctx */
803 int gost_imit_cleanup(EVP_MD_CTX *ctx)
805 memset(ctx->md_data, 0, sizeof(struct ossl_gost_imit_ctx));