Skip to content

Commit 3f3cca8

Browse files
Revert "fix(key): return RandomError instead of panicking when encapsulating"
The OS RNG failure case keeps panicking, consistent with generate_keypair. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 9577d42 commit 3f3cca8

3 files changed

Lines changed: 13 additions & 16 deletions

File tree

‎src/key/key_v2.rs‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use super::Error;
66
use super::Result;
77

88
use hkdf::Hkdf;
9-
use rand::TryRng;
9+
use rand::rand_core::UnwrapErr;
1010
use rand::rngs::SysRng;
1111
use sha2::Sha256;
1212
use x_wing::{
13-
DecapsulationKeyRejectNonContrib, Decapsulator, EncapsulationKey, KeyExport, KeyInit,
14-
TryDecapsulate,
13+
DecapsulationKeyRejectNonContrib, Decapsulator, Encapsulate, EncapsulationKey, KeyExport,
14+
KeyInit, TryDecapsulate,
1515
};
1616
use zeroize::Zeroizing;
1717

@@ -88,20 +88,16 @@ impl KeyV2Public {
8888
/// Encapsulates a fresh shared secret to this key and derives a 32-byte key from it with
8989
/// HKDF-SHA-256 (no salt) using `info` as the label.
9090
///
91-
/// Returns `Error::RandomError` if the OS random number generator is unavailable.
92-
pub fn encapsulate(&self, info: &[u8]) -> Result<(Box<KemCiphertext>, Zeroizing<[u8; 32]>)> {
93-
let mut randomness = Zeroizing::new([0u8; x_wing::ENCAPSULATION_RANDOMNESS_SIZE]);
94-
SysRng
95-
.try_fill_bytes(randomness.as_mut_slice())
96-
.map_err(|_| Error::RandomError)?;
97-
98-
let (ciphertext, shared_secret) = self.key.encapsulate_deterministic((&*randomness).into());
91+
/// # Panics
92+
/// Panics if the OS random number generator is unavailable, like `generate_keypair`.
93+
pub fn encapsulate(&self, info: &[u8]) -> (Box<KemCiphertext>, Zeroizing<[u8; 32]>) {
94+
let (ciphertext, shared_secret) = self.key.encapsulate_with_rng(&mut UnwrapErr(SysRng));
9995
let shared_secret = Zeroizing::new(shared_secret);
10096

101-
Ok((
97+
(
10298
Box::new(ciphertext),
10399
derive_key(shared_secret.as_slice(), info),
104-
))
100+
)
105101
}
106102
}
107103

‎src/key/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl PublicKey {
359359
info: &[u8],
360360
) -> Result<(Box<KemCiphertext>, Zeroizing<[u8; 32]>)> {
361361
match &self.payload {
362-
PublicKeyPayload::V2(x) => x.encapsulate(info),
362+
PublicKeyPayload::V2(x) => Ok(x.encapsulate(info)),
363363
_ => Err(Error::InvalidDataType),
364364
}
365365
}

‎src/key_encapsulation/mod.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ enum EncapsulatedKeyPayload {
8080
/// * `version` - Version of the key encapsulation scheme to use. Use `KeyEncapsulationVersion::Latest` if you're not dealing with shared data.
8181
/// # Returns
8282
/// Returns the `EncapsulatedKey` to send to the recipient and the `SecretKey` it encapsulates.
83-
/// Returns `Error::InvalidDataType` if the public key is not a `KeyVersion::V2` key,
84-
/// or `Error::RandomError` if the OS random number generator is unavailable.
83+
/// Returns `Error::InvalidDataType` if the public key is not a `KeyVersion::V2` key.
84+
/// # Panics
85+
/// Panics if the OS random number generator is unavailable, like `generate_keypair`.
8586
/// # Example
8687
/// ```rust
8788
/// use devolutions_crypto::key::{generate_keypair, KeyVersion};

0 commit comments

Comments
 (0)