-
Notifications
You must be signed in to change notification settings - Fork 249
aead: rework traits API #2427
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
Open
newpavlov
wants to merge
7
commits into
master
Choose a base branch
from
aead/minimize
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+380
−456
Open
aead: rework traits API #2427
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cabc81c
aead: rework traits API
newpavlov 7a82df0
test zeroization on failure
newpavlov ac4b579
remove check methods
newpavlov 05f7d07
tweak `AeadCore` docs
newpavlov 2627938
tweak docs
newpavlov 908c978
do not re-export `Unsigned`
newpavlov 1032fea
remove `VariableAead`
newpavlov 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,93 @@ | ||
| use crate::{AeadCore, Error, Result}; | ||
| use alloc::vec::Vec; | ||
|
|
||
| /// High-level functionality of Authenticated Encryption with Associated Data (AEAD) algorithms. | ||
| pub trait Aead { | ||
| /// Encrypt the given plaintext payload, and return the resulting | ||
| /// ciphertext as a vector of bytes. | ||
| /// | ||
| /// # Errors | ||
| /// AEAD algorithm implementations may return an error if the plaintext or AAD are too long. | ||
| fn encrypt_into_vec(&self, nonce: &[u8], aad: &[u8], plaintext: &[u8]) -> Result<Vec<u8>>; | ||
|
|
||
| /// Decrypt the given ciphertext slice, and return the resulting plaintext | ||
| /// as a vector of bytes. | ||
| /// | ||
| /// # Errors | ||
| /// - if the `ciphertext` is inauthentic (i.e. tag verification failure) | ||
| /// - if the `ciphertext` is too long | ||
| /// - if the `aad` is too long | ||
| fn decrypt_into_vec(&self, nonce: &[u8], aad: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>>; | ||
|
|
||
| /// Encrypt plaintext in `buf` extending it as necessary. | ||
| /// | ||
| /// On success, `buf` will contain the resulting ciphertext, | ||
| /// while on error it will be left intact. | ||
| /// | ||
| /// # Errors | ||
| /// AEAD algorithm implementations may return an error if the plaintext or AAD are too long. | ||
| #[inline] | ||
| fn encrypt_within_vec(&self, nonce: &[u8], aad: &[u8], buf: &mut Vec<u8>) -> Result<()> { | ||
| let res = self.encrypt_into_vec(nonce, aad, buf)?; | ||
| *buf = res; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Decrypt ciphertext in `buf` truncating it as necessary. | ||
| /// | ||
| /// On success, `buf` will contain the resulting plaintext, | ||
| /// while on error it will be zeroized. | ||
| /// | ||
| /// # Errors | ||
| /// - if the `ciphertext` is inauthentic (i.e. tag verification failure) | ||
| /// - if the `ciphertext` is too long | ||
| /// - if the `aad` is too long | ||
| #[inline] | ||
| fn decrypt_within_vec(&self, nonce: &[u8], aad: &[u8], buf: &mut Vec<u8>) -> Result<()> { | ||
| let res = self.decrypt_into_vec(nonce, aad, buf); | ||
| match res { | ||
| Ok(pt) => { | ||
| *buf = pt; | ||
| Ok(()) | ||
| } | ||
| Err(err) => { | ||
| buf.fill(0); | ||
| Err(err) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T: AeadCore> Aead for T { | ||
| #[inline] | ||
| fn encrypt_into_vec(&self, nonce: &[u8], aad: &[u8], plaintext: &[u8]) -> Result<Vec<u8>> { | ||
| let nonce = nonce.try_into().map_err(|_| Error)?; | ||
| self.encrypt_into(nonce, aad, plaintext, alloc_vec) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn decrypt_into_vec(&self, nonce: &[u8], aad: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>> { | ||
| let nonce = nonce.try_into().map_err(|_| Error)?; | ||
| self.decrypt_into(nonce, aad, ciphertext, alloc_vec) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn encrypt_within_vec(&self, nonce: &[u8], aad: &[u8], buf: &mut Vec<u8>) -> Result<()> { | ||
| let nonce = nonce.try_into().map_err(|_| Error)?; | ||
| self.encrypt_within(nonce, aad, buf, extend_vec) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn decrypt_within_vec(&self, nonce: &[u8], aad: &[u8], buf: &mut Vec<u8>) -> Result<()> { | ||
| let nonce = nonce.try_into().map_err(|_| Error)?; | ||
| self.decrypt_within(nonce, aad, buf, Vec::truncate) | ||
| } | ||
| } | ||
|
|
||
| fn alloc_vec(len: usize) -> Vec<u8> { | ||
| alloc::vec![0u8; len] | ||
| } | ||
|
|
||
| fn extend_vec(buf: &mut Vec<u8>, len: usize) { | ||
| buf.resize(len, 0); | ||
| } | ||
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.
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.
I removed the
Payloadtype for now. I think we either need a more general solution which would help with other methods as well (e.g.Nonce,Aad,Plaintext, etc. wrappers), or we could just tolerate potential incorrect reorderings of the arguments, which are hopefully somewhat unlikely in modern conditions (with IDEs and stuff).