keystream/lib.rs
1// Copyright 2016 Peter Reid. See the COPYRIGHT file at the top-level
2// directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! In cryptography, keystreams are sequences of bytes that can be
11//! XORed with a plaintext to create a ciphertext or XORed with a
12//! ciphertext to recover the plaintext. A good keystream is
13//! nearly impossible to distinguish from random stream of bytes,
14//! which makes the ciphertext appear similarly random.
15//!
16//! This crate contains traits that that encapsulate the behavior
17//! of keystreams, which allows cryptographic operations that
18//! depend on keystreams be generic over which particular keystream
19//! they use.
20
21#![no_std]
22
23/// An error when generating a keystream
24#[derive(Debug, Copy, Clone, Eq, PartialEq)]
25pub enum Error {
26 EndReached
27}
28
29/// Types that encapsulate a stream of bytes that to be combined with a cryptographic
30/// plaintext or ciphertext
31pub trait KeyStream {
32 /// XORs keystream bytes with `dest`.
33 ///
34 /// If the end of the keystream is reached, this returns an error and the contents of
35 /// dest are undefined.
36 fn xor_read(&mut self, dest: &mut [u8]) -> Result<(), Error>;
37}
38
39/// KeyStreams that allow efficiently moving to positions in the stream
40pub trait SeekableKeyStream: KeyStream {
41 /// Seeks to a position, with byte resolution, in the keystream.
42 ///
43 /// Returns an error if the seek would pass the end of the keystream.
44 fn seek_to(&mut self, byte_offset: u64) -> Result<(), Error>;
45}