From a16b33031c7bcf2eabf1e0c3571000234b7740df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 24 Jan 2021 22:46:48 +0100 Subject: Attach service --- src/pubkey.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/pubkey.cpp') diff --git a/src/pubkey.cpp b/src/pubkey.cpp index 6d85d33..6461c10 100644 --- a/src/pubkey.cpp +++ b/src/pubkey.cpp @@ -88,6 +88,45 @@ optional SecretKey::load(const Stored & pub) return SecretKey(key, pub); } +optional SecretKey::fromData(const Stored & pub, const vector & sdata) +{ + shared_ptr pkey( + EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL, + sdata.data(), sdata.size()), + EVP_PKEY_free); + if (!pkey) + return nullopt; + + vector keyData; + size_t keyLen; + + EVP_PKEY_get_raw_public_key(pkey.get(), nullptr, &keyLen); + keyData.resize(keyLen); + EVP_PKEY_get_raw_public_key(pkey.get(), keyData.data(), &keyLen); + + EVP_PKEY_get_raw_public_key(pub->key.get(), nullptr, &keyLen); + keyData.resize(keyLen); + EVP_PKEY_get_raw_public_key(pub->key.get(), keyData.data(), &keyLen); + + if (EVP_PKEY_cmp(pkey.get(), pub->key.get()) != 1) + return nullopt; + + pub.ref().storage().storeKey(pub.ref(), sdata); + return SecretKey(std::move(pkey), pub); +} + +vector SecretKey::getData() const +{ + vector keyData; + size_t keyLen; + + EVP_PKEY_get_raw_private_key(key.get(), nullptr, &keyLen); + keyData.resize(keyLen); + EVP_PKEY_get_raw_private_key(key.get(), keyData.data(), &keyLen); + + return keyData; +} + vector SecretKey::sign(const Digest & dgst) const { unique_ptr -- cgit v1.2.3