lib-q-aead 0.0.5

Post-quantum Authenticated Encryption for lib-Q
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
//! Integration tests for lib-q-aead
//!
//! These tests verify the complete AEAD functionality including the registry system,
//! algorithm implementations, and plugin architecture.

use lib_q_aead::*;
use lib_q_core::{
    AeadKey,
    Algorithm,
    AlgorithmCategory,
    Error,
    Nonce,
};

/// Generate a proper test key with good entropy (32 bytes; typical lib-q AEADs).
fn create_test_key() -> AeadKey {
    AeadKey::new(vec![
        0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32,
        0x10, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE,
        0xFF, 0x00,
    ])
}

/// Key material sized for the algorithm (Romulus uses 128-bit keys).
fn create_test_key_for(algorithm: Algorithm) -> AeadKey {
    match algorithm {
        Algorithm::RomulusN | Algorithm::RomulusM => AeadKey::new(vec![
            0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54,
            0x32, 0x10,
        ]),
        _ => create_test_key(),
    }
}

/// Second distinct key, same length as [`create_test_key_for`].
fn create_alt_test_key_for(algorithm: Algorithm) -> AeadKey {
    match algorithm {
        Algorithm::RomulusN | Algorithm::RomulusM => AeadKey::new(vec![
            0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE,
            0xFF, 0x00,
        ]),
        _ => AeadKey::new(vec![
            0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE,
            0xFF, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98,
            0x76, 0x54, 0x32, 0x10,
        ]),
    }
}

/// Generate a proper test nonce with good entropy
fn create_test_nonce() -> Nonce {
    Nonce::new(vec![
        0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32,
        0x10,
    ])
}

#[test]
fn test_available_algorithms() {
    let algorithms = available_algorithms();
    assert!(!algorithms.is_empty());

    // All returned algorithms should be AEAD algorithms
    for algorithm in algorithms {
        assert_eq!(algorithm.category(), AlgorithmCategory::Aead);
    }
}

#[test]
fn test_create_aead() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm);
        assert!(aead.is_ok(), "Failed to create AEAD for {:?}", algorithm);
    }
}

#[test]
fn test_invalid_algorithm() {
    // Try to create AEAD with non-AEAD algorithm
    let result = create_aead(Algorithm::MlKem512);
    assert!(result.is_err());

    if let Err(Error::InvalidAlgorithm { algorithm }) = result {
        assert!(algorithm.contains("not an AEAD algorithm"));
    } else {
        panic!("Expected InvalidAlgorithm error");
    }
}

#[test]
fn test_algorithm_availability() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        assert!(is_algorithm_available(algorithm));
    }

    // Non-AEAD algorithms should not be available
    assert!(!is_algorithm_available(Algorithm::MlKem512));
}

#[test]
fn test_algorithm_metadata() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let metadata = get_algorithm_metadata(algorithm);
        assert!(metadata.is_some(), "No metadata for {:?}", algorithm);

        if let Some(meta) = metadata {
            assert_eq!(meta.algorithm, algorithm);
            assert!(meta.key_size > 0);
            assert!(meta.nonce_size > 0);
            assert!(meta.tag_size > 0);
        }
    }
}

#[test]
fn test_registry_functionality() {
    let registry = registry();

    // Test available algorithms
    let algorithms = registry.available_algorithms();
    assert!(!algorithms.is_empty());

    // Test algorithm creation
    for algorithm in &algorithms {
        let aead = registry.create_aead(*algorithm);
        assert!(aead.is_ok(), "Failed to create AEAD for {:?}", algorithm);
    }

    // Test metadata retrieval
    for algorithm in &algorithms {
        let metadata = registry.get_metadata(*algorithm);
        assert!(metadata.is_some(), "No metadata for {:?}", algorithm);
    }
}

#[test]
fn test_aead_encrypt_decrypt() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();

        // Test with different key and nonce values
        let key = create_test_key_for(algorithm);
        let nonce = create_test_nonce();
        let plaintext = b"Hello, World!";
        let associated_data = b"metadata";

        // Encrypt
        let ciphertext = aead.encrypt(&key, &nonce, plaintext, Some(associated_data.as_slice()));
        assert!(ciphertext.is_ok(), "Encryption failed for {:?}", algorithm);

        let ciphertext = ciphertext.unwrap();
        assert_eq!(ciphertext.len(), plaintext.len() + aead.tag_size());

        // Decrypt
        let decrypted = aead.decrypt(&key, &nonce, &ciphertext, Some(associated_data.as_slice()));
        assert!(decrypted.is_ok(), "Decryption failed for {:?}", algorithm);
        assert_eq!(decrypted.unwrap(), plaintext);
    }
}

