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_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
26 const unsigned char *iv, int enc);
27 /* Handles block of data in CFB mode */
28 static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
29 const unsigned char *in, size_t inl);
30 /* Handles block of data in CNT mode */
31 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
32 const unsigned char *in, size_t inl);
33 /* Cleanup function */
34 static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
35 /* set/get cipher parameters */
36 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params);
37 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params);
38 /* Control function */
39 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx,int type,int arg,void *ptr);
41 EVP_CIPHER cipher_gost =
47 EVP_CIPH_CFB_MODE| EVP_CIPH_NO_PADDING |
48 EVP_CIPH_CUSTOM_IV| EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
52 sizeof(struct ossl_gost_cipher_ctx),/* ctx_size */
53 gost89_set_asn1_parameters,
54 gost89_get_asn1_parameters,
59 EVP_CIPHER cipher_gost_cpacnt =
65 EVP_CIPH_OFB_MODE| EVP_CIPH_NO_PADDING |
66 EVP_CIPH_CUSTOM_IV| EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
70 sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
71 gost89_set_asn1_parameters,
72 gost89_get_asn1_parameters,
77 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
78 /* Init functions which set specific parameters */
79 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
80 /* process block of data */
81 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
82 /* Return computed value */
83 static int gost_imit_final(EVP_MD_CTX *ctx,unsigned char *md);
85 static int gost_imit_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from);
86 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
87 /* Control function, knows how to set MAC key.*/
88 static int gost_imit_ctrl(EVP_MD_CTX *ctx,int type, int arg, void *ptr);
90 EVP_MD imit_gost_cpa =
92 NID_id_Gost28147_89_MAC,
105 sizeof(struct ossl_gost_imit_ctx),
110 * Correspondence between gost parameter OIDs and substitution blocks
111 * NID field is filed by register_gost_NID function in engine.c
112 * upon engine initialization
115 struct gost_cipher_info gost_cipher_list[]=
117 /* NID */ /* Subst block */ /* Key meshing*/
118 /*{NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},*/
119 {NID_id_Gost28147_89_cc,&GostR3411_94_CryptoProParamSet,0},
120 {NID_id_Gost28147_89_CryptoPro_A_ParamSet,&Gost28147_CryptoProParamSetA,1},
121 {NID_id_Gost28147_89_CryptoPro_B_ParamSet,&Gost28147_CryptoProParamSetB,1},
122 {NID_id_Gost28147_89_CryptoPro_C_ParamSet,&Gost28147_CryptoProParamSetC,1},
123 {NID_id_Gost28147_89_CryptoPro_D_ParamSet,&Gost28147_CryptoProParamSetD,1},
124 {NID_id_Gost28147_89_TestParamSet,&Gost28147_TestParamSet,1},
128 /* get encryption parameters from crypto network settings
129 FIXME For now we use environment var CRYPT_PARAMS as place to
130 store these settings. Actually, it is better to use engine control command, read from configuration file to set them */
131 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
134 struct gost_cipher_info *param;
137 const char * params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
138 if (!params || !strlen(params))
139 return &gost_cipher_list[1];
141 nid = OBJ_txt2nid(params);
142 if (nid == NID_undef)
144 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
145 GOST_R_INVALID_CIPHER_PARAM_OID);
151 nid= OBJ_obj2nid(obj);
153 for (param=gost_cipher_list;param->sblock!=NULL && param->nid!=nid;
157 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,GOST_R_INVALID_CIPHER_PARAMS);
163 /* Sets cipher param from paramset NID. */
164 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c,int nid)
166 const struct gost_cipher_info *param;
167 param=get_encryption_params((nid==NID_undef?NULL:OBJ_nid2obj(nid)));
168 if (!param) return 0;
170 c->paramNID = param->nid;
171 c->key_meshing=param->key_meshing;
173 gost_init(&(c->cctx), param->sblock);
177 /* Initializes EVP_CIPHER_CTX by paramset NID */
178 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx, const unsigned char *key,
179 const unsigned char *iv, int enc, int paramNID,int mode)
181 struct ossl_gost_cipher_ctx *c=ctx->cipher_data;
182 if (ctx->app_data == NULL)
184 if (!gost_cipher_set_param(c,paramNID)) return 0;
185 ctx->app_data = ctx->cipher_data;
187 if (key) gost_key(&(c->cctx),key);
188 if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
189 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
193 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
194 const unsigned char *iv, int enc)
196 struct ossl_gost_cipher_ctx *c=ctx->cipher_data;
197 gost_init(&(c->cctx),&Gost28147_CryptoProParamSetA);
200 if(key) gost_key(&(c->cctx),key);
201 if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
202 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
206 /* Initializes EVP_CIPHER_CTX with default values */
207 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
208 const unsigned char *iv, int enc)
210 return gost_cipher_init_param(ctx,key,iv,enc,NID_undef,EVP_CIPH_CFB_MODE);
212 /* Wrapper around gostcrypt function from gost89.c which perform
213 * key meshing when nesseccary
215 static void gost_crypt_mesh (void *ctx,unsigned char *iv,unsigned char *buf)
217 struct ossl_gost_cipher_ctx *c = ctx;
218 assert(c->count%8 == 0 && c->count <= 1024);
219 if (c->key_meshing && c->count==1024)
221 cryptopro_key_meshing(&(c->cctx),iv);
223 gostcrypt(&(c->cctx),iv,buf);
224 c->count = c->count%1024 + 8;
227 static void gost_cnt_next (void *ctx, unsigned char *iv, unsigned char *buf)
229 struct ossl_gost_cipher_ctx *c = ctx;
231 unsigned char buf1[8];
232 assert(c->count%8 == 0 && c->count <= 1024);
233 if (c->key_meshing && c->count==1024)
235 cryptopro_key_meshing(&(c->cctx),iv);
239 gostcrypt(&(c->cctx),iv,buf1);
245 g = buf1[0]|(buf1[1]<<8)|(buf1[2]<<16)|((word32)buf1[3]<<24);
247 buf1[0]=(unsigned char)(g&0xff);
248 buf1[1]=(unsigned char)((g>>8)&0xff);
249 buf1[2]=(unsigned char)((g>>16)&0xff);
250 buf1[3]=(unsigned char)((g>>24)&0xff);
251 g = buf1[4]|(buf1[5]<<8)|(buf1[6]<<16)|((word32)buf1[7]<<24);
254 if (go > g) /* overflow*/
256 buf1[4]=(unsigned char)(g&0xff);
257 buf1[5]=(unsigned char)((g>>8)&0xff);
258 buf1[6]=(unsigned char)((g>>16)&0xff);
259 buf1[7]=(unsigned char)((g>>24)&0xff);
261 gostcrypt(&(c->cctx),buf1,buf);
262 c->count = c->count%1024 + 8;
265 /* GOST encryption in CFB mode */
266 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
267 const unsigned char *in, size_t inl)
269 const unsigned char *in_ptr=in;
270 unsigned char *out_ptr=out;
273 /* process partial block if any */
276 for (j=ctx->num,i=0;j<8 && i<inl;j++,i++,in_ptr++,out_ptr++)
278 if (!ctx->encrypt) ctx->buf[j+8]=*in_ptr;
279 *out_ptr=ctx->buf[j]^(*in_ptr);
280 if (ctx->encrypt) ctx->buf[j+8]=*out_ptr;
284 memcpy(ctx->iv,ctx->buf+8,8);
294 for (;i+8<inl;i+=8,in_ptr+=8,out_ptr+=8)
296 /*block cipher current iv */
297 gost_crypt_mesh(ctx->cipher_data,ctx->iv,ctx->buf);
298 /*xor next block of input text with it and output it*/
299 /*output this block */
300 if (!ctx->encrypt) memcpy(ctx->iv,in_ptr,8);
303 out_ptr[j]=ctx->buf[j]^in_ptr[j];
306 /* Next iv is next block of cipher text*/
307 if (ctx->encrypt) memcpy(ctx->iv,out_ptr,8);
309 /* Process rest of buffer */
312 gost_crypt_mesh(ctx->cipher_data,ctx->iv,ctx->buf);
313 if (!ctx->encrypt) memcpy(ctx->buf+8,in_ptr,inl-i);
314 for (j=0;i<inl;j++,i++)
316 out_ptr[j]=ctx->buf[j]^in_ptr[j];
319 if (ctx->encrypt) memcpy(ctx->buf+8,out_ptr,j);
328 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
329 const unsigned char *in, size_t inl)
331 const unsigned char *in_ptr=in;
332 unsigned char *out_ptr=out;
335 /* process partial block if any */
338 for (j=ctx->num,i=0;j<8 && i<inl;j++,i++,in_ptr++,out_ptr++)
340 *out_ptr=ctx->buf[j]^(*in_ptr);
353 for (;i+8<inl;i+=8,in_ptr+=8,out_ptr+=8)
355 /*block cipher current iv */
357 gost_cnt_next(ctx->cipher_data,ctx->iv,ctx->buf);
358 /*xor next block of input text with it and output it*/
359 /*output this block */
362 out_ptr[j]=ctx->buf[j]^in_ptr[j];
365 /* Process rest of buffer */
368 gost_cnt_next(ctx->cipher_data,ctx->iv,ctx->buf);
369 for (j=0;i<inl;j++,i++)
371 out_ptr[j]=ctx->buf[j]^in_ptr[j];
382 /* Cleaning up of EVP_CIPHER_CTX */
383 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
385 gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
386 ctx->app_data = NULL;
390 /* Control function for gost cipher */
391 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx,int type,int arg,void *ptr)
395 case EVP_CTRL_RAND_KEY:
397 if (RAND_bytes((unsigned char *)ptr,ctx->key_len)<=0)
399 GOSTerr(GOST_F_GOST_CIPHER_CTL,GOST_R_RANDOM_GENERATOR_ERROR);
404 case EVP_CTRL_PBE_PRF_NID:
406 *((int *)ptr)= NID_id_HMACGostR3411_94;
413 GOSTerr(GOST_F_GOST_CIPHER_CTL,GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
419 /* Set cipher parameters from ASN1 structure */
420 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params)
423 unsigned char *buf=NULL;
424 unsigned char *p=NULL;
425 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
426 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
427 ASN1_OCTET_STRING *os = NULL;
430 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
433 if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len))
435 GOST_CIPHER_PARAMS_free(gcp);
436 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
439 ASN1_OBJECT_free(gcp->enc_param_set);
440 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
442 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
443 p = buf = (unsigned char*)OPENSSL_malloc(len);
446 GOST_CIPHER_PARAMS_free(gcp);
447 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
450 i2d_GOST_CIPHER_PARAMS(gcp, &p);
451 GOST_CIPHER_PARAMS_free(gcp);
453 os = ASN1_OCTET_STRING_new();
455 if(!os || !ASN1_OCTET_STRING_set(os, buf, len))
458 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
463 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
467 /* Store parameters into ASN1 structure */
468 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params)
472 GOST_CIPHER_PARAMS *gcp = NULL;
474 struct ossl_gost_cipher_ctx *c=ctx->cipher_data;
475 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE)
480 p = params->value.sequence->data;
482 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
483 params->value.sequence->length);
485 len = gcp->iv->length;
486 if (len != ctx->cipher->iv_len)
488 GOST_CIPHER_PARAMS_free(gcp);
489 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
490 GOST_R_INVALID_IV_LENGTH);
493 if (!gost_cipher_set_param(c,OBJ_obj2nid(gcp->enc_param_set)))
495 GOST_CIPHER_PARAMS_free(gcp);
498 memcpy(ctx->oiv, gcp->iv->data, len);
500 GOST_CIPHER_PARAMS_free(gcp);
506 int gost_imit_init_cpa(EVP_MD_CTX *ctx)
508 struct ossl_gost_imit_ctx *c = ctx->md_data;
509 memset(c->buffer,0,sizeof(c->buffer));
510 memset(c->partial_block,0,sizeof(c->partial_block));
514 gost_init(&(c->cctx),&Gost28147_CryptoProParamSetA);
518 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,const unsigned char *data)
520 unsigned char buffer[8];
521 /* We are using local buffer for iv because CryptoPro doesn't
522 * interpret internal state of MAC algorithm as iv during keymeshing
523 * (but does initialize internal state from iv in key transport
525 assert(c->count%8 == 0 && c->count <= 1024);
526 if (c->key_meshing && c->count==1024)
528 cryptopro_key_meshing(&(c->cctx),buffer);
530 mac_block(&(c->cctx),c->buffer,data);
531 c->count = c->count%1024 + 8;
534 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
536 struct ossl_gost_imit_ctx *c = ctx->md_data;
537 const unsigned char *p = data;
538 size_t bytes = count,i;
540 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
545 for (i=c->bytes_left;i<8&&bytes>0;bytes--,i++,p++)
547 c->partial_block[i]=*p;
551 mac_block_mesh(c,c->partial_block);
567 memcpy(c->partial_block,p,bytes);
573 int gost_imit_final(EVP_MD_CTX *ctx,unsigned char *md)
575 struct ossl_gost_imit_ctx *c = ctx->md_data;
577 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
580 if (c->count==0 && c->bytes_left)
582 unsigned char buffer[8];
583 memset(buffer, 0, 8);
584 gost_imit_update(ctx, buffer, 8);
589 for (i=c->bytes_left;i<8;i++)
591 c->partial_block[i]=0;
593 mac_block_mesh(c,c->partial_block);
595 get_mac(c->buffer,32,md);
599 int gost_imit_ctrl(EVP_MD_CTX *ctx,int type, int arg, void *ptr)
603 case EVP_MD_CTRL_KEY_LEN:
604 *((unsigned int*)(ptr)) = 32;
606 case EVP_MD_CTRL_SET_KEY:
609 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
613 gost_key(&(((struct ossl_gost_imit_ctx*)(ctx->md_data))->cctx),ptr) ;
614 ((struct ossl_gost_imit_ctx*)(ctx->md_data))->key_set = 1;
623 int gost_imit_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
625 memcpy(to->md_data,from->md_data,sizeof(struct ossl_gost_imit_ctx));
629 /* Clean up imit ctx */
630 int gost_imit_cleanup(EVP_MD_CTX *ctx)
632 memset(ctx->md_data,0,sizeof(struct ossl_gost_imit_ctx));