lib-q-core 0.0.4

Core types and traits for lib-Q post-quantum cryptography library
Documentation
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Security constants for lib-Q
//!
//! This module provides security-related constants used throughout the library
//! for validation and configuration.

use lib_q_types::hqc;

use crate::api::Algorithm;
use crate::error::Result;

/// Security constants for lib-Q
///
/// This struct provides access to security-related constants used throughout
/// the library for validation and configuration.
#[cfg(feature = "alloc")]
#[derive(Clone)]
pub struct SecurityConstants {
    /// Maximum plaintext, ciphertext, or associated-data size for a single AEAD operation.
    ///
    /// A modest default encourages chunking and reduces accidental nonce reuse under streaming
    /// misuse; raise via [`Self::set_max_aead_message_size`] when your deployment accepts larger
    /// single-shot bindings.
    max_aead_message_size: usize,
    /// Maximum input length for hash absorb and for signature message preimages.
    ///
    /// Defaults to [`usize::MAX`] so digest and sign APIs are not capped by the AEAD binding
    /// policy. Lower this in environments that need a hard resource ceiling on preimage size.
    max_hash_message_size: usize,
    // Standard nonce size in bytes (16 bytes)
    standard_nonce_size: usize,
    // Minimum randomness size in bytes (32 bytes)
    min_randomness_size: usize,
}

#[cfg(feature = "alloc")]
impl SecurityConstants {
    /// Create a new SecurityConstants instance
    ///
    /// # Returns
    ///
    /// A new instance of SecurityConstants with default values.
    pub fn new() -> Self {
        Self {
            max_aead_message_size: 1024 * 1024, // 1 MiB
            max_hash_message_size: usize::MAX,
            standard_nonce_size: 16, // 16 bytes
            min_randomness_size: 32, // 32 bytes
        }
    }
}

#[cfg(feature = "alloc")]
impl Default for SecurityConstants {
    fn default() -> Self {
        Self::new()
    }
}

impl SecurityConstants {
    /// Maximum plaintext, ciphertext, or AAD size for one AEAD call.
    pub fn max_aead_message_size(&self) -> usize {
        self.max_aead_message_size
    }

    /// Maximum hash input length and signature message length.
    pub fn max_hash_message_size(&self) -> usize {
        self.max_hash_message_size
    }

    /// Get the standard nonce size
    ///
    /// # Returns
    ///
    /// Returns the standard nonce size in bytes.
    pub fn standard_nonce_size(&self) -> usize {
        self.standard_nonce_size
    }

    /// Get the minimum randomness size
    ///
    /// # Returns
    ///
    /// Returns the minimum required randomness size in bytes.
    pub fn min_randomness_size(&self) -> usize {
        self.min_randomness_size
    }

