summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2019-01-19 01:07:00 +0000
committertb <>2019-01-19 01:07:00 +0000
commitdc38b357c3a6e0db4a7172af29148961b86b0724 (patch)
treef28042e7a3c924e4bf846d6cded984e02699177d
parentf11f1c0f8ad579cfb88a2559e3efe0e0367cec85 (diff)
downloadopenbsd-dc38b357c3a6e0db4a7172af29148961b86b0724.tar.gz
openbsd-dc38b357c3a6e0db4a7172af29148961b86b0724.tar.bz2
openbsd-dc38b357c3a6e0db4a7172af29148961b86b0724.zip
Partial port of EC_KEY_METHOD from OpenSSL 1.1.
This commit adds init/free, support for signing, setting and getting the method, engine support as well as extra data. from markus
-rw-r--r--src/lib/libcrypto/Makefile10
-rw-r--r--src/lib/libcrypto/Symbols.list13
-rw-r--r--src/lib/libcrypto/crypto.h3
-rw-r--r--src/lib/libcrypto/ec/ec.h31
-rw-r--r--src/lib/libcrypto/ec/ec_key.c91
-rw-r--r--src/lib/libcrypto/ec/ec_kmeth.c272
-rw-r--r--src/lib/libcrypto/ec/ec_lcl.h30
-rw-r--r--src/lib/libcrypto/ecdsa/ecdsa.h22
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_locl.h10
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_ossl.c39
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_sign.c38
-rw-r--r--src/lib/libcrypto/engine/eng_fat.c11
-rw-r--r--src/lib/libcrypto/engine/eng_int.h3
-rw-r--r--src/lib/libcrypto/engine/eng_list.c5
-rw-r--r--src/lib/libcrypto/engine/engine.h14
-rw-r--r--src/lib/libcrypto/engine/tb_eckey.c123
16 files changed, 659 insertions, 56 deletions
diff --git a/src/lib/libcrypto/Makefile b/src/lib/libcrypto/Makefile
index 8e39d92ab4..66f511761b 100644
--- a/src/lib/libcrypto/Makefile
+++ b/src/lib/libcrypto/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.30 2018/11/11 06:53:31 tb Exp $ 1# $OpenBSD: Makefile,v 1.31 2019/01/19 01:07:00 tb Exp $
2 2
3LIB= crypto 3LIB= crypto
4LIBREBUILD=y 4LIBREBUILD=y
@@ -31,6 +31,10 @@ CFLAGS+= -I${LCRYPTO_SRC}
31CFLAGS+= -I${LCRYPTO_SRC}/asn1 -I${LCRYPTO_SRC}/bn -I${LCRYPTO_SRC}/evp 31CFLAGS+= -I${LCRYPTO_SRC}/asn1 -I${LCRYPTO_SRC}/bn -I${LCRYPTO_SRC}/evp
32CFLAGS+= -I${LCRYPTO_SRC}/modes 32CFLAGS+= -I${LCRYPTO_SRC}/modes
33 33
34# XXX FIXME ecdsa and ec should be merged
35CFLAGS+= -I${LCRYPTO_SRC}/ecdsa
36CFLAGS+= -I${LCRYPTO_SRC}/ec
37
34VERSION_SCRIPT= Symbols.map 38VERSION_SCRIPT= Symbols.map
35SYMBOL_LIST= ${.CURDIR}/Symbols.list 39SYMBOL_LIST= ${.CURDIR}/Symbols.list
36 40
@@ -125,7 +129,7 @@ SRCS+= dso_openssl.c
125# ec/ 129# ec/
126SRCS+= ec_lib.c ecp_smpl.c ecp_mont.c ecp_nist.c ec_cvt.c ec_mult.c 130SRCS+= ec_lib.c ecp_smpl.c ecp_mont.c ecp_nist.c ec_cvt.c ec_mult.c
127SRCS+= ec_err.c ec_curve.c ec_check.c ec_print.c ec_asn1.c ec_key.c 131SRCS+= ec_err.c ec_curve.c ec_check.c ec_print.c ec_asn1.c ec_key.c
128SRCS+= ec2_smpl.c ec2_mult.c ec_ameth.c ec_pmeth.c eck_prn.c 132SRCS+= ec2_smpl.c ec2_mult.c ec_ameth.c ec_pmeth.c ec_kmeth.c eck_prn.c
129SRCS+= ecp_nistp224.c ecp_nistp256.c ecp_nistp521.c ecp_nistputil.c 133SRCS+= ecp_nistp224.c ecp_nistp256.c ecp_nistp521.c ecp_nistputil.c
130SRCS+= ecp_oct.c ec2_oct.c ec_oct.c 134SRCS+= ecp_oct.c ec2_oct.c ec_oct.c
131 135
@@ -139,7 +143,7 @@ SRCS+= ecs_lib.c ecs_asn1.c ecs_ossl.c ecs_sign.c ecs_vrf.c ecs_err.c
139SRCS+= eng_err.c eng_lib.c eng_list.c eng_init.c eng_ctrl.c 143SRCS+= eng_err.c eng_lib.c eng_list.c eng_init.c eng_ctrl.c
140SRCS+= eng_table.c eng_pkey.c eng_fat.c eng_all.c 144SRCS+= eng_table.c eng_pkey.c eng_fat.c eng_all.c
141SRCS+= tb_rsa.c tb_dsa.c tb_ecdsa.c tb_dh.c tb_ecdh.c tb_rand.c tb_store.c 145SRCS+= tb_rsa.c tb_dsa.c tb_ecdsa.c tb_dh.c tb_ecdh.c tb_rand.c tb_store.c
142SRCS+= tb_cipher.c tb_digest.c tb_pkmeth.c tb_asnmth.c 146SRCS+= tb_cipher.c tb_digest.c tb_pkmeth.c tb_asnmth.c tb_eckey.c
143SRCS+= eng_openssl.c eng_cnf.c eng_dyn.c 147SRCS+= eng_openssl.c eng_cnf.c eng_dyn.c
144# XXX unnecessary? handled in EVP now... 148# XXX unnecessary? handled in EVP now...
145# SRCS+= eng_aesni.c # local addition 149# SRCS+= eng_aesni.c # local addition
diff --git a/src/lib/libcrypto/Symbols.list b/src/lib/libcrypto/Symbols.list
index c4b78dbef2..2734fed62a 100644
--- a/src/lib/libcrypto/Symbols.list
+++ b/src/lib/libcrypto/Symbols.list
@@ -955,6 +955,13 @@ EC_GROUP_set_curve_name
955EC_GROUP_set_generator 955EC_GROUP_set_generator
956EC_GROUP_set_point_conversion_form 956EC_GROUP_set_point_conversion_form
957EC_GROUP_set_seed 957EC_GROUP_set_seed
958EC_KEY_METHOD_free
959EC_KEY_METHOD_get_init
960EC_KEY_METHOD_get_sign
961EC_KEY_METHOD_new
962EC_KEY_METHOD_set_init
963EC_KEY_METHOD_set_sign
964EC_KEY_OpenSSL
958EC_KEY_check_key 965EC_KEY_check_key
959EC_KEY_clear_flags 966EC_KEY_clear_flags
960EC_KEY_copy 967EC_KEY_copy
@@ -965,9 +972,12 @@ EC_KEY_get0_group
965EC_KEY_get0_private_key 972EC_KEY_get0_private_key
966EC_KEY_get0_public_key 973EC_KEY_get0_public_key
967EC_KEY_get_conv_form 974EC_KEY_get_conv_form
975EC_KEY_get_default_method
968EC_KEY_get_enc_flags 976EC_KEY_get_enc_flags
977EC_KEY_get_ex_data
969EC_KEY_get_flags 978EC_KEY_get_flags
970EC_KEY_get_key_method_data 979EC_KEY_get_key_method_data
980EC_KEY_get_method
971EC_KEY_insert_key_method_data 981EC_KEY_insert_key_method_data
972EC_KEY_new 982EC_KEY_new
973EC_KEY_new_by_curve_name 983EC_KEY_new_by_curve_name
@@ -976,9 +986,12 @@ EC_KEY_print
976EC_KEY_print_fp 986EC_KEY_print_fp
977EC_KEY_set_asn1_flag 987EC_KEY_set_asn1_flag
978EC_KEY_set_conv_form 988EC_KEY_set_conv_form
989EC_KEY_set_default_method
979EC_KEY_set_enc_flags 990EC_KEY_set_enc_flags
991EC_KEY_set_ex_data
980EC_KEY_set_flags 992EC_KEY_set_flags
981EC_KEY_set_group 993EC_KEY_set_group
994EC_KEY_set_method
982EC_KEY_set_private_key 995EC_KEY_set_private_key
983EC_KEY_set_public_key 996EC_KEY_set_public_key
984EC_KEY_set_public_key_affine_coordinates 997EC_KEY_set_public_key_affine_coordinates
diff --git a/src/lib/libcrypto/crypto.h b/src/lib/libcrypto/crypto.h
index 8d549da8b5..7de8abb437 100644
--- a/src/lib/libcrypto/crypto.h
+++ b/src/lib/libcrypto/crypto.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto.h,v 1.49 2018/11/11 16:32:28 bcook Exp $ */ 1/* $OpenBSD: crypto.h,v 1.50 2019/01/19 01:07:00 tb Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -286,6 +286,7 @@ DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS)
286#define CRYPTO_EX_INDEX_ECDH 13 286#define CRYPTO_EX_INDEX_ECDH 13
287#define CRYPTO_EX_INDEX_COMP 14 287#define CRYPTO_EX_INDEX_COMP 14
288#define CRYPTO_EX_INDEX_STORE 15 288#define CRYPTO_EX_INDEX_STORE 15
289#define CRYPTO_EX_INDEX_EC_KEY 16
289 290
290/* Dynamically assigned indexes start from this value (don't use directly, use 291/* Dynamically assigned indexes start from this value (don't use directly, use
291 * via CRYPTO_ex_data_new_class). */ 292 * via CRYPTO_ex_data_new_class). */
diff --git a/src/lib/libcrypto/ec/ec.h b/src/lib/libcrypto/ec/ec.h
index d9df48603a..1173459dae 100644
--- a/src/lib/libcrypto/ec/ec.h
+++ b/src/lib/libcrypto/ec/ec.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec.h,v 1.13 2018/05/19 10:37:02 tb Exp $ */ 1/* $OpenBSD: ec.h,v 1.14 2019/01/19 01:07:00 tb Exp $ */
2/* 2/*
3 * Originally written by Bodo Moeller for the OpenSSL project. 3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */ 4 */
@@ -705,6 +705,7 @@ int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off);
705/********************************************************************/ 705/********************************************************************/
706 706
707typedef struct ec_key_st EC_KEY; 707typedef struct ec_key_st EC_KEY;
708typedef struct ec_key_method_st EC_KEY_METHOD;
708 709
709/* some values for the encoding_flag */ 710/* some values for the encoding_flag */
710#define EC_PKEY_NO_PARAMETERS 0x001 711#define EC_PKEY_NO_PARAMETERS 0x001
@@ -945,6 +946,34 @@ int ECParameters_print_fp(FILE *fp, const EC_KEY *key);
945 */ 946 */
946int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off); 947int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off);
947 948
949#define EC_KEY_get_ex_new_index(l, p, newf, dupf, freef) \
950 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY, l, p, newf, dupf, freef)
951int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg);
952void *EC_KEY_get_ex_data(const EC_KEY *key, int idx);
953
954const EC_KEY_METHOD *EC_KEY_OpenSSL(void);
955const EC_KEY_METHOD *EC_KEY_get_default_method(void);
956void EC_KEY_set_default_method(const EC_KEY_METHOD *meth);
957const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key);
958int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth);
959EC_KEY *EC_KEY_new_method(ENGINE *engine);
960EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth);
961void EC_KEY_METHOD_free(EC_KEY_METHOD *meth);
962void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
963 int (*init)(EC_KEY *key),
964 void (*finish)(EC_KEY *key),
965 int (*copy)(EC_KEY *dest, const EC_KEY *src),
966 int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
967 int (*set_private)(EC_KEY *key, const BIGNUM *priv_key),
968 int (*set_public)(EC_KEY *key, const EC_POINT *pub_key));
969void EC_KEY_METHOD_get_init(EC_KEY_METHOD *meth,
970 int (**pinit)(EC_KEY *key),
971 void (**pfinish)(EC_KEY *key),
972 int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
973 int (**pset_group)(EC_KEY *key, const EC_GROUP *grp),
974 int (**pset_private)(EC_KEY *key, const BIGNUM *priv_key),
975 int (**pset_public)(EC_KEY *key, const EC_POINT *pub_key));
976
948EC_KEY *ECParameters_dup(EC_KEY *key); 977EC_KEY *ECParameters_dup(EC_KEY *key);
949 978
950#ifndef __cplusplus 979#ifndef __cplusplus
diff --git a/src/lib/libcrypto/ec/ec_key.c b/src/lib/libcrypto/ec/ec_key.c
index fcdf461d20..f57e078c7f 100644
--- a/src/lib/libcrypto/ec/ec_key.c
+++ b/src/lib/libcrypto/ec/ec_key.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_key.c,v 1.22 2018/11/09 23:39:45 tb Exp $ */ 1/* $OpenBSD: ec_key.c,v 1.23 2019/01/19 01:07:00 tb Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project. 3 * Written by Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -65,30 +65,18 @@
65 65
66#include <openssl/opensslconf.h> 66#include <openssl/opensslconf.h>
67 67
68#ifndef OPENSSL_NO_ENGINE
69#include <openssl/engine.h>
70#endif
71#include <openssl/err.h>
72
68#include "bn_lcl.h" 73#include "bn_lcl.h"
69#include "ec_lcl.h" 74#include "ec_lcl.h"
70#include <openssl/err.h>
71 75
72EC_KEY * 76EC_KEY *
73EC_KEY_new(void) 77EC_KEY_new(void)
74{ 78{
75 EC_KEY *ret; 79 return EC_KEY_new_method(NULL);
76
77 ret = malloc(sizeof(EC_KEY));
78 if (ret == NULL) {
79 ECerror(ERR_R_MALLOC_FAILURE);
80 return (NULL);
81 }
82 ret->version = 1;
83 ret->flags = 0;
84 ret->group = NULL;
85 ret->pub_key = NULL;
86 ret->priv_key = NULL;
87 ret->enc_flag = 0;
88 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
89 ret->references = 1;
90 ret->method_data = NULL;
91 return (ret);
92} 80}
93 81
94EC_KEY * 82EC_KEY *
@@ -102,6 +90,11 @@ EC_KEY_new_by_curve_name(int nid)
102 EC_KEY_free(ret); 90 EC_KEY_free(ret);
103 return NULL; 91 return NULL;
104 } 92 }
93 if (ret->meth->set_group != NULL &&
94 ret->meth->set_group(ret, ret->group) == 0) {
95 EC_KEY_free(ret);
96 return NULL;
97 }
105 return ret; 98 return ret;
106} 99}
107 100
@@ -117,6 +110,14 @@ EC_KEY_free(EC_KEY * r)
117 if (i > 0) 110 if (i > 0)
118 return; 111 return;
119 112
113 if (r->meth != NULL && r->meth->finish != NULL)
114 r->meth->finish(r);
115
116#ifndef OPENSSL_NO_ENGINE
117 ENGINE_finish(r->engine);
118#endif
119 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
120
120 EC_GROUP_free(r->group); 121 EC_GROUP_free(r->group);
121 EC_POINT_free(r->pub_key); 122 EC_POINT_free(r->pub_key);
122 BN_clear_free(r->priv_key); 123 BN_clear_free(r->priv_key);
@@ -135,6 +136,15 @@ EC_KEY_copy(EC_KEY * dest, const EC_KEY * src)
135 ECerror(ERR_R_PASSED_NULL_PARAMETER); 136 ECerror(ERR_R_PASSED_NULL_PARAMETER);
136 return NULL; 137 return NULL;
137 } 138 }
139 if (src->meth != dest->meth) {
140 if (dest->meth != NULL && dest->meth->finish != NULL)
141 dest->meth->finish(dest);
142#ifndef OPENSSL_NO_ENGINE
143 if (ENGINE_finish(dest->engine) == 0)
144 return 0;
145 dest->engine = NULL;
146#endif
147 }
138 /* copy the parameters */ 148 /* copy the parameters */
139 if (src->group) { 149 if (src->group) {
140 const EC_METHOD *meth = EC_GROUP_method_of(src->group); 150 const EC_METHOD *meth = EC_GROUP_method_of(src->group);
@@ -184,14 +194,32 @@ EC_KEY_copy(EC_KEY * dest, const EC_KEY * src)
184 dest->version = src->version; 194 dest->version = src->version;
185 dest->flags = src->flags; 195 dest->flags = src->flags;
186 196
197 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY, &dest->ex_data,
198 &((EC_KEY *)src)->ex_data)) /* XXX const */
199 return NULL;
200
201 if (src->meth != dest->meth) {
202#ifndef OPENSSL_NO_ENGINE
203 if (src->engine != NULL && ENGINE_init(src->engine) == 0)
204 return 0;
205 dest->engine = src->engine;
206#endif
207 dest->meth = src->meth;
208 }
209
210 if (src->meth != NULL && src->meth->copy != NULL &&
211 src->meth->copy(dest, src) == 0)
212 return 0;
213
187 return dest; 214 return dest;
188} 215}
189 216
190EC_KEY * 217EC_KEY *
191EC_KEY_dup(const EC_KEY * ec_key) 218EC_KEY_dup(const EC_KEY * ec_key)
192{ 219{
193 EC_KEY *ret = EC_KEY_new(); 220 EC_KEY *ret;
194 if (ret == NULL) 221
222 if ((ret = EC_KEY_new_method(ec_key->engine)) == NULL)
195 return NULL; 223 return NULL;
196 if (EC_KEY_copy(ret, ec_key) == NULL) { 224 if (EC_KEY_copy(ret, ec_key) == NULL) {
197 EC_KEY_free(ret); 225 EC_KEY_free(ret);
@@ -207,6 +235,18 @@ EC_KEY_up_ref(EC_KEY * r)
207 return ((i > 1) ? 1 : 0); 235 return ((i > 1) ? 1 : 0);
208} 236}
209 237
238int
239EC_KEY_set_ex_data(EC_KEY *r, int idx, void *arg)
240{
241 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
242}
243
244void *
245EC_KEY_get_ex_data(const EC_KEY *r, int idx)
246{
247 return CRYPTO_get_ex_data(&r->ex_data, idx);
248}
249
210int 250int
211EC_KEY_generate_key(EC_KEY *eckey) 251EC_KEY_generate_key(EC_KEY *eckey)
212{ 252{
@@ -407,6 +447,9 @@ EC_KEY_get0_group(const EC_KEY * key)
407int 447int
408EC_KEY_set_group(EC_KEY * key, const EC_GROUP * group) 448EC_KEY_set_group(EC_KEY * key, const EC_GROUP * group)
409{ 449{
450 if (key->meth->set_group != NULL &&
451 key->meth->set_group(key, group) == 0)
452 return 0;
410 EC_GROUP_free(key->group); 453 EC_GROUP_free(key->group);
411 key->group = EC_GROUP_dup(group); 454 key->group = EC_GROUP_dup(group);
412 return (key->group == NULL) ? 0 : 1; 455 return (key->group == NULL) ? 0 : 1;
@@ -421,6 +464,9 @@ EC_KEY_get0_private_key(const EC_KEY * key)
421int 464int
422EC_KEY_set_private_key(EC_KEY * key, const BIGNUM * priv_key) 465EC_KEY_set_private_key(EC_KEY * key, const BIGNUM * priv_key)
423{ 466{
467 if (key->meth->set_private != NULL &&
468 key->meth->set_private(key, priv_key) == 0)
469 return 0;
424 BN_clear_free(key->priv_key); 470 BN_clear_free(key->priv_key);
425 key->priv_key = BN_dup(priv_key); 471 key->priv_key = BN_dup(priv_key);
426 return (key->priv_key == NULL) ? 0 : 1; 472 return (key->priv_key == NULL) ? 0 : 1;
@@ -435,6 +481,9 @@ EC_KEY_get0_public_key(const EC_KEY * key)
435int 481int
436EC_KEY_set_public_key(EC_KEY * key, const EC_POINT * pub_key) 482EC_KEY_set_public_key(EC_KEY * key, const EC_POINT * pub_key)
437{ 483{
484 if (key->meth->set_public != NULL &&
485 key->meth->set_public(key, pub_key) == 0)
486 return 0;
438 EC_POINT_free(key->pub_key); 487 EC_POINT_free(key->pub_key);
439 key->pub_key = EC_POINT_dup(pub_key, key->group); 488 key->pub_key = EC_POINT_dup(pub_key, key->group);
440 return (key->pub_key == NULL) ? 0 : 1; 489 return (key->pub_key == NULL) ? 0 : 1;
diff --git a/src/lib/libcrypto/ec/ec_kmeth.c b/src/lib/libcrypto/ec/ec_kmeth.c
new file mode 100644
index 0000000000..b714c62236
--- /dev/null
+++ b/src/lib/libcrypto/ec/ec_kmeth.c
@@ -0,0 +1,272 @@
1/*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project.
4 */
5/* ====================================================================
6 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 */
53
54#include <openssl/ec.h>
55#ifndef OPENSSL_NO_ENGINE
56#include <openssl/engine.h>
57#endif
58#include <openssl/err.h>
59
60#include "ec_lcl.h"
61#include "ecs_locl.h"
62
63static const EC_KEY_METHOD openssl_ec_key_method = {
64 .name = "OpenSSL EC_KEY method",
65 .flags = 0,
66
67 .init = NULL,
68 .finish = NULL,
69 .copy = NULL,
70
71 .set_group = NULL,
72 .set_private = NULL,
73 .set_public = NULL,
74
75 .sign = ossl_ecdsa_sign,
76 .sign_setup = ossl_ecdsa_sign_setup,
77 .sign_sig = ossl_ecdsa_sign_sig,
78};
79
80const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method;
81
82const EC_KEY_METHOD *
83EC_KEY_OpenSSL(void)
84{
85 return &openssl_ec_key_method;
86}
87
88const EC_KEY_METHOD *
89EC_KEY_get_default_method(void)
90{
91 return default_ec_key_meth;
92}
93
94void
95EC_KEY_set_default_method(const EC_KEY_METHOD *meth)
96{
97 if (meth == NULL)
98 default_ec_key_meth = &openssl_ec_key_method;
99 else
100 default_ec_key_meth = meth;
101}
102
103const EC_KEY_METHOD *
104EC_KEY_get_method(const EC_KEY *key)
105{
106 return key->meth;
107}
108
109int
110EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth)
111{
112 void (*finish)(EC_KEY *key) = key->meth->finish;
113
114 if (finish != NULL)
115 finish(key);
116
117#ifndef OPENSSL_NO_ENGINE
118 ENGINE_finish(key->engine);
119 key->engine = NULL;
120#endif
121
122 key->meth = meth;
123 if (meth->init != NULL)
124 return meth->init(key);
125 return 1;
126}
127
128EC_KEY *
129EC_KEY_new_method(ENGINE *engine)
130{
131 EC_KEY *ret;
132
133 if ((ret = calloc(1, sizeof(EC_KEY))) == NULL) {
134 ECerror(ERR_R_MALLOC_FAILURE);
135 return NULL;
136 }
137 ret->meth = EC_KEY_get_default_method();
138#ifndef OPENSSL_NO_ENGINE
139 if (engine != NULL) {
140 if (!ENGINE_init(engine)) {
141 ECerror(ERR_R_ENGINE_LIB);
142 goto err;
143 }
144 ret->engine = engine;
145 } else
146 ret->engine = ENGINE_get_default_EC();
147 if (ret->engine) {
148 ret->meth = ENGINE_get_EC(ret->engine);
149 if (ret->meth == NULL) {
150 ECerror(ERR_R_ENGINE_LIB);
151 goto err;
152 }
153 }
154#endif
155 ret->version = 1;
156 ret->flags = 0;
157 ret->group = NULL;
158 ret->pub_key = NULL;
159 ret->priv_key = NULL;
160 ret->enc_flag = 0;
161 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
162 ret->references = 1;
163 ret->method_data = NULL;
164
165 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data))
166 goto err;
167 if (ret->meth->init != NULL && ret->meth->init(ret) == 0)
168 goto err;
169
170 return ret;
171
172 err:
173 EC_KEY_free(ret);
174 return NULL;
175}
176
177EC_KEY_METHOD *
178EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
179{
180 EC_KEY_METHOD *ret;
181
182 if ((ret = malloc(sizeof(*meth))) == NULL)
183 return NULL;
184 if (meth != NULL)
185 *ret = *meth;
186 ret->flags |= EC_KEY_METHOD_DYNAMIC;
187 return ret;
188}
189
190void
191EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
192{
193 if (meth == NULL)
194 return;
195 if (meth->flags & EC_KEY_METHOD_DYNAMIC)
196 free(meth);
197}
198
199void
200EC_KEY_METHOD_get_init(EC_KEY_METHOD *meth,
201 int (**pinit)(EC_KEY *key),
202 void (**pfinish)(EC_KEY *key),
203 int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
204 int (**pset_group)(EC_KEY *key, const EC_GROUP *grp),
205 int (**pset_private)(EC_KEY *key, const BIGNUM *priv_key),
206 int (**pset_public)(EC_KEY *key, const EC_POINT *pub_key))
207{
208 if (pinit != NULL)
209 *pinit = meth->init;
210 if (pfinish != NULL)
211 *pfinish = meth->finish;
212 if (pcopy != NULL)
213 *pcopy = meth->copy;
214 if (pset_group != NULL)
215 *pset_group = meth->set_group;
216 if (pset_private != NULL)
217 *pset_private = meth->set_private;
218 if (pset_public != NULL)
219 *pset_public = meth->set_public;
220}
221
222void
223EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
224 int (*init)(EC_KEY *key),
225 void (*finish)(EC_KEY *key),
226 int (*copy)(EC_KEY *dest, const EC_KEY *src),
227 int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
228 int (*set_private)(EC_KEY *key, const BIGNUM *priv_key),
229 int (*set_public)(EC_KEY *key, const EC_POINT *pub_key))
230{
231 meth->init = init;
232 meth->finish = finish;
233 meth->copy = copy;
234 meth->set_group = set_group;
235 meth->set_private = set_private;
236 meth->set_public = set_public;
237}
238
239void
240EC_KEY_METHOD_get_sign(EC_KEY_METHOD *meth,
241 int (**psign)(int type, const unsigned char *dgst,
242 int dlen, unsigned char *sig, unsigned int *siglen,
243 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey),
244 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
245 BIGNUM **kinvp, BIGNUM **rp),
246 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
247 int dgst_len, const BIGNUM *in_kinv, const BIGNUM *in_r,
248 EC_KEY *eckey))
249{
250 if (psign != NULL)
251 *psign = meth->sign;
252 if (psign_setup != NULL)
253 *psign_setup = meth->sign_setup;
254 if (psign_sig != NULL)
255 *psign_sig = meth->sign_sig;
256}
257
258void
259EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
260 int (*sign)(int type, const unsigned char *dgst,
261 int dlen, unsigned char *sig, unsigned int *siglen,
262 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey),
263 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
264 BIGNUM **kinvp, BIGNUM **rp),
265 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
266 int dgst_len, const BIGNUM *in_kinv,
267 const BIGNUM *in_r, EC_KEY *eckey))
268{
269 meth->sign = sign;
270 meth->sign_setup = sign_setup;
271 meth->sign_sig = sign_sig;
272}
diff --git a/src/lib/libcrypto/ec/ec_lcl.h b/src/lib/libcrypto/ec/ec_lcl.h
index c177246f36..cff0892e89 100644
--- a/src/lib/libcrypto/ec/ec_lcl.h
+++ b/src/lib/libcrypto/ec/ec_lcl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_lcl.h,v 1.11 2018/11/05 20:18:21 tb Exp $ */ 1/* $OpenBSD: ec_lcl.h,v 1.12 2019/01/19 01:07:00 tb Exp $ */
2/* 2/*
3 * Originally written by Bodo Moeller for the OpenSSL project. 3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */ 4 */
@@ -73,6 +73,7 @@
73 73
74#include <openssl/obj_mac.h> 74#include <openssl/obj_mac.h>
75#include <openssl/ec.h> 75#include <openssl/ec.h>
76#include <openssl/ecdsa.h>
76#include <openssl/bn.h> 77#include <openssl/bn.h>
77 78
78__BEGIN_HIDDEN_DECLS 79__BEGIN_HIDDEN_DECLS
@@ -245,6 +246,9 @@ struct ec_group_st {
245} /* EC_GROUP */; 246} /* EC_GROUP */;
246 247
247struct ec_key_st { 248struct ec_key_st {
249 const EC_KEY_METHOD *meth;
250 ENGINE *engine;
251
248 int version; 252 int version;
249 253
250 EC_GROUP *group; 254 EC_GROUP *group;
@@ -259,6 +263,7 @@ struct ec_key_st {
259 int flags; 263 int flags;
260 264
261 EC_EXTRA_DATA *method_data; 265 EC_EXTRA_DATA *method_data;
266 CRYPTO_EX_DATA ex_data;
262} /* EC_KEY */; 267} /* EC_KEY */;
263 268
264/* Basically a 'mixin' for extra data, but available for EC_GROUPs/EC_KEYs only 269/* Basically a 'mixin' for extra data, but available for EC_GROUPs/EC_KEYs only
@@ -441,6 +446,29 @@ int ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
441const EC_METHOD *EC_GFp_nistz256_method(void); 446const EC_METHOD *EC_GFp_nistz256_method(void);
442#endif 447#endif
443 448
449/* EC_METHOD definitions */
450
451struct ec_key_method_st {
452 const char *name;
453 int32_t flags;
454 int (*init)(EC_KEY *key);
455 void (*finish)(EC_KEY *key);
456 int (*copy)(EC_KEY *dest, const EC_KEY *src);
457 int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
458 int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
459 int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
460 int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
461 *sig, unsigned int *siglen, const BIGNUM *kinv,
462 const BIGNUM *r, EC_KEY *eckey);
463 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
464 BIGNUM **rp);
465 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
466 const BIGNUM *in_kinv, const BIGNUM *in_r,
467 EC_KEY *eckey);
468} /* EC_KEY_METHOD */;
469
470#define EC_KEY_METHOD_DYNAMIC 1
471
444/* method functions in ecp_nistp521.c */ 472/* method functions in ecp_nistp521.c */
445int ec_GFp_nistp521_group_init(EC_GROUP *group); 473int ec_GFp_nistp521_group_init(EC_GROUP *group);
446int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *n, BN_CTX *); 474int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *n, BN_CTX *);
diff --git a/src/lib/libcrypto/ecdsa/ecdsa.h b/src/lib/libcrypto/ecdsa/ecdsa.h
index 9c53230a88..12d6677ce3 100644
--- a/src/lib/libcrypto/ecdsa/ecdsa.h
+++ b/src/lib/libcrypto/ecdsa/ecdsa.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecdsa.h,v 1.5 2018/03/17 15:24:44 tb Exp $ */ 1/* $OpenBSD: ecdsa.h,v 1.6 2019/01/19 01:07:00 tb Exp $ */
2/** 2/**
3 * \file crypto/ecdsa/ecdsa.h Include file for the OpenSSL ECDSA functions 3 * \file crypto/ecdsa/ecdsa.h Include file for the OpenSSL ECDSA functions
4 * \author Written by Nils Larsch for the OpenSSL project 4 * \author Written by Nils Larsch for the OpenSSL project
@@ -269,6 +269,26 @@ int ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg);
269void *ECDSA_get_ex_data(EC_KEY *d, int idx); 269void *ECDSA_get_ex_data(EC_KEY *d, int idx);
270 270
271 271
272/* XXX should be in ec.h, but needs ECDSA_SIG */
273void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
274 int (*sign)(int type, const unsigned char *dgst,
275 int dlen, unsigned char *sig, unsigned int *siglen,
276 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey),
277 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
278 BIGNUM **kinvp, BIGNUM **rp),
279 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
280 int dgst_len, const BIGNUM *in_kinv, const BIGNUM *in_r,
281 EC_KEY *eckey));
282void EC_KEY_METHOD_get_sign(EC_KEY_METHOD *meth,
283 int (**psign)(int type, const unsigned char *dgst,
284 int dlen, unsigned char *sig, unsigned int *siglen,
285 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey),
286 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
287 BIGNUM **kinvp, BIGNUM **rp),
288 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
289 int dgst_len, const BIGNUM *in_kinv, const BIGNUM *in_r,
290 EC_KEY *eckey));
291
272/* BEGIN ERROR CODES */ 292/* BEGIN ERROR CODES */
273/* The following lines are auto generated by the script mkerr.pl. Any changes 293/* The following lines are auto generated by the script mkerr.pl. Any changes
274 * made after this point may be overwritten when the script is next run. 294 * made after this point may be overwritten when the script is next run.
diff --git a/src/lib/libcrypto/ecdsa/ecs_locl.h b/src/lib/libcrypto/ecdsa/ecs_locl.h
index 94e8874332..0a9f17908b 100644
--- a/src/lib/libcrypto/ecdsa/ecs_locl.h
+++ b/src/lib/libcrypto/ecdsa/ecs_locl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecs_locl.h,v 1.5 2016/12/21 15:49:29 jsing Exp $ */ 1/* $OpenBSD: ecs_locl.h,v 1.6 2019/01/19 01:07:00 tb Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project 3 * Written by Nils Larsch for the OpenSSL project
4 */ 4 */
@@ -81,6 +81,14 @@ typedef struct ecdsa_data_st {
81 */ 81 */
82ECDSA_DATA *ecdsa_check(EC_KEY *eckey); 82ECDSA_DATA *ecdsa_check(EC_KEY *eckey);
83 83
84int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
85 BIGNUM **rp);
86int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
87 unsigned char *sig, unsigned int *siglen, const BIGNUM *kinv,
88 const BIGNUM *r, EC_KEY *eckey);
89ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
90 const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey);
91
84__END_HIDDEN_DECLS 92__END_HIDDEN_DECLS
85 93
86#endif /* HEADER_ECS_LOCL_H */ 94#endif /* HEADER_ECS_LOCL_H */
diff --git a/src/lib/libcrypto/ecdsa/ecs_ossl.c b/src/lib/libcrypto/ecdsa/ecs_ossl.c
index 87d80642df..4e05cb9aac 100644
--- a/src/lib/libcrypto/ecdsa/ecs_ossl.c
+++ b/src/lib/libcrypto/ecdsa/ecs_ossl.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecs_ossl.c,v 1.16 2018/07/10 21:36:02 tb Exp $ */ 1/* $OpenBSD: ecs_ossl.c,v 1.17 2019/01/19 01:07:00 tb Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project 3 * Written by Nils Larsch for the OpenSSL project
4 */ 4 */
@@ -111,6 +111,21 @@ ecdsa_prepare_digest(const unsigned char *dgst, int dgst_len, BIGNUM *order,
111 return 1; 111 return 1;
112} 112}
113 113
114int
115ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen, unsigned char *sig,
116 unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
117{
118 ECDSA_SIG *s;
119
120 if ((s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey)) == NULL) {
121 *siglen = 0;
122 return 0;
123 }
124 *siglen = i2d_ECDSA_SIG(s, &sig);
125 ECDSA_SIG_free(s);
126 return 1;
127}
128
114static int 129static int
115ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) 130ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
116{ 131{
@@ -234,6 +249,16 @@ ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
234 return (ret); 249 return (ret);
235} 250}
236 251
252/* replace w/ ecdsa_sign_setup() when ECDSA_METHOD gets removed */
253int
254ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
255{
256 ECDSA_DATA *ecdsa;
257
258 if ((ecdsa = ecdsa_check(eckey)) == NULL)
259 return 0;
260 return ecdsa->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
261}
237 262
238static ECDSA_SIG * 263static ECDSA_SIG *
239ecdsa_do_sign(const unsigned char *dgst, int dgst_len, 264ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
@@ -384,6 +409,18 @@ ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
384 return ret; 409 return ret;
385} 410}
386 411
412/* replace w/ ecdsa_do_sign() when ECDSA_METHOD gets removed */
413ECDSA_SIG *
414ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
415 const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey)
416{
417 ECDSA_DATA *ecdsa;
418
419 if ((ecdsa = ecdsa_check(eckey)) == NULL)
420 return NULL;
421 return ecdsa->meth->ecdsa_do_sign(dgst, dgst_len, in_kinv, in_r, eckey);
422}
423
387static int 424static int
388ecdsa_do_verify(const unsigned char *dgst, int dgst_len, const ECDSA_SIG *sig, 425ecdsa_do_verify(const unsigned char *dgst, int dgst_len, const ECDSA_SIG *sig,
389 EC_KEY *eckey) 426 EC_KEY *eckey)
diff --git a/src/lib/libcrypto/ecdsa/ecs_sign.c b/src/lib/libcrypto/ecdsa/ecs_sign.c
index 029a0cb562..5beb853b94 100644
--- a/src/lib/libcrypto/ecdsa/ecs_sign.c
+++ b/src/lib/libcrypto/ecdsa/ecs_sign.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecs_sign.c,v 1.6 2015/02/08 13:35:07 jsing Exp $ */ 1/* $OpenBSD: ecs_sign.c,v 1.7 2019/01/19 01:07:00 tb Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -55,11 +55,13 @@
55 55
56#include <openssl/opensslconf.h> 56#include <openssl/opensslconf.h>
57 57
58#include "ecs_locl.h"
59#ifndef OPENSSL_NO_ENGINE 58#ifndef OPENSSL_NO_ENGINE
60#include <openssl/engine.h> 59#include <openssl/engine.h>
61#endif 60#endif
62 61
62#include "ecs_locl.h"
63#include "ec_lcl.h"
64
63ECDSA_SIG * 65ECDSA_SIG *
64ECDSA_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) 66ECDSA_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
65{ 67{
@@ -70,11 +72,10 @@ ECDSA_SIG *
70ECDSA_do_sign_ex(const unsigned char *dgst, int dlen, const BIGNUM *kinv, 72ECDSA_do_sign_ex(const unsigned char *dgst, int dlen, const BIGNUM *kinv,
71 const BIGNUM *rp, EC_KEY *eckey) 73 const BIGNUM *rp, EC_KEY *eckey)
72{ 74{
73 ECDSA_DATA *ecdsa = ecdsa_check(eckey); 75 if (eckey->meth->sign_sig != NULL)
74 76 return eckey->meth->sign_sig(dgst, dlen, kinv, rp, eckey);
75 if (ecdsa == NULL) 77 ECDSAerror(EVP_R_METHOD_NOT_SUPPORTED);
76 return NULL; 78 return 0;
77 return ecdsa->meth->ecdsa_do_sign(dgst, dlen, kinv, rp, eckey);
78} 79}
79 80
80int 81int
@@ -88,24 +89,17 @@ int
88ECDSA_sign_ex(int type, const unsigned char *dgst, int dlen, unsigned char *sig, 89ECDSA_sign_ex(int type, const unsigned char *dgst, int dlen, unsigned char *sig,
89 unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey) 90 unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
90{ 91{
91 ECDSA_SIG *s; 92 if (eckey->meth->sign != NULL)
92 93 return eckey->meth->sign(type, dgst, dlen, sig, siglen, kinv, r, eckey);
93 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey); 94 ECDSAerror(EVP_R_METHOD_NOT_SUPPORTED);
94 if (s == NULL) { 95 return 0;
95 *siglen = 0;
96 return 0;
97 }
98 *siglen = i2d_ECDSA_SIG(s, &sig);
99 ECDSA_SIG_free(s);
100 return 1;
101} 96}
102 97
103int 98int
104ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) 99ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
105{ 100{
106 ECDSA_DATA *ecdsa = ecdsa_check(eckey); 101 if (eckey->meth->sign_setup != NULL)
107 102 return eckey->meth->sign_setup(eckey, ctx_in, kinvp, rp);
108 if (ecdsa == NULL) 103 ECDSAerror(EVP_R_METHOD_NOT_SUPPORTED);
109 return 0; 104 return 0;
110 return ecdsa->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
111} 105}
diff --git a/src/lib/libcrypto/engine/eng_fat.c b/src/lib/libcrypto/engine/eng_fat.c
index c97695a7d3..baf1a54883 100644
--- a/src/lib/libcrypto/engine/eng_fat.c
+++ b/src/lib/libcrypto/engine/eng_fat.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: eng_fat.c,v 1.16 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: eng_fat.c,v 1.17 2019/01/19 01:07:00 tb Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -93,6 +93,10 @@ ENGINE_set_default(ENGINE *e, unsigned int flags)
93 if ((flags & ENGINE_METHOD_ECDSA) && !ENGINE_set_default_ECDSA(e)) 93 if ((flags & ENGINE_METHOD_ECDSA) && !ENGINE_set_default_ECDSA(e))
94 return 0; 94 return 0;
95#endif 95#endif
96#ifndef OPENSSL_NO_EC
97 if ((flags & ENGINE_METHOD_EC) && !ENGINE_set_default_EC(e))
98 return 0;
99#endif
96 if ((flags & ENGINE_METHOD_RAND) && !ENGINE_set_default_RAND(e)) 100 if ((flags & ENGINE_METHOD_RAND) && !ENGINE_set_default_RAND(e))
97 return 0; 101 return 0;
98 if ((flags & ENGINE_METHOD_PKEY_METHS) && 102 if ((flags & ENGINE_METHOD_PKEY_METHS) &&
@@ -123,6 +127,8 @@ int_def_cb(const char *alg, int len, void *arg)
123 *pflags |= ENGINE_METHOD_ECDSA; 127 *pflags |= ENGINE_METHOD_ECDSA;
124 else if (!strncmp(alg, "DH", len)) 128 else if (!strncmp(alg, "DH", len))
125 *pflags |= ENGINE_METHOD_DH; 129 *pflags |= ENGINE_METHOD_DH;
130 else if (strncmp(alg, "EC", len) == 0)
131 *pflags |= ENGINE_METHOD_EC;
126 else if (!strncmp(alg, "RAND", len)) 132 else if (!strncmp(alg, "RAND", len))
127 *pflags |= ENGINE_METHOD_RAND; 133 *pflags |= ENGINE_METHOD_RAND;
128 else if (!strncmp(alg, "CIPHERS", len)) 134 else if (!strncmp(alg, "CIPHERS", len))
@@ -174,6 +180,9 @@ ENGINE_register_complete(ENGINE *e)
174#ifndef OPENSSL_NO_ECDSA 180#ifndef OPENSSL_NO_ECDSA
175 ENGINE_register_ECDSA(e); 181 ENGINE_register_ECDSA(e);
176#endif 182#endif
183#ifndef OPENSSL_NO_EC
184 ENGINE_register_EC(e);
185#endif
177 ENGINE_register_RAND(e); 186 ENGINE_register_RAND(e);
178 ENGINE_register_pkey_meths(e); 187 ENGINE_register_pkey_meths(e);
179 return 1; 188 return 1;
diff --git a/src/lib/libcrypto/engine/eng_int.h b/src/lib/libcrypto/engine/eng_int.h
index dbb639949d..298c0e327f 100644
--- a/src/lib/libcrypto/engine/eng_int.h
+++ b/src/lib/libcrypto/engine/eng_int.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: eng_int.h,v 1.9 2016/12/21 15:49:29 jsing Exp $ */ 1/* $OpenBSD: eng_int.h,v 1.10 2019/01/19 01:07:00 tb Exp $ */
2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL 2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
@@ -159,6 +159,7 @@ struct engine_st {
159 const DH_METHOD *dh_meth; 159 const DH_METHOD *dh_meth;
160 const ECDH_METHOD *ecdh_meth; 160 const ECDH_METHOD *ecdh_meth;
161 const ECDSA_METHOD *ecdsa_meth; 161 const ECDSA_METHOD *ecdsa_meth;
162 const EC_KEY_METHOD *ec_meth;
162 const RAND_METHOD *rand_meth; 163 const RAND_METHOD *rand_meth;
163 const STORE_METHOD *store_meth; 164 const STORE_METHOD *store_meth;
164 /* Cipher handling is via this callback */ 165 /* Cipher handling is via this callback */
diff --git a/src/lib/libcrypto/engine/eng_list.c b/src/lib/libcrypto/engine/eng_list.c
index 134866d2c6..b29b4102e4 100644
--- a/src/lib/libcrypto/engine/eng_list.c
+++ b/src/lib/libcrypto/engine/eng_list.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: eng_list.c,v 1.23 2018/08/24 19:25:31 tb Exp $ */ 1/* $OpenBSD: eng_list.c,v 1.24 2019/01/19 01:07:00 tb Exp $ */
2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL 2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
@@ -316,6 +316,9 @@ engine_cpy(ENGINE *dest, const ENGINE *src)
316#ifndef OPENSSL_NO_ECDSA 316#ifndef OPENSSL_NO_ECDSA
317 dest->ecdsa_meth = src->ecdsa_meth; 317 dest->ecdsa_meth = src->ecdsa_meth;
318#endif 318#endif
319#ifndef OPENSSL_NO_EC
320 dest->ec_meth = src->ec_meth;
321#endif
319 dest->rand_meth = src->rand_meth; 322 dest->rand_meth = src->rand_meth;
320 dest->store_meth = src->store_meth; 323 dest->store_meth = src->store_meth;
321 dest->ciphers = src->ciphers; 324 dest->ciphers = src->ciphers;
diff --git a/src/lib/libcrypto/engine/engine.h b/src/lib/libcrypto/engine/engine.h
index 0f603feaaf..dc14be8e38 100644
--- a/src/lib/libcrypto/engine/engine.h
+++ b/src/lib/libcrypto/engine/engine.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: engine.h,v 1.32 2018/11/11 06:41:28 bcook Exp $ */ 1/* $OpenBSD: engine.h,v 1.33 2019/01/19 01:07:00 tb Exp $ */
2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL 2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
@@ -87,6 +87,9 @@
87#ifndef OPENSSL_NO_ECDSA 87#ifndef OPENSSL_NO_ECDSA
88#include <openssl/ecdsa.h> 88#include <openssl/ecdsa.h>
89#endif 89#endif
90#ifndef OPENSSL_NO_EC
91#include <openssl/ec.h>
92#endif
90#include <openssl/ui.h> 93#include <openssl/ui.h>
91#include <openssl/err.h> 94#include <openssl/err.h>
92#endif 95#endif
@@ -112,6 +115,7 @@ extern "C" {
112#define ENGINE_METHOD_STORE (unsigned int)0x0100 115#define ENGINE_METHOD_STORE (unsigned int)0x0100
113#define ENGINE_METHOD_PKEY_METHS (unsigned int)0x0200 116#define ENGINE_METHOD_PKEY_METHS (unsigned int)0x0200
114#define ENGINE_METHOD_PKEY_ASN1_METHS (unsigned int)0x0400 117#define ENGINE_METHOD_PKEY_ASN1_METHS (unsigned int)0x0400
118#define ENGINE_METHOD_EC (unsigned int)0x0800
115/* Obvious all-or-nothing cases. */ 119/* Obvious all-or-nothing cases. */
116#define ENGINE_METHOD_ALL (unsigned int)0xFFFF 120#define ENGINE_METHOD_ALL (unsigned int)0xFFFF
117#define ENGINE_METHOD_NONE (unsigned int)0x0000 121#define ENGINE_METHOD_NONE (unsigned int)0x0000
@@ -353,6 +357,10 @@ int ENGINE_register_ECDSA(ENGINE *e);
353void ENGINE_unregister_ECDSA(ENGINE *e); 357void ENGINE_unregister_ECDSA(ENGINE *e);
354void ENGINE_register_all_ECDSA(void); 358void ENGINE_register_all_ECDSA(void);
355 359
360int ENGINE_register_EC(ENGINE *e);
361void ENGINE_unregister_EC(ENGINE *e);
362void ENGINE_register_all_EC(void);
363
356int ENGINE_register_DH(ENGINE *e); 364int ENGINE_register_DH(ENGINE *e);
357void ENGINE_unregister_DH(ENGINE *e); 365void ENGINE_unregister_DH(ENGINE *e);
358void ENGINE_register_all_DH(void); 366void ENGINE_register_all_DH(void);
@@ -447,6 +455,7 @@ int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth);
447int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth); 455int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth);
448int ENGINE_set_ECDH(ENGINE *e, const ECDH_METHOD *ecdh_meth); 456int ENGINE_set_ECDH(ENGINE *e, const ECDH_METHOD *ecdh_meth);
449int ENGINE_set_ECDSA(ENGINE *e, const ECDSA_METHOD *ecdsa_meth); 457int ENGINE_set_ECDSA(ENGINE *e, const ECDSA_METHOD *ecdsa_meth);
458int ENGINE_set_EC(ENGINE *e, const EC_KEY_METHOD *ec_meth);
450int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth); 459int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth);
451int ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth); 460int ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth);
452int ENGINE_set_STORE(ENGINE *e, const STORE_METHOD *store_meth); 461int ENGINE_set_STORE(ENGINE *e, const STORE_METHOD *store_meth);
@@ -486,6 +495,7 @@ const RSA_METHOD *ENGINE_get_RSA(const ENGINE *e);
486const DSA_METHOD *ENGINE_get_DSA(const ENGINE *e); 495const DSA_METHOD *ENGINE_get_DSA(const ENGINE *e);
487const ECDH_METHOD *ENGINE_get_ECDH(const ENGINE *e); 496const ECDH_METHOD *ENGINE_get_ECDH(const ENGINE *e);
488const ECDSA_METHOD *ENGINE_get_ECDSA(const ENGINE *e); 497const ECDSA_METHOD *ENGINE_get_ECDSA(const ENGINE *e);
498const EC_KEY_METHOD *ENGINE_get_EC(const ENGINE *e);
489const DH_METHOD *ENGINE_get_DH(const ENGINE *e); 499const DH_METHOD *ENGINE_get_DH(const ENGINE *e);
490const RAND_METHOD *ENGINE_get_RAND(const ENGINE *e); 500const RAND_METHOD *ENGINE_get_RAND(const ENGINE *e);
491const STORE_METHOD *ENGINE_get_STORE(const ENGINE *e); 501const STORE_METHOD *ENGINE_get_STORE(const ENGINE *e);
@@ -553,6 +563,7 @@ ENGINE *ENGINE_get_default_RSA(void);
553ENGINE *ENGINE_get_default_DSA(void); 563ENGINE *ENGINE_get_default_DSA(void);
554ENGINE *ENGINE_get_default_ECDH(void); 564ENGINE *ENGINE_get_default_ECDH(void);
555ENGINE *ENGINE_get_default_ECDSA(void); 565ENGINE *ENGINE_get_default_ECDSA(void);
566ENGINE *ENGINE_get_default_EC(void);
556ENGINE *ENGINE_get_default_DH(void); 567ENGINE *ENGINE_get_default_DH(void);
557ENGINE *ENGINE_get_default_RAND(void); 568ENGINE *ENGINE_get_default_RAND(void);
558/* These functions can be used to get a functional reference to perform 569/* These functions can be used to get a functional reference to perform
@@ -572,6 +583,7 @@ int ENGINE_set_default_string(ENGINE *e, const char *def_list);
572int ENGINE_set_default_DSA(ENGINE *e); 583int ENGINE_set_default_DSA(ENGINE *e);
573int ENGINE_set_default_ECDH(ENGINE *e); 584int ENGINE_set_default_ECDH(ENGINE *e);
574int ENGINE_set_default_ECDSA(ENGINE *e); 585int ENGINE_set_default_ECDSA(ENGINE *e);
586int ENGINE_set_default_EC(ENGINE *e);
575int ENGINE_set_default_DH(ENGINE *e); 587int ENGINE_set_default_DH(ENGINE *e);
576int ENGINE_set_default_RAND(ENGINE *e); 588int ENGINE_set_default_RAND(ENGINE *e);
577int ENGINE_set_default_ciphers(ENGINE *e); 589int ENGINE_set_default_ciphers(ENGINE *e);
diff --git a/src/lib/libcrypto/engine/tb_eckey.c b/src/lib/libcrypto/engine/tb_eckey.c
new file mode 100644
index 0000000000..dd30815788
--- /dev/null
+++ b/src/lib/libcrypto/engine/tb_eckey.c
@@ -0,0 +1,123 @@
1/* ====================================================================
2 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20 *
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * licensing@OpenSSL.org.
25 *
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com).
52 *
53 */
54
55#include "eng_int.h"
56
57static ENGINE_TABLE *ec_table = NULL;
58static const int dummy_nid = 1;
59
60void
61ENGINE_unregister_EC(ENGINE *e)
62{
63 engine_table_unregister(&ec_table, e);
64}
65
66static void
67engine_unregister_all_EC(void)
68{
69 engine_table_cleanup(&ec_table);
70}
71
72int
73ENGINE_register_EC(ENGINE *e)
74{
75 if (e->ec_meth)
76 return engine_table_register(&ec_table,
77 engine_unregister_all_EC, e, &dummy_nid, 1, 0);
78 return 1;
79}
80
81void
82ENGINE_register_all_EC(void)
83{
84 ENGINE *e;
85
86 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e))
87 ENGINE_register_EC(e);
88}
89
90int
91ENGINE_set_default_EC(ENGINE *e)
92{
93 if (e->ec_meth != NULL)
94 return engine_table_register(&ec_table,
95 engine_unregister_all_EC, e, &dummy_nid, 1, 1);
96 return 1;
97}
98
99/*
100 * Exposed API function to get a functional reference from the implementation
101 * table (ie. try to get a functional reference from the tabled structural
102 * references).
103 */
104ENGINE *
105ENGINE_get_default_EC(void)
106{
107 return engine_table_select(&ec_table, dummy_nid);
108}
109
110/* Obtains an EC_KEY implementation from an ENGINE functional reference */
111const EC_KEY_METHOD *
112ENGINE_get_EC(const ENGINE *e)
113{
114 return e->ec_meth;
115}
116
117/* Sets an EC_KEY implementation in an ENGINE structure */
118int
119ENGINE_set_EC(ENGINE *e, const EC_KEY_METHOD *ec_meth)
120{
121 e->ec_meth = ec_meth;
122 return 1;
123}