Botan 3.7.1
Crypto and TLS for C&
cmce_encaps.cpp
Go to the documentation of this file.
1/*
2 * Classic McEliece Encapsulation
3 * Based on the public domain reference implementation by the designers
4 * (https://classic.mceliece.org/impl.html - released in Oct 2022 for NISTPQC-R4)
5 *
6 * (C) 2023 Jack Lloyd
7 * 2023,2024 Fabian Albert, Amos Treiber - Rohde & Schwarz Cybersecurity
8 *
9 * Botan is released under the Simplified BSD License (see license.txt)
10 **/
11#include <botan/internal/cmce_encaps.h>
12
13#include <botan/rng.h>
14
15namespace Botan {
16
17CmceCodeWord Classic_McEliece_Encryptor::encode(const Classic_McEliece_Parameters& params,
18 const CmceErrorVector& e,
19 const Classic_McEliece_Matrix& mat) const {
20 return mat.mul(params, e);
21}
22
23std::optional<CmceErrorVector> Classic_McEliece_Encryptor::fixed_weight_vector_gen(
24 const Classic_McEliece_Parameters& params, RandomNumberGenerator& rng) const {
25 const auto rand = rng.random_vec((params.sigma1() / 8) * params.tau());
26 CT::poison(rand);
27 uint16_t mask_m = (uint32_t(1) << params.m()) - 1; // Only take m least significant bits
29 a_values.reserve(params.tau());
30 BufferSlicer rand_slicer(rand);
31
32 // Steps 2 & 3: Create d_j from uniform random bits. The first t d_j entries
33 // in range {0,...,n-1} are defined as a_0,...,a_(t-1). ...
34 for(size_t j = 0; j < params.tau(); ++j) {
35 auto d = load_le<uint16_t>(rand_slicer.take(params.sigma1() / 8).data(), 0);
36 // This is not CT, but neither is the reference implementation here.
37 // This side channel only leaks which random elements are selected and which are dropped,
38 // but no information about their content is leaked.
39 d &= mask_m;
40 bool d_in_range = d < params.n();
41 CT::unpoison(d_in_range);
42 if(d_in_range && a_values.size() < params.t()) {
43 a_values.push_back(d);
44 }
45 }
46 if(a_values.size() < params.t()) {
47 // Step 3: ... If fewer than t of such elements exist restart
48 return std::nullopt;
49 }
50
51 // Step 4: Restart if not all a_i are distinct
52 for(size_t i = 1; i < params.t(); ++i) {
53 for(size_t j = 0; j < i; ++j) {
54 bool a_i_j_equal = a_values.at(i) == a_values.at(j);
55 CT::unpoison(a_i_j_equal);
56 if(a_i_j_equal) {
57 return std::nullopt;
58 }
59 }
60 }
61
62 secure_vector<uint8_t> a_value_byte(params.t());
63 secure_vector<uint8_t> e_bytes(ceil_tobytes(params.n()));
64
65 // Step 5: Set all bits of e at the positions of a_values
66 // Prepare the associated byte in e_bytes that is represented by each bit index in a_values
67 // if we e is represented as a byte vector
68 for(size_t j = 0; j < a_values.size(); ++j) {
69 a_value_byte[j] = 1 << (a_values[j] % 8);
70 }
71
72 for(size_t i = 0; i < params.n() / 8; ++i) {
73 for(size_t j = 0; j < a_values.size(); ++j) {
74 // If the current byte is the one that is represented by the current bit index in a_values
75 // then set the bit in e_bytes (in-byte position prepared above)
76 auto mask = CT::Mask<uint16_t>::is_equal(static_cast<uint16_t>(i), static_cast<uint16_t>(a_values[j] >> 3));
77 e_bytes[i] |= mask.if_set_return(a_value_byte[j]);
78 }
79 }
80
81 return CmceErrorVector(secure_bitvector(e_bytes, params.n()));
82}
83
84void Classic_McEliece_Encryptor::raw_kem_encrypt(std::span<uint8_t> out_encapsulated_key,
85 std::span<uint8_t> out_shared_key,
87 BOTAN_ARG_CHECK(out_encapsulated_key.size() == m_key->params().ciphertext_size(),
88 "Incorrect encapsulated key output length");
89 BOTAN_ARG_CHECK(out_shared_key.size() == m_key->params().hash_out_bytes(), "Incorrect shared key output length");
90
91 const auto& params = m_key->params();
92
93 // Call fixed_weight until it is successful to
94 // create a random error vector e of weight tau
95 const CmceErrorVector e = [&] {
96 // Emergency abort in case unexpected logical error to prevent endless loops
97 // Success probability: >24% per attempt (25% that elements are distinct * 96% enough elements are in range)
98 // => 203 attempts for 2^(-80) fail probability
99 constexpr size_t MAX_ATTEMPTS = 203;
100 for(size_t attempt = 0; attempt < MAX_ATTEMPTS; ++attempt) {
101 if(auto maybe_e = fixed_weight_vector_gen(params, rng)) {
102 return maybe_e.value();
103 }
104 }
105 throw Internal_Error("Cannot created fixed weight vector. Is your RNG broken?");
106 }();
107
108 auto hash_func = params.hash_func();
109
110 BufferStuffer big_c_stuf(out_encapsulated_key);
111 const auto e_bytes = e.get().to_bytes();
112 // Compute and store ciphertext C/C_0 from spec
113 const auto big_c_0 = encode(params, e, m_key->matrix());
114 big_c_0.to_bytes(big_c_stuf.next(ceil_tobytes(big_c_0.size())));
115 if(params.is_pc()) {
116 // Compute and store ciphertext C_1 from spec
117 hash_func->update(0x02);
118 hash_func->update(e_bytes);
119 hash_func->final(big_c_stuf.next(hash_func->output_length()));
120 }
121 BOTAN_ASSERT_NOMSG(big_c_stuf.full());
122
123 // Compute K = Hash(1,e,C) from spec
124 hash_func->update(0x01);
125 hash_func->update(e_bytes);
126 hash_func->update(out_encapsulated_key);
127 hash_func->final(out_shared_key);
128 CT::unpoison_all(out_encapsulated_key, out_shared_key);
129}
130
131} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
Helper class to ease in-place marshalling of concatenated fixed-length values.
Definition stl_util.h:142
constexpr std::span< uint8_t > next(size_t bytes)
Definition stl_util.h:150
constexpr bool full() const
Definition stl_util.h:187
static constexpr Mask< T > is_equal(T x, T y)
Definition ct_utils.h:453
void raw_kem_encrypt(std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_shared_key, RandomNumberGenerator &rng) override
constexpr T & get() &
Definition strong_type.h:50
constexpr void unpoison_all(Ts &&... ts)
Definition ct_utils.h:201
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:64
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:53
Strong< secure_bitvector, struct CmceCodeWord_ > CmceCodeWord
Represents C of decapsulation.
Definition cmce_types.h:52
bitvector_base< secure_allocator > secure_bitvector
Definition bitvector.h:1297
Strong< secure_bitvector, struct CmceErrorVector_ > CmceErrorVector
Represents e of encapsulation.
Definition cmce_types.h:49
constexpr auto load_le(ParamTs &&... params)
Definition loadstor.h:521
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
constexpr T ceil_tobytes(T bits)
Definition bit_ops.h:168