Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 253
cipher: integrate block-modes#394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
48a85f07795e4566e8d64a673eb72b0f36ed20d04af46f713014a1a83cc6b10File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| use generic_array::{ArrayLength, GenericArray, typenum::Unsigned}; | ||
| use crate::{BlockCipher, NewBlockCipher, errors::InvalidLength}; | ||
| /// Key for an algorithm that implements [`NewCipher`]. | ||
| pub type CipherKey<C> = GenericArray<u8, <C as NewCipher>::KeySize>; | ||
| /// Nonce for an algorithm that implements [`NewCipher`]. | ||
| pub type Nonce<C> = GenericArray<u8, <C as NewCipher>::NonceSize>; | ||
| /// Cipher creation trait. | ||
| /// | ||
| /// It can be used for creation of block modes, synchronous and asynchronous stream ciphers. | ||
| pub trait NewCipher: Sized { | ||
| /// Key size in bytes | ||
| type KeySize: ArrayLength<u8>; | ||
| /// Nonce size in bytes | ||
| type NonceSize: ArrayLength<u8>; | ||
| /// Create new stream cipher instance from variable length key and nonce. | ||
| fn new(key: &CipherKey<Self>, nonce: &Nonce<Self>) -> Self; | ||
| /// Create new stream cipher instance from variable length key and nonce. | ||
| #[inline] | ||
| 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(InvalidLength) | ||
| } else { | ||
| let key = GenericArray::from_slice(key); | ||
| let nonce = GenericArray::from_slice(nonce); | ||
| Ok(Self::new(key, nonce)) | ||
| } | ||
| } | ||
| } | ||
| /// Trait for types which can be initialized from a block cipher and nonce. | ||
| pub trait FromBlockCipher { | ||
| /// Block cipher | ||
| type BlockCipher: BlockCipher; | ||
| /// Nonce size in bytes | ||
| type NonceSize: ArrayLength<u8>; | ||
| /// Instantiate a stream cipher from a block cipher | ||
| fn from_block_cipher( | ||
| cipher: Self::BlockCipher, | ||
| nonce: &GenericArray<u8, Self::NonceSize>, | ||
| ) -> Self; | ||
| } | ||
| impl<C> NewCipher for C | ||
| where | ||
| C: FromBlockCipher, | ||
| C::BlockCipher: NewBlockCipher, | ||
| { | ||
| type KeySize = <<Self as FromBlockCipher>::BlockCipher as NewBlockCipher>::KeySize; | ||
| type NonceSize = <Self as FromBlockCipher>::NonceSize; | ||
| fn new(key: &CipherKey<Self>, nonce: &Nonce<Self>) -> C { | ||
| C::from_block_cipher( | ||
| <<Self as FromBlockCipher>::BlockCipher as NewBlockCipher>::new(key), | ||
| nonce, | ||
| ) | ||
| } | ||
| fn new_var(key: &[u8], nonce: &[u8]) -> Result<Self, InvalidLength> { | ||
| if nonce.len() != Self::NonceSize::USIZE { | ||
| Err(InvalidLength) | ||
| } else { | ||
| C::BlockCipher::new_var(key) | ||
| .map_err(|_| InvalidLength) | ||
| .map(|cipher| { | ||
| let nonce = GenericArray::from_slice(nonce); | ||
| Self::from_block_cipher(cipher, nonce) | ||
| }) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| mod stream; | ||
| mod block; |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo:
verstion