-
Notifications
You must be signed in to change notification settings - Fork 56
ssh-cipher: consolidate aes feature; extract Aes type
#530
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,81 @@ | ||
| //! AES block cipher. | ||
|
|
||
| use ::aes::{Aes128, Aes192, Aes256, Block}; | ||
| use ::cipher::{ | ||
| BlockCipherDecClosure, BlockCipherDecrypt, BlockCipherEncClosure, BlockCipherEncrypt, | ||
| BlockSizeUser, InvalidLength, KeyInit, array::sizes::U16, | ||
| }; | ||
|
|
||
| /// Advanced Encryption Standard (AES) low-level block cipher. | ||
| /// | ||
| /// Supports 128-bit, 192-bit, and 256-bit key sizes. | ||
| pub(crate) enum Aes { | ||
| Aes128(Aes128), | ||
| Aes192(Aes192), | ||
| Aes256(Aes256), | ||
| } | ||
|
|
||
| impl Aes { | ||
| /// Create a new AES block cipher instance. | ||
| /// | ||
| /// Supports `key` whose length is 16-bytes (128-bit), 24-bytes (192-bits), or 32-bytes | ||
| /// (256-bits). | ||
| /// | ||
| /// # Errors | ||
| /// Returns [`InvalidLength`] if the length of `key` is not any of the above. | ||
| pub(crate) fn new(key: &[u8]) -> Result<Self, InvalidLength> { | ||
| if let Ok(cipher) = Aes128::new_from_slice(key) { | ||
| return Ok(Aes::Aes128(cipher)); | ||
| } | ||
|
|
||
| if let Ok(cipher) = Aes192::new_from_slice(key) { | ||
| return Ok(Aes::Aes192(cipher)); | ||
| } | ||
|
|
||
| if let Ok(cipher) = Aes256::new_from_slice(key) { | ||
| return Ok(Aes::Aes256(cipher)); | ||
| } | ||
|
|
||
| Err(InvalidLength) | ||
| } | ||
| } | ||
|
|
||
| impl BlockCipherDecrypt for Aes { | ||
| fn decrypt_blocks(&self, blocks: &mut [Block]) { | ||
| match self { | ||
| Aes::Aes128(aes) => aes.decrypt_blocks(blocks), | ||
| Aes::Aes192(aes) => aes.decrypt_blocks(blocks), | ||
| Aes::Aes256(aes) => aes.decrypt_blocks(blocks), | ||
| } | ||
| } | ||
|
|
||
| fn decrypt_with_backend(&self, f: impl BlockCipherDecClosure<BlockSize = Self::BlockSize>) { | ||
| match self { | ||
| Aes::Aes128(aes) => aes.decrypt_with_backend(f), | ||
| Aes::Aes192(aes) => aes.decrypt_with_backend(f), | ||
| Aes::Aes256(aes) => aes.decrypt_with_backend(f), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl BlockCipherEncrypt for Aes { | ||
| fn encrypt_blocks(&self, blocks: &mut [Block]) { | ||
| match self { | ||
| Aes::Aes128(aes) => aes.encrypt_blocks(blocks), | ||
| Aes::Aes192(aes) => aes.encrypt_blocks(blocks), | ||
| Aes::Aes256(aes) => aes.encrypt_blocks(blocks), | ||
| } | ||
| } | ||
|
|
||
| fn encrypt_with_backend(&self, f: impl BlockCipherEncClosure<BlockSize = Self::BlockSize>) { | ||
| match self { | ||
| Aes::Aes128(aes) => aes.encrypt_with_backend(f), | ||
| Aes::Aes192(aes) => aes.encrypt_with_backend(f), | ||
| Aes::Aes256(aes) => aes.encrypt_with_backend(f), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl BlockSizeUser for Aes { | ||
| type BlockSize = U16; | ||
| } | ||
This file contains hidden or 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.
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.
@newpavlov I wonder if something like this would be useful in the
aescrate itself (or perhaps a single type that supports all three key sizes and can dynamically select which one at runtime without having to monomorphize the full code three times)