Skip to content

Commit

Permalink
Don't serialize the already serialized payload when signing messages
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Oct 30, 2024
1 parent cef7fab commit 5ead91a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
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

0 comments on commit 5ead91a

Please sign in to comment.