keyfork-shard: make custom error for Sharks errors

This commit is contained in:
Ryan Heywood 2024-01-04 23:11:15 -05:00
parent a79c4a4079
commit d08765b956
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
2 changed files with 16 additions and 10 deletions

View File

@ -15,6 +15,15 @@ use x25519_dalek::{EphemeralSecret, PublicKey};
#[cfg(feature = "openpgp")]
pub mod openpgp;
#[derive(thiserror::Error, Debug)]
pub enum SharksError {
#[error("Error creating share: {0}")]
Share(String),
#[error("Error combining shares: {0}")]
CombineShare(String),
}
/// Decrypt hunk version 1:
/// 1 byte: Version
/// 1 byte: Threshold
@ -94,10 +103,10 @@ pub fn remote_decrypt() -> Result<(), Box<dyn std::error::Error>> {
.into_iter()
.map(|s| Share::try_from(s.as_slice()))
.collect::<Result<Vec<_>, &str>>()
.map_err(|e| anyhow::anyhow!("{e}"))?;
.map_err(|e| SharksError::Share(e.to_string()))?;
let secret = Sharks(threshold)
.recover(&shares)
.map_err(|e| anyhow::anyhow!("{e}"))?;
.map_err(|e| SharksError::CombineShare(e.to_string()))?;
/*
* Verification would take up too much size, mnemonic would be very large

View File

@ -48,22 +48,19 @@ use smartcard::SmartcardManager;
const SHARD_METADATA_VERSION: u8 = 1;
const SHARD_METADATA_OFFSET: usize = 2;
use super::HUNK_VERSION;
use super::{HUNK_VERSION, SharksError};
// 256 bit share is 49 bytes + some amount of hunk bytes, gives us reasonable padding
const ENC_LEN: u8 = 4 * 16;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Error with creating Share: {0}")]
Share(String),
#[error("{0}")]
Sharks(#[from] SharksError),
#[error("Error decrypting share: {0}")]
SymDecryptShare(#[from] AesError),
#[error("Error combining shares: {0}")]
CombineShares(String),
#[error("Derived secret hash {0} != expected {1}")]
InvalidSecret(Fingerprint, Fingerprint),
@ -509,10 +506,10 @@ pub fn combine(
.values()
.map(|message| Share::try_from(message.as_slice()))
.collect::<Result<Vec<_>, &str>>()
.map_err(|e| Error::Share(e.to_string()))?;
.map_err(|e| SharksError::Share(e.to_string()))?;
let secret = Sharks(threshold)
.recover(&shares)
.map_err(|e| Error::CombineShares(e.to_string()))?;
.map_err(|e| SharksError::CombineShare(e.to_string()))?;
// TODO: extract as function
let userid = UserID::from("keyfork-sss");