kyber_rs/cipher/stream.rs
1use thiserror::Error;
2
3use crate::xof::blake3::XOFError;
4
5/// A [`Stream`] represents a stream cipher.
6pub trait Stream {
7 /// [`xor_key_stream()`] XORs each byte in the given slice with a byte from the
8 /// cipher's key stream; `dst` and `src` must overlap entirely or not at all.
9 ///
10 /// If `dst.len() < src.len()`, [`xor_key_stream()`] should panic. It is acceptable
11 /// to pass a `dst` bigger than `src`, and in that case, [`xor_key_stream()`] will
12 /// only update `dst[..src.len()]` and will not touch the rest of `dst`.
13 ///
14 /// Multiple calls to [`xor_key_stream()`] behave as if the concatenation of
15 /// the `src` buffers was passed in a single run. That is, [`Stream`]
16 /// maintains state and does not reset at each [`xor_key_stream()`] call.
17 fn xor_key_stream(&mut self, dst: &mut [u8], src: &[u8]) -> Result<(), StreamError>;
18}
19
20impl<S: Stream + ?Sized> Stream for Box<S> {
21 fn xor_key_stream(&mut self, dst: &mut [u8], src: &[u8]) -> Result<(), StreamError> {
22 self.as_mut().xor_key_stream(dst, src)
23 }
24}
25
26#[derive(Error, Debug)]
27pub enum StreamError {
28 #[error("io error")]
29 IoError(#[from] std::io::Error),
30 #[error("XOF error")]
31 XOFError(#[from] XOFError),
32 #[error("mismatched buffer lengths")]
33 WrongBufferLengths,
34 #[error("all readers failed")]
35 ReadersFailure,
36}