dcrypt-algorithms 1.2.3

Cryptographic primitives for the dcrypt 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
use super::*;
use crate::block::aes::{Aes128, Aes192, Aes256};
use crate::types::Nonce;
use crate::types::SecretBytes;
use hex;
use std::path::{Path, PathBuf};

fn vectors_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("..") // up to crates/
        .join("..") // up to workspace root
        .join("tests")
        .join("src")
        .join("vectors")
        .join("legacy_rsp")
        .join("cbc")
}

#[test]
fn test_aes_cbc_nist_vectors() {
    let dir = vectors_dir();

    // Path to the test vector files
    let aes128_encrypt = dir.join("CBC-AES128-Encrypt.rsp");
    let aes128_decrypt = dir.join("CBC-AES128-Decrypt.rsp");
    let aes192_encrypt = dir.join("CBC-AES192-Encrypt.rsp");
    let aes192_decrypt = dir.join("CBC-AES192-Decrypt.rsp");
    let aes256_encrypt = dir.join("CBC-AES256-Encrypt.rsp");
    let aes256_decrypt = dir.join("CBC-AES256-Decrypt.rsp");

    // Check if files exist and provide helpful message if they don't
    for path in [
        &aes128_encrypt,
        &aes128_decrypt,
        &aes192_encrypt,
        &aes192_decrypt,
        &aes256_encrypt,
        &aes256_decrypt,
    ] {
        assert!(
            path.exists(),
            "Test vector file not found: {}",
            path.display()
        );
    }

    // Run the tests
    run_aes128_cbc_tests(aes128_encrypt.to_str().unwrap(), "AES-128 Encrypt", true);
    run_aes128_cbc_tests(aes128_decrypt.to_str().unwrap(), "AES-128 Decrypt", false);
    run_aes192_cbc_tests(aes192_encrypt.to_str().unwrap(), "AES-192 Encrypt", true);
    run_aes192_cbc_tests(aes192_decrypt.to_str().unwrap(), "AES-192 Decrypt", false);
    run_aes256_cbc_tests(aes256_encrypt.to_str().unwrap(), "AES-256 Encrypt", true);
    run_aes256_cbc_tests(aes256_decrypt.to_str().unwrap(), "AES-256 Decrypt", false);
}

#[derive(Debug)]
struct AesCbcTestVector {
    count: usize,
    key: String,        // Hex-encoded key
    iv: String,         // Hex-encoded IV
    plaintext: String,  // Hex-encoded plaintext
    ciphertext: String, // Hex-encoded ciphertext
}

fn parse_aes_cbc_test_file(filepath: &str) -> Vec<AesCbcTestVector> {
    use std::fs::File;
    use std::io::{BufRead, BufReader};
    use std::path::Path;

    let file = File::open(Path::new(filepath)).expect("Failed to open test vector file");
    let reader = BufReader::new(file);
    let mut lines = reader.lines();

    let mut test_vectors = Vec::new();
    let mut current_vector: Option<AesCbcTestVector> = None;
    let mut _is_encrypt_mode = false;

    while let Some(Ok(line)) = lines.next() {
        let line = line.trim();

        // Skip empty lines and comments
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        // Detect test mode (encryption or decryption)
        if line == "[ENCRYPT]" {
            _is_encrypt_mode = true;
            continue;
        } else if line == "[DECRYPT]" {
            _is_encrypt_mode = false;
            continue;
        }

        if let Some(count_str) = line.strip_prefix("COUNT = ") {
            // Start of a new test case
            if let Some(vector) = current_vector.take() {
                test_vectors.push(vector);
            }

            // Extract count
            let count = count_str.parse::<usize>().unwrap();

            current_vector = Some(AesCbcTestVector {
                count,
                key: String::new(),
                iv: String::new(),
                plaintext: String::new(),
                ciphertext: String::new(),
            });
        } else if let Some(ref mut vector) = current_vector {
            // Parse test vector data
            if let Some(key_str) = line.strip_prefix("KEY = ") {
                vector.key = key_str.to_string();
            } else if let Some(iv_str) = line.strip_prefix("IV = ") {
                vector.iv = iv_str.to_string();
            } else if let Some(plaintext_str) = line.strip_prefix("PLAINTEXT = ") {
                vector.plaintext = plaintext_str.to_string();
            } else if let Some(ciphertext_str) = line.strip_prefix("CIPHERTEXT = ") {
                vector.ciphertext = ciphertext_str.to_string();
            }
        }
    }

    // Add the last test vector if present
    if let Some(vector) = current_vector {
        test_vectors.push(vector);
    }

    test_vectors
}

