(decoded: &[u8])
| 1 | use crate::alloc_mem::alloc_mem; |
| 2 | use obfstr::obfstr; |
| 3 | pub unsafe fn decrypt(decoded: &[u8]) -> Result<(usize, usize), String> { |
| 4 | use aes::Aes256; |
| 5 | use cipher::{BlockDecryptMut, KeyIvInit, block_padding::Pkcs7}; |
| 6 | use sha2::{Sha256, Digest}; |
| 7 | |
| 8 | type Aes256CbcDec = cbc::Decryptor<Aes256>; |
| 9 | |
| 10 | let key_len = 32; // AES-256 key size |
| 11 | let iv_len = 16; // AES block size |
| 12 | let hash_len = 32; // SHA-256 hash size |
| 13 | |
| 14 | if decoded.len() < key_len + iv_len + hash_len + 1 { |
| 15 | return Err(obfstr!("aes payload too short").to_string()); |
| 16 | } |
| 17 | |
| 18 | let key = &decoded[0..key_len]; |
| 19 | let iv = &decoded[key_len..key_len + iv_len]; |
| 20 | let hash = &decoded[key_len + iv_len..key_len + iv_len + hash_len]; |
| 21 | let encrypted = &decoded[key_len + iv_len + hash_len..]; |
| 22 | |
| 23 | let p = unsafe { alloc_mem(encrypted.len())? }; |
| 24 | std::ptr::copy_nonoverlapping(encrypted.as_ptr(), p, encrypted.len()); |
| 25 | let buf = std::slice::from_raw_parts_mut(p, encrypted.len()); |
| 26 | |
| 27 | let cipher = Aes256CbcDec::new_from_slices(key, iv) |
| 28 | .map_err(|_| obfstr!("invalid aes key or iv").to_string())?; |
| 29 | |
| 30 | let pt_len = cipher.decrypt_padded_mut::<Pkcs7>(buf) |
| 31 | .map_err(|_| obfstr!("aes decryption failed").to_string())? |
| 32 | .len(); |
| 33 | |
| 34 | let mut hasher = Sha256::new(); |
| 35 | hasher.update(&buf[..pt_len]); |
| 36 | let calc_hash = hasher.finalize(); |
| 37 | |
| 38 | if hash != calc_hash.as_slice() { |
| 39 | return Err(obfstr!("aes hash mismatch").to_string()); |
| 40 | } |
| 41 | |
| 42 | Ok((p as usize, pt_len)) |
| 43 | } |
nothing calls this directly
no test coverage detected