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 #include "brainpool.h"
35 // Get and set ECDSA keys
37 ecdsa_t *ecdsa_set_base64_public_key(const char *p) {
38 ecdsa_t *ecdsa = EC_KEY_new();
40 logger(DEBUG_ALWAYS, LOG_DEBUG, "Allocating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL));
44 EC_KEY_set_group(ecdsa, brainpoolp512r1);
47 unsigned char pubkey[len / 4 * 3 + 3];
48 const unsigned char *ppubkey = pubkey;
49 len = b64decode(p, (char *)pubkey, len);
51 if(!o2i_ECPublicKey(&ecdsa, &ppubkey, len)) {
52 logger(DEBUG_ALWAYS, LOG_DEBUG, "o2i_ECPublicKey failed: %s", ERR_error_string(ERR_get_error(), NULL));
60 char *ecdsa_get_base64_public_key(ecdsa_t *ecdsa) {
61 unsigned char *pubkey = NULL;
62 int len = i2o_ECPublicKey(ecdsa, &pubkey);
64 char *base64 = xmalloc(len * 4 / 3 + 5);
65 b64encode((char *)pubkey, base64, len);
72 // Read PEM ECDSA keys
74 ecdsa_t *ecdsa_read_pem_public_key(FILE *fp) {
75 ecdsa_t *ecdsa = PEM_read_EC_PUBKEY(fp, NULL, NULL, NULL);
78 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read ECDSA public key: %s", ERR_error_string(ERR_get_error(), NULL));
83 ecdsa_t *ecdsa_read_pem_private_key(FILE *fp) {
84 ecdsa_t *ecdsa = PEM_read_ECPrivateKey(fp, NULL, NULL, NULL);
87 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read ECDSA private key: %s", ERR_error_string(ERR_get_error(), NULL));
92 size_t ecdsa_size(ecdsa_t *ecdsa) {
93 return ECDSA_size(ecdsa);
96 // TODO: standardise output format?
98 bool ecdsa_sign(ecdsa_t *ecdsa, const void *in, size_t len, void *sig) {
99 unsigned int siglen = ECDSA_size(ecdsa);
101 unsigned char hash[SHA512_DIGEST_LENGTH];
102 SHA512(in, len, hash);
104 memset(sig, 0, siglen);
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));
114 bool ecdsa_verify(ecdsa_t *ecdsa, const void *in, size_t len, const void *sig) {
115 unsigned int siglen = ECDSA_size(ecdsa);
117 unsigned char hash[SHA512_DIGEST_LENGTH];
118 SHA512(in, len, hash);
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));
128 bool ecdsa_active(ecdsa_t *ecdsa) {
132 void ecdsa_free(ecdsa_t *ecdsa) {