Reorganize crate as lib

This commit is contained in:
Alex Page 2023-06-23 02:24:40 -04:00
parent 1129212b23
commit 83bfa98a38
9 changed files with 62 additions and 36 deletions

View file

@ -2,6 +2,10 @@
name = "umskt" name = "umskt"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
crate-type = ["lib"]
[[bin]]
name = "xpkey"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -9,7 +9,7 @@ use openssl::{
}; };
use serde_json::{from_reader, from_str}; use serde_json::{from_reader, from_str};
use crate::{ use umskt::{
bink1998, bink2002, confid, crypto::initialize_elliptic_curve, key::P_KEY_CHARSET, PK_LENGTH, bink1998, bink2002, confid, crypto::initialize_elliptic_curve, key::P_KEY_CHARSET, PK_LENGTH,
}; };
@ -156,7 +156,7 @@ impl Cli {
keys keys
} else { } else {
from_str(std::include_str!("../keys.json"))? from_str(std::include_str!("../../../keys.json"))?
} }
}; };

7
src/bin/xpkey/main.rs Normal file
View file

@ -0,0 +1,7 @@
use anyhow::Result;
mod cli;
fn main() -> Result<()> {
cli::Cli::new()?.run()
}

View file

@ -6,7 +6,10 @@ use openssl::{
sha::sha1, sha::sha1,
}; };
use crate::key::{base24_decode, base24_encode}; use crate::{
crypto::bitmask,
key::{base24_decode, base24_encode},
};
const FIELD_BITS: i32 = 384; const FIELD_BITS: i32 = 384;
const FIELD_BYTES: usize = 48; const FIELD_BYTES: usize = 48;
@ -185,10 +188,6 @@ fn pack(p_key: ProductKey) -> Vec<u8> {
.collect() .collect()
} }
fn bitmask(n: u64) -> u64 {
(1 << n) - 1
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{fs::File, io::BufReader}; use std::{fs::File, io::BufReader};

View file

@ -6,7 +6,10 @@ use openssl::{
sha::sha1, sha::sha1,
}; };
use crate::key::{base24_decode, base24_encode}; use crate::{
crypto::{bitmask, by_dword, next_sn_bits},
key::{base24_decode, base24_encode},
};
const FIELD_BITS: i32 = 512; const FIELD_BITS: i32 = 512;
const FIELD_BYTES: usize = 64; const FIELD_BYTES: usize = 64;
@ -262,22 +265,11 @@ fn pack(p_key: ProductKey) -> Vec<u8> {
.collect() .collect()
} }
fn bitmask(n: u64) -> u64 {
(1 << n) - 1
}
fn next_sn_bits(field: u64, n: u32, offset: u32) -> u64 {
(field >> offset) & ((1u64 << n) - 1)
}
fn by_dword(n: &[u8]) -> u32 {
(n[0] as u32) | (n[1] as u32) << 8 | (n[2] as u32) << 16 | (n[3] as u32) << 24
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{fs::File, io::BufReader}; use std::{fs::File, io::BufReader};
use openssl::bn::{BigNum, BigNumContext};
use serde_json::from_reader; use serde_json::from_reader;
use crate::crypto::initialize_elliptic_curve; use crate::crypto::initialize_elliptic_curve;
@ -308,4 +300,23 @@ mod tests {
assert!(super::verify(&e_curve, &gen_point, &pub_point, product_key, true).unwrap()); assert!(super::verify(&e_curve, &gen_point, &pub_point, product_key, true).unwrap());
} }
#[test]
fn prime_test() {
let mut ctx = BigNumContext::new().unwrap();
let p = BigNum::from_dec_str("9759712359818460653").unwrap();
let mut p2 = BigNum::new().unwrap();
let _ = p2.sqr(&p, &mut ctx);
println!("p2: {:?}", p2);
}
#[test]
fn sqrt_test() {
let mut ctx = BigNumContext::new().unwrap();
let s = BigNum::from_dec_str("95251985346393225982548498694509186409").unwrap();
let p = BigNum::from_dec_str("9362780380393422053").unwrap();
let mut out = BigNum::new().unwrap();
out.mod_sqrt(&s, &p, &mut ctx).unwrap();
assert_eq!(out, BigNum::from_dec_str("8965848400968383453").unwrap());
}
} }

View file

@ -79,8 +79,8 @@ fn umul128(a: u64, b: u64, hi: &mut u64) -> u64 {
r as u64 r as u64
} }
/// `hi:lo * ceil(2**170/MOD) >> (64 + 64 + 42)`
fn ui128_quotient_mod(lo: u64, hi: u64) -> u64 { fn ui128_quotient_mod(lo: u64, hi: u64) -> u64 {
// hi:lo * ceil(2**170/MOD) >> (64 + 64 + 42)
let mut prod1: u64 = 0; let mut prod1: u64 = 0;
umul128(lo, 0x604fa6a1c6346a87_i64 as u64, &mut prod1); umul128(lo, 0x604fa6a1c6346a87_i64 as u64, &mut prod1);
let mut part1hi: u64 = 0; let mut part1hi: u64 = 0;
@ -286,7 +286,6 @@ unsafe fn find_divisor_v(d: *mut TDivisor) -> i32 {
1_i32 1_i32
} }
/// generic short slow code
unsafe fn polynomial_mul( unsafe fn polynomial_mul(
adeg: i32, adeg: i32,
a: *const u64, a: *const u64,
@ -295,6 +294,7 @@ unsafe fn polynomial_mul(
mut resultprevdeg: i32, mut resultprevdeg: i32,
result: *mut u64, result: *mut u64,
) -> i32 { ) -> i32 {
// generic short slow code
if adeg < 0_i32 || bdeg < 0_i32 { if adeg < 0_i32 || bdeg < 0_i32 {
return resultprevdeg; return resultprevdeg;
} }

View file

@ -35,3 +35,15 @@ pub fn initialize_elliptic_curve(
(c_curve, gen_point, pub_point) (c_curve, gen_point, pub_point)
} }
pub fn bitmask(n: u64) -> u64 {
(1 << n) - 1
}
pub fn next_sn_bits(field: u64, n: u32, offset: u32) -> u64 {
(field >> offset) & ((1u64 << n) - 1)
}
pub fn by_dword(n: &[u8]) -> u32 {
(n[0] as u32) | (n[1] as u32) << 8 | (n[2] as u32) << 16 | (n[3] as u32) << 24
}

7
src/lib.rs Normal file
View file

@ -0,0 +1,7 @@
pub mod bink1998;
pub mod bink2002;
pub mod confid;
pub mod crypto;
pub mod key;
pub const PK_LENGTH: usize = 25;

View file

@ -1,14 +0,0 @@
use anyhow::Result;
mod bink1998;
mod bink2002;
mod cli;
mod confid;
mod crypto;
mod key;
const PK_LENGTH: usize = 25;
fn main() -> Result<()> {
cli::Cli::new()?.run()
}