12#include <botan/internal/mce_internal.h>
14#include <botan/mceliece.h>
15#include <botan/mem_ops.h>
16#include <botan/internal/bit_ops.h>
17#include <botan/internal/code_based_util.h>
29 const size_t final_bits = dimension % 8;
33 copy_mem(&x[0], a.data(), dim_bytes);
36 copy_mem(&x[0], a.data(), (dimension / 8));
37 size_t l = dimension / 8;
38 x[l] =
static_cast<uint8_t
>(a[l] & ((1 << final_bits) - 1));
40 for(
size_t k = 0; k < codimension / 8; ++k) {
41 x[l] ^=
static_cast<uint8_t
>(
b[k] << final_bits);
43 x[l] =
static_cast<uint8_t
>(
b[k] >> (8 - final_bits));
45 x[l] ^=
static_cast<uint8_t
>(
b[codimension / 8] << final_bits);
52 const std::vector<uint8_t>& public_matrix,
55 const size_t ext_deg =
ceil_log2(code_length);
56 const size_t codimension = ext_deg * t;
57 const size_t dimension = code_length - codimension;
60 const uint8_t* pt = public_matrix.data();
62 for(
size_t i = 0; i < dimension / 8; ++i) {
63 for(
size_t j = 0; j < 8; ++j) {
64 if(cleartext[i] & (1 << j)) {
65 xor_buf(cR.data(), pt, cR.size());
71 for(
size_t i = 0; i < dimension % 8; ++i) {
72 if(cleartext[dimension / 8] & (1 << i)) {
73 xor_buf(cR.data(), pt, cR.size());
79 ciphertext.resize((code_length + 7) / 8);
83secure_vector<uint8_t> create_random_error_vector(
size_t code_length,
size_t error_weight, RandomNumberGenerator& rng) {
88 while(bits_set < error_weight) {
91 const size_t byte_pos = x / 8;
92 const size_t bit_pos = x % 8;
94 const uint8_t mask = (1 << bit_pos);
96 if(result[byte_pos] & mask) {
100 result[byte_pos] |= mask;
114 const uint16_t code_length =
static_cast<uint16_t
>(key.
get_code_length());
121 ciphertext ^= error_mask;
123 ciphertext_out.swap(ciphertext);
124 error_mask_out.swap(error_mask);
const std::vector< uint8_t > & get_public_matrix() const
size_t get_code_length() const
void mceliece_encrypt(secure_vector< uint8_t > &ciphertext_out, secure_vector< uint8_t > &error_mask_out, const secure_vector< uint8_t > &plaintext, const McEliece_PublicKey &key, RandomNumberGenerator &rng)
gf2m random_code_element(uint16_t code_length, RandomNumberGenerator &rng)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
std::vector< T, secure_allocator< T > > secure_vector
size_t bit_size_to_32bit_size(size_t bit_size)
size_t bit_size_to_byte_size(size_t bit_size)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr uint8_t ceil_log2(T x)