gost_pmeth.c 14.9 KB
Newer Older
1
/**********************************************************************
2
 *                          gost_pmeth.c                              *
3 4 5 6 7 8 9 10 11 12 13 14 15
 *             Copyright (c) 2005-2006 Cryptocom LTD                  *
 *         This file is distributed under the same license as OpenSSL *
 *                                                                    *
 *   Implementation of RFC 4357 (GOST R 34.10) Publick key method     *
 *       for OpenSSL                                                  *
 *          Requires OpenSSL 0.9.9 for compilation                    *
 **********************************************************************/
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/ec.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
16 17
#include "gost_params.h"
#include "gost_lcl.h"
18 19 20
#include "e_gost_err.h"
/*-------init, cleanup, copy - uniform for all algs  ---------------*/
/* Allocates new gost_pmeth_data structure and assigns it as data */
21 22
static int pkey_gost_init(EVP_PKEY_CTX *ctx)
	{
23 24 25 26 27 28
	struct gost_pmeth_data *data;
	data = OPENSSL_malloc(sizeof(struct gost_pmeth_data));
	if (!data) return 0;
	memset(data,0,sizeof(struct gost_pmeth_data));
	EVP_PKEY_CTX_set_data(ctx,data);
	return 1;
29 30
	}

31 32
/* Copies contents of gost_pmeth_data structure */
static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
33
	{
34
	struct gost_pmeth_data *dst_data,*src_data;
35 36
	if (!pkey_gost_init(dst))
		{
37
		return 0;
38
		}
39 40 41
	src_data = EVP_PKEY_CTX_get_data(src);
	dst_data = EVP_PKEY_CTX_get_data(dst);
	*dst_data = *src_data;
42 43
	if (src_data -> eph_seckey)
		{
44
		dst_data ->eph_seckey = NULL;
45
		}	
46
	return 1;
47 48
	}

49
/* Frees up gost_pmeth_data structure */
50 51
static void pkey_gost_cleanup (EVP_PKEY_CTX *ctx)
	{
52 53 54
	struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
	if (data->eph_seckey) EVP_PKEY_free(data->eph_seckey);
	OPENSSL_free(data);
55 56
	}	

57 58
/* --------------------- control functions  ------------------------------*/
static int pkey_gost_ctrl (EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
59
	{
60 61
	struct gost_pmeth_data *pctx = (struct gost_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);
	switch (type)
62
		{
63
		case EVP_PKEY_CTRL_MD:
64 65
		{
		if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94)
66
			{
67 68
			GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
			return 0;
69
			}
70 71 72 73
		pctx->md = (EVP_MD *)p2;
		return 1;
		}
		break;
74 75 76 77

		case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
		case EVP_PKEY_CTRL_PKCS7_DECRYPT:
		case EVP_PKEY_CTRL_PKCS7_SIGN:
78
			return 1;
79 80 81 82 83

		case EVP_PKEY_CTRL_GOST_PARAMSET:
			pctx->sign_param_nid = (int)p1;
			return 1;
			
84
		}
85
	return -2;
86
	}
87

88 89 90 91 92
static int pkey_gost_ctrl94cc_str(EVP_PKEY_CTX *ctx,
	const char *type, const char *value)
	{
	if(!strcmp(type, param_ctrl_string))
		{
93
		return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
94 95 96
			NID_id_GostR3410_94_CryptoPro_A_ParamSet,
			NULL);
		}
97
	return -2;
98
	}
99

100 101 102 103 104
static int pkey_gost_ctrl01cc_str(EVP_PKEY_CTX *ctx,
	const char *type, const char *value)
	{
	if(!strcmp(type, param_ctrl_string))
		{
105
		return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
106 107
			NID_id_GostR3410_2001_ParamSet_cc,NULL);
		}
108
	return -2;
109 110 111 112 113
	}

