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
use super::*;
use hex;
use std::fs::File;
use std::io::{BufRead, BufReader};
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("sha3")
}

// Basic sanity check tests - keep these for quick development feedback
#[test]
fn test_sha3_256_empty() {
    // NIST test vector: Empty string
    let expected = "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a";

    let hash = Sha3_256::digest(&[]).unwrap();
    assert_eq!(hex::encode(&hash), expected);
}

#[test]
fn test_sha3_224_empty() {
    // NIST test vector: Empty string
    let expected = "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7";

    let hash = Sha3_224::digest(&[]).unwrap();
    assert_eq!(hex::encode(&hash), expected);
}

#[test]
fn test_sha3_384_empty() {
    // NIST test vector: Empty string
    let expected = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004";

    let hash = Sha3_384::digest(&[]).unwrap();
    assert_eq!(hex::encode(&hash), expected);
}

#[test]
fn test_sha3_512_empty() {
    // NIST test vector: Empty string
    let expected = "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26";

    let hash = Sha3_512::digest(&[]).unwrap();
    assert_eq!(hex::encode(&hash), expected);
}

#[derive(Debug)]
struct Sha3TestVector {
    len: usize,  // Bit length
    msg: String, // Hex-encoded message
    md: String,  // Hex-encoded digest (expected hash)
}

fn parse_sha3_test_file(filepath: &str) -> Vec<Sha3TestVector> {
    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<Sha3TestVector> = None;

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

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

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

            // Extract bit length
            let len = len_str.parse::<usize>().unwrap();

            current_vector = Some(Sha3TestVector {
                len,
                msg: String::new(),
                md: String::new(),
            });
        } else if let Some(ref mut vector) = current_vector {
            // Parse test vector data
            if let Some(msg) = line.strip_prefix("Msg = ") {
                vector.msg = msg.to_string();
            } else if let Some(md) = line.strip_prefix("MD = ") {
                vector.md = md.to_string();
            }
        }
    }

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

    test_vectors
}

// Modified with trait bounds to handle the type comparison
fn run_sha3_tests<H: HashFunction>(filepath: &str, name: &str) {
    let test_vectors = parse_sha3_test_file(filepath);

    for (i, test) in test_vectors.iter().enumerate() {
        // Check if bit length is 0
        if test.len == 0 {
            // If bit length is 0, use an empty message regardless of what's in the msg field
            let hash = H::digest(&[]).unwrap();

            // Convert expected hash to bytes
            let expected = hex::decode(&test.md)
                .unwrap_or_else(|_| panic!("Invalid hex in expected result {}: {}", i, test.md));

            // Convert the hash to Vec<u8> before comparison
            let hash_vec = hash.as_ref().to_vec();

            // Compare results
            assert_eq!(
                hash_vec,
                expected,
                "{} test case {} failed. Input: empty, Expected: {}, Got: {}",
                name,
                i,
                test.md,
                hex::encode(&hash)
            );

            continue;
        }

        // For non-zero bit lengths, convert hex string to bytes and proceed as before
        let msg = if test.msg.is_empty() {
            Vec::new()
        } else {
            hex::decode(&test.msg)
                .unwrap_or_else(|_| panic!("Invalid hex in test vector {}: {}", i, test.msg))
        };

        // Handle partial bytes if bit length is not a multiple of 8
        if test.len % 8 != 0 {
            let bytes = test.len / 8;
            let bits = test.len % 8;

            if bytes < msg.len() {
                let mut truncated_msg = msg[..bytes].to_vec();
                if bits > 0 {
                    // Keep only the specified number of bits in the last byte
                    let mask = (1u8 << bits) - 1;
                    truncated_msg.push(msg[bytes] & mask);
                }
                let hash = H::digest(&truncated_msg).unwrap();

                // Convert expected hash to bytes
                let expected = hex::decode(&test.md).unwrap_or_else(|_| {
                    panic!("Invalid hex in expected result {}: {}", i, test.md)
                });

                // Convert the hash to Vec<u8> before comparison
                let hash_vec = hash.as_ref().to_vec();

                // Compare results
                assert_eq!(
                    hash_vec,
                    expected,
                    "{} test case {} failed. Input: {}, Expected: {}, Got: {}",
                    name,
                    i,
                    test.msg,
                    test.md,
                    hex::encode(&hash)
                );

                continue;
            }
        }

        // Hash the message
        let hash = H::digest(&msg).unwrap();

        // Convert expected hash to bytes
        let expected = hex::decode(&test.md)
            .unwrap_or_else(|_| panic!("Invalid hex in expected result {}: {}", i, test.md));

        // Convert the hash to Vec<u8> before comparison
        let hash_vec = hash.as_ref().to_vec();

        // Compare results
        assert_eq!(
            hash_vec,
            expected,
            "{} test case {} failed. Input: {}, Expected: {}, Got: {}",
            name,
            i,
            test.msg,
            test.md,
            hex::encode(&hash)
        );
    }
}

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

    // Path to the test vector files
    let sha3_224_path = dir.join("SHA3_224ShortMsg.rsp");
    let sha3_256_path = dir.join("SHA3_256ShortMsg.rsp");
    let sha3_384_path = dir.join("SHA3_384ShortMsg.rsp");
    let sha3_512_path = dir.join("SHA3_512ShortMsg.rsp");

    // Check if files exist and FAIL if they don't
    for path in [
        &sha3_224_path,
        &sha3_256_path,
        &sha3_384_path,
        &sha3_512_path,
    ] {
        assert!(
            path.exists(),
            "Test vector file not found: {}",
            path.display()
        );
    }

    // Run the tests
    run_sha3_tests::<Sha3_224>(sha3_224_path.to_str().unwrap(), "SHA3-224");
    run_sha3_tests::<Sha3_256>(sha3_256_path.to_str().unwrap(), "SHA3-256");
    run_sha3_tests::<Sha3_384>(sha3_384_path.to_str().unwrap(), "SHA3-384");
    run_sha3_tests::<Sha3_512>(sha3_512_path.to_str().unwrap(), "SHA3-512");
}

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

    // Path to the long message test vector files
    let sha3_224_path = dir.join("SHA3_224LongMsg.rsp");
    let sha3_256_path = dir.join("SHA3_256LongMsg.rsp");
    let sha3_384_path = dir.join("SHA3_384LongMsg.rsp");
    let sha3_512_path = dir.join("SHA3_512LongMsg.rsp");

    // Check if files exist and FAIL if they don't
    for path in [
        &sha3_224_path,
        &sha3_256_path,
        &sha3_384_path,
        &sha3_512_path,
    ] {
        assert!(
            path.exists(),
            "Test vector file not found: {}",
            path.display()
        );
    }

    // Run the same test functions for long messages
    run_sha3_tests::<Sha3_224>(sha3_224_path.to_str().unwrap(), "SHA3-224");
    run_sha3_tests::<Sha3_256>(sha3_256_path.to_str().unwrap(), "SHA3-256");
    run_sha3_tests::<Sha3_384>(sha3_384_path.to_str().unwrap(), "SHA3-384");
    run_sha3_tests::<Sha3_512>(sha3_512_path.to_str().unwrap(), "SHA3-512");
}

