Separate channel_id and sequence in bink1998

This commit is contained in:
Alex Page 2023-06-23 20:44:46 -04:00
parent 49ce9a47f3
commit c380be1eae

View file

@ -26,7 +26,8 @@ const EVERYTHING_ELSE: u8 = HASH_LENGTH_BITS + SERIAL_LENGTH_BITS + UPGRADE_LENG
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ProductKey { pub struct ProductKey {
upgrade: bool, upgrade: bool,
serial: u32, channel_id: u32,
sequence: u32,
hash: u32, hash: u32,
signature: u64, signature: u64,
} }
@ -59,7 +60,8 @@ impl ProductKey {
&curve.gen_point, &curve.gen_point,
&private_key.gen_order, &private_key.gen_order,
&private_key.private_key, &private_key.private_key,
channel_id * 1_000_000 + sequence, channel_id,
sequence,
upgrade, upgrade,
)?; )?;
@ -85,7 +87,8 @@ impl ProductKey {
base_point: &EcPoint, base_point: &EcPoint,
gen_order: &BigNum, gen_order: &BigNum,
private_key: &BigNum, private_key: &BigNum,
serial: u32, channel_id: u32,
sequence: u32,
upgrade: bool, upgrade: bool,
) -> Result<Self> { ) -> Result<Self> {
let mut num_context = BigNumContext::new().unwrap(); let mut num_context = BigNumContext::new().unwrap();
@ -97,6 +100,7 @@ impl ProductKey {
let mut ek: BigNum; let mut ek: BigNum;
let serial = channel_id * 1_000_000 + sequence;
let data = serial << 1 | upgrade as u32; let data = serial << 1 | upgrade as u32;
let product_key = loop { let product_key = loop {
@ -139,7 +143,8 @@ impl ProductKey {
if signature <= bitmask(55) { if signature <= bitmask(55) {
break Self { break Self {
upgrade, upgrade,
serial, channel_id,
sequence,
hash, hash,
signature, signature,
}; };
@ -182,7 +187,8 @@ impl ProductKey {
let mut y_bin = y.to_vec_padded(FIELD_BYTES as i32)?; let mut y_bin = y.to_vec_padded(FIELD_BYTES as i32)?;
y_bin.reverse(); y_bin.reverse();
let data = self.serial << 1 | self.upgrade as u32; let serial = self.channel_id * 1_000_000 + self.sequence;
let data = serial << 1 | self.upgrade as u32;
msg_buffer[0..4].copy_from_slice(&data.to_le_bytes()); msg_buffer[0..4].copy_from_slice(&data.to_le_bytes());
msg_buffer[4..4 + FIELD_BYTES].copy_from_slice(&x_bin); msg_buffer[4..4 + FIELD_BYTES].copy_from_slice(&x_bin);
@ -206,9 +212,13 @@ impl ProductKey {
let serial = reader.read_u32(SERIAL_LENGTH_BITS)?; let serial = reader.read_u32(SERIAL_LENGTH_BITS)?;
let upgrade = reader.read_bool()?; let upgrade = reader.read_bool()?;
let sequence = serial % 1_000_000;
let channel_id = serial / 1_000_000;
Ok(Self { Ok(Self {
upgrade, upgrade,
serial, channel_id,
sequence,
hash, hash,
signature, signature,
}) })
@ -217,9 +227,11 @@ impl ProductKey {
fn pack(&self) -> Vec<u8> { fn pack(&self) -> Vec<u8> {
let mut packed_key: u128 = 0; let mut packed_key: u128 = 0;
let serial = self.channel_id * 1_000_000 + self.sequence;
packed_key |= (self.signature as u128) << EVERYTHING_ELSE; packed_key |= (self.signature as u128) << EVERYTHING_ELSE;
packed_key |= (self.hash as u128) << (SERIAL_LENGTH_BITS + UPGRADE_LENGTH_BITS); packed_key |= (self.hash as u128) << (SERIAL_LENGTH_BITS + UPGRADE_LENGTH_BITS);
packed_key |= (self.serial as u128) << UPGRADE_LENGTH_BITS; packed_key |= (serial as u128) << UPGRADE_LENGTH_BITS;
packed_key |= self.upgrade as u128; packed_key |= self.upgrade as u128;
packed_key packed_key
@ -287,7 +299,8 @@ mod tests {
fn pack_test() { fn pack_test() {
let key = super::ProductKey { let key = super::ProductKey {
upgrade: false, upgrade: false,
serial: 640010550, channel_id: 640,
sequence: 10550,
hash: 39185432, hash: 39185432,
signature: 6939952665262054, signature: 6939952665262054,
}; };