d997007f0c6d65634805751a934e1bfa55b1decf
[tinc] / src / openssl / ecdh.c
1 /*
2     ecdh.c -- Diffie-Hellman key exchange handling
3     Copyright (C) 2011-2013 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
22 #include <openssl/err.h>
23 #include <openssl/ec.h>
24 #include <openssl/ecdh.h>
25 #include <openssl/obj_mac.h>
26
27 #define __TINC_ECDH_INTERNAL__
28 typedef EC_KEY ecdh_t;
29
30 #include "../ecdh.h"
31 #include "../logger.h"
32 #include "../utils.h"
33 #include "../xalloc.h"
34
35 ecdh_t *ecdh_generate_public(void *pubkey) {
36         ecdh_t *ecdh = EC_KEY_new_by_curve_name(NID_secp521r1);
37         if(!ecdh) {
38                 logger(DEBUG_ALWAYS, LOG_ERR, "Generating EC key_by_curve_name failed: %s", ERR_error_string(ERR_get_error(), NULL));
39                 return false;
40         }
41
42         if(!EC_KEY_generate_key(ecdh)) {
43                 EC_KEY_free(ecdh);
44                 logger(DEBUG_ALWAYS, LOG_ERR, "Generating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL));
45                 return NULL;
46         }
47
48         const EC_POINT *point = EC_KEY_get0_public_key(ecdh);
49         if(!point) {
50                 EC_KEY_free(ecdh);
51                 logger(DEBUG_ALWAYS, LOG_ERR, "Getting public key failed: %s", ERR_error_string(ERR_get_error(), NULL));
52                 return NULL;
53         }
54
55         size_t result = EC_POINT_point2oct(EC_KEY_get0_group(ecdh), point, POINT_CONVERSION_COMPRESSED, pubkey, ECDH_SIZE, NULL);
56         if(!result) {
57                 EC_KEY_free(ecdh);
58                 logger(DEBUG_ALWAYS, LOG_ERR, "Converting EC_POINT to binary failed: %s", ERR_error_string(ERR_get_error(), NULL));
59                 return NULL;
60         }
61
62         return ecdh;
63 }
64
65 bool ecdh_compute_shared(ecdh_t *ecdh, const void *pubkey, void *shared) {
66         EC_POINT *point = EC_POINT_new(EC_KEY_get0_group(ecdh));
67         if(!point) {
68                 logger(DEBUG_ALWAYS, LOG_ERR, "EC_POINT_new() failed: %s", ERR_error_string(ERR_get_error(), NULL));
69                 EC_KEY_free(ecdh);
70                 return false;
71         }
72
73         int result = EC_POINT_oct2point(EC_KEY_get0_group(ecdh), point, pubkey, ECDH_SIZE, NULL);
74         if(!result) {
75                 EC_POINT_free(point);
76                 EC_KEY_free(ecdh);
77                 logger(DEBUG_ALWAYS, LOG_ERR, "Converting binary to EC_POINT failed: %s", ERR_error_string(ERR_get_error(), NULL));
78                 return false;
79         }
80
81         result = ECDH_compute_key(shared, ECDH_SIZE, point, ecdh, NULL);
82         EC_POINT_free(point);
83         EC_KEY_free(ecdh);
84
85         if(!result) {
86                 logger(DEBUG_ALWAYS, LOG_ERR, "Computing Elliptic Curve Diffie-Hellman shared key failed: %s", ERR_error_string(ERR_get_error(), NULL));
87                 return false;
88         }
89
90         return true;
91 }
92
93 void ecdh_free(ecdh_t *ecdh) {
94         if(ecdh)
95                 EC_KEY_free(ecdh);
96 }