ash-core 2.3.6

DEPRECATED — Use `ashcore` instead. This crate is End of Life (EOL).
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Comprehensive Verification Tests
//!
//! These tests cover proof verification scenarios including:
//! - Valid proof verification
//! - Invalid proof detection
//! - Tampering detection
//! - Concurrent verification

use ash_core::{
    ash_build_proof, ash_verify_proof, ash_derive_client_secret,
    ash_hash_body, ash_timing_safe_equal,
};

const TEST_NONCE: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
const TEST_BODY_HASH: &str = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";

// =========================================================================
// VALID PROOF VERIFICATION
// =========================================================================

mod valid_proofs {
    use super::*;

    #[test]
    fn test_basic_proof_verification() {
        let context_id = "ctx_basic_001";
        let binding = "POST|/api/test|";
        let timestamp = "1700000000";

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, binding, TEST_BODY_HASH).unwrap();

        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, &proof).unwrap();
        assert!(valid);
    }

    #[test]
    fn test_verification_with_query_string() {
        let context_id = "ctx_query_001";
        let binding = "GET|/api/search|q=test&page=1";
        let timestamp = "1700000001";

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, binding, TEST_BODY_HASH).unwrap();

        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, &proof).unwrap();
        assert!(valid);
    }

    #[test]
    fn test_verification_with_json_body() {
        let context_id = "ctx_json_001";
        let binding = "POST|/api/users|";
        let timestamp = "1700000002";
        let body = r#"{"name":"John","age":30}"#;
        let body_hash = ash_hash_body(body);

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, binding, &body_hash).unwrap();

        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, &body_hash, &proof).unwrap();
        assert!(valid);
    }

    #[test]
    fn test_verification_different_methods() {
        let methods = ["GET", "POST", "PUT", "DELETE", "PATCH"];

        for method in methods {
            let context_id = format!("ctx_{}", method.to_lowercase());
            let binding = format!("{}|/api/resource|", method);
            let timestamp = "1700000003";

            let secret = ash_derive_client_secret(TEST_NONCE, &context_id, &binding).unwrap();
            let proof = ash_build_proof(&secret, timestamp, &binding, TEST_BODY_HASH).unwrap();

            let valid = ash_verify_proof(TEST_NONCE, &context_id, &binding, timestamp, TEST_BODY_HASH, &proof).unwrap();
            assert!(valid, "Failed for method {}", method);
        }
    }

    #[test]
    fn test_verification_with_empty_body() {
        let context_id = "ctx_empty_001";
        let binding = "GET|/api/health|";
        let timestamp = "1700000004";
        let body_hash = ash_hash_body("");

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, binding, &body_hash).unwrap();

        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, &body_hash, &proof).unwrap();
        assert!(valid);
    }

    #[test]
    fn test_verification_multiple_times() {
        let context_id = "ctx_multi_001";
        let binding = "POST|/api/test|";
        let timestamp = "1700000005";

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, binding, TEST_BODY_HASH).unwrap();

        // Verify multiple times - should always succeed
        for _ in 0..10 {
            let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, &proof).unwrap();
            assert!(valid);
        }
    }
}

// =========================================================================
// INVALID PROOF DETECTION
// =========================================================================

mod invalid_proofs {
    use super::*;

    #[test]
    fn test_wrong_proof_rejected() {
        let context_id = "ctx_wrong_001";
        let binding = "POST|/api/test|";
        let timestamp = "1700000010";
        let wrong_proof = "0".repeat(64);

        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, &wrong_proof).unwrap();
        assert!(!valid);
    }

    #[test]
    fn test_random_proof_rejected() {
        let context_id = "ctx_random_001";
        let binding = "POST|/api/test|";
        let timestamp = "1700000011";
        let random_proof = "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890";

        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, &random_proof).unwrap();
        assert!(!valid);
    }

    #[test]
    fn test_truncated_proof_rejected() {
        let context_id = "ctx_truncated_001";
        let binding = "POST|/api/test|";
        let timestamp = "1700000012";

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let full_proof = ash_build_proof(&secret, timestamp, binding, TEST_BODY_HASH).unwrap();
        let truncated = &full_proof[..32]; // Only half

        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, truncated).unwrap();
        assert!(!valid);
    }

    #[test]
    fn test_extended_proof_rejected() {
        let context_id = "ctx_extended_001";
        let binding = "POST|/api/test|";
        let timestamp = "1700000013";

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let full_proof = ash_build_proof(&secret, timestamp, binding, TEST_BODY_HASH).unwrap();
        let extended = format!("{}extra", full_proof);

        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, &extended).unwrap();
        assert!(!valid);
    }

    #[test]
    fn test_empty_proof_rejected() {
        let context_id = "ctx_empty_proof_001";
        let binding = "POST|/api/test|";
        let timestamp = "1700000014";

        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, "").unwrap();
        assert!(!valid);
    }

    #[test]
    fn test_case_sensitive_proof() {
        let context_id = "ctx_case_001";
        let binding = "POST|/api/test|";
        let timestamp = "1700000015";

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, binding, TEST_BODY_HASH).unwrap();
        let upper_proof = proof.to_uppercase();

        // If proof was originally lowercase, uppercase should fail
        if proof != upper_proof {
            let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, &upper_proof).unwrap();
            assert!(!valid);
        }
    }
}

