π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
rust/core: add fuzz targets
Prateek Sunalb7284a6c816a
diff --git a/rust/core/fuzz/.gitignore b/rust/core/fuzz/.gitignorenew file mode 100644index 0000000000..e66a943b5d--- /dev/null+++ b/rust/core/fuzz/.gitignore@@ -0,0 +1,4 @@+/corpus/+/artifacts/+/target/+Cargo.lockdiff --git a/rust/core/fuzz/Cargo.toml b/rust/core/fuzz/Cargo.tomlnew file mode 100644index 0000000000..eee8dab9ba--- /dev/null+++ b/rust/core/fuzz/Cargo.toml@@ -0,0 +1,37 @@+[package]+name = "ente-core-fuzz"+version = "0.0.0"+publish = false+edition = "2021"++[package.metadata]+cargo-fuzz = true++[dependencies]+libfuzzer-sys = "0.4"+arbitrary = { version = "1", features = ["derive"] }+ente-core = { path = ".." }++[[bin]]+name = "secretbox"+path = "fuzz_targets/secretbox.rs"+test = false+doc = false++[[bin]]+name = "sealed_box"+path = "fuzz_targets/sealed_box.rs"+test = false+doc = false++[[bin]]+name = "stream"+path = "fuzz_targets/stream.rs"+test = false+doc = false++[[bin]]+name = "argon"+path = "fuzz_targets/argon.rs"+test = false+doc = falsediff --git a/rust/core/fuzz/fuzz_targets/argon.rs b/rust/core/fuzz/fuzz_targets/argon.rsnew file mode 100644index 0000000000..7b41808ca3--- /dev/null+++ b/rust/core/fuzz/fuzz_targets/argon.rs@@ -0,0 +1,21 @@+#![no_main]++use arbitrary::Arbitrary;+use libfuzzer_sys::fuzz_target;++#[derive(Arbitrary, Debug)]+struct ArgonInput {+ password: Vec<u8>,+ salt: [u8; 16],+ mem_kib: u16,+ ops: u8,+}++fuzz_target!(|input: ArgonInput| {+ // Clamp to values that are fast enough for fuzzing.+ let mem_limit = 8_192u32 + (u32::from(input.mem_kib % 64) * 1024); // 8 KiB .. 72 KiB+ let ops_limit = 1u32 + (u32::from(input.ops % 5)); // 1..=5++ let password = String::from_utf8_lossy(&input.password);+ let _ = ente_core::crypto::argon::derive_key(password.as_ref(), &input.salt, mem_limit, ops_limit);+});diff --git a/rust/core/fuzz/fuzz_targets/sealed_box.rs b/rust/core/fuzz/fuzz_targets/sealed_box.rsnew file mode 100644index 0000000000..4e458af04f--- /dev/null+++ b/rust/core/fuzz/fuzz_targets/sealed_box.rs@@ -0,0 +1,20 @@+#![no_main]++use arbitrary::Arbitrary;+use libfuzzer_sys::fuzz_target;++#[derive(Arbitrary, Debug)]+struct SealedBoxInput {+ ciphertext: Vec<u8>,+ recipient_pk: [u8; 32],+ recipient_sk: [u8; 32],+}++fuzz_target!(|input: SealedBoxInput| {+ // Primary goal: ensure `open()` never panics on malformed inputs.+ let _ = ente_core::crypto::sealed::open(+ &input.ciphertext,+ &input.recipient_pk,+ &input.recipient_sk,+ );+});diff --git a/rust/core/fuzz/fuzz_targets/secretbox.rs b/rust/core/fuzz/fuzz_targets/secretbox.rsnew file mode 100644index 0000000000..40f9555559--- /dev/null+++ b/rust/core/fuzz/fuzz_targets/secretbox.rs@@ -0,0 +1,38 @@+#![no_main]++use arbitrary::Arbitrary;+use libfuzzer_sys::fuzz_target;++#[derive(Arbitrary, Debug)]+struct SecretBoxInput {+ plaintext: Vec<u8>,+ key: [u8; 32],+ nonce: [u8; 24],+ flip_bit: bool,+}++fuzz_target!(|input: SecretBoxInput| {+ // Roundtrip: encrypt_with_nonce is deterministic, so fuzzing stays reproducible.+ if let Ok(ciphertext) = ente_core::crypto::secretbox::encrypt_with_nonce(+ &input.plaintext,+ &input.nonce,+ &input.key,+ ) {+ let mut ct = ciphertext;++ // Optional: corrupt a byte to exercise error paths.+ if input.flip_bit && !ct.is_empty() {+ ct[0] ^= 0x01;+ }++ let decrypted = ente_core::crypto::secretbox::decrypt(&ct, &input.nonce, &input.key);++ if input.flip_bit {+ // Corruption should fail (unless ciphertext was empty, which never happens: MAC is+ // always present, so ct is at least 16 bytes).+ assert!(decrypted.is_err());+ } else {+ assert_eq!(decrypted.unwrap(), input.plaintext);+ }+ }+});diff --git a/rust/core/fuzz/fuzz_targets/stream.rs b/rust/core/fuzz/fuzz_targets/stream.rsnew file mode 100644index 0000000000..99bc292d37--- /dev/null+++ b/rust/core/fuzz/fuzz_targets/stream.rs@@ -0,0 +1,21 @@+#![no_main]++use arbitrary::Arbitrary;+use libfuzzer_sys::fuzz_target;++#[derive(Arbitrary, Debug)]+struct StreamInput {+ header: [u8; 24],+ key: [u8; 32],+ ciphertext: Vec<u8>,+ ad: Vec<u8>,+}++fuzz_target!(|input: StreamInput| {+ // Primary goal: ensure secretstream parsing/decryption never panics.+ if let Ok(mut decryptor) = ente_core::crypto::stream::StreamDecryptor::new(&input.header, &input.key)+ {+ let _ = decryptor.pull_with_ad(&input.ciphertext, &input.ad);+ let _ = decryptor.pull_typed_with_ad(&input.ciphertext, &input.ad);+ }+});