Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
aewag committed Sep 11, 2024
1 parent 3858557 commit 4fa4a58
Showing 1 changed file with 21 additions and 39 deletions.
60 changes: 21 additions & 39 deletions examples/sst-demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ impl fmt::Display for DemoError {

impl Error for DemoError {}

impl DemoError {
pub fn raise<R>(message: String) -> Result<R, Box<dyn Error>> {
Err(Box::new(Self(message)))
}
}

type Hasher = Sha256_192;

struct GenKeyParameter {
Expand Down Expand Up @@ -154,12 +148,8 @@ fn sign(args: &ArgMatches) -> Result<(), std::io::Error> {
.try_into()
.unwrap_or_else(|_| panic!("Tree identifier has wrong length"));

let mut private_key_update_function = |new_key: &[u8]| {
if write(&private_key_filename, new_key).is_ok() {
return Ok(());
}
Err(())
};
let mut private_key_update_function =
|new_key: &[u8]| write(&private_key_filename, new_key).map_err(|_| ());

let signature = hbs_lms::sign::<Hasher>(
&message_data,
Expand Down Expand Up @@ -187,12 +177,10 @@ fn verify(args: &ArgMatches) -> bool {
hbs_lms::verify::<Hasher>(&message_data, &signature_data, &public_key_data).is_ok()
}

fn get_filename(keyname: &str, idx: Option<u8>, suffix: &str) -> String {
if let Some(idx) = idx {
format!("{}.{}.{}", keyname, &idx, suffix)
} else {
format!("{}.{}", keyname, suffix)
}
fn get_filename(filename: &str, idx: Option<u8>, suffix: &str) -> String {
idx.map_or(format!("{}.{}", filename, suffix), |idx| {
format!("{}.{}.{}", filename, &idx, suffix)
})
}

fn get_public_key_filename(keyname: &str, idx: Option<u8>) -> String {
Expand Down Expand Up @@ -236,8 +224,7 @@ fn prepare_keygen(args: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
let arg_init_tree_ident = args.get_flag(ARG_INIT_TREE_IDENT);
let treeident_filename = get_treeident_filename(&keyname);
let mut tree_identifier = if !arg_init_tree_ident {
let tree_ident_filedata = read_file(&treeident_filename);
tree_ident_filedata
read_file(&treeident_filename)
.try_into()
.unwrap_or_else(|_| panic!("Tree identifier has wrong length"))
} else {
Expand All @@ -249,33 +236,28 @@ fn prepare_keygen(args: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
&get_parameter(ARG_SSTS_PARAM, args),
&get_parameter(ARG_AUXSIZE, args),
);

let hss_parameters = genkey_parameter.hss_parameters;
let sst_extension = genkey_parameter.sst_extension;

let seed: Seed<Hasher> = if let Some(seed) = args.value_of(ARG_SEED) {
let decoded = hex::decode(seed)?;
if decoded.len() != Hasher::OUTPUT_SIZE as usize {
let error = format!(
"Seed is too short ({} of {} required bytes)",
decoded.len(),
Hasher::OUTPUT_SIZE
);
return DemoError::raise(error);
}
let mut seed = Seed::default();
seed.as_mut_slice().copy_from_slice(&decoded[..]);
seed
} else {
return DemoError::raise("Seed was not given".to_string());
};
let encoded_seed = args
.value_of(ARG_SEED)
.ok_or(DemoError("No seed given".to_string()))?;
let decoded_seed = hex::decode(encoded_seed)?;
(decoded_seed.len() == Hasher::OUTPUT_SIZE.into())
.then_some(())
.ok_or(DemoError(format!(
"Seed length is {} bytes, but length of {} bytes is expected",
decoded_seed.len(),
Hasher::OUTPUT_SIZE
)))?;
let mut seed = Seed::<Hasher>::default();
seed.as_mut_slice().copy_from_slice(&decoded_seed[..]);

let mut aux_data = vec![0u8; genkey_parameter.aux_data];
let aux_slice: &mut &mut [u8] = &mut &mut aux_data[..];

// create our private key
let (signing_key, intermed_node_hashval) = prepare_sst_keygen(
&hss_parameters,
&genkey_parameter.hss_parameters,
&sst_extension,
&seed,
Some(aux_slice),
Expand Down

0 comments on commit 4fa4a58

Please sign in to comment.