X-Git-Url: https://tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Fopenssl%2Fecdh.c;h=fde8e8a49c75a8ba58d1c36257a5c87a3f652df0;hp=804605c43908379264859d91062080e0e08a49de;hb=a7458f6dfd86dd345b43690ef977dc034f550e68;hpb=3fba80174dbe29bcfe0d121a2a1d2e61be5ee57b diff --git a/src/openssl/ecdh.c b/src/openssl/ecdh.c index 804605c4..fde8e8a4 100644 --- a/src/openssl/ecdh.c +++ b/src/openssl/ecdh.c @@ -1,6 +1,6 @@ /* ecdh.c -- Diffie-Hellman key exchange handling - Copyright (C) 2011 Guus Sliepen + Copyright (C) 2011-2013 Guus Sliepen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,59 +17,75 @@ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "system.h" -#include "utils.h" -#include "xalloc.h" +#include "../system.h" #include #include +#include #include -#include "ecdh.h" -#include "logger.h" +#define __TINC_ECDH_INTERNAL__ +typedef EC_KEY ecdh_t; -bool ecdh_generate_public(ecdh_t *ecdh, void *pubkey) { - *ecdh = EC_KEY_new_by_curve_name(NID_secp521r1); - if(!EC_KEY_generate_key(*ecdh)) { - logger(LOG_ERR, "Generating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL)); +#include "../ecdh.h" +#include "../logger.h" +#include "../utils.h" +#include "../xalloc.h" + +#include "brainpool.h" + +ecdh_t *ecdh_generate_public(void *pubkey) { + ecdh_t *ecdh = EC_KEY_new(); + if(!ecdh) { + logger(DEBUG_ALWAYS, LOG_ERR, "Allocating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL)); return false; } - - const EC_POINT *point = EC_KEY_get0_public_key(*ecdh); + + if(!EC_KEY_set_group(ecdh, brainpoolp512r1) || !EC_KEY_generate_key(ecdh)) { + EC_KEY_free(ecdh); + logger(DEBUG_ALWAYS, LOG_ERR, "Generating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL)); + return NULL; + } + + const EC_POINT *point = EC_KEY_get0_public_key(ecdh); if(!point) { - logger(LOG_ERR, "Getting public key failed: %s", ERR_error_string(ERR_get_error(), NULL)); - return false; + EC_KEY_free(ecdh); + logger(DEBUG_ALWAYS, LOG_ERR, "Getting public key failed: %s", ERR_error_string(ERR_get_error(), NULL)); + return NULL; } - size_t result = EC_POINT_point2oct(EC_KEY_get0_group(*ecdh), point, POINT_CONVERSION_COMPRESSED, pubkey, ECDH_SIZE, NULL); + size_t result = EC_POINT_point2oct(EC_KEY_get0_group(ecdh), point, POINT_CONVERSION_COMPRESSED, pubkey, ECDH_SIZE, NULL); if(!result) { - logger(LOG_ERR, "Converting EC_POINT to binary failed: %s", ERR_error_string(ERR_get_error(), NULL)); - return false; + EC_KEY_free(ecdh); + logger(DEBUG_ALWAYS, LOG_ERR, "Converting EC_POINT to binary failed: %s", ERR_error_string(ERR_get_error(), NULL)); + return NULL; } - return true; + return ecdh; } bool ecdh_compute_shared(ecdh_t *ecdh, const void *pubkey, void *shared) { - EC_POINT *point = EC_POINT_new(EC_KEY_get0_group(*ecdh)); + EC_POINT *point = EC_POINT_new(EC_KEY_get0_group(ecdh)); if(!point) { - logger(LOG_ERR, "EC_POINT_new() failed: %s", ERR_error_string(ERR_get_error(), NULL)); + logger(DEBUG_ALWAYS, LOG_ERR, "EC_POINT_new() failed: %s", ERR_error_string(ERR_get_error(), NULL)); + EC_KEY_free(ecdh); return false; } - int result = EC_POINT_oct2point(EC_KEY_get0_group(*ecdh), point, pubkey, ECDH_SIZE, NULL); + int result = EC_POINT_oct2point(EC_KEY_get0_group(ecdh), point, pubkey, ECDH_SIZE, NULL); if(!result) { - logger(LOG_ERR, "Converting binary to EC_POINT failed: %s", ERR_error_string(ERR_get_error(), NULL)); + EC_POINT_free(point); + EC_KEY_free(ecdh); + logger(DEBUG_ALWAYS, LOG_ERR, "Converting binary to EC_POINT failed: %s", ERR_error_string(ERR_get_error(), NULL)); return false; } - result = ECDH_compute_key(shared, ECDH_SIZE, point, *ecdh, NULL); + result = ECDH_compute_key(shared, ECDH_SIZE, point, ecdh, NULL); EC_POINT_free(point); - EC_KEY_free(*ecdh); - *ecdh = NULL; + EC_KEY_free(ecdh); if(!result) { - logger(LOG_ERR, "Computing Elliptic Curve Diffie-Hellman shared key failed: %s", ERR_error_string(ERR_get_error(), NULL)); + logger(DEBUG_ALWAYS, LOG_ERR, "Computing Elliptic Curve Diffie-Hellman shared key failed: %s", ERR_error_string(ERR_get_error(), NULL)); return false; } @@ -77,8 +93,6 @@ bool ecdh_compute_shared(ecdh_t *ecdh, const void *pubkey, void *shared) { } void ecdh_free(ecdh_t *ecdh) { - if(*ecdh) { - EC_KEY_free(*ecdh); - *ecdh = NULL; - } + if(ecdh) + EC_KEY_free(ecdh); }