Botan 3.14.1
Crypto and TLS for C&
Botan::Classic_McEliece_Encryptor Class Referencefinal

#include <cmce_encaps.h>

Inheritance diagram for Botan::Classic_McEliece_Encryptor:
Botan::PK_Ops::KEM_Encryption_with_KDF Botan::PK_Ops::KEM_Encryption

Public Member Functions

 Classic_McEliece_Encryptor (std::shared_ptr< const Classic_McEliece_PublicKeyInternal > key, const PK_KEM_Options_Reader &options)
size_t encapsulated_key_length () const override
void kem_encrypt (std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_shared_key, RandomNumberGenerator &rng, size_t desired_shared_key_len, std::span< const uint8_t > salt) final
void raw_kem_encrypt (std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_shared_key, RandomNumberGenerator &rng) override
size_t raw_kem_shared_key_length () const override
size_t shared_key_length (size_t desired_shared_key_len) const final

Detailed Description

Classic McEliece Encapsulation Operation

Definition at line 26 of file cmce_encaps.h.

Constructor & Destructor Documentation

◆ Classic_McEliece_Encryptor()

Botan::Classic_McEliece_Encryptor::Classic_McEliece_Encryptor ( std::shared_ptr< const Classic_McEliece_PublicKeyInternal > key,
const PK_KEM_Options_Reader & options )
inline

Definition at line 29 of file cmce_encaps.h.

30 :
31 KEM_Encryption_with_KDF(options, PK_Ops::KemSharedKeyQuality::IsUniform), m_key(std::move(key)) {}
KEM_Encryption_with_KDF(const PK_KEM_Options_Reader &options, KemSharedKeyQuality raw_key)
Definition pk_ops.cpp:259

References Botan::PK_Ops::KEM_Encryption_with_KDF::KEM_Encryption_with_KDF().

Member Function Documentation

◆ encapsulated_key_length()

size_t Botan::Classic_McEliece_Encryptor::encapsulated_key_length ( ) const
inlineoverridevirtual

Implements Botan::PK_Ops::KEM_Encryption.

Definition at line 35 of file cmce_encaps.h.

35{ return m_key->params().ciphertext_size(); }

◆ kem_encrypt()

void Botan::PK_Ops::KEM_Encryption_with_KDF::kem_encrypt ( std::span< uint8_t > out_encapsulated_key,
std::span< uint8_t > out_shared_key,
RandomNumberGenerator & rng,
size_t desired_shared_key_len,
std::span< const uint8_t > salt )
finalvirtualinherited

Implements Botan::PK_Ops::KEM_Encryption.

Definition at line 219 of file pk_ops.cpp.

223 {
224 BOTAN_ARG_CHECK(salt.empty() || m_kdf, "PK_KEM_Encryptor::encrypt requires a KDF to use a salt");
225 BOTAN_ASSERT_NOMSG(out_encapsulated_key.size() == encapsulated_key_length());
226
227 if(m_kdf) {
229 out_shared_key.size(), desired_shared_key_len, "KDF output length and shared key length match");
230
232 this->raw_kem_encrypt(out_encapsulated_key, raw_shared, rng);
233 m_kdf->derive_key(out_shared_key, raw_shared, salt, {});
234 } else {
235 BOTAN_ASSERT_EQUAL(out_shared_key.size(), raw_kem_shared_key_length(), "Shared key has raw KEM output length");
236 this->raw_kem_encrypt(out_encapsulated_key, out_shared_key, rng);
237 }
238}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition assert.h:95
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
virtual size_t raw_kem_shared_key_length() const =0
virtual void raw_kem_encrypt(std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_raw_shared_key, RandomNumberGenerator &rng)=0
virtual size_t encapsulated_key_length() const =0
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128

References BOTAN_ARG_CHECK, BOTAN_ASSERT_EQUAL, BOTAN_ASSERT_NOMSG, Botan::PK_Ops::KEM_Encryption::encapsulated_key_length(), raw_kem_encrypt(), and raw_kem_shared_key_length().

◆ raw_kem_encrypt()

