diff options
author | tb <> | 2019-01-19 01:07:00 +0000 |
---|---|---|
committer | tb <> | 2019-01-19 01:07:00 +0000 |
commit | dc38b357c3a6e0db4a7172af29148961b86b0724 (patch) | |
tree | f28042e7a3c924e4bf846d6cded984e02699177d /src/lib/libcrypto/ec | |
parent | f11f1c0f8ad579cfb88a2559e3efe0e0367cec85 (diff) | |
download | openbsd-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
Diffstat (limited to 'src/lib/libcrypto/ec')
-rw-r--r-- | src/lib/libcrypto/ec/ec.h | 31 | ||||
-rw-r--r-- | src/lib/libcrypto/ec/ec_key.c | 91 | ||||
-rw-r--r-- | src/lib/libcrypto/ec/ec_kmeth.c | 272 | ||||
-rw-r--r-- | src/lib/libcrypto/ec/ec_lcl.h | 30 |
4 files changed, 401 insertions, 23 deletions
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 | ||
707 | typedef struct ec_key_st EC_KEY; | 707 | typedef struct ec_key_st EC_KEY; |
708 | typedef 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 | */ |
946 | int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off); | 947 | int 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) | ||
951 | int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg); | ||
952 | void *EC_KEY_get_ex_data(const EC_KEY *key, int idx); | ||
953 | |||
954 | const EC_KEY_METHOD *EC_KEY_OpenSSL(void); | ||
955 | const EC_KEY_METHOD *EC_KEY_get_default_method(void); | ||
956 | void EC_KEY_set_default_method(const EC_KEY_METHOD *meth); | ||
957 | const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key); | ||
958 | int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth); | ||
959 | EC_KEY *EC_KEY_new_method(ENGINE *engine); | ||
960 | EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth); | ||
961 | void EC_KEY_METHOD_free(EC_KEY_METHOD *meth); | ||
962 | void 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)); | ||
969 | void 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 | |||
948 | EC_KEY *ECParameters_dup(EC_KEY *key); | 977 | EC_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 | ||
72 | EC_KEY * | 76 | EC_KEY * |
73 | EC_KEY_new(void) | 77 | EC_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 | ||
94 | EC_KEY * | 82 | EC_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 | ||
190 | EC_KEY * | 217 | EC_KEY * |
191 | EC_KEY_dup(const EC_KEY * ec_key) | 218 | EC_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 | ||
238 | int | ||
239 | EC_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 | |||
244 | void * | ||
245 | EC_KEY_get_ex_data(const EC_KEY *r, int idx) | ||
246 | { | ||
247 | return CRYPTO_get_ex_data(&r->ex_data, idx); | ||
248 | } | ||
249 | |||
210 | int | 250 | int |
211 | EC_KEY_generate_key(EC_KEY *eckey) | 251 | EC_KEY_generate_key(EC_KEY *eckey) |
212 | { | 252 | { |
@@ -407,6 +447,9 @@ EC_KEY_get0_group(const EC_KEY * key) | |||
407 | int | 447 | int |
408 | EC_KEY_set_group(EC_KEY * key, const EC_GROUP * group) | 448 | EC_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) | |||
421 | int | 464 | int |
422 | EC_KEY_set_private_key(EC_KEY * key, const BIGNUM * priv_key) | 465 | EC_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) | |||
435 | int | 481 | int |
436 | EC_KEY_set_public_key(EC_KEY * key, const EC_POINT * pub_key) | 482 | EC_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 | |||
63 | static 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 | |||
80 | const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method; | ||
81 | |||
82 | const EC_KEY_METHOD * | ||
83 | EC_KEY_OpenSSL(void) | ||
84 | { | ||
85 | return &openssl_ec_key_method; | ||
86 | } | ||
87 | |||
88 | const EC_KEY_METHOD * | ||
89 | EC_KEY_get_default_method(void) | ||
90 | { | ||
91 | return default_ec_key_meth; | ||
92 | } | ||
93 | |||
94 | void | ||
95 | EC_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 | |||
103 | const EC_KEY_METHOD * | ||
104 | EC_KEY_get_method(const EC_KEY *key) | ||
105 | { | ||
106 | return key->meth; | ||
107 | } | ||
108 | |||
109 | int | ||
110 | EC_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 | |||
128 | EC_KEY * | ||
129 | EC_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 | |||
177 | EC_KEY_METHOD * | ||
178 | EC_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 | |||
190 | void | ||
191 | EC_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 | |||
199 | void | ||
200 | EC_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 | |||
222 | void | ||
223 | EC_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 | |||
239 | void | ||
240 | EC_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 | |||
258 | void | ||
259 | EC_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 | ||
247 | struct ec_key_st { | 248 | struct 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); | |||
441 | const EC_METHOD *EC_GFp_nistz256_method(void); | 446 | const EC_METHOD *EC_GFp_nistz256_method(void); |
442 | #endif | 447 | #endif |
443 | 448 | ||
449 | /* EC_METHOD definitions */ | ||
450 | |||
451 | struct 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 */ |
445 | int ec_GFp_nistp521_group_init(EC_GROUP *group); | 473 | int ec_GFp_nistp521_group_init(EC_GROUP *group); |
446 | int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *n, BN_CTX *); | 474 | int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *n, BN_CTX *); |