| 1 |
use alloc::vec::Vec; |
| 2 |
use mls_rs_core::{ |
| 3 |
crypto::{HpkePublicKey, HpkeSecretKey}, |
| 4 |
error::{AnyError, IntoAnyError}, |
| 5 |
}; |
| 6 |
use mls_rs_crypto_traits::{Hash, KemResult, KemType, VariableLengthHash}; |
| 7 |
use zeroize::Zeroize; |
| 8 |
|
| 9 |
#[derive(Debug)] |
| 10 |
#[cfg_attr(feature = "std", derive(thiserror::Error))] |
| 11 |
pub enum Error { |
| 12 |
#[cfg_attr(feature = "std", error(transparent))] |
| 13 |
KemError(AnyError), |
| 14 |
#[cfg_attr(feature = "std", error(transparent))] |
| 15 |
HashError(AnyError), |
| 16 |
#[cfg_attr(feature = "std", error("invalid key data"))] |
| 17 |
InvalidKeyData, |
| 18 |
#[cfg_attr(feature = "std", error(transparent))] |
| 19 |
MlsCodecError(mls_rs_core::mls_rs_codec::Error), |
| 20 |
} |
| 21 |
|
| 22 |
impl From<mls_rs_core::mls_rs_codec::Error> for Error { |
| 23 |
#[inline] |
| 24 |
fn from(e: mls_rs_core::mls_rs_codec::Error) -> Self { |
| 25 |
Error::MlsCodecError(e) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
impl IntoAnyError for Error {} |
| 30 |
|
| 31 |
#[derive(Clone)] |
| 32 |
pub struct CombinedKem<KEM1, KEM2, H, VH, F> { |
| 33 |
kem1: KEM1, |
| 34 |
kem2: KEM2, |
| 35 |
hash: H, |
| 36 |
variable_length_hash: VH, |
| 37 |
shared_secret_hash_input: F, |
| 38 |
} |
| 39 |
|
| 40 |
impl<KEM1, KEM2, H, VH, F> CombinedKem<KEM1, KEM2, H, VH, F> { |
| 41 |
pub fn new_custom( |
| 42 |
kem1: KEM1, |
| 43 |
kem2: KEM2, |
| 44 |
hash: H, |
| 45 |
variable_length_hash: VH, |
| 46 |
shared_secret_hash_input: F, |
| 47 |
) -> Self { |
| 48 |
Self { |
| 49 |
kem1, |
| 50 |
kem2, |
| 51 |
hash, |
| 52 |
variable_length_hash, |
| 53 |
shared_secret_hash_input, |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
pub trait SharedSecretHashInput: Send + Sync { |
| 59 |
fn input<'a>( |
| 60 |
&self, |
| 61 |
ss_details1: SharedSecretDetails<'a>, |
| 62 |
ss_details2: SharedSecretDetails<'a>, |
| 63 |
) -> Vec<u8>; |
| 64 |
} |
| 65 |
|
| 66 |
#[derive(Debug, Clone, Copy)] |
| 67 |
pub struct DefaultSharedSecretHashInput; |
| 68 |
|
| 69 |
impl<KEM1, KEM2, H, VH> CombinedKem<KEM1, KEM2, H, VH, DefaultSharedSecretHashInput> { |
| 70 |
pub fn new(kem1: KEM1, kem2: KEM2, hash: H, variable_length_hash: VH) -> Self { |
| 71 |
Self { |
| 72 |
kem1, |
| 73 |
kem2, |
| 74 |
hash, |
| 75 |
variable_length_hash, |
| 76 |
shared_secret_hash_input: DefaultSharedSecretHashInput, |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/// Secure for any combiner KEMs. |
| 82 |
impl SharedSecretHashInput for DefaultSharedSecretHashInput { |
| 83 |
fn input<'a>( |
| 84 |
&self, |
| 85 |
ss_details1: SharedSecretDetails<'a>, |
| 86 |
ss_details2: SharedSecretDetails<'a>, |
| 87 |
) -> Vec<u8> { |
| 88 |
[ |
| 89 |
ss_details1.enc, |
| 90 |
ss_details1.shared_secret, |
| 91 |
ss_details1.public_key, |
| 92 |
ss_details2.enc, |
| 93 |
ss_details2.shared_secret, |
| 94 |
ss_details2.public_key, |
| 95 |
] |
| 96 |
.concat() |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
#[derive(Debug, Clone, Copy)] |
| 101 |
pub struct XWingSharedSecretHashInput; |
| 102 |
|
| 103 |
impl<KEM1, KEM2, H, VH> CombinedKem<KEM1, KEM2, H, VH, XWingSharedSecretHashInput> { |
| 104 |
pub fn new_xwing(kem1: KEM1, kem2: KEM2, hash: H, variable_length_hash: VH) -> Self { |
| 105 |
Self { |
| 106 |
kem1, |
| 107 |
kem2, |
| 108 |
hash, |
| 109 |
variable_length_hash, |
| 110 |
shared_secret_hash_input: XWingSharedSecretHashInput, |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/// Defined in https://www.ietf.org/archive/id/draft-connolly-cfrg-xwing-kem-01.html |
| 116 |
/// |
| 117 |
/// IND-CCA secure for some KEMs (also, IND-RCCA secure for all KEMs) |
| 118 |
impl SharedSecretHashInput for XWingSharedSecretHashInput { |
| 119 |
fn input<'a>( |
| 120 |
&self, |
| 121 |
ss_details1: SharedSecretDetails<'a>, |
| 122 |
ss_details2: SharedSecretDetails<'a>, |
| 123 |
) -> Vec<u8> { |
| 124 |
[ |
| 125 |
b"\\./\n/^\\", |
| 126 |
ss_details1.shared_secret, |
| 127 |
ss_details2.shared_secret, |
| 128 |
ss_details2.enc, |
| 129 |
ss_details2.public_key, |
| 130 |
] |
| 131 |
.concat() |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
pub struct SharedSecretDetails<'a> { |
| 136 |
pub shared_secret: &'a [u8], |
| 137 |
pub enc: &'a [u8], |
| 138 |
pub public_key: &'a HpkePublicKey, |
| 139 |
} |
| 140 |
|
| 141 |
impl<'a> SharedSecretDetails<'a> { |
| 142 |
pub fn new(shared_secret: &'a [u8], enc: &'a [u8], public_key: &'a HpkePublicKey) -> Self { |
| 143 |
Self { |
| 144 |
shared_secret, |
| 145 |
enc, |
| 146 |
public_key, |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 152 |
#[cfg_attr(all(target_arch = "wasm32", mls_build_async), maybe_async::must_be_async(?Send))] |
| 153 |
#[cfg_attr( |
| 154 |
all(not(target_arch = "wasm32"), mls_build_async), |
| 155 |
maybe_async::must_be_async |
| 156 |
)] |
| 157 |
impl<KEM1, KEM2, H, VH, F> KemType for CombinedKem<KEM1, KEM2, H, VH, F> |
| 158 |
where |
| 159 |
KEM1: KemType, |
| 160 |
KEM2: KemType, |
| 161 |
H: Hash, |
| 162 |
VH: VariableLengthHash, |
| 163 |
F: SharedSecretHashInput, |
| 164 |
{ |
| 165 |
type Error = Error; |
| 166 |
|
| 167 |
fn kem_id(&self) -> u16 { |
| 168 |
// TODO not set by any RFC |
| 169 |
15 |
| 170 |
} |
| 171 |
|
| 172 |
async fn generate_deterministic( |
| 173 |
&self, |
| 174 |
seed: &[u8], |
| 175 |
) -> Result<(HpkeSecretKey, HpkePublicKey), Self::Error> { |
| 176 |
self.generate_deterministic(seed).await |
| 177 |
} |
| 178 |
|
| 179 |
async fn encap(&self, remote_key: &HpkePublicKey) -> Result<KemResult, Self::Error> { |
| 180 |
let (pk1, pk2) = self.parse_key(remote_key, self.kem1.public_key_size())?; |
| 181 |
|
| 182 |
let pk1 = pk1.into(); |
| 183 |
let pk2 = pk2.into(); |
| 184 |
|
| 185 |
let ct1 = self |
| 186 |
.kem1 |
| 187 |
.encap(&pk1) |
| 188 |
.await |
| 189 |
.map_err(|e| Error::KemError(e.into_any_error()))?; |
| 190 |
|
| 191 |
let ct2 = self |
| 192 |
.kem2 |
| 193 |
.encap(&pk2) |
| 194 |
.await |
| 195 |
.map_err(|e| Error::KemError(e.into_any_error()))?; |
| 196 |
|
| 197 |
let enc = [&ct1.enc[..], &ct2.enc].concat(); |
| 198 |
|
| 199 |
let ss_details1 = SharedSecretDetails::new(&ct1.shared_secret, &ct1.enc, &pk1); |
| 200 |
let ss_details2 = SharedSecretDetails::new(&ct2.shared_secret, &ct2.enc, &pk2); |
| 201 |
|
| 202 |
let mut shared_secret_input = self |
| 203 |
.shared_secret_hash_input |
| 204 |
.input(ss_details1, ss_details2); |
| 205 |
|
| 206 |
let shared_secret = self |
| 207 |
.hash |
| 208 |
.hash(&shared_secret_input) |
| 209 |
.map_err(|e| Error::KemError(e.into_any_error()))?; |
| 210 |
|
| 211 |
shared_secret_input.zeroize(); |
| 212 |
|
| 213 |
Ok(KemResult { shared_secret, enc }) |
| 214 |
} |
| 215 |
|
| 216 |
async fn decap( |
| 217 |
&self, |
| 218 |
enc: &[u8], |
| 219 |
secret_key: &HpkeSecretKey, |
| 220 |
local_public: &HpkePublicKey, |
| 221 |
) -> Result<Vec<u8>, Self::Error> { |
| 222 |
let (pk1, pk2) = self.parse_key(local_public, self.kem1.public_key_size())?; |
| 223 |
let (sk1, sk2) = self.parse_key(secret_key, self.kem1.secret_key_size())?; |
| 224 |
let (enc1, enc2) = self.parse_key(enc, self.kem1.enc_size())?; |
| 225 |
|
| 226 |
let pk1 = pk1.into(); |
| 227 |
let pk2 = pk2.into(); |
| 228 |
let sk1 = sk1.into(); |
| 229 |
let sk2 = sk2.into(); |
| 230 |
|
| 231 |
let shared_secret1 = self |
| 232 |
.kem1 |
| 233 |
.decap(&enc1, &sk1, &pk1) |
| 234 |
.await |
| 235 |
.map_err(|e| Error::KemError(e.into_any_error()))?; |
| 236 |
|
| 237 |
let shared_secret2 = self |
| 238 |
.kem2 |
| 239 |
.decap(&enc2, &sk2, &pk2) |
| 240 |
.await |
| 241 |
.map_err(|e| Error::KemError(e.into_any_error()))?; |
| 242 |
|
| 243 |
let ss_details1 = SharedSecretDetails::new(&shared_secret1, &enc1, &pk1); |
| 244 |
let ss_details2 = SharedSecretDetails::new(&shared_secret2, &enc2, &pk2); |
| 245 |
|
| 246 |
let mut shared_secret_input = self |
| 247 |
.shared_secret_hash_input |
| 248 |
.input(ss_details1, ss_details2); |
| 249 |
|
| 250 |
let shared_secret = self |
| 251 |
.hash |
| 252 |
.hash(&shared_secret_input) |
| 253 |
.map_err(|e| Error::KemError(e.into_any_error()))?; |
| 254 |
|
| 255 |
shared_secret_input.zeroize(); |
| 256 |
|
| 257 |
Ok(shared_secret) |
| 258 |
} |
| 259 |
|
| 260 |
fn public_key_validate(&self, _key: &HpkePublicKey) -> Result<(), Self::Error> { |
| 261 |
// TODO Not clear how to do this for Kyber or how useful it is. |
| 262 |
Ok(()) |
| 263 |
} |
| 264 |
|
| 265 |
async fn generate(&self) -> Result<(HpkeSecretKey, HpkePublicKey), Self::Error> { |
| 266 |
let (sk1, pk1) = self |
| 267 |
.kem1 |
| 268 |
.generate() |
| 269 |
.await |
| 270 |
.map_err(|e| Error::KemError(e.into_any_error()))?; |
| 271 |
|
| 272 |
let (sk2, pk2) = self |
| 273 |
.kem2 |
| 274 |
.generate() |
| 275 |
.await |
| 276 |
.map_err(|e| Error::KemError(e.into_any_error()))?; |
| 277 |
|
| 278 |
let sk = [sk1.as_ref(), &sk2].concat(); |
| 279 |
let pk = [pk1.as_ref(), &pk2].concat(); |
| 280 |
|
| 281 |
Ok((sk.into(), pk.into())) |
| 282 |
} |
| 283 |
|
| 284 |
fn seed_length_for_derive(&self) -> usize { |
| 285 |
self.kem1.seed_length_for_derive() + self.kem2.seed_length_for_derive() |
| 286 |
} |
| 287 |
|
| 288 |
fn public_key_size(&self) -> usize { |
| 289 |
self.kem1.public_key_size() + self.kem2.public_key_size() |
| 290 |
} |
| 291 |
|
| 292 |
fn secret_key_size(&self) -> usize { |
| 293 |
self.kem1.secret_key_size() + self.kem2.secret_key_size() |
| 294 |
} |
| 295 |
|
| 296 |
fn enc_size(&self) -> usize { |
| 297 |
self.kem1.enc_size() + self.kem2.enc_size() |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
impl<KEM1, KEM2, H, VH, F> CombinedKem<KEM1, KEM2, H, VH, F> |
| 302 |
where |
| 303 |
KEM1: KemType, |
| 304 |
KEM2: KemType, |
| 305 |
H: Hash, |
| 306 |
VH: VariableLengthHash, |
| 307 |
F: SharedSecretHashInput, |
| 308 |
{ |
| 309 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 310 |
async fn generate_deterministic( |
| 311 |
&self, |
| 312 |
ikm: &[u8], |
| 313 |
) -> Result<(HpkeSecretKey, HpkePublicKey), Error> { |
| 314 |
let ikm = self |
| 315 |
.variable_length_hash |
| 316 |
.hash(ikm, self.seed_length_for_derive()) |
| 317 |
.map_err(|e| Error::KemError(e.into_any_error()))?; |
| 318 |
|
| 319 |
let (ikm1, ikm2) = ikm.split_at(self.kem1.seed_length_for_derive()); |
| 320 |
|
| 321 |
self.generate_key_pair_derand(ikm1, ikm2).await |
| 322 |
} |
| 323 |
|
| 324 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 325 |
pub async fn generate_key_pair_derand( |
| 326 |
&self, |
| 327 |
ikm1: &[u8], |
| 328 |
ikm2: &[u8], |
| 329 |
) -> Result<(HpkeSecretKey, HpkePublicKey), Error> { |
| 330 |
let (sk1, pk1) = self |
| 331 |
.kem1 |
| 332 |
.generate_deterministic(ikm1) |
| 333 |
.await |
| 334 |
.map_err(|e| Error::KemError(e.into_any_error()))?; |
| 335 |
|
| 336 |
let (sk2, pk2) = self |
| 337 |
.kem2 |
| 338 |
.generate_deterministic(ikm2) |
| 339 |
.await |
| 340 |
.map_err(|e| Error::KemError(e.into_any_error()))?; |
| 341 |
|
| 342 |
let sk = [sk1.as_ref(), &sk2].concat(); |
| 343 |
let pk = [pk1.as_ref(), &pk2].concat(); |
| 344 |
|
| 345 |
Ok((sk.into(), pk.into())) |
| 346 |
} |
| 347 |
|
| 348 |
fn parse_key(&self, key: &[u8], size: usize) -> Result<(Vec<u8>, Vec<u8>), Error> { |
| 349 |
(key.len() >= size) |
| 350 |
.then_some(()) |
| 351 |
.ok_or(Error::InvalidKeyData)?; |
| 352 |
|
| 353 |
let (key1, key2) = key.split_at(size); |
| 354 |
|
| 355 |
Ok((key1.to_vec(), key2.to_vec())) |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
// Makes no sense to test this both in sync and async mode |
| 360 |
#[cfg(all(test, not(mls_build_async)))] |
| 361 |
mod tests { |
| 362 |
use mls_rs_core::crypto::{HpkePublicKey, HpkeSecretKey}; |
| 363 |
use mls_rs_crypto_traits::{ |
| 364 |
mock::{MockHash, MockKemType, MockVariableLengthHash}, |
| 365 |
KemResult, KemType, |
| 366 |
}; |
| 367 |
|
| 368 |
use super::{ |
| 369 |
CombinedKem, DefaultSharedSecretHashInput, SharedSecretHashInput, |
| 370 |
XWingSharedSecretHashInput, |
| 371 |
}; |
| 372 |
|
| 373 |
fn pk(i: u8) -> HpkePublicKey { |
| 374 |
if i == 12 { |
| 375 |
b"pk1pk2".to_vec().into() |
| 376 |
} else { |
| 377 |
format!("pk{i}").into_bytes().into() |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
fn sk(i: u8) -> HpkeSecretKey { |
| 382 |
if i == 12 { |
| 383 |
b"sk1sk2".to_vec().into() |
| 384 |
} else { |
| 385 |
format!("sk{i}").into_bytes().into() |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
fn enc(i: u8) -> Vec<u8> { |
| 390 |
if i == 12 { |
| 391 |
b"enc1enc2".to_vec() |
| 392 |
} else { |
| 393 |
format!("enc{i}").into_bytes() |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
fn ss(i: u8) -> Vec<u8> { |
| 398 |
format!("ss{i}").into_bytes() |
| 399 |
} |
| 400 |
|
| 401 |
fn ikm(i: u8) -> Vec<u8> { |
| 402 |
format!("ikm{i}").into_bytes() |
| 403 |
} |
| 404 |
|
| 405 |
#[test] |
| 406 |
fn generate_deterministic() { |
| 407 |
let mut kem1 = MockKemType::new(); |
| 408 |
let mut kem2 = MockKemType::new(); |
| 409 |
let hash = MockHash::new(); |
| 410 |
let mut variable_length_hash = MockVariableLengthHash::new(); |
| 411 |
|
| 412 |
variable_length_hash |
| 413 |
.expect_hash() |
| 414 |
.withf(|ikm, ikm1_len| ikm == b"test ikm" && *ikm1_len == 8) |
| 415 |
.return_once(|_, _| Ok([ikm(1), ikm(2)].concat())); |
| 416 |
|
| 417 |
kem1.expect_seed_length_for_derive().returning(|| 4); |
| 418 |
kem2.expect_seed_length_for_derive().returning(|| 4); |
| 419 |
|
| 420 |
kem1.expect_generate_deterministic() |
| 421 |
.withf(|ikm1| ikm1 == ikm(1)) |
| 422 |
.return_once(|_| Ok((sk(1), pk(1)))); |
| 423 |
|
| 424 |
kem2.expect_generate_deterministic() |
| 425 |
.withf(|ikm1| ikm1 == ikm(2)) |
| 426 |
.return_once(|_| Ok((sk(2), pk(2)))); |
| 427 |
|
| 428 |
let kem = CombinedKem::new(kem1, kem2, hash, variable_length_hash); |
| 429 |
|
| 430 |
let keypair = kem.generate_deterministic(b"test ikm").unwrap(); |
| 431 |
assert_eq!(keypair.0, sk(12)); |
| 432 |
assert_eq!(keypair.1, pk(12)); |
| 433 |
} |
| 434 |
|
| 435 |
#[test] |
| 436 |
fn generate() { |
| 437 |
let mut kem1 = MockKemType::new(); |
| 438 |
let mut kem2 = MockKemType::new(); |
| 439 |
let hash = MockHash::new(); |
| 440 |
let variable_length_hash = MockVariableLengthHash::new(); |
| 441 |
|
| 442 |
kem1.expect_generate().return_once(|| Ok((sk(1), pk(1)))); |
| 443 |
kem2.expect_generate().return_once(|| Ok((sk(2), pk(2)))); |
| 444 |
|
| 445 |
let kem = CombinedKem::new(kem1, kem2, hash, variable_length_hash); |
| 446 |
|
| 447 |
let keypair = kem.generate().unwrap(); |
| 448 |
assert_eq!(keypair.0, sk(12)); |
| 449 |
assert_eq!(keypair.1, pk(12)); |
| 450 |
} |
| 451 |
|
| 452 |
fn encap_test<F: SharedSecretHashInput>(hash_input_bytes: Vec<u8>, hash_input_fn: F) { |
| 453 |
let mut kem1 = MockKemType::new(); |
| 454 |
let mut kem2 = MockKemType::new(); |
| 455 |
let mut hash = MockHash::new(); |
| 456 |
let variable_length_hash = MockVariableLengthHash::new(); |
| 457 |
|
| 458 |
kem1.expect_public_key_size().returning(|| pk(1).len()); |
| 459 |
kem1.expect_enc_size().returning(|| enc(1).len()); |
| 460 |
|
| 461 |
kem1.expect_encap() |
| 462 |
.withf(|pk1| pk1 == &pk(1)) |
| 463 |
.return_once(|_| Ok(KemResult::new(ss(1), enc(1)))); |
| 464 |
|
| 465 |
kem2.expect_encap() |
| 466 |
.withf(|pk2| pk2 == &pk(2)) |
| 467 |
.return_once(|_| Ok(KemResult::new(ss(2), enc(2)))); |
| 468 |
|
| 469 |
hash.expect_hash() |
| 470 |
.withf(move |input| input == hash_input_bytes) |
| 471 |
.return_once(|_| Ok(b"shared secret".to_vec())); |
| 472 |
|
| 473 |
let kem = CombinedKem::new_custom(kem1, kem2, hash, variable_length_hash, hash_input_fn); |
| 474 |
|
| 475 |
let encap_result = kem.encap(&pk(12)).unwrap(); |
| 476 |
assert_eq!(encap_result.enc, enc(12)); |
| 477 |
assert_eq!(encap_result.shared_secret, b"shared secret"); |
| 478 |
} |
| 479 |
|
| 480 |
#[test] |
| 481 |
fn encap() { |
| 482 |
encap_test( |
| 483 |
[&enc(1)[..], &ss(1), &pk(1), &enc(2), &ss(2), &pk(2)].concat(), |
| 484 |
DefaultSharedSecretHashInput, |
| 485 |
); |
| 486 |
|
| 487 |
encap_test( |
| 488 |
[b"\\./\n/^\\".as_slice(), &ss(1), &ss(2), &enc(2), &pk(2)].concat(), |
| 489 |
XWingSharedSecretHashInput, |
| 490 |
); |
| 491 |
} |
| 492 |
|
| 493 |
#[test] |
| 494 |
fn decap() { |
| 495 |
let mut kem1 = MockKemType::new(); |
| 496 |
let mut kem2 = MockKemType::new(); |
| 497 |
let mut hash = MockHash::new(); |
| 498 |
let variable_length_hash = MockVariableLengthHash::new(); |
| 499 |
|
| 500 |
kem1.expect_public_key_size().returning(|| pk(1).len()); |
| 501 |
kem1.expect_enc_size().returning(|| enc(1).len()); |
| 502 |
kem1.expect_secret_key_size().returning(|| sk(1).len()); |
| 503 |
|
| 504 |
kem1.expect_decap() |
| 505 |
.withf(|enc1, sk1, pk1| enc1 == enc(1) && sk1 == &sk(1) && pk1 == &pk(1)) |
| 506 |
.return_once(|_, _, _| Ok(ss(1))); |
| 507 |
|
| 508 |
kem2.expect_decap() |
| 509 |
.withf(|enc2, sk2, pk2| enc2 == enc(2) && sk2 == &sk(2) && pk2 == &pk(2)) |
| 510 |
.return_once(|_, _, _| Ok(ss(2))); |
| 511 |
|
| 512 |
hash.expect_hash() |
| 513 |
.withf(|input| input == [&enc(1)[..], &ss(1), &pk(1), &enc(2), &ss(2), &pk(2)].concat()) |
| 514 |
.return_once(|_| Ok(b"shared secret".to_vec())); |
| 515 |
|
| 516 |
let kem = CombinedKem::new(kem1, kem2, hash, variable_length_hash); |
| 517 |
|
| 518 |
let decap_result = kem.decap(&enc(12), &sk(12), &pk(12)).unwrap(); |
| 519 |
assert_eq!(decap_result.as_slice(), b"shared secret"); |
| 520 |
} |
| 521 |
|
| 522 |
#[test] |
| 523 |
fn sizes() { |
| 524 |
let mut kem1 = MockKemType::new(); |
| 525 |
let mut kem2 = MockKemType::new(); |
| 526 |
let hash = MockHash::new(); |
| 527 |
let variable_length_hash = MockVariableLengthHash::new(); |
| 528 |
|
| 529 |
kem1.expect_public_key_size().returning(|| 1); |
| 530 |
kem1.expect_enc_size().returning(|| 10); |
| 531 |
kem1.expect_secret_key_size().returning(|| 100); |
| 532 |
|
| 533 |
kem2.expect_public_key_size().returning(|| 1000); |
| 534 |
kem2.expect_enc_size().returning(|| 10000); |
| 535 |
kem2.expect_secret_key_size().returning(|| 100000); |
| 536 |
|
| 537 |
let kem = CombinedKem::new(kem1, kem2, hash, variable_length_hash); |
| 538 |
|
| 539 |
assert_eq!(kem.public_key_size(), 1001); |
| 540 |
assert_eq!(kem.secret_key_size(), 100100); |
| 541 |
assert_eq!(kem.enc_size(), 10010); |
| 542 |
} |
| 543 |
} |