// Test function for AES-128
fn run_aes128_cbc_tests(filepath: &str, name: &str, is_encrypt: bool) {
    let test_vectors = parse_aes_cbc_test_file(filepath);

    for (i, test) in test_vectors.iter().enumerate() {
        // Convert hex strings to bytes
        let key = hex::decode(&test.key)
            .unwrap_or_else(|_| panic!("Invalid hex key in test vector {}: {}", i, test.key));

        let iv_bytes = hex::decode(&test.iv)
            .unwrap_or_else(|_| panic!("Invalid hex IV in test vector {}: {}", i, test.iv));

        // Convert iv_bytes to Nonce<16>
        let mut iv_array = [0u8; 16];
        iv_array.copy_from_slice(&iv_bytes);
        let iv = Nonce::<16>::new(iv_array);

        // Ensure key has the expected size for AES-128
        assert_eq!(
            key.len(),
            16,
            "Key size mismatch for {} test case {} (COUNT={}). Expected: 16, Got: {}",
            name,
            i,
            test.count,
            key.len()
        );

        // Create AES-128 cipher and CBC mode
        let secret_key =
            SecretBytes::<16>::from_slice(&key).expect("Failed to create SecretBytes from key");
        let cipher = Aes128::new(&secret_key);
        let cbc = Cbc::new(cipher, &iv).unwrap();

        if is_encrypt {
            // Test encryption
            let plaintext = hex::decode(&test.plaintext).unwrap_or_else(|_| {
                panic!(
                    "Invalid hex plaintext in test vector {} (COUNT={}): {}",
                    i, test.count, test.plaintext
                )
            });

            let expected = hex::decode(&test.ciphertext).unwrap_or_else(|_| {
                panic!(
                    "Invalid hex ciphertext in test vector {} (COUNT={}): {}",
                    i, test.count, test.ciphertext
                )
            });

            let result = cbc.encrypt(&plaintext).unwrap();

            assert_eq!(
                result,
                expected,
                "{} test case {} (COUNT={}) failed.\nInput: {}\nExpected: {}\nGot: {}",
                name,
                i,
                test.count,
                test.plaintext,
                test.ciphertext,
                hex::encode(&result)
            );
        } else {
            // Test decryption
            let ciphertext = hex::decode(&test.ciphertext).unwrap_or_else(|_| {
                panic!(
                    "Invalid hex ciphertext in test vector {} (COUNT={}): {}",
                    i, test.count, test.ciphertext
                )
            });

            let expected = hex::decode(&test.plaintext).unwrap_or_else(|_| {
                panic!(
                    "Invalid hex plaintext in test vector {} (COUNT={}): {}",
                    i, test.count, test.plaintext
                )
            });

            let result = cbc.decrypt(&ciphertext).unwrap();

            assert_eq!(
                result,
                expected,
                "{} test case {} (COUNT={}) failed.\nInput: {}\nExpected: {}\nGot: {}",
                name,
                i,
                test.count,
                test.ciphertext,
                test.plaintext,
                hex::encode(&result)
            );
        }
    }
}

