1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! HQC Error Types
//!
//! This module defines error types for HQC operations following libQ patterns.
use core::fmt;
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::string::String;
/// HQC-specific error types
///
/// This enum represents all possible errors that can occur during HQC operations.
/// Each variant includes context about when the error occurs and how to resolve it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HqcError {
/// Invalid key size
///
/// **When it occurs:** A key (public or secret) has an incorrect size for the HQC parameter set.
/// **Cause:** The key data provided doesn't match the expected size for the algorithm variant (HQC-128, HQC-192, or HQC-256).
/// **Resolution:** Ensure the key size matches the parameter set: HQC-128 (2249 bytes public, 2289 bytes secret),
/// HQC-192 (4522 bytes public, 4562 bytes secret), or HQC-256 (7245 bytes public, 7285 bytes secret).
InvalidKeySize { expected: usize, actual: usize },
/// Invalid ciphertext size
///
/// **When it occurs:** A ciphertext has an incorrect size for the HQC parameter set.
/// **Cause:** The ciphertext data doesn't match the expected size for decapsulation.
/// **Resolution:** Ensure the ciphertext was generated for the same HQC parameter set and hasn't been corrupted.
InvalidCiphertextSize { expected: usize, actual: usize },
/// Invalid public key size
///
/// **When it occurs:** A public key has an incorrect size.
/// **Cause:** The public key data doesn't match the expected size for the HQC parameter set.
/// **Resolution:** Verify the public key was generated or serialized correctly for the intended parameter set.
InvalidPublicKeySize { expected: usize, actual: usize },
/// Invalid secret key size
///
/// **When it occurs:** A secret key has an incorrect size.
/// **Cause:** The secret key data doesn't match the expected size for the HQC parameter set.
/// **Resolution:** Verify the secret key was generated or deserialized correctly for the intended parameter set.
InvalidSecretKeySize { expected: usize, actual: usize },
/// Decryption failed
///
/// **When it occurs:** Decapsulation fails to recover the shared secret.
/// **Cause:** The ciphertext may be corrupted, the secret key may be incorrect, or the ciphertext was generated with a different public key.
/// **Resolution:** Verify the ciphertext and secret key are valid and correspond to each other.
DecryptionFailed,
/// Invalid size
///
/// **When it occurs:** A size parameter is invalid or out of bounds.
/// **Cause:** A size value doesn't meet the requirements for the operation.
/// **Resolution:** Check that all size parameters are within valid ranges for the HQC parameter set.
InvalidSize,
/// Encryption failed
///
/// **When it occurs:** Encapsulation fails to generate a valid ciphertext.
/// **Cause:** Random number generation may have failed, or internal computation encountered an error.
/// **Resolution:** Ensure a secure random number generator is available and functioning correctly.
EncryptionFailed,
/// Key generation failed
///
/// **When it occurs:** Key pair generation fails.
/// **Cause:** Random number generation may have failed, or internal computation encountered an error.
/// **Resolution:** Ensure a secure random number generator is available and functioning correctly.
KeyGenerationFailed,
/// Random number generation failed
///
/// **When it occurs:** The random number generator fails to produce random bytes.
/// **Cause:** The underlying RNG implementation encountered an error or is unavailable.
/// **Resolution:** Check RNG initialization and ensure a secure random source is available.
RandomGenerationFailed,
/// Internal error
///
/// **When it occurs:** An unexpected internal error occurs during HQC operations.
/// **Cause:** This typically indicates a bug in the implementation or corrupted internal state.
/// **Resolution:** Report this error as it may indicate a software bug. Check inputs and system state.
InternalError,
/// Not implemented
///
/// **When it occurs:** A requested feature or operation is not yet implemented.
/// **Cause:** The operation is not available in the current implementation.
/// **Resolution:** Check if an alternative approach is available, or wait for the feature to be implemented.
NotImplemented,
/// Invalid parameter
///
/// **When it occurs:** A parameter value is invalid for the operation.
/// **Cause:** A parameter doesn't meet the requirements or constraints for the HQC operation.
/// **Resolution:** Verify all parameters are within valid ranges and meet the algorithm requirements.
InvalidParameter,
/// Memory allocation failed
///
/// **When it occurs:** Dynamic memory allocation fails during an operation.
/// **Cause:** Insufficient memory is available, or allocation is not supported in the current environment.
/// **Resolution:** Ensure sufficient memory is available, or use a no_std-compatible configuration.
AllocationFailed,
/// Hash function error
///
/// **When it occurs:** A hash function operation fails.
/// **Cause:** The underlying hash implementation encountered an error.
/// **Resolution:** Check that the hash function implementation is properly initialized and functioning.
HashError,
/// BCH code error
///
/// **When it occurs:** BCH (Bose-Chaudhuri-Hocquenghem) code operations fail.
/// **Cause:** Error correction code computation encountered an error, possibly due to corrupted data.
/// **Resolution:** Verify input data integrity and that error correction parameters are correct.
BchError,
/// Polynomial operation error
///
/// **When it occurs:** Polynomial arithmetic operations fail.
/// **Cause:** Polynomial computation encountered an error, possibly due to invalid coefficients or degree.
/// **Resolution:** Verify polynomial inputs are valid and within expected ranges.
PolynomialError,
/// Encoding error
///
/// **When it occurs:** Encoding operations fail.
/// **Cause:** Data encoding encountered an error, possibly due to invalid input format.
/// **Resolution:** Verify input data format and encoding parameters are correct.
EncodingError,
/// Verification error
///
/// **When it occurs:** Verification operations fail.
/// **Cause:** Data verification failed, indicating the data may be corrupted or invalid.
/// **Resolution:** Verify input data integrity and that verification parameters are correct.
VerificationError,
/// Invalid weight
///
/// **When it occurs:** A polynomial weight is invalid for the operation.
/// **Cause:** The weight parameter doesn't meet the requirements for the HQC parameter set.
/// **Resolution:** Ensure the weight is within valid ranges: HQC-128 (66), HQC-192 (103), or HQC-256 (134).
InvalidWeight,
/// Allocation required (for no_std environments)
///
/// **When it occurs:** An operation requires dynamic allocation but the `alloc` feature is not enabled.
/// **Cause:** The operation needs heap allocation but the crate is compiled in `no_std` mode without `alloc`.
/// **Resolution:** Enable the `alloc` feature or use an alternative approach that doesn't require allocation.
AllocRequired,
}
impl fmt::Display for HqcError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
HqcError::InvalidKeySize { expected, actual } => {
write!(f, "Invalid key size: expected {}, got {}", expected, actual)
}
HqcError::InvalidCiphertextSize { expected, actual } => {
write!(
f,
"Invalid ciphertext size: expected {}, got {}",
expected, actual
)
}
HqcError::InvalidPublicKeySize { expected, actual } => {
write!(
f,
"Invalid public key size: expected {}, got {}",
expected, actual
)
}
HqcError::InvalidSecretKeySize { expected, actual } => {
write!(
f,
"Invalid secret key size: expected {}, got {}",
expected, actual
)
}
HqcError::DecryptionFailed => write!(f, "Decryption failed"),
HqcError::InvalidSize => write!(f, "Invalid size"),
HqcError::EncryptionFailed => write!(f, "Encryption failed"),
HqcError::KeyGenerationFailed => write!(f, "Key generation failed"),
HqcError::RandomGenerationFailed => write!(f, "Random number generation failed"),
HqcError::InternalError => write!(f, "Internal error"),
HqcError::NotImplemented => write!(f, "Not implemented"),
HqcError::InvalidParameter => write!(f, "Invalid parameter"),
HqcError::AllocationFailed => write!(f, "Memory allocation failed"),
HqcError::HashError => write!(f, "Hash function error"),
HqcError::BchError => write!(f, "BCH code error"),
HqcError::PolynomialError => write!(f, "Polynomial operation error"),
HqcError::EncodingError => write!(f, "Encoding error"),
HqcError::VerificationError => write!(f, "Verification error"),
HqcError::InvalidWeight => write!(f, "Invalid weight"),
HqcError::AllocRequired => write!(f, "Allocation required (no_std environment)"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for HqcError {}
impl From<HqcError> for lib_q_core::Error {
fn from(err: HqcError) -> Self {
match err {
HqcError::InvalidKeySize { expected, actual } => {
lib_q_core::Error::InvalidKeySize { expected, actual }
}
HqcError::InvalidCiphertextSize { expected, actual } => {
lib_q_core::Error::InvalidCiphertextSize { expected, actual }
}
HqcError::InvalidPublicKeySize { expected, actual } => {
// Map to InvalidKeySize since InvalidPublicKeySize doesn't exist in lib-q-core
lib_q_core::Error::InvalidKeySize { expected, actual }
}
HqcError::InvalidSecretKeySize { expected, actual } => {
// Map to InvalidKeySize since InvalidSecretKeySize doesn't exist in lib-q-core
lib_q_core::Error::InvalidKeySize { expected, actual }
}
HqcError::DecryptionFailed => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::DecryptionFailed {
operation: String::from("HQC decapsulation"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::DecryptionFailed {
operation: "HQC decapsulation".into(),
}
}
}
HqcError::InvalidSize => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::InternalError {
operation: String::from("HQC operation"),
details: String::from("Invalid size parameter"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::InternalError {
operation: "HQC operation".into(),
details: "Invalid size parameter".into(),
}
}
}
HqcError::EncryptionFailed => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::EncryptionFailed {
operation: String::from("HQC encapsulation"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::EncryptionFailed {
operation: "HQC encapsulation".into(),
}
}
}
HqcError::KeyGenerationFailed => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::KeyGenerationFailed {
operation: String::from("HQC key generation"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::KeyGenerationFailed {
operation: "HQC key generation".into(),
}
}
}
HqcError::RandomGenerationFailed => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::RandomGenerationFailed {
operation: String::from("HQC random generation"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::RandomGenerationFailed {
operation: "HQC random generation".into(),
}
}
}
HqcError::InternalError => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::InternalError {
operation: String::from("HQC operation"),
details: String::from("Internal error"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::InternalError {
operation: "HQC operation".into(),
details: "Internal error".into(),
}
}
}
HqcError::NotImplemented => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::NotImplemented {
feature: String::from("HQC feature"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::NotImplemented {
feature: "HQC feature".into(),
}
}
}
HqcError::InvalidParameter => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::InternalError {
operation: String::from("HQC operation"),
details: String::from("Invalid parameter"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::InternalError {
operation: "HQC operation".into(),
details: "Invalid parameter".into(),
}
}
}
HqcError::AllocationFailed => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::MemoryAllocationFailed {
operation: String::from("HQC operation"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::MemoryAllocationFailed {
operation: "HQC operation".into(),
}
}
}
HqcError::HashError => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::InternalError {
operation: String::from("HQC hash operation"),
details: String::from("Hash computation failed"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::InternalError {
operation: "HQC hash operation".into(),
details: "Hash computation failed".into(),
}
}
}
HqcError::BchError => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::InternalError {
operation: String::from("HQC BCH operation"),
details: String::from("BCH code error"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::InternalError {
operation: "HQC BCH operation".into(),
details: "BCH code error".into(),
}
}
}
HqcError::PolynomialError => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::InternalError {
operation: String::from("HQC polynomial operation"),
details: String::from("Polynomial computation error"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::InternalError {
operation: "HQC polynomial operation".into(),
details: "Polynomial computation error".into(),
}
}
}
HqcError::EncodingError => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::InternalError {
operation: String::from("HQC encoding operation"),
details: String::from("Encoding computation error"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::InternalError {
operation: "HQC encoding operation".into(),
details: "Encoding computation error".into(),
}
}
}
HqcError::VerificationError => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::InternalError {
operation: String::from("HQC verification operation"),
details: String::from("Verification computation error"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::InternalError {
operation: "HQC verification operation".into(),
details: "Verification computation error".into(),
}
}
}
HqcError::InvalidWeight => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::InternalError {
operation: String::from("HQC weight validation"),
details: String::from("Invalid polynomial weight"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::InternalError {
operation: "HQC weight validation".into(),
details: "Invalid polynomial weight".into(),
}
}
}
HqcError::AllocRequired => {
#[cfg(feature = "alloc")]
{
lib_q_core::Error::InternalError {
operation: String::from("HQC operation"),
details: String::from("Allocation required but not available"),
}
}
#[cfg(not(feature = "alloc"))]
{
lib_q_core::Error::InternalError {
operation: "HQC operation".into(),
details: "Allocation required but not available".into(),
}
}
}
}
}
}
impl From<lib_q_core::Error> for HqcError {
fn from(err: lib_q_core::Error) -> Self {
match err {
lib_q_core::Error::InvalidKeySize { expected, actual } => {
HqcError::InvalidKeySize { expected, actual }
}
lib_q_core::Error::InvalidCiphertextSize { expected, actual } => {
HqcError::InvalidCiphertextSize { expected, actual }
}
// Note: InvalidPublicKeySize and InvalidSecretKeySize don't exist in lib-q-core
// They are mapped to InvalidKeySize in the forward direction
lib_q_core::Error::DecryptionFailed { .. } => HqcError::DecryptionFailed,
lib_q_core::Error::EncryptionFailed { .. } => HqcError::EncryptionFailed,
lib_q_core::Error::KeyGenerationFailed { .. } => HqcError::KeyGenerationFailed,
lib_q_core::Error::RandomGenerationFailed { .. } => HqcError::RandomGenerationFailed,
lib_q_core::Error::InternalError { .. } => HqcError::InternalError,
lib_q_core::Error::NotImplemented { .. } => HqcError::NotImplemented,
lib_q_core::Error::MemoryAllocationFailed { .. } => HqcError::AllocationFailed,
_ => HqcError::InternalError, // Map unknown errors to internal error
}
}
}