Skip to content

Commit

Permalink
Fix handling of external error types.
Browse files Browse the repository at this point in the history
  • Loading branch information
isislovecruft committed Jul 16, 2020
1 parent 6900459 commit 7243d71
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub(crate) enum InternalError {
PrehashedContextLengthError,
}

unsafe impl Send for InternalError {}
unsafe impl Sync for InternalError {}

impl Display for InternalError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Expand All @@ -61,7 +64,7 @@ impl Display for InternalError {
name_c: nc, length_c: lc, }
=> write!(f, "Arrays must be the same length: {} has length {},
{} 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"),
}
}
Expand Down
27 changes: 20 additions & 7 deletions src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ impl Keypair {
/// # use ed25519_dalek::Digest;
/// # use ed25519_dalek::Keypair;
/// # use ed25519_dalek::Signature;
/// # use ed25519_dalek::SignatureError;
/// # use ed25519_dalek::Sha512;
/// # use rand::rngs::OsRng;
/// #
/// # #[cfg(feature = "std")]
/// # fn main() {
/// # fn do_test() -> Result<Signature, SignatureError> {
/// # let mut csprng = OsRng{};
/// # let keypair: Keypair = Keypair::generate(&mut csprng);
/// # let message: &[u8] = b"All I want is to pet all of the dogs.";
Expand All @@ -220,7 +220,13 @@ impl Keypair {
/// #
/// 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"))]
Expand All @@ -233,7 +239,7 @@ impl Keypair {
&self,
prehashed_message: D,
context: Option<&[u8]>,
) -> ed25519::Signature
) -> Result<ed25519::Signature, SignatureError>
where
D: Digest<OutputSize = U64>,
{
Expand Down Expand Up @@ -278,11 +284,11 @@ impl Keypair {
/// use ed25519_dalek::Digest;
/// use ed25519_dalek::Keypair;
/// use ed25519_dalek::Signature;
/// use ed25519_dalek::SignatureError;
/// use ed25519_dalek::Sha512;
/// use rand::rngs::OsRng;
///
/// # #[cfg(feature = "std")]
/// # fn main() {
/// # fn do_test() -> Result<(), SignatureError> {
/// let mut csprng = OsRng{};
/// let keypair: Keypair = Keypair::generate(&mut csprng);
/// let message: &[u8] = b"All I want is to pet all of the dogs.";
Expand All @@ -292,7 +298,7 @@ impl Keypair {
///
/// 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:
/// let mut prehashed_again: Sha512 = Sha512::default();
Expand All @@ -301,6 +307,13 @@ impl Keypair {
/// let verified = keypair.public.verify_prehashed(prehashed_again, Some(context), &sig);
///
/// assert!(verified.is_ok());
///
/// # verified
/// # }
/// #
/// # #[cfg(feature = "std")]
/// # fn main() {
/// # do_test();
/// # }
/// #
/// # #[cfg(not(feature = "std"))]
Expand Down
2 changes: 1 addition & 1 deletion src/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ impl ExpandedSecretKey {
let ctx: &[u8] = context.unwrap_or(b""); // By default, the context is an empty string.

if ctx.len() > 255 {
return Err(SignatureError(InternalError::PrehashedContextLengthError));
return Err(SignatureError::from_source(InternalError::PrehashedContextLengthError));
}

let ctx_len: u8 = ctx.len() as u8;
Expand Down
6 changes: 3 additions & 3 deletions tests/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mod vectors {
prehash_for_signing.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,
"Original signature from test vectors doesn't equal signature produced:\
Expand Down Expand Up @@ -169,8 +169,8 @@ mod integrations {
let context: &[u8] = b"testing testing 1 2 3";

keypair = Keypair::generate(&mut csprng);
good_sig = keypair.sign_prehashed(prehashed_good1, Some(context));
bad_sig = keypair.sign_prehashed(prehashed_bad1, Some(context));
good_sig = keypair.sign_prehashed(prehashed_good1, Some(context)).unwrap();
bad_sig = keypair.sign_prehashed(prehashed_bad1, Some(context)).unwrap();

assert!(keypair.verify_prehashed(prehashed_good2, Some(context), &good_sig).is_ok(),
"Verification of a valid signature failed!");
Expand Down

0 comments on commit 7243d71

Please sign in to comment.