// Test function for AES-192
fn run_aes192_cbc_tests(filepath: &str, name: &str, is_encrypt: bool) {
    let test_vectors = parse_aes_cbc_test_file(filepath);

    for (i, test) in test_vectors.iter().enumerate() {
        // Convert hex strings to bytes
        let key = hex::decode(&test.key)
            .unwrap_or_else(|_| panic!("Invalid hex key in test vector {}: {}", i, test.key));

        let iv_bytes = hex::decode(&test.iv)
            .unwrap_or_else(|_| panic!("Invalid hex IV in test vector {}: {}", i, test.iv));

        // Convert iv_bytes to Nonce<16>
        let mut iv_array = [0u8; 16];
        iv_array.copy_from_slice(&iv_bytes);
        let iv = Nonce::<16>::new(iv_array);

        // Ensure key has the expected size for AES-192
        assert_eq!(
            key.len(),
            24,
            "Key size mismatch for {} test case {} (COUNT={}). Expected: 24, Got: {}",
            name,
            i,
            test.count,
            key.len()
        );

        // Create AES-192 cipher and CBC mode
        let secret_key =
            SecretBytes::<24>::from_slice(&key).expect("Failed to create SecretBytes from key");
        let cipher = Aes192::new(&secret_key);
        let cbc = Cbc::new(cipher, &iv).unwrap();

        if is_encrypt {
            // Test encryption
            let plaintext = hex::decode(&test.plaintext).unwrap_or_else(|_| {
                panic!(
                    "Invalid hex plaintext in test vector {} (COUNT={}): {}",
                    i, test.count, test.plaintext
                )
            });

            let expected = hex::decode(&test.ciphertext).unwrap_or_else(|_| {
                panic!(
                    "Invalid hex ciphertext in test vector {} (COUNT={}): {}",
                    i, test.count, test.ciphertext
                )
            });

            let result = cbc.encrypt(&plaintext).unwrap();

            assert_eq!(
                result,
                expected,
                "{} test case {} (COUNT={}) failed.\nInput: {}\nExpected: {}\nGot: {}",
                name,
                i,
                test.count,
                test.plaintext,
                test.ciphertext,
                hex::encode(&result)
            );
        } else {
            // Test decryption
            let ciphertext = hex::decode(&test.ciphertext).unwrap_or_else(|_| {
                panic!(
                    "Invalid hex ciphertext in test vector {} (COUNT={}): {}",
                    i, test.count, test.ciphertext
                )
            });

            let expected = hex::decode(&test.plaintext).unwrap_or_else(|_| {
                panic!(
                    "Invalid hex plaintext in test vector {} (COUNT={}): {}",
                    i, test.count, test.plaintext
                )
            });

            let result = cbc.decrypt(&ciphertext).unwrap();

            assert_eq!(
                result,
                expected,
                "{} test case {} (COUNT={}) failed.\nInput: {}\nExpected: {}\nGot: {}",
                name,
                i,
                test.count,
                test.ciphertext,
                test.plaintext,
                hex::encode(&result)
            );
        }
    }
}

// Test function for AES-256
fn run_aes256_cbc_tests(filepath: &str, name: &str, is_encrypt: bool) {
    let test_vectors = parse_aes_cbc_test_file(filepath);

    for (i, test) in test_vectors.iter().enumerate() {
        // Convert hex strings to bytes
        let key = hex::decode(&test.key)
            .unwrap_or_else(|_| panic!("Invalid hex key in test vector {}: {}", i, test.key));

        let iv_bytes = hex::decode(&test.iv)
            .unwrap_or_else(|_| panic!("Invalid hex IV in test vector {}: {}", i, test.iv));

        // Convert iv_bytes to Nonce<16>
        let mut iv_array = [0u8; 16];
        iv_array.copy_from_slice(&iv_bytes);
        let iv = Nonce::<16>::new(iv_array);

        // Ensure key has the expected size for AES-256
        assert_eq!(
            key.len(),
            32,
            "Key size mismatch for {} test case {} (COUNT={}). Expected: 32, Got: {}",
            name,
            i,
            test.count,
            key.len()
        );

        // Create AES-256 cipher and CBC mode
        let secret_key =
            SecretBytes::<32>::from_slice(&key).expect("Failed to create SecretBytes from key");
        let cipher = Aes256::new(&secret_key);
        let cbc = Cbc::new(cipher, &iv).unwrap();

        if is_encrypt {
            // Test encryption
            let plaintext = hex::decode(&test.plaintext).unwrap_or_else(|_| {
                panic!(
                    "Invalid hex plaintext in test vector {} (COUNT={}): {}",
                    i, test.count, test.plaintext
                )
            });

            let expected = hex::decode(&test.ciphertext).unwrap_or_else(|_| {
                panic!(
                    "Invalid hex ciphertext in test vector {} (COUNT={}): {}",
                    i, test.count, test.ciphertext
                )
            });

            let result = cbc.encrypt(&plaintext).unwrap();

            assert_eq!(
                result,
                expected,
                "{} test case {} (COUNT={}) failed.\nInput: {}\nExpected: {}\nGot: {}",
                name,
                i,
                test.count,
                test.plaintext,
                test.ciphertext,
                hex::encode(&result)
            );
        } else {
            // Test decryption
            let ciphertext = hex::decode(&test.ciphertext).unwrap_or_else(|_| {
                panic!(
                    "Invalid hex ciphertext in test vector {} (COUNT={}): {}",
                    i, test.count, test.ciphertext
                )
            });

            let expected = hex::decode(&test.plaintext).unwrap_or_else(|_| {
                panic!(
                    "Invalid hex plaintext in test vector {} (COUNT={}): {}",
                    i, test.count, test.plaintext
                )
            });

            let result = cbc.decrypt(&ciphertext).unwrap();

            assert_eq!(
                result,
                expected,
                "{} test case {} (COUNT={}) failed.\nInput: {}\nExpected: {}\nGot: {}",
                name,
                i,
                test.count,
                test.ciphertext,
                test.plaintext,
                hex::encode(&result)
            );
        }
    }
}