static int pkey_gost_ctrl94_str(EVP_PKEY_CTX *ctx,
	const char *type, const char *value)
	{
114
	int param_nid=0;
115 116 117 118
	if(!strcmp(type, param_ctrl_string))
		{
		if (!value)
			{
119
			return 0;
120 121 122 123 124
			}
		if (strlen(value) == 1)
			{
			switch(toupper(value[0]))
				{
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
				case 'A':
					param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
					break;
				case 'B':
					param_nid = NID_id_GostR3410_94_CryptoPro_B_ParamSet;
					break;
				case 'C':
					param_nid = NID_id_GostR3410_94_CryptoPro_C_ParamSet;
					break;
				case 'D':
					param_nid = NID_id_GostR3410_94_CryptoPro_D_ParamSet;
					break;
				default:
					return 0;
					break;
140
				}
141
			}
142 143 144 145
		else if ((strlen(value) == 2) && (toupper(value[0]) == 'X'))
			{
			switch (toupper(value[1]))
				{
146 147 148 149 150 151 152 153 154 155 156 157
				case 'A':
					param_nid = NID_id_GostR3410_94_CryptoPro_XchA_ParamSet;
					break;
				case 'B':
					param_nid = NID_id_GostR3410_94_CryptoPro_XchB_ParamSet;
					break;
				case 'C':
					param_nid = NID_id_GostR3410_94_CryptoPro_XchC_ParamSet;
					break;
				default:
					return 0;
					break;
158
				}
159
			}
160 161
		else
			{
162 163
			R3410_params *p = R3410_paramset;
			param_nid = OBJ_txt2nid(value);
164 165
			if (param_nid == NID_undef)
				{
166
				return 0;
167 168 169
				}
			for (;p->nid != NID_undef;p++)
				{
170
				if (p->nid == param_nid) break;
171 172 173
				}
			if (p->nid == NID_undef)
				{
174
				GOSTerr(GOST_F_PKEY_GOST_CTRL94_STR,
175
					GOST_R_INVALID_PARAMSET);
176
				return 0;
177
				}
178 179
			}

180 181 182
		return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
			param_nid, NULL);
		}
183
	return -2;
184
	}
185

186 187 188
static int pkey_gost_ctrl01_str(EVP_PKEY_CTX *ctx,
	const char *type, const char *value)
	{
189
	int param_nid=0;
190 191 192 193
	if(!strcmp(type, param_ctrl_string))
		{
		if (!value)
			{
194
			return 0;
195 196 197 198 199
			}
		if (strlen(value) == 1)
			{
			switch(toupper(value[0]))
				{
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
				case 'A':
					param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
					break;	
				case 'B':
					param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
					break;
				case 'C':
					param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
					break;
				case '0':
					param_nid = NID_id_GostR3410_2001_TestParamSet;
					break;
				default:
					return 0;
					break;
215
				}
216
			}
217 218 219 220
		else if ((strlen(value) == 2) && (toupper(value[0]) == 'X'))
			{
			switch (toupper(value[1]))
				{
221 222 223 224 225 226 227 228 229
				case 'A':
					param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
					break;
				case 'B':
					param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
					break;
				default:
					return 0;
					break;
230
				}
231
			}
232 233
		else
			{
234 235
			R3410_2001_params *p = R3410_2001_paramset;
			param_nid = OBJ_txt2nid(value);
236 237
			if (param_nid == NID_undef)
				{
238
				return 0;
239 240 241
				}
			for (;p->nid != NID_undef;p++)
				{
242
				if (p->nid == param_nid) break;
243 244 245
				}
			if (p->nid == NID_undef)
				{
246
				GOSTerr(GOST_F_PKEY_GOST_CTRL01_STR,
247
					GOST_R_INVALID_PARAMSET);
248
				return 0;
249
				}
250 251
			}

252 253 254
		return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
			param_nid, NULL);
		}
255
	return -2;
256 257
	}

258 259 260
/* --------------------- key generation  --------------------------------*/
/* Generates GOST 94 key and assigns it setting specified type */
static int pkey_gost94_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey,int type)
261
	{
262 263
	struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
	DSA *dsa=NULL;
264 265 266 267
	if (data->sign_param_nid == NID_undef)
		{
		if (type== NID_id_GostR3410_94_cc)
			{
268
			data->sign_param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
269 270 271
			}
		else
			{
272
			GOSTerr(GOST_F_PKEY_GOST94_KEYGEN,
273
				GOST_R_NO_PARAMETERS_SET);
274
			return 0;
275 276
			}	
		}
277
	dsa = DSA_new();
278 279
	if (!fill_GOST94_params(dsa,data->sign_param_nid))
		{
280 281
		DSA_free(dsa);
		return 0;
282
		}
283 284 285
	gost_sign_keygen(dsa);
	EVP_PKEY_assign(pkey,type,dsa);
	return 1;
286 287
	}

288
/* Generates Gost_R3410_94_cc key */
289 290
static int pkey_gost94cc_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
	{
291
	return pkey_gost94_keygen(ctx,pkey,NID_id_GostR3410_94_cc);
292
	}
293 294

/* Generates Gost_R3410_94_cp key */
295 296
static int pkey_gost94cp_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
	{
297
	return pkey_gost94_keygen(ctx,pkey,NID_id_GostR3410_94);
298 299
	}

