1ebfedea0SLionel Sambuc /* crypto/ecdh/ech_lib.c */
2ebfedea0SLionel Sambuc /* ====================================================================
3ebfedea0SLionel Sambuc * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6ebfedea0SLionel Sambuc * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7ebfedea0SLionel Sambuc * to the OpenSSL project.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * The ECC Code is licensed pursuant to the OpenSSL open source
10ebfedea0SLionel Sambuc * license provided below.
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * The ECDH software is originally written by Douglas Stebila of
13ebfedea0SLionel Sambuc * Sun Microsystems Laboratories.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc */
16ebfedea0SLionel Sambuc /* ====================================================================
17ebfedea0SLionel Sambuc * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
20ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
21ebfedea0SLionel Sambuc * are met:
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
24ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
25ebfedea0SLionel Sambuc *
26ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
28ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
29ebfedea0SLionel Sambuc * distribution.
30ebfedea0SLionel Sambuc *
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
32ebfedea0SLionel Sambuc * software must display the following 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 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
38ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
39ebfedea0SLionel Sambuc * openssl-core@OpenSSL.org.
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
42ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
43ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
44ebfedea0SLionel Sambuc *
45ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
46ebfedea0SLionel Sambuc * acknowledgment:
47ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
48ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
49ebfedea0SLionel Sambuc *
50ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
54ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
62ebfedea0SLionel Sambuc * ====================================================================
63ebfedea0SLionel Sambuc *
64ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
65ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
66ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
67ebfedea0SLionel Sambuc *
68ebfedea0SLionel Sambuc */
69ebfedea0SLionel Sambuc
70ebfedea0SLionel Sambuc #include "ech_locl.h"
71ebfedea0SLionel Sambuc #include <string.h>
72ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
73ebfedea0SLionel Sambuc # include <openssl/engine.h>
74ebfedea0SLionel Sambuc #endif
75ebfedea0SLionel Sambuc #include <openssl/err.h>
76ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
77ebfedea0SLionel Sambuc # include <openssl/fips.h>
78ebfedea0SLionel Sambuc #endif
79ebfedea0SLionel Sambuc
80ebfedea0SLionel Sambuc const char ECDH_version[] = "ECDH" OPENSSL_VERSION_PTEXT;
81ebfedea0SLionel Sambuc
82ebfedea0SLionel Sambuc static const ECDH_METHOD *default_ECDH_method = NULL;
83ebfedea0SLionel Sambuc
84ebfedea0SLionel Sambuc static void *ecdh_data_new(void);
85ebfedea0SLionel Sambuc static void *ecdh_data_dup(void *);
86ebfedea0SLionel Sambuc static void ecdh_data_free(void *);
87ebfedea0SLionel Sambuc
ECDH_set_default_method(const ECDH_METHOD * meth)88ebfedea0SLionel Sambuc void ECDH_set_default_method(const ECDH_METHOD *meth)
89ebfedea0SLionel Sambuc {
90ebfedea0SLionel Sambuc default_ECDH_method = meth;
91ebfedea0SLionel Sambuc }
92ebfedea0SLionel Sambuc
ECDH_get_default_method(void)93ebfedea0SLionel Sambuc const ECDH_METHOD *ECDH_get_default_method(void)
94ebfedea0SLionel Sambuc {
95*0a6a1f1dSLionel Sambuc if (!default_ECDH_method) {
96ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
97ebfedea0SLionel Sambuc if (FIPS_mode())
98ebfedea0SLionel Sambuc return FIPS_ecdh_openssl();
99ebfedea0SLionel Sambuc else
100ebfedea0SLionel Sambuc return ECDH_OpenSSL();
101ebfedea0SLionel Sambuc #else
102ebfedea0SLionel Sambuc default_ECDH_method = ECDH_OpenSSL();
103ebfedea0SLionel Sambuc #endif
104ebfedea0SLionel Sambuc }
105ebfedea0SLionel Sambuc return default_ECDH_method;
106ebfedea0SLionel Sambuc }
107ebfedea0SLionel Sambuc
ECDH_set_method(EC_KEY * eckey,const ECDH_METHOD * meth)108ebfedea0SLionel Sambuc int ECDH_set_method(EC_KEY *eckey, const ECDH_METHOD *meth)
109ebfedea0SLionel Sambuc {
110ebfedea0SLionel Sambuc ECDH_DATA *ecdh;
111ebfedea0SLionel Sambuc
112ebfedea0SLionel Sambuc ecdh = ecdh_check(eckey);
113ebfedea0SLionel Sambuc
114ebfedea0SLionel Sambuc if (ecdh == NULL)
115ebfedea0SLionel Sambuc return 0;
116ebfedea0SLionel Sambuc
117ebfedea0SLionel Sambuc #if 0
118ebfedea0SLionel Sambuc mtmp = ecdh->meth;
119ebfedea0SLionel Sambuc if (mtmp->finish)
120ebfedea0SLionel Sambuc mtmp->finish(eckey);
121ebfedea0SLionel Sambuc #endif
122ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
123*0a6a1f1dSLionel Sambuc if (ecdh->engine) {
124ebfedea0SLionel Sambuc ENGINE_finish(ecdh->engine);
125ebfedea0SLionel Sambuc ecdh->engine = NULL;
126ebfedea0SLionel Sambuc }
127ebfedea0SLionel Sambuc #endif
128ebfedea0SLionel Sambuc ecdh->meth = meth;
129ebfedea0SLionel Sambuc #if 0
130ebfedea0SLionel Sambuc if (meth->init)
131ebfedea0SLionel Sambuc meth->init(eckey);
132ebfedea0SLionel Sambuc #endif
133ebfedea0SLionel Sambuc return 1;
134ebfedea0SLionel Sambuc }
135ebfedea0SLionel Sambuc
ECDH_DATA_new_method(ENGINE * engine)136ebfedea0SLionel Sambuc static ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine)
137ebfedea0SLionel Sambuc {
138ebfedea0SLionel Sambuc ECDH_DATA *ret;
139ebfedea0SLionel Sambuc
140ebfedea0SLionel Sambuc ret = (ECDH_DATA *)OPENSSL_malloc(sizeof(ECDH_DATA));
141*0a6a1f1dSLionel Sambuc if (ret == NULL) {
142ebfedea0SLionel Sambuc ECDHerr(ECDH_F_ECDH_DATA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
143ebfedea0SLionel Sambuc return (NULL);
144ebfedea0SLionel Sambuc }
145ebfedea0SLionel Sambuc
146ebfedea0SLionel Sambuc ret->init = NULL;
147ebfedea0SLionel Sambuc
148ebfedea0SLionel Sambuc ret->meth = ECDH_get_default_method();
149ebfedea0SLionel Sambuc ret->engine = engine;
150ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
151ebfedea0SLionel Sambuc if (!ret->engine)
152ebfedea0SLionel Sambuc ret->engine = ENGINE_get_default_ECDH();
153*0a6a1f1dSLionel Sambuc if (ret->engine) {
154ebfedea0SLionel Sambuc ret->meth = ENGINE_get_ECDH(ret->engine);
155*0a6a1f1dSLionel Sambuc if (!ret->meth) {
156ebfedea0SLionel Sambuc ECDHerr(ECDH_F_ECDH_DATA_NEW_METHOD, ERR_R_ENGINE_LIB);
157ebfedea0SLionel Sambuc ENGINE_finish(ret->engine);
158ebfedea0SLionel Sambuc OPENSSL_free(ret);
159ebfedea0SLionel Sambuc return NULL;
160ebfedea0SLionel Sambuc }
161ebfedea0SLionel Sambuc }
162ebfedea0SLionel Sambuc #endif
163ebfedea0SLionel Sambuc
164ebfedea0SLionel Sambuc ret->flags = ret->meth->flags;
165ebfedea0SLionel Sambuc CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDH, ret, &ret->ex_data);
166ebfedea0SLionel Sambuc #if 0
167*0a6a1f1dSLionel Sambuc if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
168ebfedea0SLionel Sambuc CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDH, ret, &ret->ex_data);
169ebfedea0SLionel Sambuc OPENSSL_free(ret);
170ebfedea0SLionel Sambuc ret = NULL;
171ebfedea0SLionel Sambuc }
172ebfedea0SLionel Sambuc #endif
173ebfedea0SLionel Sambuc return (ret);
174ebfedea0SLionel Sambuc }
175ebfedea0SLionel Sambuc
ecdh_data_new(void)176ebfedea0SLionel Sambuc static void *ecdh_data_new(void)
177ebfedea0SLionel Sambuc {
178ebfedea0SLionel Sambuc return (void *)ECDH_DATA_new_method(NULL);
179ebfedea0SLionel Sambuc }
180ebfedea0SLionel Sambuc
ecdh_data_dup(void * data)181ebfedea0SLionel Sambuc static void *ecdh_data_dup(void *data)
182ebfedea0SLionel Sambuc {
183ebfedea0SLionel Sambuc ECDH_DATA *r = (ECDH_DATA *)data;
184ebfedea0SLionel Sambuc
185ebfedea0SLionel Sambuc /* XXX: dummy operation */
186ebfedea0SLionel Sambuc if (r == NULL)
187ebfedea0SLionel Sambuc return NULL;
188ebfedea0SLionel Sambuc
189ebfedea0SLionel Sambuc return (void *)ecdh_data_new();
190ebfedea0SLionel Sambuc }
191ebfedea0SLionel Sambuc
ecdh_data_free(void * data)192ebfedea0SLionel Sambuc void ecdh_data_free(void *data)
193ebfedea0SLionel Sambuc {
194ebfedea0SLionel Sambuc ECDH_DATA *r = (ECDH_DATA *)data;
195ebfedea0SLionel Sambuc
196ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
197ebfedea0SLionel Sambuc if (r->engine)
198ebfedea0SLionel Sambuc ENGINE_finish(r->engine);
199ebfedea0SLionel Sambuc #endif
200ebfedea0SLionel Sambuc
201ebfedea0SLionel Sambuc CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDH, r, &r->ex_data);
202ebfedea0SLionel Sambuc
203ebfedea0SLionel Sambuc OPENSSL_cleanse((void *)r, sizeof(ECDH_DATA));
204ebfedea0SLionel Sambuc
205ebfedea0SLionel Sambuc OPENSSL_free(r);
206ebfedea0SLionel Sambuc }
207ebfedea0SLionel Sambuc
ecdh_check(EC_KEY * key)208ebfedea0SLionel Sambuc ECDH_DATA *ecdh_check(EC_KEY *key)
209ebfedea0SLionel Sambuc {
210ebfedea0SLionel Sambuc ECDH_DATA *ecdh_data;
211ebfedea0SLionel Sambuc
212ebfedea0SLionel Sambuc void *data = EC_KEY_get_key_method_data(key, ecdh_data_dup,
213ebfedea0SLionel Sambuc ecdh_data_free, ecdh_data_free);
214*0a6a1f1dSLionel Sambuc if (data == NULL) {
215ebfedea0SLionel Sambuc ecdh_data = (ECDH_DATA *)ecdh_data_new();
216ebfedea0SLionel Sambuc if (ecdh_data == NULL)
217ebfedea0SLionel Sambuc return NULL;
218ebfedea0SLionel Sambuc data = EC_KEY_insert_key_method_data(key, (void *)ecdh_data,
219*0a6a1f1dSLionel Sambuc ecdh_data_dup, ecdh_data_free,
220*0a6a1f1dSLionel Sambuc ecdh_data_free);
221*0a6a1f1dSLionel Sambuc if (data != NULL) {
222*0a6a1f1dSLionel Sambuc /*
223*0a6a1f1dSLionel Sambuc * Another thread raced us to install the key_method data and
224*0a6a1f1dSLionel Sambuc * won.
225*0a6a1f1dSLionel Sambuc */
226ebfedea0SLionel Sambuc ecdh_data_free(ecdh_data);
227ebfedea0SLionel Sambuc ecdh_data = (ECDH_DATA *)data;
228ebfedea0SLionel Sambuc }
229*0a6a1f1dSLionel Sambuc } else
230ebfedea0SLionel Sambuc ecdh_data = (ECDH_DATA *)data;
231ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
232ebfedea0SLionel Sambuc if (FIPS_mode() && !(ecdh_data->flags & ECDH_FLAG_FIPS_METHOD)
233*0a6a1f1dSLionel Sambuc && !(EC_KEY_get_flags(key) & EC_FLAG_NON_FIPS_ALLOW)) {
234ebfedea0SLionel Sambuc ECDHerr(ECDH_F_ECDH_CHECK, ECDH_R_NON_FIPS_METHOD);
235ebfedea0SLionel Sambuc return NULL;
236ebfedea0SLionel Sambuc }
237ebfedea0SLionel Sambuc #endif
238ebfedea0SLionel Sambuc
239ebfedea0SLionel Sambuc return ecdh_data;
240ebfedea0SLionel Sambuc }
241ebfedea0SLionel Sambuc
ECDH_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)242ebfedea0SLionel Sambuc int ECDH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
243ebfedea0SLionel Sambuc CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
244ebfedea0SLionel Sambuc {
245ebfedea0SLionel Sambuc return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDH, argl, argp,
246ebfedea0SLionel Sambuc new_func, dup_func, free_func);
247ebfedea0SLionel Sambuc }
248ebfedea0SLionel Sambuc
ECDH_set_ex_data(EC_KEY * d,int idx,void * arg)249ebfedea0SLionel Sambuc int ECDH_set_ex_data(EC_KEY *d, int idx, void *arg)
250ebfedea0SLionel Sambuc {
251ebfedea0SLionel Sambuc ECDH_DATA *ecdh;
252ebfedea0SLionel Sambuc ecdh = ecdh_check(d);
253ebfedea0SLionel Sambuc if (ecdh == NULL)
254ebfedea0SLionel Sambuc return 0;
255ebfedea0SLionel Sambuc return (CRYPTO_set_ex_data(&ecdh->ex_data, idx, arg));
256ebfedea0SLionel Sambuc }
257ebfedea0SLionel Sambuc
ECDH_get_ex_data(EC_KEY * d,int idx)258ebfedea0SLionel Sambuc void *ECDH_get_ex_data(EC_KEY *d, int idx)
259ebfedea0SLionel Sambuc {
260ebfedea0SLionel Sambuc ECDH_DATA *ecdh;
261ebfedea0SLionel Sambuc ecdh = ecdh_check(d);
262ebfedea0SLionel Sambuc if (ecdh == NULL)
263ebfedea0SLionel Sambuc return NULL;
264ebfedea0SLionel Sambuc return (CRYPTO_get_ex_data(&ecdh->ex_data, idx));
265ebfedea0SLionel Sambuc }
266