From 15dcb5b41ca5a4376aa74e3606c9ff981d1ad4d1 Mon Sep 17 00:00:00 2001 From: Franziskus Kiefer Date: Wed, 15 Jan 2025 15:22:25 +0100 Subject: [PATCH] kem: fix decoding of hybrid private keys --- libcrux-kem/src/kem.rs | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/libcrux-kem/src/kem.rs b/libcrux-kem/src/kem.rs index 99866ad05..0306aff48 100644 --- a/libcrux-kem/src/kem.rs +++ b/libcrux-kem/src/kem.rs @@ -497,34 +497,18 @@ impl PrivateKey { .try_into() .map_err(|_| Error::InvalidPrivateKey) .map(Self::MlKem768), - Algorithm::X25519MlKem768Draft00 => { - let key: [u8; MlKem768PrivateKey::len() + 32] = - bytes.try_into().map_err(|_| Error::InvalidPrivateKey)?; - let (xsk, ksk) = key.split_at(32); - Ok(Self::X25519MlKem768Draft00( - X25519MlKem768Draft00PrivateKey { - mlkem: ksk.try_into().map_err(|_| Error::InvalidPrivateKey)?, - x25519: xsk.try_into().map_err(|_| Error::InvalidPrivateKey)?, - }, - )) - } + Algorithm::X25519MlKem768Draft00 => X25519MlKem768Draft00PrivateKey::decode(bytes) + .map_err(|_| Error::InvalidPrivateKey) + .map(Self::X25519MlKem768Draft00), Algorithm::XWingKemDraft02 => { let pk = XWingKemDraft02PrivateKey::decode(bytes) .map_err(|_| Error::InvalidPrivateKey)?; Ok(Self::XWingKemDraft02(pk)) } #[cfg(feature = "kyber")] - Algorithm::X25519Kyber768Draft00 => { - let key: [u8; MlKem768PrivateKey::len() + 32] = - bytes.try_into().map_err(|_| Error::InvalidPrivateKey)?; - let (xsk, ksk) = key.split_at(32); - Ok(Self::X25519Kyber768Draft00( - X25519MlKem768Draft00PrivateKey { - mlkem: ksk.try_into().map_err(|_| Error::InvalidPrivateKey)?, - x25519: xsk.try_into().map_err(|_| Error::InvalidPrivateKey)?, - }, - )) - } + Algorithm::X25519Kyber768Draft00 => X25519MlKem768Draft00PrivateKey::decode(bytes) + .map_err(|_| Error::InvalidPrivateKey) + .map(Self::X25519Kyber768Draft00), #[cfg(feature = "kyber")] Algorithm::XWingKyberDraft02 => { let pk = XWingKemDraft02PrivateKey::decode(bytes) @@ -914,6 +898,7 @@ pub fn key_gen( Algorithm::X25519MlKem768Draft00 => { let (mlkem_private, mlkem_public) = gen_mlkem768(rng)?; let (x25519_private, x25519_public) = libcrux_ecdh::x25519_key_gen(rng)?; + Ok(( PrivateKey::X25519MlKem768Draft00(X25519MlKem768Draft00PrivateKey { mlkem: mlkem_private,