Skip to main content

fedimint_aead/
error.rs

1use thiserror::Error;
2
3/// Why [`encrypt`](crate::encrypt) could not seal a plaintext.
4///
5/// The cipher refuses a plaintext longer than it can process as one message.
6#[derive(Debug, Error)]
7#[error("Encryption failed due to unspecified aead error")]
8pub struct EncryptError;
9
10/// Why [`decrypt`](crate::decrypt) could not open a ciphertext.
11#[derive(Debug, Error)]
12#[non_exhaustive]
13pub enum DecryptError {
14    /// The ciphertext is shorter than the nonce it must start with.
15    #[error("Ciphertext too short: {len}")]
16    CiphertextTooShort {
17        /// The length of the ciphertext, in bytes.
18        len: usize,
19    },
20
21    /// The ciphertext could not be opened: it does not authenticate under
22    /// the key (the key is wrong or the ciphertext was altered), it is too
23    /// short to hold the tag, or it is longer than the cipher can process
24    /// as one message.
25    #[error("Decryption failed due to unspecified aead error")]
26    Open,
27}
28
29/// Why [`encrypted_write`](crate::encrypted_write) could not write a file.
30#[derive(Debug, Error)]
31#[non_exhaustive]
32pub enum EncryptedWriteError {
33    /// The file could not be created, written or synced; it must not exist
34    /// yet.
35    #[error(transparent)]
36    Io(#[from] std::io::Error),
37
38    /// The data could not be encrypted.
39    #[error(transparent)]
40    Encrypt(#[from] EncryptError),
41}
42
43/// Why [`encrypted_read`](crate::encrypted_read) could not read a file.
44#[derive(Debug, Error)]
45#[non_exhaustive]
46pub enum EncryptedReadError {
47    /// The file could not be read as UTF-8 text.
48    #[error(transparent)]
49    Io(#[from] std::io::Error),
50
51    /// The file's content is not hex.
52    #[error(transparent)]
53    Hex(#[from] hex::FromHexError),
54
55    /// The decoded content could not be decrypted.
56    #[error(transparent)]
57    Decrypt(#[from] DecryptError),
58}
59
60/// Why [`get_encryption_key`](crate::get_encryption_key) could not derive a
61/// key: Argon2 rejected the password or the salt, such as a salt that is too
62/// short.
63#[derive(Debug, Error)]
64#[error("could not hash password")]
65pub struct EncryptionKeyError(#[source] pub(crate) argon2::Error);