Restructure into a workspace

This commit is contained in:
Alex Page 2023-06-24 17:11:37 -04:00
parent 589f0bb52b
commit fe08910831
16 changed files with 534 additions and 39 deletions

75
xpkey/src/cli.rs Normal file
View 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,
}

76
xpkey/src/keys.rs Normal file
View file

@ -0,0 +1,76 @@
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>,
verbose: bool,
) -> 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)?;
if verbose {
println!("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(())
}
}

160
xpkey/src/main.rs Normal file
View file

@ -0,0 +1,160 @@
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();
let verbose = args.verbose;
match &args.command {
Commands::List(args) => list(args, verbose),
Commands::Generate(args) => generate(args, verbose),
Commands::Validate(args) => validate(args, verbose),
Commands::ConfirmationId(args) => confirmation_id(&args.instid),
}
}
fn list(args: &ListArgs, verbose: bool) -> Result<()> {
let keys = load_keys(args.keys_path.as_ref(), verbose)?;
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, verbose: bool) -> Result<()> {
if args.channel_id > 999 {
bail!("Channel ID must be 3 digits or fewer");
}
let keys = load_keys(args.keys_path.as_ref(), verbose)?;
let bink_id = args.bink_id.to_ascii_uppercase();
let bink = &keys.bink[&bink_id];
// 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, verbose)?;
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, verbose)?;
} else {
bink2002_generate(&curve, &private_key, args.channel_id, args.count, verbose)?;
}
Ok(())
}
fn validate(args: &ValidateArgs, verbose: bool) -> 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(), verbose)?;
let bink_id = args.bink_id.to_ascii_uppercase();
let bink = &keys.bink[&bink_id];
let curve = initialize_curve(bink, &bink_id, verbose)?;
if u32::from_str_radix(&bink_id, 16)? < 0x40 {
bink1998_validate(&curve, &args.key_to_check, verbose)?;
} else {
bink2002_validate(&curve, &args.key_to_check, verbose)?;
}
Ok(())
}
fn initialize_curve(bink: &Bink, bink_id: &str, verbose: bool) -> Result<EllipticCurve> {
let p = &bink.p;
let a = &bink.a;
let b = &bink.b;
let gx = &bink.g.x;
let gy = &bink.g.y;
let kx = &bink.public.x;
let ky = &bink.public.y;
if verbose {
println!("-----------------------------------------------------------");
println!("Elliptic curve parameters for BINK ID {bink_id}:");
println!("-----------------------------------------------------------");
println!("{bink}");
}
EllipticCurve::new(p, a, b, gx, gy, kx, ky)
}
fn bink1998_generate(
curve: &EllipticCurve,
private_key: &PrivateKey,
channel_id: u32,
count: u64,
verbose: bool,
) -> Result<()> {
for _ in 0..count {
let product_key = bink1998::ProductKey::new(curve, private_key, channel_id, None, None)?;
if verbose {
println!("{:?}", product_key);
}
println!("{product_key}");
}
Ok(())
}
fn bink2002_generate(
curve: &EllipticCurve,
private_key: &PrivateKey,
channel_id: u32,
count: u64,
verbose: bool,
) -> Result<()> {
for _ in 0..count {
let product_key = bink2002::ProductKey::new(curve, private_key, channel_id, None, None)?;
if verbose {
println!("{:?}", product_key);
}
println!("{product_key}");
}
Ok(())
}
fn bink1998_validate(curve: &EllipticCurve, key: &str, verbose: bool) -> Result<()> {
let product_key = bink1998::ProductKey::from_key(curve, key)?;
if verbose {
println!("{:?}", product_key);
}
println!("{product_key}");
println!("Key validated successfully!");
Ok(())
}
fn bink2002_validate(curve: &EllipticCurve, key: &str, verbose: bool) -> Result<()> {
let product_key = bink2002::ProductKey::from_key(curve, key)?;
if verbose {
println!("{:?}", product_key);
}
println!("{product_key}");
println!("Key validated successfully!");
Ok(())
}
fn confirmation_id(installation_id: &str) -> Result<()> {
let confirmation_id = confid::generate(installation_id)?;
println!("Confirmation ID: {confirmation_id}");
Ok(())
}