tc_buffered_cipher/errors/buffered_error.rs
1//! Buffered-cipher processing error type.
2
3use core::convert::Infallible;
4use core::error::Error;
5use core::fmt;
6
7/// A failure of a buffered adapter; `E` is the wrapped cipher's processing
8/// error, `Infallible` where there is none.
9///
10/// The AEAD adapters report their engine's own error instead.
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12#[non_exhaustive]
13pub enum BufferedError<E = Infallible> {
14 /// Processing was requested before successful initialization.
15 NotInitialized,
16 /// The output buffer is shorter than required.
17 OutputTooShort {
18 /// The output length the call needs, in bytes.
19 required: usize,
20 /// The length of the output buffer that was supplied, in bytes.
21 available: usize,
22 },
23 /// The length of the input exceeds what can be counted, or, for an
24 /// asymmetric cipher, what one block takes.
25 InputTooLong,
26 /// Finalization found a trailing partial block it cannot resolve.
27 ///
28 /// Decrypting a padded message requires a whole number of blocks, and an
29 /// unpadded mode that rejects partial blocks requires the same.
30 IncompleteLastBlock,
31 /// The padding on the final block was rejected.
32 ///
33 /// Callers must not report this separately from an authentication failure:
34 /// distinguishing the two is what a padding oracle needs.
35 CorruptPadding,
36 /// The padding scheme could not pad the final block.
37 PaddingFailed,
38 /// A fixed-size adapter's buffer is shorter than the cipher needs under the
39 /// key given to `init`.
40 BufferTooSmall {
41 /// The buffer length the cipher needs, in bytes.
42 required: usize,
43 /// The length of the adapter's buffer, in bytes.
44 available: usize,
45 },
46 /// The wrapped cipher failed; [`source`](Error::source) returns its error.
47 Cipher(E),
48}
49
50impl<E> fmt::Display for BufferedError<E> {
51 /// Writes a description of this layer only; the cipher's error is reachable
52 /// through `source`. Constant time: the fields hold only public lengths.
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 match self {
55 Self::NotInitialized => f.write_str("buffered cipher not initialized"),
56 Self::OutputTooShort {
57 required,
58 available,
59 } => write!(
60 f,
61 "output buffer is too short: requires {required} bytes, has {available}"
62 ),
63 Self::InputTooLong => f.write_str("buffered cipher input length limit exceeded"),
64 Self::IncompleteLastBlock => f.write_str("last block incomplete"),
65 Self::CorruptPadding => f.write_str("pad block corrupted"),
66 Self::PaddingFailed => f.write_str("padding could not be added"),
67 Self::BufferTooSmall {
68 required,
69 available,
70 } => write!(
71 f,
72 "fixed buffer is too short: requires {required} bytes, has {available}"
73 ),
74 Self::Cipher(_) => f.write_str("underlying cipher failed"),
75 }
76 }
77}
78
79impl<E: Error + 'static> Error for BufferedError<E> {
80 /// Returns the cipher's error for `Cipher`, and `None` otherwise. Constant time.
81 fn source(&self) -> Option<&(dyn Error + 'static)> {
82 match self {
83 Self::Cipher(error) => Some(error),
84 _ => None,
85 }
86 }
87}