soft-aes 0.2.2

A Rust-based software library for AES.
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
//! Test Suite for AES ECB Implementation Against NIST AESAVS
//!
//! This module contains tests for validating the AES ECB (Electronic Codebook)
//! mode implementation against the Known Answer Tests (KAT) provided by The
//! Advanced Encryption Standard Algorithm Validation Suite (AESAVS) released
//! on November 15, 2002.
//!
//! The tests are designed to ensure that the AES ECB implementation conforms to
//! the standards and recommendations set forth by the National Institute of
//! Standards and Technology (NIST), particularly as detailed by Lawrence E.
//! Bassham III from the NIST Information Technology Laboratory, Computer
//! Security Division.
//!
//! The AESAVS document serves as a guideline and basis for these tests and can
//! be found at:
//! https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/aes/AESAVS.pdf
//!
//! # Tests Overview
//!
//! - The tests cover various aspects of AES ECB mode, including encryption and
//!   decryption processes, for different key sizes (128, 192, and 256 bits).
//! - Test vectors used in these tests include plaintexts, ciphertexts, and
//!   keys as specified in the AESAVS documentation.
//! - Both encryption and decryption functionalities are tested against fixed
//!   plaintexts and varying keys, as well as varying plaintexts with fixed keys,
//!   to ensure comprehensive coverage and conformance with the AES standard.
//!
//! The test cases in this module are critical for verifying the correctness and
//! reliability of the AES ECB implementation, especially for applications
//! requiring cryptographic standards compliance and robust data security.

use crate::aes::{aes_dec_ecb, aes_enc_ecb};

use hex;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;

#[test]
fn test_aes_enc_ecb_vartxt_kat_aes_128() {
    let key = [0u8; 16]; // All zeros key

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let plaintext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_d_1_vartxt_kat_keysize_128_pt_values.txt");
    let ciphertext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_d_1_vartxt_kat_keysize_128_ct_values.txt");

    let plaintext_lines = read_lines(plaintext_file).expect("Failed to read plaintext file");
    let ciphertext_lines = read_lines(ciphertext_file).expect("Failed to read ciphertext file");

    for (plaintext_str, expected_ciphertext_str) in plaintext_lines.zip(ciphertext_lines) {
        let plaintext_hex = plaintext_str.expect("Error reading plaintext line");
        let expected_ciphertext_hex =
            expected_ciphertext_str.expect("Error reading ciphertext line");

        let plaintext = hex::decode(plaintext_hex).expect("Failed to decode plaintext hex");
        let expected_ciphertext =
            hex::decode(expected_ciphertext_hex).expect("Failed to decode ciphertext hex");

        let ciphertext = aes_enc_ecb(&plaintext, &key, None).expect("Encryption failed");

        assert_eq!(
            ciphertext, expected_ciphertext,
            "Ciphertext does not match expected value"
        );
    }
}

#[test]
fn test_aes_enc_ecb_vartxt_kat_aes_192() {
    let key = [0u8; 24]; // All zeros key

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let plaintext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_d_2_vartxt_kat_keysize_192_pt_values.txt");
    let ciphertext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_d_2_vartxt_kat_keysize_192_ct_values.txt");

    let plaintext_lines = read_lines(plaintext_file).expect("Failed to read plaintext file");
    let ciphertext_lines = read_lines(ciphertext_file).expect("Failed to read ciphertext file");

    for (plaintext_str, expected_ciphertext_str) in plaintext_lines.zip(ciphertext_lines) {
        let plaintext_hex = plaintext_str.expect("Error reading plaintext line");
        let expected_ciphertext_hex =
            expected_ciphertext_str.expect("Error reading ciphertext line");

        let plaintext = hex::decode(plaintext_hex).expect("Failed to decode plaintext hex");
        let expected_ciphertext =
            hex::decode(expected_ciphertext_hex).expect("Failed to decode ciphertext hex");

        let ciphertext = aes_enc_ecb(&plaintext, &key, None).expect("Encryption failed");

        assert_eq!(
            ciphertext, expected_ciphertext,
            "Ciphertext does not match expected value"
        );
    }
}

