Skip to main content

ps_cypher/
error.rs

1#![allow(clippy::module_name_repetitions)]
2
3use ps_buffer::BufferError;
4use ps_compress::{CompressionError, DecompressionError};
5use ps_ecc::{DecodeError, EncodeError};
6use ps_hash::HashError;
7use thiserror::Error;
8
9/// Errors returned by [`encrypt`](crate::encrypt).
10#[derive(Clone, Debug, Error)]
11#[non_exhaustive]
12pub enum EncryptionError {
13    /// Returned if buffer allocation fails.
14    #[error("buffer allocation failed: {0}")]
15    Buffer(#[from] BufferError),
16    /// Returned if ChaCha20-Poly1305 encryption fails.
17    #[error("encryption failed (chacha20poly1305)")]
18    ChaCha(#[source] chacha20poly1305::Error),
19    /// Returned if compression fails.
20    #[error("compression failed: {0}")]
21    Compression(#[from] CompressionError),
22    /// Returned if ECC encoding fails.
23    #[error("ECC encoding failed: {0}")]
24    Ecc(#[from] EncodeError),
25    /// Returned if hashing fails.
26    #[error("hashing failed: {0}")]
27    Hash(#[from] HashError),
28}
29
30/// Errors returned by [`decrypt`](crate::decrypt).
31#[derive(Clone, Debug, Error)]
32#[non_exhaustive]
33pub enum DecryptionError {
34    /// Returned if ChaCha20-Poly1305 rejects the ciphertext or key.
35    #[error("decryption failed (chacha20poly1305)")]
36    ChaCha(#[source] chacha20poly1305::Error),
37    /// Returned if decompression fails or the output would exceed the key's length bound.
38    #[error("decompression failed: {0}")]
39    Decompression(#[from] DecompressionError),
40    /// Returned if ECC decoding fails.
41    #[error("ECC decoding failed: {0}")]
42    Ecc(#[from] DecodeError),
43    /// Returned if hashing the decrypted data fails.
44    #[error("hashing failed: {0}")]
45    Hash(#[from] HashError),
46    /// Returned if the decrypted data does not hash to the decryption key.
47    #[error("decrypted data does not hash to the decryption key")]
48    KeyMismatch,
49}