Skip to content

Commit a830d9a

Browse files
authored
kem: make Encapsulate fully infallible (#2235)
Switches from `TryCryptoRng` back to `CryptoRng` for `encapsulate_with_rng`. We originally switched to #2049 with the rationale that the whole trait was fallible anyway, so we might as well handle the RNG errors. But then in #2216 we made the rest of the trait infallible, only using fallibility for the RNG. `Decapsulate` is also now fully infallible, but for cases where we need to handle errors there's a `TryDecapsulate` trait. Prospectively we could do the same thing here, and have a fallible `TryEncapsulate` trait that uses `TryCryptoRng` and handles RNG errors. This PR doesn't attempt to add one because it has some trait design issues around how we convert RNG errors into KEM-specific error types. Closes #2214 (and see also that issue for the problems around error type conversions)
1 parent 665f36b commit a830d9a

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

kem/src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use common::{
1414

1515
use common::array::{self, ArraySize};
1616
use core::{array::TryFromSliceError, convert::Infallible};
17-
use rand_core::TryCryptoRng;
17+
use rand_core::CryptoRng;
1818

1919
#[cfg(feature = "getrandom")]
2020
use common::getrandom::{SysRng, rand_core::UnwrapErr};
@@ -50,17 +50,14 @@ pub trait KemParams {
5050
pub trait Encapsulate: KemParams + TryKeyInit + KeyExport {
5151
/// Encapsulates a fresh [`SharedSecret`] generated using the supplied random number
5252
/// generator `R`.
53-
fn encapsulate_with_rng<R: TryCryptoRng + ?Sized>(
54-
&self,
55-
rng: &mut R,
56-
) -> Result<(Ciphertext<Self>, SharedSecret<Self>), R::Error>;
53+
fn encapsulate_with_rng<R>(&self, rng: &mut R) -> (Ciphertext<Self>, SharedSecret<Self>)
54+
where
55+
R: CryptoRng + ?Sized;
5756

5857
/// Encapsulate a fresh shared secret generated using the system's secure RNG.
5958
#[cfg(feature = "getrandom")]
6059
fn encapsulate(&self) -> (Ciphertext<Self>, SharedSecret<Self>) {
61-
match self.encapsulate_with_rng(&mut UnwrapErr(SysRng)) {
62-
Ok(ret) => ret,
63-
}
60+
self.encapsulate_with_rng(&mut UnwrapErr(SysRng))
6461
}
6562
}
6663

0 commit comments

Comments
 (0)