1160814Ssimon/* crypto/ecdh/ech_lib.c */
2160814Ssimon/* ====================================================================
3160814Ssimon * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4160814Ssimon *
5160814Ssimon * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6160814Ssimon * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7160814Ssimon * to the OpenSSL project.
8160814Ssimon *
9160814Ssimon * The ECC Code is licensed pursuant to the OpenSSL open source
10160814Ssimon * license provided below.
11160814Ssimon *
12160814Ssimon * The ECDH software is originally written by Douglas Stebila of
13160814Ssimon * Sun Microsystems Laboratories.
14160814Ssimon *
15160814Ssimon */
16160814Ssimon/* ====================================================================
17160814Ssimon * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
18160814Ssimon *
19160814Ssimon * Redistribution and use in source and binary forms, with or without
20160814Ssimon * modification, are permitted provided that the following conditions
21160814Ssimon * are met:
22160814Ssimon *
23160814Ssimon * 1. Redistributions of source code must retain the above copyright
24280304Sjkim *    notice, this list of conditions and the following disclaimer.
25160814Ssimon *
26160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
27160814Ssimon *    notice, this list of conditions and the following disclaimer in
28160814Ssimon *    the documentation and/or other materials provided with the
29160814Ssimon *    distribution.
30160814Ssimon *
31160814Ssimon * 3. All advertising materials mentioning features or use of this
32160814Ssimon *    software must display the following acknowledgment:
33160814Ssimon *    "This product includes software developed by the OpenSSL Project
34160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
35160814Ssimon *
36160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37160814Ssimon *    endorse or promote products derived from this software without
38160814Ssimon *    prior written permission. For written permission, please contact
39160814Ssimon *    openssl-core@OpenSSL.org.
40160814Ssimon *
41160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
42160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
43160814Ssimon *    permission of the OpenSSL Project.
44160814Ssimon *
45160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
46160814Ssimon *    acknowledgment:
47160814Ssimon *    "This product includes software developed by the OpenSSL Project
48160814Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
49160814Ssimon *
50160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
54160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
62160814Ssimon * ====================================================================
63160814Ssimon *
64160814Ssimon * This product includes cryptographic software written by Eric Young
65160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
66160814Ssimon * Hudson (tjh@cryptsoft.com).
67160814Ssimon *
68160814Ssimon */
69160814Ssimon
70160814Ssimon#include "ech_locl.h"
71160814Ssimon#include <string.h>
72160814Ssimon#ifndef OPENSSL_NO_ENGINE
73280304Sjkim# include <openssl/engine.h>
74160814Ssimon#endif
75160814Ssimon#include <openssl/err.h>
76238405Sjkim#ifdef OPENSSL_FIPS
77280304Sjkim# include <openssl/fips.h>
78238405Sjkim#endif
79160814Ssimon
80280304Sjkimconst char ECDH_version[] = "ECDH" OPENSSL_VERSION_PTEXT;
81160814Ssimon
82160814Ssimonstatic const ECDH_METHOD *default_ECDH_method = NULL;
83160814Ssimon
84160814Ssimonstatic void *ecdh_data_new(void);
85160814Ssimonstatic void *ecdh_data_dup(void *);
86280304Sjkimstatic void ecdh_data_free(void *);
87160814Ssimon
88160814Ssimonvoid ECDH_set_default_method(const ECDH_METHOD *meth)
89280304Sjkim{
90280304Sjkim    default_ECDH_method = meth;
91280304Sjkim}
92160814Ssimon
93160814Ssimonconst ECDH_METHOD *ECDH_get_default_method(void)
94280304Sjkim{
95280304Sjkim    if (!default_ECDH_method) {
96238405Sjkim#ifdef OPENSSL_FIPS
97280304Sjkim        if (FIPS_mode())
98280304Sjkim            return FIPS_ecdh_openssl();
99280304Sjkim        else
100280304Sjkim            return ECDH_OpenSSL();
101238405Sjkim#else
102280304Sjkim        default_ECDH_method = ECDH_OpenSSL();
103238405Sjkim#endif
104280304Sjkim    }
105280304Sjkim    return default_ECDH_method;
106280304Sjkim}
107160814Ssimon
108160814Ssimonint ECDH_set_method(EC_KEY *eckey, const ECDH_METHOD *meth)
109280304Sjkim{
110280304Sjkim    ECDH_DATA *ecdh;
111160814Ssimon
112280304Sjkim    ecdh = ecdh_check(eckey);
113160814Ssimon
114280304Sjkim    if (ecdh == NULL)
115280304Sjkim        return 0;
116160814Ssimon
117238405Sjkim#if 0
118280304Sjkim    mtmp = ecdh->meth;
119280304Sjkim    if (mtmp->finish)
120280304Sjkim        mtmp->finish(eckey);
121238405Sjkim#endif
122160814Ssimon#ifndef OPENSSL_NO_ENGINE
123280304Sjkim    if (ecdh->engine) {
124280304Sjkim        ENGINE_finish(ecdh->engine);
125280304Sjkim        ecdh->engine = NULL;
126280304Sjkim    }
127160814Ssimon#endif
128280304Sjkim    ecdh->meth = meth;
129160814Ssimon#if 0
130280304Sjkim    if (meth->init)
131280304Sjkim        meth->init(eckey);
132160814Ssimon#endif
133280304Sjkim    return 1;
134280304Sjkim}
135160814Ssimon
136160814Ssimonstatic ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine)
137280304Sjkim{
138280304Sjkim    ECDH_DATA *ret;
139160814Ssimon
140280304Sjkim    ret = (ECDH_DATA *)OPENSSL_malloc(sizeof(ECDH_DATA));
141280304Sjkim    if (ret == NULL) {
142280304Sjkim        ECDHerr(ECDH_F_ECDH_DATA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
143280304Sjkim        return (NULL);
144280304Sjkim    }
145160814Ssimon
146280304Sjkim    ret->init = NULL;
147160814Ssimon
148280304Sjkim    ret->meth = ECDH_get_default_method();
149280304Sjkim    ret->engine = engine;
150160814Ssimon#ifndef OPENSSL_NO_ENGINE
151280304Sjkim    if (!ret->engine)
152280304Sjkim        ret->engine = ENGINE_get_default_ECDH();
153280304Sjkim    if (ret->engine) {
154280304Sjkim        ret->meth = ENGINE_get_ECDH(ret->engine);
155280304Sjkim        if (!ret->meth) {
156280304Sjkim            ECDHerr(ECDH_F_ECDH_DATA_NEW_METHOD, ERR_R_ENGINE_LIB);
157280304Sjkim            ENGINE_finish(ret->engine);
158280304Sjkim            OPENSSL_free(ret);
159280304Sjkim            return NULL;
160280304Sjkim        }
161280304Sjkim    }
162160814Ssimon#endif
163160814Ssimon
164280304Sjkim    ret->flags = ret->meth->flags;
165280304Sjkim    CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDH, ret, &ret->ex_data);
166160814Ssimon#if 0
167280304Sjkim    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
168280304Sjkim        CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDH, ret, &ret->ex_data);
169280304Sjkim        OPENSSL_free(ret);
170280304Sjkim        ret = NULL;
171280304Sjkim    }
172280304Sjkim#endif
173280304Sjkim    return (ret);
174280304Sjkim}
175160814Ssimon
176160814Ssimonstatic void *ecdh_data_new(void)
177280304Sjkim{
178280304Sjkim    return (void *)ECDH_DATA_new_method(NULL);
179280304Sjkim}
180160814Ssimon
181160814Ssimonstatic void *ecdh_data_dup(void *data)
182160814Ssimon{
183280304Sjkim    ECDH_DATA *r = (ECDH_DATA *)data;
184160814Ssimon
185280304Sjkim    /* XXX: dummy operation */
186280304Sjkim    if (r == NULL)
187280304Sjkim        return NULL;
188160814Ssimon
189280304Sjkim    return (void *)ecdh_data_new();
190160814Ssimon}
191160814Ssimon
192160814Ssimonvoid ecdh_data_free(void *data)
193280304Sjkim{
194280304Sjkim    ECDH_DATA *r = (ECDH_DATA *)data;
195160814Ssimon
196160814Ssimon#ifndef OPENSSL_NO_ENGINE
197280304Sjkim    if (r->engine)
198280304Sjkim        ENGINE_finish(r->engine);
199160814Ssimon#endif
200160814Ssimon
201280304Sjkim    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDH, r, &r->ex_data);
202160814Ssimon
203280304Sjkim    OPENSSL_cleanse((void *)r, sizeof(ECDH_DATA));
204160814Ssimon
205280304Sjkim    OPENSSL_free(r);
206280304Sjkim}
207160814Ssimon
208160814SsimonECDH_DATA *ecdh_check(EC_KEY *key)
209280304Sjkim{
210280304Sjkim    ECDH_DATA *ecdh_data;
211280304Sjkim
212280304Sjkim    void *data = EC_KEY_get_key_method_data(key, ecdh_data_dup,
213280304Sjkim                                            ecdh_data_free, ecdh_data_free);
214280304Sjkim    if (data == NULL) {
215280304Sjkim        ecdh_data = (ECDH_DATA *)ecdh_data_new();
216280304Sjkim        if (ecdh_data == NULL)
217280304Sjkim            return NULL;
218280304Sjkim        data = EC_KEY_insert_key_method_data(key, (void *)ecdh_data,
219280304Sjkim                                             ecdh_data_dup, ecdh_data_free,
220280304Sjkim                                             ecdh_data_free);
221280304Sjkim        if (data != NULL) {
222280304Sjkim            /*
223280304Sjkim             * Another thread raced us to install the key_method data and
224280304Sjkim             * won.
225280304Sjkim             */
226280304Sjkim            ecdh_data_free(ecdh_data);
227280304Sjkim            ecdh_data = (ECDH_DATA *)data;
228280304Sjkim        }
229280304Sjkim    } else
230280304Sjkim        ecdh_data = (ECDH_DATA *)data;
231238405Sjkim#ifdef OPENSSL_FIPS
232280304Sjkim    if (FIPS_mode() && !(ecdh_data->flags & ECDH_FLAG_FIPS_METHOD)
233280304Sjkim        && !(EC_KEY_get_flags(key) & EC_FLAG_NON_FIPS_ALLOW)) {
234280304Sjkim        ECDHerr(ECDH_F_ECDH_CHECK, ECDH_R_NON_FIPS_METHOD);
235280304Sjkim        return NULL;
236280304Sjkim    }
237238405Sjkim#endif
238160814Ssimon
239280304Sjkim    return ecdh_data;
240280304Sjkim}
241160814Ssimon
242160814Ssimonint ECDH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
243280304Sjkim                          CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
244280304Sjkim{
245280304Sjkim    return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDH, argl, argp,
246280304Sjkim                                   new_func, dup_func, free_func);
247280304Sjkim}
248160814Ssimon
249160814Ssimonint ECDH_set_ex_data(EC_KEY *d, int idx, void *arg)
250280304Sjkim{
251280304Sjkim    ECDH_DATA *ecdh;
252280304Sjkim    ecdh = ecdh_check(d);
253280304Sjkim    if (ecdh == NULL)
254280304Sjkim        return 0;
255280304Sjkim    return (CRYPTO_set_ex_data(&ecdh->ex_data, idx, arg));
256280304Sjkim}
257160814Ssimon
258160814Ssimonvoid *ECDH_get_ex_data(EC_KEY *d, int idx)
259280304Sjkim{
260280304Sjkim    ECDH_DATA *ecdh;
261280304Sjkim    ecdh = ecdh_check(d);
262280304Sjkim    if (ecdh == NULL)
263280304Sjkim        return NULL;
264280304Sjkim    return (CRYPTO_get_ex_data(&ecdh->ex_data, idx));
265280304Sjkim}
266