gost2001_keyx.c 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**********************************************************************
 *                          gost_keyx.c                               *
 *             Copyright (c) 2005-2006 Cryptocom LTD                  *
 *         This file is distributed under the same license as OpenSSL *
 *                                                                    *
 *   VK0 34.10-2001 key exchange and GOST R 34.10-2001                *
 *   based PKCS7/SMIME support                                        *
 *          Requires OpenSSL 0.9.9 for compilation                    *
 **********************************************************************/
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <string.h>
#include <openssl/objects.h>
#include "gost89.h"
#include "gosthash.h"
#include "e_gost_err.h"
17 18
#include "gost_keywrap.h"
#include "gost_lcl.h"
B
Ben Laurie 已提交
19
#include "gost2001_keyx.h"
20 21 22

/* Transform ECDH shared key into little endian as required by Cryptocom
 * key exchange */
23 24
static void *make_key_le(const void *in, size_t inlen, void *out, size_t *outlen)
	{
25 26 27
	const char* inbuf= in;
	char* outbuf= out;
	int i;
28 29 30 31 32 33
	if (*outlen < inlen)
		{
		return NULL;
		}
	for (i=0;i<inlen;i++)
		{
34
		outbuf[inlen-1-i]=inbuf[i];
35
		}
36 37
	*outlen = inlen;
	return out;
38
	}	
39 40

/* Create gost 2001 ephemeral key with same parameters as peer key */
41 42
static EC_KEY *make_ec_ephemeral_key(EC_KEY *peer_key,BIGNUM *seckey)
	{
43 44 45 46 47
	EC_KEY *out = EC_KEY_new();
	EC_KEY_copy(out,peer_key);
	EC_KEY_set_private_key(out,seckey);
	gost2001_compute_public(out);
	return out;
48
	}
49 50 51 52
/* Packs GOST elliptic curve key into EVP_PKEY setting same parameters
 * as in passed pubkey
 */
static EVP_PKEY *ec_ephemeral_key_to_EVP(EVP_PKEY *pubk,int type,EC_KEY *ephemeral) 
53
	{
54 55 56 57
	EVP_PKEY *newkey;
	newkey = EVP_PKEY_new();
	EVP_PKEY_assign(newkey,type,ephemeral);
	return newkey;
58
	}	
59 60 61 62 63 64 65 66

/*  
 * EVP_PKEY_METHOD callback encrypt  
 * Implementation of GOST2001 key transport, cryptocom variation 
 */

int pkey_GOST01cc_encrypt (EVP_PKEY_CTX *pctx,unsigned char *out, 
	size_t *out_len, const unsigned char *key,size_t key_len)
67
	{
68 69 70 71 72 73 74 75
	EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
	struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);	
	GOST_KEY_TRANSPORT *gkt = NULL;
	int ret=0;
	gost_ctx ctx;
	EC_KEY *ephemeral=NULL;
	const EC_POINT *pub_key_point=NULL;
	unsigned char shared_key[32],encrypted_key[32],hmac[4],
76
		iv[8]={0,0,0,0,0,0,0,0};
77 78 79 80 81
	ephemeral = make_ec_ephemeral_key(EVP_PKEY_get0(pubk), gost_get_priv_key(data->eph_seckey));
	if (!ephemeral) goto err;
	/* compute shared key */
	pub_key_point=EC_KEY_get0_public_key(EVP_PKEY_get0(pubk));
	if (!ECDH_compute_key(shared_key,32,pub_key_point,ephemeral,make_key_le)) 
82
		{
83 84
		GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
		goto err;
85
		}	
86 87 88 89 90 91
	/* encrypt session key */
	gost_init(&ctx, &GostR3411_94_CryptoProParamSet);
	gost_key(&ctx,shared_key);
	encrypt_cryptocom_key(key,key_len,encrypted_key,&ctx);
	/* compute hmac of session key */
	if (!gost_mac(&ctx,32,key,32,hmac)) 
92
		{
93 94
		GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_ERROR_COMPUTING_MAC);
		return -1;
95
		}
96 97
	gkt = GOST_KEY_TRANSPORT_new();
	if (!gkt) 
98
		{
99 100
		GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_NO_MEMORY);
		return -1;
101
		}	
102 103
	/* Store IV which is always zero in our case */
	if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,iv,8))
104
		{
105 106
		GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_ERROR_STORING_IV);
		goto err;
107
		}	
