Botan 3.13.0
Crypto and TLS for C&
cmce.cpp
Go to the documentation of this file.
1/*
2 * Classic McEliece Key Generation
3 * (C) 2023 Jack Lloyd
4 * 2023,2024 Fabian Albert, Amos Treiber - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 **/
8
9#include <botan/cmce.h>
10#include <botan/pk_ops.h>
11#include <botan/rng.h>
12#include <botan/internal/cmce_decaps.h>
13#include <botan/internal/cmce_encaps.h>
14#include <botan/internal/cmce_keys_internal.h>
15#include <botan/internal/cmce_matrix.h>
16#include <botan/internal/ct_utils.h>
17
18namespace Botan {
19
21 std::span<const uint8_t> key_bits) :
22 Classic_McEliece_PublicKey(key_bits, Classic_McEliece_Parameter_Set::from_oid(alg_id.oid())) {
23 // The parameter set is identified by the OID; no parameters are defined.
24 if(!alg_id.parameters_are_empty()) {
25 throw Decoding_Error("Unexpected parameters for Classic McEliece public key");
26 }
27}
28
31 auto params = Classic_McEliece_Parameters::create(param_set);
32 BOTAN_ARG_CHECK(key_bits.size() == params.pk_size_bytes(), "Wrong public key length");
33 m_public = std::make_shared<Classic_McEliece_PublicKeyInternal>(
34 params, Classic_McEliece_Matrix(params, {key_bits.begin(), key_bits.end()}));
35}
36
38 m_public = std::make_shared<Classic_McEliece_PublicKeyInternal>(*other.m_public);
39}
40
42 if(this != &other) {
43 m_public = std::make_shared<Classic_McEliece_PublicKeyInternal>(*other.m_public);
44 }
45 return *this;
46}
47
51
53 return m_public->params().object_identifier();
54}
55
57 // The key length is the dimension k of the goppa code (i.e. the code has 2^k codewords)
58 return m_public->params().pk_no_cols();
59}
60
62 return m_public->params().estimated_strength();
63}
64
65std::vector<uint8_t> Classic_McEliece_PublicKey::public_key_bits() const {
66 return raw_public_key_bits();
67}
68
70 return m_public->matrix().bytes();
71}
72
73bool Classic_McEliece_PublicKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const {
74 return true;
75}
76
78 return std::make_unique<Classic_McEliece_PrivateKey>(rng, m_public->params().parameter_set());
79}
80
81std::unique_ptr<PK_Ops::KEM_Encryption> Classic_McEliece_PublicKey::create_kem_encryption_op(
82 std::string_view params, std::string_view provider) const {
83 if(provider.empty() || provider == "base") {
84 return std::make_unique<Classic_McEliece_Encryptor>(this->m_public, params);
85 }
86 throw Provider_Not_Found(algo_name(), provider);
87}
88
100
103 auto scope = CT::scoped_poison(sk);
104 auto params = Classic_McEliece_Parameters::create(param_set);
105 auto sk_internal = Classic_McEliece_PrivateKeyInternal::from_bytes(params, sk);
106 m_private = std::make_shared<Classic_McEliece_PrivateKeyInternal>(std::move(sk_internal));
107 // This creates and loads the public key, which is very large. Potentially, we could only load
108 // it on demand (since one may use the private key only for decapsulation without needing the public key).
109 // TODO: consider building a load-on-demand mechanism for the public key
111 CT::unpoison_all(*m_public, *m_private);
112}
113
115 std::span<const uint8_t> key_bits) :
116 Classic_McEliece_PrivateKey(key_bits, Classic_McEliece_Parameter_Set::from_oid(alg_id.oid())) {
117 // The parameter set is identified by the OID; no parameters are defined.
118 if(!alg_id.parameters_are_empty()) {
119 throw Decoding_Error("Unexpected parameters for Classic McEliece private key");
120 }
121}
122
123std::unique_ptr<Public_Key> Classic_McEliece_PrivateKey::public_key() const {
124 return std::make_unique<Classic_McEliece_PublicKey>(*this);
125}
126
130
132 return m_private->serialize();
133}
134
136 return m_private->check_key();
137}
138
139std::unique_ptr<PK_Ops::KEM_Decryption> Classic_McEliece_PrivateKey::create_kem_decryption_op(
140 RandomNumberGenerator& rng, std::string_view params, std::string_view provider) const {
141 BOTAN_UNUSED(rng);
142 if(provider.empty() || provider == "base") {
143 return std::make_unique<Classic_McEliece_Decryptor>(this->m_private, params);
144 }
145 throw Provider_Not_Found(algo_name(), provider);
146}
147
148} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:144
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:114
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
bool parameters_are_empty() const
Definition asn1_obj.h:715
Representation of the binary Classic McEliece matrix H, with H = (I_mt | T).
Definition cmce_matrix.h:26
static Classic_McEliece_Parameters create(Classic_McEliece_Parameter_Set set)
Create Classic McEliece parameters from a parameter set.
static Classic_McEliece_PrivateKeyInternal from_bytes(const Classic_McEliece_Parameters &params, std::span< const uint8_t > sk_bytes)
Parses a Classic McEliece private key from a byte sequence.
Classic_McEliece_PrivateKey(RandomNumberGenerator &rng, Classic_McEliece_Parameter_Set param_set)
Create a new Classic McEliece private key for a specified parameter set.
Definition cmce.cpp:89
std::unique_ptr< PK_Ops::KEM_Decryption > create_kem_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition cmce.cpp:139
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition cmce.cpp:135
secure_vector< uint8_t > private_key_bits() const override
Definition cmce.cpp:127
std::unique_ptr< Public_Key > public_key() const override
Definition cmce.cpp:123
secure_vector< uint8_t > raw_private_key_bits() const override
Definition cmce.cpp:131
static std::shared_ptr< Classic_McEliece_PublicKeyInternal > create_from_private_key(const Classic_McEliece_PrivateKeyInternal &sk)
Create a Classic McEliece public key from a private key.
size_t key_length() const override
Definition cmce.cpp:56
AlgorithmIdentifier algorithm_identifier() const override
Definition cmce.cpp:48
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition cmce.cpp:77
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition cmce.cpp:73
std::unique_ptr< PK_Ops::KEM_Encryption > create_kem_encryption_op(std::string_view params, std::string_view provider) const override
Definition cmce.cpp:81
Classic_McEliece_PublicKey & operator=(const Classic_McEliece_PublicKey &other)
Definition cmce.cpp:41
Classic_McEliece_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Load a Classic McEliece public key from bytes.
Definition cmce.cpp:20
std::shared_ptr< const Classic_McEliece_PublicKeyInternal > m_public
Definition cmce.h:91
size_t estimated_strength() const override
Definition cmce.cpp:61
std::string algo_name() const override
Definition cmce.h:62
OID object_identifier() const override
Definition cmce.cpp:52
std::vector< uint8_t > public_key_bits() const override
Definition cmce.cpp:65
std::vector< uint8_t > raw_public_key_bits() const override
Definition cmce.cpp:69
void random_vec(std::span< uint8_t > v)
Definition rng.h:244
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:222
constexpr void unpoison_all(const Ts &... ts)
Definition ct_utils.h:207
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:56
Strong< secure_vector< uint8_t >, struct CmceInitialSeed_ > CmceInitialSeed
Represents initial delta of keygen.
Definition cmce_types.h:31
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
static Classic_McEliece_KeyPair_Internal generate(const Classic_McEliece_Parameters &params, StrongSpan< const CmceInitialSeed > seed)
Generate a Classic McEliece key pair using the algorithm described in Classic McEliece ISO Section 8....
std::pair< std::shared_ptr< Classic_McEliece_PrivateKeyInternal >, std::shared_ptr< Classic_McEliece_PublicKeyInternal > > decompose_to_pair() &&
Decompose the key pair into a pair of shared pointers to the private and public key.