Botan 3.7.1
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_field_ordering.h>
15#include <botan/internal/cmce_keys_internal.h>
16#include <botan/internal/cmce_matrix.h>
17#include <botan/internal/ct_utils.h>
18#include <botan/internal/pk_ops_impl.h>
19
20#include <algorithm>
21
22namespace Botan {
23
25 std::span<const uint8_t> key_bits) :
26 Classic_McEliece_PublicKey(key_bits, Classic_McEliece_Parameter_Set::from_oid(alg_id.oid())) {}
27
30 auto params = Classic_McEliece_Parameters::create(param_set);
31 BOTAN_ARG_CHECK(key_bits.size() == params.pk_size_bytes(), "Wrong public key length");
32 m_public = std::make_shared<Classic_McEliece_PublicKeyInternal>(
33 params, Classic_McEliece_Matrix(params, {key_bits.begin(), key_bits.end()}));
34}
35
37 m_public = std::make_shared<Classic_McEliece_PublicKeyInternal>(*other.m_public);
38}
39
41 if(this != &other) {
42 m_public = std::make_shared<Classic_McEliece_PublicKeyInternal>(*other.m_public);
43 }
44 return *this;
45}
46
50
52 return m_public->params().object_identifier();
53}
54
56 // The key length is the dimension k of the goppa code (i.e. the code has 2^k codewords)
57 return m_public->params().pk_no_cols();
58}
59
61 return m_public->params().estimated_strength();
62}
63
64std::vector<uint8_t> Classic_McEliece_PublicKey::public_key_bits() const {
65 return raw_public_key_bits();
66}
67
69 return m_public->matrix().bytes();
70}
71
73 return true;
74}
75
77 return std::make_unique<Classic_McEliece_PrivateKey>(rng, m_public->params().parameter_set());
78}
79
80std::unique_ptr<PK_Ops::KEM_Encryption> Classic_McEliece_PublicKey::create_kem_encryption_op(
81 std::string_view params, std::string_view provider) const {
82 if(provider.empty() || provider == "base") {
83 return std::make_unique<Classic_McEliece_Encryptor>(this->m_public, params);
84 }
85 throw Provider_Not_Found(algo_name(), provider);
86}
87
99
102 auto scope = CT::scoped_poison(sk);
103 auto params = Classic_McEliece_Parameters::create(param_set);
104 auto sk_internal = Classic_McEliece_PrivateKeyInternal::from_bytes(params, sk);
105 m_private = std::make_shared<Classic_McEliece_PrivateKeyInternal>(std::move(sk_internal));
106 // This creates and loads the public key, which is very large. Potentially, we could only load
107 // it on demand (since one may use the private key only for decapsulation without needing the public key).
108 // TODO: consider building a load-on-demand mechanism for the public key
110 CT::unpoison_all(*m_public, *m_private);
111}
112
114 std::span<const uint8_t> key_bits) :
115 Classic_McEliece_PrivateKey(key_bits, Classic_McEliece_Parameter_Set::from_oid(alg_id.oid())) {}
116
117std::unique_ptr<Public_Key> Classic_McEliece_PrivateKey::public_key() const {
118 return std::make_unique<Classic_McEliece_PublicKey>(*this);
119}
120
124
126 return m_private->serialize();
127}
128
130 return m_private->check_key();
131}
132
133std::unique_ptr<PK_Ops::KEM_Decryption> Classic_McEliece_PrivateKey::create_kem_decryption_op(
134 RandomNumberGenerator& rng, std::string_view params, std::string_view provider) const {
135 BOTAN_UNUSED(rng);
136 if(provider.empty() || provider == "base") {
137 return std::make_unique<Classic_McEliece_Decryptor>(this->m_private, params);
138 }
139 throw Provider_Not_Found(algo_name(), provider);
140}
141
142} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:118
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:86
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
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:88
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:133
secure_vector< uint8_t > private_key_bits() const override
Definition cmce.cpp:121
bool check_key(RandomNumberGenerator &, bool) const override
Definition cmce.cpp:129
std::unique_ptr< Public_Key > public_key() const override
Definition cmce.cpp:117
secure_vector< uint8_t > raw_private_key_bits() const override
Definition cmce.cpp:125
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:55
AlgorithmIdentifier algorithm_identifier() const override
Definition cmce.cpp:47
bool check_key(RandomNumberGenerator &, bool) const override
Definition cmce.cpp:72
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition cmce.cpp:76
std::shared_ptr< Classic_McEliece_PublicKeyInternal > m_public
Definition cmce.h:92
std::unique_ptr< PK_Ops::KEM_Encryption > create_kem_encryption_op(std::string_view params, std::string_view provider) const override
Definition cmce.cpp:80
Classic_McEliece_PublicKey & operator=(const Classic_McEliece_PublicKey &other)
Definition cmce.cpp:40
size_t estimated_strength() const override
Definition cmce.cpp:60
std::string algo_name() const override
Definition cmce.h:62
OID object_identifier() const override
Definition cmce.cpp:51
std::vector< uint8_t > public_key_bits() const override
Definition cmce.cpp:64
std::vector< uint8_t > raw_public_key_bits() const override
Definition cmce.cpp:68
void random_vec(std::span< uint8_t > v)
Definition rng.h:180
constexpr void unpoison_all(Ts &&... ts)
Definition ct_utils.h:201
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:216
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:53
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
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.