From 867bcf151483f2c9192a165aab6fa3fa5684efb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Tue, 29 Nov 2016 17:00:04 -0500 Subject: [PATCH] crypto: add EC key generation --- include/opendht/crypto.h | 1 + src/crypto.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/opendht/crypto.h b/include/opendht/crypto.h index 1390f4ca..3514ba75 100644 --- a/include/opendht/crypto.h +++ b/include/opendht/crypto.h @@ -148,6 +148,7 @@ struct OPENDHT_PUBLIC PrivateKey * Recommended values: 4096, 8192 */ static PrivateKey generate(unsigned key_length = 4096); + static PrivateKey generateEC(); gnutls_privkey_t key {}; gnutls_x509_privkey_t x509_key {}; diff --git a/src/crypto.cpp b/src/crypto.cpp index 280e472a..f34849a4 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -739,6 +739,20 @@ PrivateKey::generate(unsigned key_length) return PrivateKey{key}; } +PrivateKey +PrivateKey::generateEC() +{ + gnutls_x509_privkey_t key; + if (gnutls_x509_privkey_init(&key) != GNUTLS_E_SUCCESS) + throw CryptoException("Can't initialize private key."); + int err = gnutls_x509_privkey_generate(key, GNUTLS_PK_EC, gnutls_sec_param_to_pk_bits(GNUTLS_PK_EC, GNUTLS_SEC_PARAM_ULTRA), 0); + if (err != GNUTLS_E_SUCCESS) { + gnutls_x509_privkey_deinit(key); + throw CryptoException(std::string("Can't generate EC key pair: ") + gnutls_strerror(err)); + } + return PrivateKey{key}; +} + Identity generateIdentity(const std::string& name, crypto::Identity ca, unsigned key_length, bool is_ca) { -- GitLab