// =========================================================================
// TAMPERING DETECTION
// =========================================================================

mod tampering_detection {
    use super::*;

    #[test]
    fn test_tampered_body_detected() {
        let context_id = "ctx_tamper_body_001";
        let binding = "POST|/api/test|";
        let timestamp = "1700000020";

        let original_body = r#"{"amount":100}"#;
        let tampered_body = r#"{"amount":1000}"#;
        let original_hash = ash_hash_body(original_body);
        let tampered_hash = ash_hash_body(tampered_body);

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, binding, &original_hash).unwrap();

        // Original should verify
        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, &original_hash, &proof).unwrap();
        assert!(valid);

        // Tampered should fail
        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, &tampered_hash, &proof).unwrap();
        assert!(!valid);
    }

    #[test]
    fn test_tampered_binding_detected() {
        let context_id = "ctx_tamper_binding_001";
        let original_binding = "POST|/api/users|";
        let tampered_binding = "POST|/api/admin|";
        let timestamp = "1700000021";

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, original_binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, original_binding, TEST_BODY_HASH).unwrap();

        // Original should verify
        let valid = ash_verify_proof(TEST_NONCE, context_id, original_binding, timestamp, TEST_BODY_HASH, &proof).unwrap();
        assert!(valid);

        // Tampered binding should fail
        let valid = ash_verify_proof(TEST_NONCE, context_id, tampered_binding, timestamp, TEST_BODY_HASH, &proof).unwrap();
        assert!(!valid);
    }

    #[test]
    fn test_tampered_timestamp_detected() {
        let context_id = "ctx_tamper_ts_001";
        let binding = "POST|/api/test|";
        let original_timestamp = "1700000022";
        let tampered_timestamp = "1700000023";

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let proof = ash_build_proof(&secret, original_timestamp, binding, TEST_BODY_HASH).unwrap();

        // Original should verify
        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, original_timestamp, TEST_BODY_HASH, &proof).unwrap();
        assert!(valid);

        // Tampered timestamp should fail
        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, tampered_timestamp, TEST_BODY_HASH, &proof).unwrap();
        assert!(!valid);
    }

    #[test]
    fn test_tampered_context_id_detected() {
        let original_context = "ctx_tamper_ctx_001";
        let tampered_context = "ctx_tamper_ctx_002";
        let binding = "POST|/api/test|";
        let timestamp = "1700000024";

        let secret = ash_derive_client_secret(TEST_NONCE, original_context, binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, binding, TEST_BODY_HASH).unwrap();

        // Original should verify
        let valid = ash_verify_proof(TEST_NONCE, original_context, binding, timestamp, TEST_BODY_HASH, &proof).unwrap();
        assert!(valid);

        // Different context should fail
        let valid = ash_verify_proof(TEST_NONCE, tampered_context, binding, timestamp, TEST_BODY_HASH, &proof).unwrap();
        assert!(!valid);
    }

    #[test]
    fn test_tampered_nonce_detected() {
        let context_id = "ctx_tamper_nonce_001";
        let binding = "POST|/api/test|";
        let timestamp = "1700000025";
        let different_nonce = "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210";

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, binding, TEST_BODY_HASH).unwrap();

        // Original should verify
        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, &proof).unwrap();
        assert!(valid);

        // Different nonce should fail
        let valid = ash_verify_proof(different_nonce, context_id, binding, timestamp, TEST_BODY_HASH, &proof).unwrap();
        assert!(!valid);
    }

    #[test]
    fn test_single_bit_change_in_proof_detected() {
        let context_id = "ctx_bit_001";
        let binding = "POST|/api/test|";
        let timestamp = "1700000026";

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, binding, TEST_BODY_HASH).unwrap();

        // Change one character in the proof
        let mut modified = proof.clone();
        let first_char = modified.chars().next().unwrap();
        let new_char = if first_char == '0' { '1' } else { '0' };
        modified.replace_range(0..1, &new_char.to_string());

        let valid = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, &modified).unwrap();
        assert!(!valid);
    }

    #[test]
    fn test_method_case_tampering() {
        let context_id = "ctx_method_case_001";
        let original_binding = "POST|/api/test|";
        let tampered_binding = "post|/api/test|"; // lowercase method
        let timestamp = "1700000027";

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, original_binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, original_binding, TEST_BODY_HASH).unwrap();

        // Tampered (lowercase) binding should fail
        let valid = ash_verify_proof(TEST_NONCE, context_id, tampered_binding, timestamp, TEST_BODY_HASH, &proof).unwrap();
        assert!(!valid);
    }
}

