Skip to main content

crypto_dispatch/algorithms/
x_wing.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use crate::AlgorithmError;
6use crypto_core::Algorithm;
7use zeroize::Zeroizing;
8
9/// X-Wing ML-KEM-768 hybrid KEM adapter.
10pub struct XWing768Algo;
11
12/// X-Wing ML-KEM-1024 hybrid KEM adapter.
13pub struct XWing1024Algo;
14
15impl XWing768Algo {
16    /// The algorithm selector this adapter implements.
17    pub const ALG: Algorithm = Algorithm::XWing768;
18
19    /// Generate an X-Wing-768 keypair.
20    pub fn generate_keypair() -> Result<(Vec<u8>, Zeroizing<Vec<u8>>), AlgorithmError> {
21        crypto_x_wing::generate_x_wing_768_keypair().map_err(AlgorithmError::from)
22    }
23
24    /// Encapsulate using the public key.
25    pub fn encapsulate(public_key: &[u8]) -> Result<(Zeroizing<Vec<u8>>, Vec<u8>), AlgorithmError> {
26        let (ciphertext, shared_secret) =
27            crypto_x_wing::x_wing_768_encapsulate(public_key).map_err(AlgorithmError::from)?;
28        Ok((shared_secret, ciphertext))
29    }
30
31    /// Decapsulate using the secret key.
32    pub fn decapsulate(
33        ciphertext: &[u8],
34        secret_key: &[u8],
35    ) -> Result<Zeroizing<Vec<u8>>, AlgorithmError> {
36        crypto_x_wing::x_wing_768_decapsulate(ciphertext, secret_key).map_err(AlgorithmError::from)
37    }
38}
39
40impl XWing1024Algo {
41    /// The algorithm selector this adapter implements.
42    pub const ALG: Algorithm = Algorithm::XWing1024;
43
44    /// Generate an X-Wing-1024 keypair.
45    pub fn generate_keypair() -> Result<(Vec<u8>, Zeroizing<Vec<u8>>), AlgorithmError> {
46        crypto_x_wing::generate_x_wing_1024_keypair().map_err(AlgorithmError::from)
47    }
48
49    /// Encapsulate using the public key.
50    pub fn encapsulate(public_key: &[u8]) -> Result<(Zeroizing<Vec<u8>>, Vec<u8>), AlgorithmError> {
51        let (ciphertext, shared_secret) =
52            crypto_x_wing::x_wing_1024_encapsulate(public_key).map_err(AlgorithmError::from)?;
53        Ok((shared_secret, ciphertext))
54    }
55
56    /// Decapsulate using the secret key.
57    pub fn decapsulate(
58        ciphertext: &[u8],
59        secret_key: &[u8],
60    ) -> Result<Zeroizing<Vec<u8>>, AlgorithmError> {
61        crypto_x_wing::x_wing_1024_decapsulate(ciphertext, secret_key).map_err(AlgorithmError::from)
62    }
63}