summaryrefslogtreecommitdiff
path: root/src/pubkey.cpp
blob: e26beade1f7e56caf12d54c8919ea89f1e96df46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "pubkey.h"

#include <stdexcept>

using std::unique_ptr;
using std::runtime_error;
using std::string;

using namespace erebos;

optional<PublicKey> PublicKey::load(const Ref & ref)
{
	auto rec = ref->asRecord();
	if (!rec)
		return nullopt;

	if (auto ktype = rec->item("type").asText())
		if (ktype.value() != "ed25519")
			throw runtime_error("unsupported key type " + ktype.value());

	if (auto pubkey = rec->item("pubkey").asBinary())
		return PublicKey(EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, nullptr,
					pubkey.value().data(), pubkey.value().size()));

	return nullopt;
}

Ref PublicKey::store(const Storage & st) const
{
	vector<Record::Item> items;

	items.emplace_back("type", "ed25519");

	vector<uint8_t> keyData;
	size_t keyLen;
	EVP_PKEY_get_raw_public_key(key.get(), nullptr, &keyLen);
	keyData.resize(keyLen);
	EVP_PKEY_get_raw_public_key(key.get(), keyData.data(), &keyLen);
	items.emplace_back("pubkey", keyData);

	return st.storeObject(Record(std::move(items)));
}

SecretKey SecretKey::generate(const Storage & st)
{
	unique_ptr<EVP_PKEY_CTX, void(*)(EVP_PKEY_CTX*)>
		pctx(EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, NULL), &EVP_PKEY_CTX_free);
	if (!pctx)
		throw runtime_error("failed to generate key");

	if (EVP_PKEY_keygen_init(pctx.get()) != 1)
		throw runtime_error("failed to generate key");

	EVP_PKEY *pkey = NULL;
	if (EVP_PKEY_keygen(pctx.get(), &pkey) != 1)
		throw runtime_error("failed to generate key");
	shared_ptr<EVP_PKEY> seckey(pkey, EVP_PKEY_free);

	vector<uint8_t> keyData;
	size_t keyLen;

	EVP_PKEY_get_raw_public_key(seckey.get(), nullptr, &keyLen);
	keyData.resize(keyLen);
	EVP_PKEY_get_raw_public_key(seckey.get(), keyData.data(), &keyLen);
	auto pubkey = st.store(PublicKey(EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, nullptr,
					keyData.data(), keyData.size())));

	EVP_PKEY_get_raw_private_key(seckey.get(), nullptr, &keyLen);
	keyData.resize(keyLen);
	EVP_PKEY_get_raw_private_key(seckey.get(), keyData.data(), &keyLen);
	st.storeKey(pubkey.ref, keyData);

	return SecretKey(std::move(seckey), pubkey);
}

optional<SecretKey> SecretKey::load(const Stored<PublicKey> & pub)
{
	auto keyData = pub.ref.storage().loadKey(pub.ref);
	if (!keyData)
		return nullopt;

	EVP_PKEY * key = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, nullptr,
				keyData->data(), keyData->size());
	if (!key)
		throw runtime_error("falied to parse secret key");
	return SecretKey(key, pub);
}

vector<uint8_t> SecretKey::sign(const Digest & dgst) const
{
	unique_ptr<EVP_MD_CTX, void(*)(EVP_MD_CTX*)>
		mdctx(EVP_MD_CTX_create(), &EVP_MD_CTX_free);
	if (!mdctx)
		throw runtime_error("failed to create EVP_MD_CTX");

	if (EVP_DigestSignInit(mdctx.get(), nullptr, EVP_md_null(),
				nullptr, key.get()) != 1)
		throw runtime_error("failed to initialize EVP_MD_CTX");

	size_t sigLen;
	if (EVP_DigestSign(mdctx.get(), nullptr, &sigLen,
			dgst.arr().data(), Digest::size) != 1)
		throw runtime_error("failed to sign data");

	vector<uint8_t> sigData(sigLen);
	if (EVP_DigestSign(mdctx.get(), sigData.data(), &sigLen,
			dgst.arr().data(), Digest::size) != 1)
		throw runtime_error("failed to sign data");

	return sigData;
}

optional<Signature> Signature::load(const Ref & ref)
{
	auto rec = ref->asRecord();
	if (!rec)
		return nullopt;

	auto key = rec->item("key").as<PublicKey>();
	auto sig = rec->item("sig").asBinary();

	if (!key || !sig)
		return nullopt;

	return Signature {
		.key = key.value(),
		.sig = sig.value(),
	};
}

Ref Signature::store(const Storage & st) const
{
	vector<Record::Item> items;

	items.emplace_back("key", key);
	items.emplace_back("sig", sig);

	return st.storeObject(Record(std::move(items)));
}

bool Signature::verify(const Ref & ref) const
{
	unique_ptr<EVP_MD_CTX, void(*)(EVP_MD_CTX*)>
		mdctx(EVP_MD_CTX_create(), &EVP_MD_CTX_free);
	if (!mdctx)
		throw runtime_error("failed to create EVP_MD_CTX");

	if (EVP_DigestVerifyInit(mdctx.get(), nullptr, EVP_md_null(),
				nullptr, key->key.get()) != 1)
		throw runtime_error("failed to initialize EVP_MD_CTX");

	return EVP_DigestVerify(mdctx.get(), sig.data(), sig.size(),
			ref.digest().arr().data(), Digest::size) == 1;
}