300 301
/* Generates GOST_R3410 2001 key and assigns it using specified type */
static int pkey_gost01_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey,int type)
302
	{
303 304
	struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
	EC_KEY *ec=NULL;
305 306 307 308
	if (data->sign_param_nid == NID_undef)
		{
		if (type == NID_id_GostR3410_2001_cc)
			{
309
			data->sign_param_nid = NID_id_GostR3410_2001_ParamSet_cc;
310 311
			}
		else {	
312
			GOSTerr(GOST_F_PKEY_GOST01_KEYGEN,
313
				GOST_R_NO_PARAMETERS_SET);
314
			return 0;
315
			}
316 317
		}
	ec = EC_KEY_new();
318 319
	if (!fill_GOST2001_params(ec,data->sign_param_nid))
		{
320 321
		EC_KEY_free(ec);
		return 0;
322
		}
323 324 325 326
	gost2001_keygen(ec);

	EVP_PKEY_assign(pkey,type,ec);
	return 1;
327 328
	}

329
/* Generates GOST R3410 2001_cc key */
330 331
static int pkey_gost01cc_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
	{
332
	return pkey_gost01_keygen(ctx,pkey,NID_id_GostR3410_2001_cc);
333
	}
334 335

/* Generates GOST R3410 2001_cp key */
336 337
static int pkey_gost01cp_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
	{
338
	return pkey_gost01_keygen(ctx,pkey,NID_id_GostR3410_2001);
339 340
	}

341 342 343
/* ----------- sign callbacks --------------------------------------*/
static int pkey_gost94_cc_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
	const unsigned char *tbs, size_t tbs_len)
344
	{
345 346 347
	DSA_SIG *unpacked_sig=NULL;
	EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
	if (!siglen) return 0;
348 349
	if (!sig)
		{
350 351
		*siglen= 64; /* better to check size of pkey->pkey.dsa-q */
		return 1;
352
		}	
353
	unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
354 355
	if (!unpacked_sig)
		{
356
		return 0;
357 358
		}
	return pack_sign_cc(unpacked_sig,32,sig,siglen);
359 360 361 362
	}

static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
	const unsigned char *tbs, size_t tbs_len)
363
	{
364 365 366
	DSA_SIG *unpacked_sig=NULL;
	EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
	if (!siglen) return 0;
367 368
	if (!sig)
		{
369 370
		*siglen= 64; /* better to check size of pkey->pkey.dsa-q */
		return 1;
371
		}	
372
	unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
373 374
	if (!unpacked_sig)
		{
375
		return 0;
376
		}
377
	return pack_sign_cp(unpacked_sig,32,sig,siglen);
378 379
	}

380 381
static int pkey_gost01_cc_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
	const unsigned char *tbs, size_t tbs_len)
382
	{
383 384 385
	DSA_SIG *unpacked_sig=NULL;
	EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
	if (!siglen) return 0;
386 387
	if (!sig)
		{
388 389
		*siglen= 64; /* better to check size of curve order*/
		return 1;
390
		}	
391
	unpacked_sig = gost2001_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
392 393
	if (!unpacked_sig)
		{
394
		return 0;
395
		}
396
	return pack_sign_cc(unpacked_sig,32,sig,siglen);
397 398
	}

399 400
static int pkey_gost01_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
	const unsigned char *tbs, size_t tbs_len)
401
	{
402 403 404
	DSA_SIG *unpacked_sig=NULL;
	EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
	if (!siglen) return 0;
405 406
	if (!sig)
		{
407 408
		*siglen= 64; /* better to check size of curve order*/
		return 1;
409
		}	
410
	unpacked_sig = gost2001_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
411 412
	if (!unpacked_sig)
		{
413
		return 0;
414
		}
415
	return pack_sign_cp(unpacked_sig,32,sig,siglen);
416 417
	}

418 419 420
/* ------------------- verify callbacks ---------------------------*/
static int pkey_gost94_cc_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
	size_t siglen, const unsigned char *tbs, size_t tbs_len)
421
	{
422 423 424 425 426 427 428
	int ok = 0;
	EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
	DSA_SIG *s=unpack_cc_signature(sig,siglen);
	if (!s) return 0;
	if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
	DSA_SIG_free(s);
	return ok;
429
	}
430 431 432

static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
	size_t siglen, const unsigned char *tbs, size_t tbs_len)
433
	{
434 435 436 437 438 439 440
	int ok = 0;
	EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
	DSA_SIG *s=unpack_cp_signature(sig,siglen);
	if (!s) return 0;
	if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
	DSA_SIG_free(s);
	return ok;
441 442
	}

443 444
static int pkey_gost01_cc_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
	size_t siglen, const unsigned char *tbs, size_t tbs_len)
445
	{
446 447 448
	int ok = 0;
	EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
	DSA_SIG *s=unpack_cc_signature(sig,siglen);
449
#ifdef DEBUG_SIGN	
450 451 452 453 454
	fprintf(stderr,"R=");
	BN_print_fp(stderr,s->r);
	fprintf(stderr,"\nS=");
	BN_print_fp(stderr,s->s);
	fprintf(stderr,"\n");
455
#endif	
456 457 458 459
	if (!s) return 0;
	if (pub_key) ok = gost2001_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
	DSA_SIG_free(s);
	return ok;
460
	}
461 462 463

static int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
	size_t siglen, const unsigned char *tbs, size_t tbs_len)
464
	{
465 466 467 468
	int ok = 0;
	EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
	DSA_SIG *s=unpack_cp_signature(sig,siglen);
	if (!s) return 0;
469
#ifdef DEBUG_SIGN	
470 471 472 473 474
	fprintf(stderr,"R=");
	BN_print_fp(stderr,s->r);
	fprintf(stderr,"\nS=");
	BN_print_fp(stderr,s->s);
	fprintf(stderr,"\n");
475
#endif	
476 477 478
	if (pub_key) ok = gost2001_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
	DSA_SIG_free(s);
	return ok;
479 480
	}

481 482
/* ------------- encrypt init -------------------------------------*/
/* Generates ephermeral key */
483 484
static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
	{
485 486 487 488 489 490 491
	struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
	EVP_PKEY *eph_key = EVP_PKEY_new();
	EVP_PKEY *old_key =EVP_PKEY_CTX_get0_pkey(ctx);

	if (data->eph_seckey) EVP_PKEY_free(data->eph_seckey);
	EVP_PKEY_assign(eph_key,EVP_PKEY_base_id(old_key),NULL);
	if (!EVP_PKEY_copy_parameters(eph_key,old_key)) return 0;
492 493 494 495 496 497 498 499 500 501
	switch (EVP_PKEY_base_id(old_key))
		{
		case NID_id_GostR3410_2001:
		case NID_id_GostR3410_2001_cc:
			gost2001_keygen(EVP_PKEY_get0(eph_key));
			break;
		case NID_id_GostR3410_94:
		case NID_id_GostR3410_94_cc:
			gost_sign_keygen(EVP_PKEY_get0(eph_key));
			break;
502 503
	
			
504
		}
505 506 507 508

	
	data->eph_seckey=eph_key;
	return 1;
509 510
	}

511
/* ----------------------------------------------------------------*/
512 513
int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth,int flags)
	{
514 515 516
	*pmeth = EVP_PKEY_meth_new(id, flags);
	if (!*pmeth) return 0;

517 518
	switch (id)
		{
519
		case NID_id_GostR3410_94_cc:
520
			EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94cc_str);
521 522 523
			EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cc_keygen);
			EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cc_sign);
			EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cc_verify);
524
			EVP_PKEY_meth_set_encrypt(*pmeth,
525
				pkey_gost_encrypt_init, pkey_GOST94cc_encrypt);
526
			EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cc_decrypt);
527 528 529 530 531 532
			break;
		case NID_id_GostR3410_94:
			EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94_str);
			EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cp_keygen);
			EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign);
			EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify);
533
			EVP_PKEY_meth_set_encrypt(*pmeth,
534
				pkey_gost_encrypt_init, pkey_GOST94cp_encrypt);
535
			EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt);
536 537 538 539 540 541 542 543 544 545

	
			break;
		case NID_id_GostR3410_2001_cc:
			EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01cc_str);
			EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cc_sign);
			EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cc_verify);

			EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cc_keygen);

546
			EVP_PKEY_meth_set_encrypt(*pmeth,
547
				pkey_gost_encrypt_init, pkey_GOST01cc_encrypt);
548
			EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cc_decrypt);
549 550 551 552 553 554 555 556 557
			break;
			/* There is intentionally no break here */
		case NID_id_GostR3410_2001:
			EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01_str);
			EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign);
			EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify);

			EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen);

558
			EVP_PKEY_meth_set_encrypt(*pmeth,
559
				pkey_gost_encrypt_init, pkey_GOST01cp_encrypt);
560
			EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt);
561
			break;
562
		default: /*Unsupported method*/
563
			return 0;
564
		}
565 566 567 568
	EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
	EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);

	EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
569
	/*FIXME derive etc...*/
570 571
	
	return 1;
572
	}
573