summaryrefslogtreecommitdiffstats
path: root/engines/ccgost/gost94_keyx.c
blob: a7cdb2a26d230c2031119e648eb35fda3a3c1972 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**********************************************************************
 *                             gost94_keyx.c                          *
 *             Copyright (c) 2005-2006 Cryptocom LTD                  *
 *         This file is distributed under the same license as OpenSSL *
 *                                                                    *
 *     Implements generation and parsing of GOST_KEY_TRANSPORT for    *
 *     			GOST R 34.10-94 algorithms                            *
 *																	  *
 *          Requires OpenSSL 0.9.9 for compilation                    *
 **********************************************************************/
#include <string.h>
#include <openssl/dh.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
#include <openssl/objects.h>

#include "gost89.h"
#include "gosthash.h"
#include "e_gost_err.h"
#include "gost_keywrap.h"
#include "gost_lcl.h"
/* Common functions for both 94 and 2001 key exchange schemes */
int decrypt_cryptocom_key(unsigned char *sess_key,int max_key_len,
	const unsigned char *crypted_key,int crypted_key_len, gost_ctx *ctx)
	{
	int i;
	int j;
	int blocks = crypted_key_len >>3;
	unsigned char gamma[8];
	if (max_key_len <crypted_key_len)
		{
		GOSTerr(GOST_F_DECRYPT_CRYPTOCOM_KEY,GOST_R_NOT_ENOUGH_SPACE_FOR_KEY);
		return 0;
		}	
	if ((crypted_key_len & 7) !=0) 
		{
		GOSTerr(GOST_F_DECRYPT_CRYPTOCOM_KEY,GOST_R_INVALID_ENCRYPTED_KEY_SIZE);
		return 0;
		}	
	for (i=blocks-1;i>0;i--) 
		{
		gostcrypt(ctx,crypted_key+(i-1)*8,gamma);
		for(j=0;j<8;j++) 
			{
			sess_key[i*8+j]=gamma[j]^crypted_key[i*8+j];
			}
		}	
	gostcrypt(ctx,sess_key+crypted_key_len-8,gamma);	
	for(j=0;j<8;j++) 
		{
		sess_key[j]=gamma[j]^crypted_key[j];
		}
	return 1;
	}
int encrypt_cryptocom_key(const unsigned char *sess_key,int key_len,
	unsigned char *crypted_key, gost_ctx *ctx)
	{
	int i;
	int j;
	unsigned char gamma[8];
	memcpy(gamma,sess_key+key_len-8,8);
	for (i=0;i<key_len;i+=8)
		{
		gostcrypt(ctx,gamma,gamma);
		for (j=0;j<8;j++)
			gamma[j]=crypted_key[i+j]=sess_key[i+j]^gamma[j];
		}
	return 1;
	}
/* Implementation of the Diffi-Hellman key agreement scheme based on
 * GOST-94 keys */

/* Computes Diffie-Hellman key and stores it into buffer in
 * little-endian byte order as expected by both versions of GOST 94
 * algorigthm
 */
static int compute_pair_key_le(unsigned char *pair_key,BIGNUM *pub_key,DH *dh) 
	{
	unsigned char be_key[128];
	int i,key_size;
	key_size=DH_compute_key(be_key,pub_key,dh);
	if (!key_size) return 0;
	memset(pair_key,0,128);
	for (i=0;i<key_size;i++)
		{
		pair_key[i]=be_key[key_size-1-i];
		}
	return key_size;	
	}	
/*
 * Computes 256 bit key exchange key for CryptoCom variation of GOST 94
 * algorithm
 */
static int make_gost_shared_key(DH *dh,EVP_PKEY *pubk,unsigned char *shared_key) 
	{
	unsigned char dh_key [128];
	int i;
	/* Compute key */
	memset(dh_key,0,128);
	if (!compute_pair_key_le(dh_key,((DSA *)EVP_PKEY_get0(pubk))->pub_key,dh)) return 0;	
	/* Fold it down to 256 bit */
	/* According to GOST  either 2^1020<p<2^1024 or 
	 * 2^509<p<2^512, so DH_size can be exactly 128 or exactly 64 only
	 */
	
	if (DH_size(dh)==128)
		{
		for (i=0;i<64;i++)
			{
			dh_key[i]^=dh_key[64+i];
			}
		}
	for (i=0;i<32;i++)
		{
		shared_key[i]=dh_key[i]^dh_key[32+i];
		}
	return 1;
	}

static DH *make_ephemeral_key(EVP_PKEY *pubk,BIGNUM *ephemeral_key)
	{
	DH *dh = DH_new();
	dh->g = BN_dup(pubk->pkey.dsa->g);
	dh->p = BN_dup(pubk->pkey.dsa->p);
	dh->priv_key = BN_dup(ephemeral_key);
	/* Generate ephemeral key pair */
	if (!DH_generate_key(dh))
		{
		DH_free(dh);
		return NULL;
		}	
	return dh;
	}	
/*
 * Computes 256 bit Key exchange key as specified in RFC 4357 
 */
static int make_cp_exchange_key(DH *dh,EVP_PKEY *pubk, unsigned char *shared_key)
	{
	unsigned char dh_key [128];
	gost_hash_ctx hash_ctx;
	memset(dh_key,0,128);
	if (!compute_pair_key_le(dh_key,((DSA *)(EVP_PKEY_get0(pubk)))->pub_key,dh)) return 0;
	init_gost_hash_ctx(&hash_ctx,&GostR3411_94_CryptoProParamSet);
	start_hash(&hash_ctx);
	hash_block(&hash_ctx,dh_key,128);
	finish_hash(&hash_ctx,shared_key);
	done_gost_hash_ctx(&hash_ctx);
	return 1;
	}

/* EVP_PKEY_METHOD callback encrypt for
 * GOST R 34.10-94 cryptopro modification
 */

int pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* key, size_t key_len ) 
	{
	GOST_KEY_TRANSPORT *gkt=NULL;
	DH *dh = NULL;
	unsigned char shared_key[32], ukm[8],crypted_key[44];
	const struct gost_cipher_info *param=get_encryption_params(NULL);
	EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(ctx);
	struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
	int size=-1;
	gost_ctx cctx;

	if (!(data->eph_seckey))
		{
		GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
			GOST_R_CTX_NOT_INITIALIZED_FOR_ENCRYPT);
		return -1;
		}	

	dh = make_ephemeral_key(pubk,gost_get_priv_key(data->eph_seckey));
	gost_init(&cctx,param->sblock);	
	make_cp_exchange_key(dh,pubk,shared_key);
	if (RAND_bytes(ukm,8)<=0)
		{
		GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
			GOST_R_RANDOM_GENERATOR_FAILURE);
		return -1;
		}	
	keyWrapCryptoPro(&cctx,shared_key,ukm,key,crypted_key);
	gkt = GOST_KEY_TRANSPORT_new();
	if (!gkt)
		{
		goto memerr;
		}	
	if(!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,
			ukm,8))
		{
		goto memerr;
		}	
	if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,crypted_key+40,4))
		{
		goto memerr;
		}
	if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,crypted_key+8,32))
		{
		goto memerr;
		}
	if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,data->eph_seckey))
		{
		GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
		goto err;
		}	
	ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
	gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
	*outlen = i2d_GOST_KEY_TRANSPORT(gkt,&out);
	if (!size)
		{
		GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,GOST_R_ERROR_PACKING_KEY_TRANSPORT_INFO);
		size=-1;
		}
	GOST_KEY_TRANSPORT_free(gkt);
	DH_free(dh);
	return 1;	
	memerr:
	GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
		GOST_R_MALLOC_FAILURE);
	er