Skip to content

Commit

Permalink
hsm: Define a type for key shares.
Browse files Browse the repository at this point in the history
Save some typing.
  • Loading branch information
flihp committed Nov 5, 2024
1 parent c492504 commit 9909caa
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/hsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::{
str::FromStr,
};
use thiserror::Error;
use vsss_rs::{Feldman, FeldmanVerifier, Share};
use vsss_rs::{Feldman, FeldmanVerifier};
use yubihsm::{
authentication::{self, Key, DEFAULT_AUTHENTICATION_KEY_ID},
object::{Id, Label, Type},
Expand Down Expand Up @@ -51,6 +51,8 @@ sa::const_assert!(THRESHOLD <= SHARES);
const BACKUP_EXT: &str = ".backup.json";
const ATTEST_FILE_NAME: &str = "hsm.attest.cert.pem";

pub type Share = vsss_rs::Share<SHARE_LEN>;

#[derive(Error, Debug)]
pub enum HsmError {
#[error("path not a directory")]
Expand Down Expand Up @@ -427,7 +429,7 @@ impl Hsm {
info!("Restoring HSM from backup");
info!("Restoring backup / wrap key from shares");
// vector used to collect shares
let mut shares: Vec<Share<SHARE_LEN>> = Vec::new();
let mut shares: Vec<Share> = Vec::new();

// deserialize verifier:
// verifier was serialized to output/verifier.json in the provisioning ceremony
Expand Down Expand Up @@ -491,26 +493,25 @@ impl Hsm {
Ok(share) => share,
Err(_) => {
println!(
"Failed to decode Share. The value entered \
isn't a valid hex string: try again."
"Failed to decode Share. The value entered isn't \
a valid hex string: try again."
);
continue;
}
};

// construct a Share from the decoded hex string
let share: Share<SHARE_LEN> =
match Share::try_from(&share_vec[..]) {
Ok(share) => share,
Err(_) => {
println!(
"Failed to convert share entered to Share \
type. The value entered is the wrong length \
... try again."
);
continue;
}
};
let share = match Share::try_from(&share_vec[..]) {
Ok(share) => share,
Err(_) => {
println!(
"Failed to convert share entered to Share type. \
The value entered is the wrong length ... try \
again."
);
continue;
}
};

if verifier.verify(&share) {
// if we're going to switch from paper to CDs for key
Expand Down Expand Up @@ -944,7 +945,7 @@ mod tests {
secret
}

fn deserialize_share(share: &str) -> Result<Share<SHARE_LEN>> {
fn deserialize_share(share: &str) -> Result<Share> {
// filter out whitespace to keep hex::decode happy
let share: String =
share.chars().filter(|c| !c.is_whitespace()).collect();
Expand Down Expand Up @@ -1012,9 +1013,8 @@ mod tests {
serde_json::from_str(VERIFIER)
.context("Failed to deserialize FeldmanVerifier from JSON.")?;

let share: Share<SHARE_LEN> =
Share::try_from([0u8; SHARE_LEN].as_ref())
.context("Failed to create Share from static array.")?;
let share = Share::try_from([0u8; SHARE_LEN].as_ref())
.context("Failed to create Share from static array.")?;

assert!(!verifier.verify(&share));

Expand Down Expand Up @@ -1046,7 +1046,7 @@ mod tests {

#[test]
fn recover_secret() -> Result<()> {
let mut shares: Vec<Share<SHARE_LEN>> = Vec::new();
let mut shares: Vec<Share> = Vec::new();
for share in SHARE_ARRAY {
shares.push(deserialize_share(share)?);
}
Expand Down

0 comments on commit 9909caa

Please sign in to comment.