-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(alpha.8) - Refactor key-exchange crate (#685)
* refactor(key-exchange): adapt key-exchange to new vm * fix: fix feature flags * simplify * delete old msg module * clean up error --------- Co-authored-by: sinu <[email protected]>
- Loading branch information
Showing
9 changed files
with
779 additions
and
610 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,92 @@ | ||
use core::fmt; | ||
use std::error::Error; | ||
|
||
/// A key exchange error. | ||
/// MPC-TLS protocol error. | ||
#[derive(Debug, thiserror::Error)] | ||
pub struct KeyExchangeError { | ||
kind: ErrorKind, | ||
#[source] | ||
source: Option<Box<dyn Error + Send + Sync>>, | ||
#[error(transparent)] | ||
pub struct KeyExchangeError(#[from] ErrorRepr); | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
#[error("key exchange error: {0}")] | ||
pub(crate) enum ErrorRepr { | ||
#[error("state error: {0}")] | ||
State(Box<dyn Error + Send + Sync + 'static>), | ||
#[error("context error: {0}")] | ||
Ctx(Box<dyn Error + Send + Sync + 'static>), | ||
#[error("io error: {0}")] | ||
Io(std::io::Error), | ||
#[error("vm error: {0}")] | ||
Vm(Box<dyn Error + Send + Sync + 'static>), | ||
#[error("share conversion error: {0}")] | ||
ShareConversion(Box<dyn Error + Send + Sync + 'static>), | ||
#[error("role error: {0}")] | ||
Role(Box<dyn Error + Send + Sync + 'static>), | ||
#[error("key error: {0}")] | ||
Key(Box<dyn Error + Send + Sync + 'static>), | ||
} | ||
|
||
impl KeyExchangeError { | ||
pub(crate) fn new<E>(kind: ErrorKind, source: E) -> Self | ||
pub(crate) fn state<E>(err: E) -> KeyExchangeError | ||
where | ||
E: Into<Box<dyn Error + Send + Sync>>, | ||
E: Into<Box<dyn Error + Send + Sync + 'static>>, | ||
{ | ||
Self { | ||
kind, | ||
source: Some(source.into()), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
pub(crate) fn kind(&self) -> &ErrorKind { | ||
&self.kind | ||
} | ||
|
||
pub(crate) fn state(msg: impl Into<String>) -> Self { | ||
Self { | ||
kind: ErrorKind::State, | ||
source: Some(msg.into().into()), | ||
} | ||
} | ||
|
||
pub(crate) fn role(msg: impl Into<String>) -> Self { | ||
Self { | ||
kind: ErrorKind::Role, | ||
source: Some(msg.into().into()), | ||
} | ||
Self(ErrorRepr::State(err.into())) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) enum ErrorKind { | ||
Io, | ||
Context, | ||
Vm, | ||
ShareConversion, | ||
Key, | ||
State, | ||
Role, | ||
} | ||
|
||
impl fmt::Display for KeyExchangeError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self.kind { | ||
ErrorKind::Io => write!(f, "io error")?, | ||
ErrorKind::Context => write!(f, "context error")?, | ||
ErrorKind::Vm => write!(f, "vm error")?, | ||
ErrorKind::ShareConversion => write!(f, "share conversion error")?, | ||
ErrorKind::Key => write!(f, "key error")?, | ||
ErrorKind::State => write!(f, "state error")?, | ||
ErrorKind::Role => write!(f, "role error")?, | ||
} | ||
|
||
if let Some(ref source) = self.source { | ||
write!(f, " caused by: {}", source)?; | ||
} | ||
|
||
Ok(()) | ||
pub(crate) fn ctx<E>(err: E) -> KeyExchangeError | ||
where | ||
E: Into<Box<dyn Error + Send + Sync + 'static>>, | ||
{ | ||
Self(ErrorRepr::Ctx(err.into())) | ||
} | ||
} | ||
|
||
impl From<mpz_common::ContextError> for KeyExchangeError { | ||
fn from(error: mpz_common::ContextError) -> Self { | ||
Self::new(ErrorKind::Context, error) | ||
pub(crate) fn vm<E>(err: E) -> KeyExchangeError | ||
where | ||
E: Into<Box<dyn Error + Send + Sync + 'static>>, | ||
{ | ||
Self(ErrorRepr::Vm(err.into())) | ||
} | ||
} | ||
|
||
impl From<mpz_garble::MemoryError> for KeyExchangeError { | ||
fn from(error: mpz_garble::MemoryError) -> Self { | ||
Self::new(ErrorKind::Vm, error) | ||
pub(crate) fn share_conversion<E>(err: E) -> KeyExchangeError | ||
where | ||
E: Into<Box<dyn Error + Send + Sync + 'static>>, | ||
{ | ||
Self(ErrorRepr::ShareConversion(err.into())) | ||
} | ||
} | ||
|
||
impl From<mpz_garble::LoadError> for KeyExchangeError { | ||
fn from(error: mpz_garble::LoadError) -> Self { | ||
Self::new(ErrorKind::Vm, error) | ||
pub(crate) fn role<E>(err: E) -> KeyExchangeError | ||
where | ||
E: Into<Box<dyn Error + Send + Sync + 'static>>, | ||
{ | ||
Self(ErrorRepr::Role(err.into())) | ||
} | ||
} | ||
|
||
impl From<mpz_garble::ExecutionError> for KeyExchangeError { | ||
fn from(error: mpz_garble::ExecutionError) -> Self { | ||
Self::new(ErrorKind::Vm, error) | ||
pub(crate) fn key<E>(err: E) -> KeyExchangeError | ||
where | ||
E: Into<Box<dyn Error + Send + Sync + 'static>>, | ||
{ | ||
Self(ErrorRepr::Key(err.into())) | ||
} | ||
} | ||
|
||
impl From<mpz_garble::DecodeError> for KeyExchangeError { | ||
fn from(error: mpz_garble::DecodeError) -> Self { | ||
Self::new(ErrorKind::Vm, error) | ||
#[cfg(test)] | ||
pub(crate) fn kind(&self) -> &ErrorRepr { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl From<mpz_share_conversion::ShareConversionError> for KeyExchangeError { | ||
fn from(error: mpz_share_conversion::ShareConversionError) -> Self { | ||
Self::new(ErrorKind::ShareConversion, error) | ||
impl From<mpz_common::ContextError> for KeyExchangeError { | ||
fn from(value: mpz_common::ContextError) -> Self { | ||
Self::ctx(value) | ||
} | ||
} | ||
|
||
impl From<p256::elliptic_curve::Error> for KeyExchangeError { | ||
fn from(error: p256::elliptic_curve::Error) -> Self { | ||
Self::new(ErrorKind::Key, error) | ||
fn from(value: p256::elliptic_curve::Error) -> Self { | ||
Self::key(value) | ||
} | ||
} | ||
|
||
impl From<std::io::Error> for KeyExchangeError { | ||
fn from(error: std::io::Error) -> Self { | ||
Self::new(ErrorKind::Io, error) | ||
fn from(err: std::io::Error) -> Self { | ||
Self(ErrorRepr::Io(err)) | ||
} | ||
} |
Oops, something went wrong.