Skip to content

Commit

Permalink
fix: Broken Proteus implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
OtaK committed Dec 14, 2022
1 parent cfe1837 commit f0dc510
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
interval: "weekly"
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ branch = "otak/fix-1.0.3"
[patch.crates-io.proteus-traits]
package = "proteus-traits"
git = "https://github.com/wireapp/proteus"
branch = "otak/oxidize-wasm-v2"
branch = "otak/2.0"

[patch.crates-io.openmls]
git = "https://github.com/wireapp/openmls"
Expand Down
24 changes: 16 additions & 8 deletions crypto-ffi/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,14 @@ impl CoreCrypto<'_> {
/// See [core_crypto::proteus::ProteusCentral::encrypt]
pub fn proteus_encrypt(&self, session_id: &str, plaintext: &[u8]) -> CryptoResult<Vec<u8>> {
proteus_impl! {{
self.central
.lock()
.map_err(|_| CryptoError::LockPoisonError)?
.proteus_encrypt(session_id, plaintext)
future::block_on(
self.executor.lock().map_err(|_| CryptoError::LockPoisonError)?.run(
self.central
.lock()
.map_err(|_| CryptoError::LockPoisonError)?
.proteus_encrypt(session_id, plaintext)
),
)
}}
}

Expand All @@ -883,10 +887,14 @@ impl CoreCrypto<'_> {
plaintext: &[u8],
) -> CryptoResult<std::collections::HashMap<String, Vec<u8>>> {
proteus_impl! {{
self.central
.lock()
.map_err(|_| CryptoError::LockPoisonError)?
.proteus_encrypt_batched(sessions.as_slice(), plaintext)
future::block_on(
self.executor.lock().map_err(|_| CryptoError::LockPoisonError)?.run(
self.central
.lock()
.map_err(|_| CryptoError::LockPoisonError)?
.proteus_encrypt_batched(sessions.as_slice(), plaintext)
)
)
}}
}

Expand Down
4 changes: 2 additions & 2 deletions crypto-ffi/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ impl CoreCrypto {
#[cfg_attr(not(feature = "proteus"), allow(unused_variables))]
pub async fn proteus_encrypt(&self, session_id: String, plaintext: Box<[u8]>) -> WasmCryptoResult<Uint8Array> {
proteus_impl!({
let encrypted = self.0.write().await.proteus_encrypt(&session_id, &plaintext)?;
let encrypted = self.0.write().await.proteus_encrypt(&session_id, &plaintext).await?;
WasmCryptoResult::Ok(Uint8Array::from(encrypted.as_slice()).into())
} or throw WasmCryptoResult<_>)
}
Expand All @@ -1455,7 +1455,7 @@ impl CoreCrypto {
) -> WasmCryptoResult<js_sys::Map> {
proteus_impl!({
let session_ids: Vec<String> = sessions.into_iter().map(String::from).collect();
let batch = self.0.write().await.proteus_encrypt_batched(session_ids.as_slice(), &plaintext)?;
let batch = self.0.write().await.proteus_encrypt_batched(session_ids.as_slice(), &plaintext).await?;
let js_obj = js_sys::Map::new();
for (key, payload) in batch.into_iter() {
js_obj.set(&js_sys::JsString::from(key).into(), &Uint8Array::from(payload.as_slice()));
Expand Down
7 changes: 3 additions & 4 deletions crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ serde_json = "1.0"

[dependencies.proteus-wasm]
version = "2.0"
package = "proteus"
features = ["hazmat"]
optional = true
git = "https://github.com/wireapp/proteus"
branch = "otak/oxidize-wasm-v2"
branch = "otak/2.0"

[target.'cfg(not(target_family = "wasm"))'.dependencies]
async-fs = { version = "1.6", optional = true }
Expand Down Expand Up @@ -87,8 +86,8 @@ proteus-traits = "2.0"
async-trait = "0.1"

[target.'cfg(not(target_family = "wasm"))'.dev-dependencies]
cryptobox = { git = "https://github.com/wireapp/cryptobox" }
proteus = { git = "https://github.com/wireapp//proteus", branch = "otak/fix-1.0.3" }
cryptobox = { git = "https://github.com/wireapp/cryptobox", tag = "v1.0.3" }
proteus = { git = "https://github.com/wireapp//proteus", branch = "otak/fix-1.0.3", features = [] }

[target.'cfg(not(target_family = "wasm"))'.dev-dependencies.criterion]
version = "0.3"
Expand Down
9 changes: 7 additions & 2 deletions crypto/benches/mls_proteus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,14 @@ fn encrypt_message_bench(c: &mut Criterion) {
let text = Alphanumeric.sample_string(&mut rand::thread_rng(), MSG_MAX);
(central, keystore, session_material, text)
},
|(mut central, keystore, session_material, text)| async move {
|(mut central, mut keystore, session_material, text)| async move {
for (session_id, _) in session_material {
black_box(central.encrypt(&session_id, text.as_bytes()).unwrap());
black_box(
central
.encrypt(&mut keystore, &session_id, text.as_bytes())
.await
.unwrap(),
);
black_box(central.session_save(&keystore, &session_id).await.unwrap());
}
},
Expand Down
Loading

0 comments on commit f0dc510

Please sign in to comment.