#[test]
fn test_aes_enc_ecb_vartxt_kat_aes_256() {
    let key = [0u8; 32]; // All zeros key

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let plaintext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_d_3_vartxt_kat_keysize_256_pt_values.txt");
    let ciphertext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_d_3_vartxt_kat_keysize_256_ct_values.txt");

    let plaintext_lines = read_lines(plaintext_file).expect("Failed to read plaintext file");
    let ciphertext_lines = read_lines(ciphertext_file).expect("Failed to read ciphertext file");

    for (plaintext_str, expected_ciphertext_str) in plaintext_lines.zip(ciphertext_lines) {
        let plaintext_hex = plaintext_str.expect("Error reading plaintext line");
        let expected_ciphertext_hex =
            expected_ciphertext_str.expect("Error reading ciphertext line");

        let plaintext = hex::decode(plaintext_hex).expect("Failed to decode plaintext hex");
        let expected_ciphertext =
            hex::decode(expected_ciphertext_hex).expect("Failed to decode ciphertext hex");

        let ciphertext = aes_enc_ecb(&plaintext, &key, None).expect("Encryption failed");

        assert_eq!(
            ciphertext, expected_ciphertext,
            "Ciphertext does not match expected value"
        );
    }
}

#[test]
fn test_aes_dec_ecb_vartxt_kat_aes_128() {
    let key = [0u8; 16]; // All zeros key

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let ciphertext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_d_1_vartxt_kat_keysize_128_ct_values.txt");
    let plaintext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_d_1_vartxt_kat_keysize_128_pt_values.txt");

    let ciphertext_lines = read_lines(ciphertext_file).expect("Failed to read ciphertext file");
    let plaintext_lines = read_lines(plaintext_file).expect("Failed to read plaintext file");

    for (ciphertext_str, expected_plaintext_str) in ciphertext_lines.zip(plaintext_lines) {
        let ciphertext_hex = ciphertext_str.expect("Error reading ciphertext line");
        let expected_plaintext_hex = expected_plaintext_str.expect("Error reading plaintext line");

        let ciphertext = hex::decode(ciphertext_hex).expect("Failed to decode ciphertext hex");
        let expected_plaintext =
            hex::decode(expected_plaintext_hex).expect("Failed to decode plaintext hex");

        let plaintext = aes_dec_ecb(&ciphertext, &key, None).expect("Decryption failed");

        assert_eq!(
            plaintext, expected_plaintext,
            "Plaintext does not match expected value"
        );
    }
}

#[test]
fn test_aes_dec_ecb_vartxt_kat_aes_192() {
    let key = [0u8; 24]; // All zeros key

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let ciphertext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_d_2_vartxt_kat_keysize_192_ct_values.txt");
    let plaintext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_d_2_vartxt_kat_keysize_192_pt_values.txt");

    let ciphertext_lines = read_lines(ciphertext_file).expect("Failed to read ciphertext file");
    let plaintext_lines = read_lines(plaintext_file).expect("Failed to read plaintext file");

    for (ciphertext_str, expected_plaintext_str) in ciphertext_lines.zip(plaintext_lines) {
        let ciphertext_hex = ciphertext_str.expect("Error reading ciphertext line");
        let expected_plaintext_hex = expected_plaintext_str.expect("Error reading plaintext line");

        let ciphertext = hex::decode(ciphertext_hex).expect("Failed to decode ciphertext hex");
        let expected_plaintext =
            hex::decode(expected_plaintext_hex).expect("Failed to decode plaintext hex");

        let plaintext = aes_dec_ecb(&ciphertext, &key, None).expect("Decryption failed");

        assert_eq!(
            plaintext, expected_plaintext,
            "Plaintext does not match expected value"
        );
    }
}

