xpkey -> mskey
This commit is contained in:
parent
1085bec913
commit
e6bb94c378
6 changed files with 19 additions and 17 deletions
14
mskey/Cargo.toml
Normal file
14
mskey/Cargo.toml
Normal file
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "mskey"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
umskt = { path = "../umskt" }
|
||||
anyhow = "1.0.71"
|
||||
clap = { version = "4.3.4", features = ["derive"] }
|
||||
serde = { version = "1.0.164", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
log = "0.4.19"
|
||||
simple_logger = { version = "4.2.0", default-features = false }
|
||||
|
75
mskey/src/cli.rs
Normal file
75
mskey/src/cli.rs
Normal file
|
@ -0,0 +1,75 @@
|
|||
use clap::{Args, Parser, Subcommand};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, about, version, long_about = None)]
|
||||
pub struct Cli {
|
||||
/// Enable verbose output
|
||||
#[arg(short, long)]
|
||||
pub verbose: bool,
|
||||
|
||||
#[command(subcommand)]
|
||||
pub command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Clone, Debug)]
|
||||
pub enum Commands {
|
||||
/// Show which products/binks can be loaded
|
||||
#[command(visible_alias = "l")]
|
||||
List(ListArgs),
|
||||
/// Generate new product keys
|
||||
#[command(visible_alias = "g")]
|
||||
Generate(GenerateArgs),
|
||||
/// Validate a product key
|
||||
#[command(visible_alias = "v")]
|
||||
Validate(ValidateArgs),
|
||||
/// Generate a phone activation Confirmation ID from an Installation ID
|
||||
#[command(name = "confid", visible_alias = "c")]
|
||||
ConfirmationId(ConfirmationIdArgs),
|
||||
}
|
||||
|
||||
#[derive(Args, Clone, Debug)]
|
||||
pub struct ListArgs {
|
||||
/// Optional path to load a keys.json file
|
||||
#[arg(short, long = "keys")]
|
||||
pub keys_path: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Args, Clone, Debug)]
|
||||
pub struct GenerateArgs {
|
||||
/// Which BINK identifier to use
|
||||
#[arg(short, long = "bink", default_value = "2E")]
|
||||
pub bink_id: String,
|
||||
|
||||
/// Channel Identifier to use
|
||||
#[arg(short, long = "channel", default_value = "640")]
|
||||
pub channel_id: u32,
|
||||
|
||||
/// Number of keys to generate
|
||||
#[arg(short = 'n', long = "number", default_value = "1")]
|
||||
pub count: u64,
|
||||
|
||||
/// Optional path to load a keys.json file
|
||||
#[arg(short, long = "keys")]
|
||||
pub keys_path: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Args, Clone, Debug)]
|
||||
pub struct ValidateArgs {
|
||||
/// Which BINK identifier to use
|
||||
#[arg(short, long = "bink", default_value = "2E")]
|
||||
pub bink_id: String,
|
||||
|
||||
/// Optional path to load a keys.json file
|
||||
#[arg(short, long = "keys")]
|
||||
pub keys_path: Option<String>,
|
||||
|
||||
/// The Product key to validate, with or without hyphens
|
||||
pub key_to_check: String,
|
||||
}
|
||||
|
||||
#[derive(Args, Clone, Debug)]
|
||||
pub struct ConfirmationIdArgs {
|
||||
/// The Installation ID used to generate the Confirmation ID
|
||||
#[arg(name = "INSTALLATION_ID")]
|
||||
pub instid: String,
|
||||
}
|
71
mskey/src/keys.rs
Normal file
71
mskey/src/keys.rs
Normal file
|
@ -0,0 +1,71 @@
|
|||
use std::{collections::HashMap, fmt::Display, fs::File, io::BufReader, path::Path};
|
||||
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{from_reader, from_str};
|
||||
|
||||
pub fn load_keys<P: AsRef<Path> + std::fmt::Display>(path: Option<P>) -> Result<Keys> {
|
||||
let keys = {
|
||||
if let Some(path) = path {
|
||||
let file = File::open(&path)?;
|
||||
let reader = BufReader::new(file);
|
||||
let keys: Keys = from_reader(reader)?;
|
||||
|
||||
log::info!("Loaded keys from {}", path);
|
||||
|
||||
keys
|
||||
} else {
|
||||
from_str(std::include_str!("../../keys.json"))?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(keys)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Keys {
|
||||
#[serde(rename = "Products")]
|
||||
pub products: HashMap<String, Product>,
|
||||
#[serde(rename = "BINK")]
|
||||
pub bink: HashMap<String, Bink>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Product {
|
||||
#[serde(rename = "BINK")]
|
||||
pub bink: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Bink {
|
||||
pub p: String,
|
||||
pub a: String,
|
||||
pub b: String,
|
||||
pub g: Point,
|
||||
#[serde(rename = "pub")]
|
||||
pub public: Point,
|
||||
pub n: String,
|
||||
#[serde(rename = "priv")]
|
||||
pub private: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Point {
|
||||
pub x: String,
|
||||
pub y: String,
|
||||
}
|
||||
|
||||
impl Display for Bink {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
writeln!(f, " P: {}", self.p)?;
|
||||
writeln!(f, " a: {}", self.a)?;
|
||||
writeln!(f, " b: {}", self.b)?;
|
||||
writeln!(f, "Gx: {}", self.g.x)?;
|
||||
writeln!(f, "Gy: {}", self.g.y)?;
|
||||
writeln!(f, "Kx: {}", self.public.x)?;
|
||||
writeln!(f, "Ky: {}", self.public.y)?;
|
||||
writeln!(f, " n: {}", self.n)?;
|
||||
writeln!(f, " k: {}", self.private)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
163
mskey/src/main.rs
Normal file
163
mskey/src/main.rs
Normal file
|
@ -0,0 +1,163 @@
|
|||
mod cli;
|
||||
mod keys;
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use clap::Parser;
|
||||
use keys::Bink;
|
||||
use umskt::{
|
||||
bink1998, bink2002, confid,
|
||||
crypto::{EllipticCurve, PrivateKey},
|
||||
};
|
||||
|
||||
use crate::{cli::*, keys::load_keys};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args = Cli::parse();
|
||||
|
||||
if args.verbose {
|
||||
simple_logger::init_with_level(log::Level::Info)?;
|
||||
} else {
|
||||
simple_logger::init_with_level(log::Level::Warn)?;
|
||||
}
|
||||
|
||||
match &args.command {
|
||||
Commands::List(args) => list(args),
|
||||
Commands::Generate(args) => generate(args),
|
||||
Commands::Validate(args) => validate(args),
|
||||
Commands::ConfirmationId(args) => confirmation_id(args),
|
||||
}
|
||||
}
|
||||
|
||||
fn list(args: &ListArgs) -> Result<()> {
|
||||
let keys = load_keys(args.keys_path.as_ref())?;
|
||||
for (key, value) in keys.products.iter() {
|
||||
println!("{}: {:?}", key, value.bink);
|
||||
}
|
||||
|
||||
println!("\n\n** Please note: any BINK ID other than 2E is considered experimental at this time **\n");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn generate(args: &GenerateArgs) -> Result<()> {
|
||||
if args.channel_id > 999 {
|
||||
bail!("Channel ID must be 3 digits or fewer");
|
||||
}
|
||||
|
||||
let keys = load_keys(args.keys_path.as_ref())?;
|
||||
|
||||
let bink_id = args.bink_id.to_ascii_uppercase();
|
||||
let bink = &keys.bink[&bink_id];
|
||||
|
||||
println!("Using BINK ID {bink_id}, which applies to these products:");
|
||||
for (key, value) in keys.products.iter() {
|
||||
if value.bink.contains(&bink_id) {
|
||||
println!(" {}", key);
|
||||
}
|
||||
}
|
||||
|
||||
// gen_order is the order of the generator G, a value we have to reverse -> Schoof's Algorithm.
|
||||
let gen_order = &bink.n;
|
||||
|
||||
// We cannot produce a valid key without knowing the private key k. The reason for this is that
|
||||
// we need the result of the function K(x; y) = kG(x; y).
|
||||
let private_key = &bink.private;
|
||||
|
||||
let curve = initialize_curve(bink, &bink_id)?;
|
||||
let private_key = PrivateKey::new(gen_order, private_key)?;
|
||||
|
||||
if u32::from_str_radix(&bink_id, 16)? < 0x40 {
|
||||
bink1998_generate(&curve, &private_key, args.channel_id, args.count)?;
|
||||
} else {
|
||||
bink2002_generate(&curve, &private_key, args.channel_id, args.count)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(args: &ValidateArgs) -> Result<()> {
|
||||
// We can validate any given key using the available public key: {p, a, b, G, K}.
|
||||
// No private key or gen_order is required.
|
||||
let keys = load_keys(args.keys_path.as_ref())?;
|
||||
let bink_id = args.bink_id.to_ascii_uppercase();
|
||||
|
||||
println!("Using BINK ID {bink_id}, which applies to these products:");
|
||||
for (key, value) in keys.products.iter() {
|
||||
if value.bink.contains(&bink_id) {
|
||||
println!(" {}", key);
|
||||
}
|
||||
}
|
||||
|
||||
let bink = &keys.bink[&bink_id];
|
||||
let curve = initialize_curve(bink, &bink_id)?;
|
||||
|
||||
if u32::from_str_radix(&bink_id, 16)? < 0x40 {
|
||||
bink1998_validate(&curve, &args.key_to_check)?;
|
||||
} else {
|
||||
bink2002_validate(&curve, &args.key_to_check)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn initialize_curve(bink: &Bink, bink_id: &str) -> Result<EllipticCurve> {
|
||||
let p = &bink.p;
|
||||
let a = &bink.a;
|
||||
let gx = &bink.g.x;
|
||||
let gy = &bink.g.y;
|
||||
let kx = &bink.public.x;
|
||||
let ky = &bink.public.y;
|
||||
|
||||
log::info!("Elliptic curve parameters for BINK ID {bink_id}:\n{bink}");
|
||||
|
||||
EllipticCurve::new(p, a, gx, gy, kx, ky)
|
||||
}
|
||||
|
||||
fn bink1998_generate(
|
||||
curve: &EllipticCurve,
|
||||
private_key: &PrivateKey,
|
||||
channel_id: u32,
|
||||
count: u64,
|
||||
) -> Result<()> {
|
||||
for _ in 0..count {
|
||||
let product_key = bink1998::ProductKey::new(curve, private_key, channel_id, None, None)?;
|
||||
log::info!("{:?}", product_key);
|
||||
println!("{product_key}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn bink2002_generate(
|
||||
curve: &EllipticCurve,
|
||||
private_key: &PrivateKey,
|
||||
channel_id: u32,
|
||||
count: u64,
|
||||
) -> Result<()> {
|
||||
for _ in 0..count {
|
||||
let product_key = bink2002::ProductKey::new(curve, private_key, channel_id, None, None)?;
|
||||
log::info!("{:?}", product_key);
|
||||
println!("{product_key}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn bink1998_validate(curve: &EllipticCurve, key: &str) -> Result<()> {
|
||||
let product_key = bink1998::ProductKey::from_key(curve, key)?;
|
||||
log::info!("{:?}", product_key);
|
||||
println!("{product_key}");
|
||||
println!("Key validated successfully!");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn bink2002_validate(curve: &EllipticCurve, key: &str) -> Result<()> {
|
||||
let product_key = bink2002::ProductKey::from_key(curve, key)?;
|
||||
log::info!("{:?}", product_key);
|
||||
println!("{product_key}");
|
||||
println!("Key validated successfully!");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn confirmation_id(args: &ConfirmationIdArgs) -> Result<()> {
|
||||
let confirmation_id = confid::generate(&args.instid)?;
|
||||
println!("Confirmation ID: {confirmation_id}");
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue