OpenSSL: update to 1.0.1m
[tomato.git] / release / src / router / openssl / engines / ccgost / gost_pmeth.c
blob9af7b06d43aa731a0c822922045c128876814a6f
1 /**********************************************************************
2 * gost_pmeth.c *
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
5 * *
6 * Implementation of RFC 4357 (GOST R 34.10) Publick key method *
7 * for OpenSSL *
8 * Requires OpenSSL 0.9.9 for compilation *
9 **********************************************************************/
10 #include <openssl/evp.h>
11 #include <openssl/objects.h>
12 #include <openssl/ec.h>
13 #include <openssl/x509v3.h> /* For string_to_hex */
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include "gost_params.h"
18 #include "gost_lcl.h"
19 #include "e_gost_err.h"
20 /* -----init, cleanup, copy - uniform for all algs ---------------*/
21 /* Allocates new gost_pmeth_data structure and assigns it as data */
22 static int pkey_gost_init(EVP_PKEY_CTX *ctx)
24 struct gost_pmeth_data *data;
25 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
26 data = OPENSSL_malloc(sizeof(struct gost_pmeth_data));
27 if (!data)
28 return 0;
29 memset(data, 0, sizeof(struct gost_pmeth_data));
30 if (pkey && EVP_PKEY_get0(pkey)) {
31 switch (EVP_PKEY_base_id(pkey)) {
32 case NID_id_GostR3410_94:
33 data->sign_param_nid = gost94_nid_by_params(EVP_PKEY_get0(pkey));
34 break;
35 case NID_id_GostR3410_2001:
36 data->sign_param_nid =
37 EC_GROUP_get_curve_name(EC_KEY_get0_group
38 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
39 break;
40 default:
41 return 0;
44 EVP_PKEY_CTX_set_data(ctx, data);
45 return 1;
48 /* Copies contents of gost_pmeth_data structure */
49 static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
51 struct gost_pmeth_data *dst_data, *src_data;
52 if (!pkey_gost_init(dst)) {
53 return 0;
55 src_data = EVP_PKEY_CTX_get_data(src);
56 dst_data = EVP_PKEY_CTX_get_data(dst);
57 *dst_data = *src_data;
58 if (src_data->shared_ukm) {
59 dst_data->shared_ukm = NULL;
61 return 1;
64 /* Frees up gost_pmeth_data structure */
65 static void pkey_gost_cleanup(EVP_PKEY_CTX *ctx)
67 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
68 if (data->shared_ukm)
69 OPENSSL_free(data->shared_ukm);
70 OPENSSL_free(data);
73 /* --------------------- control functions ------------------------------*/
74 static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
76 struct gost_pmeth_data *pctx =
77 (struct gost_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
78 switch (type) {
79 case EVP_PKEY_CTRL_MD:
81 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94) {
82 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
83 return 0;
85 pctx->md = (EVP_MD *)p2;
86 return 1;
88 break;
90 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
91 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
92 case EVP_PKEY_CTRL_PKCS7_SIGN:
93 case EVP_PKEY_CTRL_DIGESTINIT:
94 #ifndef OPENSSL_NO_CMS
95 case EVP_PKEY_CTRL_CMS_ENCRYPT:
96 case EVP_PKEY_CTRL_CMS_DECRYPT:
97 case EVP_PKEY_CTRL_CMS_SIGN:
98 #endif
99 return 1;
101 case EVP_PKEY_CTRL_GOST_PARAMSET:
102 pctx->sign_param_nid = (int)p1;
103 return 1;
104 case EVP_PKEY_CTRL_SET_IV:
105 pctx->shared_ukm = OPENSSL_malloc((int)p1);
106 memcpy(pctx->shared_ukm, p2, (int)p1);
107 return 1;
108 case EVP_PKEY_CTRL_PEER_KEY:
109 if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */
110 return 1;
111 if (p1 == 2) /* TLS: peer key used? */
112 return pctx->peer_key_used;
113 if (p1 == 3) /* TLS: peer key used! */
114 return (pctx->peer_key_used = 1);
115 return -2;
117 return -2;
120 static int pkey_gost_ctrl94_str(EVP_PKEY_CTX *ctx,
121 const char *type, const char *value)
123 int param_nid = 0;
124 if (!strcmp(type, param_ctrl_string)) {
125 if (!value) {
126 return 0;
128 if (strlen(value) == 1) {
129 switch (toupper((unsigned char)value[0])) {
130 case 'A':
131 param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
132 break;
133 case 'B':
134 param_nid = NID_id_GostR3410_94_CryptoPro_B_ParamSet;
135 break;
136 case 'C':
137 param_nid = NID_id_GostR3410_94_CryptoPro_C_ParamSet;
138 break;
139 case 'D':
140 param_nid = NID_id_GostR3410_94_CryptoPro_D_ParamSet;
141 break;
142 default:
143 return 0;
144 break;
146 } else if ((strlen(value) == 2)
147 && (toupper((unsigned char)value[0]) == 'X')) {
148 switch (toupper((unsigned char)value[1])) {
149 case 'A':
150 param_nid = NID_id_GostR3410_94_CryptoPro_XchA_ParamSet;
151 break;
152 case 'B':
153 param_nid = NID_id_GostR3410_94_CryptoPro_XchB_ParamSet;
154 break;
155 case 'C':
156 param_nid = NID_id_GostR3410_94_CryptoPro_XchC_ParamSet;
157 break;
158 default:
159 return 0;
160 break;
162 } else {
163 R3410_params *p = R3410_paramset;
164 param_nid = OBJ_txt2nid(value);
165 if (param_nid == NID_undef) {
166 return 0;
168 for (; p->nid != NID_undef; p++) {
169 if (p->nid == param_nid)
170 break;
172 if (p->nid == NID_undef) {
173 GOSTerr(GOST_F_PKEY_GOST_CTRL94_STR, GOST_R_INVALID_PARAMSET);
174 return 0;
178 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
179 param_nid, NULL);
181 return -2;
184 static int pkey_gost_ctrl01_str(EVP_PKEY_CTX *ctx,
185 const char *type, const char *value)
187 int param_nid = 0;
188 if (!strcmp(type, param_ctrl_string)) {
189 if (!value) {
190 return 0;
192 if (strlen(value) == 1) {
193 switch (toupper((unsigned char)value[0])) {
194 case 'A':
195 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
196 break;
197 case 'B':
198 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
199 break;
200 case 'C':
201 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
202 break;
203 case '0':
204 param_nid = NID_id_GostR3410_2001_TestParamSet;
205 break;
206 default:
207 return 0;
208 break;
210 } else if ((strlen(value) == 2)
211 && (toupper((unsigned char)value[0]) == 'X')) {
212 switch (toupper((unsigned char)value[1])) {
213 case 'A':
214 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
215 break;
216 case 'B':
217 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
218 break;
219 default:
220 return 0;
221 break;
223 } else {
224 R3410_2001_params *p = R3410_2001_paramset;
225 param_nid = OBJ_txt2nid(value);
226 if (param_nid == NID_undef) {
227 return 0;
229 for (; p->nid != NID_undef; p++) {
230 if (p->nid == param_nid)
231 break;
233 if (p->nid == NID_undef) {
234 GOSTerr(GOST_F_PKEY_GOST_CTRL01_STR, GOST_R_INVALID_PARAMSET);
235 return 0;
239 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
240 param_nid, NULL);
242 return -2;
245 /* --------------------- key generation --------------------------------*/
247 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx)
249 return 1;
252 static int pkey_gost94_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
254 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
255 DSA *dsa = NULL;
256 if (data->sign_param_nid == NID_undef) {
257 GOSTerr(GOST_F_PKEY_GOST94_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
258 return 0;
260 dsa = DSA_new();
261 if (!fill_GOST94_params(dsa, data->sign_param_nid)) {
262 DSA_free(dsa);
263 return 0;
265 EVP_PKEY_assign(pkey, NID_id_GostR3410_94, dsa);
266 return 1;
269 static int pkey_gost01_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
271 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
272 EC_KEY *ec = NULL;
274 if (data->sign_param_nid == NID_undef) {
275 GOSTerr(GOST_F_PKEY_GOST01_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
276 return 0;
278 if (!ec)
279 ec = EC_KEY_new();
280 if (!fill_GOST2001_params(ec, data->sign_param_nid)) {
281 EC_KEY_free(ec);
282 return 0;
284 EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec);
285 return 1;
288 /* Generates Gost_R3410_94_cp key */
289 static int pkey_gost94cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
291 DSA *dsa;
292 if (!pkey_gost94_paramgen(ctx, pkey))
293 return 0;
294 dsa = EVP_PKEY_get0(pkey);
295 gost_sign_keygen(dsa);
296 return 1;
299 /* Generates GOST_R3410 2001 key and assigns it using specified type */
300 static int pkey_gost01cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
302 EC_KEY *ec;
303 if (!pkey_gost01_paramgen(ctx, pkey))
304 return 0;
305 ec = EVP_PKEY_get0(pkey);
306 gost2001_keygen(ec);
307 return 1;
310 /* ----------- sign callbacks --------------------------------------*/
312 static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
313 size_t *siglen, const unsigned char *tbs,
314 size_t tbs_len)
316 DSA_SIG *unpacked_sig = NULL;
317 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
318 if (!siglen)
319 return 0;
320 if (!sig) {
321 *siglen = 64; /* better to check size of pkey->pkey.dsa-q */
322 return 1;
324 unpacked_sig = gost_do_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
325 if (!unpacked_sig) {
326 return 0;
328 return pack_sign_cp(unpacked_sig, 32, sig, siglen);
331 static int pkey_gost01_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
332 size_t *siglen, const unsigned char *tbs,
333 size_t tbs_len)
335 DSA_SIG *unpacked_sig = NULL;
336 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
337 if (!siglen)
338 return 0;
339 if (!sig) {
340 *siglen = 64; /* better to check size of curve order */
341 return 1;
343 unpacked_sig = gost2001_do_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
344 if (!unpacked_sig) {
345 return 0;
347 return pack_sign_cp(unpacked_sig, 32, sig, siglen);
350 /* ------------------- verify callbacks ---------------------------*/
352 static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
353 size_t siglen, const unsigned char *tbs,
354 size_t tbs_len)
356 int ok = 0;
357 EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
358 DSA_SIG *s = unpack_cp_signature(sig, siglen);
359 if (!s)
360 return 0;
361 if (pub_key)
362 ok = gost_do_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
363 DSA_SIG_free(s);
364 return ok;
367 static int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
368 size_t siglen, const unsigned char *tbs,
369 size_t tbs_len)
371 int ok = 0;
372 EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
373 DSA_SIG *s = unpack_cp_signature(sig, siglen);
374 if (!s)
375 return 0;
376 #ifdef DEBUG_SIGN
377 fprintf(stderr, "R=");
378 BN_print_fp(stderr, s->r);
379 fprintf(stderr, "\nS=");
380 BN_print_fp(stderr, s->s);
381 fprintf(stderr, "\n");
382 #endif
383 if (pub_key)
384 ok = gost2001_do_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
385 DSA_SIG_free(s);
386 return ok;
389 /* ------------- encrypt init -------------------------------------*/
390 /* Generates ephermeral key */
391 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
393 return 1;
396 /* --------------- Derive init ------------------------------------*/
397 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
399 return 1;
402 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
403 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
405 struct gost_mac_pmeth_data *data;
406 data = OPENSSL_malloc(sizeof(struct gost_mac_pmeth_data));
407 if (!data)
408 return 0;
409 memset(data, 0, sizeof(struct gost_mac_pmeth_data));
410 EVP_PKEY_CTX_set_data(ctx, data);
411 return 1;
414 static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
416 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
417 OPENSSL_free(data);
420 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
422 struct gost_mac_pmeth_data *dst_data, *src_data;
423 if (!pkey_gost_mac_init(dst)) {
424 return 0;
426 src_data = EVP_PKEY_CTX_get_data(src);
427 dst_data = EVP_PKEY_CTX_get_data(dst);
428 *dst_data = *src_data;
429 return 1;
432 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
434 struct gost_mac_pmeth_data *data =
435 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
437 switch (type) {
438 case EVP_PKEY_CTRL_MD:
440 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_Gost28147_89_MAC) {
441 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
442 GOST_R_INVALID_DIGEST_TYPE);
443 return 0;
445 data->md = (EVP_MD *)p2;
446 return 1;
448 break;
450 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
451 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
452 case EVP_PKEY_CTRL_PKCS7_SIGN:
453 return 1;
454 case EVP_PKEY_CTRL_SET_MAC_KEY:
455 if (p1 != 32) {
456 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
457 return 0;
460 memcpy(data->key, p2, 32);
461 data->key_set = 1;
462 return 1;
463 case EVP_PKEY_CTRL_DIGESTINIT:
465 EVP_MD_CTX *mctx = p2;
466 void *key;
467 if (!data->key_set) {
468 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
469 if (!pkey) {
470 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
471 GOST_R_MAC_KEY_NOT_SET);
472 return 0;
474 key = EVP_PKEY_get0(pkey);
475 if (!key) {
476 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
477 GOST_R_MAC_KEY_NOT_SET);
478 return 0;
480 } else {
481 key = &(data->key);
483 return mctx->digest->md_ctrl(mctx, EVP_MD_CTRL_SET_KEY, 32, key);
486 return -2;
489 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
490 const char *type, const char *value)
492 if (!strcmp(type, key_ctrl_string)) {
493 if (strlen(value) != 32) {
494 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
495 GOST_R_INVALID_MAC_KEY_LENGTH);
496 return 0;
498 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
499 32, (char *)value);
501 if (!strcmp(type, hexkey_ctrl_string)) {
502 long keylen;
503 int ret;
504 unsigned char *keybuf = string_to_hex(value, &keylen);
505 if (keylen != 32) {
506 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
507 GOST_R_INVALID_MAC_KEY_LENGTH);
508 OPENSSL_free(keybuf);
509 return 0;
511 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
512 OPENSSL_free(keybuf);
513 return ret;
516 return -2;
519 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
521 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
522 unsigned char *keydata;
523 if (!data->key_set) {
524 GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN, GOST_R_MAC_KEY_NOT_SET);
525 return 0;
527 keydata = OPENSSL_malloc(32);
528 memcpy(keydata, data->key, 32);
529 EVP_PKEY_assign(pkey, NID_id_Gost28147_89_MAC, keydata);
530 return 1;
533 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
535 return 1;
538 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
539 size_t *siglen, EVP_MD_CTX *mctx)
541 unsigned int tmpsiglen = *siglen; /* for platforms where
542 * sizeof(int)!=sizeof(size_t) */
543 int ret;
544 if (!sig) {
545 *siglen = 4;
546 return 1;
548 ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
549 *siglen = tmpsiglen;
550 return ret;
553 /* ----------------------------------------------------------------*/
554 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags)
556 *pmeth = EVP_PKEY_meth_new(id, flags);
557 if (!*pmeth)
558 return 0;
560 switch (id) {
561 case NID_id_GostR3410_94:
562 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_ctrl, pkey_gost_ctrl94_str);
563 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost94cp_keygen);
564 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign);
565 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify);
566 EVP_PKEY_meth_set_encrypt(*pmeth,
567 pkey_gost_encrypt_init,
568 pkey_GOST94cp_encrypt);
569 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt);
570 EVP_PKEY_meth_set_derive(*pmeth,
571 pkey_gost_derive_init, pkey_gost94_derive);
572 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
573 pkey_gost94_paramgen);
574 break;
575 case NID_id_GostR3410_2001:
576 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_ctrl, pkey_gost_ctrl01_str);
577 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign);
578 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify);
580 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen);
582 EVP_PKEY_meth_set_encrypt(*pmeth,
583 pkey_gost_encrypt_init,
584 pkey_GOST01cp_encrypt);
585 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt);
586 EVP_PKEY_meth_set_derive(*pmeth,
587 pkey_gost_derive_init, pkey_gost2001_derive);
588 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
589 pkey_gost01_paramgen);
590 break;
591 case NID_id_Gost28147_89_MAC:
592 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
593 pkey_gost_mac_ctrl_str);
594 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
595 pkey_gost_mac_signctx);
596 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen);
597 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
598 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
599 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
600 return 1;
601 default: /* Unsupported method */
602 return 0;
604 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
605 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
607 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
609 * FIXME derive etc...
612 return 1;