Add the brainpoolp512r1 curve and use it.
[tinc] / src / openssl / ecdsa.c
1 /*
2     ecdsa.c -- ECDSA key 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/pem.h>
23 #include <openssl/err.h>
24
25 #define __TINC_ECDSA_INTERNAL__
26 typedef EC_KEY ecdsa_t;
27
28 #include "../logger.h"
29 #include "../ecdsa.h"
30 #include "../utils.h"
31 #include "../xalloc.h"
32
33 #include "brainpool.h"
34
35 // Get and set ECDSA keys
36 //
37 ecdsa_t *ecdsa_set_base64_public_key(const char *p) {
38         ecdsa_t *ecdsa = EC_KEY_new();
39         if(!ecdsa) {
40                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Allocating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL));
41                 return NULL;
42         }
43
44         EC_KEY_set_group(ecdsa, brainpoolp512r1);
45
46         int len = strlen(p);
47         unsigned char pubkey[len / 4 * 3 + 3];
48         const unsigned char *ppubkey = pubkey;
49         len = b64decode(p, (char *)pubkey, len);
50
51         if(!o2i_ECPublicKey(&ecdsa, &ppubkey, len)) {
52                 logger(DEBUG_ALWAYS, LOG_DEBUG, "o2i_ECPublicKey failed: %s", ERR_error_string(ERR_get_error(), NULL));
53                 EC_KEY_free(ecdsa);
54                 return NULL;
55         }
56
57         return ecdsa;
58 }
59
60 char *ecdsa_get_base64_public_key(ecdsa_t *ecdsa) {
61         unsigned char *pubkey = NULL;
62         int len = i2o_ECPublicKey(ecdsa, &pubkey);
63
64         char *base64 = xmalloc(len * 4 / 3 + 5);
65         b64encode((char *)pubkey, base64, len);
66
67         free(pubkey);
68
69         return base64;
70 }
71
72 // Read PEM ECDSA keys
73
74 ecdsa_t *ecdsa_read_pem_public_key(FILE *fp) {
75         ecdsa_t *ecdsa = PEM_read_EC_PUBKEY(fp, NULL, NULL, NULL);
76
77         if(!ecdsa)
78                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read ECDSA public key: %s", ERR_error_string(ERR_get_error(), NULL));
79
80         return ecdsa;
81 }
82
83 ecdsa_t *ecdsa_read_pem_private_key(FILE *fp) {
84         ecdsa_t *ecdsa = PEM_read_ECPrivateKey(fp, NULL, NULL, NULL);
85
86         if(!ecdsa)
87                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read ECDSA private key: %s", ERR_error_string(ERR_get_error(), NULL));
88
89         return ecdsa;
90 }
91
92 size_t ecdsa_size(ecdsa_t *ecdsa) {
93         return ECDSA_size(ecdsa);
94 }
95
96 // TODO: standardise output format?
97
98 bool ecdsa_sign(ecdsa_t *ecdsa, const void *in, size_t len, void *sig) {
99         unsigned int siglen = ECDSA_size(ecdsa);
100
101         unsigned char hash[SHA512_DIGEST_LENGTH];
102         SHA512(in, len, hash);
103
104         memset(sig, 0, siglen);
105
106         if(!ECDSA_sign(0, hash, sizeof hash, sig, &siglen, ecdsa)) {
107                 logger(DEBUG_ALWAYS, LOG_DEBUG, "ECDSA_sign() failed: %s", ERR_error_string(ERR_get_error(), NULL));
108                 return false;
109         }
110
111         return true;
112 }
113
114 bool ecdsa_verify(ecdsa_t *ecdsa, const void *in, size_t len, const void *sig) {
115         unsigned int siglen = ECDSA_size(ecdsa);
116
117         unsigned char hash[SHA512_DIGEST_LENGTH];
118         SHA512(in, len, hash);
119
120         if(!ECDSA_verify(0, hash, sizeof hash, sig, siglen, ecdsa)) {
121                 logger(DEBUG_ALWAYS, LOG_DEBUG, "ECDSA_verify() failed: %s", ERR_error_string(ERR_get_error(), NULL));
122                 return false;
123         }
124
125         return true;
126 }
127
128 bool ecdsa_active(ecdsa_t *ecdsa) {
129         return ecdsa;
130 }
131
132 void ecdsa_free(ecdsa_t *ecdsa) {
133         if(ecdsa)
134                 EC_KEY_free(ecdsa);
135 }