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