pyrus_crypto/message/
header.rs

1use ed25519_dalek::Signature;
2
3use indexmap::IndexMap;
4
5use postcard;
6use serde::{Deserialize, Serialize};
7
8use crate::Fingerprint;
9use crate::error::Result;
10
11/// Header of a message.
12///
13/// For encryption the [`Header`] is used as AAD and thus it only provides 
14/// a serialization method [`Header::as_bytes`]. The header is public only 
15/// for the reasons described in [`Message::header`](super::Message::header).
16#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
17pub struct Header {
18    /// The message type and additional data. See [`MessageType`].
19    pub msg_type: MessageType,
20    /// The issuer's certificate fingerprint. Users are expected to fetch 
21    /// their certificate using this fingerprint.
22    #[serde(with = "super::ser::fpr")]
23    pub issuer: Fingerprint,
24}
25
26impl Header {
27    /// Serializes the header to a [`Vec<u8>`].
28    ///
29    /// # Errors
30    /// * [`CryptoError::SerializationError`](crate::error::CryptoError::SerializationError) 
31    /// if a serialization error occured.
32    pub fn as_bytes(&self) -> Result<Vec<u8>> {
33        Ok(postcard::to_stdvec(self)?)
34    }
35}
36
37/// Describes the message structure and provides additional data related to 
38/// that structure.
39///
40/// The [`IndexMap`] type is the same as a [`HashMap`](std::collections::HashMap), 
41/// but it preserves insertion order (important for serialization and 
42/// consistent AAD).
43#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
44pub enum MessageType {
45    /// Signed message.
46    Signed {
47        #[serde(with = "super::ser::sig")]
48        signature: Signature,
49    },
50    /// Symmetrically encrypted message.
51    EncryptedSym {
52        #[serde(with = "super::ser::salt")]
53        salt: [u8; 32],
54        #[serde(with = "super::ser::nonce")]
55        nonce: [u8; 24],
56    },
57    /// Asymmetrically encrypted message.
58    ///
59    /// `keys` is a map of recipients' fingerprints and their associated 
60    /// encrypted symmetric keys.
61    EncryptedAsm {
62        #[serde(with = "super::ser::keys")]
63        keys: IndexMap<Fingerprint, Vec<u8>>,
64        #[serde(with = "super::ser::nonce")]
65        nonce: [u8; 24],
66    },
67    /// Signed and symmetrically encrypted message.
68    SignedEncryptedSym {
69        signature: Signature,
70        salt: [u8; 32],
71        #[serde(with = "super::ser::nonce")]
72        nonce: [u8; 24],
73    },
74    /// Signed and symmetrically encrypted message.
75    ///
76    /// `keys` is a map of recipients' fingerprints and their associated 
77    /// encrypted symmetric keys.
78    SignedEncryptedAsm {
79        #[serde(with = "super::ser::sig")]
80        signature: Signature,
81        #[serde(with = "super::ser::keys")]
82        keys: IndexMap<Fingerprint, Vec<u8>>,
83        #[serde(with = "super::ser::nonce")]
84        nonce: [u8; 24],
85    },
86}