    /// Get the expected key size for a given algorithm
    ///
    /// # Arguments
    ///
    /// * `algorithm` - The algorithm to get the key size for
    /// * `is_secret` - Whether this is a secret key (affects expected size)
    ///
    /// # Returns
    ///
    /// Returns the expected key size in bytes, or an error if the algorithm
    /// doesn't use keys or is not supported.
    pub fn get_expected_key_size(&self, algorithm: Algorithm, is_secret: bool) -> Result<usize> {
        let expected_size = match algorithm {
            // KEM algorithms
            Algorithm::MlKem512 => {
                if is_secret {
                    1632
                } else {
                    800
                }
            }
            Algorithm::MlKem768 => {
                if is_secret {
                    2400
                } else {
                    1184
                }
            }
            Algorithm::MlKem1024 => {
                if is_secret {
                    3168
                } else {
                    1568
                }
            }
            // CB-KEM algorithms
            Algorithm::CbKem348864 => {
                if is_secret {
                    6492 // CB-KEM-348864 secret key size
                } else {
                    261120 // CB-KEM-348864 public key size
                }
            }
            Algorithm::CbKem460896 => {
                if is_secret {
                    13608 // CB-KEM-460896 secret key size
                } else {
                    524160 // CB-KEM-460896 public key size
                }
            }
            Algorithm::CbKem6688128 => {
                if is_secret {
                    13932 // CB-KEM-6688128 secret key size
                } else {
                    1044992 // CB-KEM-6688128 public key size
                }
            }
            Algorithm::CbKem6960119 => {
                if is_secret {
                    13948 // CB-KEM-6960119 secret key size
                } else {
                    1047319 // CB-KEM-6960119 public key size
                }
            }
            Algorithm::CbKem8192128 => {
                if is_secret {
                    14120 // CB-KEM-8192128 secret key size
                } else {
                    1357824 // CB-KEM-8192128 public key size
                }
            }

            // HQC KEM — sizes from `lib_q_types::hqc` (single source of truth).
            Algorithm::Hqc128 => {
                if is_secret {
                    hqc::HQC128_SECRET_KEY_BYTES
                } else {
                    hqc::HQC128_PUBLIC_KEY_BYTES
                }
            }
            Algorithm::Hqc192 => {
                if is_secret {
                    hqc::HQC192_SECRET_KEY_BYTES
                } else {
                    hqc::HQC192_PUBLIC_KEY_BYTES
                }
            }
            Algorithm::Hqc256 => {
                if is_secret {
                    hqc::HQC256_SECRET_KEY_BYTES
                } else {
                    hqc::HQC256_PUBLIC_KEY_BYTES
                }
            }

            // Signature algorithms
            Algorithm::MlDsa44 => {
                if is_secret {
                    2560 // ML-DSA-44 secret key size
                } else {
                    1312 // ML-DSA-44 public key size
                }
            }
            Algorithm::MlDsa65 => {
                if is_secret {
                    4032 // ML-DSA-65 secret key size
                } else {
                    1952 // ML-DSA-65 public key size
                }
            }
            Algorithm::MlDsa87 => {
                if is_secret {
                    4896 // ML-DSA-87 secret key size
                } else {
                    2592 // ML-DSA-87 public key size
                }
            }
            Algorithm::FnDsa => {
                if is_secret {
                    1281 // FN-DSA-512 secret key size (logn=9)
                } else {
                    897 // FN-DSA-512 public key size (logn=9)
                }
            }
            Algorithm::FnDsa512 => {
                if is_secret {
                    1281 // FN-DSA-512 secret key size (logn=9)
                } else {
                    897 // FN-DSA-512 public key size (logn=9)
                }
            }
            Algorithm::FnDsa1024 => {
                if is_secret {
                    2561 // FN-DSA-1024 secret key size (logn=10)
                } else {
                    1793 // FN-DSA-1024 public key size (logn=10)
                }
            }

            // SLH-DSA algorithms
            Algorithm::SlhDsaSha256128fRobust => {
                if is_secret {
                    64 // SLH-DSA SHA256-128f secret key size (4 * N where N=16)
                } else {
                    32 // SLH-DSA SHA256-128f public key size (2 * N where N=16)
                }
            }
            Algorithm::SlhDsaSha256192fRobust => {
                if is_secret {
                    96 // SLH-DSA SHA256-192f secret key size (4 * N where N=24)
                } else {
                    48 // SLH-DSA SHA256-192f public key size (2 * N where N=24)
                }
            }
            Algorithm::SlhDsaSha256256fRobust => {
                if is_secret {
                    128 // SLH-DSA SHA256-256f secret key size (4 * N where N=32)
                } else {
                    64 // SLH-DSA SHA256-256f public key size (2 * N where N=32)
                }
            }
            Algorithm::SlhDsaShake256128fRobust => {
                if is_secret {
                    64 // SLH-DSA SHAKE256-128f secret key size (4 * N where N=16)
                } else {
                    32 // SLH-DSA SHAKE256-128f public key size (2 * N where N=16)
                }
            }
            Algorithm::SlhDsaShake256192fRobust => {
                if is_secret {
                    96 // SLH-DSA SHAKE256-192f secret key size (4 * N where N=24)
                } else {
                    48 // SLH-DSA SHAKE256-192f public key size (2 * N where N=24)
                }
            }
            Algorithm::SlhDsaShake256256fRobust => {
                if is_secret {
                    128 // SLH-DSA SHAKE256-256f secret key size (4 * N where N=32)
                } else {
                    64 // SLH-DSA SHAKE256-256f public key size (2 * N where N=32)
                }
            }

            // Hash algorithms don't have keys
            _ => {
                return Err(crate::error::Error::InvalidAlgorithm {
                    algorithm: "Algorithm does not use keys",
                });
            }
        };

        Ok(expected_size)
    }

