|
| 1 | +// Copyright (c) 2025 Arista Networks, Inc. |
| 2 | +// Use of this source code is governed by the Apache License 2.0 |
| 3 | +// that can be found in the LICENSE file. |
| 4 | + |
| 5 | +use rand::Rng; |
| 6 | + |
| 7 | +const SIMPLE_7_SEED: &[u8] = b"dsfd;kfoA,.iyewrkldJKDHSUBsgvca69834ncxv9873254k;fg87"; |
| 8 | + |
| 9 | +#[derive(Debug, derive_more::Display, derive_more::From)] |
| 10 | +pub enum Simple7Error { |
| 11 | + #[display("Invalid salt format in encrypted data")] |
| 12 | + InvalidSaltFormat(std::num::ParseIntError), |
| 13 | + #[display("Invalid hex encoding in encrypted data")] |
| 14 | + InvalidHexEncoding(hex::FromHexError), |
| 15 | + #[display("Decrypted data is not valid UTF-8")] |
| 16 | + InvalidUtf8(std::string::FromUtf8Error), |
| 17 | + #[display("Salt must be in the range 0-15, got {_0}")] |
| 18 | + InvalidSaltValue(u8), |
| 19 | + #[display("Encrypted data too short (minimum 2 characters required for salt)")] |
| 20 | + DataTooShort, |
| 21 | +} |
| 22 | +impl std::error::Error for Simple7Error {} |
| 23 | + |
| 24 | +/// Decrypt (deobfuscate) a password from insecure type-7. |
| 25 | +pub fn simple_7_decrypt(data: &str) -> Result<String, Simple7Error> { |
| 26 | + if data.len() < 2 { |
| 27 | + return Err(Simple7Error::DataTooShort); |
| 28 | + } |
| 29 | + |
| 30 | + let salt = data[0..2].parse::<usize>()?; |
| 31 | + |
| 32 | + // Validate salt is in valid range (0-15) |
| 33 | + if salt > 15 { |
| 34 | + return Err(Simple7Error::InvalidSaltValue(salt as u8)); |
| 35 | + } |
| 36 | + |
| 37 | + let secret = hex::decode(&data[2..])?; |
| 38 | + |
| 39 | + let decrypted: Vec<u8> = secret |
| 40 | + .iter() |
| 41 | + .enumerate() |
| 42 | + .map(|(i, &byte)| byte ^ SIMPLE_7_SEED[(salt + i) % 53]) |
| 43 | + .collect(); |
| 44 | + |
| 45 | + Ok(String::from_utf8(decrypted)?) |
| 46 | +} |
| 47 | + |
| 48 | +/// Encrypt (obfuscate) a password with insecure type-7. |
| 49 | +/// |
| 50 | +/// If `salt` is `None`, a random salt in the range 0-15 will be used. |
| 51 | +/// Returns an error if the provided salt is not in the range 0-15. |
| 52 | +pub fn simple_7_encrypt(data: &str, salt: Option<u8>) -> Result<String, Simple7Error> { |
| 53 | + let salt = match salt { |
| 54 | + Some(s) if s > 15 => return Err(Simple7Error::InvalidSaltValue(s)), |
| 55 | + Some(s) => s, |
| 56 | + None => rand::thread_rng().gen_range(0..16), |
| 57 | + }; |
| 58 | + |
| 59 | + let cleartext = data.as_bytes(); |
| 60 | + |
| 61 | + let encrypted: Vec<u8> = cleartext |
| 62 | + .iter() |
| 63 | + .enumerate() |
| 64 | + .map(|(i, &byte)| byte ^ SIMPLE_7_SEED[(salt as usize + i) % 53]) |
| 65 | + .collect(); |
| 66 | + |
| 67 | + Ok(format!("{:02}{}", salt, hex::encode_upper(encrypted))) |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use super::*; |
| 73 | + |
| 74 | + const TEST_PASSWORD: &str = "foo"; |
| 75 | + |
| 76 | + // (salt, encrypted_password) pairs for TEST_PASSWORD |
| 77 | + const VALID_ENCRYPT_DECRYPT_PAIRS: [(u8, &str); 7] = [ |
| 78 | + (1, "0115090B"), |
| 79 | + (6, "0600002E"), |
| 80 | + (9, "094A4106"), |
| 81 | + (3, "03025404"), |
| 82 | + (12, "121F0A18"), |
| 83 | + (10, "10480616"), |
| 84 | + (15, "15140403"), |
| 85 | + ]; |
| 86 | + |
| 87 | + // Invalid salt values for encryption |
| 88 | + const INVALID_SALT_VALUES: [u8; 3] = [16, 99, 255]; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_simple_7_encrypt_ok() { |
| 92 | + for (salt, expected) in VALID_ENCRYPT_DECRYPT_PAIRS { |
| 93 | + let result = simple_7_encrypt(TEST_PASSWORD, Some(salt)) |
| 94 | + .expect("Encryption failed"); |
| 95 | + assert_eq!(result, expected, "Failed for salt {}", salt); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_simple_7_decrypt_ok() { |
| 101 | + for (salt, encrypted) in VALID_ENCRYPT_DECRYPT_PAIRS { |
| 102 | + let result = simple_7_decrypt(encrypted) |
| 103 | + .expect("Decryption failed"); |
| 104 | + assert_eq!(result, TEST_PASSWORD, "Failed for salt {}", salt); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_simple_7_encrypt_decrypt_roundtrip() { |
| 110 | + let original = "test_password_123"; |
| 111 | + let encrypted = simple_7_encrypt(original, Some(5)) |
| 112 | + .expect("Encryption failed"); |
| 113 | + let decrypted = simple_7_decrypt(&encrypted) |
| 114 | + .expect("Decryption failed"); |
| 115 | + assert_eq!(decrypted, original); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn test_simple_7_encrypt_random_salt() { |
| 120 | + let result = simple_7_encrypt(TEST_PASSWORD, None) |
| 121 | + .expect("Encryption with random salt failed"); |
| 122 | + // Should be 2 chars for salt + hex encoded data |
| 123 | + assert!(result.len() >= 2); |
| 124 | + // Should be able to decrypt it back |
| 125 | + let decrypted = simple_7_decrypt(&result) |
| 126 | + .expect("Decryption failed"); |
| 127 | + assert_eq!(decrypted, TEST_PASSWORD); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_simple_7_encrypt_invalid_salt() { |
| 132 | + for salt in INVALID_SALT_VALUES { |
| 133 | + let result = simple_7_encrypt(TEST_PASSWORD, Some(salt)); |
| 134 | + assert!(result.is_err(), "Expected error for salt {}", salt); |
| 135 | + assert!( |
| 136 | + matches!(result.unwrap_err(), Simple7Error::InvalidSaltValue(_)), |
| 137 | + "Expected InvalidSaltValue error for salt {}", |
| 138 | + salt |
| 139 | + ); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn test_simple_7_decrypt_data_too_short() { |
| 145 | + let result = simple_7_decrypt(""); |
| 146 | + assert!(matches!(result.unwrap_err(), Simple7Error::DataTooShort)); |
| 147 | + |
| 148 | + let result = simple_7_decrypt("0"); |
| 149 | + assert!(matches!(result.unwrap_err(), Simple7Error::DataTooShort)); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn test_simple_7_decrypt_invalid_hex() { |
| 154 | + let result = simple_7_decrypt("01GGGG"); |
| 155 | + assert!(matches!(result.unwrap_err(), Simple7Error::InvalidHexEncoding(_))); |
| 156 | + } |
| 157 | + |
| 158 | + #[test] |
| 159 | + fn test_simple_7_decrypt_invalid_salt() { |
| 160 | + // Invalid salt format (not a number) |
| 161 | + let result = simple_7_decrypt("XX1234"); |
| 162 | + assert!(matches!(result.unwrap_err(), Simple7Error::InvalidSaltFormat(_))); |
| 163 | + |
| 164 | + // Salt out of range (0-15) |
| 165 | + let result = simple_7_decrypt("161234"); |
| 166 | + assert!(matches!(result.unwrap_err(), Simple7Error::InvalidSaltValue(16))); |
| 167 | + |
| 168 | + let result = simple_7_decrypt("991234"); |
| 169 | + assert!(matches!(result.unwrap_err(), Simple7Error::InvalidSaltValue(99))); |
| 170 | + } |
| 171 | +} |
0 commit comments