108
	if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,hmac,4)) 
109
		{
110 111
		GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_ERROR_STORING_MAC);
		goto err;
112
		}	
113
	if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,encrypted_key,32))
114
		{	
115 116
		GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_ERROR_STORING_ENCRYPTED_KEY);
		goto err;
117
		}
118
	
119 120
	if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,data->eph_seckey))
		{
121 122
		GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
		goto err;
123
		}	
124 125 126 127
	ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
	gkt->key_agreement_info->cipher = OBJ_nid2obj(NID_id_Gost28147_89_cc);
	if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt,&out))>0) ret = 1;
	;
128
	err:
129 130
	if (gkt) GOST_KEY_TRANSPORT_free(gkt);
	return ret;
131
	}
132 133 134 135
/*  
 * EVP_PKEY_METHOD callback decrypt  
 * Implementation of GOST2001 key transport, cryptocom variation 
 */
136 137
int pkey_GOST01cc_decrypt (EVP_PKEY_CTX *pctx, unsigned char *key, size_t *key_len, const unsigned char *in, size_t in_len)
	{
138 139 140 141 142 143 144 145 146 147 148 149
	/* Form DH params from compute shared key */
	EVP_PKEY *priv=EVP_PKEY_CTX_get0_pkey(pctx);
	GOST_KEY_TRANSPORT *gkt = NULL;
	const unsigned char *p=in;
	unsigned char shared_key[32];
	unsigned char hmac[4],hmac_comp[4];
	unsigned char iv[8];
	int i;
	gost_ctx ctx;
	const EC_POINT *pub_key_point;
	EVP_PKEY *eph_key;

150 151
	if (!key)
		{
152 153
		*key_len = 32;
		return 1;
154
		}	
155 156
	/* Parse passed octet string and find out public key, iv and HMAC*/
	gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p,
157 158 159
		in_len);
	if (!gkt)
		{
160 161
		GOSTerr(GOST_F_PKEY_GOST01CC_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
		return 0;
162
		}	
163 164 165 166 167 168 169 170 171 172 173 174
	eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
	/* Initialization vector is really ignored here */
	OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8);
	memcpy(iv,gkt->key_agreement_info->eph_iv->data,8);
	/* HMAC should be computed and checked */
	OPENSSL_assert(gkt->key_info->imit->length==4);
	memcpy(hmac,gkt->key_info->imit->data,4);	
	/* Compute shared key */
	pub_key_point=EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key));
	i=ECDH_compute_key(shared_key,32,pub_key_point,EVP_PKEY_get0(priv),make_key_le);
	EVP_PKEY_free(eph_key);
	if (!i) 
175
		{
176 177 178
		GOSTerr(GOST_F_PKEY_GOST01CC_DECRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
		GOST_KEY_TRANSPORT_free(gkt);
		return 0;
179
		}
180 181 182 183 184 185
	/* Decrypt session key */
	gost_init(&ctx, &GostR3411_94_CryptoProParamSet);
	gost_key(&ctx,shared_key);
	
	if (!decrypt_cryptocom_key(key,*key_len,gkt->key_info->encrypted_key->data, 
			gkt->key_info->encrypted_key->length, &ctx)) 
186
		{
187 188
		GOST_KEY_TRANSPORT_free(gkt);
		return 0;
189
		}
190 191
	GOST_KEY_TRANSPORT_free(gkt);
	/* check HMAC of session key*/
192 193
	if (!gost_mac(&ctx,32,key,32,hmac_comp))
		{
194 195
		GOSTerr(GOST_F_PKEY_GOST01CC_DECRYPT,GOST_R_ERROR_COMPUTING_MAC);
		return 0;
196 197 198 199
		}
	/* HMAC of session key is not correct */
    if (memcmp(hmac,hmac_comp,4)!=0)
		{
200 201
		GOSTerr(GOST_F_PKEY_GOST01CC_DECRYPT,GOST_R_SESSION_KEY_MAC_DOES_NOT_MATCH);
		return 0;
202
		}	
203
	return 1; 
204
	}
205 206

