diff --git a/dhkem/src/ecdh_kem.rs b/dhkem/src/ecdh_kem.rs index 8dda9e0..03bf256 100644 --- a/dhkem/src/ecdh_kem.rs +++ b/dhkem/src/ecdh_kem.rs @@ -1,7 +1,7 @@ //! Generic Elliptic Curve Diffie-Hellman KEM adapter. use crate::{DhDecapsulator, DhEncapsulator, DhKem}; -use core::marker::PhantomData; +use core::{convert::Infallible, marker::PhantomData}; use elliptic_curve::{ ecdh::{EphemeralSecret, SharedSecret}, CurveArithmetic, PublicKey, @@ -19,7 +19,7 @@ impl Encapsulate, SharedSecret> for DhEncapsulator Decapsulate, SharedSecret> for DhDecapsulator) -> Result, Self::Error> { let ss = self.0.diffie_hellman(encapsulated_key); diff --git a/dhkem/src/x25519_kem.rs b/dhkem/src/x25519_kem.rs index 2deb80c..6cd35fd 100644 --- a/dhkem/src/x25519_kem.rs +++ b/dhkem/src/x25519_kem.rs @@ -1,4 +1,5 @@ use crate::{DhDecapsulator, DhEncapsulator, DhKem}; +use core::convert::Infallible; use kem::{Decapsulate, Encapsulate}; use rand_core::CryptoRngCore; use x25519::{PublicKey, ReusableSecret, SharedSecret}; @@ -9,7 +10,7 @@ use x25519::{PublicKey, ReusableSecret, SharedSecret}; pub struct X25519Kem; impl Encapsulate for DhEncapsulator { - type Error = (); + type Error = Infallible; fn encapsulate( &self, @@ -25,7 +26,7 @@ impl Encapsulate for DhEncapsulator { } impl Decapsulate for DhDecapsulator { - type Error = (); + type Error = Infallible; fn decapsulate(&self, encapsulated_key: &PublicKey) -> Result { let ss = self.0.diffie_hellman(encapsulated_key); diff --git a/ml-kem/src/kem.rs b/ml-kem/src/kem.rs index 0047125..4abc52a 100644 --- a/ml-kem/src/kem.rs +++ b/ml-kem/src/kem.rs @@ -1,3 +1,4 @@ +use core::convert::Infallible; use core::marker::PhantomData; use hybrid_array::typenum::U32; use rand_core::CryptoRngCore; @@ -85,12 +86,12 @@ impl

::kem::Decapsulate, SharedKey> for DecapsulationKey where P: KemParams, { - // Decapsulation is infallible - // XXX(RLB): Maybe we should reflect decryption failure as an error? - // TODO(RLB) Make Infallible - type Error = (); + type Error = Infallible; - fn decapsulate(&self, encapsulated_key: &EncodedCiphertext

) -> Result { + fn decapsulate( + &self, + encapsulated_key: &EncodedCiphertext

, + ) -> Result { let mp = self.dk_pke.decrypt(encapsulated_key); let (Kp, rp) = G(&[&mp, &self.ek.h]); let Kbar = J(&[self.z.as_slice(), encapsulated_key.as_ref()]); @@ -187,9 +188,7 @@ impl

::kem::Encapsulate, SharedKey> for EncapsulationKey where P: KemParams, { - // TODO(RLB) Make Infallible - // TODO(RLB) Swap the order of the - type Error = (); + type Error = Infallible; fn encapsulate( &self, @@ -205,8 +204,7 @@ impl

crate::EncapsulateDeterministic, SharedKey> for Enc where P: KemParams, { - // TODO(RLB) Make Infallible - type Error = (); + type Error = Infallible; fn encapsulate_deterministic( &self, diff --git a/x-wing/src/lib.rs b/x-wing/src/lib.rs index a0eaa9e..cd24274 100644 --- a/x-wing/src/lib.rs +++ b/x-wing/src/lib.rs @@ -27,6 +27,8 @@ //! assert_eq!(ss_sender, ss_receiver); //! ``` +use core::convert::Infallible; + use kem::{Decapsulate, Encapsulate}; use ml_kem::array::ArrayN; use ml_kem::{kem, EncodedSizeUser, KemCore, MlKem768, MlKem768Params, B32}; @@ -73,7 +75,7 @@ pub struct EncapsulationKey { } impl Encapsulate for EncapsulationKey { - type Error = (); + type Error = Infallible; fn encapsulate( &self, @@ -133,7 +135,7 @@ pub struct DecapsulationKey { } impl Decapsulate for DecapsulationKey { - type Error = (); + type Error = Infallible; fn decapsulate(&self, ct: &Ciphertext) -> Result { let (sk_m, sk_x, _pk_m, pk_x) = self.expand_key(); @@ -346,18 +348,16 @@ mod tests { /// Test with test vectors from: https://github.com/dconnolly/draft-connolly-cfrg-xwing-kem/blob/main/spec/test-vectors.json #[test] - fn rfc_test_vectors() -> Result<(), ()> { + fn rfc_test_vectors() { let test_vectors = serde_json::from_str::>(include_str!("test-vectors.json")).unwrap(); for test_vector in test_vectors { - run_test(test_vector)?; + run_test(test_vector); } - - Ok(()) } - fn run_test(test_vector: TestVector) -> Result<(), ()> { + fn run_test(test_vector: TestVector) { let mut seed = SeedRng::new(test_vector.seed); let (sk, pk) = generate_key_pair(&mut seed); @@ -365,15 +365,13 @@ mod tests { assert_eq!(pk.as_bytes().to_vec(), test_vector.pk); let mut eseed = SeedRng::new(test_vector.eseed); - let (ct, ss) = pk.encapsulate(&mut eseed)?; + let (ct, ss) = pk.encapsulate(&mut eseed).unwrap(); assert_eq!(ss, test_vector.ss); assert_eq!(ct.as_bytes().to_vec(), test_vector.ct); - let ss = sk.decapsulate(&ct)?; + let ss = sk.decapsulate(&ct).unwrap(); assert_eq!(ss, test_vector.ss); - - Ok(()) } #[test]