rustcrypto: add X25519, ML-KEM and X-Wing KEMs
Pure-Rust KEMs for the hybrid key agreement; ML-KEM keys are 64-byte FIPS 203 seeds and X25519 keeps the wolfSSL ephemeral-DH convention. Refs: R3-4
This commit is contained in:
parent
2a8dbdcd6b
commit
2f1248e447
|
|
@ -167,6 +167,12 @@ dependencies = [
|
|||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||
|
||||
[[package]]
|
||||
name = "base64ct"
|
||||
version = "1.8.3"
|
||||
|
|
@ -290,14 +296,18 @@ dependencies = [
|
|||
"blake2 0.11.0",
|
||||
"ccc-crypto-core",
|
||||
"chacha20poly1305",
|
||||
"getrandom",
|
||||
"hex",
|
||||
"hkdf",
|
||||
"hmac",
|
||||
"log",
|
||||
"ml-kem",
|
||||
"poly1305",
|
||||
"sha2",
|
||||
"sha3",
|
||||
"subtle",
|
||||
"x-wing",
|
||||
"x25519-dalek",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
|
|
@ -466,6 +476,42 @@ dependencies = [
|
|||
"cmov",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "curve25519-dalek"
|
||||
version = "5.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5eed333089e2e1c1ac8c6c0398e5e2497b4c9926ca6d0365ed1e099afa5bc23"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cpufeatures 0.3.1",
|
||||
"curve25519-dalek-derive",
|
||||
"fiat-crypto",
|
||||
"rustc_version",
|
||||
"subtle",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "curve25519-dalek-derive"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "der"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a996a108feb8b427a6e12fcf6af4aa83409b79d72c15337d7741368131ee004"
|
||||
dependencies = [
|
||||
"const-oid",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.10.7"
|
||||
|
|
@ -518,6 +564,12 @@ dependencies = [
|
|||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fiat-crypto"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "64cd1e32ddd350061ae6edb1b082d7c54915b5c672c389143b9a63403a109f24"
|
||||
|
||||
[[package]]
|
||||
name = "find-msvc-tools"
|
||||
version = "0.1.9"
|
||||
|
|
@ -534,6 +586,17 @@ dependencies = [
|
|||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"r-efi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ghash"
|
||||
version = "0.6.0"
|
||||
|
|
@ -580,7 +643,9 @@ version = "0.4.10"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3944cf8cf766b40e2a1a333ee5e9b563f854d5fa49d6a8ca2764e97c6eddb214"
|
||||
dependencies = [
|
||||
"ctutils",
|
||||
"typenum",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -647,6 +712,16 @@ dependencies = [
|
|||
"cpufeatures 0.3.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kem"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "01737161ba802849cfd486b5bd209d38ba4943494c249a8126005170c7621edd"
|
||||
dependencies = [
|
||||
"crypto-common 0.2.2",
|
||||
"rand_core 0.10.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.182"
|
||||
|
|
@ -681,6 +756,34 @@ version = "0.2.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||
|
||||
[[package]]
|
||||
name = "ml-kem"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "84ca78b66f46050d49fa21693ae8adab2081d8101b65d8067316b6b70b64a0d1"
|
||||
dependencies = [
|
||||
"hybrid-array",
|
||||
"kem",
|
||||
"module-lattice",
|
||||
"pkcs8",
|
||||
"rand_core 0.10.1",
|
||||
"sha3",
|
||||
"shake",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "module-lattice"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4a1e412c8ee83a1c678f427806219c49c96b2511b9f07497514f0e43fb26155"
|
||||
dependencies = [
|
||||
"ctutils",
|
||||
"hybrid-array",
|
||||
"num-traits",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
|
|
@ -691,6 +794,15 @@ dependencies = [
|
|||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.3"
|
||||
|
|
@ -733,6 +845,16 @@ dependencies = [
|
|||
"ctutils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pkcs8"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "451913da69c775a56034ea8d9003d27ee8948e12443eae7c038ba100a4f21cb7"
|
||||
dependencies = [
|
||||
"der",
|
||||
"spki",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "poly1305"
|
||||
version = "0.9.1"
|
||||
|
|
@ -798,6 +920,12 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "r-efi"
|
||||
version = "6.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.4"
|
||||
|
|
@ -845,6 +973,21 @@ version = "2.1.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"
|
||||
dependencies = [
|
||||
"semver",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "1.0.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
|
|
@ -910,12 +1053,33 @@ dependencies = [
|
|||
"sponge-cursor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shake"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09057cb2149ad4cbd2da1e26b351f9a4c354219421229c69c3063e6f61947c4a"
|
||||
dependencies = [
|
||||
"digest 0.11.3",
|
||||
"keccak",
|
||||
"sponge-cursor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||
|
||||
[[package]]
|
||||
name = "spki"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ef958a98b9d5da290cfc78946e9f3e61e1e62a18db0d92cac0b83cc161491a9"
|
||||
dependencies = [
|
||||
"base64ct",
|
||||
"der",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sponge-cursor"
|
||||
version = "0.1.0"
|
||||
|
|
@ -1008,6 +1172,31 @@ dependencies = [
|
|||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "x-wing"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "01576e56599d01d75c5efcd5b30fa5de29e776f5a225e43fbf8c92c7302d4c8b"
|
||||
dependencies = [
|
||||
"kem",
|
||||
"ml-kem",
|
||||
"sha3",
|
||||
"shake",
|
||||
"x25519-dalek",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "x25519-dalek"
|
||||
version = "3.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7e8131a03190127fb2263afc72b322ecadae46b6ff8c6f399ff5d02f5559af6"
|
||||
dependencies = [
|
||||
"curve25519-dalek",
|
||||
"rand_core 0.10.1",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zeroize"
|
||||
version = "1.8.2"
|
||||
|
|
|
|||
|
|
@ -32,5 +32,12 @@ subtle = "2.6"
|
|||
hkdf = "0.13"
|
||||
argon2 = { version = "0.6", default-features = false, features = ["alloc", "zeroize"] }
|
||||
|
||||
# KEM. `hazmat` exposes deterministic encapsulation for the self-test KATs.
|
||||
x25519-dalek = { version = "3.0", features = ["static_secrets"] }
|
||||
ml-kem = { version = "0.3", features = ["hazmat", "zeroize"] }
|
||||
# X-Wing draft-connolly-cfrg-xwing-kem-06 (ML-KEM-768 + X25519).
|
||||
x-wing = { version = "0.1", features = ["hazmat", "zeroize"] }
|
||||
getrandom = "0.4"
|
||||
|
||||
[dev-dependencies]
|
||||
hex = "0.4"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,338 @@
|
|||
//! KEMs: X25519 (`x25519-dalek`), ML-KEM-768/1024 (`ml-kem`, FIPS 203) and
|
||||
//! X-Wing (`x-wing`, draft-connolly-cfrg-xwing-kem-06).
|
||||
//!
|
||||
//! Key and ciphertext formats:
|
||||
//!
|
||||
//! | Algorithm | Private key | Public key | Ciphertext | Secret |
|
||||
//! |-------------|------------------------|------------|-----------------------|--------|
|
||||
//! | X25519 | 32-byte scalar (LE) | 32 | 32 (ephemeral public) | 32 |
|
||||
//! | ML-KEM-768 | 64-byte seed `d \|\| z` | 1184 | 1088 | 32 |
|
||||
//! | ML-KEM-1024 | 64-byte seed `d \|\| z` | 1568 | 1568 | 32 |
|
||||
//! | X-Wing | 32-byte seed | 1216 | 1120 | 32 |
|
||||
//!
|
||||
//! X25519 follows the wolfSSL provider's KEM convention: encapsulation makes
|
||||
//! an ephemeral key pair, the ciphertext is its public key and the secret is
|
||||
//! the raw RFC 7748 shared value. An all-zero shared value (a low-order
|
||||
//! peer point) is rejected.
|
||||
//!
|
||||
//! X448 and Classic McEliece are not offered by this provider.
|
||||
|
||||
use ml_kem::array::Array;
|
||||
use ml_kem::kem::{Decapsulate, KeyExport, TryKeyInit};
|
||||
use x25519_dalek::{PublicKey, StaticSecret};
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
use ccc_crypto_core::{
|
||||
algorithms::KemAlgorithm,
|
||||
error::CryptoError,
|
||||
types::{KemEncapResult, KemKeyPair},
|
||||
};
|
||||
|
||||
/// Generate a key pair.
|
||||
pub fn generate_keypair(algo: KemAlgorithm) -> Result<KemKeyPair, CryptoError> {
|
||||
match algo {
|
||||
KemAlgorithm::X25519 => {
|
||||
let sk = random::<32>()?;
|
||||
let pk = PublicKey::from(&StaticSecret::from(*sk));
|
||||
Ok(KemKeyPair {
|
||||
public_key: pk.as_bytes().to_vec(),
|
||||
private_key: sk.to_vec(),
|
||||
})
|
||||
}
|
||||
KemAlgorithm::MlKem768 => mlkem768::generate(),
|
||||
KemAlgorithm::MlKem1024 => mlkem1024::generate(),
|
||||
KemAlgorithm::XWing => {
|
||||
let seed = random::<32>()?;
|
||||
Ok(KemKeyPair {
|
||||
public_key: xwing_public_key(&seed[..])?,
|
||||
private_key: seed.to_vec(),
|
||||
})
|
||||
}
|
||||
KemAlgorithm::X448 | KemAlgorithm::ClassicMcEliece460896 => not_compiled(algo),
|
||||
}
|
||||
}
|
||||
|
||||
/// Encapsulate a fresh shared secret to `public_key`.
|
||||
pub fn encapsulate(algo: KemAlgorithm, public_key: &[u8]) -> Result<KemEncapResult, CryptoError> {
|
||||
match algo {
|
||||
KemAlgorithm::X25519 => {
|
||||
let eph = random::<32>()?;
|
||||
x25519_encapsulate_deterministic(&eph[..], public_key)
|
||||
}
|
||||
KemAlgorithm::MlKem768 => mlkem768::encapsulate_deterministic(public_key, &*random()?),
|
||||
KemAlgorithm::MlKem1024 => mlkem1024::encapsulate_deterministic(public_key, &*random()?),
|
||||
KemAlgorithm::XWing => xwing_encapsulate_deterministic(public_key, &*random()?),
|
||||
KemAlgorithm::X448 | KemAlgorithm::ClassicMcEliece460896 => not_compiled(algo),
|
||||
}
|
||||
}
|
||||
|
||||
/// Recover the shared secret from `ciphertext` with `private_key`.
|
||||
pub fn decapsulate(
|
||||
algo: KemAlgorithm,
|
||||
private_key: &[u8],
|
||||
ciphertext: &[u8],
|
||||
) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
|
||||
match algo {
|
||||
KemAlgorithm::X25519 => x25519_dh(private_key, ciphertext),
|
||||
KemAlgorithm::MlKem768 => mlkem768::decapsulate(private_key, ciphertext),
|
||||
KemAlgorithm::MlKem1024 => mlkem1024::decapsulate(private_key, ciphertext),
|
||||
KemAlgorithm::XWing => xwing_decapsulate(private_key, ciphertext),
|
||||
KemAlgorithm::X448 | KemAlgorithm::ClassicMcEliece460896 => not_compiled(algo),
|
||||
}
|
||||
}
|
||||
|
||||
fn not_compiled<T>(algo: KemAlgorithm) -> Result<T, CryptoError> {
|
||||
Err(CryptoError::FeatureNotCompiled(format!(
|
||||
"{} is not offered by the rustcrypto provider",
|
||||
algo.name()
|
||||
)))
|
||||
}
|
||||
|
||||
fn random<const N: usize>() -> Result<Zeroizing<[u8; N]>, CryptoError> {
|
||||
let mut buf = Zeroizing::new([0u8; N]);
|
||||
getrandom::fill(&mut buf[..])
|
||||
.map_err(|e| CryptoError::InternalError(format!("getrandom failed: {e}")))?;
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
fn array32(bytes: &[u8], what: &str) -> Result<Zeroizing<[u8; 32]>, CryptoError> {
|
||||
let arr: [u8; 32] = bytes
|
||||
.try_into()
|
||||
.map_err(|_| CryptoError::InvalidKey(format!("{what}: expected 32 bytes")))?;
|
||||
Ok(Zeroizing::new(arr))
|
||||
}
|
||||
|
||||
// ── X25519 ───────────────────────────────────────────────────────────────────
|
||||
|
||||
/// X25519 encapsulation with a caller-chosen ephemeral scalar (for KATs).
|
||||
pub fn x25519_encapsulate_deterministic(
|
||||
ephemeral_private: &[u8],
|
||||
public_key: &[u8],
|
||||
) -> Result<KemEncapResult, CryptoError> {
|
||||
let eph = StaticSecret::from(*array32(ephemeral_private, "X25519 private key")?);
|
||||
let ct = PublicKey::from(&eph).as_bytes().to_vec();
|
||||
let ss = x25519_dh(eph.as_bytes(), public_key)?;
|
||||
Ok(KemEncapResult {
|
||||
ciphertext: ct,
|
||||
shared_secret: ss.to_vec(),
|
||||
})
|
||||
}
|
||||
|
||||
fn x25519_dh(private_key: &[u8], public_key: &[u8]) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
|
||||
if private_key.len() != 32 || public_key.len() != 32 {
|
||||
return Err(CryptoError::InvalidKey(
|
||||
"X25519: keys must be 32 bytes each".into(),
|
||||
));
|
||||
}
|
||||
let sk = StaticSecret::from(*array32(private_key, "X25519 private key")?);
|
||||
let pk = PublicKey::from(*array32(public_key, "X25519 public key")?);
|
||||
let ss = sk.diffie_hellman(&pk);
|
||||
if !ss.was_contributory() {
|
||||
return Err(CryptoError::InvalidKey(
|
||||
"X25519: low-order public key (all-zero shared secret)".into(),
|
||||
));
|
||||
}
|
||||
Ok(Zeroizing::new(ss.as_bytes().to_vec()))
|
||||
}
|
||||
|
||||
// ── ML-KEM ───────────────────────────────────────────────────────────────────
|
||||
|
||||
macro_rules! mlkem_impl {
|
||||
($module:ident, $params:ident, $name:literal) => {
|
||||
pub(crate) mod $module {
|
||||
use super::*;
|
||||
use ml_kem::$params::{Ciphertext, DecapsulationKey, EncapsulationKey};
|
||||
|
||||
fn dk_from_seed(seed: &[u8]) -> Result<DecapsulationKey, CryptoError> {
|
||||
let seed = ml_kem::Seed::try_from(seed).map_err(|_| {
|
||||
CryptoError::InvalidKey(
|
||||
concat!($name, ": private key must be a 64-byte seed").into(),
|
||||
)
|
||||
})?;
|
||||
Ok(DecapsulationKey::from_seed(seed))
|
||||
}
|
||||
|
||||
/// Public key for a 64-byte seed (FIPS 203 `ML-KEM.KeyGen_internal`).
|
||||
pub(crate) fn public_key(seed: &[u8]) -> Result<Vec<u8>, CryptoError> {
|
||||
Ok(dk_from_seed(seed)?.encapsulation_key().to_bytes().to_vec())
|
||||
}
|
||||
|
||||
pub(super) fn generate() -> Result<KemKeyPair, CryptoError> {
|
||||
let seed = random::<64>()?;
|
||||
Ok(KemKeyPair {
|
||||
public_key: public_key(&seed[..])?,
|
||||
private_key: seed.to_vec(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Encapsulate with caller-chosen randomness `m` (for KATs).
|
||||
pub(crate) fn encapsulate_deterministic(
|
||||
public_key: &[u8],
|
||||
m: &[u8; 32],
|
||||
) -> Result<KemEncapResult, CryptoError> {
|
||||
let ek = EncapsulationKey::new_from_slice(public_key).map_err(|_| {
|
||||
CryptoError::InvalidKey(concat!($name, ": invalid public key").into())
|
||||
})?;
|
||||
let (ct, ss) = ek.encapsulate_deterministic(&Array::from(*m));
|
||||
Ok(KemEncapResult {
|
||||
ciphertext: ct.to_vec(),
|
||||
shared_secret: ss.to_vec(),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn decapsulate(
|
||||
private_key: &[u8],
|
||||
ciphertext: &[u8],
|
||||
) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
|
||||
let dk = dk_from_seed(private_key)?;
|
||||
let ct = Ciphertext::try_from(ciphertext).map_err(|_| {
|
||||
CryptoError::InvalidInput(concat!($name, ": wrong ciphertext length").into())
|
||||
})?;
|
||||
Ok(Zeroizing::new(dk.decapsulate(&ct).to_vec()))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
mlkem_impl!(mlkem768, ml_kem_768, "ML-KEM-768");
|
||||
mlkem_impl!(mlkem1024, ml_kem_1024, "ML-KEM-1024");
|
||||
|
||||
// ── X-Wing ───────────────────────────────────────────────────────────────────
|
||||
|
||||
fn xwing_dk(seed: &[u8]) -> Result<x_wing::DecapsulationKey, CryptoError> {
|
||||
Ok(x_wing::DecapsulationKey::from(*array32(
|
||||
seed,
|
||||
"X-Wing private key",
|
||||
)?))
|
||||
}
|
||||
|
||||
pub(crate) fn xwing_public_key(seed: &[u8]) -> Result<Vec<u8>, CryptoError> {
|
||||
use x_wing::Decapsulator;
|
||||
Ok(xwing_dk(seed)?.encapsulation_key().to_bytes().to_vec())
|
||||
}
|
||||
|
||||
/// X-Wing encapsulation with caller-chosen 64-byte randomness (for KATs).
|
||||
pub(crate) fn xwing_encapsulate_deterministic(
|
||||
public_key: &[u8],
|
||||
eseed: &[u8; 64],
|
||||
) -> Result<KemEncapResult, CryptoError> {
|
||||
let ek = x_wing::EncapsulationKey::try_from(public_key)
|
||||
.map_err(|_| CryptoError::InvalidKey("X-Wing: invalid public key".into()))?;
|
||||
let (ct, ss) = ek.encapsulate_deterministic(&Array::from(*eseed));
|
||||
Ok(KemEncapResult {
|
||||
ciphertext: ct.to_vec(),
|
||||
shared_secret: ss.to_vec(),
|
||||
})
|
||||
}
|
||||
|
||||
fn xwing_decapsulate(
|
||||
private_key: &[u8],
|
||||
ciphertext: &[u8],
|
||||
) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
|
||||
let dk = xwing_dk(private_key)?;
|
||||
let ss = dk
|
||||
.decapsulate_slice(ciphertext)
|
||||
.map_err(|_| CryptoError::InvalidInput("X-Wing: wrong ciphertext length".into()))?;
|
||||
Ok(Zeroizing::new(ss.to_vec()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const OFFERED: [KemAlgorithm; 4] = [
|
||||
KemAlgorithm::X25519,
|
||||
KemAlgorithm::MlKem768,
|
||||
KemAlgorithm::MlKem1024,
|
||||
KemAlgorithm::XWing,
|
||||
];
|
||||
|
||||
#[test]
|
||||
fn roundtrip_all_offered() {
|
||||
for algo in OFFERED {
|
||||
let kp = generate_keypair(algo).unwrap();
|
||||
let enc = encapsulate(algo, &kp.public_key).unwrap();
|
||||
let ss = decapsulate(algo, &kp.private_key, &enc.ciphertext).unwrap();
|
||||
assert_eq!(*ss, enc.shared_secret, "{}", algo.name());
|
||||
assert_eq!(ss.len(), 32);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sizes() {
|
||||
for (algo, sk, pk, ct) in [
|
||||
(KemAlgorithm::X25519, 32, 32, 32),
|
||||
(KemAlgorithm::MlKem768, 64, 1184, 1088),
|
||||
(KemAlgorithm::MlKem1024, 64, 1568, 1568),
|
||||
(KemAlgorithm::XWing, 32, 1216, 1120),
|
||||
] {
|
||||
let kp = generate_keypair(algo).unwrap();
|
||||
assert_eq!(kp.private_key.len(), sk);
|
||||
assert_eq!(kp.public_key.len(), pk);
|
||||
assert_eq!(
|
||||
encapsulate(algo, &kp.public_key).unwrap().ciphertext.len(),
|
||||
ct
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn x25519_rfc7748() {
|
||||
let alice = hex::decode("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a")
|
||||
.unwrap();
|
||||
let bob_pub =
|
||||
hex::decode("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f")
|
||||
.unwrap();
|
||||
let ss = decapsulate(KemAlgorithm::X25519, &alice, &bob_pub).unwrap();
|
||||
assert_eq!(
|
||||
hex::encode(&*ss),
|
||||
"4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn x25519_low_order_point_rejected() {
|
||||
let sk = [0x11u8; 32];
|
||||
assert!(matches!(
|
||||
decapsulate(KemAlgorithm::X25519, &sk, &[0u8; 32]),
|
||||
Err(CryptoError::InvalidKey(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_lengths_rejected() {
|
||||
for algo in OFFERED {
|
||||
let kp = generate_keypair(algo).unwrap();
|
||||
let enc = encapsulate(algo, &kp.public_key).unwrap();
|
||||
assert!(encapsulate(algo, &kp.public_key[1..]).is_err());
|
||||
assert!(decapsulate(algo, &kp.private_key[1..], &enc.ciphertext).is_err());
|
||||
assert!(decapsulate(algo, &kp.private_key, &enc.ciphertext[1..]).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mlkem_implicit_rejection() {
|
||||
for algo in [
|
||||
KemAlgorithm::MlKem768,
|
||||
KemAlgorithm::MlKem1024,
|
||||
KemAlgorithm::XWing,
|
||||
] {
|
||||
let kp = generate_keypair(algo).unwrap();
|
||||
let enc = encapsulate(algo, &kp.public_key).unwrap();
|
||||
let mut ct = enc.ciphertext.clone();
|
||||
ct[0] ^= 1;
|
||||
let ss = decapsulate(algo, &kp.private_key, &ct).unwrap();
|
||||
assert_ne!(*ss, enc.shared_secret);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_kems() {
|
||||
for algo in [KemAlgorithm::X448, KemAlgorithm::ClassicMcEliece460896] {
|
||||
assert!(matches!(
|
||||
generate_keypair(algo),
|
||||
Err(CryptoError::FeatureNotCompiled(_))
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -10,4 +10,5 @@
|
|||
pub mod aead;
|
||||
pub mod hash;
|
||||
pub mod kdf;
|
||||
pub mod kem;
|
||||
pub mod mac;
|
||||
|
|
|
|||
Loading…
Reference in New Issue