Fix handling of external error types.

This commit is contained in:
Isis Lovecruft 2020-07-16 22:19:40 +00:00
parent 69004599c5
commit 7243d7151d
No known key found for this signature in database
GPG key ID: AB41313533E8E812
4 changed files with 28 additions and 12 deletions

View file

@ -45,6 +45,9 @@ pub(crate) enum InternalError {
PrehashedContextLengthError, PrehashedContextLengthError,
} }
unsafe impl Send for InternalError {}
unsafe impl Sync for InternalError {}
impl Display for InternalError { impl Display for InternalError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self { match *self {
@ -61,7 +64,7 @@ impl Display for InternalError {
name_c: nc, length_c: lc, } name_c: nc, length_c: lc, }
=> write!(f, "Arrays must be the same length: {} has length {}, => write!(f, "Arrays must be the same length: {} has length {},
{} has length {}, {} has length {}.", na, la, nb, lb, nc, lc), {} has length {}, {} has length {}.", na, la, nb, lb, nc, lc),
InternalError::PrehashedContextError InternalError::PrehashedContextLengthError
=> write!(f, "An ed25519ph signature can only take up to 255 octets of context"), => write!(f, "An ed25519ph signature can only take up to 255 octets of context"),
} }
} }

View file

@ -207,11 +207,11 @@ impl Keypair {
/// # use ed25519_dalek::Digest; /// # use ed25519_dalek::Digest;
/// # use ed25519_dalek::Keypair; /// # use ed25519_dalek::Keypair;
/// # use ed25519_dalek::Signature; /// # use ed25519_dalek::Signature;
/// # use ed25519_dalek::SignatureError;
/// # use ed25519_dalek::Sha512; /// # use ed25519_dalek::Sha512;
/// # use rand::rngs::OsRng; /// # use rand::rngs::OsRng;
/// # /// #
/// # #[cfg(feature = "std")] /// # fn do_test() -> Result<Signature, SignatureError> {
/// # fn main() {
/// # let mut csprng = OsRng{}; /// # let mut csprng = OsRng{};
/// # let keypair: Keypair = Keypair::generate(&mut csprng); /// # let keypair: Keypair = Keypair::generate(&mut csprng);
/// # let message: &[u8] = b"All I want is to pet all of the dogs."; /// # let message: &[u8] = b"All I want is to pet all of the dogs.";
@ -220,7 +220,13 @@ impl Keypair {
/// # /// #
/// let context: &[u8] = b"Ed25519DalekSignPrehashedDoctest"; /// let context: &[u8] = b"Ed25519DalekSignPrehashedDoctest";
/// ///
/// let sig: Signature = keypair.sign_prehashed(prehashed, Some(context)); /// let sig: Signature = keypair.sign_prehashed(prehashed, Some(context))?;
/// #
/// # Ok(sig)
/// # }
/// # #[cfg(feature = "std")]
/// # fn main() {
/// # do_test();
/// # } /// # }
/// # /// #
/// # #[cfg(not(feature = "std"))] /// # #[cfg(not(feature = "std"))]
@ -233,7 +239,7 @@ impl Keypair {
&self, &self,
prehashed_message: D, prehashed_message: D,
context: Option<&[u8]>, context: Option<&[u8]>,
) -> ed25519::Signature ) -> Result<ed25519::Signature, SignatureError>
where where
D: Digest<OutputSize = U64>, D: Digest<OutputSize = U64>,
{ {
@ -278,11 +284,11 @@ impl Keypair {
/// use ed25519_dalek::Digest; /// use ed25519_dalek::Digest;
/// use ed25519_dalek::Keypair; /// use ed25519_dalek::Keypair;
/// use ed25519_dalek::Signature; /// use ed25519_dalek::Signature;
/// use ed25519_dalek::SignatureError;
/// use ed25519_dalek::Sha512; /// use ed25519_dalek::Sha512;
/// use rand::rngs::OsRng; /// use rand::rngs::OsRng;
/// ///
/// # #[cfg(feature = "std")] /// # fn do_test() -> Result<(), SignatureError> {
/// # fn main() {
/// let mut csprng = OsRng{}; /// let mut csprng = OsRng{};
/// let keypair: Keypair = Keypair::generate(&mut csprng); /// let keypair: Keypair = Keypair::generate(&mut csprng);
/// let message: &[u8] = b"All I want is to pet all of the dogs."; /// let message: &[u8] = b"All I want is to pet all of the dogs.";
@ -292,7 +298,7 @@ impl Keypair {
/// ///
/// let context: &[u8] = b"Ed25519DalekSignPrehashedDoctest"; /// let context: &[u8] = b"Ed25519DalekSignPrehashedDoctest";
/// ///
/// let sig: Signature = keypair.sign_prehashed(prehashed, Some(context)); /// let sig: Signature = keypair.sign_prehashed(prehashed, Some(context))?;
/// ///
/// // The sha2::Sha512 struct doesn't implement Copy, so we'll have to create a new one: /// // The sha2::Sha512 struct doesn't implement Copy, so we'll have to create a new one:
/// let mut prehashed_again: Sha512 = Sha512::default(); /// let mut prehashed_again: Sha512 = Sha512::default();
@ -301,6 +307,13 @@ impl Keypair {
/// let verified = keypair.public.verify_prehashed(prehashed_again, Some(context), &sig); /// let verified = keypair.public.verify_prehashed(prehashed_again, Some(context), &sig);
/// ///
/// assert!(verified.is_ok()); /// assert!(verified.is_ok());
///
/// # verified
/// # }
/// #
/// # #[cfg(feature = "std")]
/// # fn main() {
/// # do_test();
/// # } /// # }
/// # /// #
/// # #[cfg(not(feature = "std"))] /// # #[cfg(not(feature = "std"))]

View file

@ -466,7 +466,7 @@ impl ExpandedSecretKey {
let ctx: &[u8] = context.unwrap_or(b""); // By default, the context is an empty string. let ctx: &[u8] = context.unwrap_or(b""); // By default, the context is an empty string.
if ctx.len() > 255 { if ctx.len() > 255 {
return Err(SignatureError(InternalError::PrehashedContextLengthError)); return Err(SignatureError::from_source(InternalError::PrehashedContextLengthError));
} }
let ctx_len: u8 = ctx.len() as u8; let ctx_len: u8 = ctx.len() as u8;

View file

@ -104,7 +104,7 @@ mod vectors {
prehash_for_signing.input(&msg_bytes[..]); prehash_for_signing.input(&msg_bytes[..]);
prehash_for_verifying.input(&msg_bytes[..]); prehash_for_verifying.input(&msg_bytes[..]);
let sig2: Signature = keypair.sign_prehashed(prehash_for_signing, None); let sig2: Signature = keypair.sign_prehashed(prehash_for_signing, None).unwrap();
assert!(sig1 == sig2, assert!(sig1 == sig2,
"Original signature from test vectors doesn't equal signature produced:\ "Original signature from test vectors doesn't equal signature produced:\
@ -169,8 +169,8 @@ mod integrations {
let context: &[u8] = b"testing testing 1 2 3"; let context: &[u8] = b"testing testing 1 2 3";
keypair = Keypair::generate(&mut csprng); keypair = Keypair::generate(&mut csprng);
good_sig = keypair.sign_prehashed(prehashed_good1, Some(context)); good_sig = keypair.sign_prehashed(prehashed_good1, Some(context)).unwrap();
bad_sig = keypair.sign_prehashed(prehashed_bad1, Some(context)); bad_sig = keypair.sign_prehashed(prehashed_bad1, Some(context)).unwrap();
assert!(keypair.verify_prehashed(prehashed_good2, Some(context), &good_sig).is_ok(), assert!(keypair.verify_prehashed(prehashed_good2, Some(context), &good_sig).is_ok(),
"Verification of a valid signature failed!"); "Verification of a valid signature failed!");