#[test]
fn test_aead_authentication_failure() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();

        let key = create_test_key_for(algorithm);
        let nonce = create_test_nonce();
        let plaintext = b"Hello, World!";

        // Encrypt
        let ciphertext = aead.encrypt(&key, &nonce, plaintext, None).unwrap();

        // Tamper with ciphertext
        let mut tampered = ciphertext.clone();
        tampered[0] ^= 0xFF;

        // Decrypt should fail
        let result = aead.decrypt(&key, &nonce, &tampered, None);
        assert!(
            result.is_err(),
            "Authentication should fail for {:?}",
            algorithm
        );

        match result {
            Err(Error::VerificationFailed { operation }) => {
                assert!(operation.contains("AEAD tag verification"));
            }
            Err(Error::AuthenticationFailed { operation }) => {
                assert!(operation.contains("Tag verification failed"));
            }
            _ => {
                println!("Actual error: {:?}", result);
                panic!(
                    "Expected VerificationFailed or AuthenticationFailed error for {:?}",
                    algorithm
                );
            }
        }
    }
}

#[test]
fn test_aead_wrong_key() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();

        let key1 = create_test_key_for(algorithm);
        let key2 = create_alt_test_key_for(algorithm);
        let nonce = create_test_nonce();
        let plaintext = b"Hello, World!";

        // Encrypt with key1
        let ciphertext = aead.encrypt(&key1, &nonce, plaintext, None).unwrap();

        // Decrypt with key2 should fail
        let result = aead.decrypt(&key2, &nonce, &ciphertext, None);
        assert!(
            result.is_err(),
            "Decryption with wrong key should fail for {:?}",
            algorithm
        );
    }
}

#[test]
fn test_aead_wrong_nonce() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();

        let key = create_test_key_for(algorithm);
        let nonce1 = create_test_nonce();
        let nonce2 = Nonce::new(vec![
            0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE,
            0xFF, 0x00,
        ]);
        let plaintext = b"Hello, World!";

        // Encrypt with nonce1
        let ciphertext = aead.encrypt(&key, &nonce1, plaintext, None).unwrap();

        // Decrypt with nonce2 should fail
        let result = aead.decrypt(&key, &nonce2, &ciphertext, None);
        assert!(
            result.is_err(),
            "Decryption with wrong nonce should fail for {:?}",
            algorithm
        );
    }
}

#[test]
fn test_aead_empty_plaintext() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();

        let key = create_test_key_for(algorithm);
        let nonce = create_test_nonce();
        let plaintext = b"";

        // Encrypt empty plaintext
        let ciphertext = aead.encrypt(&key, &nonce, plaintext, None);
        assert!(
            ciphertext.is_ok(),
            "Empty plaintext encryption should work for {:?}",
            algorithm
        );

        let ciphertext = ciphertext.unwrap();
        assert_eq!(ciphertext.len(), aead.tag_size());

        // Decrypt should work
        let decrypted = aead.decrypt(&key, &nonce, &ciphertext, None);
        assert!(
            decrypted.is_ok(),
            "Empty plaintext decryption should work for {:?}",
            algorithm
        );
        assert_eq!(decrypted.unwrap(), plaintext);
    }
}

#[test]
fn test_aead_large_plaintext() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();

        let key = create_test_key_for(algorithm);
        let nonce = create_test_nonce();
        let plaintext = vec![0u8; 1024 * 1024]; // 1MB

        // Encrypt large plaintext
        let ciphertext = aead.encrypt(&key, &nonce, &plaintext, None);
        assert!(
            ciphertext.is_ok(),
            "Large plaintext encryption should work for {:?}",
            algorithm
        );

        let ciphertext = ciphertext.unwrap();
        assert_eq!(ciphertext.len(), plaintext.len() + aead.tag_size());

        // Decrypt should work
        let decrypted = aead.decrypt(&key, &nonce, &ciphertext, None);
        assert!(
            decrypted.is_ok(),
            "Large plaintext decryption should work for {:?}",
            algorithm
        );
        assert_eq!(decrypted.unwrap(), plaintext);
    }
}

