Botan 3.11.0
Crypto and TLS for C&
cmce_parameters.h
Go to the documentation of this file.
1/*
2 * Classic McEliece Parameters
3 * (C) 2023 Jack Lloyd
4 * 2023,2024 Fabian Albert, Amos Treiber - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 **/
8
9#ifndef BOTAN_CMCE_PARAMS_H_
10#define BOTAN_CMCE_PARAMS_H_
11
12#include <botan/asn1_obj.h>
13#include <botan/cmce_parameter_set.h>
14#include <botan/hash.h>
15#include <botan/xof.h>
16#include <botan/internal/cmce_gf.h>
17#include <botan/internal/cmce_poly.h>
18
19#include <string_view>
20
21namespace Botan {
22
23/**
24 * Container for all Classic McEliece parameters.
25 */
26class BOTAN_TEST_API Classic_McEliece_Parameters final {
27 public:
28 /**
29 * @brief Create Classic McEliece parameters from a parameter set.
30 */
31 static Classic_McEliece_Parameters create(Classic_McEliece_Parameter_Set set);
32
33 /**
34 * @brief Create Classic McEliece parameters from a parameter set name.
35 */
36 static Classic_McEliece_Parameters create(std::string_view name);
37
38 /**
39 * @brief Create Classic McEliece parameters from an OID.
40 */
41 static Classic_McEliece_Parameters create(const OID& oid);
42
43 /**
44 * @brief The parameter set for this Classic McEliece instance.
45 */
47
48 /**
49 * @brief The OID for the Classic McEliece instance.
50 */
51 OID object_identifier() const;
52
53 /**
54 * @returns true iff the instance is a plaintext confirmation (PC) instance.
55 */
64
65 /**
66 * @returns true iff the instance is a fast (F) instance, i.e. if the semi-systematic
67 * matrix creation is used.
68` */
69 bool is_f() const {
78 }
79
80 /**
81 * @brief The degree of the Classic McEliece instance's underlying Galois Field, i.e. GF(q) = GF(2^m).
82 */
83 size_t m() const { return m_m; }
84
85 /**
86 * @brief The field size of the Classic McEliece instance's underlying Galois Field, i.e.
87 * GF(q) is the underlying field.
88 */
89 size_t q() const { return (size_t(1) << m_m); }
90
91 /**
92 * @brief The code length of the Classic McEliece instance.
93 *
94 * E.g. the Classic McEliece matrix H is of size m*t x n,
95 * the encoded error vector is, therefore, of size n.
96 */
97 size_t n() const { return m_n; }
98
99 /**
100 * @brief The weight of the error vector e.
101 */
102 size_t t() const { return m_poly_ring.degree(); }
103
104 /**
105 * @brief Bit output length of the hash function H.
106 */
107 static constexpr size_t ell() { return 256; }
108
109 /**
110 * @brief The number of bits each GF element is encoded with.
111 */
112 static constexpr size_t sigma1() { return 16; }
113
114 /**
115 * @brief Constant for field-ordering generation. (see Classic McEliece ISO 8.2)
116 */
117 static constexpr size_t sigma2() { return 32; }
118
119 /**
120 * @brief Constant mu for semi-systematic matrix creation. (see Classic McEliece ISO 7.2.3)
121 */
122 static constexpr size_t mu() { return 32; }
123
124 /**
125 * @brief Constant nu for semi-systematic matrix creation. (see Classic McEliece ISO 7.2.3)
126 */
127 static constexpr size_t nu() { return 64; }
128
129 /**
130 * @brief Constant tau for fixed-weight vector generation. (see Classic McEliece ISO 8.4)
131 */
132 size_t tau() const {
133 // Section 8.4 of ISO:
134 // The integer tau is defined as t if n=q; as 2t if q/2<=n<q; as 4t if q/4<=n<q/2; etc
135 const size_t tau_fact = size_t(1) << (m() - floor_log2(n()));
136 return tau_fact * t();
137 }
138
139 /**
140 * @brief The monic irreducible polynomial f(z) of degree m over GF(2). Used for modular
141 * reduction in GF(2^m).
142 */
143 CmceGfMod poly_f() const { return m_poly_ring.poly_f(); }
144
145 /**
146 * @brief The estimated bit security strength of the Classic McEliece instance.
147 *
148 * Reference: Classic McEliece NIST Round 4 submission, Guide for security reviewers
149 */
150 size_t estimated_strength() const;
151
152 /**
153 * @brief The byte length of the seed delta. See ISO 9.2.12.
154 */
155 static constexpr size_t seed_len() { return ell() / 8; }
156
157 /**
158 * @brief The byte length of the column selection c. See ISO 9.2.12.
159 */
160 static constexpr size_t sk_c_bytes() { return 8; }
161
162 /**
163 * @brief The length of the byte representation of the minimal polynomial g. See ISO 9.2.12.
164 */
165 size_t sk_poly_g_bytes() const { return t() * sizeof(uint16_t); }
166
167 /**
168 * @brief The length of the byte representation of the field ordering's control bits. See ISO 9.2.12.
169 */
170 size_t sk_alpha_control_bytes() const { return (2 * m() - 1) * (size_t(1) << (m() - 4)); }
171
172 /**
173 * @brief The byte length of the seed s. s is used for implicit rejection. See ISO 9.2.12.
174 */
175 size_t sk_s_bytes() const { return n() / 8; }
176
177 /**
178 * @brief The byte length of the secret key sk. See ISO 9.2.12.
179 */
180 size_t sk_size_bytes() const {
181 // ISO 9.2.12: sk = (delta, c, g, alpha(control bits), s)
183 }
184
185 /**
186 * @brief The number of rows in the public key's matrix.
187 */
188 size_t pk_no_rows() const { return t() * m(); }
189
190 /**
191 * @brief The number of columns in the public key's matrix.
192 *
193 * Note that this is only the column number of the submatrix T (with H = (I_mt | T)),
194 * which is stored in the public key. The column number of the whole matrix H is n.
195 * This constant is also denoted as k in the spec.
196 */
197 size_t pk_no_cols() const { return n() - pk_no_rows(); }
198
199 /**
200 * @brief The number of bytes for each row in the public key's matrix.
201 */
202 size_t pk_row_size_bytes() const { return (pk_no_cols() + 7) / 8; }
203
204 /**
205 * @brief The number of bytes for the public key.
206 *
207 * Equal to the byte size of the CMCE matrix.
208 */
209 size_t pk_size_bytes() const { return pk_no_rows() * pk_row_size_bytes(); }
210
211 /**
212 * @brief The output byte size of the encoding algorithm. See ISO 7.3
213 */
214 size_t encode_out_size() const { return ceil_division<size_t>(m() * t(), 8); }
215
216 /**
217 * @brief The byte size of the hash output.
218 *
219 * This is also the size of the shared key K that is a hash output.
220 */
221 static constexpr size_t hash_out_bytes() { return ell() / 8; }
222
223 /**
224 * @brief The byte size of the ciphertext.
225 */
226 size_t ciphertext_size() const {
227 if(is_pc()) {
228 // C_0 + C_1
229 return encode_out_size() + hash_out_bytes();
230 } else {
231 return encode_out_size();
232 }
233 }
234
235 /**
236 * @brief The underlying polynomial ring.
237 */
238 const Classic_McEliece_Polynomial_Ring& poly_ring() const { return m_poly_ring; }
239
240 /**
241 * @brief Create a seeded XOF object representing Classic McEliece's PRG.
242 * See Classic McEliece ISO 9.1.
243 *
244 * @param seed The seed used for the XOF.
245 */
246 std::unique_ptr<XOF> prg(std::span<const uint8_t> seed) const;
247
248 /**
249 * @brief Create an instance of the hash function Hash(x) used in Classic McEliece's
250 * Decaps and Encaps algorithms.
251 *
252 * @return a new instance of the hash function.
253 */
254 std::unique_ptr<HashFunction> hash_func() const { return HashFunction::create_or_throw("SHAKE-256(256)"); }
255
256 /**
257 * @brief Create a GF(q) element using the modulus for the current instance.
258 *
259 * @param elem The GF(q) element value.
260 * @return The GF(q) element.
261 */
263
264 private:
266 size_t m,
267 size_t n,
269
271 size_t m_m;
272 size_t m_n;
274};
275
276} // namespace Botan
277
278#endif
#define BOTAN_TEST_API
Definition api.h:41
Represents an element of the finite field GF(q) for q = 2^m.
Definition cmce_gf.h:29
Classic_McEliece_GF gf(CmceGfElem elem) const
Create a GF(q) element using the modulus for the current instance.
size_t m() const
The degree of the Classic McEliece instance's underlying Galois Field, i.e. GF(q) = GF(2^m).
static constexpr size_t seed_len()
The byte length of the seed delta. See ISO 9.2.12.
std::unique_ptr< HashFunction > hash_func() const
Create an instance of the hash function Hash(x) used in Classic McEliece's Decaps and Encaps algorith...
size_t q() const
The field size of the Classic McEliece instance's underlying Galois Field, i.e. GF(q) is the underlyi...
CmceGfMod poly_f() const
The monic irreducible polynomial f(z) of degree m over GF(2). Used for modular reduction in GF(2^m).
static constexpr size_t sk_c_bytes()
The byte length of the column selection c. See ISO 9.2.12.
static constexpr size_t nu()
Constant nu for semi-systematic matrix creation. (see Classic McEliece ISO 7.2.3).
static constexpr size_t mu()
Constant mu for semi-systematic matrix creation. (see Classic McEliece ISO 7.2.3).
static Classic_McEliece_Parameters create(Classic_McEliece_Parameter_Set set)
Create Classic McEliece parameters from a parameter set.
size_t pk_no_rows() const
The number of rows in the public key's matrix.
size_t pk_row_size_bytes() const
The number of bytes for each row in the public key's matrix.
static constexpr size_t hash_out_bytes()
The byte size of the hash output.
size_t n() const
The code length of the Classic McEliece instance.
size_t sk_poly_g_bytes() const
The length of the byte representation of the minimal polynomial g. See ISO 9.2.12.
size_t tau() const
Constant tau for fixed-weight vector generation. (see Classic McEliece ISO 8.4).
size_t pk_no_cols() const
The number of columns in the public key's matrix.
size_t sk_alpha_control_bytes() const
The length of the byte representation of the field ordering's control bits. See ISO 9....
size_t pk_size_bytes() const
The number of bytes for the public key.
size_t ciphertext_size() const
The byte size of the ciphertext.
const Classic_McEliece_Polynomial_Ring & poly_ring() const
The underlying polynomial ring.
size_t encode_out_size() const
The output byte size of the encoding algorithm. See ISO 7.3.
size_t sk_s_bytes() const
The byte length of the seed s. s is used for implicit rejection. See ISO 9.2.12.
static constexpr size_t sigma2()
Constant for field-ordering generation. (see Classic McEliece ISO 8.2).
size_t sk_size_bytes() const
The byte length of the secret key sk. See ISO 9.2.12.
static constexpr size_t sigma1()
The number of bits each GF element is encoded with.
static constexpr size_t ell()
Bit output length of the hash function H.
size_t t() const
The weight of the error vector e.
Classic_McEliece_Parameter_Set parameter_set() const
The parameter set for this Classic McEliece instance.
Represents the polynomial ring GF(q)[y]/F(y) where F(y) is the modulus polynomial in GF(q)[y] of degr...
Definition cmce_poly.h:104
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:308
BOTAN_FORCE_INLINE constexpr T floor_log2(T n)
Definition bit_ops.h:134
Strong< uint16_t, struct CmceGfMod_ > CmceGfMod
Represents a GF(q) modulus.
Definition cmce_types.h:22
BOTAN_FORCE_INLINE constexpr T ceil_division(T a, T b)
Definition bit_ops.h:167
Strong< uint16_t, struct CmceGfElem_ > CmceGfElem
Represents a GF(q) element.
Definition cmce_types.h:19