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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
|
/* $OpenBSD: gostr341001_pmeth.c,v 1.14 2017/01/29 17:49:23 beck Exp $ */
/*
* Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
* Copyright (c) 2005-2006 Cryptocom LTD
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* openssl-core@openssl.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
#include <string.h>
#include <openssl/opensslconf.h>
#ifndef OPENSSL_NO_GOST
#include <openssl/bn.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/gost.h>
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/x509.h>
#include "evp_locl.h"
#include "gost_locl.h"
#include "gost_asn1.h"
static ECDSA_SIG *
unpack_signature_cp(const unsigned char *sig, size_t siglen)
{
ECDSA_SIG *s;
s = ECDSA_SIG_new();
if (s == NULL) {
GOSTerror(ERR_R_MALLOC_FAILURE);
return NULL;
}
BN_bin2bn(sig, siglen / 2, s->s);
BN_bin2bn(sig + siglen / 2, siglen / 2, s->r);
return s;
}
static int
pack_signature_cp(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
{
int r_len = BN_num_bytes(s->r);
int s_len = BN_num_bytes(s->s);
if (r_len > order || s_len > order)
return 0;
*siglen = 2 * order;
memset(sig, 0, *siglen);
BN_bn2bin(s->s, sig + order - s_len);
BN_bn2bin(s->r, sig + 2 * order - r_len);
ECDSA_SIG_free(s);
return 1;
}
static ECDSA_SIG *
unpack_signature_le(const unsigned char *sig, size_t siglen)
{
ECDSA_SIG *s;
s = ECDSA_SIG_new();
if (s == NULL) {
GOSTerror(ERR_R_MALLOC_FAILURE);
return NULL;
}
GOST_le2bn(sig, siglen / 2, s->r);
GOST_le2bn(sig + siglen / 2, siglen / 2, s->s);
return s;
}
static int
pack_signature_le(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
{
*siglen = 2 * order;
memset(sig, 0, *siglen);
GOST_bn2le(s->r, sig, order);
GOST_bn2le(s->s, sig + order, order);
ECDSA_SIG_free(s);
return 1;
}
struct gost_pmeth_data {
int sign_param_nid; /* Should be set whenever parameters are filled */
int digest_nid;
EVP_MD *md;
unsigned char *shared_ukm;
int peer_key_used;
int sig_format;
};
static int
pkey_gost01_init(EVP_PKEY_CTX *ctx)
{
struct gost_pmeth_data *data;
EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
data = calloc(1, sizeof(struct gost_pmeth_data));
if (data == NULL)
return 0;
if (pkey != NULL && pkey->pkey.gost != NULL) {
data->sign_param_nid =
EC_GROUP_get_curve_name(GOST_KEY_get0_group(pkey->pkey.gost));
data->digest_nid = GOST_KEY_get_digest(pkey->pkey.gost);
}
EVP_PKEY_CTX_set_data(ctx, data);
return 1;
}
/* Copies contents of gost_pmeth_data structure */
static int
pkey_gost01_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
{
struct gost_pmeth_data *dst_data, *src_data;
if (pkey_gost01_init(dst) == 0)
return 0;
src_data = EVP_PKEY_CTX_get_data(src);
dst_data = EVP_PKEY_CTX_get_data(dst);
*dst_data = *src_data;
if (src_data->shared_ukm != NULL)
dst_data->shared_ukm = NULL;
return 1;
}
/* Frees up gost_pmeth_data structure */
static void
pkey_gost01_cleanup(EVP_PKEY_CTX *ctx)
{
struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
free(data->shared_ukm);
free(data);
}
static int
pkey_gost01_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
{
struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
EC_GROUP *group = NULL;
GOST_KEY *gost = NULL;
int ret = 0;
if (data->sign_param_nid == NID_undef ||
data->digest_nid == NID_undef) {
GOSTerror(GOST_R_NO_PARAMETERS_SET);
return 0;
}
group = EC_GROUP_new_by_curve_name(data->sign_param_nid);
if (group == NULL)
goto done;
EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
gost = GOST_KEY_new();
if (gost == NULL)
goto done;
if (GOST_KEY_set_digest(gost, data->digest_nid) == 0)
goto done;
if (GOST_KEY_set_group(gost, group) != 0)
ret = EVP_PKEY_assign_GOST(pkey, gost);
done:
if (ret == 0)
GOST_KEY_free(gost);
EC_GROUP_free(group);
return ret;
}
static int
pkey_gost01_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
{
if (pkey_gost01_paramgen(ctx, pkey) == 0)
return 0;
return gost2001_keygen(pkey->pkey.gost) != 0;
}
static int
pkey_gost01_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
const unsigned char *tbs, size_t tbs_len)
{
ECDSA_SIG *unpacked_sig = NULL;
EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
struct gost_pmeth_data *pctx = EVP_PKEY_CTX_get_data(ctx);
BIGNUM *md;
size_t size;
int ret;
if (pkey == NULL || pkey->pkey.gost == NULL)
return 0;
size = GOST_KEY_get_size(pkey->pkey.gost);
if (siglen == NULL)
return 0;
if (sig == NULL) {
*siglen = 2 * size;
return 1;
} else if (*siglen < 2 * size) {
GOSTerror(EC_R_BUFFER_TOO_SMALL);
return 0;
}
if (tbs_len != 32 && tbs_len != 64) {
GOSTerror(EVP_R_BAD_BLOCK_LENGTH);
return 0;
}
md = GOST_le2bn(tbs, tbs_len, NULL);
if (md == NULL)
return 0;
unpacked_sig = gost2001_do_sign(md, pkey->pkey.gost);
BN_free(md);
if (unpacked_sig == NULL) {
return 0;
}
switch (pctx->sig_format) {
case GOST_SIG_FORMAT_SR_BE:
ret = pack_signature_cp(unpacked_sig, size, sig, siglen);
break;
case GOST_SIG_FORMAT_RS_LE:
ret = pack_signature_le(unpacked_sig, size, sig, siglen);
break;
default:
ret = -1;
break;
}
if (ret <= 0)
ECDSA_SIG_free(unpacked_sig);
return ret;
}
static int
pkey_gost01_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen,
const unsigned char *tbs, size_t tbs_len)
{
int ok = 0;
EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
struct gost_pmeth_data *pctx = EVP_PKEY_CTX_get_data(ctx);
ECDSA_SIG *s = NULL;
BIGNUM *md;
if (pub_key == NULL)
return 0;
switch (pctx->sig_format) {
case GOST_SIG_FORMAT_SR_BE:
s = unpack_signature_cp(sig, siglen);
break;
case GOST_SIG_FORMAT_RS_LE:
s = unpack_signature_le(sig, siglen);
break;
}
if (s == NULL)
return 0;
md = GOST_le2bn(tbs, tbs_len, NULL);
if (md == NULL)
goto err;
ok = gost2001_do_verify(md, s, pub_key->pkey.gost);
err:
BN_free(md);
ECDSA_SIG_free(s);
return ok;
}
static int
gost01_VKO_key(EVP_PKEY *pub_key, EVP_PKEY *priv_key, const unsigned char *ukm,
unsigned char *key)
{
unsigned char hashbuf[128];
int digest_nid;
int ret = 0;
BN_CTX *ctx = BN_CTX_new();
BIGNUM *UKM, *X, *Y;
if (ctx == NULL)
return 0;
BN_CTX_start(ctx);
if ((UKM = BN_CTX_get(ctx)) == NULL)
goto err;
if ((X = BN_CTX_get(ctx)) == NULL)
goto err;
if ((Y = BN_CTX_get(ctx)) == NULL)
goto err;
GOST_le2bn(ukm, 8, UKM);
digest_nid = GOST_KEY_get_digest(priv_key->pkey.gost);
if (VKO_compute_key(X, Y, pub_key->pkey.gost, priv_key->pkey.gost,
UKM) == 0)
goto err;
switch (digest_nid) {
case NID_id_GostR3411_94_CryptoProParamSet:
GOST_bn2le(X, hashbuf, 32);
GOST_bn2le(Y, hashbuf + 32, 32);
GOSTR341194(hashbuf, 64, key, digest_nid);
ret = 1;
break;
case NID_id_tc26_gost3411_2012_256:
GOST_bn2le(X, hashbuf, 32);
GOST_bn2le(Y, hashbuf + 32, 32);
STREEBOG256(hashbuf, 64, key);
ret = 1;
break;
case NID_id_tc26_gost3411_2012_512:
GOST_bn2le(X, hashbuf, 64);
GOST_bn2le(Y, hashbuf + 64, 64);
STREEBOG256(hashbuf, 128, key);
ret = 1;
break;
default:
ret = -2;
break;
}
err:
BN_CTX_end(ctx);
BN_CTX_free(ctx);
return ret;
}
int
pkey_gost01_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key, size_t *key_len,
const unsigned char *in, size_t in_len)
{
const unsigned char *p = in;
EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
GOST_KEY_TRANSPORT *gkt = NULL;
int ret = 0;
unsigned char wrappedKey[44];
unsigned char sharedKey[32];
EVP_PKEY *eph_key = NULL, *peerkey = NULL;
int nid;
if (key == NULL) {
*key_len = 32;
return 1;
}
gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
if (gkt == NULL) {
GOSTerror(GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
return -1;
}
/* If key transport structure contains public key, use it */
eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
if (eph_key != NULL) {
if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
GOSTerror(GOST_R_INCOMPATIBLE_PEER_KEY);
goto err;
}
} else {
/* Set control "public key from client certificate used" */
if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3,
NULL) <= 0) {
GOSTerror(GOST_R_CTRL_CALL_FAILED);
goto err;
}
}
peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
if (peerkey == NULL) {
GOSTerror(GOST_R_NO_PEER_KEY);
goto err;
}
nid = OBJ_obj2nid(gkt->key_agreement_info->cipher);
if (gkt->key_agreement_info->eph_iv->length != 8) {
GOSTerror(GOST_R_INVALID_IV_LENGTH);
goto err;
}
memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
if (gkt->key_info->encrypted_key->length != 32) {
GOSTerror(EVP_R_BAD_KEY_LENGTH);
goto err;
}
memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
if (gkt->key_info->imit->length != 4) {
GOSTerror(ERR_R_INTERNAL_ERROR);
goto err;
}
memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
if (gost01_VKO_key(peerkey, priv, wrappedKey, sharedKey) <= 0)
goto err;
if (gost_key_unwrap_crypto_pro(nid, sharedKey, wrappedKey, key) == 0) {
GOSTerror(GOST_R_ERROR_COMPUTING_SHARED_KEY);
goto err;
}
ret = 1;
err:
EVP_PKEY_free(eph_key);
GOST_KEY_TRANSPORT_free(gkt);
return ret;
}
int
pkey_gost01_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
{
/*
* Public key of peer in the ctx field peerkey
* Our private key in the ctx pkey
* ukm is in the algorithm specific context data
*/
EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
if (data->shared_ukm == NULL) {
GOSTerror(GOST_R_UKM_NOT_SET);
return 0;
}
if (key == NULL) {
*keylen = 32;
return 32;
}
if (gost01_VKO_key(peer_key, my_key, data->shared_ukm, key) <= 0)
return 0;
*keylen = 32;
return 1;
}
int
pkey_gost01_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out, size_t *out_len,
const unsigned char *key, size_t key_len)
{
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], shared_key[32], crypted_key[44];
int ret = 0;
int key_is_ephemeral;
EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
int nid = NID_id_Gost28147_89_CryptoPro_A_ParamSet;
if (data->shared_ukm != NULL) {
memcpy(ukm, data->shared_ukm, 8);
} else /* if (out != NULL) */ {
arc4random_buf(ukm, 8);
}
/* Check for private key in the peer_key of context */
if (sec_key) {
key_is_ephemeral = 0;
if (GOST_KEY_get0_private_key(sec_key->pkey.gost) == 0) {
GOSTerror(GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
goto err;
}
} else {
key_is_ephemeral = 1;
if (out != NULL) {
GOST_KEY *tmp_key;
sec_key = EVP_PKEY_new();
if (sec_key == NULL)
goto err;
tmp_key = GOST_KEY_new();
if (tmp_key == NULL)
goto err;
if (EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk),
tmp_key) == 0) {
GOST_KEY_free(tmp_key);
goto err;
}
if (EVP_PKEY_copy_parameters(sec_key, pubk) == 0)
goto err;
if (gost2001_keygen(sec_key->pkey.gost) == 0) {
goto err;
}
}
}
if (out != NULL) {
if (gost01_VKO_key(pubk, sec_key, ukm, shared_key) <= 0)
goto err;
gost_key_wrap_crypto_pro(nid, shared_key, ukm, key,
crypted_key);
}
gkt = GOST_KEY_TRANSPORT_new();
if (gkt == NULL)
goto err;
if (ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8) == 0)
goto err;
if (ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40,
4) == 0)
goto err;
if (ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key, crypted_key + 8,
32) == 0)
goto err;
if (key_is_ephemeral) {
if (X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,
out != NULL ? sec_key : pubk) == 0) {
GOSTerror(GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
goto err;
}
}
ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
gkt->key_agreement_info->cipher = OBJ_nid2obj(nid);
if (key_is_ephemeral)
EVP_PKEY_free(sec_key);
else {
/* Set control "public key from client certificate used" */
if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3,
NULL) <= 0) {
GOSTerror(GOST_R_CTRL_CALL_FAILED);
goto err;
}
}
if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
ret = 1;
GOST_KEY_TRANSPORT_free(gkt);
return ret;
err:
if (key_is_ephemeral)
EVP_PKEY_free(sec_key);
GOST_KEY_TRANSPORT_free(gkt);
return -1;
}
static int
pkey_gost01_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
{
struct gost_pmeth_data *pctx = EVP_PKEY_CTX_get_data(ctx);
switch (type) {
case EVP_PKEY_CTRL_MD:
if (EVP_MD_type(p2) !=
GostR3410_get_md_digest(pctx->digest_nid)) {
GOSTerror(GOST_R_INVALID_DIGEST_TYPE);
return 0;
}
pctx->md = p2;
return 1;
case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
case EVP_PKEY_CTRL_PKCS7_DECRYPT:
case EVP_PKEY_CTRL_PKCS7_SIGN:
case EVP_PKEY_CTRL_DIGESTINIT:
return 1;
case EVP_PKEY_CTRL_GOST_PARAMSET:
pctx->sign_param_nid = (int)p1;
return 1;
case EVP_PKEY_CTRL_SET_IV:
{
char *ukm = malloc(p1);
if (ukm == NULL) {
GOSTerror(ERR_R_MALLOC_FAILURE);
return 0;
}
memcpy(ukm, p2, p1);
free(pctx->shared_ukm);
pctx->shared_ukm = ukm;
return 1;
}
case EVP_PKEY_CTRL_PEER_KEY:
if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */
return 1;
if (p1 == 2) /* TLS: peer key used? */
return pctx->peer_key_used;
if (p1 == 3) /* TLS: peer key used! */
return (pctx->peer_key_used = 1);
return -2;
case EVP_PKEY_CTRL_GOST_SIG_FORMAT:
switch (p1) {
case GOST_SIG_FORMAT_SR_BE:
case GOST_SIG_FORMAT_RS_LE:
pctx->sig_format = p1;
return 1;
default:
return 0;
}
break;
case EVP_PKEY_CTRL_GOST_SET_DIGEST:
pctx->digest_nid = (int)p1;
return 1;
case EVP_PKEY_CTRL_GOST_GET_DIGEST:
*(int *)p2 = pctx->digest_nid;
return 1;
default:
return -2;
}
}
static int
pkey_gost01_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value)
{
int param_nid = NID_undef;
int digest_nid = NID_undef;
if (strcmp(type, "paramset") == 0) {
if (value == NULL)
return 0;
if (pkey_gost01_ctrl(ctx, EVP_PKEY_CTRL_GOST_GET_DIGEST, 0,
&digest_nid) == 0)
return 0;
if (digest_nid == NID_id_tc26_gost3411_2012_512)
param_nid = GostR3410_512_param_id(value);
else
param_nid = GostR3410_256_param_id(value);
if (param_nid == NID_undef)
param_nid = OBJ_txt2nid(value);
if (param_nid == NID_undef)
return 0;
return pkey_gost01_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
param_nid, NULL);
}
if (strcmp(type, "dgst") == 0) {
if (value == NULL)
return 0;
else if (strcmp(value, "gost94") == 0 ||
strcmp(value, "md_gost94") == 0)
digest_nid = NID_id_GostR3411_94_CryptoProParamSet;
else if (strcmp(value, "streebog256") == 0)
digest_nid = NID_id_tc26_gost3411_2012_256;
else if (strcmp(value, "streebog512") == 0)
digest_nid = NID_id_tc26_gost3411_2012_512;
if (digest_nid == NID_undef)
return 0;
return pkey_gost01_ctrl(ctx, EVP_PKEY_CTRL_GOST_SET_DIGEST,
digest_nid, NULL);
}
return -2;
}
const EVP_PKEY_METHOD gostr01_pkey_meth = {
.pkey_id = EVP_PKEY_GOSTR01,
.init = pkey_gost01_init,
.copy = pkey_gost01_copy,
.cleanup = pkey_gost01_cleanup,
.paramgen = pkey_gost01_paramgen,
.keygen = pkey_gost01_keygen,
.sign = pkey_gost01_sign,
.verify = pkey_gost01_verify,
.encrypt = pkey_gost01_encrypt,
.decrypt = pkey_gost01_decrypt,
.derive = pkey_gost01_derive,
.ctrl = pkey_gost01_ctrl,
.ctrl_str = pkey_gost01_ctrl_str,
};
#endif
|