holochain_integrity_types/
signature.rs

1//! Signature for authenticity of data
2use holo_hash::AgentPubKey;
3use holochain_secure_primitive::secure_primitive;
4use holochain_serialized_bytes::prelude::*;
5
6/// Ed25519 signatures are always the same length, 64 bytes.
7pub const SIGNATURE_BYTES: usize = 64;
8
9/// The raw bytes of a signature.
10#[derive(Clone, PartialOrd, Hash, Ord)]
11// The equality is not different, it's just constant time, so we can derive a hash.
12// For an actually secure thing we wouldn't want to just assume a safe default hashing
13// But that is not what clippy is complaining about here.
14#[allow(clippy::derived_hash_with_manual_eq)]
15pub struct Signature(pub [u8; SIGNATURE_BYTES]);
16
17// This is more for convenience/convention that being worried
18// about things like constant time equality.
19// Signature verification should always defer to the host.
20// What's nice about this is that we can easily handle fixed size signatures.
21secure_primitive!(Signature, SIGNATURE_BYTES);
22
23/// The output of ephemeral signing.
24/// The private key for this public key has been discarded by this point.
25/// The signatures match the public key provided but cannot be reproduced
26/// or forged because the private key no longer exists.
27/// The signatures match the input items positionally in the vector,
28/// it is up to the caller to reconstruct/align/zip them back together.
29#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
30pub struct EphemeralSignatures {
31    /// The public key associated with the now-discarded private key used to sign.
32    pub key: holo_hash::AgentPubKey,
33    /// The signatures for the input data to be matched in order, pairwise.
34    pub signatures: Vec<Signature>,
35}
36
37/// Mirror struct for Sign that includes a signature to verify against a key and data.
38#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, SerializedBytes)]
39pub struct VerifySignature {
40    /// The public key associated with the private key that should be used to
41    /// verify the signature.
42    pub key: holo_hash::AgentPubKey,
43
44    /// The signature being verified.
45    pub signature: Signature,
46
47    /// The signed data
48    #[serde(with = "serde_bytes")]
49    pub data: Vec<u8>,
50}
51
52impl AsRef<Signature> for VerifySignature {
53    fn as_ref(&self) -> &Signature {
54        &self.signature
55    }
56}
57
58impl AsRef<holo_hash::AgentPubKey> for VerifySignature {
59    fn as_ref(&self) -> &AgentPubKey {
60        &self.key
61    }
62}
63
64impl VerifySignature {
65    /// Alias for as_ref for data.
66    pub fn as_data_ref(&self) -> &[u8] {
67        self.data.as_ref()
68    }
69
70    /// Alias for as_ref for signature.
71    pub fn as_signature_ref(&self) -> &Signature {
72        self.as_ref()
73    }
74
75    /// Alias for as_ref for agent key.
76    pub fn as_key_ref(&self) -> &holo_hash::AgentPubKey {
77        self.as_ref()
78    }
79
80    /// construct a new VerifySignature struct.
81    pub fn new<D>(
82        key: holo_hash::AgentPubKey,
83        signature: Signature,
84        data: D,
85    ) -> Result<Self, SerializedBytesError>
86    where
87        D: serde::Serialize + std::fmt::Debug,
88    {
89        Ok(Self {
90            key,
91            signature,
92            data: holochain_serialized_bytes::encode(&data)?,
93        })
94    }
95
96    /// construct a new Sign struct from raw bytes.
97    pub fn new_raw(key: holo_hash::AgentPubKey, signature: Signature, data: Vec<u8>) -> Self {
98        Self {
99            key,
100            signature,
101            data,
102        }
103    }
104}
105
106#[test]
107fn signature_roundtrip() {
108    let bytes = Signature::from([1u8; 64]);
109    let json = serde_json::to_string(&bytes).unwrap();
110    let _: Signature = serde_json::from_str(&json).unwrap();
111}