1160814Ssimon/* crypto/ecdsa/ecs_lib.c */
2160814Ssimon/* ====================================================================
3160814Ssimon * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
4160814Ssimon *
5160814Ssimon * Redistribution and use in source and binary forms, with or without
6160814Ssimon * modification, are permitted provided that the following conditions
7160814Ssimon * are met:
8160814Ssimon *
9160814Ssimon * 1. Redistributions of source code must retain the above copyright
10296465Sdelphij *    notice, this list of conditions and the following disclaimer.
11160814Ssimon *
12160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
13160814Ssimon *    notice, this list of conditions and the following disclaimer in
14160814Ssimon *    the documentation and/or other materials provided with the
15160814Ssimon *    distribution.
16160814Ssimon *
17160814Ssimon * 3. All advertising materials mentioning features or use of this
18160814Ssimon *    software must display the following acknowledgment:
19160814Ssimon *    "This product includes software developed by the OpenSSL Project
20160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21160814Ssimon *
22160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23160814Ssimon *    endorse or promote products derived from this software without
24160814Ssimon *    prior written permission. For written permission, please contact
25160814Ssimon *    openssl-core@OpenSSL.org.
26160814Ssimon *
27160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
28160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
29160814Ssimon *    permission of the OpenSSL Project.
30160814Ssimon *
31160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
32160814Ssimon *    acknowledgment:
33160814Ssimon *    "This product includes software developed by the OpenSSL Project
34160814Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35160814Ssimon *
36160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
48160814Ssimon * ====================================================================
49160814Ssimon *
50160814Ssimon * This product includes cryptographic software written by Eric Young
51160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
52160814Ssimon * Hudson (tjh@cryptsoft.com).
53160814Ssimon *
54160814Ssimon */
55160814Ssimon
56160814Ssimon#include <string.h>
57160814Ssimon#include "ecs_locl.h"
58160814Ssimon#ifndef OPENSSL_NO_ENGINE
59296465Sdelphij# include <openssl/engine.h>
60160814Ssimon#endif
61160814Ssimon#include <openssl/err.h>
62160814Ssimon#include <openssl/bn.h>
63160814Ssimon
64296465Sdelphijconst char ECDSA_version[] = "ECDSA" OPENSSL_VERSION_PTEXT;
65160814Ssimon
66160814Ssimonstatic const ECDSA_METHOD *default_ECDSA_method = NULL;
67160814Ssimon
68160814Ssimonstatic void *ecdsa_data_new(void);
69160814Ssimonstatic void *ecdsa_data_dup(void *);
70296465Sdelphijstatic void ecdsa_data_free(void *);
71160814Ssimon
72160814Ssimonvoid ECDSA_set_default_method(const ECDSA_METHOD *meth)
73160814Ssimon{
74296465Sdelphij    default_ECDSA_method = meth;
75160814Ssimon}
76160814Ssimon
77160814Ssimonconst ECDSA_METHOD *ECDSA_get_default_method(void)
78160814Ssimon{
79296465Sdelphij    if (!default_ECDSA_method)
80296465Sdelphij        default_ECDSA_method = ECDSA_OpenSSL();
81296465Sdelphij    return default_ECDSA_method;
82160814Ssimon}
83160814Ssimon
84160814Ssimonint ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth)
85160814Ssimon{
86296465Sdelphij    ECDSA_DATA *ecdsa;
87160814Ssimon
88296465Sdelphij    ecdsa = ecdsa_check(eckey);
89160814Ssimon
90296465Sdelphij    if (ecdsa == NULL)
91296465Sdelphij        return 0;
92160814Ssimon
93160814Ssimon#ifndef OPENSSL_NO_ENGINE
94296465Sdelphij    if (ecdsa->engine) {
95296465Sdelphij        ENGINE_finish(ecdsa->engine);
96296465Sdelphij        ecdsa->engine = NULL;
97296465Sdelphij    }
98160814Ssimon#endif
99296465Sdelphij    ecdsa->meth = meth;
100160814Ssimon
101296465Sdelphij    return 1;
102160814Ssimon}
103160814Ssimon
104160814Ssimonstatic ECDSA_DATA *ECDSA_DATA_new_method(ENGINE *engine)
105160814Ssimon{
106296465Sdelphij    ECDSA_DATA *ret;
107160814Ssimon
108296465Sdelphij    ret = (ECDSA_DATA *)OPENSSL_malloc(sizeof(ECDSA_DATA));
109296465Sdelphij    if (ret == NULL) {
110296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DATA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
111296465Sdelphij        return (NULL);
112296465Sdelphij    }
113160814Ssimon
114296465Sdelphij    ret->init = NULL;
115160814Ssimon
116296465Sdelphij    ret->meth = ECDSA_get_default_method();
117296465Sdelphij    ret->engine = engine;
118160814Ssimon#ifndef OPENSSL_NO_ENGINE
119296465Sdelphij    if (!ret->engine)
120296465Sdelphij        ret->engine = ENGINE_get_default_ECDSA();
121296465Sdelphij    if (ret->engine) {
122296465Sdelphij        ret->meth = ENGINE_get_ECDSA(ret->engine);
123296465Sdelphij        if (!ret->meth) {
124296465Sdelphij            ECDSAerr(ECDSA_F_ECDSA_DATA_NEW_METHOD, ERR_R_ENGINE_LIB);
125296465Sdelphij            ENGINE_finish(ret->engine);
126296465Sdelphij            OPENSSL_free(ret);
127296465Sdelphij            return NULL;
128296465Sdelphij        }
129296465Sdelphij    }
130160814Ssimon#endif
131160814Ssimon
132296465Sdelphij    ret->flags = ret->meth->flags;
133296465Sdelphij    CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDSA, ret, &ret->ex_data);
134160814Ssimon#if 0
135296465Sdelphij    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
136296465Sdelphij        CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDSA, ret, &ret->ex_data);
137296465Sdelphij        OPENSSL_free(ret);
138296465Sdelphij        ret = NULL;
139296465Sdelphij    }
140296465Sdelphij#endif
141296465Sdelphij    return (ret);
142160814Ssimon}
143160814Ssimon
144160814Ssimonstatic void *ecdsa_data_new(void)
145160814Ssimon{
146296465Sdelphij    return (void *)ECDSA_DATA_new_method(NULL);
147160814Ssimon}
148160814Ssimon
149160814Ssimonstatic void *ecdsa_data_dup(void *data)
150160814Ssimon{
151296465Sdelphij    ECDSA_DATA *r = (ECDSA_DATA *)data;
152160814Ssimon
153296465Sdelphij    /* XXX: dummy operation */
154296465Sdelphij    if (r == NULL)
155296465Sdelphij        return NULL;
156160814Ssimon
157296465Sdelphij    return ecdsa_data_new();
158160814Ssimon}
159160814Ssimon
160160814Ssimonstatic void ecdsa_data_free(void *data)
161160814Ssimon{
162296465Sdelphij    ECDSA_DATA *r = (ECDSA_DATA *)data;
163160814Ssimon
164160814Ssimon#ifndef OPENSSL_NO_ENGINE
165296465Sdelphij    if (r->engine)
166296465Sdelphij        ENGINE_finish(r->engine);
167160814Ssimon#endif
168296465Sdelphij    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDSA, r, &r->ex_data);
169160814Ssimon
170296465Sdelphij    OPENSSL_cleanse((void *)r, sizeof(ECDSA_DATA));
171160814Ssimon
172296465Sdelphij    OPENSSL_free(r);
173160814Ssimon}
174160814Ssimon
175160814SsimonECDSA_DATA *ecdsa_check(EC_KEY *key)
176160814Ssimon{
177296465Sdelphij    ECDSA_DATA *ecdsa_data;
178160814Ssimon
179296465Sdelphij    void *data = EC_KEY_get_key_method_data(key, ecdsa_data_dup,
180296465Sdelphij                                            ecdsa_data_free, ecdsa_data_free);
181296465Sdelphij    if (data == NULL) {
182296465Sdelphij        ecdsa_data = (ECDSA_DATA *)ecdsa_data_new();
183296465Sdelphij        if (ecdsa_data == NULL)
184296465Sdelphij            return NULL;
185296465Sdelphij        data = EC_KEY_insert_key_method_data(key, (void *)ecdsa_data,
186296465Sdelphij                                             ecdsa_data_dup, ecdsa_data_free,
187296465Sdelphij                                             ecdsa_data_free);
188296465Sdelphij        if (data != NULL) {
189296465Sdelphij            /*
190296465Sdelphij             * Another thread raced us to install the key_method data and
191296465Sdelphij             * won.
192296465Sdelphij             */
193296465Sdelphij            ecdsa_data_free(ecdsa_data);
194296465Sdelphij            ecdsa_data = (ECDSA_DATA *)data;
195296465Sdelphij        }
196296465Sdelphij    } else
197296465Sdelphij        ecdsa_data = (ECDSA_DATA *)data;
198296465Sdelphij
199296465Sdelphij    return ecdsa_data;
200160814Ssimon}
201160814Ssimon
202160814Ssimonint ECDSA_size(const EC_KEY *r)
203160814Ssimon{
204296465Sdelphij    int ret, i;
205296465Sdelphij    ASN1_INTEGER bs;
206296465Sdelphij    BIGNUM *order = NULL;
207296465Sdelphij    unsigned char buf[4];
208296465Sdelphij    const EC_GROUP *group;
209160814Ssimon
210296465Sdelphij    if (r == NULL)
211296465Sdelphij        return 0;
212296465Sdelphij    group = EC_KEY_get0_group(r);
213296465Sdelphij    if (group == NULL)
214296465Sdelphij        return 0;
215160814Ssimon
216296465Sdelphij    if ((order = BN_new()) == NULL)
217296465Sdelphij        return 0;
218296465Sdelphij    if (!EC_GROUP_get_order(group, order, NULL)) {
219296465Sdelphij        BN_clear_free(order);
220296465Sdelphij        return 0;
221296465Sdelphij    }
222296465Sdelphij    i = BN_num_bits(order);
223296465Sdelphij    bs.length = (i + 7) / 8;
224296465Sdelphij    bs.data = buf;
225296465Sdelphij    bs.type = V_ASN1_INTEGER;
226296465Sdelphij    /* If the top bit is set the asn1 encoding is 1 larger. */
227296465Sdelphij    buf[0] = 0xff;
228160814Ssimon
229296465Sdelphij    i = i2d_ASN1_INTEGER(&bs, NULL);
230296465Sdelphij    i += i;                     /* r and s */
231296465Sdelphij    ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
232296465Sdelphij    BN_clear_free(order);
233296465Sdelphij    return (ret);
234160814Ssimon}
235160814Ssimon
236160814Ssimonint ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
237296465Sdelphij                           CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
238160814Ssimon{
239296465Sdelphij    return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDSA, argl, argp,
240296465Sdelphij                                   new_func, dup_func, free_func);
241160814Ssimon}
242160814Ssimon
243160814Ssimonint ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg)
244160814Ssimon{
245296465Sdelphij    ECDSA_DATA *ecdsa;
246296465Sdelphij    ecdsa = ecdsa_check(d);
247296465Sdelphij    if (ecdsa == NULL)
248296465Sdelphij        return 0;
249296465Sdelphij    return (CRYPTO_set_ex_data(&ecdsa->ex_data, idx, arg));
250160814Ssimon}
251160814Ssimon
252160814Ssimonvoid *ECDSA_get_ex_data(EC_KEY *d, int idx)
253160814Ssimon{
254296465Sdelphij    ECDSA_DATA *ecdsa;
255296465Sdelphij    ecdsa = ecdsa_check(d);
256296465Sdelphij    if (ecdsa == NULL)
257296465Sdelphij        return NULL;
258296465Sdelphij    return (CRYPTO_get_ex_data(&ecdsa->ex_data, idx));
259160814Ssimon}
260