/* Implementation of CryptoPro VKO 34.10-2001 algorithm */
207 208
static int VKO_compute_key(unsigned char *shared_key,size_t shared_key_size,const EC_POINT *pub_key,EC_KEY *priv_key,const unsigned char *ukm)
	{
209 210 211 212 213 214 215 216
	unsigned char ukm_be[8],databuf[64],hashbuf[64];
	BIGNUM *UKM=NULL,*p=NULL,*order=NULL,*X=NULL,*Y=NULL;
	const BIGNUM* key=EC_KEY_get0_private_key(priv_key);
	EC_POINT *pnt=EC_POINT_new(EC_KEY_get0_group(priv_key));
	int i;
	gost_hash_ctx hash_ctx;
	BN_CTX *ctx = BN_CTX_new();

217 218
	for (i=0;i<8;i++)
		{
219
		ukm_be[7-i]=ukm[i];
220
		}
221 222 223 224 225 226 227 228 229 230
	BN_CTX_start(ctx);
	UKM=getbnfrombuf(ukm_be,8);
	p=BN_CTX_get(ctx);
	order = BN_CTX_get(ctx);
	X=BN_CTX_get(ctx);
	Y=BN_CTX_get(ctx);
	EC_GROUP_get_order(EC_KEY_get0_group(priv_key),order,ctx);
	BN_mod_mul(p,key,UKM,order,ctx);	
	EC_POINT_mul(EC_KEY_get0_group(priv_key),pnt,NULL,pub_key,p,ctx);
	EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(priv_key),
231
		pnt,X,Y,ctx);
232 233 234 235 236
	/*Serialize elliptic curve point same way as we do it when saving
	 * key */
	store_bignum(Y,databuf,32);
	store_bignum(X,databuf+32,32);
 	/* And reverse byte order of whole buffer */
237 238
	for (i=0;i<64;i++)
		{
239
		hashbuf[63-i]=databuf[i];
240
		}
241 242 243 244 245 246 247 248 249 250
	init_gost_hash_ctx(&hash_ctx,&GostR3411_94_CryptoProParamSet);
	start_hash(&hash_ctx);
	hash_block(&hash_ctx,hashbuf,64);
	finish_hash(&hash_ctx,shared_key);
	done_gost_hash_ctx(&hash_ctx);
	BN_free(UKM);
	BN_CTX_end(ctx);
	BN_CTX_free(ctx);
	EC_POINT_free(pnt);
	return 32;
251
	}
252 253 254 255 256 257 258

/* Generates ephemeral key based on pubk algorithm
 * computes shared key using VKO and returns filled up
 * GOST_KEY_TRANSPORT structure
 */
/* Public, because it would be needed in SSL implementation */
GOST_KEY_TRANSPORT *make_rfc4490_keytransport_2001(EVP_PKEY *pubk,BIGNUM *eph_key,
259 260 261
	const unsigned char *key,size_t keylen, unsigned char *ukm,
	size_t ukm_len)
	{
262 263 264 265 266 267 268 269 270 271

	const struct gost_cipher_info *param=get_encryption_params(NULL);
	EC_KEY *ephemeral = NULL;
	GOST_KEY_TRANSPORT *gkt=NULL;
	const EC_POINT *pub_key_point = EC_KEY_get0_public_key(EVP_PKEY_get0(pubk));
	unsigned char shared_key[32],crypted_key[44];
	gost_ctx ctx;
	EVP_PKEY *newkey=NULL;
	
	/* Do not use vizir cipher parameters with cryptopro */
272 273
	if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS) && param ==  gost_cipher_list)
		{
274
		param= gost_cipher_list+1;
275
		}	
276 277 278 279 280
	ephemeral = make_ec_ephemeral_key(EVP_PKEY_get0(pubk),eph_key);
    VKO_compute_key(shared_key,32,pub_key_point,ephemeral,ukm);
	gost_init(&ctx,param->sblock);	
	keyWrapCryptoPro(&ctx,shared_key,ukm,key,crypted_key);
	gkt = GOST_KEY_TRANSPORT_new();
281 282
	if (!gkt)
		{
283
		goto memerr;
284
		}	
285
	if(!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,
286 287
			ukm,8))
		{
288
		goto memerr;
289 290 291
		}	
	if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,crypted_key+40,4))
		{
292
		goto memerr;
293 294 295
		}
	if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,crypted_key+8,32))
		{
296
		goto memerr;
297
		}
298
	newkey = ec_ephemeral_key_to_EVP(pubk,NID_id_GostR3410_2001,ephemeral);
