Skip to content

Commit 0dabd78

Browse files
authored
kem: remove associated Error types (#2216)
All of our `kem` implementations either use `type Error = Infallible` or use the error type exclusively for handling RNG errors. That's good, because having an error case for decapsulation introduces a potential sidechannel, which can be eliminated by instead using implicit rejection that returns a pseudorandom rejection symbol as its output. This removes the `Error` types and makes `Decapsulate::decapsulate` infallible in order to close the potential sidechannel having fallible decapsulation provides. `Encapsulate::encapsulate_with_rng` now only uses the `Result` for handling RNG errors and returns `R::Error`, which should hopefully help mitigate the concerns in #2214. For end users, `Encapsulate::encapsulate` now provides infallible encapsulation using the system RNG.
1 parent 4e195b4 commit 0dabd78

1 file changed

Lines changed: 10 additions & 13 deletions

File tree

kem/src/lib.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ pub use crypto_common::{Generate, KeyExport, KeySizeUser, TryKeyInit, typenum::c
1212

1313
use rand_core::TryCryptoRng;
1414

15+
#[cfg(feature = "getrandom")]
16+
use {crypto_common::getrandom::SysRng, rand_core::TryRngCore};
17+
1518
/// Encapsulator for shared secrets.
1619
///
1720
/// Often, this will just be a public key. However, it can also be a bundle of public keys, or it
1821
/// can include a sender's private key for authenticated encapsulation.
1922
pub trait Encapsulate<EK, SS>: TryKeyInit + KeyExport {
20-
/// Encapsulation error
21-
type Error: core::error::Error;
22-
2323
/// Encapsulates a fresh shared secret
24-
fn encapsulate_with_rng<R: TryCryptoRng + ?Sized>(
25-
&self,
26-
rng: &mut R,
27-
) -> Result<(EK, SS), Self::Error>;
24+
fn encapsulate_with_rng<R>(&self, rng: &mut R) -> Result<(EK, SS), R::Error>
25+
where
26+
R: TryCryptoRng + ?Sized;
2827

2928
/// Encapsulate a fresh shared secret generated using the system's secure RNG.
3029
#[cfg(feature = "getrandom")]
31-
fn encapsulate(&self) -> Result<(EK, SS), Self::Error> {
32-
self.encapsulate_with_rng(&mut crypto_common::getrandom::SysRng)
30+
fn encapsulate(&self) -> (EK, SS) {
31+
let Ok(ret) = self.encapsulate_with_rng(&mut SysRng.unwrap_err());
32+
ret
3333
}
3434
}
3535

@@ -44,11 +44,8 @@ pub trait Decapsulate<EK, SS> {
4444
/// Encapsulator which corresponds to this decapsulator.
4545
type Encapsulator: Encapsulate<EK, SS>;
4646

47-
/// Decapsulation error
48-
type Error: core::error::Error;
49-
5047
/// Decapsulates the given encapsulated key
51-
fn decapsulate(&self, encapsulated_key: &EK) -> Result<SS, Self::Error>;
48+
fn decapsulate(&self, encapsulated_key: &EK) -> SS;
5249

5350
/// Retrieve the encapsulator associated with this decapsulator.
5451
fn encapsulator(&self) -> Self::Encapsulator;

0 commit comments

Comments
 (0)