-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoqs_cpp.h
755 lines (653 loc) · 24.7 KB
/
oqs_cpp.h
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/**
* @file oqs_cpp.h
* @brief Main header file for the liboqs C++ wrapper
*/
#ifndef OQS_CPP_H_
#define OQS_CPP_H_
#include <algorithm>
#include <cstdlib>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include "common.h"
/**
* \namespace oqs
* \brief Main namespace for the liboqs C++ wrapper
*/
namespace oqs {
/**
* \namespace oqs::C
* \brief Namespace containing all of the oqs C functions, so they do not
* pollute the oqs namespace
*/
namespace C {
// everything in liboqs has C linkage
extern "C" {
#include <oqs/oqs.h>
}
} // namespace C
/**
* \class oqs::MechanismNotSupportedError
* \brief Cryptographic scheme not supported
*/
class MechanismNotSupportedError : public std::runtime_error {
public:
/**
* \brief Constructor
* \param alg_name Cryptographic algorithm name
*/
explicit MechanismNotSupportedError(const std::string& alg_name)
: std::runtime_error{"\"" + alg_name + "\"" +
" is not supported by OQS"} {}
}; // class MechanismNotSupportedError
/**
* \class oqs::MechanismNotEnabledError
* \brief Cryptographic scheme not enabled
*/
class MechanismNotEnabledError : public std::runtime_error {
public:
/**
* \brief Constructor
* \param alg_name Cryptographic algorithm name
*/
explicit MechanismNotEnabledError(const std::string& alg_name)
: std::runtime_error{"\"" + alg_name + "\"" +
" is not enabled by OQS"} {}
}; // class MechanismNotEnabledError
/**
* \class oqs::KEMs
* \brief Singleton class, contains details about supported/enabled key exchange
* mechanisms (KEMs)
*/
class KEMs final : public internal::Singleton<const KEMs> {
friend class internal::Singleton<const KEMs>;
/**
* \brief Private default constructor
* \note Use oqs::KEMs::get_instance() to create an instance
*/
KEMs() = default;
public:
/**
* \brief Maximum number of supported KEM algorithms
* \return Maximum number of supported KEM algorithms
*/
static std::size_t max_number_KEMs() {
static std::size_t max_number_KEMs_ = C::OQS_KEM_alg_count();
return max_number_KEMs_;
}
/**
* \brief Checks whether the KEM algorithm \a alg_name is supported
* \param alg_name Cryptographic algorithm name
* \return True if the KEM algorithm is supported, false otherwise
*/
static bool is_KEM_supported(const std::string& alg_name) {
auto supported_KEMs = get_supported_KEMs();
return std::find(supported_KEMs.begin(), supported_KEMs.end(),
alg_name) != supported_KEMs.end();
}
/**
* \brief Checks whether the KEM algorithm \a alg_name is enabled
* \param alg_name Cryptographic algorithm name
* \return True if the KEM algorithm is enabled, false otherwise
*/
static bool is_KEM_enabled(const std::string& alg_name) {
return C::OQS_KEM_alg_is_enabled(alg_name.c_str());
}
/**
* \brief KEM algorithm name
* \param alg_id Cryptographic algorithm numerical id
* \return KEM algorithm name
*/
static std::string get_KEM_name(std::size_t alg_id) {
if (alg_id >= max_number_KEMs())
throw std::out_of_range("Algorithm ID out of range");
return C::OQS_KEM_alg_identifier(alg_id);
}
/**
* \brief Vector of supported KEM algorithms
* \return Vector of supported KEM algorithms
*/
static const std::vector<std::string>& get_supported_KEMs() {
static std::vector<std::string> supported_KEMs;
static bool fnc_already_invoked = false; // function was already invoked
if (!fnc_already_invoked) {
for (std::size_t i = 0; i < max_number_KEMs(); ++i)
supported_KEMs.emplace_back(get_KEM_name(i));
fnc_already_invoked = true;
}
return supported_KEMs;
}
/**
* \brief Vector of enabled KEM algorithms
* \return Vector of enabled KEM algorithms
*/
static const std::vector<std::string>& get_enabled_KEMs() {
static std::vector<std::string> enabled_KEMs;
static bool fnc_already_invoked = false; // function was already invoked
if (!fnc_already_invoked) {
for (auto&& elem : get_supported_KEMs())
if (is_KEM_enabled(elem))
enabled_KEMs.emplace_back(elem);
fnc_already_invoked = true;
}
return enabled_KEMs;
}
}; // class KEMs
/**
* \class oqs::KeyEncapsulation
* \brief Key encapsulation mechanisms
*/
class KeyEncapsulation {
std::shared_ptr<C::OQS_KEM> kem_{nullptr, [](C::OQS_KEM* p) {
C::OQS_KEM_free(p);
}}; ///< liboqs smart pointer to C::OQS_KEM
bytes secret_key_{}; ///< secret key
public:
/**
* \brief KEM algorithm details
*/
struct KeyEncapsulationDetails {
std::string name;
std::string version;
std::size_t claimed_nist_level;
bool is_ind_cca;
std::size_t length_public_key;
std::size_t length_secret_key;
std::size_t length_ciphertext;
std::size_t length_shared_secret;
};
private:
KeyEncapsulationDetails alg_details_{}; ///< KEM algorithm details
public:
/**
* \brief Constructs an instance of oqs::KeyEncapsulation
* \param alg_name Cryptographic algorithm name
* \param secret_key Secret key (optional)
*/
explicit KeyEncapsulation(const std::string& alg_name,
bytes secret_key = {})
: secret_key_{std::move(secret_key)} {
// KEM not enabled
if (!KEMs::is_KEM_enabled(alg_name)) {
// perhaps it's supported
if (KEMs::is_KEM_supported(alg_name))
throw MechanismNotEnabledError(alg_name);
else
throw MechanismNotSupportedError(alg_name);
}
kem_.reset(C::OQS_KEM_new(alg_name.c_str()),
[](C::OQS_KEM* p) { C::OQS_KEM_free(p); });
alg_details_.name = kem_->method_name;
alg_details_.version = kem_->alg_version;
alg_details_.claimed_nist_level = kem_->claimed_nist_level;
alg_details_.is_ind_cca = kem_->ind_cca;
alg_details_.length_public_key = kem_->length_public_key;
alg_details_.length_secret_key = kem_->length_secret_key;
alg_details_.length_ciphertext = kem_->length_ciphertext;
alg_details_.length_shared_secret = kem_->length_shared_secret;
}
/**
* \brief Default copy constructor
*/
KeyEncapsulation(const KeyEncapsulation&) = default;
/**
* \brief Default copy assignment operator
* \return Reference to the current instance
*/
KeyEncapsulation& operator=(const KeyEncapsulation&) = default;
/**
* \brief Move constructor, guarantees that the rvalue secret key is always
* zeroed
* \param rhs oqs::KeyEncapsulation instance
*/
KeyEncapsulation(KeyEncapsulation&& rhs) noexcept
: kem_{std::move(rhs.kem_)}, alg_details_{std::move(rhs.alg_details_)} {
// paranoid move via copy/clean/resize, see
// https://stackoverflow.com/questions/55054187/can-i-resize-a-vector-that-was-moved-from
secret_key_ = rhs.secret_key_; // copy
// clean (zero)
C::OQS_MEM_cleanse(rhs.secret_key_.data(), rhs.secret_key_.size());
rhs.secret_key_.resize(0); // resize
}
/**
* \brief Move assignment operator, guarantees that the rvalue secret key is
* always zeroed
* \param rhs oqs::KeyEncapsulation instance
* \return Reference to the current instance
*/
KeyEncapsulation& operator=(KeyEncapsulation&& rhs) noexcept {
kem_ = std::move(rhs.kem_);
alg_details_ = std::move(rhs.alg_details_);
// paranoid move via copy/clean/resize, see
// https://stackoverflow.com/questions/55054187/can-i-resize-a-vector-that-was-moved-from
secret_key_ = rhs.secret_key_; // copy
// clean (zero)
C::OQS_MEM_cleanse(rhs.secret_key_.data(), rhs.secret_key_.size());
rhs.secret_key_.resize(0); // resize
return *this;
}
/**
* \brief Virtual default destructor
*/
virtual ~KeyEncapsulation() {
if (!secret_key_.empty())
C::OQS_MEM_cleanse(secret_key_.data(), secret_key_.size());
}
/**
* \brief KEM algorithm details, lvalue overload
* \return KEM algorithm details
*/
const KeyEncapsulationDetails& get_details() const& { return alg_details_; }
/**
* \brief KEM algorithm details, rvalue overload
* \return KEM algorithm details
*/
KeyEncapsulationDetails get_details() const&& { return alg_details_; }
/**
* \brief Generate public key/secret key pair
* \return Public key
*/
bytes generate_keypair() {
bytes public_key(alg_details_.length_public_key, 0);
secret_key_ = bytes(alg_details_.length_secret_key, 0);
OQS_STATUS rv_ = C::OQS_KEM_keypair(kem_.get(), public_key.data(),
secret_key_.data());
if (rv_ != OQS_STATUS::OQS_SUCCESS)
throw std::runtime_error("Can not generate keypair");
return public_key;
}
/**
* \brief Generate public key/secret key pair
* \return Public key
*/
bytes generate_keypair_based_on_input(uint8_t *key_input) {
bytes public_key(alg_details_.length_public_key, 0);
secret_key_ = bytes(alg_details_.length_secret_key, 0);
OQS_STATUS rv_ = C::OQS_KEM_keypair_based_on_input(key_input, kem_.get(), public_key.data(),
secret_key_.data());
if (rv_ != OQS_STATUS::OQS_SUCCESS)
throw std::runtime_error("Can not generate keypair");
return public_key;
}
/**
* \brief Export secret key
* \return Secret key
*/
bytes export_secret_key() const { return secret_key_; }
/**
* \brief Encapsulate secret
* \param public_key Public key
* \return Pair consisting of 1) ciphertext, and 2) shared secret
*/
std::pair<bytes, bytes> encap_secret(const bytes& public_key) const {
if (public_key.size() != alg_details_.length_public_key)
throw std::runtime_error("Incorrect public key length");
bytes ciphertext(alg_details_.length_ciphertext, 0);
bytes shared_secret(alg_details_.length_shared_secret, 0);
OQS_STATUS rv_ =
C::OQS_KEM_encaps(kem_.get(), ciphertext.data(),
shared_secret.data(), public_key.data());
if (rv_ != OQS_STATUS::OQS_SUCCESS)
throw std::runtime_error("Can not encapsulate secret");
return std::make_pair(ciphertext, shared_secret);
}
/**
* \brief Decapsulate secret
* \param ciphertext Ciphertext
* \return Shared secret
*/
bytes decap_secret(const bytes& ciphertext) const {
if (ciphertext.size() != alg_details_.length_ciphertext)
throw std::runtime_error("Incorrect ciphertext length");
if (secret_key_.size() != alg_details_.length_secret_key)
throw std::runtime_error(
"Incorrect secret key length, make sure you "
"specify one in the constructor or run "
"oqs::Signature::generate_keypair()");
bytes shared_secret(alg_details_.length_shared_secret, 0);
OQS_STATUS rv_ =
C::OQS_KEM_decaps(kem_.get(), shared_secret.data(),
ciphertext.data(), secret_key_.data());
if (rv_ != OQS_STATUS::OQS_SUCCESS)
throw std::runtime_error("Can not decapsulate secret");
return shared_secret;
}
/**
* \brief std::ostream extraction operator for the KEM algorithm details
* \param os Output stream
* \param rhs Algorithm details instance
* \return Reference to the output stream
*/
friend std::ostream& operator<<(std::ostream& os,
const KeyEncapsulationDetails& rhs) {
os << "Name: " << rhs.name << '\n';
os << "Version: " << rhs.version << '\n';
os << "Claimed NIST level: " << rhs.claimed_nist_level << '\n';
os << "Is IND_CCA: " << rhs.is_ind_cca << '\n';
os << "Length public key (bytes): " << rhs.length_public_key << '\n';
os << "Length secret key (bytes): " << rhs.length_secret_key << '\n';
os << "Length ciphertext (bytes): " << rhs.length_ciphertext << '\n';
os << "Length shared secret (bytes): " << rhs.length_shared_secret;
return os;
}
/**
* \brief std::ostream extraction operator for oqs::KeyEncapsulation
* \param os Output stream
* \param rhs oqs::KeyEncapsulation instance
* \return Reference to the output stream
*/
friend std::ostream& operator<<(std::ostream& os,
const KeyEncapsulation& rhs) {
return os << "Key encapsulation mechanism: " << rhs.alg_details_.name;
}
}; // class KeyEncapsulation
/**
* \class oqs::Sigs
* \brief Singleton class, contains details about supported/enabled signature
* mechanisms
*/
class Sigs final : public internal::Singleton<const Sigs> {
friend class internal::Singleton<const Sigs>;
/**
* \brief Private default constructor
* \note Use oqs::Sigs::get_instance() to create an instance
*/
Sigs() = default;
public:
/**
* \brief Maximum number of supported signature algorithms
* \return Maximum number of supported signature algorithms
*/
static std::size_t max_number_sigs() {
static std::size_t max_number_sigs_ = C::OQS_SIG_alg_count();
return max_number_sigs_;
}
/**
* \brief Checks whether the signature algorithm \a alg_name is supported
* \param alg_name Cryptographic algorithm name
* \return True if the signature algorithm is supported, false otherwise
*/
static bool is_sig_supported(const std::string& alg_name) {
auto supported_sigs = get_supported_sigs();
return std::find(supported_sigs.begin(), supported_sigs.end(),
alg_name) != supported_sigs.end();
}
/**
* \brief Checks whether the signature algorithm \a alg_name is enabled
* \param alg_name Cryptographic algorithm name
* \return True if the signature algorithm is enabled, false otherwise
*/
static bool is_sig_enabled(const std::string& alg_name) {
return C::OQS_SIG_alg_is_enabled(alg_name.c_str());
}
/**
* \brief Signature algorithm name
* \param alg_id Cryptographic algorithm numerical id
* \return Signature algorithm name
*/
static std::string get_sig_name(std::size_t alg_id) {
if (alg_id >= max_number_sigs())
throw std::out_of_range("Algorithm ID out of range");
return C::OQS_SIG_alg_identifier(alg_id);
}
/**
* \brief Vector of supported signature algorithms
* \return Vector of supported signature algorithms
*/
static const std::vector<std::string>& get_supported_sigs() {
static std::vector<std::string> supported_sigs;
static bool fnc_already_invoked = false;
if (!fnc_already_invoked) {
for (std::size_t i = 0; i < max_number_sigs(); ++i)
supported_sigs.emplace_back(get_sig_name(i));
fnc_already_invoked = true;
}
return supported_sigs;
}
/**
* \brief Vector of enabled signature algorithms
* \return Vector of enabled signature algorithms
*/
static const std::vector<std::string>& get_enabled_sigs() {
static std::vector<std::string> enabled_sigs;
static bool fnc_already_invoked = false;
if (!fnc_already_invoked) {
for (auto&& elem : get_supported_sigs())
if (is_sig_enabled(elem))
enabled_sigs.emplace_back(elem);
fnc_already_invoked = true;
}
return enabled_sigs;
}
}; // class Sigs
/**
* \class oqs::Signature
* \brief Signature mechanisms
*/
class Signature {
std::shared_ptr<C::OQS_SIG> sig_{nullptr, [](C::OQS_SIG* p) {
C::OQS_SIG_free(p);
}}; ///< liboqs smart pointer to C::OQS_SIG
bytes secret_key_{}; ///< secret key
public:
/**
* \brief Signature algorithm details
*/
struct SignatureDetails {
std::string name;
std::string version;
std::size_t claimed_nist_level;
bool is_euf_cma;
std::size_t length_public_key;
std::size_t length_secret_key;
std::size_t max_length_signature;
};
private:
SignatureDetails alg_details_{}; ///< Signature algorithm details
public:
/**
* \brief Constructs an instance of oqs::Signature
* \param alg_name Cryptographic algorithm name
* \param secret_key Secret key (optional)
*/
explicit Signature(const std::string& alg_name,
bytes secret_key = {})
: secret_key_{std::move(secret_key)} {
// signature not enabled
if (!Sigs::is_sig_enabled(alg_name)) {
// perhaps it's supported
if (Sigs::is_sig_supported(alg_name))
throw MechanismNotEnabledError(alg_name);
else
throw MechanismNotSupportedError(alg_name);
}
sig_.reset(C::OQS_SIG_new(alg_name.c_str()),
[](C::OQS_SIG* p) { C::OQS_SIG_free(p); });
alg_details_.name = sig_->method_name;
alg_details_.version = sig_->alg_version;
alg_details_.claimed_nist_level = sig_->claimed_nist_level;
alg_details_.is_euf_cma = sig_->euf_cma;
alg_details_.length_public_key = sig_->length_public_key;
alg_details_.length_secret_key = sig_->length_secret_key;
alg_details_.max_length_signature = sig_->length_signature;
}
/**
* \brief Default copy constructor
*/
Signature(const Signature&) = default;
/**
* \brief Default copy assignment operator
* \return Reference to the current instance
*/
Signature& operator=(const Signature&) = default;
/**
* \brief Move constructor, guarantees that the rvalue secret key is always
* zeroed
* \param rhs oqs::Signature instance
*/
Signature(Signature&& rhs) noexcept
: sig_{std::move(rhs.sig_)}, alg_details_{std::move(rhs.alg_details_)} {
// paranoid move via copy/clean/resize, see
// https://stackoverflow.com/questions/55054187/can-i-resize-a-vector-that-was-moved-from
secret_key_ = rhs.secret_key_; // copy
// clean (zero)
C::OQS_MEM_cleanse(rhs.secret_key_.data(), rhs.secret_key_.size());
rhs.secret_key_.resize(0); // resize
}
/**
* \brief Move assignment operator, guarantees that the rvalue secret key is
* always zeroed
* \param rhs oqs::Signature instance
* \return Reference to the current instance
*/
Signature& operator=(Signature&& rhs) noexcept {
sig_ = std::move(rhs.sig_);
alg_details_ = std::move(rhs.alg_details_);
// paranoid move via copy/clean/resize, see
// https://stackoverflow.com/questions/55054187/can-i-resize-a-vector-that-was-moved-from
secret_key_ = rhs.secret_key_; // copy
// clean (zero)
C::OQS_MEM_cleanse(rhs.secret_key_.data(), rhs.secret_key_.size());
rhs.secret_key_.resize(0); // resize
return *this;
}
/**
* \brief Virtual default destructor
*/
virtual ~Signature() {
if (!secret_key_.empty())
C::OQS_MEM_cleanse(secret_key_.data(), secret_key_.size());
}
/**
* \brief Signature algorithm details, lvalue overload
* \return Signature algorithm details
*/
const SignatureDetails& get_details() const& { return alg_details_; }
/**
* \brief Signature algorithm details, rvalue overload
* \return Signature algorithm details
*/
SignatureDetails get_details() const&& { return alg_details_; }
/**
* \brief Generate public key/secret key pair
* \return Public key
*/
bytes generate_keypair() {
bytes public_key(get_details().length_public_key, 0);
secret_key_ = bytes(alg_details_.length_secret_key, 0);
OQS_STATUS rv_ = C::OQS_SIG_keypair(sig_.get(), public_key.data(),
secret_key_.data());
if (rv_ != OQS_STATUS::OQS_SUCCESS)
throw std::runtime_error("Can not generate keypair");
return public_key;
}
/**
* \brief Export secret key
* \return Secret key
*/
bytes export_secret_key() const { return secret_key_; }
/**
* \brief Sign message
* \param message Message
* \return Message signature
*/
bytes sign(const bytes& message) const {
if (secret_key_.size() != alg_details_.length_secret_key)
throw std::runtime_error(
"Incorrect secret key length, make sure you "
"specify one in the constructor or run "
"oqs::Signature::generate_keypair()");
bytes signature(alg_details_.max_length_signature, 0);
std::size_t len_sig;
OQS_STATUS rv_ =
C::OQS_SIG_sign(sig_.get(), signature.data(), &len_sig,
message.data(), message.size(), secret_key_.data());
if (rv_ != OQS_STATUS::OQS_SUCCESS)
throw std::runtime_error("Can not sign message");
signature.resize(len_sig);
return signature;
}
/**
* \brief Verify signature
* \param message Message
* \param signature Signature
* \param public_key Public key
* \return True if the signature is valid, false otherwise
*/
bool verify(const bytes& message, const bytes& signature,
const bytes& public_key) const {
if (public_key.size() != alg_details_.length_public_key)
throw std::runtime_error("Incorrect public key length");
if (signature.size() > alg_details_.max_length_signature)
throw std::runtime_error("Incorrect signature size");
OQS_STATUS rv_ = C::OQS_SIG_verify(sig_.get(), message.data(),
message.size(), signature.data(),
signature.size(), public_key.data());
return rv_ == OQS_STATUS::OQS_SUCCESS;
}
/**
* \brief std::ostream extraction operator for the signature algorithm
* details
* \param os Output stream
* \param rhs Algorithm details instance
* \return Reference to the output stream
*/
friend std::ostream& operator<<(std::ostream& os,
const SignatureDetails& rhs) {
os << "Name: " << rhs.name << '\n';
os << "Version: " << rhs.version << '\n';
os << "Claimed NIST level: " << rhs.claimed_nist_level << '\n';
os << "Is EUF_CMA: " << rhs.is_euf_cma << '\n';
os << "Length public key (bytes): " << rhs.length_public_key << '\n';
os << "Length secret key (bytes): " << rhs.length_secret_key << '\n';
os << "Maximum length signature (bytes): " << rhs.max_length_signature;
return os;
}
/**
* \brief std::ostream extraction operator for oqs::Signature
* \param os Output stream
* \param rhs oqs::Signature instance
* \return Reference to the output stream
*/
friend std::ostream& operator<<(std::ostream& os, const Signature& rhs) {
return os << "Signature mechanism: " << rhs.alg_details_.name;
}
}; // class Signature
namespace internal {
/**
* \class oqs::internal::Init
* \brief liboqs initialization
*/
class Init final : public internal::Singleton<const Init> {
friend class internal::Singleton<const Init>;
std::string oqs_cpp_version(){
return std::string("0.7.2");
}
/**
* \brief Private default constructor
* \note Use oqs::internal::Init::get_instance() to create an instance
*/
Init() {
C::OQS_init();
std::string oqs_ver = oqs_version();
std::string oqs_cpp_ver = oqs_cpp_version();
if (oqs_ver != oqs_cpp_ver) {
std::cerr << "Warning! liboqs version " << oqs_ver
<< " differs from liboqs-python version " << oqs_cpp_ver
<< std::endl;
}
}
};
static const Init& init_ = Init::get_instance(); ///< liboqs initialization
// instantiate the KEMs and Sigs singletons (if ever needed)
static const KEMs& kems_ =
KEMs::get_instance(); ///< instantiates the KEMs singleton
static const Sigs& sigs_ =
Sigs::get_instance(); ///< instantiates the Sigs singleton
} // namespace internal
} // namespace oqs
#endif // OQS_CPP_H_