use a generic EC_KEY structure (EC keys are not ECDSA specific)
[openssl.git] / crypto / ecdsa / ecs_lib.c
1 /* crypto/ecdsa/ecs_lib.c */
2 /* ====================================================================
3  * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
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
10  *    notice, this list of conditions and the following disclaimer. 
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  */
55
56 #include "ecdsa.h"
57 #include <openssl/engine.h>
58
59 const char *ECDSA_version="ECDSA" OPENSSL_VERSION_PTEXT;
60
61 static void ecdsa_finish(EC_KEY *);
62
63 static const ECDSA_METHOD *default_ECDSA_method = NULL;
64
65 void ECDSA_set_default_method(const ECDSA_METHOD *meth)
66 {
67         default_ECDSA_method = meth;
68 }
69
70 const ECDSA_METHOD *ECDSA_get_default_method(void)
71 {
72         if(!default_ECDSA_method) 
73                 default_ECDSA_method = ECDSA_OpenSSL();
74         return default_ECDSA_method;
75 }
76
77 int ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth)
78 {
79         const ECDSA_METHOD *mtmp;
80         ECDSA_DATA *ecdsa;
81
82         ecdsa = ecdsa_check(eckey);
83
84         if (ecdsa == NULL)
85                 return 0;
86
87         mtmp = ecdsa->meth;
88 #if 0
89         if (mtmp->finish)
90                 mtmp->finish(eckey);
91 #endif
92         if (ecdsa->engine)
93         {
94                 ENGINE_finish(ecdsa->engine);
95                 ecdsa->engine = NULL;
96         }
97         ecdsa->meth = meth;
98 #if 0
99         if (meth->init) 
100                 meth->init(eckey);
101 #endif
102         return 1;
103 }
104
105 ECDSA_DATA *ECDSA_DATA_new(void)
106 {
107         return ECDSA_DATA_new_method(NULL);
108 }
109
110 ECDSA_DATA *ECDSA_DATA_new_method(ENGINE *engine)
111 {
112         ECDSA_DATA *ret;
113
114         ret=(ECDSA_DATA *)OPENSSL_malloc(sizeof(ECDSA_DATA));
115         if (ret == NULL)
116         {
117                 ECDSAerr(ECDSA_F_ECDSA_DATA_NEW, ERR_R_MALLOC_FAILURE);
118                 return(NULL);
119         }
120
121         ret->init = NULL;
122         ret->finish = ecdsa_finish;
123
124         ret->kinv = NULL;
125         ret->r    = NULL;
126
127         ret->meth = ECDSA_get_default_method();
128         ret->engine = engine;
129         if (!ret->engine)
130                 ret->engine = ENGINE_get_default_ECDSA();
131         if (ret->engine)
132         {
133                 ret->meth = ENGINE_get_ECDSA(ret->engine);
134                 if (!ret->meth)
135                 {
136                         ECDSAerr(ECDSA_F_ECDSA_DATA_NEW, ERR_R_ENGINE_LIB);
137                         ENGINE_finish(ret->engine);
138                         OPENSSL_free(ret);
139                         return NULL;
140                 }
141         }
142
143         ret->flags = ret->meth->flags;
144         CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDSA, ret, &ret->ex_data);
145 #if 0
146         if ((ret->meth->init != NULL) && !ret->meth->init(ret))
147         {
148                 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDSA, ret, &ret->ex_data);
149                 OPENSSL_free(ret);
150                 ret=NULL;
151         }
152 #endif  
153         return(ret);
154 }
155
156 void ECDSA_DATA_free(ECDSA_DATA *r)
157 {
158         if (r->kinv)
159                 BN_clear_free(r->kinv);
160         if (r->r)
161                 BN_clear_free(r->r);
162
163 #if 0
164         if (r->meth->finish)
165                 r->meth->finish(r);
166 #endif
167         if (r->engine)
168                 ENGINE_finish(r->engine);
169
170         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDSA, r, &r->ex_data);
171
172         memset((void *)r, 0x0, sizeof(ECDSA_DATA));
173
174         OPENSSL_free(r);
175 }
176
177 ECDSA_DATA *ecdsa_check(EC_KEY *key)
178 {
179         if (key->meth_data)
180         {
181                 if (key->meth_data->finish != ecdsa_finish)
182                 {
183                         key->meth_data->finish(key);
184                         key->meth_data = (EC_KEY_METH_DATA *)ECDSA_DATA_new();
185                 }
186         }
187         else
188                 key->meth_data = (EC_KEY_METH_DATA *)ECDSA_DATA_new();
189         return (ECDSA_DATA *)key->meth_data;
190 }
191
192 static void ecdsa_finish(EC_KEY *key)
193 {
194         if (key->meth_data && key->meth_data->finish == ecdsa_finish)
195                 ECDSA_DATA_free((ECDSA_DATA *)key->meth_data);
196 }
197
198 int ECDSA_size(const EC_KEY *r)
199 {
200         int ret,i;
201         ASN1_INTEGER bs;
202         BIGNUM  *order=NULL;
203         unsigned char buf[4];
204
205         if (r == NULL || r->group == NULL)
206                 return 0;
207         if ((order = BN_new()) == NULL) return 0;
208         if (!EC_GROUP_get_order(r->group,order,NULL))
209         {
210                 BN_clear_free(order);
211                 return 0;
212         } 
213         i=BN_num_bits(order);
214         bs.length=(i+7)/8;
215         bs.data=buf;
216         bs.type=V_ASN1_INTEGER;
217         /* If the top bit is set the asn1 encoding is 1 larger. */
218         buf[0]=0xff;    
219
220         i=i2d_ASN1_INTEGER(&bs,NULL);
221         i+=i; /* r and s */
222         ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
223         BN_clear_free(order);
224         return(ret);
225 }
226
227
228 int ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
229              CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
230 {
231         return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDSA, argl, argp,
232                                 new_func, dup_func, free_func);
233 }
234
235 int ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg)
236 {
237         ECDSA_DATA *ecdsa;
238         ecdsa = ecdsa_check(d);
239         if (ecdsa == NULL)
240                 return 0;
241         return(CRYPTO_set_ex_data(&ecdsa->ex_data,idx,arg));
242 }
243
244 void *ECDSA_get_ex_data(EC_KEY *d, int idx)
245 {
246         ECDSA_DATA *ecdsa;
247         ecdsa = ecdsa_check(d);
248         if (ecdsa == NULL)
249                 return NULL;
250         return(CRYPTO_get_ex_data(&ecdsa->ex_data,idx));
251 }