Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

crypto: gate blob final tag and default hash len

Prateek Sunalced97e76f1d2
diff --git a/rust/core/Cargo.toml b/rust/core/Cargo.toml
index 27b514186d..d69b4df4a9 100644
--- a/rust/core/Cargo.toml
+++ b/rust/core/Cargo.toml
@@ -40,7 +40,7 @@ sha2 = "0.10"
getrandom = "0.2"
[features]
-default = []
+default = ["blob-require-final-tag"]
# Enable SRP (Secure Remote Password) client for password-based authentication.
#
@@ -51,6 +51,9 @@ default = []
# ente-core = { version = "0.0.1", features = ["srp"] }
srp = ["dep:srp", "dep:sha2", "dep:getrandom"]
+# Enforce TAG_FINAL on blob decrypt (reject non-final chunks).
+blob-require-final-tag = []
+
# No features needed - pure Rust is always enabled
[lints.rust]
diff --git a/rust/core/src/crypto/impl_pure/blob.rs b/rust/core/src/crypto/impl_pure/blob.rs
index 47513b5207..fd3283ef8f 100644
--- a/rust/core/src/crypto/impl_pure/blob.rs
+++ b/rust/core/src/crypto/impl_pure/blob.rs
@@ -59,6 +59,10 @@ pub fn encrypt(plaintext: &[u8], key: &[u8]) -> Result<EncryptedBlob> {
///
/// # Returns
/// The decrypted plaintext.
+///
+/// # Errors
+/// Returns `CryptoError::StreamTruncated` when the `blob-require-final-tag`
+/// feature is enabled and the ciphertext does not carry `TAG_FINAL`.
pub fn decrypt(ciphertext: &[u8], header: &[u8], key: &[u8]) -> Result<Vec<u8>> {
if key.len() != KEY_BYTES {
return Err(CryptoError::InvalidKeyLength {
@@ -85,7 +89,10 @@ pub fn decrypt(ciphertext: &[u8], header: &[u8], key: &[u8]) -> Result<Vec<u8>>
let mut decryptor = StreamDecryptor::new(header, key)?;
// Decrypt
- let (plaintext, _tag) = decryptor.pull(ciphertext)?;
+ let (plaintext, tag) = decryptor.pull(ciphertext)?;
+ if cfg!(feature = "blob-require-final-tag") && tag != TAG_FINAL {
+ return Err(CryptoError::StreamTruncated);
+ }
Ok(plaintext)
}
@@ -164,6 +171,21 @@ mod tests {
assert_eq!(decrypted, plaintext);
}
+ #[test]
+ fn test_decrypt_non_final_tag_behavior() {
+ let key = keys::generate_stream_key();
+ let mut encryptor = StreamEncryptor::new(&key).unwrap();
+ let header = encryptor.header.clone();
+ let ciphertext = encryptor.push(b"partial", false).unwrap();
+
+ let result = decrypt(&ciphertext, &header, &key);
+ if cfg!(feature = "blob-require-final-tag") {
+ assert!(matches!(result, Err(CryptoError::StreamTruncated)));
+ } else {
+ assert!(result.is_ok());
+ }
+ }
+
#[test]
fn test_encrypt_data_wrapper() {
let key = keys::generate_stream_key();
diff --git a/rust/core/src/crypto/impl_pure/hash.rs b/rust/core/src/crypto/impl_pure/hash.rs
index ae99725625..260dd9f4ae 100644
--- a/rust/core/src/crypto/impl_pure/hash.rs
+++ b/rust/core/src/crypto/impl_pure/hash.rs
@@ -29,13 +29,13 @@ pub const KEY_BYTES_MAX: usize = 64;
///
/// # Arguments
/// * `data` - Data to hash.
-/// * `out_len` - Optional output length (16-64 bytes). Defaults to 64.
+/// * `out_len` - Optional output length (16-64 bytes). Defaults to 32.
/// * `key` - Optional key for keyed hashing (0 or 16-64 bytes).
///
/// # Returns
/// Hash output of the specified length.
pub fn hash(data: &[u8], out_len: Option<usize>, key: Option<&[u8]>) -> Result<Vec<u8>> {
- let out_len = out_len.unwrap_or(HASH_BYTES_MAX);
+ let out_len = out_len.unwrap_or(HASH_BYTES);
if !(HASH_BYTES_MIN..=HASH_BYTES_MAX).contains(&out_len) {
return Err(CryptoError::InvalidKeyLength {
@@ -64,15 +64,15 @@ pub fn hash(data: &[u8], out_len: Option<usize>, key: Option<&[u8]>) -> Result<V
Ok(hash.as_bytes()[..out_len].to_vec())
}
-/// Compute BLAKE2b hash with default parameters (64-byte output, no key).
+/// Compute BLAKE2b hash with default parameters (32-byte output, no key).
///
/// # Arguments
/// * `data` - Data to hash.
///
/// # Returns
-/// 64-byte hash output.
+/// 32-byte hash output.
pub fn hash_default(data: &[u8]) -> Result<Vec<u8>> {
- hash(data, Some(HASH_BYTES_MAX), None)
+ hash(data, Some(HASH_BYTES), None)
}
/// Streaming hash state for incremental hashing.
@@ -88,13 +88,13 @@ impl HashState {
/// Create a new hash state.
///
/// # Arguments
- /// * `out_len` - Optional output length (16-64 bytes). Defaults to 64.
+ /// * `out_len` - Optional output length (16-64 bytes). Defaults to 32.
/// * `key` - Optional key for keyed hashing (0 or 16-64 bytes).
///
/// # Returns
/// A new hash state ready for incremental updates.
pub fn new(out_len: Option<usize>, key: Option<&[u8]>) -> Result<Self> {
- let out_len = out_len.unwrap_or(HASH_BYTES_MAX);
+ let out_len = out_len.unwrap_or(HASH_BYTES);
if !(HASH_BYTES_MIN..=HASH_BYTES_MAX).contains(&out_len) {
return Err(CryptoError::InvalidKeyLength {
@@ -143,9 +143,9 @@ impl HashState {
}
}
-/// Create a new hash state with default parameters (64-byte output, no key).
+/// Create a new hash state with default parameters (32-byte output, no key).
pub fn hash_state_new() -> Result<HashState> {
- HashState::new(Some(HASH_BYTES_MAX), None)
+ HashState::new(Some(HASH_BYTES), None)
}
/// Hash data from a reader.
@@ -154,7 +154,7 @@ pub fn hash_state_new() -> Result<HashState> {
///
/// # Arguments
/// * `reader` - Source to read data from.
-/// * `out_len` - Optional output length (16-64 bytes). Defaults to 64.
+/// * `out_len` - Optional output length (16-64 bytes). Defaults to 32.
///
/// # Returns
/// Hash output of the specified length.
@@ -181,7 +181,7 @@ mod tests {
fn test_hash_default() {
let data = b"Hello, World!";
let hash = hash_default(data).unwrap();
- assert_eq!(hash.len(), HASH_BYTES_MAX);
+ assert_eq!(hash.len(), HASH_BYTES);
}
#[test]
@@ -276,14 +276,14 @@ mod tests {
fn test_empty_data() {
let data = b"";
let hash = hash_default(data).unwrap();
- assert_eq!(hash.len(), HASH_BYTES_MAX);
+ assert_eq!(hash.len(), HASH_BYTES);
}
#[test]
fn test_large_data() {
let data = vec![0x42u8; 1024 * 1024]; // 1 MB
let hash = hash_default(&data).unwrap();
- assert_eq!(hash.len(), HASH_BYTES_MAX);
+ assert_eq!(hash.len(), HASH_BYTES);
}
#[test]
@@ -302,6 +302,6 @@ mod tests {
.count();
// Should have significant differences (avalanche effect)
- assert!(diff_count > 30); // Expect ~50% different
+ assert!(diff_count > 15); // Expect ~50% different
}
}