Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't serialize the already serialized payload when signing messages #55

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions manul/src/protocol/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ mod private {
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MessagePayload(#[serde(with = "SliceLike::<Base64>")] pub Box<[u8]>);

impl AsRef<[u8]> for MessagePayload {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

pub trait ProtocolMessageWrapper: Sized {
fn new_inner(maybe_message: Option<MessagePayload>) -> Self;
fn maybe_message(&self) -> &Option<MessagePayload>;
Expand Down
33 changes: 26 additions & 7 deletions manul/src/session/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use super::{
wire_format::WireFormat,
LocalError,
};
use crate::protocol::{DeserializationError, DirectMessage, EchoBroadcast, NormalBroadcast, RoundId};
use crate::protocol::{
DeserializationError, DirectMessage, EchoBroadcast, NormalBroadcast, ProtocolMessagePart, RoundId,
};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct SerializedSignature(#[serde(with = "SliceLike::<Hex>")] Box<[u8]>);
Expand Down Expand Up @@ -76,9 +78,26 @@ pub struct MessageWithMetadata<M> {
message: M,
}

impl<M: ProtocolMessagePart> MessageWithMetadata<M> {
fn digest<SP>(&self) -> Result<SP::Digest, LocalError>
where
SP: SessionParameters,
{
let digest =
SP::Digest::new_with_prefix(b"SignedMessage").chain_update(SP::WireFormat::serialize(&self.metadata)?);

let digest = match self.message.maybe_message().as_ref() {
None => digest.chain_update([0u8]),
Some(payload) => digest.chain_update([1u8]).chain_update(payload),
};

Ok(digest)
}
}

impl<M> SignedMessage<M>
where
M: Serialize,
M: ProtocolMessagePart,
{
pub fn new<SP>(
rng: &mut impl CryptoRngCore,
Expand All @@ -92,8 +111,7 @@ where
{
let metadata = MessageMetadata::new(session_id, round_id);
let message_with_metadata = MessageWithMetadata { metadata, message };
let message_bytes = SP::WireFormat::serialize(&message_with_metadata)?;
let digest = SP::Digest::new_with_prefix(b"SignedMessage").chain_update(message_bytes);
let digest = message_with_metadata.digest::<SP>()?;
let signature = signer
.try_sign_digest_with_rng(rng, digest)
.map_err(|err| LocalError::new(format!("Failed to sign: {:?}", err)))?;
Expand All @@ -115,9 +133,10 @@ where
where
SP: SessionParameters,
{
let message_bytes =
SP::WireFormat::serialize(&self.message_with_metadata).map_err(MessageVerificationError::Local)?;
let digest = SP::Digest::new_with_prefix(b"SignedMessage").chain_update(message_bytes);
let digest = self
.message_with_metadata
.digest::<SP>()
.map_err(MessageVerificationError::Local)?;
let signature = self
.signature
.deserialize::<SP>()
Expand Down
Loading