Skip to main content

pamoja_security/
signature.rs

1//! The signature a device produces over a payload.
2
3use ed25519_dalek::Signature as Ed25519Signature;
4
5/// A detached ed25519 signature over a payload.
6///
7/// A signature is 64 bytes on the wire. Send it alongside the payload it covers, and
8/// the receiver checks it with the signer's [`PublicIdentity`](crate::PublicIdentity)
9/// to confirm the payload came from that device and was not altered in transit.
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub struct Signature(pub(crate) Ed25519Signature);
12
13impl Signature {
14    /// The length of a signature on the wire, in bytes.
15    pub const LEN: usize = 64;
16
17    /// Returns the 64-byte wire form of the signature.
18    ///
19    /// # Returns
20    ///
21    /// The signature encoded as 64 bytes.
22    pub fn to_bytes(&self) -> [u8; 64] {
23        self.0.to_bytes()
24    }
25
26    /// Reconstructs a signature from its 64-byte wire form.
27    ///
28    /// The bytes are not validated here; an invalid signature is rejected when it is
29    /// checked by [`PublicIdentity::verify`](crate::PublicIdentity::verify).
30    ///
31    /// # Arguments
32    ///
33    /// * `bytes` - the 64-byte encoded signature.
34    ///
35    /// # Returns
36    ///
37    /// The signature.
38    pub fn from_bytes(bytes: &[u8; 64]) -> Self {
39        Self(Ed25519Signature::from_bytes(bytes))
40    }
41}