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