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))
209 return &gost_cipher_list[4];
211 nid = OBJ_txt2nid(params);
212 if (nid == NID_undef) {
213 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
214 GOST_R_INVALID_CIPHER_PARAM_OID);
218 nid = OBJ_obj2nid(obj);
220 for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
222 if (!param->sblock) {
223 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
229 /* Sets cipher param from paramset NID. */
230 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
232 const struct gost_cipher_info *param;
234 get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
238 c->paramNID = param->nid;
239 c->key_meshing = param->key_meshing;
241 gost_init(&(c->cctx), param->sblock);
245 /* Initializes EVP_CIPHER_CTX by paramset NID */
246 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
247 const unsigned char *key,
248 const unsigned char *iv, int enc,
249 int paramNID, int mode)
251 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
252 if (ctx->app_data == NULL) {
253 if (!gost_cipher_set_param(c, paramNID))
255 ctx->app_data = ctx->cipher_data;
258 gost_key(&(c->cctx), key);
260 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
261 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
265 static int gost_cipher_init_cnt(EVP_CIPHER_CTX *ctx,
266 const unsigned char *key,
267 const unsigned char *iv,
268 gost_subst_block * block)
270 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
271 gost_init(&(c->cctx), block);
275 gost_key(&(c->cctx), key);
277 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
278 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
282 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
283 const unsigned char *iv, int enc)
285 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_CryptoProParamSetA);
288 static int gost_cipher_init_cp_12(EVP_CIPHER_CTX *ctx,
289 const unsigned char *key,
290 const unsigned char *iv, int enc)
292 return gost_cipher_init_cnt(ctx, key, iv, &Gost28147_TC26ParamSetZ);
295 /* Initializes EVP_CIPHER_CTX with default values */
296 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
297 const unsigned char *iv, int enc)
299 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
303 /* Initializes EVP_CIPHER_CTX with default values */
304 int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
305 const unsigned char *iv, int enc)
307 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
313 * Wrapper around gostcrypt function from gost89.c which perform key meshing
316 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
318 struct ossl_gost_cipher_ctx *c = ctx;
319 assert(c->count % 8 == 0 && c->count <= 1024);
320 if (c->key_meshing && c->count == 1024) {
321 cryptopro_key_meshing(&(c->cctx), iv);
323 gostcrypt(&(c->cctx), iv, buf);
324 c->count = c->count % 1024 + 8;
327 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
329 struct ossl_gost_cipher_ctx *c = ctx;
331 unsigned char buf1[8];
332 assert(c->count % 8 == 0 && c->count <= 1024);
333 if (c->key_meshing && c->count == 1024) {
334 cryptopro_key_meshing(&(c->cctx), iv);
337 gostcrypt(&(c->cctx), iv, buf1);
341 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
343 buf1[0] = (unsigned char)(g & 0xff);
344 buf1[1] = (unsigned char)((g >> 8) & 0xff);
345 buf1[2] = (unsigned char)((g >> 16) & 0xff);
346 buf1[3] = (unsigned char)((g >> 24) & 0xff);
347 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
350 if (go > g) /* overflow */
352 buf1[4] = (unsigned char)(g & 0xff);
353 buf1[5] = (unsigned char)((g >> 8) & 0xff);
354 buf1[6] = (unsigned char)((g >> 16) & 0xff);
355 buf1[7] = (unsigned char)((g >> 24) & 0xff);
357 gostcrypt(&(c->cctx), buf1, buf);
358 c->count = c->count % 1024 + 8;
361 /* GOST encryptoon in CBC mode */
362 int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
363 const unsigned char *in, size_t inl)
365 OPENSSL_assert(inl % 8 ==0);
367 const unsigned char *in_ptr=in;
368 unsigned char *out_ptr=out;
370 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
377 b[i]=ctx->iv[i]^in_ptr[i];
379 gostcrypt(&(c->cctx),b,out_ptr);
380 memcpy(ctx->iv,out_ptr,8);
389 gostdecrypt(&(c->cctx),in_ptr,b);
392 out_ptr[i]=ctx->iv[i]^b[i];
394 memcpy(ctx->iv,in_ptr,8);
402 /* GOST encryption in CFB mode */
403 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
404 const unsigned char *in, size_t inl)
406 const unsigned char *in_ptr = in;
407 unsigned char *out_ptr = out;
410 /* process partial block if any */
412 for (j = ctx->num, i = 0; j < 8 && i < inl;
413 j++, i++, in_ptr++, out_ptr++) {
415 ctx->buf[j + 8] = *in_ptr;
416 *out_ptr = ctx->buf[j] ^ (*in_ptr);
418 ctx->buf[j + 8] = *out_ptr;
421 memcpy(ctx->iv, ctx->buf + 8, 8);
429 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
431 * block cipher current iv
433 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
435 * xor next block of input text with it and output it
441 memcpy(ctx->iv, in_ptr, 8);
442 for (j = 0; j < 8; j++) {
443 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
446 /* Next iv is next block of cipher text */
448 memcpy(ctx->iv, out_ptr, 8);
450 /* Process rest of buffer */
452 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
454 memcpy(ctx->buf + 8, in_ptr, inl - i);
455 for (j = 0; i < inl; j++, i++) {
456 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
460 memcpy(ctx->buf + 8, out_ptr, j);
467 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
468 const unsigned char *in, size_t inl)
470 const unsigned char *in_ptr = in;
471 unsigned char *out_ptr = out;
474 /* process partial block if any */
476 for (j = ctx->num, i = 0; j < 8 && i < inl;
477 j++, i++, in_ptr++, out_ptr++) {
478 *out_ptr = ctx->buf[j] ^ (*in_ptr);
488 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
490 * block cipher current iv
493 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
495 * xor next block of input text with it and output it
500 for (j = 0; j < 8; j++) {
501 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
504 /* Process rest of buffer */
506 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
507 for (j = 0; i < inl; j++, i++) {
508 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
517 /* Cleaning up of EVP_CIPHER_CTX */
518 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
520 gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
521 ctx->app_data = NULL;
525 /* Control function for gost cipher */
526 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
531 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
535 return gost_cipher_set_param(c, arg);
537 case EVP_CTRL_RAND_KEY:
539 if (RAND_bytes((unsigned char *)ptr, ctx->key_len) <= 0) {
540 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
545 case EVP_CTRL_PBE_PRF_NID:
547 const char *params = get_gost_engine_param(GOST_PARAM_PBE_PARAMS);
548 int nid = NID_id_tc26_hmac_gost_3411_2012_512;
551 if (!strcmp("md_gost12_256", params))
552 nid = NID_id_tc26_hmac_gost_3411_2012_256;
553 else if (!strcmp("md_gost12_512", params))
554 nid = NID_id_tc26_hmac_gost_3411_2012_512;
555 else if (!strcmp("md_gost94", params))
556 nid = NID_id_HMACGostR3411_94;
565 GOSTerr(GOST_F_GOST_CIPHER_CTL,
566 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
572 /* Set cipher parameters from ASN1 structure */
573 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
576 unsigned char *buf = NULL;
577 unsigned char *p = NULL;
578 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
579 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
580 ASN1_OCTET_STRING *os = NULL;
582 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
585 if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) {
586 GOST_CIPHER_PARAMS_free(gcp);
587 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
590 ASN1_OBJECT_free(gcp->enc_param_set);
591 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
593 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
594 p = buf = OPENSSL_malloc(len);
596 GOST_CIPHER_PARAMS_free(gcp);
597 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
600 i2d_GOST_CIPHER_PARAMS(gcp, &p);
601 GOST_CIPHER_PARAMS_free(gcp);
603 os = ASN1_OCTET_STRING_new();
605 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
607 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
612 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
616 /* Store parameters into ASN1 structure */
617 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
621 GOST_CIPHER_PARAMS *gcp = NULL;
623 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
626 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
630 p = params->value.sequence->data;
632 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
633 params->value.sequence->length);
635 len = gcp->iv->length;
636 if (len != ctx->cipher->iv_len) {
637 GOST_CIPHER_PARAMS_free(gcp);
638 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
642 nid = OBJ_obj2nid(gcp->enc_param_set);
643 if (nid == NID_undef) {
644 GOST_CIPHER_PARAMS_free(gcp);
645 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
646 GOST_R_INVALID_CIPHER_PARAM_OID);
650 if (!gost_cipher_set_param(c, nid)) {
651 GOST_CIPHER_PARAMS_free(gcp);
654 memcpy(ctx->oiv, gcp->iv->data, len);
656 GOST_CIPHER_PARAMS_free(gcp);
661 static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
663 struct ossl_gost_imit_ctx *c = ctx->md_data;
664 memset(c->buffer, 0, sizeof(c->buffer));
665 memset(c->partial_block, 0, sizeof(c->partial_block));
670 gost_init(&(c->cctx), block);
674 static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
676 return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
679 static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
681 return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
684 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
685 const unsigned char *data)
687 unsigned char buffer[8];
689 * We are using local buffer for iv because CryptoPro doesn't interpret
690 * internal state of MAC algorithm as iv during keymeshing (but does
691 * initialize internal state from iv in key transport
693 assert(c->count % 8 == 0 && c->count <= 1024);
694 if (c->key_meshing && c->count == 1024) {
695 cryptopro_key_meshing(&(c->cctx), buffer);
697 mac_block(&(c->cctx), c->buffer, data);
698 c->count = c->count % 1024 + 8;
701 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
703 struct ossl_gost_imit_ctx *c = ctx->md_data;
704 const unsigned char *p = data;
705 size_t bytes = count, i;
707 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
711 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
712 c->partial_block[i] = *p;
715 mac_block_mesh(c, c->partial_block);
722 mac_block_mesh(c, p);
727 memcpy(c->partial_block, p, bytes);
729 c->bytes_left = bytes;
733 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
735 struct ossl_gost_imit_ctx *c = ctx->md_data;
737 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
740 if (c->count == 0 && c->bytes_left) {
741 unsigned char buffer[8];
742 memset(buffer, 0, 8);
743 gost_imit_update(ctx, buffer, 8);
747 for (i = c->bytes_left; i < 8; i++) {
748 c->partial_block[i] = 0;
750 mac_block_mesh(c, c->partial_block);
752 get_mac(c->buffer, 8 * c->dgst_size, md);
756 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
759 case EVP_MD_CTRL_KEY_LEN:
760 *((unsigned int *)(ptr)) = 32;
762 case EVP_MD_CTRL_SET_KEY:
765 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
769 gost_key(&(((struct ossl_gost_imit_ctx *)(ctx->md_data))->cctx),
771 ((struct ossl_gost_imit_ctx *)(ctx->md_data))->key_set = 1;
775 case EVP_MD_CTRL_MAC_LEN:
777 if (arg < 1 || arg > 8) {
778 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
781 struct ossl_gost_imit_ctx *c = ctx->md_data;
791 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
793 memcpy(to->md_data, from->md_data, sizeof(struct ossl_gost_imit_ctx));
797 /* Clean up imit ctx */
798 int gost_imit_cleanup(EVP_MD_CTX *ctx)
800 memset(ctx->md_data, 0, sizeof(struct ossl_gost_imit_ctx));