From 83bfa98a38ee04a5bce354d246c9af6e4fce5818 Mon Sep 17 00:00:00 2001 From: Alex Page Date: Fri, 23 Jun 2023 02:24:40 -0400 Subject: [PATCH] Reorganize crate as lib --- Cargo.toml | 4 ++++ src/{ => bin/xpkey}/cli.rs | 4 ++-- src/bin/xpkey/main.rs | 7 +++++++ src/bink1998.rs | 9 ++++----- src/bink2002.rs | 37 ++++++++++++++++++++++++------------- src/confid/black_box.rs | 4 ++-- src/crypto.rs | 12 ++++++++++++ src/lib.rs | 7 +++++++ src/main.rs | 14 -------------- 9 files changed, 62 insertions(+), 36 deletions(-) rename src/{ => bin/xpkey}/cli.rs (99%) create mode 100644 src/bin/xpkey/main.rs create mode 100644 src/lib.rs delete mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml index f622acf..84db26a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,10 @@ name = "umskt" version = "0.1.0" edition = "2021" +crate-type = ["lib"] + +[[bin]] +name = "xpkey" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/cli.rs b/src/bin/xpkey/cli.rs similarity index 99% rename from src/cli.rs rename to src/bin/xpkey/cli.rs index a422deb..f7e00a9 100644 --- a/src/cli.rs +++ b/src/bin/xpkey/cli.rs @@ -9,7 +9,7 @@ use openssl::{ }; use serde_json::{from_reader, from_str}; -use crate::{ +use umskt::{ bink1998, bink2002, confid, crypto::initialize_elliptic_curve, key::P_KEY_CHARSET, PK_LENGTH, }; @@ -156,7 +156,7 @@ impl Cli { keys } else { - from_str(std::include_str!("../keys.json"))? + from_str(std::include_str!("../../../keys.json"))? } }; diff --git a/src/bin/xpkey/main.rs b/src/bin/xpkey/main.rs new file mode 100644 index 0000000..5e60ce0 --- /dev/null +++ b/src/bin/xpkey/main.rs @@ -0,0 +1,7 @@ +use anyhow::Result; + +mod cli; + +fn main() -> Result<()> { + cli::Cli::new()?.run() +} diff --git a/src/bink1998.rs b/src/bink1998.rs index 1c9f12b..9d3f4ee 100644 --- a/src/bink1998.rs +++ b/src/bink1998.rs @@ -6,7 +6,10 @@ use openssl::{ 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_BYTES: usize = 48; @@ -185,10 +188,6 @@ fn pack(p_key: ProductKey) -> Vec { .collect() } -fn bitmask(n: u64) -> u64 { - (1 << n) - 1 -} - #[cfg(test)] mod tests { use std::{fs::File, io::BufReader}; diff --git a/src/bink2002.rs b/src/bink2002.rs index ce5655c..1f3f46d 100644 --- a/src/bink2002.rs +++ b/src/bink2002.rs @@ -6,7 +6,10 @@ use openssl::{ 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_BYTES: usize = 64; @@ -262,22 +265,11 @@ fn pack(p_key: ProductKey) -> Vec { .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)] mod tests { use std::{fs::File, io::BufReader}; + use openssl::bn::{BigNum, BigNumContext}; use serde_json::from_reader; 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()); } + + #[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()); + } } diff --git a/src/confid/black_box.rs b/src/confid/black_box.rs index af491ea..688552d 100644 --- a/src/confid/black_box.rs +++ b/src/confid/black_box.rs @@ -79,8 +79,8 @@ fn umul128(a: u64, b: u64, hi: &mut u64) -> u64 { r as u64 } -/// `hi:lo * ceil(2**170/MOD) >> (64 + 64 + 42)` fn ui128_quotient_mod(lo: u64, hi: u64) -> u64 { + // hi:lo * ceil(2**170/MOD) >> (64 + 64 + 42) let mut prod1: u64 = 0; umul128(lo, 0x604fa6a1c6346a87_i64 as u64, &mut prod1); let mut part1hi: u64 = 0; @@ -286,7 +286,6 @@ unsafe fn find_divisor_v(d: *mut TDivisor) -> i32 { 1_i32 } -/// generic short slow code unsafe fn polynomial_mul( adeg: i32, a: *const u64, @@ -295,6 +294,7 @@ unsafe fn polynomial_mul( mut resultprevdeg: i32, result: *mut u64, ) -> i32 { + // generic short slow code if adeg < 0_i32 || bdeg < 0_i32 { return resultprevdeg; } diff --git a/src/crypto.rs b/src/crypto.rs index ce406c3..530824b 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -35,3 +35,15 @@ pub fn initialize_elliptic_curve( (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 +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ecf24a8 --- /dev/null +++ b/src/lib.rs @@ -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; diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 1a5ccb1..0000000 --- a/src/main.rs +++ /dev/null @@ -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() -}