summaryrefslogtreecommitdiff
path: root/src/pubkey.cpp
blob: 6d85d33b3f439870bf53d9edb916528f1d4755a2 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "pubkey.h"

#include <stdexcept>

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

using namespace erebos;

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

	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 PublicKey(nullptr);
}

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

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

	if (key) {
		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;
}

Signature Signature::load(const Ref & ref)
{
	if (auto rec = ref->asRecord())
		if (auto key = rec->item("key").as<PublicKey>())
			if (auto sig = rec->item("sig").asBinary())
				return Signature(*key, *sig);

	return Signature(Stored<PublicKey>::load(ref.storage().zref()), {});
}

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;
}


PublicKexKey PublicKexKey::load(const Ref & ref)
{
	auto rec = ref->asRecord();
	if (!rec)
		return PublicKexKey(nullptr);

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

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

	return PublicKexKey(nullptr);
}

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

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

	if (key) {
		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)));
}

SecretKexKey SecretKexKey::generate(const Storage & st)
{
	unique_ptr<EVP_PKEY_CTX, void(*)(EVP_PKEY_CTX*)>
		pctx(EVP_PKEY_CTX_new_id(EVP_PKEY_X25519, 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(PublicKexKey(EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, 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 SecretKexKey(std::move(seckey), pubkey);
}

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

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

vector<uint8_t> SecretKexKey::dh(const PublicKexKey & pubkey) const
{
	unique_ptr<EVP_PKEY_CTX, void(*)(EVP_PKEY_CTX*)>
		pctx(EVP_PKEY_CTX_new(key.get(), nullptr), &EVP_PKEY_CTX_free);
	if (!pctx)
		throw runtime_error("failed to derive shared secret");

	if (EVP_PKEY_derive_init(pctx.get()) <= 0)
		throw runtime_error("failed to derive shared secret");

	if (EVP_PKEY_derive_set_peer(pctx.get(), pubkey.key.get()) <= 0)
		throw runtime_error("failed to derive shared secret");

	size_t dhlen;
	if (EVP_PKEY_derive(pctx.get(), NULL, &dhlen) <= 0)
		throw runtime_error("failed to derive shared secret");

	vector<uint8_t> dhsecret(dhlen);

	if (EVP_PKEY_derive(pctx.get(), dhsecret.data(), &dhlen) <= 0)
		throw runtime_error("failed to derive shared secret");

	return dhsecret;
}