From b73401cefb19fa5a91b10bcf1a630754e1a86247 Mon Sep 17 00:00:00 2001 From: Alex Page Date: Fri, 23 Jun 2023 20:29:13 -0400 Subject: [PATCH] Cleanup --- src/bink1998.rs | 52 +++++++++++++++++++++++++------------------------ src/bink2002.rs | 27 ++++++++++++------------- src/key.rs | 2 +- src/lib.rs | 2 -- 4 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/bink1998.rs b/src/bink1998.rs index 981315a..94cc4c9 100644 --- a/src/bink1998.rs +++ b/src/bink1998.rs @@ -92,10 +92,11 @@ impl ProductKey { let mut c = BigNum::new()?; let mut s = BigNum::new()?; - let mut s_2 = BigNum::new()?; let mut x = BigNum::new()?; let mut y = BigNum::new()?; + let mut ek: BigNum; + let p_data = p_serial << 1 | p_upgrade as u32; let product_key = loop { @@ -128,10 +129,10 @@ impl ProductKey { let p_hash: u32 = u32::from_le_bytes(msg_digest[0..4].try_into().unwrap()) >> 4 & bitmask(28) as u32; - s_2.copy_from_slice(&private_key.to_vec())?; - s_2.mul_word(p_hash)?; + ek = (*private_key).to_owned()?; + ek.mul_word(p_hash)?; - s.mod_add(&s_2, &c, gen_order, &mut num_context)?; + s.mod_add(&ek, &c, gen_order, &mut num_context)?; let p_signature = u64::from_be_bytes(s.to_vec_padded(8)?.try_into().unwrap()); @@ -154,9 +155,7 @@ impl ProductKey { base_point: &EcPoint, public_key: &EcPoint, ) -> Result { - let mut num_context = BigNumContext::new()?; - - let p_data = self.serial << 1 | self.upgrade as u32; + let mut ctx = BigNumContext::new()?; let e = BigNum::from_u32(self.hash)?; let s = BigNum::from_slice(&self.signature.to_be_bytes())?; @@ -165,15 +164,16 @@ impl ProductKey { let mut t = EcPoint::new(e_curve)?; let mut p = EcPoint::new(e_curve)?; - let mut p_2 = EcPoint::new(e_curve)?; - t.mul(e_curve, base_point, &s, &num_context)?; - p.mul(e_curve, public_key, &e, &num_context)?; - p_2.mul(e_curve, public_key, &e, &num_context)?; + t.mul(e_curve, base_point, &s, &ctx)?; + p.mul(e_curve, public_key, &e, &ctx)?; - p.add(e_curve, &t, &p_2, &mut num_context)?; + { + let p_copy = p.to_owned(e_curve)?; + p.add(e_curve, &t, &p_copy, &mut ctx)?; + } - p.affine_coordinates(e_curve, &mut x, &mut y, &mut num_context)?; + p.affine_coordinates(e_curve, &mut x, &mut y, &mut ctx)?; let mut msg_buffer: [u8; SHA_MSG_LENGTH] = [0; SHA_MSG_LENGTH]; @@ -182,6 +182,8 @@ impl ProductKey { let mut y_bin = y.to_vec_padded(FIELD_BYTES as i32)?; y_bin.reverse(); + let p_data = self.serial << 1 | self.upgrade as u32; + msg_buffer[0..4].copy_from_slice(&p_data.to_le_bytes()); msg_buffer[4..4 + FIELD_BYTES].copy_from_slice(&x_bin); msg_buffer[4 + FIELD_BYTES..4 + FIELD_BYTES * 2].copy_from_slice(&y_bin); @@ -194,21 +196,21 @@ impl ProductKey { Ok(hash == self.hash) } - fn from_packed(p_raw: &[u8]) -> Result { - let mut reader = BitReader::new(p_raw); - // The signature length is unknown, but everything else is, so we can calculate it - let signature_length_bits = (p_raw.len() * 8) as u8 - EVERYTHING_ELSE; + fn from_packed(packed_key: &[u8]) -> Result { + let mut reader = BitReader::new(packed_key); + // The signature length isn't known, but everything else is, so we can calculate it + let signature_length_bits = (packed_key.len() * 8) as u8 - EVERYTHING_ELSE; - let p_signature = reader.read_u64(signature_length_bits)?; - let p_hash = reader.read_u32(HASH_LENGTH_BITS)?; - let p_serial = reader.read_u32(SERIAL_LENGTH_BITS)?; - let p_upgrade = reader.read_bool()?; + let signature = reader.read_u64(signature_length_bits)?; + let hash = reader.read_u32(HASH_LENGTH_BITS)?; + let serial = reader.read_u32(SERIAL_LENGTH_BITS)?; + let upgrade = reader.read_bool()?; Ok(Self { - upgrade: p_upgrade, - serial: p_serial, - hash: p_hash, - signature: p_signature, + upgrade, + serial, + hash, + signature, }) } diff --git a/src/bink2002.rs b/src/bink2002.rs index 2a1aaad..b822542 100644 --- a/src/bink2002.rs +++ b/src/bink2002.rs @@ -279,22 +279,23 @@ impl ProductKey { Ok(hash == self.hash) } - fn from_packed(p_raw: &[u8]) -> Result { - let mut reader = BitReader::new(p_raw); - let auth_info_length_bits = (p_raw.len() * 8) as u8 - EVERYTHING_ELSE; + fn from_packed(packed_key: &[u8]) -> Result { + let mut reader = BitReader::new(packed_key); + // The auth info length isn't known, but everything else is, so we can calculate it + let auth_info_length_bits = (packed_key.len() * 8) as u8 - EVERYTHING_ELSE; - let p_auth_info = reader.read_u32(auth_info_length_bits)?; - let p_signature = reader.read_u64(SIGNATURE_LENGTH_BITS)?; - let p_hash = reader.read_u32(HASH_LENGTH_BITS)?; - let p_channel_id = reader.read_u32(CHANNEL_ID_LENGTH_BITS)?; - let p_upgrade = reader.read_bool()?; + let auth_info = reader.read_u32(auth_info_length_bits)?; + let signature = reader.read_u64(SIGNATURE_LENGTH_BITS)?; + let hash = reader.read_u32(HASH_LENGTH_BITS)?; + let channel_id = reader.read_u32(CHANNEL_ID_LENGTH_BITS)?; + let upgrade = reader.read_bool()?; Ok(Self { - upgrade: p_upgrade, - channel_id: p_channel_id, - hash: p_hash, - signature: p_signature, - auth_info: p_auth_info, + upgrade, + channel_id, + hash, + signature, + auth_info, }) } diff --git a/src/key.rs b/src/key.rs index 838c308..d470090 100644 --- a/src/key.rs +++ b/src/key.rs @@ -3,7 +3,7 @@ use std::collections::VecDeque; use anyhow::{anyhow, Result}; use openssl::bn::BigNum; -use crate::PK_LENGTH; +const PK_LENGTH: usize = 25; /// The allowed character set in a product key. pub const P_KEY_CHARSET: [char; 24] = [ diff --git a/src/lib.rs b/src/lib.rs index a9d9f1b..2910152 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,5 +4,3 @@ pub mod confid; pub mod crypto; mod key; mod math; - -const PK_LENGTH: usize = 25;