#[test]
fn test_aes_cbc() {
    // NIST SP 800-38A test vector F.2.1
    // Key: 2b7e151628aed2a6abf7158809cf4f3c
    // IV: 000102030405060708090a0b0c0d0e0f
    // Plaintext: 6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710
    // Ciphertext: 7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a7

    let key = hex::decode("2b7e151628aed2a6abf7158809cf4f3c").unwrap();
    let iv_bytes = hex::decode("000102030405060708090a0b0c0d0e0f").unwrap();

    // Convert iv_bytes to Nonce<16>
    let mut iv_array = [0u8; 16];
    iv_array.copy_from_slice(&iv_bytes);
    let iv = Nonce::<16>::new(iv_array);

    let plaintext = hex::decode("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710").unwrap();
    let expected_ciphertext = hex::decode("7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a7").unwrap();

    let secret_key =
        SecretBytes::<16>::from_slice(&key).expect("Failed to create SecretBytes from key");
    let cipher = Aes128::new(&secret_key);
    let cbc = Cbc::new(cipher, &iv).unwrap();

    let ciphertext = cbc.encrypt(&plaintext).unwrap();
    assert_eq!(ciphertext, expected_ciphertext);

    // Test decryption
    let secret_key =
        SecretBytes::<16>::from_slice(&key).expect("Failed to create SecretBytes from key");
    let cipher = Aes128::new(&secret_key);
    let cbc = Cbc::new(cipher, &iv).unwrap();
    let decrypted = cbc.decrypt(&ciphertext).unwrap();
    assert_eq!(decrypted, plaintext);
}

#[test]
fn test_cbc_multiple_blocks() {
    let key_array = [0x42; 16]; // 16-byte key for AES-128
    let iv_array = [0x24; 16]; // 16-byte IV
    let iv = Nonce::<16>::new(iv_array); // Create Nonce<16> from array

    // Generate a three-block plaintext
    let plaintext = vec![0xAA; 48]; // 3 blocks of 16 bytes

    let secret_key = SecretBytes::new(key_array);
    let cipher = Aes128::new(&secret_key);
    let cbc = Cbc::new(cipher, &iv).unwrap();

    let ciphertext = cbc.encrypt(&plaintext).unwrap();

    // Ensure the ciphertext is the same length as the plaintext
    assert_eq!(ciphertext.len(), plaintext.len());

    // Ensure the ciphertext is different from plaintext
    assert_ne!(ciphertext, plaintext);

    // Decrypt and verify
    let secret_key = SecretBytes::new(key_array);
    let cipher = Aes128::new(&secret_key);
    let cbc = Cbc::new(cipher, &iv).unwrap();
    let decrypted = cbc.decrypt(&ciphertext).unwrap();

    assert_eq!(decrypted, plaintext);
}