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 **********************************************************************/
10 #include <openssl/crypto.h>
12 #include <openssl/rand.h>
13 #include "e_gost_err.h"
15 static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
16 const unsigned char *iv, int enc);
17 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
18 const unsigned char *iv, int enc);
19 /* Handles block of data in CFB mode */
20 static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
21 const unsigned char *in, size_t inl);
22 /* Handles block of data in CNT mode */
23 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
24 const unsigned char *in, size_t inl);
25 /* Cleanup function */
26 static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
27 /* set/get cipher parameters */
28 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params);
29 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params);
30 /* Control function */
31 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx,int type,int arg,void *ptr);
33 EVP_CIPHER cipher_gost =
39 EVP_CIPH_CFB_MODE| EVP_CIPH_NO_PADDING |
40 EVP_CIPH_CUSTOM_IV| EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
44 sizeof(struct ossl_gost_cipher_ctx),/* ctx_size */
45 gost89_set_asn1_parameters,
46 gost89_get_asn1_parameters,
51 EVP_CIPHER cipher_gost_cpacnt =
57 EVP_CIPH_OFB_MODE| EVP_CIPH_NO_PADDING |
58 EVP_CIPH_CUSTOM_IV| EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
62 sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
63 gost89_set_asn1_parameters,
64 gost89_get_asn1_parameters,
69 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
70 /* Init functions which set specific parameters */
71 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
72 /* process block of data */
73 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
74 /* Return computed value */
75 static int gost_imit_final(EVP_MD_CTX *ctx,unsigned char *md);
77 static int gost_imit_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from);
78 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
79 /* Control function, knows how to set MAC key.*/
80 static int gost_imit_ctrl(EVP_MD_CTX *ctx,int type, int arg, void *ptr);
82 EVP_MD imit_gost_cpa =
84 NID_id_Gost28147_89_MAC,
97 sizeof(struct ossl_gost_imit_ctx),
102 * Correspondence between gost parameter OIDs and substitution blocks
103 * NID field is filed by register_gost_NID function in engine.c
104 * upon engine initialization
107 struct gost_cipher_info gost_cipher_list[]=
109 /* NID */ /* Subst block */ /* Key meshing*/
110 /*{NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},*/
111 {NID_id_Gost28147_89_cc,&GostR3411_94_CryptoProParamSet,0},
112 {NID_id_Gost28147_89_CryptoPro_A_ParamSet,&Gost28147_CryptoProParamSetA,1},
113 {NID_id_Gost28147_89_CryptoPro_B_ParamSet,&Gost28147_CryptoProParamSetB,1},
114 {NID_id_Gost28147_89_CryptoPro_C_ParamSet,&Gost28147_CryptoProParamSetC,1},
115 {NID_id_Gost28147_89_CryptoPro_D_ParamSet,&Gost28147_CryptoProParamSetD,1},
116 {NID_id_Gost28147_89_TestParamSet,&Gost28147_TestParamSet,1},
120 /* get encryption parameters from crypto network settings
121 FIXME For now we use environment var CRYPT_PARAMS as place to
122 store these settings. Actually, it is better to use engine control command, read from configuration file to set them */
123 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
126 struct gost_cipher_info *param;
129 const char * params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
130 if (!params || !strlen(params))
131 return &gost_cipher_list[1];
133 nid = OBJ_txt2nid(params);
134 if (nid == NID_undef)
136 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
137 GOST_R_INVALID_CIPHER_PARAM_OID);
143 nid= OBJ_obj2nid(obj);
145 for (param=gost_cipher_list;param->sblock!=NULL && param->nid!=nid;
149 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,GOST_R_INVALID_CIPHER_PARAMS);
155 /* Sets cipher param from paramset NID. */
156 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c,int nid)
158 const struct gost_cipher_info *param;
159 param=get_encryption_params((nid==NID_undef?NULL:OBJ_nid2obj(nid)));
160 if (!param) return 0;
162 c->paramNID = param->nid;
163 c->key_meshing=param->key_meshing;
165 gost_init(&(c->cctx), param->sblock);
169 /* Initializes EVP_CIPHER_CTX by paramset NID */
170 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx, const unsigned char *key,
171 const unsigned char *iv, int enc, int paramNID,int mode)
173 struct ossl_gost_cipher_ctx *c=ctx->cipher_data;
174 if (ctx->app_data == NULL)
176 if (!gost_cipher_set_param(c,paramNID)) return 0;
177 ctx->app_data = ctx->cipher_data;
179 if (key) gost_key(&(c->cctx),key);
180 if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
181 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
185 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
186 const unsigned char *iv, int enc)
188 struct ossl_gost_cipher_ctx *c=ctx->cipher_data;
189 gost_init(&(c->cctx),&Gost28147_CryptoProParamSetA);
192 if(key) gost_key(&(c->cctx),key);
193 if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
194 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
198 /* Initializes EVP_CIPHER_CTX with default values */
199 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
200 const unsigned char *iv, int enc)
202 return gost_cipher_init_param(ctx,key,iv,enc,NID_undef,EVP_CIPH_CFB_MODE);
204 /* Wrapper around gostcrypt function from gost89.c which perform
205 * key meshing when nesseccary
207 static void gost_crypt_mesh (void *ctx,unsigned char *iv,unsigned char *buf)
209 struct ossl_gost_cipher_ctx *c = ctx;
210 if (c->count&&c->key_meshing && c->count%1024==0)
212 cryptopro_key_meshing(&(c->cctx),iv);
214 gostcrypt(&(c->cctx),iv,buf);
218 static void gost_cnt_next (void *ctx, unsigned char *iv, unsigned char *buf)
220 struct ossl_gost_cipher_ctx *c = ctx;
222 unsigned char buf1[8];
223 if (c->count && c->key_meshing && c->count %1024 ==0)
225 cryptopro_key_meshing(&(c->cctx),iv);
229 gostcrypt(&(c->cctx),iv,buf1);
235 g = buf1[0]|(buf1[1]<<8)|(buf1[2]<<16)|(buf1[3]<<24);
237 buf1[0]=(unsigned char)(g&0xff);
238 buf1[1]=(unsigned char)((g>>8)&0xff);
239 buf1[2]=(unsigned char)((g>>16)&0xff);
240 buf1[3]=(unsigned char)((g>>24)&0xff);
241 g = buf1[4]|(buf1[5]<<8)|(buf1[6]<<16)|(buf1[7]<<24);
244 if (go > g) /* overflow*/
246 buf1[4]=(unsigned char)(g&0xff);
247 buf1[5]=(unsigned char)((g>>8)&0xff);
248 buf1[6]=(unsigned char)((g>>16)&0xff);
249 buf1[7]=(unsigned char)((g>>24)&0xff);
251 gostcrypt(&(c->cctx),buf1,buf);
255 /* GOST encryption in CFB mode */
256 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
257 const unsigned char *in, size_t inl)
259 const unsigned char *in_ptr=in;
260 unsigned char *out_ptr=out;
263 /* process partial block if any */
266 for (j=ctx->num,i=0;j<8 && i<inl;j++,i++,in_ptr++,out_ptr++)
268 if (!ctx->encrypt) ctx->buf[j+8]=*in_ptr;
269 *out_ptr=ctx->buf[j]^(*in_ptr);
270 if (ctx->encrypt) ctx->buf[j+8]=*out_ptr;
274 memcpy(ctx->iv,ctx->buf+8,8);
284 for (;i+8<inl;i+=8,in_ptr+=8,out_ptr+=8)
286 /*block cipher current iv */
287 gost_crypt_mesh(ctx->cipher_data,ctx->iv,ctx->buf);
288 /*xor next block of input text with it and output it*/
289 /*output this block */
290 if (!ctx->encrypt) memcpy(ctx->iv,in_ptr,8);
293 out_ptr[j]=ctx->buf[j]^in_ptr[j];
296 /* Next iv is next block of cipher text*/
297 if (ctx->encrypt) memcpy(ctx->iv,out_ptr,8);
299 /* Process rest of buffer */
302 gost_crypt_mesh(ctx->cipher_data,ctx->iv,ctx->buf);
303 if (!ctx->encrypt) memcpy(ctx->buf+8,in_ptr,inl-i);
304 for (j=0;i<inl;j++,i++)
306 out_ptr[j]=ctx->buf[j]^in_ptr[j];
309 if (ctx->encrypt) memcpy(ctx->buf+8,out_ptr,j);
318 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
319 const unsigned char *in, size_t inl)
321 const unsigned char *in_ptr=in;
322 unsigned char *out_ptr=out;
325 /* process partial block if any */
328 for (j=ctx->num,i=0;j<8 && i<inl;j++,i++,in_ptr++,out_ptr++)
330 *out_ptr=ctx->buf[j]^(*in_ptr);
343 for (;i+8<inl;i+=8,in_ptr+=8,out_ptr+=8)
345 /*block cipher current iv */
347 gost_cnt_next(ctx->cipher_data,ctx->iv,ctx->buf);
348 /*xor next block of input text with it and output it*/
349 /*output this block */
352 out_ptr[j]=ctx->buf[j]^in_ptr[j];
355 /* Process rest of buffer */
358 gost_cnt_next(ctx->cipher_data,ctx->iv,ctx->buf);
359 for (j=0;i<inl;j++,i++)
361 out_ptr[j]=ctx->buf[j]^in_ptr[j];
372 /* Cleaning up of EVP_CIPHER_CTX */
373 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
375 gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
376 ctx->app_data = NULL;
380 /* Control function for gost cipher */
381 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx,int type,int arg,void *ptr)
385 case EVP_CTRL_RAND_KEY:
387 if (RAND_bytes((unsigned char *)ptr,ctx->key_len)<=0)
389 GOSTerr(GOST_F_GOST_CIPHER_CTL,GOST_R_RANDOM_GENERATOR_ERROR);
394 case EVP_CTRL_PBE_PRF_NID:
396 *((int *)ptr)= NID_id_HMACGostR3411_94;
403 GOSTerr(GOST_F_GOST_CIPHER_CTL,GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
409 /* Set cipher parameters from ASN1 structure */
410 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params)
413 unsigned char *buf=NULL;
414 unsigned char *p=NULL;
415 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
416 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
417 ASN1_OCTET_STRING *os = NULL;
420 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
423 if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len))
425 GOST_CIPHER_PARAMS_free(gcp);
426 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
429 ASN1_OBJECT_free(gcp->enc_param_set);
430 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
432 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
433 p = buf = (unsigned char*)OPENSSL_malloc(len);
436 GOST_CIPHER_PARAMS_free(gcp);
437 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
440 i2d_GOST_CIPHER_PARAMS(gcp, &p);
441 GOST_CIPHER_PARAMS_free(gcp);
443 os = ASN1_OCTET_STRING_new();
445 if(!os || !ASN1_OCTET_STRING_set(os, buf, len))
448 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
453 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
457 /* Store parameters into ASN1 structure */
458 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params)
462 GOST_CIPHER_PARAMS *gcp = NULL;
464 struct ossl_gost_cipher_ctx *c=ctx->cipher_data;
465 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE)
470 p = params->value.sequence->data;
472 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
473 params->value.sequence->length);
475 len = gcp->iv->length;
476 if (len != ctx->cipher->iv_len)
478 GOST_CIPHER_PARAMS_free(gcp);
479 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
480 GOST_R_INVALID_IV_LENGTH);
483 if (!gost_cipher_set_param(c,OBJ_obj2nid(gcp->enc_param_set)))
485 GOST_CIPHER_PARAMS_free(gcp);
488 memcpy(ctx->oiv, gcp->iv->data, len);
490 GOST_CIPHER_PARAMS_free(gcp);
496 int gost_imit_init_cpa(EVP_MD_CTX *ctx)
498 struct ossl_gost_imit_ctx *c = ctx->md_data;
499 memset(c->buffer,0,sizeof(c->buffer));
500 memset(c->partial_block,0,sizeof(c->partial_block));
504 gost_init(&(c->cctx),&Gost28147_CryptoProParamSetA);
508 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,const unsigned char *data)
510 unsigned char buffer[8];
511 /* We are using local buffer for iv because CryptoPro doesn't
512 * interpret internal state of MAC algorithm as iv during keymeshing
513 * (but does initialize internal state from iv in key transport
515 if (c->key_meshing&& c->count && c->count %1024 ==0)
517 cryptopro_key_meshing(&(c->cctx),buffer);
519 mac_block(&(c->cctx),c->buffer,data);
523 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
525 struct ossl_gost_imit_ctx *c = ctx->md_data;
526 const unsigned char *p = data;
527 size_t bytes = count,i;
529 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
534 for (i=c->bytes_left;i<8&&bytes>0;bytes--,i++,p++)
536 c->partial_block[i]=*p;
540 mac_block_mesh(c,c->partial_block);
556 memcpy(c->partial_block,p,bytes);
562 int gost_imit_final(EVP_MD_CTX *ctx,unsigned char *md)
564 struct ossl_gost_imit_ctx *c = ctx->md_data;
566 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
572 for (i=c->bytes_left;i<8;i++)
574 c->partial_block[i]=0;
576 mac_block_mesh(c,c->partial_block);
578 get_mac(c->buffer,32,md);
582 int gost_imit_ctrl(EVP_MD_CTX *ctx,int type, int arg, void *ptr)
586 case EVP_MD_CTRL_KEY_LEN:
587 *((unsigned int*)(ptr)) = 32;
589 case EVP_MD_CTRL_SET_KEY:
592 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
596 gost_key(&(((struct ossl_gost_imit_ctx*)(ctx->md_data))->cctx),ptr) ;
597 ((struct ossl_gost_imit_ctx*)(ctx->md_data))->key_set = 1;
606 int gost_imit_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
608 memcpy(to->md_data,from->md_data,sizeof(struct ossl_gost_imit_ctx));
612 /* Clean up imit ctx */
613 int gost_imit_cleanup(EVP_MD_CTX *ctx)
615 memset(ctx->md_data,0,sizeof(struct ossl_gost_imit_ctx));