Files
opaque-vx/src/errors.rs
T

205 lines
7.1 KiB
Rust
Raw Normal View History

2023-05-22 23:04:26 -07:00
// Copyright (c) Meta Platforms, Inc. and affiliates.
2020-06-05 09:35:14 -07:00
//
2023-05-22 23:04:26 -07:00
// This source code is dual-licensed under either the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree or the Apache
2021-12-03 14:38:11 -08:00
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
2023-05-22 23:04:26 -07:00
// of this source tree. You may select, at your option, one of the above-listed
// licenses.
2020-06-05 09:35:14 -07:00
//! A list of error types which are produced during an execution of the protocol
2021-08-12 06:25:07 +02:00
use core::convert::Infallible;
2025-04-15 22:31:37 +02:00
use core::error::Error;
2021-08-12 06:25:07 +02:00
use core::fmt::Debug;
2021-07-20 11:49:37 +02:00
use displaydoc::Display;
2020-06-05 09:35:14 -07:00
/// Represents an error in the manipulation of internal cryptographic data
2022-01-04 00:50:40 +01:00
#[derive(Clone, Copy, Display, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum InternalError {
2022-02-25 07:13:22 +01:00
/// Size of input is empty or longer then [`u16::MAX`].
HashToScalar,
/// Computing HKDF failed while deriving subkeys
2020-06-05 09:35:14 -07:00
HkdfError,
/// Computing HMAC failed while supplying a secret key
2020-06-05 09:35:14 -07:00
HmacError,
2022-04-02 01:10:00 +02:00
/// Computing the key stretching function failed
KsfError,
2021-07-31 00:36:57 +02:00
/** This error occurs when the envelope seal open hmac check fails
HMAC check in seal open failed. */
SealOpenHmacError,
2021-07-31 00:36:57 +02:00
/** This error occurs when attempting to open an envelope of the wrong
type (base mode, custom identifier) */
IncompatibleEnvelopeModeError,
2021-10-25 02:54:32 -07:00
/// Error from the OPRF evaluation
2022-01-06 00:10:57 +01:00
OprfError(voprf::Error),
2022-02-25 07:13:22 +01:00
/// Error from the OPRF evaluation
OprfInternalError(voprf::InternalError),
2020-06-05 09:35:14 -07:00
}
impl Debug for InternalError {
2021-08-12 06:25:07 +02:00
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2021-07-20 11:49:37 +02:00
match self {
2022-02-25 07:13:22 +01:00
Self::HashToScalar => f.debug_tuple("HashToScalar").finish(),
2021-07-20 11:49:37 +02:00
Self::HkdfError => f.debug_tuple("HkdfError").finish(),
Self::HmacError => f.debug_tuple("HmacError").finish(),
2022-04-02 01:10:00 +02:00
Self::KsfError => f.debug_tuple("KsfError").finish(),
2021-07-20 11:49:37 +02:00
Self::SealOpenHmacError => f.debug_tuple("SealOpenHmacError").finish(),
Self::IncompatibleEnvelopeModeError => {
f.debug_tuple("IncompatibleEnvelopeModeError").finish()
}
2021-10-25 02:54:32 -07:00
Self::OprfError(error) => f.debug_tuple("OprfError").field(error).finish(),
2022-02-25 07:13:22 +01:00
Self::OprfInternalError(error) => {
f.debug_tuple("OprfInternalError").field(error).finish()
}
2021-07-20 11:49:37 +02:00
}
}
}
impl Error for InternalError {}
2021-07-20 11:49:37 +02:00
2022-01-06 00:10:57 +01:00
impl From<voprf::Error> for InternalError {
fn from(voprf_error: voprf::Error) -> Self {
2021-10-25 02:54:32 -07:00
Self::OprfError(voprf_error)
}
}
2022-01-06 00:10:57 +01:00
impl From<voprf::Error> for ProtocolError {
fn from(voprf_error: voprf::Error) -> Self {
2021-10-25 02:54:32 -07:00
Self::LibraryError(InternalError::OprfError(voprf_error))
}
}
2022-02-25 07:13:22 +01:00
impl From<voprf::InternalError> for ProtocolError {
fn from(voprf_error: voprf::InternalError) -> Self {
Self::LibraryError(InternalError::OprfInternalError(voprf_error))
}
}
2021-08-22 12:28:19 -07:00
/// Represents an error in protocol handling
2022-01-04 00:50:40 +01:00
#[derive(Clone, Copy, Display, Eq, Hash, Ord, PartialEq, PartialOrd)]
2021-08-22 12:28:19 -07:00
pub enum ProtocolError<T = Infallible> {
/// Internal error encountered
LibraryError(InternalError),
/// Error in validating credentials
2020-06-05 09:35:14 -07:00
InvalidLoginError,
/// Error with serializing / deserializing protocol messages
SerializationError,
/// Invalid length for `name`: expected `len`, but is actually `actual_len`.
SizeError {
/// name
name: &'static str,
/// length
len: usize,
/// actual
actual_len: usize,
},
2021-08-22 12:28:19 -07:00
/** This error occurs when the client detects that the server has
reflected the OPRF value (beta == alpha) */
ReflectedValueError,
2022-01-06 06:19:02 +01:00
/** Identity group element was encountered during deserialization, which is
invalid */
IdentityGroupElementError,
/// Custom [`SecretKey`](crate::keypair::PrivateKeySerialization) error type
Custom(T),
2020-06-05 09:35:14 -07:00
}
2021-08-22 12:28:19 -07:00
impl<T: Debug> Debug for ProtocolError<T> {
2021-08-12 06:25:07 +02:00
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2021-07-20 11:49:37 +02:00
match self {
2021-08-22 12:28:19 -07:00
Self::LibraryError(pake_error) => {
f.debug_tuple("LibraryError").field(pake_error).finish()
2021-07-20 11:49:37 +02:00
}
Self::InvalidLoginError => f.debug_tuple("InvalidLoginError").finish(),
Self::SerializationError => f.debug_tuple("SerializationError").finish(),
Self::SizeError {
name,
len,
actual_len,
} => f
.debug_struct("SizeError")
.field("name", name)
.field("len", len)
.field("actual_len", actual_len)
.finish(),
2021-07-20 11:49:37 +02:00
Self::ReflectedValueError => f.debug_tuple("ReflectedValueError").finish(),
2021-08-22 12:28:19 -07:00
Self::IdentityGroupElementError => f.debug_tuple("IdentityGroupElementError").finish(),
Self::Custom(custom) => f.debug_tuple("Custom").field(custom).finish(),
2021-07-20 11:49:37 +02:00
}
}
}
impl<T: Error> Error for ProtocolError<T> {}
2020-06-05 09:35:14 -07:00
// This is meant to express future(ly) non-trivial ways of converting the
// internal error into a ProtocolError
impl<T> From<InternalError> for ProtocolError<T> {
fn from(e: InternalError) -> ProtocolError<T> {
2021-08-22 12:28:19 -07:00
Self::LibraryError(e)
2020-06-05 09:35:14 -07:00
}
}
2022-01-06 06:19:02 +01:00
// See https://github.com/rust-lang/rust/issues/64715 and remove this when merged,
// and https://github.com/dtolnay/thiserror/issues/62 for why this comes up in our
// doc tests.
2021-08-12 06:25:07 +02:00
impl<T> From<::core::convert::Infallible> for ProtocolError<T> {
fn from(_: ::core::convert::Infallible) -> Self {
2020-06-05 09:35:14 -07:00
unreachable!()
}
}
2021-07-20 11:49:37 +02:00
impl ProtocolError {
2022-12-16 23:14:53 +01:00
/// Convert `ProtocolError<Infallible>` into `ProtocolError<T>`
2021-07-20 11:49:37 +02:00
pub fn into_custom<T>(self) -> ProtocolError<T> {
match self {
Self::LibraryError(internal_error) => ProtocolError::LibraryError(internal_error),
2021-08-22 12:28:19 -07:00
Self::InvalidLoginError => ProtocolError::InvalidLoginError,
Self::SerializationError => ProtocolError::SerializationError,
Self::SizeError {
name,
len,
actual_len,
} => ProtocolError::SizeError {
name,
len,
actual_len,
},
2021-07-20 11:49:37 +02:00
Self::ReflectedValueError => ProtocolError::ReflectedValueError,
2021-08-22 12:28:19 -07:00
Self::IdentityGroupElementError => ProtocolError::IdentityGroupElementError,
2021-07-20 11:49:37 +02:00
}
}
}
2020-06-05 09:35:14 -07:00
pub(crate) mod utils {
use super::*;
pub fn check_slice_size<'a>(
2020-06-05 09:35:14 -07:00
slice: &'a [u8],
expected_len: usize,
arg_name: &'static str,
) -> Result<&'a [u8], ProtocolError> {
2020-06-05 09:35:14 -07:00
if slice.len() != expected_len {
return Err(ProtocolError::SizeError {
2020-06-05 09:35:14 -07:00
name: arg_name,
len: expected_len,
actual_len: slice.len(),
});
}
Ok(slice)
}
pub fn check_slice_size_atleast<'a>(
slice: &'a [u8],
expected_len: usize,
arg_name: &'static str,
) -> Result<&'a [u8], ProtocolError> {
if slice.len() < expected_len {
return Err(ProtocolError::SizeError {
name: arg_name,
len: expected_len,
actual_len: slice.len(),
});
}
Ok(slice)
}
2020-06-05 09:35:14 -07:00
}