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