    /// Get the expected ciphertext size for a given algorithm
    ///
    /// # Arguments
    ///
    /// * `algorithm` - The algorithm to get the ciphertext size for
    ///
    /// # Returns
    ///
    /// Returns the expected ciphertext size in bytes, or an error if the algorithm
    /// doesn't produce ciphertext or is not supported.
    pub fn get_expected_ciphertext_size(&self, algorithm: Algorithm) -> Result<usize> {
        let expected_size = match algorithm {
            Algorithm::MlKem512 => 768,   // ML-KEM-512 ciphertext size
            Algorithm::MlKem768 => 1088,  // ML-KEM-768 ciphertext size
            Algorithm::MlKem1024 => 1568, // ML-KEM-1024 ciphertext size

            // CB-KEM algorithms
            Algorithm::CbKem348864 => 96, // CB-KEM-348864 ciphertext size
            Algorithm::CbKem460896 => 156, // CB-KEM-460896 ciphertext size
            Algorithm::CbKem6688128 => 208, // CB-KEM-6688128 ciphertext size
            Algorithm::CbKem6960119 => 194, // CB-KEM-6960119 ciphertext size
            Algorithm::CbKem8192128 => 208, // CB-KEM-8192128 ciphertext size

            Algorithm::Hqc128 => hqc::HQC128_CIPHERTEXT_BYTES,
            Algorithm::Hqc192 => hqc::HQC192_CIPHERTEXT_BYTES,
            Algorithm::Hqc256 => hqc::HQC256_CIPHERTEXT_BYTES,

            _ => {
                return Err(crate::error::Error::InvalidAlgorithm {
                    algorithm: "Algorithm does not produce ciphertext",
                });
            }
        };

        Ok(expected_size)
    }

    /// Get the expected signature size for a given algorithm
    ///
    /// # Arguments
    ///
    /// * `algorithm` - The algorithm to get the signature size for
    ///
    /// # Returns
    ///
    /// Returns the expected signature size in bytes, or an error if the algorithm
    /// doesn't produce signatures or is not supported.
    pub fn get_expected_signature_size(&self, algorithm: Algorithm) -> Result<usize> {
        let expected_size = match algorithm {
            Algorithm::MlDsa44 => 2420,   // ML-DSA-44 signature size
            Algorithm::MlDsa65 => 3309,   // ML-DSA-65 signature size
            Algorithm::MlDsa87 => 4627,   // ML-DSA-87 signature size
            Algorithm::FnDsa => 666,      // FN-DSA-512 signature size (logn=9)
            Algorithm::FnDsa512 => 666,   // FN-DSA-512 signature size (logn=9)
            Algorithm::FnDsa1024 => 1280, // FN-DSA-1024 signature size (logn=10)

            // SLH-DSA signature sizes (actual sizes from implementation)
            Algorithm::SlhDsaSha256128fRobust => 17088, // SLH-DSA SHA256-128f signature size
            Algorithm::SlhDsaSha256192fRobust => 35664, // SLH-DSA SHA256-192f signature size
            Algorithm::SlhDsaSha256256fRobust => 49856, // SLH-DSA SHA256-256f signature size
            Algorithm::SlhDsaShake256128fRobust => 17088, // SLH-DSA SHAKE256-128f signature size
            Algorithm::SlhDsaShake256192fRobust => 35664, // SLH-DSA SHAKE256-192f signature size
            Algorithm::SlhDsaShake256256fRobust => 49856, // SLH-DSA SHAKE256-256f signature size

            _ => {
                return Err(crate::error::Error::InvalidAlgorithm {
                    algorithm: "Algorithm does not produce signatures",
                });
            }
        };

        Ok(expected_size)
    }

    /// Set the maximum AEAD plaintext, ciphertext, or AAD size (bytes) for one operation.
    pub fn set_max_aead_message_size(&mut self, max_size: usize) {
        self.max_aead_message_size = max_size;
    }

    /// Set the maximum hash input and signature message size (bytes).
    pub fn set_max_hash_message_size(&mut self, max_size: usize) {
        self.max_hash_message_size = max_size;
    }

    /// Set the standard nonce size
    ///
    /// # Arguments
    ///
    /// * `nonce_size` - The standard nonce size in bytes
    pub fn set_standard_nonce_size(&mut self, nonce_size: usize) {
        self.standard_nonce_size = nonce_size;
    }

    /// Set the minimum randomness size
    ///
    /// # Arguments
    ///
    /// * `min_size` - The minimum randomness size in bytes
    pub fn set_min_randomness_size(&mut self, min_size: usize) {
        self.min_randomness_size = min_size;
    }
}

#[cfg(test)]
mod tests {
    use lib_q_types::hqc;

    use super::*;

    #[test]
    fn test_security_constants_creation() {
        let constants = SecurityConstants::new();
        assert_eq!(constants.max_aead_message_size(), 1024 * 1024);
        assert_eq!(constants.max_hash_message_size(), usize::MAX);
        assert_eq!(constants.standard_nonce_size(), 16);
        assert_eq!(constants.min_randomness_size(), 32);
    }

