Simplifying error handling (#232)

This commit is contained in:
Kevin Lewi
2021-08-22 12:28:19 -07:00
committed by GitHub
parent a99a934ead
commit aee4b5d50e
18 changed files with 177 additions and 324 deletions
+4 -4
View File
@@ -3,7 +3,7 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
use crate::errors::{InternalPakeError, ProtocolError};
use crate::errors::{InternalError, ProtocolError};
use crate::hash::Hash;
use crate::serialization::i2osp;
use alloc::vec::Vec;
@@ -16,9 +16,9 @@ fn div_ceil(x: usize, y: usize) -> usize {
x / y + additive
}
fn xor(x: &[u8], y: &[u8]) -> Result<Vec<u8>, InternalPakeError> {
fn xor(x: &[u8], y: &[u8]) -> Result<Vec<u8>, InternalError> {
if x.len() != y.len() {
return Err(InternalPakeError::HashToCurveError);
return Err(InternalError::HashToCurveError);
}
Ok(x.iter().zip(y).map(|(&x1, &x2)| x1 ^ x2).collect())
@@ -36,7 +36,7 @@ pub fn expand_message_xmd<H: Hash>(
let ell = div_ceil(len_in_bytes, b_in_bytes);
if ell > 255 {
return Err(InternalPakeError::HashToCurveError.into());
return Err(InternalError::HashToCurveError.into());
}
let dst_prime = [dst, &i2osp(dst.len(), 1)?].concat();
let z_pad = i2osp(0, r_in_bytes)?;
+3 -3
View File
@@ -12,7 +12,7 @@ pub(crate) mod p256;
mod ristretto;
mod x25519;
use crate::errors::{InternalPakeError, ProtocolError};
use crate::errors::{InternalError, ProtocolError};
use crate::hash::Hash;
use core::ops::Mul;
use generic_array::{ArrayLength, GenericArray};
@@ -47,7 +47,7 @@ pub trait Group: Copy + Sized + for<'a> Mul<&'a <Self as Group>::Scalar, Output
/// Return a scalar from its fixed-length bytes representation
fn from_scalar_slice(
scalar_bits: &GenericArray<u8, Self::ScalarLen>,
) -> Result<Self::Scalar, InternalPakeError>;
) -> Result<Self::Scalar, InternalError>;
/// picks a scalar at random
fn random_nonzero_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar;
/// Serializes a scalar to bytes
@@ -60,7 +60,7 @@ pub trait Group: Copy + Sized + for<'a> Mul<&'a <Self as Group>::Scalar, Output
/// Return an element from its fixed-length bytes representation
fn from_element_slice(
element_bits: &GenericArray<u8, Self::ElemLen>,
) -> Result<Self, InternalPakeError>;
) -> Result<Self, InternalError>;
/// Serializes the `self` group element
fn to_arr(&self) -> GenericArray<u8, Self::ElemLen>;
+6 -6
View File
@@ -9,7 +9,7 @@
)]
use super::Group;
use crate::errors::{InternalPakeError, ProtocolError};
use crate::errors::{InternalError, ProtocolError};
use crate::hash::Hash;
use core::ops::{Add, Div, Mul, Neg, Sub};
use core::str::FromStr;
@@ -71,12 +71,12 @@ impl Group for ProjectivePoint {
let p0 = AffinePoint::from_encoded_point(&EncodedPoint::from_affine_coordinates(
&q0x, &q0y, false,
))
.ok_or(InternalPakeError::PointError)?
.ok_or(InternalError::PointError)?
.to_curve();
let p1 = AffinePoint::from_encoded_point(&EncodedPoint::from_affine_coordinates(
&q1x, &q1y, false,
))
.ok_or(InternalPakeError::PointError)?;
.ok_or(InternalError::PointError)?;
Ok(p0 + p1)
}
@@ -113,7 +113,7 @@ impl Group for ProjectivePoint {
fn from_scalar_slice(
scalar_bits: &GenericArray<u8, Self::ScalarLen>,
) -> Result<Self::Scalar, InternalPakeError> {
) -> Result<Self::Scalar, InternalError> {
Ok(Self::Scalar::from_bytes_reduced(scalar_bits))
}
@@ -131,8 +131,8 @@ impl Group for ProjectivePoint {
fn from_element_slice(
element_bits: &GenericArray<u8, Self::ElemLen>,
) -> Result<Self, InternalPakeError> {
Option::from(Self::from_bytes(element_bits)).ok_or(InternalPakeError::PointError)
) -> Result<Self, InternalError> {
Option::from(Self::from_bytes(element_bits)).ok_or(InternalError::PointError)
}
fn to_arr(&self) -> GenericArray<u8, Self::ElemLen> {
+6 -6
View File
@@ -4,7 +4,7 @@
// LICENSE file in the root directory of this source tree.
use super::Group;
use crate::errors::{InternalPakeError, ProtocolError};
use crate::errors::{InternalError, ProtocolError};
use crate::hash::Hash;
use core::convert::TryInto;
use curve25519_dalek::{
@@ -30,7 +30,7 @@ impl Group for RistrettoPoint {
uniform_bytes
.as_slice()
.try_into()
.map_err(|_| InternalPakeError::HashToCurveError)?,
.map_err(|_| InternalError::HashToCurveError)?,
))
}
@@ -43,7 +43,7 @@ impl Group for RistrettoPoint {
uniform_bytes
.as_slice()
.try_into()
.map_err(|_| InternalPakeError::HashToCurveError)?,
.map_err(|_| InternalError::HashToCurveError)?,
))
}
@@ -51,7 +51,7 @@ impl Group for RistrettoPoint {
type ScalarLen = U32;
fn from_scalar_slice(
scalar_bits: &GenericArray<u8, Self::ScalarLen>,
) -> Result<Self::Scalar, InternalPakeError> {
) -> Result<Self::Scalar, InternalError> {
Ok(Scalar::from_bytes_mod_order(*scalar_bits.as_ref()))
}
fn random_nonzero_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar {
@@ -89,10 +89,10 @@ impl Group for RistrettoPoint {
type ElemLen = U32;
fn from_element_slice(
element_bits: &GenericArray<u8, Self::ElemLen>,
) -> Result<Self, InternalPakeError> {
) -> Result<Self, InternalError> {
CompressedRistretto::from_slice(element_bits)
.decompress()
.ok_or(InternalPakeError::PointError)
.ok_or(InternalError::PointError)
}
// serialization of a group element
fn to_arr(&self) -> GenericArray<u8, Self::ElemLen> {
+6 -8
View File
@@ -4,7 +4,7 @@
// LICENSE file in the root directory of this source tree.
use super::Group;
use crate::errors::{InternalPakeError, ProtocolError};
use crate::errors::{InternalError, ProtocolError};
use crate::hash::Hash;
use curve25519_dalek::{constants::X25519_BASEPOINT, montgomery::MontgomeryPoint, scalar::Scalar};
use generic_array::{typenum::U32, GenericArray};
@@ -26,7 +26,7 @@ impl Group for MontgomeryPoint {
type ScalarLen = U32;
fn from_scalar_slice(
scalar_bits: &GenericArray<u8, Self::ScalarLen>,
) -> Result<Self::Scalar, InternalPakeError> {
) -> Result<Self::Scalar, InternalError> {
Ok(Scalar::from_bytes_mod_order(*scalar_bits.as_ref()))
}
fn random_nonzero_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar {
@@ -64,7 +64,7 @@ impl Group for MontgomeryPoint {
type ElemLen = U32;
fn from_element_slice(
element_bits: &GenericArray<u8, Self::ElemLen>,
) -> Result<Self, InternalPakeError> {
) -> Result<Self, InternalError> {
Ok(Self(*element_bits.as_ref()))
}
// serialization of a group element
@@ -93,8 +93,8 @@ impl Group for MontgomeryPoint {
#[test]
fn test() -> Result<(), ProtocolError> {
use crate::{
errors::PakeError, key_exchange::tripledh::TripleDH, slow_hash::NoOpHash, CipherSuite,
ClientLogin, ClientLoginFinishParameters, ClientLoginFinishResult, ClientLoginStartResult,
key_exchange::tripledh::TripleDH, slow_hash::NoOpHash, CipherSuite, ClientLogin,
ClientLoginFinishParameters, ClientLoginFinishResult, ClientLoginStartResult,
ClientRegistration, ClientRegistrationFinishParameters, ClientRegistrationFinishResult,
ClientRegistrationStartResult, ServerLogin, ServerLoginStartParameters,
ServerLoginStartResult, ServerRegistration, ServerSetup,
@@ -173,9 +173,7 @@ fn test() -> Result<(), ProtocolError> {
assert!(matches!(
client.finish(message, ClientLoginFinishParameters::Default),
Err(ProtocolError::VerificationError(
PakeError::InvalidLoginError
))
Err(ProtocolError::InvalidLoginError)
));
Ok(())