Fix whitespace.
[tinc] / src / openssl / ecdh.c
1 /*
2     ecdh.c -- Diffie-Hellman key exchange handling
3     Copyright (C) 2011 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21 #include "utils.h"
22 #include "xalloc.h"
23
24 #include <openssl/err.h>
25 #include <openssl/ec.h>
26 #include <openssl/obj_mac.h>
27
28 #include "ecdh.h"
29 #include "logger.h"
30
31 bool ecdh_generate_public(ecdh_t *ecdh, void *pubkey) {
32         *ecdh = EC_KEY_new_by_curve_name(NID_secp521r1);
33         if(!*ecdh) {
34                 logger(DEBUG_ALWAYS, LOG_ERR, "Generating EC key_by_curve_name failed: %s", ERR_error_string(ERR_get_error(), NULL));
35                 return false;
36         }
37
38         if(!EC_KEY_generate_key(*ecdh)) {
39                 EC_KEY_free(*ecdh);
40                 *ecdh = NULL;
41                 logger(DEBUG_ALWAYS, LOG_ERR, "Generating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL));
42                 return false;
43         }
44
45         const EC_POINT *point = EC_KEY_get0_public_key(*ecdh);
46         if(!point) {
47                 EC_KEY_free(*ecdh);
48                 *ecdh = NULL;
49                 logger(DEBUG_ALWAYS, LOG_ERR, "Getting public key failed: %s", ERR_error_string(ERR_get_error(), NULL));
50                 return false;
51         }
52
53         size_t result = EC_POINT_point2oct(EC_KEY_get0_group(*ecdh), point, POINT_CONVERSION_COMPRESSED, pubkey, ECDH_SIZE, NULL);
54         if(!result) {
55                 EC_KEY_free(*ecdh);
56                 *ecdh = NULL;
57                 logger(DEBUG_ALWAYS, LOG_ERR, "Converting EC_POINT to binary failed: %s", ERR_error_string(ERR_get_error(), NULL));
58                 return false;
59         }
60
61         return true;
62 }
63
64 bool ecdh_compute_shared(ecdh_t *ecdh, const void *pubkey, void *shared) {
65         EC_POINT *point = EC_POINT_new(EC_KEY_get0_group(*ecdh));
66         if(!point) {
67                 logger(DEBUG_ALWAYS, LOG_ERR, "EC_POINT_new() failed: %s", ERR_error_string(ERR_get_error(), NULL));
68                 return false;
69         }
70
71         int result = EC_POINT_oct2point(EC_KEY_get0_group(*ecdh), point, pubkey, ECDH_SIZE, NULL);
72         if(!result) {
73                 EC_POINT_free(point);
74                 logger(DEBUG_ALWAYS, LOG_ERR, "Converting binary to EC_POINT failed: %s", ERR_error_string(ERR_get_error(), NULL));
75                 return false;
76         }
77
78         result = ECDH_compute_key(shared, ECDH_SIZE, point, *ecdh, NULL);
79         EC_POINT_free(point);
80         EC_KEY_free(*ecdh);
81         *ecdh = NULL;
82
83         if(!result) {
84                 logger(DEBUG_ALWAYS, LOG_ERR, "Computing Elliptic Curve Diffie-Hellman shared key failed: %s", ERR_error_string(ERR_get_error(), NULL));
85                 return false;
86         }
87
88         return true;
89 }
90
91 void ecdh_free(ecdh_t *ecdh) {
92         if(*ecdh) {
93                 EC_KEY_free(*ecdh);
94                 *ecdh = NULL;
95         }
96 }