#[derive(Debug)]
struct Sha3MonteTestVector {
    seed: String,     // Initial seed
    count: usize,     // Number of iterations
    expected: String, // Expected final MD value
}

fn parse_sha3_monte_test_file(filepath: &str) -> Vec<Sha3MonteTestVector> {
    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_seed = String::new();
    let mut current_expected = String::new();
    let mut count = 0;

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

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

        if let Some(seed) = line.strip_prefix("Seed = ") {
            // Start of a new test case
            if !current_seed.is_empty() && !current_expected.is_empty() {
                test_vectors.push(Sha3MonteTestVector {
                    seed: current_seed.clone(),
                    count,
                    expected: current_expected.clone(),
                });
            }

            current_seed = seed.to_string();
            current_expected = String::new();
            count = 0;
        } else if let Some(count_str) = line.strip_prefix("COUNT = ") {
            count = count_str.trim().parse::<usize>().unwrap_or(0);
        } else if line.starts_with("MD = ") && count == 100 {
            // Last iteration
            current_expected = line.strip_prefix("MD = ").unwrap().to_string();
        }
    }

    // Add the last test vector if present
    if !current_seed.is_empty() && !current_expected.is_empty() {
        test_vectors.push(Sha3MonteTestVector {
            seed: current_seed,
            count,
            expected: current_expected,
        });
    }

    test_vectors
}

// Modified to convert the hash function output to Vec<u8>
fn run_sha3_monte_tests<H: HashFunction>(filepath: &str, name: &str) {
    let test_vectors = parse_sha3_monte_test_file(filepath);

    for (i, test) in test_vectors.iter().enumerate() {
        // Convert seed to bytes
        let seed_bytes = hex::decode(&test.seed).unwrap_or_else(|_| {
            panic!(
                "{} Monte Carlo test {}: Invalid seed hex: {}",
                name, i, test.seed
            )
        });

        // Perform Monte Carlo test according to NIST procedure
        let mut md = seed_bytes.clone();

        // Perform the specified number of iterations (typically 100 for SHA-3)
        for _j in 0..=test.count {
            // Create a new hash instance
            let mut hasher = H::new();

            // Update with the current MD value
            hasher.update(&md).unwrap();

            // Generate the next MD value - convert to Vec<u8> to match md's type
            md = hasher.finalize().unwrap().as_ref().to_vec();
        }

        // Verify the final result matches the expected value
        let expected = hex::decode(&test.expected).unwrap_or_else(|_| {
            panic!(
                "{} Monte Carlo test {}: Invalid expected hex: {}",
                name, i, test.expected
            )
        });

        assert_eq!(
            hex::encode(&md),
            hex::encode(&expected),
            "{} Monte Carlo test case {} failed.\nExpected: {}\nGot: {}",
            name,
            i,
            test.expected,
            hex::encode(&md)
        );
    }
}

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

    // Path to the Monte Carlo test vector files
    let sha3_224_path = dir.join("SHA3_224Monte.rsp");
    let sha3_256_path = dir.join("SHA3_256Monte.rsp");
    let sha3_384_path = dir.join("SHA3_384Monte.rsp");
    let sha3_512_path = dir.join("SHA3_512Monte.rsp");

    // Check if files exist and FAIL if they don't
    for path in [
        &sha3_224_path,
        &sha3_256_path,
        &sha3_384_path,
        &sha3_512_path,
    ] {
        assert!(
            path.exists(),
            "Test vector file not found: {}",
            path.display()
        );
    }

    // Run Monte Carlo tests for each SHA-3 variant
    run_sha3_monte_tests::<Sha3_224>(sha3_224_path.to_str().unwrap(), "SHA3-224");
    run_sha3_monte_tests::<Sha3_256>(sha3_256_path.to_str().unwrap(), "SHA3-256");
    run_sha3_monte_tests::<Sha3_384>(sha3_384_path.to_str().unwrap(), "SHA3-384");
    run_sha3_monte_tests::<Sha3_512>(sha3_512_path.to_str().unwrap(), "SHA3-512");
}