12#include <botan/internal/cmce_encaps.h>
15#include <botan/internal/buffer_slicer.h>
16#include <botan/internal/buffer_stuffer.h>
23 return mat.mul(params, e);
26std::optional<CmceErrorVector> Classic_McEliece_Encryptor::fixed_weight_vector_gen(
28 const auto rand = rng.random_vec((params.sigma1() / 8) * params.tau());
30 const uint16_t mask_m = (uint32_t(1) << params.m()) - 1;
32 a_values.reserve(params.tau());
33 BufferSlicer rand_slicer(rand);
37 for(
size_t j = 0; j < params.tau(); ++j) {
43 const bool d_in_range = d < params.n();
45 if(d_in_range && a_values.size() < params.t()) {
46 a_values.push_back(d);
49 if(a_values.size() < params.t()) {
55 for(
size_t i = 1; i < params.t(); ++i) {
56 for(
size_t j = 0; j < i; ++j) {
57 const bool a_i_j_equal = a_values.at(i) == a_values.at(j);
71 for(
size_t j = 0; j < a_values.size(); ++j) {
72 a_value_byte[j] = 1 << (a_values[j] % 8);
75 for(
size_t i = 0; i < params.n() / 8; ++i) {
76 for(
size_t j = 0; j < a_values.size(); ++j) {
80 e_bytes[i] |= mask.if_set_return(a_value_byte[j]);
88 std::span<uint8_t> out_shared_key,
90 BOTAN_ARG_CHECK(out_encapsulated_key.size() == m_key->params().ciphertext_size(),
91 "Incorrect encapsulated key output length");
92 BOTAN_ARG_CHECK(out_shared_key.size() == m_key->params().hash_out_bytes(),
"Incorrect shared key output length");
94 const auto& params = m_key->params();
102 constexpr size_t MAX_ATTEMPTS = 647;
103 for(
size_t attempt = 0; attempt < MAX_ATTEMPTS; ++attempt) {
104 if(
auto maybe_e = fixed_weight_vector_gen(params, rng)) {
105 return maybe_e.value();
108 throw Internal_Error(
"Cannot created fixed weight vector. Is your RNG broken?");
111 auto hash_func = params.hash_func();
116 const auto big_c_0 = encode(params, e, m_key->matrix());
120 hash_func->update(0x02);
121 hash_func->update(e_bytes);
122 hash_func->final(big_c_stuf.
next(hash_func->output_length()));
127 hash_func->update(0x01);
128 hash_func->update(e_bytes);
129 hash_func->update(out_encapsulated_key);
130 hash_func->final(out_shared_key);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
Helper class to ease in-place marshalling of concatenated fixed-length values.
constexpr std::span< uint8_t > next(size_t bytes)
constexpr bool full() const
static constexpr Mask< T > is_equal(T x, T y)
void raw_kem_encrypt(std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_shared_key, RandomNumberGenerator &rng) override
Representation of the binary Classic McEliece matrix H, with H = (I_mt | T).
constexpr void unpoison_all(const Ts &... ts)
constexpr void unpoison(const T *p, size_t n)
constexpr void poison(const T *p, size_t n)
Strong< secure_bitvector, struct CmceCodeWord_ > CmceCodeWord
Represents C of decapsulation.
bitvector_base< secure_allocator > secure_bitvector
Strong< secure_bitvector, struct CmceErrorVector_ > CmceErrorVector
Represents e of encapsulation.
BOTAN_FORCE_INLINE constexpr T ceil_tobytes(T bits)
constexpr auto load_le(ParamTs &&... params)
std::vector< T, secure_allocator< T > > secure_vector