Skip to content

armgit / ente

Public Imported from GitHub
0

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

Remove redundant crypto size aliases

Manav Rathi918cced01ad2
diff --git a/rust/crates/core/src/crypto/blob.rs b/rust/crates/core/src/crypto/blob.rs
index fd9e5681c0..5a86eabc6a 100644
--- a/rust/crates/core/src/crypto/blob.rs
+++ b/rust/crates/core/src/crypto/blob.rs
@@ -1,10 +1,6 @@
use super::stream::{Decryptor, Encryptor};
use crate::crypto::{Error, Header, Key, Result};
-pub const KEY_BYTES: usize = Key::BYTES;
-
-pub const HEADER_BYTES: usize = Header::BYTES;
-
pub use super::stream::ABYTES;
#[derive(Debug, Clone)]
diff --git a/rust/crates/core/src/crypto/kdf.rs b/rust/crates/core/src/crypto/kdf.rs
index f04dd54d04..dc8f58487d 100644
--- a/rust/crates/core/src/crypto/kdf.rs
+++ b/rust/crates/core/src/crypto/kdf.rs
@@ -4,8 +4,6 @@ use crate::crypto::{Key, Result, SecretVec};
pub const CONTEXT_BYTES: usize = 8;
-pub const KEY_BYTES: usize = Key::BYTES;
-
pub const SUBKEY_BYTES_MIN: usize = 16;
pub const SUBKEY_BYTES_MAX: usize = 64;
diff --git a/rust/crates/core/src/crypto/sealed.rs b/rust/crates/core/src/crypto/sealed.rs
index 65812b5614..09025d9fc1 100644
--- a/rust/crates/core/src/crypto/sealed.rs
+++ b/rust/crates/core/src/crypto/sealed.rs
@@ -9,10 +9,6 @@ use zeroize::Zeroize;
use crate::crypto::{Error, PublicKey, Result, SecretKey, fill_random};
-pub const PUBLIC_KEY_BYTES: usize = PublicKey::BYTES;
-
-pub const SECRET_KEY_BYTES: usize = SecretKey::BYTES;
-
pub const SEAL_OVERHEAD: usize = 32 + 16;
fn seal_nonce(ephemeral_pk: &[u8; 32], recipient_pk: &[u8; 32]) -> [u8; 24] {
diff --git a/rust/crates/core/src/crypto/stream.rs b/rust/crates/core/src/crypto/stream.rs
index 80570c65f6..4b2ee3b8d4 100644
--- a/rust/crates/core/src/crypto/stream.rs
+++ b/rust/crates/core/src/crypto/stream.rs
@@ -15,10 +15,6 @@ use crate::crypto::{Error, Header, Key, Result};
const _: () = assert!(Key::BYTES == UpstreamKey::BYTES);
const _: () = assert!(Header::BYTES == UpstreamHeader::BYTES);
-pub const HEADER_BYTES: usize = Header::BYTES;
-
-pub const KEY_BYTES: usize = Key::BYTES;
-
pub const ABYTES: usize = Stream::ABYTES;
pub const ENCRYPTION_CHUNK_SIZE: usize = 4 * 1024 * 1024;
@@ -676,7 +672,7 @@ mod tests {
assert_eq!(plaintext.len(), decrypted.len());
assert_eq!(plaintext, decrypted);
- let ciphertext_len = encrypted.len() - HEADER_BYTES;
+ let ciphertext_len = encrypted.len() - Header::BYTES;
assert_eq!(ciphertext_len, estimate_encrypted_size(plaintext.len()));
}
@@ -735,7 +731,7 @@ mod tests {
encryptor.finish().expect("finish failed");
}
- assert_eq!(encrypted.len(), HEADER_BYTES + ABYTES);
+ assert_eq!(encrypted.len(), Header::BYTES + ABYTES);
let reader = Cursor::new(&encrypted);
let mut decryptor =
@@ -877,8 +873,8 @@ mod tests {
#[test]
fn test_constants_match_upstream() {
- assert_eq!(HEADER_BYTES, 24);
- assert_eq!(KEY_BYTES, 32);
+ assert_eq!(Header::BYTES, 24);
+ assert_eq!(Key::BYTES, 32);
assert_eq!(ABYTES, 17);
}
@@ -1243,7 +1239,7 @@ mod tests {
encryptor.write(&plaintext).expect("write failed");
encryptor.finish().expect("finish failed");
}
- let stream_ciphertext_len = enc_stream.len() - HEADER_BYTES;
+ let stream_ciphertext_len = enc_stream.len() - Header::BYTES;
let expected = estimate_encrypted_size(size);
diff --git a/rust/crates/ensu/src/db/chat/mod.rs b/rust/crates/ensu/src/db/chat/mod.rs
index ef72ac3599..f35559fc31 100644
--- a/rust/crates/ensu/src/db/chat/mod.rs
+++ b/rust/crates/ensu/src/db/chat/mod.rs
@@ -6,6 +6,7 @@ mod schema;
use std::collections::BTreeSet;
use std::sync::Arc;
+use ente_core::crypto::Key;
use uuid::Uuid;
use zeroize::Zeroizing;
@@ -396,9 +397,9 @@ impl ChatDb<crate::db::backend::sqlite::SqliteBackend> {
}
fn validate_key(key: Vec<u8>) -> Result<Zeroizing<Vec<u8>>> {
- if key.len() != crypto::KEY_BYTES {
+ if key.len() != Key::BYTES {
return Err(Error::InvalidKeyLength {
- expected: crypto::KEY_BYTES,
+ expected: Key::BYTES,
actual: key.len(),
});
}
@@ -443,10 +444,11 @@ mod tests {
use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::{Arc, Mutex};
+ use ente_core::crypto::Header;
+
use super::*;
use crate::db::backend::sqlite::SqliteBackend;
use crate::db::backend::{BackendTx, RowExt, Value};
- use crate::db::crypto::KEY_BYTES;
use crate::db::models::{AttachmentKind, AttachmentMeta};
#[derive(Debug)]
@@ -496,7 +498,7 @@ mod tests {
fn make_db(uuids: Vec<Uuid>, clock: Arc<dyn Clock>) -> ChatDb<SqliteBackend> {
ChatDb::new(
SqliteBackend::open_in_memory().unwrap(),
- vec![1u8; KEY_BYTES],
+ vec![1u8; Key::BYTES],
clock,
Arc::new(TestUuidGen::new(uuids)),
)
@@ -522,7 +524,7 @@ mod tests {
.unwrap()
.unwrap();
let stored = row.get_blob(0).unwrap();
- assert!(stored.len() >= crypto::HEADER_BYTES);
+ assert!(stored.len() >= Header::BYTES);
assert_ne!(stored, b"hello".to_vec());
}
@@ -582,7 +584,7 @@ mod tests {
let root = std::env::temp_dir().join(format!("ensu-v4-{}", Uuid::new_v4()));
std::fs::create_dir_all(&root).unwrap();
let path = root.join("llmchat.db");
- let key = vec![2u8; KEY_BYTES];
+ let key = vec![2u8; Key::BYTES];
let (session_id, message_id) = {
let db = ChatDb::open_sqlite_with_defaults(&path, key.clone()).unwrap();
let session = db.create_session("Before migration").unwrap();
diff --git a/rust/crates/ensu/src/db/chat/retired_storage.rs b/rust/crates/ensu/src/db/chat/retired_storage.rs
index 16b236abbd..518f97ecf5 100644
--- a/rust/crates/ensu/src/db/chat/retired_storage.rs
+++ b/rust/crates/ensu/src/db/chat/retired_storage.rs
@@ -16,10 +16,11 @@ pub(super) fn cleanup(db_path: &Path) {
#[cfg(test)]
mod tests {
- use crate::db::chat::ChatDb;
- use crate::db::crypto::KEY_BYTES;
+ use ente_core::crypto::Key;
use uuid::Uuid;
+ use crate::db::chat::ChatDb;
+
#[test]
fn sqlite_open_removes_retired_storage() {
let root = std::env::temp_dir().join(format!("ensu-db-{}", Uuid::new_v4()));
@@ -34,7 +35,7 @@ mod tests {
std::fs::write(root.join(name), b"obsolete").unwrap();
}
- ChatDb::open_sqlite_with_defaults(root.join("llmchat.db"), vec![1u8; KEY_BYTES]).unwrap();
+ ChatDb::open_sqlite_with_defaults(root.join("llmchat.db"), vec![1u8; Key::BYTES]).unwrap();
assert!(!root.join("llmchat_sync.db").exists());
assert!(!root.join("llmchat_sync.db-wal").exists());
diff --git a/rust/crates/ensu/src/db/crypto.rs b/rust/crates/ensu/src/db/crypto.rs
index 98f96ad48d..4dd6feba4f 100644
--- a/rust/crates/ensu/src/db/crypto.rs
+++ b/rust/crates/ensu/src/db/crypto.rs
@@ -3,9 +3,6 @@ use ente_core::crypto::{Header, Key, blob};
use crate::db::{Error, Result};
-pub const HEADER_BYTES: usize = blob::HEADER_BYTES;
-pub const KEY_BYTES: usize = blob::KEY_BYTES;
-
pub fn encrypt_blob(plaintext: &[u8], key: &[u8]) -> Result<Vec<u8>> {
Ok(blob::encrypt_combined(
plaintext,
@@ -14,13 +11,13 @@ pub fn encrypt_blob(plaintext: &[u8], key: &[u8]) -> Result<Vec<u8>> {
}
pub fn decrypt_blob(data: &[u8], key: &[u8]) -> Result<Vec<u8>> {
- if data.len() < HEADER_BYTES {
+ if data.len() < Header::BYTES {
return Err(Error::InvalidBlobLength {
- minimum: HEADER_BYTES,
+ minimum: Header::BYTES,
actual: data.len(),
});
}
- let (header, ciphertext) = data.split_at(HEADER_BYTES);
+ let (header, ciphertext) = data.split_at(Header::BYTES);
Ok(blob::decrypt(
ciphertext,
&Header::try_from_slice(header)?,
@@ -68,7 +65,7 @@ mod tests {
#[test]
fn blob_roundtrip() {
- let key = vec![7u8; KEY_BYTES];
+ let key = vec![7u8; Key::BYTES];
let plaintext = b"hello";
let encrypted = encrypt_blob(plaintext, &key).unwrap();
@@ -80,7 +77,7 @@ mod tests {
#[test]
fn json_field_roundtrip() {
- let key = vec![9u8; KEY_BYTES];
+ let key = vec![9u8; Key::BYTES];
let plaintext = "file-name.png";
let encrypted = encrypt_json_field(plaintext, &key).unwrap();
diff --git a/rust/crates/space/src/crypto.rs b/rust/crates/space/src/crypto.rs
index ffb7770911..3b07117e11 100644
--- a/rust/crates/space/src/crypto.rs
+++ b/rust/crates/space/src/crypto.rs
@@ -1,11 +1,11 @@
use ente_core::b64;
-use ente_core::crypto::{Key, Nonce, PublicKey, SecretKey, blob, sealed, secretbox};
+use ente_core::crypto::{Header, Key, Nonce, PublicKey, SecretKey, blob, sealed, secretbox};
use md5::{Digest, Md5};
use crate::error::Result;
pub(crate) const SECRETBOX_PAYLOAD_OVERHEAD_BYTES: usize = Nonce::BYTES + secretbox::MAC_BYTES;
-pub(crate) const ASSET_PAYLOAD_OVERHEAD_BYTES: usize = blob::HEADER_BYTES + blob::ABYTES;
+pub(crate) const ASSET_PAYLOAD_OVERHEAD_BYTES: usize = Header::BYTES + blob::ABYTES;
pub fn encrypt_space_root_entity_key(
space_root_key_b64: &str,