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 24280297Sjkim * 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 73280297Sjkim# include <openssl/engine.h> 74160814Ssimon#endif 75160814Ssimon#include <openssl/err.h> 76238405Sjkim#ifdef OPENSSL_FIPS 77280297Sjkim# include <openssl/fips.h> 78238405Sjkim#endif 79160814Ssimon 80280297Sjkimconst 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 *); 86280297Sjkimstatic void ecdh_data_free(void *); 87160814Ssimon 88160814Ssimonvoid ECDH_set_default_method(const ECDH_METHOD *meth) 89280297Sjkim{ 90280297Sjkim default_ECDH_method = meth; 91280297Sjkim} 92160814Ssimon 93160814Ssimonconst ECDH_METHOD *ECDH_get_default_method(void) 94280297Sjkim{ 95280297Sjkim if (!default_ECDH_method) { 96238405Sjkim#ifdef OPENSSL_FIPS 97280297Sjkim if (FIPS_mode()) 98280297Sjkim return FIPS_ecdh_openssl(); 99280297Sjkim else 100280297Sjkim return ECDH_OpenSSL(); 101238405Sjkim#else 102280297Sjkim default_ECDH_method = ECDH_OpenSSL(); 103238405Sjkim#endif 104280297Sjkim } 105280297Sjkim return default_ECDH_method; 106280297Sjkim} 107160814Ssimon 108160814Ssimonint ECDH_set_method(EC_KEY *eckey, const ECDH_METHOD *meth) 109280297Sjkim{ 110280297Sjkim ECDH_DATA *ecdh; 111160814Ssimon 112280297Sjkim ecdh = ecdh_check(eckey); 113160814Ssimon 114280297Sjkim if (ecdh == NULL) 115280297Sjkim return 0; 116160814Ssimon 117238405Sjkim#if 0 118280297Sjkim mtmp = ecdh->meth; 119280297Sjkim if (mtmp->finish) 120280297Sjkim mtmp->finish(eckey); 121238405Sjkim#endif 122160814Ssimon#ifndef OPENSSL_NO_ENGINE 123280297Sjkim if (ecdh->engine) { 124280297Sjkim ENGINE_finish(ecdh->engine); 125280297Sjkim ecdh->engine = NULL; 126280297Sjkim } 127160814Ssimon#endif 128280297Sjkim ecdh->meth = meth; 129160814Ssimon#if 0 130280297Sjkim if (meth->init) 131280297Sjkim meth->init(eckey); 132160814Ssimon#endif 133280297Sjkim return 1; 134280297Sjkim} 135160814Ssimon 136160814Ssimonstatic ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine) 137280297Sjkim{ 138280297Sjkim ECDH_DATA *ret; 139160814Ssimon 140280297Sjkim ret = (ECDH_DATA *)OPENSSL_malloc(sizeof(ECDH_DATA)); 141280297Sjkim if (ret == NULL) { 142280297Sjkim ECDHerr(ECDH_F_ECDH_DATA_NEW_METHOD, ERR_R_MALLOC_FAILURE); 143280297Sjkim return (NULL); 144280297Sjkim } 145160814Ssimon 146280297Sjkim ret->init = NULL; 147160814Ssimon 148280297Sjkim ret->meth = ECDH_get_default_method(); 149280297Sjkim ret->engine = engine; 150160814Ssimon#ifndef OPENSSL_NO_ENGINE 151280297Sjkim if (!ret->engine) 152280297Sjkim ret->engine = ENGINE_get_default_ECDH(); 153280297Sjkim if (ret->engine) { 154280297Sjkim ret->meth = ENGINE_get_ECDH(ret->engine); 155280297Sjkim if (!ret->meth) { 156280297Sjkim ECDHerr(ECDH_F_ECDH_DATA_NEW_METHOD, ERR_R_ENGINE_LIB); 157280297Sjkim ENGINE_finish(ret->engine); 158280297Sjkim OPENSSL_free(ret); 159280297Sjkim return NULL; 160280297Sjkim } 161280297Sjkim } 162160814Ssimon#endif 163160814Ssimon 164280297Sjkim ret->flags = ret->meth->flags; 165280297Sjkim CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDH, ret, &ret->ex_data); 166160814Ssimon#if 0 167280297Sjkim if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { 168280297Sjkim CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDH, ret, &ret->ex_data); 169280297Sjkim OPENSSL_free(ret); 170280297Sjkim ret = NULL; 171280297Sjkim } 172280297Sjkim#endif 173280297Sjkim return (ret); 174280297Sjkim} 175160814Ssimon 176160814Ssimonstatic void *ecdh_data_new(void) 177280297Sjkim{ 178280297Sjkim return (void *)ECDH_DATA_new_method(NULL); 179280297Sjkim} 180160814Ssimon 181160814Ssimonstatic void *ecdh_data_dup(void *data) 182160814Ssimon{ 183280297Sjkim ECDH_DATA *r = (ECDH_DATA *)data; 184160814Ssimon 185280297Sjkim /* XXX: dummy operation */ 186280297Sjkim if (r == NULL) 187280297Sjkim return NULL; 188160814Ssimon 189280297Sjkim return (void *)ecdh_data_new(); 190160814Ssimon} 191160814Ssimon 192160814Ssimonvoid ecdh_data_free(void *data) 193280297Sjkim{ 194280297Sjkim ECDH_DATA *r = (ECDH_DATA *)data; 195160814Ssimon 196160814Ssimon#ifndef OPENSSL_NO_ENGINE 197280297Sjkim if (r->engine) 198280297Sjkim ENGINE_finish(r->engine); 199160814Ssimon#endif 200160814Ssimon 201280297Sjkim CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDH, r, &r->ex_data); 202160814Ssimon 203280297Sjkim OPENSSL_cleanse((void *)r, sizeof(ECDH_DATA)); 204160814Ssimon 205280297Sjkim OPENSSL_free(r); 206280297Sjkim} 207160814Ssimon 208160814SsimonECDH_DATA *ecdh_check(EC_KEY *key) 209280297Sjkim{ 210280297Sjkim ECDH_DATA *ecdh_data; 211280297Sjkim 212280297Sjkim void *data = EC_KEY_get_key_method_data(key, ecdh_data_dup, 213280297Sjkim ecdh_data_free, ecdh_data_free); 214280297Sjkim if (data == NULL) { 215280297Sjkim ecdh_data = (ECDH_DATA *)ecdh_data_new(); 216280297Sjkim if (ecdh_data == NULL) 217280297Sjkim return NULL; 218280297Sjkim data = EC_KEY_insert_key_method_data(key, (void *)ecdh_data, 219280297Sjkim ecdh_data_dup, ecdh_data_free, 220280297Sjkim ecdh_data_free); 221280297Sjkim if (data != NULL) { 222280297Sjkim /* 223280297Sjkim * Another thread raced us to install the key_method data and 224280297Sjkim * won. 225280297Sjkim */ 226280297Sjkim ecdh_data_free(ecdh_data); 227280297Sjkim ecdh_data = (ECDH_DATA *)data; 228325337Sjkim } else if (EC_KEY_get_key_method_data(key, ecdh_data_dup, 229325337Sjkim ecdh_data_free, 230325337Sjkim ecdh_data_free) != ecdh_data) { 231325337Sjkim /* Or an out of memory error in EC_KEY_insert_key_method_data. */ 232325337Sjkim ecdh_data_free(ecdh_data); 233325337Sjkim return NULL; 234280297Sjkim } 235325337Sjkim } else { 236280297Sjkim ecdh_data = (ECDH_DATA *)data; 237325337Sjkim } 238238405Sjkim#ifdef OPENSSL_FIPS 239280297Sjkim if (FIPS_mode() && !(ecdh_data->flags & ECDH_FLAG_FIPS_METHOD) 240280297Sjkim && !(EC_KEY_get_flags(key) & EC_FLAG_NON_FIPS_ALLOW)) { 241280297Sjkim ECDHerr(ECDH_F_ECDH_CHECK, ECDH_R_NON_FIPS_METHOD); 242280297Sjkim return NULL; 243280297Sjkim } 244238405Sjkim#endif 245160814Ssimon 246280297Sjkim return ecdh_data; 247280297Sjkim} 248160814Ssimon 249160814Ssimonint ECDH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 250280297Sjkim CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 251280297Sjkim{ 252280297Sjkim return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDH, argl, argp, 253280297Sjkim new_func, dup_func, free_func); 254280297Sjkim} 255160814Ssimon 256160814Ssimonint ECDH_set_ex_data(EC_KEY *d, int idx, void *arg) 257280297Sjkim{ 258280297Sjkim ECDH_DATA *ecdh; 259280297Sjkim ecdh = ecdh_check(d); 260280297Sjkim if (ecdh == NULL) 261280297Sjkim return 0; 262280297Sjkim return (CRYPTO_set_ex_data(&ecdh->ex_data, idx, arg)); 263280297Sjkim} 264160814Ssimon 265160814Ssimonvoid *ECDH_get_ex_data(EC_KEY *d, int idx) 266280297Sjkim{ 267280297Sjkim ECDH_DATA *ecdh; 268280297Sjkim ecdh = ecdh_check(d); 269280297Sjkim if (ecdh == NULL) 270280297Sjkim return NULL; 271280297Sjkim return (CRYPTO_get_ex_data(&ecdh->ex_data, idx)); 272280297Sjkim} 273