#[test]
fn test_aes_dec_ecb_vartxt_kat_aes_256() {
    let key = [0u8; 32]; // All zeros key

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let ciphertext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_d_3_vartxt_kat_keysize_256_ct_values.txt");
    let plaintext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_d_3_vartxt_kat_keysize_256_pt_values.txt");

    let ciphertext_lines = read_lines(ciphertext_file).expect("Failed to read ciphertext file");
    let plaintext_lines = read_lines(plaintext_file).expect("Failed to read plaintext file");

    for (ciphertext_str, expected_plaintext_str) in ciphertext_lines.zip(plaintext_lines) {
        let ciphertext_hex = ciphertext_str.expect("Error reading ciphertext line");
        let expected_plaintext_hex = expected_plaintext_str.expect("Error reading plaintext line");

        let ciphertext = hex::decode(ciphertext_hex).expect("Failed to decode ciphertext hex");
        let expected_plaintext =
            hex::decode(expected_plaintext_hex).expect("Failed to decode plaintext hex");

        let plaintext = aes_dec_ecb(&ciphertext, &key, None).expect("Decryption failed");

        assert_eq!(
            plaintext, expected_plaintext,
            "Plaintext does not match expected value"
        );
    }
}

#[test]
fn test_aes_enc_ecb_varkey_kat_aes_128() {
    let plaintext = [0u8; 16]; // All zeros plaintext

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let key_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_e_1_varkey_kat_keysize_128_key_values.txt");
    let ciphertext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_e_1_varkey_kat_keysize_128_ct_values.txt");

    let key_lines = read_lines(key_file).expect("Failed to read key file");
    let ciphertext_lines = read_lines(ciphertext_file).expect("Failed to read ciphertext file");

    for (key_str, expected_ciphertext_str) in key_lines.zip(ciphertext_lines) {
        let key_hex = key_str.expect("Error reading key line");
        let expected_ciphertext_hex =
            expected_ciphertext_str.expect("Error reading ciphertext line");

        let key = hex::decode(key_hex).expect("Failed to decode key hex");
        let expected_ciphertext =
            hex::decode(expected_ciphertext_hex).expect("Failed to decode ciphertext hex");

        let ciphertext = aes_enc_ecb(&plaintext, &key, None).expect("Encryption failed");

        assert_eq!(
            ciphertext, expected_ciphertext,
            "Ciphertext does not match expected value"
        );
    }
}

#[test]
fn test_aes_enc_ecb_varkey_kat_aes_192() {
    let plaintext = [0u8; 16]; // All zeros plaintext

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let key_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_e_2_varkey_kat_keysize_192_key_values.txt");
    let ciphertext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_e_2_varkey_kat_keysize_192_ct_values.txt");

    let key_lines = read_lines(key_file).expect("Failed to read key file");
    let ciphertext_lines = read_lines(ciphertext_file).expect("Failed to read ciphertext file");

    for (key_str, expected_ciphertext_str) in key_lines.zip(ciphertext_lines) {
        let key_hex = key_str.expect("Error reading key line");
        let expected_ciphertext_hex =
            expected_ciphertext_str.expect("Error reading ciphertext line");

        let key = hex::decode(key_hex).expect("Failed to decode key hex");
        let expected_ciphertext =
            hex::decode(expected_ciphertext_hex).expect("Failed to decode ciphertext hex");

        let ciphertext = aes_enc_ecb(&plaintext, &key, None).expect("Encryption failed");

        assert_eq!(
            ciphertext, expected_ciphertext,
            "Ciphertext does not match expected value"
        );
    }
}

#[test]
fn test_aes_enc_ecb_varkey_kat_aes_256() {
    let plaintext = [0u8; 16]; // All zeros plaintext

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let key_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_e_3_varkey_kat_keysize_256_key_values.txt");
    let ciphertext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_e_3_varkey_kat_keysize_256_ct_values.txt");

    let key_lines = read_lines(key_file).expect("Failed to read key file");
    let ciphertext_lines = read_lines(ciphertext_file).expect("Failed to read ciphertext file");

    for (key_str, expected_ciphertext_str) in key_lines.zip(ciphertext_lines) {
        let key_hex = key_str.expect("Error reading key line");
        let expected_ciphertext_hex =
            expected_ciphertext_str.expect("Error reading ciphertext line");

        let key = hex::decode(key_hex).expect("Failed to decode key hex");
        let expected_ciphertext =
            hex::decode(expected_ciphertext_hex).expect("Failed to decode ciphertext hex");

        let ciphertext = aes_enc_ecb(&plaintext, &key, None).expect("Encryption failed");

        assert_eq!(
            ciphertext, expected_ciphertext,
            "Ciphertext does not match expected value"
        );
    }
}

