Repository navigation
feat(crypto): add X-Wing post-quantum hybrid KEM (ML-KEM-768 + X25519) #422
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
Sébastien Duquette (sduquette-devolutions)
merged 4 commits into
master
from
feat/pq-xwing-core
Nov 11, 2026
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
411fbee
feat(crypto): add X-Wing post-quantum hybrid KEM (ML-KEM-768 + X25519)
sduquette-devolutions 288829b
docs: apply review wording changes
sduquette-devolutions 9577d42
fix(key): return RandomError instead of panicking when encapsulating
sduquette-devolutions 3f3cca8
Revert "fix(key): return RandomError instead of panicking when encaps…
sduquette-devolutions 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
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,97 @@ | ||
| //! Ciphertext V3: X-Wing (X25519 + ML-KEM-768) + HKDF-SHA-256 + XChaCha20Poly1305 | ||
| //! | ||
| //! Wire format of the payload (after the header): | ||
| //! `X-Wing ciphertext (1120 bytes) || nonce (24 bytes) || XChaCha20Poly1305 ciphertext and tag`. | ||
| use super::{PrivateKey, PublicKey}; | ||
|
|
||
| use super::Error; | ||
| use super::Header; | ||
| use super::Result; | ||
|
|
||
| use super::Ciphertext; | ||
| use super::ciphertext_v2::CiphertextV2Symmetric; | ||
|
|
||
| use crate::key::key_v2::{KEM_CIPHERTEXT_SIZE, KemCiphertext}; | ||
|
|
||
| use std::convert::TryFrom; | ||
|
|
||
| #[cfg(feature = "fuzz")] | ||
| use arbitrary::Arbitrary; | ||
|
|
||
| /// HKDF-SHA-256 `info` label binding the X-Wing shared secret to asymmetric ciphertext V3. | ||
| /// Part of the wire format: changing it breaks decryption of existing data. | ||
| const HKDF_INFO: &[u8] = b"devolutions_crypto ciphertext_v3"; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct CiphertextV3Asymmetric { | ||
| kem_ciphertext: Box<KemCiphertext>, | ||
| ciphertext: CiphertextV2Symmetric, | ||
| } | ||
|
|
||
| #[cfg(feature = "fuzz")] | ||
| impl<'a> Arbitrary<'a> for CiphertextV3Asymmetric { | ||
| fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { | ||
| let kem_ciphertext: [u8; KEM_CIPHERTEXT_SIZE] = Arbitrary::arbitrary(u)?; | ||
| let ciphertext = CiphertextV2Symmetric::arbitrary(u)?; | ||
| Ok(Self { | ||
| kem_ciphertext: Box::new(KemCiphertext::from(kem_ciphertext)), | ||
| ciphertext, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl From<CiphertextV3Asymmetric> for Vec<u8> { | ||
| fn from(cipher: CiphertextV3Asymmetric) -> Self { | ||
| let mut data = cipher.kem_ciphertext.to_vec(); | ||
| let mut ciphertext: Vec<u8> = cipher.ciphertext.into(); | ||
| data.append(&mut ciphertext); | ||
| data | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<&[u8]> for CiphertextV3Asymmetric { | ||
| type Error = Error; | ||
| fn try_from(data: &[u8]) -> Result<Self> { | ||
| if data.len() <= KEM_CIPHERTEXT_SIZE { | ||
| return Err(Error::InvalidLength); | ||
| }; | ||
|
|
||
| let kem_ciphertext = KemCiphertext::try_from(&data[..KEM_CIPHERTEXT_SIZE]) | ||
| .map_err(|_| Error::InvalidLength)?; | ||
| let ciphertext = CiphertextV2Symmetric::try_from(&data[KEM_CIPHERTEXT_SIZE..])?; | ||
|
|
||
| Ok(Self { | ||
| kem_ciphertext: Box::new(kem_ciphertext), | ||
| ciphertext, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl CiphertextV3Asymmetric { | ||
| pub fn encrypt( | ||
| data: &[u8], | ||
| public_key: &PublicKey, | ||
| aad: &[u8], | ||
| header: &Header<Ciphertext>, | ||
| ) -> Result<Self> { | ||
| let (kem_ciphertext, key) = public_key.x_wing_encapsulate(HKDF_INFO)?; | ||
|
|
||
| let ciphertext = CiphertextV2Symmetric::encrypt_with_derived_key(data, &key, aad, header)?; | ||
|
|
||
| Ok(Self { | ||
| kem_ciphertext, | ||
| ciphertext, | ||
| }) | ||
| } | ||
|
|
||
| pub fn decrypt( | ||
| &self, | ||
| private_key: &PrivateKey, | ||
| aad: &[u8], | ||
| header: &Header<Ciphertext>, | ||
| ) -> Result<Vec<u8>> { | ||
| let key = private_key.x_wing_decapsulate(&self.kem_ciphertext, HKDF_INFO)?; | ||
|
|
||
| self.ciphertext.decrypt_with_derived_key(&key, aad, header) | ||
| } | ||
| } |
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.