-
Notifications
You must be signed in to change notification settings - Fork 916
Rust wrapper: add one-shot XChaCha20-Poly1305 encrypt/decrypt functions #9624
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
holtrop-wolfssl
wants to merge
1
commit into
wolfSSL:master
Choose a base branch
from
holtrop-wolfssl:rust-xchacha20-poly1305
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.
+179
−4
Open
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
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
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 |
|---|---|---|
|
|
@@ -33,8 +33,11 @@ pub struct ChaCha20Poly1305 { | |
| } | ||
|
|
||
| impl ChaCha20Poly1305 { | ||
| /// Key size for ChaCha20-Poly1305 stream cipher. | ||
| pub const KEYSIZE: usize = sys::CHACHA20_POLY1305_AEAD_KEYSIZE as usize; | ||
| /// IV size for ChaCha20-Poly1305 stream cipher. | ||
| pub const IV_SIZE: usize = sys::CHACHA20_POLY1305_AEAD_IV_SIZE as usize; | ||
| /// Authentication tag size for ChaCha20-Poly1305 stream cipher. | ||
| pub const AUTH_TAG_SIZE: usize = sys::CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE as usize; | ||
|
|
||
| /// Decrypt an input message from `ciphertext` using the ChaCha20 stream | ||
|
|
@@ -240,3 +243,100 @@ impl ChaCha20Poly1305 { | |
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(xchacha20_poly1305)] | ||
| pub struct XChaCha20Poly1305 { | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although this struct is empty right now, if we do go back and add the streaming/chunking API for XChaCha20Poly1305, a field will be added here. I set it up to use a struct to future-proof for this and to maintain symmetry with the ChaCha20Poly1305 interface above. |
||
|
|
||
| #[cfg(xchacha20_poly1305)] | ||
| impl XChaCha20Poly1305 { | ||
| /// Key size for XChaCha20-Poly1305 stream cipher. | ||
| pub const KEYSIZE: usize = sys::CHACHA20_POLY1305_AEAD_KEYSIZE as usize; | ||
| /// IV size for XChaCha20-Poly1305 stream cipher. | ||
| pub const IV_SIZE: usize = sys::XCHACHA20_POLY1305_AEAD_NONCE_SIZE as usize; | ||
| /// Authentication tag size for XChaCha20-Poly1305 stream cipher. | ||
| pub const AUTH_TAG_SIZE: usize = sys::CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE as usize; | ||
|
|
||
| /// Decrypt an input message from `ciphertext` using the XChaCha20 stream | ||
| /// cipher into the `plaintext` output buffer. It also performs Poly-1305 | ||
| /// authentication. The authentication tag is expected to be located in the | ||
| /// last 16 bytes of the `ciphertext` buffer. | ||
| /// If Err is returned, the output data, `plaintext` is undefined. | ||
| /// However, callers must unconditionally zeroize the output buffer to | ||
| /// guard against leakage of cleartext data. | ||
| /// | ||
| /// # Parameters | ||
| /// | ||
| /// * `key`: Encryption key (must be 32 bytes). | ||
| /// * `iv`: Initialization Vector (must be 24 bytes). | ||
| /// * `aad`: Additional authenticated data (can be any length). | ||
| /// * `ciphertext`: Input buffer containing encrypted cipher text. | ||
| /// * `plaintext`: Output buffer containing decrypted plain text. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// Returns either Ok(()) on success or Err(e) containing the wolfSSL | ||
| /// library error code value. | ||
| pub fn decrypt(key: &[u8], iv: &[u8], aad: &[u8], ciphertext: &[u8], | ||
| plaintext: &mut [u8]) -> Result<(), i32> { | ||
| if key.len() != Self::KEYSIZE { | ||
| return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); | ||
| } | ||
| if iv.len() != Self::IV_SIZE { | ||
| return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); | ||
| } | ||
| let rc = unsafe { | ||
| sys::wc_XChaCha20Poly1305_Decrypt( | ||
| plaintext.as_mut_ptr(), plaintext.len(), | ||
| ciphertext.as_ptr(), ciphertext.len(), | ||
| aad.as_ptr(), aad.len(), | ||
| iv.as_ptr(), iv.len(), | ||
| key.as_ptr(), key.len()) | ||
| }; | ||
| if rc != 0 { | ||
| return Err(rc); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Encrypt an input message from `plaintext` using the XChaCha20 stream | ||
| /// cipher into the `ciphertext` output buffer performing Poly-1305 | ||
| /// authentication on the cipher text. | ||
| /// The authentication tag is stored in the last 16 bytes of the | ||
| /// `ciphertext` buffer, so the `ciphertext` buffer must be large enough | ||
| /// for both the cipher text and authentication tag. | ||
| /// | ||
| /// # Parameters | ||
| /// | ||
| /// * `key`: Encryption key (must be 32 bytes). | ||
| /// * `iv`: Initialization Vector (must be 24 bytes). | ||
| /// * `aad`: Additional authenticated data (can be any length). | ||
| /// * `plaintext`: Input plain text to encrypt. | ||
| /// * `ciphertext`: Output buffer for encrypted cipher text. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// Returns either Ok(()) on success or Err(e) containing the wolfSSL | ||
| /// library error code value. | ||
| pub fn encrypt(key: &[u8], iv: &[u8], aad: &[u8], plaintext: &[u8], | ||
| ciphertext: &mut [u8]) -> Result<(), i32> { | ||
| if key.len() != Self::KEYSIZE { | ||
| return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); | ||
| } | ||
| if iv.len() != Self::IV_SIZE { | ||
| return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); | ||
| } | ||
| let rc = unsafe { | ||
| sys::wc_XChaCha20Poly1305_Encrypt( | ||
| ciphertext.as_mut_ptr(), ciphertext.len(), | ||
| plaintext.as_ptr(), plaintext.len(), | ||
| aad.as_ptr(), aad.len(), | ||
| iv.as_ptr(), iv.len(), | ||
| key.as_ptr(), key.len()) | ||
| }; | ||
| if rc != 0 { | ||
| return Err(rc); | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
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
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.
The parameter order was incorrect for this function. I talked with @douzzer about it and updating the prototype to match the implementation made the most sense.
I originally was planning to wrap this function with a chunking API for XChaCha20-Poly1305 but ended up not needing that right now and there are not corresponding encrypt/decrypt functions for XChaCha20-Poly1305 like there are for ChaCha20-Poly1305 so I am omitting a Rust wrapper for that at the moment.