]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ecdsa/ecs_lib.c
Run util/openssl-format-source -v -c .
[thirdparty/openssl.git] / crypto / ecdsa / ecs_lib.c
CommitLineData
4d94ae00
BM
1/* crypto/ecdsa/ecs_lib.c */
2/* ====================================================================
9dd84053 3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4d94ae00
BM
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
ae5c8664 10 * notice, this list of conditions and the following disclaimer.
4d94ae00
BM
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
4d94ae00 55
5454829a 56#include <string.h>
6a50d0a4 57#include "ecs_locl.h"
58ae65cd 58#ifndef OPENSSL_NO_ENGINE
ae5c8664 59# include <openssl/engine.h>
58ae65cd 60#endif
60a938c6 61#include <openssl/err.h>
0f814687 62#include <openssl/bn.h>
6342b6e3 63#ifdef OPENSSL_FIPS
ae5c8664 64# include <openssl/fips.h>
6342b6e3 65#endif
4d94ae00 66
ae5c8664 67const char ECDSA_version[] = "ECDSA" OPENSSL_VERSION_PTEXT;
4d94ae00
BM
68
69static const ECDSA_METHOD *default_ECDSA_method = NULL;
70
bbbd6710 71static void *ecdsa_data_new(void);
9dd84053 72static void *ecdsa_data_dup(void *);
ae5c8664 73static void ecdsa_data_free(void *);
9dd84053 74
4d94ae00
BM
75void ECDSA_set_default_method(const ECDSA_METHOD *meth)
76{
ae5c8664 77 default_ECDSA_method = meth;
4d94ae00
BM
78}
79
80const ECDSA_METHOD *ECDSA_get_default_method(void)
81{
ae5c8664 82 if (!default_ECDSA_method) {
6342b6e3 83#ifdef OPENSSL_FIPS
ae5c8664
MC
84 if (FIPS_mode())
85 return FIPS_ecdsa_openssl();
86 else
87 return ECDSA_OpenSSL();
3a5b97b7 88#else
ae5c8664 89 default_ECDSA_method = ECDSA_OpenSSL();
6342b6e3 90#endif
ae5c8664
MC
91 }
92 return default_ECDSA_method;
4d94ae00
BM
93}
94
14a7cfb3 95int ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth)
4d94ae00 96{
ae5c8664 97 ECDSA_DATA *ecdsa;
14a7cfb3 98
ae5c8664 99 ecdsa = ecdsa_check(eckey);
14a7cfb3 100
ae5c8664
MC
101 if (ecdsa == NULL)
102 return 0;
14a7cfb3 103
58ae65cd 104#ifndef OPENSSL_NO_ENGINE
ae5c8664
MC
105 if (ecdsa->engine) {
106 ENGINE_finish(ecdsa->engine);
107 ecdsa->engine = NULL;
108 }
58ae65cd 109#endif
ae5c8664 110 ecdsa->meth = meth;
4d94ae00 111
ae5c8664 112 return 1;
14a7cfb3
BM
113}
114
9dd84053 115static ECDSA_DATA *ECDSA_DATA_new_method(ENGINE *engine)
4d94ae00 116{
ae5c8664 117 ECDSA_DATA *ret;
4d94ae00 118
ae5c8664
MC
119 ret = (ECDSA_DATA *)OPENSSL_malloc(sizeof(ECDSA_DATA));
120 if (ret == NULL) {
121 ECDSAerr(ECDSA_F_ECDSA_DATA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
122 return (NULL);
123 }
4d94ae00 124
ae5c8664 125 ret->init = NULL;
14a7cfb3 126
ae5c8664
MC
127 ret->meth = ECDSA_get_default_method();
128 ret->engine = engine;
58ae65cd 129#ifndef OPENSSL_NO_ENGINE
ae5c8664
MC
130 if (!ret->engine)
131 ret->engine = ENGINE_get_default_ECDSA();
132 if (ret->engine) {
133 ret->meth = ENGINE_get_ECDSA(ret->engine);
134 if (!ret->meth) {
135 ECDSAerr(ECDSA_F_ECDSA_DATA_NEW_METHOD, ERR_R_ENGINE_LIB);
136 ENGINE_finish(ret->engine);
137 OPENSSL_free(ret);
138 return NULL;
139 }
140 }
58ae65cd 141#endif
4d94ae00 142
ae5c8664
MC
143 ret->flags = ret->meth->flags;
144 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDSA, ret, &ret->ex_data);
14a7cfb3 145#if 0
ae5c8664
MC
146 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
147 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDSA, ret, &ret->ex_data);
148 OPENSSL_free(ret);
149 ret = NULL;
150 }
151#endif
152 return (ret);
4d94ae00
BM
153}
154
bbbd6710 155static void *ecdsa_data_new(void)
4d94ae00 156{
ae5c8664 157 return (void *)ECDSA_DATA_new_method(NULL);
9dd84053
NL
158}
159
160static void *ecdsa_data_dup(void *data)
161{
ae5c8664 162 ECDSA_DATA *r = (ECDSA_DATA *)data;
9dd84053 163
ae5c8664
MC
164 /* XXX: dummy operation */
165 if (r == NULL)
166 return NULL;
9dd84053 167
ae5c8664 168 return ecdsa_data_new();
9dd84053
NL
169}
170
171static void ecdsa_data_free(void *data)
172{
ae5c8664 173 ECDSA_DATA *r = (ECDSA_DATA *)data;
4d94ae00 174
58ae65cd 175#ifndef OPENSSL_NO_ENGINE
ae5c8664
MC
176 if (r->engine)
177 ENGINE_finish(r->engine);
58ae65cd 178#endif
ae5c8664 179 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDSA, r, &r->ex_data);
4d94ae00 180
ae5c8664 181 OPENSSL_cleanse((void *)r, sizeof(ECDSA_DATA));
14a7cfb3 182
ae5c8664 183 OPENSSL_free(r);
4d94ae00
BM
184}
185
14a7cfb3
BM
186ECDSA_DATA *ecdsa_check(EC_KEY *key)
187{
ae5c8664
MC
188 ECDSA_DATA *ecdsa_data;
189
190 void *data = EC_KEY_get_key_method_data(key, ecdsa_data_dup,
191 ecdsa_data_free, ecdsa_data_free);
192 if (data == NULL) {
193 ecdsa_data = (ECDSA_DATA *)ecdsa_data_new();
194 if (ecdsa_data == NULL)
195 return NULL;
196 data = EC_KEY_insert_key_method_data(key, (void *)ecdsa_data,
197 ecdsa_data_dup, ecdsa_data_free,
198 ecdsa_data_free);
199 if (data != NULL) {
200 /*
201 * Another thread raced us to install the key_method data and
202 * won.
203 */
204 ecdsa_data_free(ecdsa_data);
205 ecdsa_data = (ECDSA_DATA *)data;
206 }
207 } else
208 ecdsa_data = (ECDSA_DATA *)data;
b6d63b25 209#ifdef OPENSSL_FIPS
ae5c8664
MC
210 if (FIPS_mode() && !(ecdsa_data->flags & ECDSA_FLAG_FIPS_METHOD)
211 && !(EC_KEY_get_flags(key) & EC_FLAG_NON_FIPS_ALLOW)) {
212 ECDSAerr(ECDSA_F_ECDSA_CHECK, ECDSA_R_NON_FIPS_METHOD);
213 return NULL;
214 }
b6d63b25 215#endif
14a7cfb3 216
ae5c8664 217 return ecdsa_data;
14a7cfb3
BM
218}
219
220int ECDSA_size(const EC_KEY *r)
4d94ae00 221{
ae5c8664
MC
222 int ret, i;
223 ASN1_INTEGER bs;
224 BIGNUM *order = NULL;
225 unsigned char buf[4];
226 const EC_GROUP *group;
227
228 if (r == NULL)
229 return 0;
230 group = EC_KEY_get0_group(r);
231 if (group == NULL)
232 return 0;
233
234 if ((order = BN_new()) == NULL)
235 return 0;
236 if (!EC_GROUP_get_order(group, order, NULL)) {
237 BN_clear_free(order);
238 return 0;
239 }
240 i = BN_num_bits(order);
241 bs.length = (i + 7) / 8;
242 bs.data = buf;
243 bs.type = V_ASN1_INTEGER;
244 /* If the top bit is set the asn1 encoding is 1 larger. */
245 buf[0] = 0xff;
246
247 i = i2d_ASN1_INTEGER(&bs, NULL);
248 i += i; /* r and s */
249 ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
250 BN_clear_free(order);
251 return (ret);
4d94ae00
BM
252}
253
254int ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
ae5c8664 255 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
4d94ae00 256{
ae5c8664
MC
257 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDSA, argl, argp,
258 new_func, dup_func, free_func);
4d94ae00
BM
259}
260
14a7cfb3 261int ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg)
4d94ae00 262{
ae5c8664
MC
263 ECDSA_DATA *ecdsa;
264 ecdsa = ecdsa_check(d);
265 if (ecdsa == NULL)
266 return 0;
267 return (CRYPTO_set_ex_data(&ecdsa->ex_data, idx, arg));
0bee0e62
BM
268}
269
14a7cfb3 270void *ECDSA_get_ex_data(EC_KEY *d, int idx)
0bee0e62 271{
ae5c8664
MC
272 ECDSA_DATA *ecdsa;
273 ecdsa = ecdsa_check(d);
274 if (ecdsa == NULL)
275 return NULL;
276 return (CRYPTO_get_ex_data(&ecdsa->ex_data, idx));
0bee0e62 277}
7c23127f
DSH
278
279ECDSA_METHOD *ECDSA_METHOD_new(ECDSA_METHOD *ecdsa_meth)
ae5c8664
MC
280{
281 ECDSA_METHOD *ret;
282
283 ret = OPENSSL_malloc(sizeof(ECDSA_METHOD));
284 if (ret == NULL) {
285 ECDSAerr(ECDSA_F_ECDSA_METHOD_NEW, ERR_R_MALLOC_FAILURE);
286 return NULL;
287 }
288
289 if (ecdsa_meth)
290 *ret = *ecdsa_meth;
291 else {
292 ret->ecdsa_sign_setup = 0;
293 ret->ecdsa_do_sign = 0;
294 ret->ecdsa_do_verify = 0;
295 ret->name = NULL;
296 ret->flags = 0;
297 }
298 ret->flags |= ECDSA_METHOD_FLAG_ALLOCATED;
299 return ret;
300}
7c23127f
DSH
301
302void ECDSA_METHOD_set_sign(ECDSA_METHOD *ecdsa_method,
ae5c8664
MC
303 ECDSA_SIG *(*ecdsa_do_sign) (const unsigned char
304 *dgst, int dgst_len,
305 const BIGNUM *inv,
306 const BIGNUM *rp,
307 EC_KEY *eckey))
308{
309 ecdsa_method->ecdsa_do_sign = ecdsa_do_sign;
310}
7c23127f
DSH
311
312void ECDSA_METHOD_set_sign_setup(ECDSA_METHOD *ecdsa_method,
ae5c8664
MC
313 int (*ecdsa_sign_setup) (EC_KEY *eckey,
314 BN_CTX *ctx,
315 BIGNUM **kinv,
316 BIGNUM **r))
317{
318 ecdsa_method->ecdsa_sign_setup = ecdsa_sign_setup;
319}
7c23127f
DSH
320
321void ECDSA_METHOD_set_verify(ECDSA_METHOD *ecdsa_method,
ae5c8664
MC
322 int (*ecdsa_do_verify) (const unsigned char
323 *dgst, int dgst_len,
324 const ECDSA_SIG *sig,
325 EC_KEY *eckey))
326{
327 ecdsa_method->ecdsa_do_verify = ecdsa_do_verify;
328}
7c23127f
DSH
329
330void ECDSA_METHOD_set_flags(ECDSA_METHOD *ecdsa_method, int flags)
ae5c8664
MC
331{
332 ecdsa_method->flags = flags | ECDSA_METHOD_FLAG_ALLOCATED;
333}
7c23127f
DSH
334
335void ECDSA_METHOD_set_name(ECDSA_METHOD *ecdsa_method, char *name)
ae5c8664
MC
336{
337 ecdsa_method->name = name;
338}
7c23127f
DSH
339
340void ECDSA_METHOD_free(ECDSA_METHOD *ecdsa_method)
ae5c8664
MC
341{
342 if (ecdsa_method->flags & ECDSA_METHOD_FLAG_ALLOCATED)
343 OPENSSL_free(ecdsa_method);
344}
7c23127f 345
654ae3d6 346void ECDSA_METHOD_set_app_data(ECDSA_METHOD *ecdsa_method, void *app)
ae5c8664
MC
347{
348 ecdsa_method->app_data = app;
349}
350
351void *ECDSA_METHOD_get_app_data(ECDSA_METHOD *ecdsa_method)
352{
353 return ecdsa_method->app_data;
354}