diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib/libcrypto/ec/ec_kmeth.c | 272 |
1 files changed, 272 insertions, 0 deletions
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 | } | ||