#[test]
fn test_aes_dec_ecb_varkey_kat_aes_128() {
    let expected_plaintext = [0u8; 16]; // All zeros plaintext

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let key_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_e_1_varkey_kat_keysize_128_key_values.txt");
    let ciphertext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_e_1_varkey_kat_keysize_128_ct_values.txt");

    let key_lines = read_lines(key_file).expect("Failed to read key file");
    let ciphertext_lines = read_lines(ciphertext_file).expect("Failed to read ciphertext file");

    for (key_str, ciphertext_str) in key_lines.zip(ciphertext_lines) {
        let key_hex = key_str.expect("Error reading key line");
        let ciphertext_hex = ciphertext_str.expect("Error reading ciphertext line");

        let key = hex::decode(key_hex).expect("Failed to decode key hex");
        let ciphertext = hex::decode(ciphertext_hex).expect("Failed to decode ciphertext hex");

        let plaintext = aes_dec_ecb(&ciphertext, &key, None).expect("Decryption failed");

        assert_eq!(
            plaintext, expected_plaintext,
            "Plaintext does not match expected value"
        );
    }
}

#[test]
fn test_aes_dec_ecb_varkey_kat_aes_192() {
    let expected_plaintext = [0u8; 16]; // All zeros plaintext

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let key_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_e_2_varkey_kat_keysize_192_key_values.txt");
    let ciphertext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_e_2_varkey_kat_keysize_192_ct_values.txt");

    let key_lines = read_lines(key_file).expect("Failed to read key file");
    let ciphertext_lines = read_lines(ciphertext_file).expect("Failed to read ciphertext file");

    for (key_str, ciphertext_str) in key_lines.zip(ciphertext_lines) {
        let key_hex = key_str.expect("Error reading key line");
        let ciphertext_hex = ciphertext_str.expect("Error reading ciphertext line");

        let key = hex::decode(key_hex).expect("Failed to decode key hex");
        let ciphertext = hex::decode(ciphertext_hex).expect("Failed to decode ciphertext hex");

        let plaintext = aes_dec_ecb(&ciphertext, &key, None).expect("Decryption failed");

        assert_eq!(
            plaintext, expected_plaintext,
            "Plaintext does not match expected value"
        );
    }
}

#[test]
fn test_aes_dec_ecb_varkey_kat_aes_256() {
    let expected_plaintext = [0u8; 16]; // All zeros plaintext

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let key_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_e_3_varkey_kat_keysize_256_key_values.txt");
    let ciphertext_file = Path::new(manifest_dir)
        .join("src/aes/tests/nist/aesavs_appendix_e_3_varkey_kat_keysize_256_ct_values.txt");

    let key_lines = read_lines(key_file).expect("Failed to read key file");
    let ciphertext_lines = read_lines(ciphertext_file).expect("Failed to read ciphertext file");

    for (key_str, ciphertext_str) in key_lines.zip(ciphertext_lines) {
        let key_hex = key_str.expect("Error reading key line");
        let ciphertext_hex = ciphertext_str.expect("Error reading ciphertext line");

        let key = hex::decode(key_hex).expect("Failed to decode key hex");
        let ciphertext = hex::decode(ciphertext_hex).expect("Failed to decode ciphertext hex");

        let plaintext = aes_dec_ecb(&ciphertext, &key, None).expect("Decryption failed");

        assert_eq!(
            plaintext, expected_plaintext,
            "Plaintext does not match expected value"
        );
    }
}

fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
    P: AsRef<Path>,
{
    let file = File::open(filename)?;
    Ok(io::BufReader::new(file).lines())
}