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
//! Thread safety tests for lib-q-aead
//!
//! These tests verify that the AEAD registry and related functionality
//! is thread-safe when compiled with std features.

#[cfg(feature = "std")]
mod thread_safety_tests {
    use std::sync::Arc;
    use std::sync::atomic::{
        AtomicUsize,
        Ordering,
    };
    use std::thread;
    use std::time::Duration;

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

    /// Test that multiple threads can safely access the global registry
    #[test]
    fn test_registry_thread_safety() {
        const NUM_THREADS: usize = 10;
        const OPERATIONS_PER_THREAD: usize = 100;

        let success_count = Arc::new(AtomicUsize::new(0));
        let mut handles = Vec::new();

        for _ in 0..NUM_THREADS {
            let success_count = Arc::clone(&success_count);

            let handle = thread::spawn(move || {
                for _ in 0..OPERATIONS_PER_THREAD {
                    // Test registry access
                    let algorithms = available_algorithms();
                    if !algorithms.is_empty() {
                        success_count.fetch_add(1, Ordering::Relaxed);
                    }

                    // Test algorithm availability checks
                    for algorithm in &algorithms {
                        if is_algorithm_available(*algorithm) {
                            success_count.fetch_add(1, Ordering::Relaxed);
                        }
                    }

                    // Test metadata retrieval
                    for algorithm in &algorithms {
                        if get_algorithm_metadata(*algorithm).is_some() {
                            success_count.fetch_add(1, Ordering::Relaxed);
                        }
                    }
                }
            });

            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().expect("Thread should not panic");
        }

        // Per iteration: 1 (non-empty registry) + 2 per registered algorithm
        // (availability check + metadata lookup).
        let n = available_algorithms().len();
        let ops_per_iteration = 1 + 2 * n;
        let total_operations = NUM_THREADS * OPERATIONS_PER_THREAD * ops_per_iteration;
        assert_eq!(success_count.load(Ordering::Relaxed), total_operations);
    }

