]>
Commit | Line | Data |
---|---|---|
a04549cc | 1 | /********************************************************************** |
926c41bd | 2 | * gost_pmeth.c * |
a04549cc DSH |
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> | |
f50ffd10 | 13 | #include <openssl/err.h> |
0f113f3e | 14 | #include <openssl/x509v3.h> /* For string_to_hex */ |
a04549cc DSH |
15 | #include <stdlib.h> |
16 | #include <string.h> | |
17 | #include <ctype.h> | |
926c41bd DSH |
18 | #include "gost_params.h" |
19 | #include "gost_lcl.h" | |
a04549cc | 20 | #include "e_gost_err.h" |
0f113f3e | 21 | /* -----init, cleanup, copy - uniform for all algs ---------------*/ |
a04549cc | 22 | /* Allocates new gost_pmeth_data structure and assigns it as data */ |
926c41bd | 23 | static int pkey_gost_init(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
24 | { |
25 | struct gost_pmeth_data *data; | |
26 | EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx); | |
27 | data = OPENSSL_malloc(sizeof(struct gost_pmeth_data)); | |
28 | if (!data) | |
29 | return 0; | |
30 | memset(data, 0, sizeof(struct gost_pmeth_data)); | |
31 | if (pkey && EVP_PKEY_get0(pkey)) { | |
32 | switch (EVP_PKEY_base_id(pkey)) { | |
33 | case NID_id_GostR3410_94: | |
34 | data->sign_param_nid = gost94_nid_by_params(EVP_PKEY_get0(pkey)); | |
35 | break; | |
36 | case NID_id_GostR3410_2001: | |
37 | data->sign_param_nid = | |
38 | EC_GROUP_get_curve_name(EC_KEY_get0_group | |
39 | (EVP_PKEY_get0((EVP_PKEY *)pkey))); | |
40 | break; | |
41 | default: | |
42 | return 0; | |
43 | } | |
44 | } | |
45 | EVP_PKEY_CTX_set_data(ctx, data); | |
46 | return 1; | |
47 | } | |
926c41bd | 48 | |
a04549cc DSH |
49 | /* Copies contents of gost_pmeth_data structure */ |
50 | static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) | |
0f113f3e MC |
51 | { |
52 | struct gost_pmeth_data *dst_data, *src_data; | |
53 | if (!pkey_gost_init(dst)) { | |
54 | return 0; | |
55 | } | |
56 | src_data = EVP_PKEY_CTX_get_data(src); | |
57 | dst_data = EVP_PKEY_CTX_get_data(dst); | |
58 | *dst_data = *src_data; | |
59 | if (src_data->shared_ukm) { | |
60 | dst_data->shared_ukm = NULL; | |
61 | } | |
62 | return 1; | |
63 | } | |
926c41bd | 64 | |
a04549cc | 65 | /* Frees up gost_pmeth_data structure */ |
0f113f3e MC |
66 | static void pkey_gost_cleanup(EVP_PKEY_CTX *ctx) |
67 | { | |
68 | struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx); | |
69 | if (data->shared_ukm) | |
70 | OPENSSL_free(data->shared_ukm); | |
71 | OPENSSL_free(data); | |
72 | } | |
926c41bd | 73 | |
a04549cc | 74 | /* --------------------- control functions ------------------------------*/ |
92e2c81a | 75 | static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) |
0f113f3e MC |
76 | { |
77 | struct gost_pmeth_data *pctx = | |
78 | (struct gost_pmeth_data *)EVP_PKEY_CTX_get_data(ctx); | |
79 | switch (type) { | |
80 | case EVP_PKEY_CTRL_MD: | |
81 | { | |
82 | if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94) { | |
83 | GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE); | |
84 | return 0; | |
85 | } | |
86 | pctx->md = (EVP_MD *)p2; | |
87 | return 1; | |
88 | } | |
89 | break; | |
90 | ||
91 | case EVP_PKEY_CTRL_GET_MD: | |
92 | *(const EVP_MD **)p2 = pctx->md; | |
93 | return 1; | |
94 | ||
95 | case EVP_PKEY_CTRL_PKCS7_ENCRYPT: | |
96 | case EVP_PKEY_CTRL_PKCS7_DECRYPT: | |
97 | case EVP_PKEY_CTRL_PKCS7_SIGN: | |
98 | case EVP_PKEY_CTRL_DIGESTINIT: | |
99 | #ifndef OPENSSL_NO_CMS | |
100 | case EVP_PKEY_CTRL_CMS_ENCRYPT: | |
101 | case EVP_PKEY_CTRL_CMS_DECRYPT: | |
102 | case EVP_PKEY_CTRL_CMS_SIGN: | |
103 | #endif | |
104 | return 1; | |
105 | ||
106 | case EVP_PKEY_CTRL_GOST_PARAMSET: | |
107 | pctx->sign_param_nid = (int)p1; | |
108 | return 1; | |
109 | case EVP_PKEY_CTRL_SET_IV: | |
110 | pctx->shared_ukm = OPENSSL_malloc((int)p1); | |
111 | if (pctx->shared_ukm == NULL) { | |
112 | GOSTerr(GOST_F_PKEY_GOST_CTRL, ERR_R_MALLOC_FAILURE); | |
113 | return 0; | |
114 | } | |
115 | memcpy(pctx->shared_ukm, p2, (int)p1); | |
116 | return 1; | |
117 | case EVP_PKEY_CTRL_PEER_KEY: | |
118 | if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */ | |
119 | return 1; | |
120 | if (p1 == 2) /* TLS: peer key used? */ | |
121 | return pctx->peer_key_used; | |
122 | if (p1 == 3) /* TLS: peer key used! */ | |
123 | return (pctx->peer_key_used = 1); | |
124 | return -2; | |
125 | } | |
126 | return -2; | |
127 | } | |
926c41bd DSH |
128 | |
129 | static int pkey_gost_ctrl94_str(EVP_PKEY_CTX *ctx, | |
0f113f3e MC |
130 | const char *type, const char *value) |
131 | { | |
132 | int param_nid = 0; | |
133 | if (!strcmp(type, param_ctrl_string)) { | |
134 | if (!value) { | |
135 | return 0; | |
136 | } | |
137 | if (strlen(value) == 1) { | |
138 | switch (toupper((unsigned char)value[0])) { | |
139 | case 'A': | |
140 | param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet; | |
141 | break; | |
142 | case 'B': | |
143 | param_nid = NID_id_GostR3410_94_CryptoPro_B_ParamSet; | |
144 | break; | |
145 | case 'C': | |
146 | param_nid = NID_id_GostR3410_94_CryptoPro_C_ParamSet; | |
147 | break; | |
148 | case 'D': | |
149 | param_nid = NID_id_GostR3410_94_CryptoPro_D_ParamSet; | |
150 | break; | |
151 | default: | |
152 | return 0; | |
153 | break; | |
154 | } | |
155 | } else if ((strlen(value) == 2) | |
156 | && (toupper((unsigned char)value[0]) == 'X')) { | |
157 | switch (toupper((unsigned char)value[1])) { | |
158 | case 'A': | |
159 | param_nid = NID_id_GostR3410_94_CryptoPro_XchA_ParamSet; | |
160 | break; | |
161 | case 'B': | |
162 | param_nid = NID_id_GostR3410_94_CryptoPro_XchB_ParamSet; | |
163 | break; | |
164 | case 'C': | |
165 | param_nid = NID_id_GostR3410_94_CryptoPro_XchC_ParamSet; | |
166 | break; | |
167 | default: | |
168 | return 0; | |
169 | break; | |
170 | } | |
171 | } else { | |
172 | R3410_params *p = R3410_paramset; | |
173 | param_nid = OBJ_txt2nid(value); | |
174 | if (param_nid == NID_undef) { | |
175 | return 0; | |
176 | } | |
177 | for (; p->nid != NID_undef; p++) { | |
178 | if (p->nid == param_nid) | |
179 | break; | |
180 | } | |
181 | if (p->nid == NID_undef) { | |
182 | GOSTerr(GOST_F_PKEY_GOST_CTRL94_STR, GOST_R_INVALID_PARAMSET); | |
183 | return 0; | |
184 | } | |
185 | } | |
186 | ||
187 | return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, | |
188 | param_nid, NULL); | |
189 | } | |
190 | return -2; | |
191 | } | |
a04549cc | 192 | |
926c41bd | 193 | static int pkey_gost_ctrl01_str(EVP_PKEY_CTX *ctx, |
0f113f3e MC |
194 | const char *type, const char *value) |
195 | { | |
196 | int param_nid = 0; | |
197 | if (!strcmp(type, param_ctrl_string)) { | |
198 | if (!value) { | |
199 | return 0; | |
200 | } | |
201 | if (strlen(value) == 1) { | |
202 | switch (toupper((unsigned char)value[0])) { | |
203 | case 'A': | |
204 | param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet; | |
205 | break; | |
206 | case 'B': | |
207 | param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet; | |
208 | break; | |
209 | case 'C': | |
210 | param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet; | |
211 | break; | |
212 | case '0': | |
213 | param_nid = NID_id_GostR3410_2001_TestParamSet; | |
214 | break; | |
215 | default: | |
216 | return 0; | |
217 | break; | |
218 | } | |
219 | } else if ((strlen(value) == 2) | |
220 | && (toupper((unsigned char)value[0]) == 'X')) { | |
221 | switch (toupper((unsigned char)value[1])) { | |
222 | case 'A': | |
223 | param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet; | |
224 | break; | |
225 | case 'B': | |
226 | param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet; | |
227 | break; | |
228 | default: | |
229 | return 0; | |
230 | break; | |
231 | } | |
232 | } else { | |
233 | R3410_2001_params *p = R3410_2001_paramset; | |
234 | param_nid = OBJ_txt2nid(value); | |
235 | if (param_nid == NID_undef) { | |
236 | return 0; | |
237 | } | |
238 | for (; p->nid != NID_undef; p++) { | |
239 | if (p->nid == param_nid) | |
240 | break; | |
241 | } | |
242 | if (p->nid == NID_undef) { | |
243 | GOSTerr(GOST_F_PKEY_GOST_CTRL01_STR, GOST_R_INVALID_PARAMSET); | |
244 | return 0; | |
245 | } | |
246 | } | |
247 | ||
248 | return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, | |
249 | param_nid, NULL); | |
250 | } | |
251 | return -2; | |
252 | } | |
926c41bd | 253 | |
a04549cc | 254 | /* --------------------- key generation --------------------------------*/ |
0e1dba93 | 255 | |
0f113f3e MC |
256 | static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx) |
257 | { | |
258 | return 1; | |
259 | } | |
260 | ||
261 | static int pkey_gost94_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | |
262 | { | |
263 | struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx); | |
264 | DSA *dsa = NULL; | |
265 | if (data->sign_param_nid == NID_undef) { | |
266 | GOSTerr(GOST_F_PKEY_GOST94_PARAMGEN, GOST_R_NO_PARAMETERS_SET); | |
267 | return 0; | |
268 | } | |
269 | dsa = DSA_new(); | |
270 | if (!fill_GOST94_params(dsa, data->sign_param_nid)) { | |
271 | DSA_free(dsa); | |
272 | return 0; | |
273 | } | |
274 | EVP_PKEY_assign(pkey, NID_id_GostR3410_94, dsa); | |
275 | return 1; | |
276 | } | |
277 | ||
b6af2c7e | 278 | static int pkey_gost01_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) |
0f113f3e MC |
279 | { |
280 | struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx); | |
281 | EC_KEY *ec = NULL; | |
282 | ||
283 | if (data->sign_param_nid == NID_undef) { | |
284 | GOSTerr(GOST_F_PKEY_GOST01_PARAMGEN, GOST_R_NO_PARAMETERS_SET); | |
285 | return 0; | |
286 | } | |
287 | if (!ec) | |
288 | ec = EC_KEY_new(); | |
289 | if (!fill_GOST2001_params(ec, data->sign_param_nid)) { | |
290 | EC_KEY_free(ec); | |
291 | return 0; | |
292 | } | |
293 | EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec); | |
294 | return 1; | |
295 | } | |
926c41bd | 296 | |
b6af2c7e DSH |
297 | /* Generates Gost_R3410_94_cp key */ |
298 | static int pkey_gost94cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | |
0f113f3e MC |
299 | { |
300 | DSA *dsa; | |
301 | if (!pkey_gost94_paramgen(ctx, pkey)) | |
302 | return 0; | |
303 | dsa = EVP_PKEY_get0(pkey); | |
304 | gost_sign_keygen(dsa); | |
305 | return 1; | |
306 | } | |
b6af2c7e DSH |
307 | |
308 | /* Generates GOST_R3410 2001 key and assigns it using specified type */ | |
309 | static int pkey_gost01cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | |
0f113f3e MC |
310 | { |
311 | EC_KEY *ec; | |
312 | if (!pkey_gost01_paramgen(ctx, pkey)) | |
313 | return 0; | |
314 | ec = EVP_PKEY_get0(pkey); | |
315 | gost2001_keygen(ec); | |
316 | return 1; | |
317 | } | |
926c41bd | 318 | |
a04549cc | 319 | /* ----------- sign callbacks --------------------------------------*/ |
a04549cc | 320 | |
0f113f3e MC |
321 | static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, |
322 | size_t *siglen, const unsigned char *tbs, | |
323 | size_t tbs_len) | |
324 | { | |
325 | DSA_SIG *unpacked_sig = NULL; | |
326 | EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx); | |
327 | if (!siglen) | |
328 | return 0; | |
329 | if (!sig) { | |
330 | *siglen = 64; /* better to check size of pkey->pkey.dsa-q */ | |
331 | return 1; | |
332 | } | |
333 | unpacked_sig = gost_do_sign(tbs, tbs_len, EVP_PKEY_get0(pkey)); | |
334 | if (!unpacked_sig) { | |
335 | return 0; | |
336 | } | |
337 | return pack_sign_cp(unpacked_sig, 32, sig, siglen); | |
338 | } | |
339 | ||
340 | static int pkey_gost01_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, | |
341 | size_t *siglen, const unsigned char *tbs, | |
342 | size_t tbs_len) | |
343 | { | |
344 | DSA_SIG *unpacked_sig = NULL; | |
345 | EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx); | |
346 | if (!siglen) | |
347 | return 0; | |
348 | if (!sig) { | |
349 | *siglen = 64; /* better to check size of curve order */ | |
350 | return 1; | |
351 | } | |
352 | unpacked_sig = gost2001_do_sign(tbs, tbs_len, EVP_PKEY_get0(pkey)); | |
353 | if (!unpacked_sig) { | |
354 | return 0; | |
355 | } | |
356 | return pack_sign_cp(unpacked_sig, 32, sig, siglen); | |
357 | } | |
926c41bd | 358 | |
a04549cc | 359 | /* ------------------- verify callbacks ---------------------------*/ |
a04549cc DSH |
360 | |
361 | static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, | |
0f113f3e MC |
362 | size_t siglen, const unsigned char *tbs, |
363 | size_t tbs_len) | |
364 | { | |
365 | int ok = 0; | |
366 | EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx); | |
367 | DSA_SIG *s = unpack_cp_signature(sig, siglen); | |
368 | if (!s) | |
369 | return 0; | |
370 | if (pub_key) | |
371 | ok = gost_do_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key)); | |
372 | DSA_SIG_free(s); | |
373 | return ok; | |
374 | } | |
a04549cc DSH |
375 | |
376 | static int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, | |
0f113f3e MC |
377 | size_t siglen, const unsigned char *tbs, |
378 | size_t tbs_len) | |
379 | { | |
380 | int ok = 0; | |
381 | EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx); | |
382 | DSA_SIG *s = unpack_cp_signature(sig, siglen); | |
383 | if (!s) | |
384 | return 0; | |
385 | #ifdef DEBUG_SIGN | |
386 | fprintf(stderr, "R="); | |
387 | BN_print_fp(stderr, s->r); | |
388 | fprintf(stderr, "\nS="); | |
389 | BN_print_fp(stderr, s->s); | |
390 | fprintf(stderr, "\n"); | |
391 | #endif | |
392 | if (pub_key) | |
393 | ok = gost2001_do_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key)); | |
394 | DSA_SIG_free(s); | |
395 | return ok; | |
396 | } | |
926c41bd | 397 | |
a04549cc DSH |
398 | /* ------------- encrypt init -------------------------------------*/ |
399 | /* Generates ephermeral key */ | |
926c41bd | 400 | static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
401 | { |
402 | return 1; | |
403 | } | |
404 | ||
0e1dba93 | 405 | /* --------------- Derive init ------------------------------------*/ |
10f0c85c | 406 | static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx) |
0e1dba93 | 407 | { |
0f113f3e | 408 | return 1; |
0e1dba93 | 409 | } |
0f113f3e | 410 | |
a4346646 DSH |
411 | /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/ |
412 | static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
413 | { |
414 | struct gost_mac_pmeth_data *data; | |
415 | data = OPENSSL_malloc(sizeof(struct gost_mac_pmeth_data)); | |
416 | if (!data) | |
417 | return 0; | |
418 | memset(data, 0, sizeof(struct gost_mac_pmeth_data)); | |
419 | EVP_PKEY_CTX_set_data(ctx, data); | |
420 | return 1; | |
421 | } | |
422 | ||
423 | static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx) | |
424 | { | |
425 | struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx); | |
426 | OPENSSL_free(data); | |
427 | } | |
428 | ||
a4346646 | 429 | static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) |
0f113f3e MC |
430 | { |
431 | struct gost_mac_pmeth_data *dst_data, *src_data; | |
432 | if (!pkey_gost_mac_init(dst)) { | |
433 | return 0; | |
434 | } | |
435 | src_data = EVP_PKEY_CTX_get_data(src); | |
436 | dst_data = EVP_PKEY_CTX_get_data(dst); | |
437 | *dst_data = *src_data; | |
438 | return 1; | |
439 | } | |
440 | ||
92e2c81a | 441 | static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) |
0f113f3e MC |
442 | { |
443 | struct gost_mac_pmeth_data *data = | |
444 | (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx); | |
445 | ||
446 | switch (type) { | |
447 | case EVP_PKEY_CTRL_MD: | |
448 | { | |
449 | if (EVP_MD_type((const EVP_MD *)p2) != NID_id_Gost28147_89_MAC) { | |
450 | GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, | |
451 | GOST_R_INVALID_DIGEST_TYPE); | |
452 | return 0; | |
453 | } | |
454 | data->md = (EVP_MD *)p2; | |
455 | return 1; | |
456 | } | |
457 | break; | |
458 | ||
459 | case EVP_PKEY_CTRL_GET_MD: | |
460 | *(const EVP_MD **)p2 = data->md; | |
461 | return 1; | |
462 | ||
463 | case EVP_PKEY_CTRL_PKCS7_ENCRYPT: | |
464 | case EVP_PKEY_CTRL_PKCS7_DECRYPT: | |
465 | case EVP_PKEY_CTRL_PKCS7_SIGN: | |
466 | return 1; | |
467 | case EVP_PKEY_CTRL_SET_MAC_KEY: | |
468 | if (p1 != 32) { | |
469 | GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH); | |
470 | return 0; | |
471 | } | |
472 | ||
473 | memcpy(data->key, p2, 32); | |
474 | data->key_set = 1; | |
475 | return 1; | |
476 | case EVP_PKEY_CTRL_DIGESTINIT: | |
477 | { | |
478 | EVP_MD_CTX *mctx = p2; | |
479 | void *key; | |
480 | if (!data->key_set) { | |
481 | EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx); | |
482 | if (!pkey) { | |
483 | GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, | |
484 | GOST_R_MAC_KEY_NOT_SET); | |
485 | return 0; | |
486 | } | |
487 | key = EVP_PKEY_get0(pkey); | |
488 | if (!key) { | |
489 | GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, | |
490 | GOST_R_MAC_KEY_NOT_SET); | |
491 | return 0; | |
492 | } | |
493 | } else { | |
494 | key = &(data->key); | |
495 | } | |
496 | return mctx->digest->md_ctrl(mctx, EVP_MD_CTRL_SET_KEY, 32, key); | |
497 | } | |
498 | } | |
499 | return -2; | |
500 | } | |
501 | ||
a4346646 | 502 | static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx, |
0f113f3e MC |
503 | const char *type, const char *value) |
504 | { | |
505 | if (!strcmp(type, key_ctrl_string)) { | |
506 | if (strlen(value) != 32) { | |
507 | GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, | |
508 | GOST_R_INVALID_MAC_KEY_LENGTH); | |
509 | return 0; | |
510 | } | |
511 | return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, | |
512 | 32, (char *)value); | |
513 | } | |
514 | if (!strcmp(type, hexkey_ctrl_string)) { | |
515 | long keylen; | |
516 | int ret; | |
517 | unsigned char *keybuf = string_to_hex(value, &keylen); | |
518 | if (keylen != 32) { | |
519 | GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, | |
520 | GOST_R_INVALID_MAC_KEY_LENGTH); | |
521 | OPENSSL_free(keybuf); | |
522 | return 0; | |
523 | } | |
524 | ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf); | |
525 | OPENSSL_free(keybuf); | |
526 | return ret; | |
527 | ||
528 | } | |
529 | return -2; | |
530 | } | |
a4346646 DSH |
531 | |
532 | static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | |
0f113f3e MC |
533 | { |
534 | struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx); | |
535 | unsigned char *keydata; | |
536 | if (!data->key_set) { | |
537 | GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN, GOST_R_MAC_KEY_NOT_SET); | |
538 | return 0; | |
539 | } | |
540 | keydata = OPENSSL_malloc(32); | |
541 | if (keydata == NULL) | |
542 | return 0; | |
543 | memcpy(keydata, data->key, 32); | |
544 | EVP_PKEY_assign(pkey, NID_id_Gost28147_89_MAC, keydata); | |
545 | return 1; | |
546 | } | |
a4346646 DSH |
547 | |
548 | static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx) | |
0f113f3e MC |
549 | { |
550 | return 1; | |
551 | } | |
552 | ||
553 | static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, | |
554 | size_t *siglen, EVP_MD_CTX *mctx) | |
555 | { | |
556 | unsigned int tmpsiglen = *siglen; /* for platforms where | |
557 | * sizeof(int)!=sizeof(size_t) */ | |
558 | int ret; | |
559 | if (!sig) { | |
560 | *siglen = 4; | |
561 | return 1; | |
562 | } | |
563 | ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen); | |
564 | *siglen = tmpsiglen; | |
565 | return ret; | |
a4346646 DSH |
566 | } |
567 | ||
a04549cc | 568 | /* ----------------------------------------------------------------*/ |
0f113f3e MC |
569 | int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags) |
570 | { | |
571 | *pmeth = EVP_PKEY_meth_new(id, flags); | |
572 | if (!*pmeth) | |
573 | return 0; | |
574 | ||
575 | switch (id) { | |
576 | case NID_id_GostR3410_94: | |
577 | EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_ctrl, pkey_gost_ctrl94_str); | |
578 | EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost94cp_keygen); | |
579 | EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign); | |
580 | EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify); | |
581 | EVP_PKEY_meth_set_encrypt(*pmeth, | |
582 | pkey_gost_encrypt_init, | |
583 | pkey_GOST94cp_encrypt); | |
584 | EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt); | |
585 | EVP_PKEY_meth_set_derive(*pmeth, | |
586 | pkey_gost_derive_init, pkey_gost94_derive); | |
587 | EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init, | |
588 | pkey_gost94_paramgen); | |
589 | break; | |
590 | case NID_id_GostR3410_2001: | |
591 | EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_ctrl, pkey_gost_ctrl01_str); | |
592 | EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign); | |
593 | EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify); | |
594 | ||
595 | EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen); | |
596 | ||
597 | EVP_PKEY_meth_set_encrypt(*pmeth, | |
598 | pkey_gost_encrypt_init, | |
599 | pkey_GOST01cp_encrypt); | |
600 | EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt); | |
601 | EVP_PKEY_meth_set_derive(*pmeth, | |
602 | pkey_gost_derive_init, pkey_gost2001_derive); | |
603 | EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init, | |
604 | pkey_gost01_paramgen); | |
605 | break; | |
606 | case NID_id_Gost28147_89_MAC: | |
607 | EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl, | |
608 | pkey_gost_mac_ctrl_str); | |
609 | EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init, | |
610 | pkey_gost_mac_signctx); | |
611 | EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen); | |
612 | EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init); | |
613 | EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup); | |
614 | EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy); | |
615 | return 1; | |
616 | default: /* Unsupported method */ | |
617 | return 0; | |
618 | } | |
619 | EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init); | |
620 | EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup); | |
621 | ||
622 | EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy); | |
623 | /* | |
624 | * FIXME derive etc... | |
625 | */ | |
626 | ||
627 | return 1; | |
628 | } |