void Botan::Classic_McEliece_Encryptor::raw_kem_encrypt ( std::span< uint8_t > out_encapsulated_key,
std::span< uint8_t > out_shared_key,
RandomNumberGenerator & rng )
overridevirtual

Implements Botan::PK_Ops::KEM_Encryption_with_KDF.

Definition at line 88 of file cmce_encaps.cpp.

90 {
91 BOTAN_ARG_CHECK(out_encapsulated_key.size() == m_key->params().ciphertext_size(),
92 "Incorrect encapsulated key output length");
93 BOTAN_ARG_CHECK(out_shared_key.size() == m_key->params().hash_out_bytes(), "Incorrect shared key output length");
94
95 const auto& params = m_key->params();
96
97 // Call fixed_weight until it is successful to
98 // create a random error vector e of weight tau
99 const CmceErrorVector e = [&] {
100 // Emergency abort in case unexpected logical error to prevent endless loops
101 // Success probability: >24% per attempt (25% that elements are distinct * 96% enough elements are in range)
102 // => 647 attempts for 2^(-256) fail probability
103 constexpr size_t MAX_ATTEMPTS = 647;
104 for(size_t attempt = 0; attempt < MAX_ATTEMPTS; ++attempt) {
105 if(auto maybe_e = fixed_weight_vector_gen(params, rng)) {
106 return maybe_e.value();
107 }
108 }
109 throw Internal_Error("Cannot created fixed weight vector. Is your RNG broken?");
110 }();
111
112 auto hash_func = params.hash_func();
113
114 BufferStuffer big_c_stuf(out_encapsulated_key);
115 const auto e_bytes = e.get().to_bytes();
116 // Compute and store ciphertext C/C_0 from spec
117 const auto big_c_0 = encode(params, e, m_key->matrix());
118 big_c_0.to_bytes(big_c_stuf.next(ceil_tobytes(big_c_0.size())));
119 if(params.is_pc()) {
120 // Compute and store ciphertext C_1 from spec
121 hash_func->update(0x02);
122 hash_func->update(e_bytes);
123 hash_func->final(big_c_stuf.next(hash_func->output_length()));
124 }
125 BOTAN_ASSERT_NOMSG(big_c_stuf.full());
126
127 // Compute K = Hash(1,e,C) from spec
128 hash_func->update(0x01);
129 hash_func->update(e_bytes);
130 hash_func->update(out_encapsulated_key);
131 hash_func->final(out_shared_key);
132 CT::unpoison_all(out_encapsulated_key, out_shared_key);
133}
constexpr void unpoison_all(const Ts &... ts)
Definition ct_utils.h:207
Strong< secure_bitvector, struct CmceErrorVector_ > CmceErrorVector
Represents e of encapsulation.
Definition cmce_types.h:49
BOTAN_FORCE_INLINE constexpr T ceil_tobytes(T bits)
Definition bit_ops.h:176

References BOTAN_ARG_CHECK, BOTAN_ASSERT_NOMSG, Botan::ceil_tobytes(), Botan::BufferStuffer::full(), Botan::detail::Strong_Base< T >::get(), Botan::BufferStuffer::next(), Botan::bitvector_base< AllocatorT >::to_bytes(), and Botan::CT::unpoison_all().

◆ raw_kem_shared_key_length()

size_t Botan::Classic_McEliece_Encryptor::raw_kem_shared_key_length ( ) const
inlineoverridevirtual

Implements Botan::PK_Ops::KEM_Encryption_with_KDF.

Definition at line 33 of file cmce_encaps.h.

33{ return m_key->params().hash_out_bytes(); }

◆ shared_key_length()

size_t Botan::PK_Ops::KEM_Encryption_with_KDF::shared_key_length ( size_t desired_shared_key_len) const
finalvirtualinherited

Implements Botan::PK_Ops::KEM_Encryption.

Definition at line 211 of file pk_ops.cpp.

211 {
212 if(m_kdf) {
213 return desired_shared_key_len;
214 } else {
215 return this->raw_kem_shared_key_length();
216 }
217}

References raw_kem_shared_key_length().


The documentation for this class was generated from the following files: