Skip to content

Commit

Permalink
Use a specific error type in verification
Browse files Browse the repository at this point in the history
Otherwise clippy is complaining
  • Loading branch information
fjarri committed Mar 13, 2022
1 parent 07a8fda commit 28d39b7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
3 changes: 3 additions & 0 deletions nucypher-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ mod revocation_order;
mod treasure_map;
mod versioning;

/// Error returned by various `verify()` methods in the crate.
pub struct VerificationError;

pub use address::Address;
pub use fleet_state::FleetStateChecksum;
pub use hrac::HRAC;
Expand Down
8 changes: 6 additions & 2 deletions nucypher-core/src/node_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::fleet_state::FleetStateChecksum;
use crate::versioning::{
messagepack_deserialize, messagepack_serialize, ProtocolObject, ProtocolObjectInner,
};
use crate::VerificationError;

impl SerializeAsBytes for recoverable::Signature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Expand Down Expand Up @@ -275,14 +276,17 @@ impl MetadataResponse {
}

/// Verifies the metadata response and returns the contained payload.
pub fn verify(self, verifying_pk: &PublicKey) -> Result<MetadataResponsePayload, ()> {
pub fn verify(
self,
verifying_pk: &PublicKey,
) -> Result<MetadataResponsePayload, VerificationError> {
if self
.signature
.verify(verifying_pk, &self.payload.to_bytes())
{
Ok(self.payload)
} else {
Err(())
Err(VerificationError)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions nucypher-core/src/reencryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::key_frag::EncryptedKeyFrag;
use crate::versioning::{
messagepack_deserialize, messagepack_serialize, ProtocolObject, ProtocolObjectInner,
};
use crate::VerificationError;

/// A request for an Ursula to reencrypt for several capsules.
#[derive(PartialEq, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -121,18 +122,18 @@ impl ReencryptionResponse {
ursula_verifying_key: &PublicKey,
policy_encrypting_key: &PublicKey,
bob_encrypting_key: &PublicKey,
) -> Result<Box<[VerifiedCapsuleFrag]>, ()> {
) -> Result<Box<[VerifiedCapsuleFrag]>, VerificationError> {
if capsules.len() != self.cfrags.len() {
// Mismatched number of capsules and cfrags
return Err(());
return Err(VerificationError);
}

// Validate re-encryption signature
if !self.signature.verify(
ursula_verifying_key,
&signed_message(capsules, &self.cfrags),
) {
return Err(());
return Err(VerificationError);
}

let vcfrags = self
Expand All @@ -154,7 +155,7 @@ impl ReencryptionResponse {
// in the error case, but at this point nobody's interested in that.
vcfrags
.map(|vcfrags| vcfrags.into_boxed_slice())
.map_err(|_err| ())
.map_err(|_err| VerificationError)
}
}

Expand Down
5 changes: 3 additions & 2 deletions nucypher-core/src/revocation_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::key_frag::EncryptedKeyFrag;
use crate::versioning::{
messagepack_deserialize, messagepack_serialize, ProtocolObject, ProtocolObjectInner,
};
use crate::VerificationError;

/// Represents a string used by characters to perform a revocation on a specific Ursula.
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -43,7 +44,7 @@ impl RevocationOrder {
pub fn verify(
self,
alice_verifying_key: &PublicKey,
) -> Result<(Address, EncryptedKeyFrag), ()> {
) -> Result<(Address, EncryptedKeyFrag), VerificationError> {
let message = [
self.staking_provider_address.as_ref(),
&self.encrypted_kfrag.to_bytes(),
Expand All @@ -52,7 +53,7 @@ impl RevocationOrder {
if self.signature.verify(alice_verifying_key, &message) {
Ok((self.staking_provider_address, self.encrypted_kfrag))
} else {
Err(())
Err(VerificationError)
}
}
}
Expand Down

0 comments on commit 28d39b7

Please sign in to comment.