299 300
	if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,newkey))
		{
301 302
		GOSTerr(GOST_F_MAKE_RFC4490_KEYTRANSPORT_2001,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
		goto err;
303
		}	
304 305 306 307
	ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
	gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
	EVP_PKEY_free(newkey);
	return gkt;
308 309 310 311 312 313 314
	memerr:
	GOSTerr(GOST_F_MAKE_RFC4490_KEYTRANSPORT_2001,
		GOST_R_MALLOC_FAILURE);
	err:		
	GOST_KEY_TRANSPORT_free(gkt);
	return NULL;
	}
315 316 317 318 319 320 321

/*  
 * EVP_PKEY_METHOD callback encrypt  
 * Implementation of GOST2001 key transport, cryptopo variation 
 */

int pkey_GOST01cp_encrypt (EVP_PKEY_CTX *pctx, unsigned char *out, size_t *out_len, const unsigned char *key,size_t key_len) 
322
	{
323 324 325 326 327
	GOST_KEY_TRANSPORT *gkt=NULL; 
	EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
	struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
	unsigned char ukm[8];
	int ret=0;
328 329
	if (RAND_bytes(ukm,8)<=0)
		{
330 331 332
		GOSTerr(GOST_F_PKEY_GOST01CP_ENCRYPT,
			GOST_R_RANDOM_GENERATOR_FAILURE);
		return 0;
333
		}	
334
		
335 336
	if (!(gkt=make_rfc4490_keytransport_2001(pubk,gost_get_priv_key(data->eph_seckey),key, key_len,ukm,8)))
		{
337
		goto err;
338
		}	
339 340 341
	if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt,&out))>0) ret =1;
	GOST_KEY_TRANSPORT_free(gkt);
	return ret;	
342 343 344 345
	err:		
	GOST_KEY_TRANSPORT_free(gkt);
	return -1;
	}
346 347
/* Public, because it would be needed in SSL implementation */
int decrypt_rfc4490_shared_key_2001(EVP_PKEY *priv,GOST_KEY_TRANSPORT *gkt,
348 349
	unsigned char *key_buf,int key_buf_len) 
	{
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	unsigned char wrappedKey[44];
	unsigned char sharedKey[32];
	gost_ctx ctx;
	const struct gost_cipher_info *param=NULL;
	EVP_PKEY *eph_key=NULL;
	
	eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
	param = get_encryption_params(gkt->key_agreement_info->cipher);
	gost_init(&ctx,param->sblock);	
	OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8);
	memcpy(wrappedKey,gkt->key_agreement_info->eph_iv->data,8);
	OPENSSL_assert(gkt->key_info->encrypted_key->length==32);
	memcpy(wrappedKey+8,gkt->key_info->encrypted_key->data,32);
	OPENSSL_assert(gkt->key_info->imit->length==4);
	memcpy(wrappedKey+40,gkt->key_info->imit->data,4);	
	VKO_compute_key(sharedKey,32,EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key)),
366 367 368
		EVP_PKEY_get0(priv),wrappedKey);
	if (!keyUnwrapCryptoPro(&ctx,sharedKey,wrappedKey,key_buf))
		{
369
		GOSTerr(GOST_F_PKCS7_GOST94CP_KEY_TRANSPORT_DECRYPT,
370
			GOST_R_ERROR_COMPUTING_SHARED_KEY);
371
		goto err;
372
		}	
373 374 375
				
	EVP_PKEY_free(eph_key);
	return 32;
376
	err:
377 378
	EVP_PKEY_free(eph_key);
	return -1;
379
	}
380 381 382 383
/*  
 * EVP_PKEY_METHOD callback decrypt  
 * Implementation of GOST2001 key transport, cryptopo variation 
 */
384 385
int pkey_GOST01cp_decrypt (EVP_PKEY_CTX *pctx, unsigned char *key, size_t * key_len, const unsigned char *in, size_t in_len)
	{
386 387 388 389 390
	const unsigned char *p = in;
	EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
	GOST_KEY_TRANSPORT *gkt = NULL;
	int ret=0;	

391 392
	if (!key)
		{
393 394
		*key_len = 32;
		return 1;
395
		}	
396
	gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p,
397 398 399
		in_len);
	if (!gkt)
		{
400 401
		GOSTerr(GOST_F_PKCS7_GOST94CP_KEY_TRANSPORT_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
		return -1;
402
		}	
403 404 405
	ret = 	decrypt_rfc4490_shared_key_2001(priv,gkt,key,*key_len);
	GOST_KEY_TRANSPORT_free(gkt);
	return ret;
406
	}