Skip to main content

simploxide_client/crypto/
mod.rs

1pub mod fs;
2
3#[cfg(feature = "native_crypto")]
4pub mod native;
5
6pub type XSalsa20Key = [u8; 32];
7pub type XSalsa20Nonce = [u8; 24];
8pub type Poly1305Tag = [u8; 16];
9
10pub trait SimplexSecretBox {
11    /// Return a properly initialized SimpleX `secretbox`.
12    ///
13    /// Beware that SimpleX uses a non-standard initialization like this:
14    ///
15    /// intermediate = hsalsa20(xsalsa20_key, [0u8; 16]);
16    /// xsalsa20 = xsalsa20_init(intermediate, xsalsa20_nonce);
17    /// poly1305_key = (first 32 bytes of xsalsa20 cipherstream);
18    fn init(key: &XSalsa20Key, nonce: &XSalsa20Nonce) -> Self;
19
20    /// Write a ciphertext into a `buf`. Update poly1305 but do not authenticate the chunk, the
21    /// auth tag must be put only at the end of the whole message.
22    fn encrypt_chunk(&mut self, chunk: impl AsRef<[u8]>, buf: impl AsMut<[u8]>);
23
24    /// Write a plaintext into a `buf`. `chunk` is always pure ciphertext, `simploxide` utilities
25    /// guarantee that `auth_tag` won't appear in the input chunk.
26    fn decrypt_chunk(&mut self, chunk: impl AsRef<[u8]>, buf: impl AsMut<[u8]>);
27
28    fn auth_tag(&mut self) -> Poly1305Tag;
29
30    fn verify_tag(&mut self, tag_to_verify: &Poly1305Tag) -> bool;
31}
32
33#[derive(Debug, Clone, Copy)]
34pub struct InvalidAuthTag;
35
36impl InvalidAuthTag {
37    pub fn io_error() -> std::io::Error {
38        std::io::Error::new(std::io::ErrorKind::InvalidData, InvalidAuthTag)
39    }
40}
41
42impl From<InvalidAuthTag> for ::std::io::Error {
43    fn from(_: InvalidAuthTag) -> Self {
44        InvalidAuthTag::io_error()
45    }
46}
47
48impl std::fmt::Display for InvalidAuthTag {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        write!(f, "Invalid poly1305 auth tag")
51    }
52}
53
54impl std::error::Error for InvalidAuthTag {}
55
56#[derive(Debug, Clone, Copy)]
57pub struct InvalidCryptoArgs;
58
59impl InvalidCryptoArgs {
60    pub fn io_error() -> std::io::Error {
61        std::io::Error::new(std::io::ErrorKind::InvalidData, InvalidCryptoArgs)
62    }
63}
64
65impl From<InvalidCryptoArgs> for ::std::io::Error {
66    fn from(_: InvalidCryptoArgs) -> Self {
67        InvalidCryptoArgs::io_error()
68    }
69}
70
71impl std::fmt::Display for InvalidCryptoArgs {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        write!(f, "Invalid file crypto args")
74    }
75}
76
77impl std::error::Error for InvalidCryptoArgs {}