Fix bink1998 verify

This commit is contained in:
Alex Page 2023-06-19 02:49:46 -04:00
parent 3993cafa35
commit 3057f1bc95
2 changed files with 10 additions and 13 deletions

View file

@ -6,7 +6,7 @@ use openssl::{
sha::sha1, sha::sha1,
}; };
use crate::{key::unbase24, FIELD_BYTES}; use crate::{key::base24_decode, FIELD_BYTES};
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
struct ProductKey { struct ProductKey {
@ -24,11 +24,7 @@ pub fn verify(
) -> Result<bool> { ) -> Result<bool> {
let mut num_context = BigNumContext::new()?; let mut num_context = BigNumContext::new()?;
let p_raw = unbase24(p_key); let p_raw = base24_decode(p_key);
for byte in &p_raw {
print!("{:02X}, ", *byte);
}
println!();
let product_key = unpack(&p_raw)?; let product_key = unpack(&p_raw)?;
let p_data = product_key.serial << 1 | product_key.upgrade as u32; let p_data = product_key.serial << 1 | product_key.upgrade as u32;
@ -63,7 +59,7 @@ pub fn verify(
let msg_digest = sha1(&msg_buffer); let msg_digest = sha1(&msg_buffer);
let hash: u32 = u32::from_be_bytes(msg_digest[0..4].try_into().unwrap()) & ((1 << 28) - 1); let hash: u32 = u32::from_le_bytes(msg_digest[0..4].try_into().unwrap()) >> 4 & ((1 << 28) - 1);
Ok(hash == product_key.hash) Ok(hash == product_key.hash)
} }
@ -82,11 +78,12 @@ pub fn generate(
fn unpack(p_raw: &[u8]) -> Result<ProductKey> { fn unpack(p_raw: &[u8]) -> Result<ProductKey> {
const HASH_LENGTH_BITS: u8 = 28; const HASH_LENGTH_BITS: u8 = 28;
const SERIAL_LENGTH_BITS: u8 = 30; const SERIAL_LENGTH_BITS: u8 = 30;
const UPGRADE_LENGTH_BITS: u8 = 1;
const EVERYTHING_ELSE: u8 = HASH_LENGTH_BITS + SERIAL_LENGTH_BITS + UPGRADE_LENGTH_BITS;
let mut reader = BitReader::new(p_raw); let mut reader = BitReader::new(p_raw);
// The signature length is unknown, but everything else is, so we can calculate it // The signature length is unknown, but everything else is, so we can calculate it
let signature_length_bits = let signature_length_bits = (p_raw.len() * 8) as u8 - EVERYTHING_ELSE;
(p_raw.len() * 8) as u8 - (HASH_LENGTH_BITS + SERIAL_LENGTH_BITS + 1);
let p_signature = reader.read_u64(signature_length_bits)?; let p_signature = reader.read_u64(signature_length_bits)?;
let p_hash = reader.read_u32(HASH_LENGTH_BITS)?; let p_hash = reader.read_u32(HASH_LENGTH_BITS)?;

View file

@ -9,7 +9,7 @@ const P_CHARSET: [char; 24] = [
const PK_LENGTH: usize = 25; const PK_LENGTH: usize = 25;
pub fn unbase24(cd_key: &str) -> Vec<u8> { pub fn base24_decode(cd_key: &str) -> Vec<u8> {
let p_decoded_key: Vec<u8> = cd_key let p_decoded_key: Vec<u8> = cd_key
.chars() .chars()
.filter_map(|c| P_CHARSET.iter().position(|&x| x == c).map(|i| i as u8)) .filter_map(|c| P_CHARSET.iter().position(|&x| x == c).map(|i| i as u8))
@ -25,7 +25,7 @@ pub fn unbase24(cd_key: &str) -> Vec<u8> {
y.to_vec() y.to_vec()
} }
pub fn base24(byte_seq: &[u8]) -> String { pub fn base24_encode(byte_seq: &[u8]) -> String {
let mut z = BigNum::from_slice(byte_seq).unwrap(); let mut z = BigNum::from_slice(byte_seq).unwrap();
let mut out: VecDeque<char> = VecDeque::new(); let mut out: VecDeque<char> = VecDeque::new();
(0..=24).for_each(|_| out.push_front(P_CHARSET[z.div_word(24).unwrap() as usize])); (0..=24).for_each(|_| out.push_front(P_CHARSET[z.div_word(24).unwrap() as usize]));
@ -37,9 +37,9 @@ mod tests {
#[test] #[test]
fn test_base24() { fn test_base24() {
let input = "JTW3TJ7PFJ7V9CCMX84V9PFT8"; let input = "JTW3TJ7PFJ7V9CCMX84V9PFT8";
let unbase24 = super::unbase24(input); let unbase24 = super::base24_decode(input);
println!("{:?}", unbase24); println!("{:?}", unbase24);
let base24 = super::base24(&unbase24); let base24 = super::base24_encode(&unbase24);
println!("{}", base24); println!("{}", base24);
assert_eq!(input, base24); assert_eq!(input, base24);
} }