13#include <botan/mceliece.h>
15#include <botan/ber_dec.h>
16#include <botan/der_enc.h>
18#include <botan/internal/bit_ops.h>
19#include <botan/internal/code_based_util.h>
20#include <botan/internal/loadstor.h>
21#include <botan/internal/mce_internal.h>
22#include <botan/internal/pk_ops_impl.h>
23#include <botan/internal/polyn_gf2m.h>
24#include <botan/internal/stl_util.h>
30McEliece_PrivateKey& McEliece_PrivateKey::operator=(const McEliece_PrivateKey&) = default;
31McEliece_PrivateKey& McEliece_PrivateKey::operator=(McEliece_PrivateKey&&) noexcept = default;
32McEliece_PrivateKey::~McEliece_PrivateKey() = default;
35 const std::vector<uint32_t>& parity_check_matrix_coeffs,
36 const std::vector<
polyn_gf2m>& square_root_matrix,
37 const std::vector<
gf2m>& inverse_support,
38 const std::vector<uint8_t>& public_matrix) :
41 m_sqrtmod(square_root_matrix),
42 m_Linv(inverse_support),
43 m_coeffs(parity_check_matrix_coeffs),
44 m_codimension(
static_cast<size_t>(
ceil_log2(inverse_support.size())) * goppa_polyn.get_degree()),
45 m_dimension(inverse_support.size() - m_codimension) {}
48 uint32_t ext_deg =
ceil_log2(code_length);
65 rng.
randomize(plaintext.data(), plaintext.size());
68 if(uint32_t used = bits % 8) {
69 const uint8_t mask = (1 << used) - 1;
70 plaintext[plaintext.size() - 1] &= mask;
85 std::vector<uint8_t> output;
130 for(
size_t i = 0; i < m_sqrtmod.size(); i++) {
136 for(uint16_t Linv : m_Linv) {
142 for(uint32_t coef : m_coeffs) {
164 if(errors != errors_out || plaintext != plaintext_out) {
183 if(t == 0 || n == 0) {
190 m_codimension = (ext_deg * t);
191 m_dimension = (n - m_codimension);
193 auto sp_field = std::make_shared<GF2m_Field>(ext_deg);
195 if(m_g[0].get_degree() !=
static_cast<int>(t)) {
196 throw Decoding_Error(
"degree of decoded Goppa polynomial is incorrect");
199 for(uint32_t i = 0; i < t / 2; i++) {
202 while(sqrt_enc.size() < (t * 2)) {
204 sqrt_enc.push_back(0);
205 sqrt_enc.push_back(0);
207 if(sqrt_enc.size() != t * 2) {
208 throw Decoding_Error(
"length of square root polynomial entry is too large");
210 m_sqrtmod.push_back(
polyn_gf2m(sqrt_enc, sp_field));
214 if(enc_support.size() % 2) {
217 if(enc_support.size() / 2 != n) {
218 throw Decoding_Error(
"encoded support has length different from code length");
220 for(uint32_t i = 0; i < n * 2; i += 2) {
221 gf2m el = (enc_support[i] << 8) | enc_support[i + 1];
222 m_Linv.push_back(el);
226 if(enc_H.size() % 4) {
227 throw Decoding_Error(
"encoded parity check matrix has length which is not a multiple of four");
230 throw Decoding_Error(
"encoded parity check matrix has wrong length");
233 for(uint32_t i = 0; i < enc_H.size(); i += 4) {
234 uint32_t coeff = (enc_H[i] << 24) | (enc_H[i + 1] << 16) | (enc_H[i + 2] << 8) | enc_H[i + 3];
235 m_coeffs.push_back(coeff);
243 if(m_g != other.m_g) {
247 if(m_sqrtmod != other.m_sqrtmod) {
250 if(m_Linv != other.m_Linv) {
253 if(m_coeffs != other.m_coeffs) {
257 if(m_codimension != other.m_codimension || m_dimension != other.m_dimension) {
286 KEM_Encryption_with_KDF(kdf), m_key(key) {}
289 size_t raw_kem_shared_key_length()
const override {
290 const size_t err_sz = (m_key.get_code_length() + 7) / 8;
291 const size_t ptext_sz = (m_key.get_message_word_bit_length() + 7) / 8;
292 return ptext_sz + err_sz;
295 size_t encapsulated_key_length()
const override {
return (m_key.get_code_length() + 7) / 8; }
297 void raw_kem_encrypt(std::span<uint8_t> out_encapsulated_key,
298 std::span<uint8_t> raw_shared_key,
299 RandomNumberGenerator& rng)
override {
307 std::copy(ciphertext.begin(), ciphertext.end(), out_encapsulated_key.begin());
310 BufferStuffer bs(raw_shared_key);
311 bs.append(plaintext);
312 bs.append(error_mask);
315 const McEliece_PublicKey& m_key;
318class MCE_KEM_Decryptor
final :
public PK_Ops::KEM_Decryption_with_KDF {
320 MCE_KEM_Decryptor(
const McEliece_PrivateKey& key, std::string_view kdf) :
321 KEM_Decryption_with_KDF(kdf), m_key(key) {}
324 size_t raw_kem_shared_key_length()
const override {
325 const size_t err_sz = (m_key.get_code_length() + 7) / 8;
326 const size_t ptext_sz = (m_key.get_message_word_bit_length() + 7) / 8;
327 return ptext_sz + err_sz;
330 size_t encapsulated_key_length()
const override {
return (m_key.get_code_length() + 7) / 8; }
332 void raw_kem_decrypt(std::span<uint8_t> out_shared_key, std::span<const uint8_t> encapsulated_key)
override {
334 mceliece_decrypt(plaintext, error_mask, encapsulated_key.data(), encapsulated_key.size(), m_key);
338 BufferStuffer bs(out_shared_key);
339 bs.append(plaintext);
340 bs.append(error_mask);
343 const McEliece_PrivateKey& m_key;
353 std::string_view provider)
const {
354 if(provider ==
"base" || provider.empty()) {
355 return std::make_unique<MCE_KEM_Encryptor>(*
this, params);
361 std::string_view params,
362 std::string_view provider)
const {
363 if(provider ==
"base" || provider.empty()) {
364 return std::make_unique<MCE_KEM_Decryptor>(*
this, params);
#define BOTAN_ASSERT_NOMSG(expr)
virtual OID object_identifier() const
BER_Decoder & decode(bool &out)
BER_Decoder start_sequence()
secure_vector< uint8_t > get_contents()
DER_Encoder & start_sequence()
DER_Encoder & encode(bool b)
secure_vector< uint8_t > private_key_bits() const override
McEliece_PrivateKey(RandomNumberGenerator &rng, size_t code_length, size_t t)
std::unique_ptr< Public_Key > public_key() const override
std::unique_ptr< PK_Ops::KEM_Decryption > create_kem_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
const polyn_gf2m & get_goppa_polyn() const
bool operator==(const McEliece_PrivateKey &other) const
bool check_key(RandomNumberGenerator &rng, bool strong) const override
secure_vector< uint8_t > random_plaintext_element(RandomNumberGenerator &rng) const
size_t get_message_word_bit_length() const
std::vector< uint8_t > raw_public_key_bits() const override
std::unique_ptr< PK_Ops::KEM_Encryption > create_kem_encryption_op(std::string_view params, std::string_view provider) const override
std::string algo_name() const override
std::vector< uint8_t > public_key_bits() const override
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
std::vector< uint8_t > m_public_matrix
const std::vector< uint8_t > & get_public_matrix() const
size_t estimated_strength() const override
size_t get_code_length() const
bool operator==(const McEliece_PublicKey &other) const
AlgorithmIdentifier algorithm_identifier() const override
size_t key_length() const override
void randomize(std::span< uint8_t > output)
int(* final)(unsigned char *, CTX *)
constexpr uint8_t get_byte(T input)
void mceliece_decrypt(secure_vector< uint8_t > &plaintext_out, secure_vector< uint8_t > &error_mask_out, const secure_vector< uint8_t > &ciphertext, const McEliece_PrivateKey &key)
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)
McEliece_PrivateKey generate_mceliece_key(RandomNumberGenerator &rng, size_t ext_deg, size_t code_length, size_t t)
size_t mceliece_work_factor(size_t n, size_t t)
std::vector< T, secure_allocator< T > > secure_vector
size_t bit_size_to_32bit_size(size_t bit_size)
constexpr uint8_t ceil_log2(T x)