-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrainpool_ec_impl.cc
More file actions
454 lines (381 loc) · 11.5 KB
/
brainpool_ec_impl.cc
File metadata and controls
454 lines (381 loc) · 11.5 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Brainpool Elliptic Curve Cryptography Implementation
*
* Uses OpenSSL EVP API for Brainpool curve support:
* - OpenSSL 1.0.2+: Basic Brainpool curve support
* - OpenSSL 3.x: Improved Brainpool support with enhanced EVP API
*
* All operations use the EVP (Envelope) API which provides a consistent interface
* across OpenSSL versions and enables future-proofing.
*/
#include <gnuradio/linux_crypto/brainpool_ec_impl.h>
#include <gnuradio/linux_crypto/brainpool_ec.h>
#include <openssl/ec.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <cstring>
#include <sstream>
namespace gr {
namespace linux_crypto {
brainpool_ec_impl::brainpool_ec_impl(Curve curve)
: d_curve(curve), d_group(nullptr)
{
d_group = create_curve_group(d_curve);
// Errors are indicated by d_group being nullptr
// Error details available via OpenSSL error queue if needed
}
brainpool_ec_impl::~brainpool_ec_impl()
{
if (d_group) {
EC_GROUP_free(d_group);
}
}
EC_GROUP* brainpool_ec_impl::create_curve_group(Curve curve)
{
int nid = 0;
switch (curve) {
case Curve::BRAINPOOLP256R1:
nid = OBJ_sn2nid("brainpoolP256r1");
break;
case Curve::BRAINPOOLP384R1:
nid = OBJ_sn2nid("brainpoolP384r1");
break;
case Curve::BRAINPOOLP512R1:
nid = OBJ_sn2nid("brainpoolP512r1");
break;
}
if (nid == 0) {
// Curve not available - return nullptr to indicate error
return nullptr;
}
EC_GROUP* group = EC_GROUP_new_by_curve_name(nid);
if (!group) {
print_openssl_error();
return nullptr;
}
return group;
}
brainpool_ec_impl::KeyPair brainpool_ec_impl::generate_keypair()
{
return generate_keypair(d_curve);
}
brainpool_ec_impl::KeyPair brainpool_ec_impl::generate_keypair(Curve curve)
{
KeyPair keypair = { nullptr, nullptr };
EC_GROUP* group = create_curve_group(curve);
if (!group) {
return keypair;
}
EC_KEY* ec_key = EC_KEY_new();
if (!ec_key) {
EC_GROUP_free(group);
return keypair;
}
if (EC_KEY_set_group(ec_key, group) != 1) {
EC_GROUP_free(group);
EC_KEY_free(ec_key);
return keypair;
}
if (EC_KEY_generate_key(ec_key) != 1) {
EC_GROUP_free(group);
EC_KEY_free(ec_key);
print_openssl_error();
return keypair;
}
EVP_PKEY* pkey = EVP_PKEY_new();
if (!pkey) {
EC_GROUP_free(group);
EC_KEY_free(ec_key);
print_openssl_error();
return keypair;
}
if (EVP_PKEY_assign_EC_KEY(pkey, ec_key) != 1) {
EVP_PKEY_free(pkey);
EC_GROUP_free(group);
print_openssl_error();
return keypair;
}
EC_KEY* pub_ec_key = EC_KEY_new_by_curve_name(EC_GROUP_get_curve_name(group));
if (!pub_ec_key) {
EVP_PKEY_free(pkey);
EC_GROUP_free(group);
return keypair;
}
const EC_POINT* pub_point = EC_KEY_get0_public_key(ec_key);
if (EC_KEY_set_public_key(pub_ec_key, pub_point) != 1) {
EC_KEY_free(pub_ec_key);
EVP_PKEY_free(pkey);
EC_GROUP_free(group);
print_openssl_error();
return keypair;
}
EVP_PKEY* pub_pkey = EVP_PKEY_new();
if (!pub_pkey) {
EC_KEY_free(pub_ec_key);
EVP_PKEY_free(pkey);
EC_GROUP_free(group);
return keypair;
}
if (EVP_PKEY_assign_EC_KEY(pub_pkey, pub_ec_key) != 1) {
EVP_PKEY_free(pub_pkey);
EC_KEY_free(pub_ec_key);
EVP_PKEY_free(pkey);
EC_GROUP_free(group);
print_openssl_error();
return keypair;
}
EC_GROUP_free(group);
keypair.private_key = pkey;
keypair.public_key = pub_pkey;
return keypair;
}
std::vector<uint8_t> brainpool_ec_impl::ecdh_exchange(EVP_PKEY* private_key, EVP_PKEY* peer_public_key)
{
std::vector<uint8_t> shared_secret;
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(private_key, nullptr);
if (!ctx) {
print_openssl_error();
return shared_secret;
}
if (EVP_PKEY_derive_init(ctx) != 1) {
EVP_PKEY_CTX_free(ctx);
print_openssl_error();
return shared_secret;
}
if (EVP_PKEY_derive_set_peer(ctx, peer_public_key) != 1) {
EVP_PKEY_CTX_free(ctx);
print_openssl_error();
return shared_secret;
}
size_t secret_len = 0;
if (EVP_PKEY_derive(ctx, nullptr, &secret_len) != 1) {
EVP_PKEY_CTX_free(ctx);
print_openssl_error();
return shared_secret;
}
shared_secret.resize(secret_len);
if (EVP_PKEY_derive(ctx, shared_secret.data(), &secret_len) != 1) {
EVP_PKEY_CTX_free(ctx);
print_openssl_error();
shared_secret.clear();
return shared_secret;
}
EVP_PKEY_CTX_free(ctx);
return shared_secret;
}
std::vector<uint8_t> brainpool_ec_impl::sign(const std::vector<uint8_t>& data, EVP_PKEY* private_key)
{
return sign(data.data(), data.size(), private_key);
}
std::vector<uint8_t> brainpool_ec_impl::sign(const uint8_t* data, size_t data_len, EVP_PKEY* private_key)
{
std::vector<uint8_t> signature;
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
if (!ctx) {
print_openssl_error();
return signature;
}
if (EVP_DigestSignInit(ctx, nullptr, EVP_sha256(), nullptr, private_key) != 1) {
EVP_MD_CTX_free(ctx);
print_openssl_error();
return signature;
}
if (EVP_DigestSignUpdate(ctx, data, data_len) != 1) {
EVP_MD_CTX_free(ctx);
print_openssl_error();
return signature;
}
size_t sig_len = 0;
if (EVP_DigestSignFinal(ctx, nullptr, &sig_len) != 1) {
EVP_MD_CTX_free(ctx);
print_openssl_error();
return signature;
}
signature.resize(sig_len);
if (EVP_DigestSignFinal(ctx, signature.data(), &sig_len) != 1) {
EVP_MD_CTX_free(ctx);
print_openssl_error();
signature.clear();
return signature;
}
EVP_MD_CTX_free(ctx);
return signature;
}
bool brainpool_ec_impl::verify(const std::vector<uint8_t>& data,
const std::vector<uint8_t>& signature,
EVP_PKEY* public_key)
{
return verify(data.data(), data.size(), signature.data(), signature.size(), public_key);
}
bool brainpool_ec_impl::verify(const uint8_t* data, size_t data_len,
const uint8_t* signature, size_t sig_len,
EVP_PKEY* public_key)
{
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
if (!ctx) {
print_openssl_error();
return false;
}
if (EVP_DigestVerifyInit(ctx, nullptr, EVP_sha256(), nullptr, public_key) != 1) {
EVP_MD_CTX_free(ctx);
print_openssl_error();
return false;
}
if (EVP_DigestVerifyUpdate(ctx, data, data_len) != 1) {
EVP_MD_CTX_free(ctx);
print_openssl_error();
return false;
}
int result = EVP_DigestVerifyFinal(ctx, signature, sig_len);
EVP_MD_CTX_free(ctx);
return (result == 1);
}
std::vector<uint8_t> brainpool_ec_impl::serialize_public_key(EVP_PKEY* public_key)
{
std::vector<uint8_t> pem_data;
BIO* bio = BIO_new(BIO_s_mem());
if (!bio) {
print_openssl_error();
return pem_data;
}
if (PEM_write_bio_PUBKEY(bio, public_key) != 1) {
BIO_free(bio);
print_openssl_error();
return pem_data;
}
char* pem_ptr = nullptr;
long pem_len = BIO_get_mem_data(bio, &pem_ptr);
if (pem_len > 0 && pem_ptr) {
pem_data.assign(reinterpret_cast<const uint8_t*>(pem_ptr),
reinterpret_cast<const uint8_t*>(pem_ptr) + pem_len);
}
BIO_free(bio);
return pem_data;
}
std::vector<uint8_t> brainpool_ec_impl::serialize_private_key(EVP_PKEY* private_key, const std::string& password)
{
std::vector<uint8_t> pem_data;
BIO* bio = BIO_new(BIO_s_mem());
if (!bio) {
print_openssl_error();
return pem_data;
}
const EVP_CIPHER* cipher = nullptr;
const char* passwd = nullptr;
if (!password.empty()) {
cipher = EVP_aes_256_cbc();
passwd = password.c_str();
}
if (PEM_write_bio_PrivateKey(bio, private_key, cipher, nullptr, 0, nullptr,
const_cast<char*>(passwd)) != 1) {
BIO_free(bio);
print_openssl_error();
return pem_data;
}
char* pem_ptr = nullptr;
long pem_len = BIO_get_mem_data(bio, &pem_ptr);
if (pem_len > 0 && pem_ptr) {
pem_data.assign(reinterpret_cast<const uint8_t*>(pem_ptr),
reinterpret_cast<const uint8_t*>(pem_ptr) + pem_len);
}
BIO_free(bio);
return pem_data;
}
EVP_PKEY* brainpool_ec_impl::load_public_key(const std::vector<uint8_t>& pem_data)
{
BIO* bio = BIO_new_mem_buf(pem_data.data(), pem_data.size());
if (!bio) {
print_openssl_error();
return nullptr;
}
EVP_PKEY* pkey = PEM_read_bio_PUBKEY(bio, nullptr, nullptr, nullptr);
BIO_free(bio);
if (!pkey) {
print_openssl_error();
}
return pkey;
}
EVP_PKEY* brainpool_ec_impl::load_private_key(const std::vector<uint8_t>& pem_data, const std::string& password)
{
BIO* bio = BIO_new_mem_buf(pem_data.data(), pem_data.size());
if (!bio) {
print_openssl_error();
return nullptr;
}
const char* passwd = password.empty() ? nullptr : password.c_str();
EVP_PKEY* pkey = PEM_read_bio_PrivateKey(bio, nullptr, nullptr,
const_cast<char*>(passwd));
BIO_free(bio);
if (!pkey) {
print_openssl_error();
}
return pkey;
}
void brainpool_ec_impl::set_curve(Curve curve)
{
if (d_curve == curve) {
return;
}
if (d_group) {
EC_GROUP_free(d_group);
}
d_curve = curve;
d_group = create_curve_group(d_curve);
// Errors are indicated by d_group being nullptr
// Error details available via OpenSSL error queue if needed
}
std::string brainpool_ec_impl::curve_to_string(Curve curve)
{
switch (curve) {
case Curve::BRAINPOOLP256R1:
return "brainpoolP256r1";
case Curve::BRAINPOOLP384R1:
return "brainpoolP384r1";
case Curve::BRAINPOOLP512R1:
return "brainpoolP512r1";
default:
return "unknown";
}
}
brainpool_ec_impl::Curve brainpool_ec_impl::string_to_curve(const std::string& curve_name)
{
if (curve_name == "brainpoolP256r1") {
return Curve::BRAINPOOLP256R1;
} else if (curve_name == "brainpoolP384r1") {
return Curve::BRAINPOOLP384R1;
} else if (curve_name == "brainpoolP512r1") {
return Curve::BRAINPOOLP512R1;
}
return Curve::BRAINPOOLP256R1;
}
std::vector<std::string> brainpool_ec_impl::get_supported_curves()
{
return {
"brainpoolP256r1",
"brainpoolP384r1",
"brainpoolP512r1"
};
}
void brainpool_ec_impl::print_openssl_error()
{
// Error reporting is handled via return codes and exceptions
// Debug only - not for production use
// OpenSSL errors are propagated through return values and exceptions
(void)ERR_get_error(); // Clear error queue
}
// Wrapper class implementation
brainpool_ec::brainpool_ec(brainpool_ec_impl::Curve curve)
: brainpool_ec_impl(curve)
{
}
brainpool_ec::~brainpool_ec()
{
}
brainpool_ec::sptr brainpool_ec::make(brainpool_ec_impl::Curve curve)
{
return std::make_shared<brainpool_ec>(curve);
}
} // namespace linux_crypto
} // namespace gr