Preliminary implementation of Elliptic Curve Diffie-Hellman Ephemeral key exchange.
[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 EC_GROUP *secp256k1 = NULL;
32 EC_GROUP *secp384r1 = NULL;
33 EC_GROUP *secp521r1 = NULL;
34
35 // TODO: proper KDF
36 static void *kdf(const void *in, size_t inlen, void *out, size_t *outlen) {
37         memcpy(out, in, inlen);
38         *outlen = inlen;
39         return out;
40 }
41
42 bool ecdh_generate_public(ecdh_t *ecdh, void *pubkey) {
43         if(!secp521r1)
44                 secp521r1 = EC_GROUP_new_by_curve_name(NID_secp521r1);
45
46         *ecdh = EC_KEY_new_by_curve_name(NID_secp521r1);
47         if(!EC_KEY_generate_key(*ecdh)) {
48                 logger(LOG_ERR, "Generating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL));
49                 abort();
50         }
51         
52         const EC_POINT *point = EC_KEY_get0_public_key(*ecdh);
53         if(!point) {
54                 logger(LOG_ERR, "Getting public key failed: %s", ERR_error_string(ERR_get_error(), NULL));
55                 abort();
56         }
57
58         size_t result = EC_POINT_point2oct(secp521r1, point, POINT_CONVERSION_COMPRESSED, pubkey, ECDH_SIZE, NULL);
59         if(!result) {
60                 logger(LOG_ERR, "Converting EC_POINT to binary failed: %s", ERR_error_string(ERR_get_error(), NULL));
61                 abort();
62         }
63
64         return true;
65 }
66
67 bool ecdh_compute_shared(ecdh_t *ecdh, const void *pubkey, void *shared) {
68         EC_POINT *point = EC_POINT_new(secp521r1);
69
70         int result = EC_POINT_oct2point(secp521r1, point, pubkey, ECDH_SIZE, NULL);
71         if(!point) {
72                 logger(LOG_ERR, "Converting binary to EC_POINT failed: %s", ERR_error_string(ERR_get_error(), NULL));
73                 abort();
74         }
75
76         result = ECDH_compute_key(shared, ECDH_SIZE, point, *ecdh, kdf);
77         EC_POINT_free(point);
78         EC_KEY_free(*ecdh);
79         *ecdh = NULL;
80
81         if(!result) {
82                 logger(LOG_ERR, "Computing Elliptic Curve Diffie-Hellman shared key failed: %s", ERR_error_string(ERR_get_error(), NULL));
83                 return false;
84         }
85
86         return true;
87 }