// =========================================================================
// CONCURRENT VERIFICATION
// =========================================================================

mod concurrent_verification {
    use super::*;
    use std::thread;
    use std::sync::Arc;

    #[test]
    fn test_concurrent_verification_same_proof() {
        let context_id = "ctx_concurrent_001";
        let binding = "POST|/api/test|";
        let timestamp = "1700000030";

        let secret = ash_derive_client_secret(TEST_NONCE, context_id, binding).unwrap();
        let proof = ash_build_proof(&secret, timestamp, binding, TEST_BODY_HASH).unwrap();

        let proof = Arc::new(proof);
        let mut handles = vec![];

        for _ in 0..10 {
            let proof = Arc::clone(&proof);
            let handle = thread::spawn(move || {
                let valid = ash_verify_proof(
                    TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, &proof
                ).unwrap();
                assert!(valid);
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }
    }

    #[test]
    fn test_concurrent_verification_different_proofs() {
        let binding = "POST|/api/test|";
        let base_timestamp = 1700000040u64;

        let mut handles = vec![];

        for i in 0..10 {
            let context_id = format!("ctx_concurrent_{}", i);
            let timestamp = (base_timestamp + i as u64).to_string();

            let handle = thread::spawn(move || {
                let secret = ash_derive_client_secret(TEST_NONCE, &context_id, binding).unwrap();
                let proof = ash_build_proof(&secret, &timestamp, binding, TEST_BODY_HASH).unwrap();

                let valid = ash_verify_proof(
                    TEST_NONCE, &context_id, binding, &timestamp, TEST_BODY_HASH, &proof
                ).unwrap();
                assert!(valid);
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }
    }

    #[test]
    fn test_concurrent_hash_computation() {
        let mut handles = vec![];

        for i in 0..20 {
            let handle = thread::spawn(move || {
                let body = format!("body content {}", i);
                let hash = ash_hash_body(&body);
                assert_eq!(hash.len(), 64);
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }
    }
}

// =========================================================================
// TIMING SAFE COMPARISON TESTS
// =========================================================================

mod timing_safe {
    use super::*;

    #[test]
    fn test_timing_safe_equal_strings() {
        let a = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
        let b = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";

        assert!(ash_timing_safe_equal(a.as_bytes(), b.as_bytes()));
    }

    #[test]
    fn test_timing_safe_different_strings() {
        let a = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
        let b = "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";

        assert!(!ash_timing_safe_equal(a.as_bytes(), b.as_bytes()));
    }

    #[test]
    fn test_timing_safe_different_lengths() {
        let a = "short";
        let b = "much longer string";

        assert!(!ash_timing_safe_equal(a.as_bytes(), b.as_bytes()));
    }

    #[test]
    fn test_timing_safe_empty_strings() {
        assert!(ash_timing_safe_equal(b"", b""));
    }

    #[test]
    fn test_timing_safe_one_empty() {
        assert!(!ash_timing_safe_equal(b"", b"not empty"));
        assert!(!ash_timing_safe_equal(b"not empty", b""));
    }

    #[test]
    fn test_timing_safe_single_char_difference() {
        let a = "abcdefghij";
        let b = "abcdefghik";

        assert!(!ash_timing_safe_equal(a.as_bytes(), b.as_bytes()));
    }
}

// =========================================================================
// ERROR HANDLING TESTS
// =========================================================================

mod error_handling {
    use super::*;

    #[test]
    fn test_invalid_timestamp_format() {
        let context_id = "ctx_err_001";
        let binding = "POST|/api/test|";
        let invalid_timestamp = "not_a_number";

        let result = ash_verify_proof(TEST_NONCE, context_id, binding, invalid_timestamp, TEST_BODY_HASH, "x".repeat(64).as_str());
        assert!(result.is_err());
    }

    #[test]
    fn test_timestamp_with_leading_zeros_rejected() {
        let context_id = "ctx_err_002";
        let binding = "POST|/api/test|";
        let timestamp = "0123456789";

        let result = ash_verify_proof(TEST_NONCE, context_id, binding, timestamp, TEST_BODY_HASH, "x".repeat(64).as_str());
        assert!(result.is_err());
    }

    #[test]
    fn test_short_nonce_rejected() {
        let short_nonce = "abc123";
        let context_id = "ctx_err_003";
        let binding = "POST|/api/test|";

        let result = ash_derive_client_secret(short_nonce, context_id, binding);
        assert!(result.is_err());
    }

    #[test]
    fn test_non_hex_nonce_rejected() {
        let non_hex = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
        let context_id = "ctx_err_004";
        let binding = "POST|/api/test|";

        let result = ash_derive_client_secret(non_hex, context_id, binding);
        assert!(result.is_err());
    }

    #[test]
    fn test_empty_context_id_rejected() {
        let binding = "POST|/api/test|";

        let result = ash_derive_client_secret(TEST_NONCE, "", binding);
        assert!(result.is_err());
    }
}