X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fopenssl%2Fecdh.c;h=f94555dbd7cdb6cf5c43229ca591dca644da96a4;hb=70a1a5594af5d4e6a364186b42ba4e34c676009b;hp=642b3c3097b148cd01a963849a6f6af9986aee9e;hpb=ee8a214318fd6dbe6bc5d6b510896f30d92d46c6;p=tinc diff --git a/src/openssl/ecdh.c b/src/openssl/ecdh.c index 642b3c30..f94555db 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-2012 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 @@ -28,60 +28,69 @@ #include "ecdh.h" #include "logger.h" -EC_GROUP *secp256k1 = NULL; -EC_GROUP *secp384r1 = NULL; -EC_GROUP *secp521r1 = NULL; - -// TODO: proper KDF -static void *kdf(const void *in, size_t inlen, void *out, size_t *outlen) { - memcpy(out, in, inlen); - *outlen = inlen; - return out; -} - bool ecdh_generate_public(ecdh_t *ecdh, void *pubkey) { - if(!secp521r1) - secp521r1 = EC_GROUP_new_by_curve_name(NID_secp521r1); - *ecdh = EC_KEY_new_by_curve_name(NID_secp521r1); + if(!*ecdh) { + logger(DEBUG_ALWAYS, LOG_ERR, "Generating EC key_by_curve_name failed: %s", ERR_error_string(ERR_get_error(), NULL)); + return false; + } + if(!EC_KEY_generate_key(*ecdh)) { - logger(LOG_ERR, "Generating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL)); - abort(); + EC_KEY_free(*ecdh); + *ecdh = NULL; + logger(DEBUG_ALWAYS, LOG_ERR, "Generating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL)); + return false; } - + 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)); - abort(); + EC_KEY_free(*ecdh); + *ecdh = NULL; + logger(DEBUG_ALWAYS, LOG_ERR, "Getting public key failed: %s", ERR_error_string(ERR_get_error(), NULL)); + return false; } - size_t result = EC_POINT_point2oct(secp521r1, 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)); - abort(); + EC_KEY_free(*ecdh); + *ecdh = NULL; + logger(DEBUG_ALWAYS, LOG_ERR, "Converting EC_POINT to binary failed: %s", ERR_error_string(ERR_get_error(), NULL)); + return false; } return true; } bool ecdh_compute_shared(ecdh_t *ecdh, const void *pubkey, void *shared) { - EC_POINT *point = EC_POINT_new(secp521r1); - - int result = EC_POINT_oct2point(secp521r1, point, pubkey, ECDH_SIZE, NULL); + EC_POINT *point = EC_POINT_new(EC_KEY_get0_group(*ecdh)); if(!point) { - logger(LOG_ERR, "Converting binary to EC_POINT failed: %s", ERR_error_string(ERR_get_error(), NULL)); - abort(); + logger(DEBUG_ALWAYS, LOG_ERR, "EC_POINT_new() failed: %s", ERR_error_string(ERR_get_error(), NULL)); + return false; + } + + int result = EC_POINT_oct2point(EC_KEY_get0_group(*ecdh), point, pubkey, ECDH_SIZE, NULL); + if(!result) { + EC_POINT_free(point); + 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, kdf); + result = ECDH_compute_key(shared, ECDH_SIZE, point, *ecdh, NULL); EC_POINT_free(point); EC_KEY_free(*ecdh); *ecdh = NULL; 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; } return true; } + +void ecdh_free(ecdh_t *ecdh) { + if(*ecdh) { + EC_KEY_free(*ecdh); + *ecdh = NULL; + } +}