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
cipher: v0.3 changes (without block-modes integration) #435
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
48a85f0
cipher: v0.3 changes
newpavlov 7795e45
remove SeekNum impls for signed integers
newpavlov 66e8d64
fix block mode compilation bug
newpavlov a673eb7
change SeekNum impls to fit with the new BlockBuffer
newpavlov 2b0f36e
fix block tests
newpavlov d20d04a
fix stream tests
newpavlov f46f713
return SeenNum impl for i32
newpavlov 014a1a8
re-export blobby from root
newpavlov 3cc6b10
reorganize modules
newpavlov 07219ec
Remove WIP `block-modes` integration
tarcieri b386e9c
crypto-mac: use new_var method name
tarcieri File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use crate::{errors::InvalidLength, BlockCipher, NewBlockCipher}; | ||
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray}; | ||
|
||
/// 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) | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod block; | ||
mod stream; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//! Error types | ||
//! Error types. | ||
|
||
use core::fmt; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
As long as we're bikeshedding this method name, I've thought it would be clearer if it had
slice
in the name, e.g.new_from_slice
orfrom_slice
.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.
new_from_slice
sounds good!from_slice
is shorter, but I think will be more confusing, since it could be mistaken for a specialized version of theFrom
trait. Also usuallyfrom*
methods imply simple conversion, which is not the case here.i wonder if for stream cipher and block modes we should use plural
new_from_slices
, since they accept two slices.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.
@newpavlov sounds good