2 ecdsa.c -- ECDSA key handling
3 Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
20 #include "../system.h"
22 #include <openssl/pem.h>
23 #include <openssl/err.h>
25 #define __TINC_ECDSA_INTERNAL__
26 typedef EC_KEY ecdsa_t;
28 #include "../logger.h"
31 #include "../xalloc.h"
33 // Get and set ECDSA keys
35 ecdsa_t *ecdsa_set_base64_public_key(const char *p) {
36 ecdsa_t *ecdsa = EC_KEY_new_by_curve_name(NID_secp521r1);
38 logger(DEBUG_ALWAYS, LOG_DEBUG, "EC_KEY_new_by_curve_name failed: %s", ERR_error_string(ERR_get_error(), NULL));
43 unsigned char pubkey[len / 4 * 3 + 3];
44 const unsigned char *ppubkey = pubkey;
45 len = b64decode(p, (char *)pubkey, len);
47 if(!o2i_ECPublicKey(&ecdsa, &ppubkey, len)) {
48 logger(DEBUG_ALWAYS, LOG_DEBUG, "o2i_ECPublicKey failed: %s", ERR_error_string(ERR_get_error(), NULL));
56 char *ecdsa_get_base64_public_key(ecdsa_t *ecdsa) {
57 unsigned char *pubkey = NULL;
58 int len = i2o_ECPublicKey(ecdsa, &pubkey);
60 char *base64 = xmalloc(len * 4 / 3 + 5);
61 b64encode((char *)pubkey, base64, len);
68 // Read PEM ECDSA keys
70 ecdsa_t *ecdsa_read_pem_public_key(FILE *fp) {
71 ecdsa_t *ecdsa = PEM_read_EC_PUBKEY(fp, NULL, NULL, NULL);
74 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read ECDSA public key: %s", ERR_error_string(ERR_get_error(), NULL));
79 ecdsa_t *ecdsa_read_pem_private_key(FILE *fp) {
80 ecdsa_t *ecdsa = PEM_read_ECPrivateKey(fp, NULL, NULL, NULL);
83 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read ECDSA private key: %s", ERR_error_string(ERR_get_error(), NULL));
88 size_t ecdsa_size(ecdsa_t *ecdsa) {
89 return ECDSA_size(ecdsa);
92 // TODO: standardise output format?
94 bool ecdsa_sign(ecdsa_t *ecdsa, const void *in, size_t len, void *sig) {
95 unsigned int siglen = ECDSA_size(ecdsa);
97 unsigned char hash[SHA512_DIGEST_LENGTH];
98 SHA512(in, len, hash);
100 memset(sig, 0, siglen);
102 if(!ECDSA_sign(0, hash, sizeof hash, sig, &siglen, ecdsa)) {
103 logger(DEBUG_ALWAYS, LOG_DEBUG, "ECDSA_sign() failed: %s", ERR_error_string(ERR_get_error(), NULL));
110 bool ecdsa_verify(ecdsa_t *ecdsa, const void *in, size_t len, const void *sig) {
111 unsigned int siglen = ECDSA_size(ecdsa);
113 unsigned char hash[SHA512_DIGEST_LENGTH];
114 SHA512(in, len, hash);
116 if(!ECDSA_verify(0, hash, sizeof hash, sig, siglen, ecdsa)) {
117 logger(DEBUG_ALWAYS, LOG_DEBUG, "ECDSA_verify() failed: %s", ERR_error_string(ERR_get_error(), NULL));
124 bool ecdsa_active(ecdsa_t *ecdsa) {
128 void ecdsa_free(ecdsa_t *ecdsa) {