-
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
20
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.
Open
aead: rework traits API #2427
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2498765
aead: minimize API surface
newpavlov ae80b2a
update CI config
newpavlov 664ac26
Update Cargo.lock
newpavlov 6bbdcb7
enable `rand_core` when `getrandom` is enabled
newpavlov 831a55a
Add support for variable size nonces to `AeadCore`
newpavlov 515b195
Add variable nonce support to `Aead`
newpavlov a514471
fix typo
newpavlov 2125343
test `dyn Aead`
newpavlov 43ab6c7
Add `into` and `within` methods
newpavlov d5e9c1d
rename `AeadTagPosition` to `AeadWithTag`
newpavlov 4cb6442
Use macros to implement `AeadWithTag` methods
newpavlov 82f3ee2
Move `TAG_POSITION` to `AeadCore`
newpavlov d5e76b7
Tweak `Aead`
newpavlov 0170201
fix typos
newpavlov 1d2b049
tweak test macros
newpavlov ea1ea54
Add blanket impls for `Aead::de/encrypt_within_vec`
newpavlov 2082826
tweak attributes
newpavlov 4caeffc
Introduce `VariableAead` trait
newpavlov a0e6029
fix typo
newpavlov 908b188
add variable tag support
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,81 @@ | ||
| use crate::{Result, VariableAead}; | ||
| 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: VariableAead> Aead for T { | ||
| #[inline] | ||
| fn encrypt_into_vec(&self, nonce: &[u8], aad: &[u8], plaintext: &[u8]) -> Result<Vec<u8>> { | ||
| self.variable_encrypt_into(nonce, aad, plaintext, |n| alloc::vec![0u8; n]) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn decrypt_into_vec(&self, nonce: &[u8], aad: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>> { | ||
| self.variable_decrypt_into(nonce, aad, ciphertext, |n| alloc::vec![0u8; n]) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn encrypt_within_vec(&self, nonce: &[u8], aad: &[u8], buf: &mut Vec<u8>) -> Result<()> { | ||
| self.variable_encrypt_within(nonce, aad, buf, |buf, len| buf.resize(len, 0)) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn decrypt_within_vec(&self, nonce: &[u8], aad: &[u8], buf: &mut Vec<u8>) -> Result<()> { | ||
| self.variable_decrypt_within(nonce, aad, buf, Vec::truncate) | ||
| } | ||
| } | ||
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 the potential incorrect reordering of arguments, which hopefully is somewhat unlikely in modern conditions (IDEs and stuff).