Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cipher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ blobby = { version = "0.4.0-pre.1", optional = true }
block-buffer = { version = "0.11.0-rc.5", optional = true }
zeroize = { version = "1.8", optional = true, default-features = false }

[dev-dependencies]
hex-literal = "1"

[features]
alloc = []
block-padding = ["inout/block-padding"]
Expand Down
Binary file added cipher/tests/data/dummy_stream_cipher.blb
Binary file not shown.
160 changes: 160 additions & 0 deletions cipher/tests/stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
use cipher::{
BlockSizeUser, IvSizeUser, KeyIvInit, KeySizeUser, ParBlocksSizeUser, StreamCipherBackend,
StreamCipherClosure, StreamCipherCore, StreamCipherSeekCore,
consts::{U1, U4, U16},
};
use hex_literal::hex;

const KEY: [u8; 4] = hex!("00010203");
const IV: [u8; 4] = hex!("04050607");

/// Core of dummy insecure stream cipher.
pub struct DummyStreamCipherCore {
key_iv: u64,
pos: u64,
}

impl KeySizeUser for DummyStreamCipherCore {
type KeySize = U4;
}

impl IvSizeUser for DummyStreamCipherCore {
type IvSize = U4;
}

impl KeyIvInit for DummyStreamCipherCore {
fn new(key: &cipher::Key<Self>, iv: &cipher::Iv<Self>) -> Self {
let mut key_iv = [0u8; 8];
key_iv[..4].copy_from_slice(key);
key_iv[4..].copy_from_slice(iv);
Self {
key_iv: u64::from_le_bytes(key_iv),
pos: 0,
}
}
}

impl BlockSizeUser for DummyStreamCipherCore {
type BlockSize = U16;
}

impl StreamCipherCore for DummyStreamCipherCore {
fn remaining_blocks(&self) -> Option<usize> {
let rem = u64::MAX - self.pos;
usize::try_from(rem).ok()
}

fn process_with_backend(&mut self, f: impl StreamCipherClosure<BlockSize = U16>) {
f.call(self);
}
}

impl ParBlocksSizeUser for DummyStreamCipherCore {
type ParBlocksSize = U1;
}

impl StreamCipherBackend for DummyStreamCipherCore {
fn gen_ks_block(&mut self, block: &mut cipher::Block<Self>) {
const C1: u64 = 0x87c3_7b91_1142_53d5;
const C2: u64 = 0x4cf5_ad43_2745_937f;

let a = self.key_iv ^ C1;
let b = self.pos ^ C2;
let a = a.rotate_left(13).wrapping_mul(b);
let b = b.rotate_left(13).wrapping_mul(a);

block[..8].copy_from_slice(&a.to_le_bytes());
block[8..].copy_from_slice(&b.to_le_bytes());
self.pos = self.pos.wrapping_add(1);
}
}

impl StreamCipherSeekCore for DummyStreamCipherCore {
type Counter = u64;

fn get_block_pos(&self) -> Self::Counter {
self.pos
}

fn set_block_pos(&mut self, pos: Self::Counter) {
self.pos = pos;
}
}

#[test]
fn dummy_stream_cipher_core() {
let mut cipher = DummyStreamCipherCore::new(&KEY.into(), &IV.into());
assert_eq!(cipher.get_block_pos(), 0);

let mut block = [0u8; 16].into();
cipher.write_keystream_block(&mut block);
assert_eq!(block, hex!("e82393543cc96089305116003a417acc"));
assert_eq!(cipher.get_block_pos(), 1);

cipher.set_block_pos(200);
assert_eq!(cipher.get_block_pos(), 200);

cipher.write_keystream_block(&mut block);
assert_eq!(block, hex!("28a96998fe4874ffb0ce9b046c6a9ddb"));
assert_eq!(cipher.get_block_pos(), 201);
}

#[cfg(feature = "stream-wrapper")]
mod wrapper {
use super::*;
use cipher::{StreamCipher, StreamCipherCoreWrapper, StreamCipherSeek};

/// Dummy insecure stream cipher.
pub type DummyStreamCipher = StreamCipherCoreWrapper<DummyStreamCipherCore>;

#[test]
fn dummy_stream_cipher_basic() {
let mut cipher = DummyStreamCipher::new(&KEY.into(), &IV.into());
assert_eq!(cipher.current_pos::<u64>(), 0);

let mut buf = [0u8; 20];
cipher.apply_keystream(&mut buf);
assert_eq!(buf, hex!("e82393543cc96089305116003a417accd073384a"));
assert_eq!(cipher.current_pos::<usize>(), buf.len());

const SEEK_POS: usize = 500;
cipher.seek(SEEK_POS);
cipher.apply_keystream(&mut buf);
assert_eq!(buf, hex!("6b014c6a3c376b13c4720590d26147c5ebf334c5"));
assert_eq!(cipher.current_pos::<usize>(), SEEK_POS + buf.len());
}

#[test]
fn dummy_stream_cipher_seek_limit() {
let mut cipher = DummyStreamCipher::new(&KEY.into(), &IV.into());

let pos = ((u64::MAX as u128) << 4) - 20;
cipher.try_seek(pos).unwrap();

let mut buf = [0u8; 30];
let res = cipher.try_apply_keystream(&mut buf);
assert!(res.is_err());
let cur_pos: u128 = cipher.current_pos();
assert_eq!(cur_pos, pos);

let res = cipher.try_apply_keystream(&mut buf[..19]);
assert!(res.is_ok());
let cur_pos: u128 = cipher.current_pos();
assert_eq!(cur_pos, pos + 19);

cipher.try_seek(pos).unwrap();

// TODO: fix as part of https://github.com/RustCrypto/traits/issues/1808
// let res = cipher.try_apply_keystream(&mut buf[..20]);
// assert!(res.is_err());
}

#[cfg(feature = "dev")]
cipher::stream_cipher_test!(
dummy_stream_cipher,
"dummy_stream_cipher",
DummyStreamCipher,
);
#[cfg(feature = "dev")]
cipher::stream_cipher_seek_test!(dummy_stream_cipher_seek, DummyStreamCipher);
}