summaryrefslogtreecommitdiff
path: root/src/pubkey.cpp
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2021-01-24 22:46:48 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2021-01-29 22:28:01 +0100
commita16b33031c7bcf2eabf1e0c3571000234b7740df (patch)
treef012ff7814abe7fa22c5a610388faf4310d53772 /src/pubkey.cpp
parent3a3cce8eed7e43faa8c9f606e56bb43ba3bd9451 (diff)
Attach service
Diffstat (limited to 'src/pubkey.cpp')
-rw-r--r--src/pubkey.cpp39
1 files changed, 39 insertions, 0 deletions
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> SecretKey::load(const Stored<PublicKey> & pub)
return SecretKey(key, pub);
}
+optional<SecretKey> SecretKey::fromData(const Stored<PublicKey> & pub, const vector<uint8_t> & sdata)
+{
+ shared_ptr<EVP_PKEY> pkey(
+ EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL,
+ sdata.data(), sdata.size()),
+ EVP_PKEY_free);
+ if (!pkey)
+ return nullopt;
+
+ vector<uint8_t> 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<uint8_t> SecretKey::getData() const
+{
+ vector<uint8_t> 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<uint8_t> SecretKey::sign(const Digest & dgst) const
{
unique_ptr<EVP_MD_CTX, void(*)(EVP_MD_CTX*)>