|
| 1 | +#[cfg(feature = "std")] |
| 2 | +use sanitization::ExpiringSecretBytes; |
| 3 | +#[cfg(all( |
| 4 | + feature = "guard-pages", |
| 5 | + target_os = "linux", |
| 6 | + any(target_arch = "x86_64", target_arch = "aarch64"), |
| 7 | + not(miri) |
| 8 | +))] |
| 9 | +use sanitization::GuardedSecretVec; |
| 10 | +#[cfg(all( |
| 11 | + feature = "memory-lock", |
| 12 | + target_os = "linux", |
| 13 | + any(target_arch = "x86_64", target_arch = "aarch64") |
| 14 | +))] |
| 15 | +use sanitization::LockedSecretBytes; |
| 16 | +#[cfg(all(feature = "cache-flush", target_arch = "x86_64", not(miri)))] |
| 17 | +use sanitization::{cache_flush::cache_flush_sanitize_bytes, SecretBytes}; |
| 18 | + |
| 19 | +fn main() { |
| 20 | + #[cfg(feature = "std")] |
| 21 | + { |
| 22 | + let mut key = |
| 23 | + ExpiringSecretBytes::<32>::from_array([7; 32], std::time::Duration::from_secs(300)); |
| 24 | + assert_eq!(key.try_constant_time_eq(&[7; 32]), Ok(true)); |
| 25 | + } |
| 26 | + |
| 27 | + #[cfg(all( |
| 28 | + feature = "memory-lock", |
| 29 | + target_os = "linux", |
| 30 | + any(target_arch = "x86_64", target_arch = "aarch64") |
| 31 | + ))] |
| 32 | + { |
| 33 | + let mut key = LockedSecretBytes::<32>::from_array([9; 32]).unwrap(); |
| 34 | + assert!(key.constant_time_eq(&[9; 32])); |
| 35 | + key.secure_clear(); |
| 36 | + } |
| 37 | + |
| 38 | + #[cfg(all(feature = "cache-flush", target_arch = "x86_64", not(miri)))] |
| 39 | + { |
| 40 | + let mut scratch = [0xA5; 32]; |
| 41 | + cache_flush_sanitize_bytes(&mut scratch); |
| 42 | + assert_eq!(scratch, [0; 32]); |
| 43 | + |
| 44 | + let mut key = SecretBytes::<32>::from_array([1; 32]); |
| 45 | + key.secure_clear_and_flush(); |
| 46 | + assert!(key.constant_time_eq(&[0; 32])); |
| 47 | + } |
| 48 | + |
| 49 | + #[cfg(all( |
| 50 | + feature = "guard-pages", |
| 51 | + target_os = "linux", |
| 52 | + any(target_arch = "x86_64", target_arch = "aarch64"), |
| 53 | + not(miri) |
| 54 | + ))] |
| 55 | + { |
| 56 | + let mut token = GuardedSecretVec::from_slice(b"session-key").unwrap(); |
| 57 | + token.extend_from_slice(b"-v2").unwrap(); |
| 58 | + assert_eq!(token.with_secret(|bytes| bytes.len()), 14); |
| 59 | + token.clear_secret(); |
| 60 | + } |
| 61 | +} |
0 commit comments