封装两设备 MLS 客户端引擎与状态恢复

This commit is contained in:
xhgit committed 2026-11-13 18:31:29 +08:00
1 parent 0b17dd07c5
commit 1e05d7a3af
7 files changed
+448

No files matched your search

+63
View File
@@ -1,3 +1,9 @@
#[cfg(any(test, target_arch = "wasm32"))]
mod engine;
#[cfg(target_arch = "wasm32")]
mod wasm;
#[cfg(test)]
mod tests {
use ::tls_codec::{Deserialize as _, Serialize as _};
@@ -12,6 +18,63 @@ mod tests {
const SUITE: Ciphersuite = Ciphersuite::MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn device_conversation_survives_restore() {
use crate::engine::Engine;
let alice_id = "c82d961b-aa65-4d44-9de8-1967d32ee8d3";
let bob_id = "346c8638-f422-471a-827d-d07a1bcf8ae8";
let alice = Engine::new(alice_id).unwrap();
let bob = Engine::new(bob_id).unwrap();
let group = [5; 32];
let package = bob.key_package().unwrap();
assert!(alice.start(&group, bob_id, &[0; 32], &package).is_err());
let welcome = alice
.start(&group, bob_id, bob.public_key(), &package)
.unwrap();
assert!(
alice
.start(&group, bob_id, bob.public_key(), &package)
.is_err()
);
let bob_backup = bob.snapshot().unwrap();
let wrong_key = Engine::restore(&bob_backup).unwrap();
assert!(wrong_key.join(alice_id, &[0; 32], &welcome).is_err());
let bob = Engine::restore(&bob_backup).unwrap();
assert_eq!(bob.device(), bob_id);
assert_eq!(
bob.join(alice_id, alice.public_key(), &welcome).unwrap(),
group
);
let alice = Engine::restore(&alice.snapshot().unwrap()).unwrap();
let message = alice.encrypt(&group, b"message-1", b"hello").unwrap();
let bob_backup = bob.snapshot().unwrap();
let wrong_aad = Engine::restore(&bob_backup).unwrap();
assert!(
wrong_aad
.decrypt(alice_id, alice.public_key(), b"message-2", &message)
.is_err()
);
let bob = Engine::restore(&bob_backup).unwrap();
assert_eq!(
bob.decrypt(alice_id, alice.public_key(), b"message-1", &message)
.unwrap(),
b"hello"
);
let bob = Engine::restore(&bob.snapshot().unwrap()).unwrap();
assert!(
bob.decrypt(alice_id, alice.public_key(), b"message-1", &message)
.is_err()
);
let reply = bob.encrypt(&group, b"reply", b"hello back").unwrap();
assert_eq!(
alice
.decrypt(bob_id, bob.public_key(), b"reply", &reply)
.unwrap(),
b"hello back"
);
}
fn identity(id: &[u8]) -> (SignatureKeyPair, CredentialWithKey) {
let signer = SignatureKeyPair::new(SUITE.signature_algorithm()).unwrap();
let credential = CredentialWithKey {