Skip to content

Commit

Permalink
add new_var method
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Oct 16, 2018
1 parent 703e348 commit caca608
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
2 changes: 1 addition & 1 deletion stream-cipher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stream-cipher"
version = "0.2.1"
version = "0.2.2"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
description = "Stream cipher traits"
Expand Down
12 changes: 6 additions & 6 deletions stream-cipher/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ macro_rules! new_core_test {

let data = include_bytes!(concat!("data/", $test_name, ".blb"));
for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() {
let key = GenericArray::from_slice(row[0]);
let iv = GenericArray::from_slice(row[1]);
let key = row[0];
let iv = row[1];
let plaintext = row[2];
let ciphertext = row[3];

for chunk_n in 1..256 {
let mut mode = <$cipher>::new(key, iv);
let mut mode = <$cipher>::new_var(key, iv).unwrap();
let mut pt = plaintext.to_vec();
for chunk in pt.chunks_mut(chunk_n) {
mode.apply_keystream(chunk);
Expand Down Expand Up @@ -52,12 +52,12 @@ macro_rules! new_seek_test {

let data = include_bytes!(concat!("data/", $test_name, ".blb"));
for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() {
let key = GenericArray::from_slice(row[0]);
let iv = GenericArray::from_slice(row[1]);
let key = row[0];
let iv = row[1];
let plaintext = row[2];
let ciphertext = row[3];

let mut mode = <$cipher>::new(key, iv);
let mut mode = <$cipher>::new_var(key, iv).unwrap();
for seek_n in 0..512 {
let mut pt = plaintext[seek_n..].to_vec();
mode.seek(seek_n as u64);
Expand Down
38 changes: 38 additions & 0 deletions stream-cipher/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use core::fmt;
#[cfg(feature = "std")]
use std::error;

/// Error which notifies that stream cipher has reached the end of a keystream.
#[derive(Copy, Clone, Debug)]
pub struct LoopError;

impl fmt::Display for LoopError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str("Loop Error")
}
}

#[cfg(feature = "std")]
impl error::Error for LoopError {
fn description(&self) -> &str {
"stream cipher loop detected"
}
}

/// Error which notifies that key or/and nonce used in stream cipher
/// initialization had an invalid length.
#[derive(Copy, Clone, Debug)]
pub struct InvalidKeyNonceLength;

impl fmt::Display for InvalidKeyNonceLength {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str("Loop Error")
}
}

#[cfg(feature = "std")]
impl error::Error for InvalidKeyNonceLength {
fn description(&self) -> &str {
"stream cipher loop detected"
}
}
40 changes: 21 additions & 19 deletions stream-cipher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,22 @@
//! See [RustCrypto/stream-ciphers](https://github.com/RustCrypto/stream-ciphers)
//! for ciphers implementation.
#![no_std]
#![doc(html_logo_url =
"https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")]
pub extern crate generic_array;
#[cfg(feature = "dev")]
pub extern crate blobby;
#[cfg(feature = "std")]
extern crate std;

use generic_array::{GenericArray, ArrayLength};
use core::fmt;
use generic_array::typenum::Unsigned;

#[cfg(feature = "dev")]
pub mod dev;
mod errors;

/// Error which notifies that stream cipher has reached the end of a keystream.
#[derive(Copy, Clone, Debug)]
pub struct LoopError;

impl fmt::Display for LoopError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str("Loop Error")
}
}

#[cfg(feature = "std")]
impl std::error::Error for LoopError {
fn description(&self) -> &str {
"stream cipher loop detected"
}
}
pub use errors::{LoopError, InvalidKeyNonceLength};

/// Synchronous stream cipher core trait
pub trait StreamCipherCore {
Expand Down Expand Up @@ -65,16 +53,30 @@ pub trait StreamCipherSeek {
fn seek(&mut self, pos: u64);
}

//TODO: rename to NewStreamCipher in next minor release
/// Synchronous stream cipher creation trait
pub trait NewFixStreamCipher {
pub trait NewFixStreamCipher: Sized {
/// Key size in bytes
type KeySize: ArrayLength<u8>;
/// Nonce size in bytes
type NonceSize: ArrayLength<u8>;

/// Create new stream cipher instance
/// Create new stream cipher instance from variable length key and nonce.
fn new(
key: &GenericArray<u8, Self::KeySize>,
nonce: &GenericArray<u8, Self::NonceSize>,
) -> Self;

/// Create new stream cipher instance from variable length key and nonce.
fn new_var(key: &[u8], nonce: &[u8]) -> Result<Self, InvalidKeyNonceLength> {
let kl = Self::KeySize::to_usize();
let nl = Self::NonceSize::to_usize();
if key.len() != kl || nonce.len() != nl {
Err(InvalidKeyNonceLength)
} else {
let key = GenericArray::from_slice(key);
let nonce = GenericArray::from_slice(nonce);
Ok(Self::new(key, nonce))
}
}
}

0 comments on commit caca608

Please sign in to comment.