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
9 changes: 3 additions & 6 deletions cipher/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))]
pub mod dev;

mod errors;

pub use errors::InvalidKeyLength;

use crate::errors::InvalidLength;
use core::convert::TryInto;
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};

Expand All @@ -41,9 +38,9 @@ pub trait NewBlockCipher: Sized {
///
/// Default implementation will accept only keys with length equal to
/// `KeySize`, but some ciphers can accept range of key lengths.
fn new_varkey(key: &[u8]) -> Result<Self, InvalidKeyLength> {
fn new_varkey(key: &[u8]) -> Result<Self, InvalidLength> {
if key.len() != Self::KeySize::to_usize() {
Err(InvalidKeyLength)
Err(InvalidLength)
} else {
Ok(Self::new(GenericArray::from_slice(key)))
}
Expand Down
14 changes: 0 additions & 14 deletions cipher/src/block/errors.rs

This file was deleted.

8 changes: 5 additions & 3 deletions cipher/src/stream/errors.rs → cipher/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Error types

use core::fmt;

/// The error type returned when stream cipher has reached the end of a keystream.
Expand All @@ -16,16 +18,16 @@ impl std::error::Error for LoopError {}
/// The error type returned when key and/or nonce used in stream cipher
/// initialization had an invalid length.
#[derive(Copy, Clone, Debug)]
pub struct InvalidKeyNonceLength;
pub struct InvalidLength;

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

#[cfg(feature = "std")]
impl std::error::Error for InvalidKeyNonceLength {}
impl std::error::Error for InvalidLength {}

/// The error type returned when a cipher position can not be represented
/// by the requested type.
Expand Down
1 change: 1 addition & 0 deletions cipher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
extern crate std;

pub mod block;
pub mod errors;
pub mod stream;

pub use crate::{
Expand Down
19 changes: 9 additions & 10 deletions cipher/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
#[cfg(feature = "dev")]
mod dev;

mod errors;

pub use errors::{InvalidKeyNonceLength, LoopError, OverflowError};

#[cfg(feature = "dev")]
pub use blobby;

use crate::block::{BlockCipher, NewBlockCipher};
use crate::{
block::{BlockCipher, NewBlockCipher},
errors::{InvalidLength, LoopError, OverflowError},
};
use core::convert::{TryFrom, TryInto};
use generic_array::typenum::Unsigned;
use generic_array::{ArrayLength, GenericArray};
Expand All @@ -39,11 +38,11 @@ pub trait NewStreamCipher: Sized {

/// Create new stream cipher instance from variable length key and nonce.
#[inline]
fn new_var(key: &[u8], nonce: &[u8]) -> Result<Self, InvalidKeyNonceLength> {
fn new_var(key: &[u8], nonce: &[u8]) -> Result<Self, InvalidLength> {
let kl = Self::KeySize::to_usize();
let nl = Self::NonceSize::to_usize();
if key.len() != kl || nonce.len() != nl {
Err(InvalidKeyNonceLength)
Err(InvalidLength)
} else {
let key = GenericArray::from_slice(key);
let nonce = GenericArray::from_slice(nonce);
Expand Down Expand Up @@ -178,12 +177,12 @@ where
)
}

fn new_var(key: &[u8], nonce: &[u8]) -> Result<Self, InvalidKeyNonceLength> {
fn new_var(key: &[u8], nonce: &[u8]) -> Result<Self, InvalidLength> {
if nonce.len() != Self::NonceSize::USIZE {
Err(InvalidKeyNonceLength)
Err(InvalidLength)
} else {
C::BlockCipher::new_varkey(key)
.map_err(|_| InvalidKeyNonceLength)
.map_err(|_| InvalidLength)
.map(|cipher| {
let nonce = GenericArray::from_slice(nonce);
Self::from_block_cipher(cipher, nonce)
Expand Down