    /// Test that multiple threads can safely create AEAD instances
    #[test]
    fn test_aead_creation_thread_safety() {
        const NUM_THREADS: usize = 5;
        const OPERATIONS_PER_THREAD: usize = 50;

        let success_count = Arc::new(AtomicUsize::new(0));
        let mut handles = Vec::new();

        for _ in 0..NUM_THREADS {
            let success_count = Arc::clone(&success_count);

            let handle = thread::spawn(move || {
                for _ in 0..OPERATIONS_PER_THREAD {
                    let algorithms = available_algorithms();

                    for algorithm in algorithms {
                        match create_aead(algorithm) {
                            Ok(aead) => {
                                // Test basic operations
                                let key = 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,
                                ]);
                                let nonce = Nonce::new(vec![
                                    0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC,
                                    0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
                                ]);
                                let plaintext = b"test message";

                                match aead.encrypt(&key, &nonce, plaintext, None) {
                                    Ok(_) => {
                                        success_count.fetch_add(1, Ordering::Relaxed);
                                    }
                                    Err(_) => {
                                        // Some algorithms might not support encryption
                                        // This is not a thread safety issue
                                    }
                                }
                            }
                            Err(_) => {
                                // Some algorithms might not be available
                                // This is not a thread safety issue
                            }
                        }
                    }
                }
            });

            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().expect("Thread should not panic");
        }

        // Verify that at least some operations succeeded
        assert!(success_count.load(Ordering::Relaxed) > 0);
    }

    /// Test concurrent access to registry with mixed operations
    #[test]
    fn test_mixed_operations_thread_safety() {
        const NUM_THREADS: usize = 8;
        const OPERATIONS_PER_THREAD: usize = 25;

        let success_count = Arc::new(AtomicUsize::new(0));
        let mut handles = Vec::new();

        for thread_id in 0..NUM_THREADS {
            let success_count = Arc::clone(&success_count);

            let handle = thread::spawn(move || {
                for _ in 0..OPERATIONS_PER_THREAD {
                    // Mix different operations
                    match thread_id % 4 {
                        0 => {
                            // Test algorithm listing
                            let algorithms = available_algorithms();
                            if !algorithms.is_empty() {
                                success_count.fetch_add(1, Ordering::Relaxed);
                            }
                        }
                        1 => {
                            // Test algorithm availability
                            let algorithms = available_algorithms();
                            for algorithm in algorithms {
                                if is_algorithm_available(algorithm) {
                                    success_count.fetch_add(1, Ordering::Relaxed);
                                }
                            }
                        }
                        2 => {
                            // Test metadata retrieval
                            let algorithms = available_algorithms();
                            for algorithm in algorithms {
                                if get_algorithm_metadata(algorithm).is_some() {
                                    success_count.fetch_add(1, Ordering::Relaxed);
                                }
                            }
                        }
                        3 => {
                            // Test AEAD creation
                            let algorithms = available_algorithms();
                            for algorithm in algorithms {
                                if create_aead(algorithm).is_ok() {
                                    success_count.fetch_add(1, Ordering::Relaxed);
                                }
                            }
                        }
                        _ => unreachable!(),
                    }

                    // Small delay to increase chance of race conditions
                    thread::sleep(Duration::from_millis(1));
                }
            });

            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().expect("Thread should not panic");
        }

        // Verify that operations succeeded
        assert!(success_count.load(Ordering::Relaxed) > 0);
    }

    /// Test that the registry is properly initialized in all threads
    #[test]
    fn test_registry_initialization_thread_safety() {
        const NUM_THREADS: usize = 20;

        let success_count = Arc::new(AtomicUsize::new(0));
        let mut handles = Vec::new();

        for _ in 0..NUM_THREADS {
            let success_count = Arc::clone(&success_count);

            let handle = thread::spawn(move || {
                // Access the registry multiple times to ensure it's properly initialized
                for _ in 0..10 {
                    let algorithms = available_algorithms();
                    if !algorithms.is_empty() {
                        success_count.fetch_add(1, Ordering::Relaxed);
                    }

                    // Test that the same algorithms are always available
                    let algorithms2 = available_algorithms();
                    if algorithms == algorithms2 {
                        success_count.fetch_add(1, Ordering::Relaxed);
                    }
                }
            });

            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().expect("Thread should not panic");
        }

        // Verify that all operations succeeded
        let expected_operations = NUM_THREADS * 10 * 2; // 2 operations per iteration
        assert_eq!(success_count.load(Ordering::Relaxed), expected_operations);
    }

    /// Test specific algorithms for thread safety
    #[test]
    fn test_specific_algorithms_thread_safety() {
        const NUM_THREADS: usize = 5;
        const OPERATIONS_PER_THREAD: usize = 20;

        // Test with specific known algorithms (feature-gated registrations)
        let test_algorithms = {
            #[allow(unused_mut)]
            let mut v = vec![Algorithm::Shake256Aead];
            #[cfg(feature = "saturnin")]
            v.push(Algorithm::Saturnin);
            #[cfg(feature = "duplex-sponge-aead")]
            v.push(Algorithm::DuplexSpongeAead);
            #[cfg(feature = "tweak-aead")]
            v.push(Algorithm::TweakAead);
            #[cfg(feature = "romulus-n")]
            v.push(Algorithm::RomulusN);
            #[cfg(feature = "romulus-m")]
            v.push(Algorithm::RomulusM);
            v
        };

        let success_count = Arc::new(AtomicUsize::new(0));
        let mut handles = Vec::new();

        for _ in 0..NUM_THREADS {
            let success_count = Arc::clone(&success_count);
            let algorithms = test_algorithms.clone();

            let handle = thread::spawn(move || {
                for _ in 0..OPERATIONS_PER_THREAD {
                    for algorithm in &algorithms {
                        // Test algorithm availability
                        if is_algorithm_available(*algorithm) {
                            success_count.fetch_add(1, Ordering::Relaxed);
                        }

                        // Test metadata retrieval
                        if get_algorithm_metadata(*algorithm).is_some() {
                            success_count.fetch_add(1, Ordering::Relaxed);
                        }

                        // Test AEAD creation
                        match create_aead(*algorithm) {
                            Ok(aead) => {
                                // Test basic operations
                                let key = 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,
                                ]);
                                let nonce = Nonce::new(vec![
                                    0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC,
                                    0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
                                ]);
                                let plaintext = b"thread safety test";

                                match aead.encrypt(&key, &nonce, plaintext, None) {
                                    Ok(ciphertext) => {
                                        // Test decryption
                                        match aead.decrypt(&key, &nonce, &ciphertext, None) {
                                            Ok(decrypted) => {
                                                if decrypted == plaintext {
                                                    success_count.fetch_add(1, Ordering::Relaxed);
                                                }
                                            }
                                            Err(_) => {
                                                // Decryption failure is not a thread safety issue
                                            }
                                        }
                                    }
                                    Err(_) => {
                                        // Encryption failure is not a thread safety issue
                                    }
                                }
                            }
                            Err(_) => {
                                // AEAD creation failure is not a thread safety issue
                            }
                        }
                    }
                }
            });

            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().expect("Thread should not panic");
        }

        // Verify that at least some operations succeeded
        assert!(success_count.load(Ordering::Relaxed) > 0);
    }

    /// Test stress scenario with many concurrent operations
    #[test]
    fn test_stress_thread_safety() {
        const NUM_THREADS: usize = 50;
        const OPERATIONS_PER_THREAD: usize = 10;

        let success_count = Arc::new(AtomicUsize::new(0));
        let error_count = Arc::new(AtomicUsize::new(0));
        let mut handles = Vec::new();

        for _ in 0..NUM_THREADS {
            let success_count = Arc::clone(&success_count);
            let error_count = Arc::clone(&error_count);

            let handle = thread::spawn(move || {
                for _ in 0..OPERATIONS_PER_THREAD {
                    // Perform various operations that could expose thread safety issues
                    let algorithms = available_algorithms();

                    for algorithm in algorithms {
                        // Test availability
                        if !is_algorithm_available(algorithm) {
                            error_count.fetch_add(1, Ordering::Relaxed);
                            continue;
                        }

                        // Test metadata retrieval
                        if get_algorithm_metadata(algorithm).is_none() {
                            error_count.fetch_add(1, Ordering::Relaxed);
                            continue;
                        }

                        // Test AEAD creation
                        match create_aead(algorithm) {
                            Ok(_) => {
                                success_count.fetch_add(1, Ordering::Relaxed);
                            }
                            Err(_) => {
                                error_count.fetch_add(1, Ordering::Relaxed);
                            }
                        }
                    }
                }
            });

            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().expect("Thread should not panic");
        }

        // Verify that most operations succeeded
        let total_operations =
            success_count.load(Ordering::Relaxed) + error_count.load(Ordering::Relaxed);
        let success_rate = success_count.load(Ordering::Relaxed) as f64 / total_operations as f64;

        // Allow for some errors due to algorithm unavailability, but most should succeed
        assert!(
            success_rate > 0.8,
            "Success rate was {:.2}%, expected > 80%",
            success_rate * 100.0
        );
    }
}

#[cfg(not(feature = "std"))]
mod thread_safety_tests {
    // Thread safety tests are not applicable in no_std environments
    #[test]
    fn test_no_std_thread_safety_placeholder() {
        // This test always passes in no_std environments
        // since threading is not available
        assert!(true);
    }
}