#[test]
fn test_aead_associated_data() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();

        let key = create_test_key_for(algorithm);
        let nonce = create_test_nonce();
        let plaintext = b"Hello, World!";
        let associated_data = Some(b"important metadata".as_slice());

        // Encrypt with associated data
        let ciphertext = aead.encrypt(&key, &nonce, plaintext, associated_data);
        assert!(
            ciphertext.is_ok(),
            "Encryption with AD should work for {:?}",
            algorithm
        );

        let ciphertext = ciphertext.unwrap();

        // Decrypt with correct associated data should work
        let decrypted = aead.decrypt(&key, &nonce, &ciphertext, associated_data);
        assert!(
            decrypted.is_ok(),
            "Decryption with correct AD should work for {:?}",
            algorithm
        );
        assert_eq!(decrypted.unwrap(), plaintext);

        // Decrypt with wrong associated data should fail
        let wrong_ad = Some(b"wrong metadata".as_slice());
        let result = aead.decrypt(&key, &nonce, &ciphertext, wrong_ad);
        assert!(
            result.is_err(),
            "Decryption with wrong AD should fail for {:?}",
            algorithm
        );
    }
}

#[test]
fn test_aead_key_validation() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();

        let ks = aead.key_size();
        // Test valid key
        let key = AeadKey::new(vec![0u8; ks]);
        assert!(aead.validate_key(&key).is_ok());

        // Test invalid key size (pick a wrong length distinct from ks)
        let bad_len = if ks == 32 { 16usize } else { 32usize };
        let invalid_key = AeadKey::new(vec![0u8; bad_len]);
        assert!(aead.validate_key(&invalid_key).is_err());
    }
}

#[test]
fn test_aead_nonce_validation() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();

        // Test valid nonce
        let nonce = Nonce::new(vec![0u8; 16]);
        assert!(aead.validate_nonce(&nonce).is_ok());

        // Test invalid nonce size
        let invalid_nonce = Nonce::new(vec![0u8; 12]);
        assert!(aead.validate_nonce(&invalid_nonce).is_err());
    }
}

#[test]
fn test_aead_ciphertext_validation() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();

        // Test valid ciphertext size
        assert!(aead.validate_ciphertext_size(aead.tag_size()).is_ok());

        // Test invalid ciphertext size
        assert!(aead.validate_ciphertext_size(aead.tag_size() - 1).is_err());
    }
}

#[test]
fn test_aead_metadata() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();
        let metadata = aead.metadata();

        assert_eq!(metadata.algorithm, algorithm);
        assert!(metadata.key_size > 0);
        assert!(metadata.nonce_size > 0);
        assert!(metadata.tag_size > 0);
        assert!(metadata.security_level > 0);
        assert!(!metadata.name.is_empty());
        assert!(!metadata.description.is_empty());
    }
}

#[test]
fn test_aead_performance_tier() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();
        let metadata = aead.metadata();

        // All algorithms should have a valid performance tier
        let tier = metadata.performance_tier();
        assert!(matches!(
            tier,
            lib_q_aead::PerformanceTier::UltraSecure |
                lib_q_aead::PerformanceTier::Balanced |
                lib_q_aead::PerformanceTier::Performance |
                lib_q_aead::PerformanceTier::Hybrid
        ));
    }
}

#[test]
fn test_aead_security_level_suitability() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();
        let metadata = aead.metadata();

        // Test security level suitability
        assert!(metadata.is_suitable_for_security_level(1));
        assert!(metadata.is_suitable_for_security_level(metadata.security_level));

        if metadata.security_level < 5 {
            assert!(!metadata.is_suitable_for_security_level(metadata.security_level + 1));
        }
    }
}

#[test]
fn test_aead_total_overhead() {
    let algorithms = available_algorithms();

    for algorithm in algorithms {
        let aead = create_aead(algorithm).unwrap();
        let metadata = aead.metadata();

        // Total overhead should be nonce size + tag size
        let expected_overhead = metadata.nonce_size + metadata.tag_size;
        assert_eq!(metadata.total_overhead(), expected_overhead);
    }
}