    #[test]
    fn test_get_expected_key_size() {
        let constants = SecurityConstants::new();

        // Test ML-KEM-512
        let public_size = constants
            .get_expected_key_size(Algorithm::MlKem512, false)
            .unwrap();
        assert_eq!(public_size, 800);

        let secret_size = constants
            .get_expected_key_size(Algorithm::MlKem512, true)
            .unwrap();
        assert_eq!(secret_size, 1632);

        // Test ML-DSA-65
        let public_size = constants
            .get_expected_key_size(Algorithm::MlDsa65, false)
            .unwrap();
        assert_eq!(public_size, 1952);

        let secret_size = constants
            .get_expected_key_size(Algorithm::MlDsa65, true)
            .unwrap();
        assert_eq!(secret_size, 4032);

        assert_eq!(
            constants
                .get_expected_key_size(Algorithm::Hqc128, false)
                .unwrap(),
            hqc::HQC128_PUBLIC_KEY_BYTES
        );
        assert_eq!(
            constants
                .get_expected_key_size(Algorithm::Hqc128, true)
                .unwrap(),
            hqc::HQC128_SECRET_KEY_BYTES
        );

        // Test hash algorithm (should fail)
        let result = constants.get_expected_key_size(Algorithm::Sha3_256, false);
        assert!(result.is_err(), "Hash algorithms should not have keys");
    }

    #[test]
    fn test_get_expected_ciphertext_size() {
        let constants = SecurityConstants::new();

        // Test ML-KEM algorithms
        assert_eq!(
            constants
                .get_expected_ciphertext_size(Algorithm::MlKem512)
                .unwrap(),
            768
        );
        assert_eq!(
            constants
                .get_expected_ciphertext_size(Algorithm::MlKem768)
                .unwrap(),
            1088
        );
        assert_eq!(
            constants
                .get_expected_ciphertext_size(Algorithm::MlKem1024)
                .unwrap(),
            1568
        );

        assert_eq!(
            constants
                .get_expected_ciphertext_size(Algorithm::Hqc128)
                .unwrap(),
            hqc::HQC128_CIPHERTEXT_BYTES
        );
        assert_eq!(
            constants
                .get_expected_ciphertext_size(Algorithm::Hqc192)
                .unwrap(),
            hqc::HQC192_CIPHERTEXT_BYTES
        );
        assert_eq!(
            constants
                .get_expected_ciphertext_size(Algorithm::Hqc256)
                .unwrap(),
            hqc::HQC256_CIPHERTEXT_BYTES
        );

        // Test non-KEM algorithm (should fail)
        let result = constants.get_expected_ciphertext_size(Algorithm::Sha3_256);
        assert!(
            result.is_err(),
            "Non-KEM algorithms should not produce ciphertext"
        );
    }

    #[test]
    fn test_get_expected_signature_size() {
        let constants = SecurityConstants::new();

        // Test ML-DSA algorithms
        assert_eq!(
            constants
                .get_expected_signature_size(Algorithm::MlDsa44)
                .unwrap(),
            2420
        );
        assert_eq!(
            constants
                .get_expected_signature_size(Algorithm::MlDsa65)
                .unwrap(),
            3309
        );
        assert_eq!(
            constants
                .get_expected_signature_size(Algorithm::MlDsa87)
                .unwrap(),
            4627
        );

        // Test FN-DSA algorithms
        assert_eq!(
            constants
                .get_expected_signature_size(Algorithm::FnDsa)
                .unwrap(),
            666
        );
        assert_eq!(
            constants
                .get_expected_signature_size(Algorithm::FnDsa512)
                .unwrap(),
            666
        );
        assert_eq!(
            constants
                .get_expected_signature_size(Algorithm::FnDsa1024)
                .unwrap(),
            1280
        );

        // Test non-signature algorithm (should fail)
        let result = constants.get_expected_signature_size(Algorithm::Sha3_256);
        assert!(
            result.is_err(),
            "Non-signature algorithms should not produce signatures"
        );
    }

    #[test]
    fn test_set_constants() {
        let mut constants = SecurityConstants::new();

        constants.set_max_aead_message_size(2048 * 1024);
        assert_eq!(constants.max_aead_message_size(), 2048 * 1024);

        constants.set_max_hash_message_size(4096);
        assert_eq!(constants.max_hash_message_size(), 4096);

        // Test setting nonce size
        constants.set_standard_nonce_size(32);
        assert_eq!(constants.standard_nonce_size(), 32);

        // Test setting minimum randomness size
        constants.set_min_randomness_size(64);
        assert_eq!(constants.min_randomness_size(), 64);
    }
}