mirror of
https://github.com/chatmail/core.git
synced 2026-04-17 21:46:35 +03:00
Follow-up for https://github.com/chatmail/core/pull/7042, part of https://github.com/chatmail/core/issues/6884. This will make it possible to create invite-QR codes for broadcast channels, and make them symmetrically end-to-end encrypted. - [x] Go through all the changes in #7042, and check which ones I still need, and revert all other changes - [x] Use the classical Securejoin protocol, rather than the new 2-step protocol - [x] Make the Rust tests pass - [x] Make the Python tests pass - [x] Fix TODOs in the code - [x] Test it, and fix any bugs I find - [x] I found a bug when exporting all profiles at once fails sometimes, though this bug is unrelated to channels: https://github.com/chatmail/core/issues/7281 - [x] Do a self-review (i.e. read all changes, and check if I see some things that should be changed) - [x] Have this PR reviewed and merged - [ ] Open an issue for "TODO: There is a known bug in the securejoin protocol" - [ ] Create an issue that outlines how we can improve the Securejoin protocol in the future (I don't have the time to do this right now, but want to do it sometime in winter) - [ ] Write a guide for UIs how to adapt to the changes (see https://github.com/deltachat/deltachat-android/pull/3886) ## Backwards compatibility This is not very backwards compatible: - Trying to join a symmetrically-encrypted broadcast channel with an old device will fail - If you joined a symmetrically-encrypted broadcast channel with one device, and use an old core on the other device, then the other device will show a mostly empty chat (except for two device messages) - If you created a broadcast channel in the past, then you will get an error message when trying to send into the channel: > The up to now "experimental channels feature" is about to become an officially supported one. By that, privacy will be improved, it will become faster, and less traffic will be consumed. > > As we do not guarantee feature-stability for such experiments, this means, that you will need to create the channel again. > > Here is what to do: > • Create a new channel > • Tap on the channel name > • Tap on "QR Invite Code" > • Have all recipients scan the QR code, or send them the link > > If you have any questions, please send an email to delta@merlinux.eu or ask at https://support.delta.chat/. ## The symmetric encryption Symmetric encryption uses a shared secret. Currently, we use AES128 for encryption everywhere in Delta Chat, so, this is what I'm using for broadcast channels (though it wouldn't be hard to switch to AES256). The secret shared between all members of a broadcast channel has 258 bits of entropy (see `fn create_broadcast_shared_secret` in the code). Since the shared secrets have more entropy than the AES session keys, it's not necessary to have a hard-to-compute string2key algorithm, so, I'm using the string2key algorithm `salted`. This is fast enough that Delta Chat can just try out all known shared secrets. [^1] In order to prevent DOS attacks, Delta Chat will not attempt to decrypt with a string2key algorithm other than `salted` [^2]. ## The "Securejoin" protocol that adds members to the channel after they scanned a QR code This PR uses the classical securejoin protocol, the same that is also used for group and 1:1 invitations. The messages sent back and forth are called `vg-request`, `vg-auth-required`, `vg-request-with-auth`, and `vg-member-added`. I considered using the `vc-` prefix, because from a protocol-POV, the distinction between `vc-` and `vg-` isn't important (as @link2xt pointed out in an in-person discussion), but 1. it would be weird if groups used `vg-` while broadcasts and 1:1 chats used `vc-`, 2. we don't have a `vc-member-added` message yet, so, this would mean one more different kind of message 3. we anyways want to switch to a new securejoin protocol soon, which will be a backwards incompatible change with a transition phase. When we do this change, we can make everything `vc-`. [^1]: In a symmetrically encrypted message, it's not visible which secret was used to encrypt without trying out all secrets. If this does turn out to be too slow in the future, then we can remember which secret was used more recently, and and try the most recent secret first. If this is still too slow, then we can assign a short, non-unique (~2 characters) id to every shared secret, and send it in cleartext. The receiving Delta Chat will then only try out shared secrets with this id. Of course, this would leak a little bit of metadata in cleartext, so, I would like to avoid it. [^2]: A DOS attacker could send a message with a lot of encrypted session keys, all of which use a very hard-to-compute string2key algorithm. Delta Chat would then try to decrypt all of the encrypted session keys with all of the known shared secrets. In order to prevent this, as I said, Delta Chat will not attempt to decrypt with a string2key algorithm other than `salted` BREAKING CHANGE: A new QR type AskJoinBroadcast; cloning a broadcast channel is no longer possible; manually adding a member to a broadcast channel is no longer possible (only by having them scan a QR code)
201 lines
6.4 KiB
Rust
201 lines
6.4 KiB
Rust
//! Benchmarks for message decryption,
|
|
//! comparing decryption of symmetrically-encrypted messages
|
|
//! to decryption of asymmetrically-encrypted messages.
|
|
//!
|
|
//! Call with
|
|
//!
|
|
//! ```text
|
|
//! cargo bench --bench decrypting --features="internals"
|
|
//! ```
|
|
//!
|
|
//! or, if you want to only run e.g. the 'Decrypt a symmetrically encrypted message' benchmark:
|
|
//!
|
|
//! ```text
|
|
//! cargo bench --bench decrypting --features="internals" -- 'Decrypt a symmetrically encrypted message'
|
|
//! ```
|
|
//!
|
|
//! You can also pass a substring.
|
|
//! So, you can run all 'Decrypt and parse' benchmarks with:
|
|
//!
|
|
//! ```text
|
|
//! cargo bench --bench decrypting --features="internals" -- 'Decrypt and parse'
|
|
//! ```
|
|
//!
|
|
//! Symmetric decryption has to try out all known secrets,
|
|
//! You can benchmark this by adapting the `NUM_SECRETS` variable.
|
|
|
|
use std::hint::black_box;
|
|
|
|
use criterion::{Criterion, criterion_group, criterion_main};
|
|
use deltachat::internals_for_benches::create_broadcast_secret;
|
|
use deltachat::internals_for_benches::create_dummy_keypair;
|
|
use deltachat::internals_for_benches::save_broadcast_secret;
|
|
use deltachat::{
|
|
Events,
|
|
chat::ChatId,
|
|
config::Config,
|
|
context::Context,
|
|
internals_for_benches::key_from_asc,
|
|
internals_for_benches::parse_and_get_text,
|
|
internals_for_benches::store_self_keypair,
|
|
pgp::{KeyPair, decrypt, pk_encrypt, symm_encrypt_message},
|
|
stock_str::StockStrings,
|
|
};
|
|
use rand::{Rng, rng};
|
|
use tempfile::tempdir;
|
|
|
|
const NUM_SECRETS: usize = 500;
|
|
|
|
async fn create_context() -> Context {
|
|
let dir = tempdir().unwrap();
|
|
let dbfile = dir.path().join("db.sqlite");
|
|
let context = Context::new(dbfile.as_path(), 100, Events::new(), StockStrings::new())
|
|
.await
|
|
.unwrap();
|
|
|
|
context
|
|
.set_config(Config::ConfiguredAddr, Some("bob@example.net"))
|
|
.await
|
|
.unwrap();
|
|
let secret = key_from_asc(include_str!("../test-data/key/bob-secret.asc")).unwrap();
|
|
let public = secret.signed_public_key();
|
|
let key_pair = KeyPair { public, secret };
|
|
store_self_keypair(&context, &key_pair)
|
|
.await
|
|
.expect("Failed to save key");
|
|
|
|
context
|
|
}
|
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
|
let mut group = c.benchmark_group("Decrypt");
|
|
|
|
// ===========================================================================================
|
|
// Benchmarks for decryption only, without any other parsing
|
|
// ===========================================================================================
|
|
|
|
group.sample_size(10);
|
|
|
|
group.bench_function("Decrypt a symmetrically encrypted message", |b| {
|
|
let plain = generate_plaintext();
|
|
let secrets = generate_secrets();
|
|
let encrypted = tokio::runtime::Runtime::new().unwrap().block_on(async {
|
|
let secret = secrets[NUM_SECRETS / 2].clone();
|
|
symm_encrypt_message(
|
|
plain.clone(),
|
|
create_dummy_keypair("alice@example.org").unwrap().secret,
|
|
black_box(&secret),
|
|
true,
|
|
)
|
|
.await
|
|
.unwrap()
|
|
});
|
|
|
|
b.iter(|| {
|
|
let mut msg =
|
|
decrypt(encrypted.clone().into_bytes(), &[], black_box(&secrets)).unwrap();
|
|
let decrypted = msg.as_data_vec().unwrap();
|
|
|
|
assert_eq!(black_box(decrypted), plain);
|
|
});
|
|
});
|
|
|
|
group.bench_function("Decrypt a public-key encrypted message", |b| {
|
|
let plain = generate_plaintext();
|
|
let key_pair = create_dummy_keypair("alice@example.org").unwrap();
|
|
let secrets = generate_secrets();
|
|
let encrypted = tokio::runtime::Runtime::new().unwrap().block_on(async {
|
|
pk_encrypt(
|
|
plain.clone(),
|
|
vec![black_box(key_pair.public.clone())],
|
|
Some(key_pair.secret.clone()),
|
|
true,
|
|
true,
|
|
)
|
|
.await
|
|
.unwrap()
|
|
});
|
|
|
|
b.iter(|| {
|
|
let mut msg = decrypt(
|
|
encrypted.clone().into_bytes(),
|
|
std::slice::from_ref(&key_pair.secret),
|
|
black_box(&secrets),
|
|
)
|
|
.unwrap();
|
|
let decrypted = msg.as_data_vec().unwrap();
|
|
|
|
assert_eq!(black_box(decrypted), plain);
|
|
});
|
|
});
|
|
|
|
// ===========================================================================================
|
|
// Benchmarks for the whole parsing pipeline, incl. decryption (but excl. receive_imf())
|
|
// ===========================================================================================
|
|
|
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
|
let mut secrets = generate_secrets();
|
|
|
|
// "secret" is the shared secret that was used to encrypt text_symmetrically_encrypted.eml.
|
|
// Put it into the middle of our secrets:
|
|
secrets[NUM_SECRETS / 2] = "secret".to_string();
|
|
|
|
let context = rt.block_on(async {
|
|
let context = create_context().await;
|
|
for (i, secret) in secrets.iter().enumerate() {
|
|
save_broadcast_secret(&context, ChatId::new(10 + i as u32), secret)
|
|
.await
|
|
.unwrap();
|
|
}
|
|
context
|
|
});
|
|
|
|
group.bench_function("Decrypt and parse a symmetrically encrypted message", |b| {
|
|
b.to_async(&rt).iter(|| {
|
|
let ctx = context.clone();
|
|
async move {
|
|
let text = parse_and_get_text(
|
|
&ctx,
|
|
include_bytes!("../test-data/message/text_symmetrically_encrypted.eml"),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(text, "Symmetrically encrypted message");
|
|
}
|
|
});
|
|
});
|
|
|
|
group.bench_function("Decrypt and parse a public-key encrypted message", |b| {
|
|
b.to_async(&rt).iter(|| {
|
|
let ctx = context.clone();
|
|
async move {
|
|
let text = parse_and_get_text(
|
|
&ctx,
|
|
include_bytes!("../test-data/message/text_from_alice_encrypted.eml"),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(text, "hi");
|
|
}
|
|
});
|
|
});
|
|
|
|
group.finish();
|
|
}
|
|
|
|
fn generate_secrets() -> Vec<String> {
|
|
let secrets: Vec<String> = (0..NUM_SECRETS)
|
|
.map(|_| create_broadcast_secret())
|
|
.collect();
|
|
secrets
|
|
}
|
|
|
|
fn generate_plaintext() -> Vec<u8> {
|
|
let mut plain: Vec<u8> = vec![0; 500];
|
|
rng().fill(&mut plain[..]);
|
|
plain
|
|
}
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
criterion_main!(benches);
|