Skip to content

armgit / ente

Public Imported from GitHub
0

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

Trim wasm-core docs to summaries and refine crypto phrasing

Manav Rathi05559bc1c996
diff --git a/rust/crates/core/src/crypto/blob.rs b/rust/crates/core/src/crypto/blob.rs
index 1518af569b..be76a1e388 100644
--- a/rust/crates/core/src/crypto/blob.rs
+++ b/rust/crates/core/src/crypto/blob.rs
@@ -59,7 +59,7 @@ impl EncryptedBlob {
}
}
-/// Encrypt `data` under `key` as a single secretstream message.
+/// Encrypt `data` with `key` as a single secretstream message.
///
/// A fresh header is generated for every call. The message is tagged final, so
/// [`decrypt`] can later confirm the ciphertext is complete. Returns the
@@ -135,7 +135,7 @@ fn decrypt_impl(data: &[u8], header: &Header, key: &Key) -> Result<(Vec<u8>, boo
decryptor.pull(data)
}
-/// Encrypt `data` under `key` into one self-contained buffer.
+/// Encrypt `data` with `key` into one self-contained buffer.
///
/// Like [`encrypt`], but prepends the decryption header to the ciphertext,
/// returning `header β€– ciphertext`: everything needed to decrypt except the
diff --git a/rust/crates/core/src/crypto/secretbox.rs b/rust/crates/core/src/crypto/secretbox.rs
index d62546c8e6..76525781e9 100644
--- a/rust/crates/core/src/crypto/secretbox.rs
+++ b/rust/crates/core/src/crypto/secretbox.rs
@@ -1,6 +1,6 @@
//! Authenticated encryption with XSalsa20-Poly1305.
//!
-//! SecretBox encrypts and authenticates a single, self-contained value under a
+//! SecretBox encrypts and authenticates a single, self-contained value with a
//! 256-bit key. It suits small, independent pieces of data such as a wrapped
//! key or a short field. Ente uses [`blob`](super::blob) for data attached to
//! an object (file or collection metadata and the like), and
@@ -10,10 +10,8 @@
//! `crypto_secretbox`; the implementation here is pure Rust but wire-compatible
//! (recorded per function below).
//!
-//! Every message takes a 192-bit nonce. The nonce is not secret, but it must
-//! never be reused with the same key. The
-//! [`encrypt`] and [`encrypt_combined`] functions generate a fresh random nonce
-//! for you, which is the safe default.
+//! Every message uses a 192-bit nonce, generated fresh on each call. It is not
+//! secret, but it is needed to decrypt.
//!
//! Two payload shapes are offered, differing only in how the nonce travels:
//!
@@ -60,15 +58,11 @@ impl EncryptedBox {
}
}
-/// Encrypt `data` under `key`, generating a fresh random nonce.
+/// Encrypt `data` with `key` and a freshly generated random nonce.
///
-/// This is the default choice for secretbox encryption: a new nonce is
-/// generated on every call, so a (key, nonce) pair cannot be reused by mistake.
-/// The plaintext is not padded, so the ciphertext length reveals the exact
-/// plaintext length.
-///
-/// Returns the ciphertext and the generated nonce; decrypt with [`decrypt`] or
-/// [`EncryptedBox::decrypt`].
+/// Returns the ciphertext and the nonce; decrypt with [`decrypt`] or
+/// [`EncryptedBox::decrypt`]. The plaintext is not padded, so the ciphertext
+/// length reveals the exact plaintext length.
///
/// Wire-compatible with libsodium's `crypto_secretbox_easy`.
pub fn encrypt(data: &[u8], key: &Key) -> EncryptedBox {
@@ -81,7 +75,7 @@ pub fn encrypt(data: &[u8], key: &Key) -> EncryptedBox {
}
/// Shared back end for [`encrypt`] and [`encrypt_combined`]: encrypts `data`
-/// under `key` with the given `nonce`, returning `MAC β€– ciphertext`. Both
+/// with `key` and the given `nonce`, returning `MAC β€– ciphertext`. Both
/// callers generate a fresh nonce, so a (key, nonce) pair is never reused.
fn encrypt_with_nonce(data: &[u8], nonce: &Nonce, key: &Key) -> Vec<u8> {
let cipher = XSalsa20Poly1305::new(GenericArray::from_slice(key.as_bytes()));
@@ -125,9 +119,9 @@ pub fn decrypt(data: &[u8], nonce: &Nonce, key: &Key) -> Result<Vec<u8>> {
.map_err(|_| CryptoError::DecryptionFailed)
}
-/// Encrypt `data` under `key` into one self-contained buffer.
+/// Encrypt `data` with `key` into one self-contained buffer.
///
-/// Like [`encrypt`], but prepends a fresh random nonce to the ciphertext,
+/// Like [`encrypt`], but prepends a random nonce to the ciphertext,
/// returning `nonce (24 bytes) β€– MAC (16 bytes) β€– ciphertext`: everything
/// needed to decrypt except the key. Prefer this when a single opaque blob is
/// easier to store or pass around than a separate ciphertext and nonce. Decrypt
diff --git a/rust/crates/core/src/crypto/stream.rs b/rust/crates/core/src/crypto/stream.rs
index 93ac2734f9..b6ae3a8727 100644
--- a/rust/crates/core/src/crypto/stream.rs
+++ b/rust/crates/core/src/crypto/stream.rs
@@ -67,7 +67,7 @@ pub struct Encryptor {
}
impl Encryptor {
- /// Create an encryptor under `key`, generating a fresh random header.
+ /// Create an encryptor with `key`, generating a fresh random header.
pub fn new(key: &Key) -> Self {
let (header, stream) = PushStream::init(OsRng, &upstream_key(key));
Self {
@@ -248,7 +248,7 @@ pub struct StreamingEncryptor<W: Write> {
}
impl<W: Write> StreamingEncryptor<W> {
- /// Create a streaming encryptor under `key`, writing the decryption header
+ /// Create a streaming encryptor with `key`, writing the decryption header
/// to `writer` before any ciphertext.
pub fn new(key: &Key, mut writer: W) -> Result<Self> {
let encryptor = Encryptor::new(key);
@@ -371,7 +371,7 @@ fn ensure_reader_exhausted<R: Read>(reader: &mut R) -> Result<()> {
}
impl<R: Read> StreamingDecryptor<R> {
- /// Create a streaming decryptor under `key`, reading the decryption header
+ /// Create a streaming decryptor with `key`, reading the decryption header
/// from `reader` before any ciphertext.
pub fn new(key: &Key, mut reader: R) -> Result<Self> {
let mut header = [0u8; Header::BYTES];
diff --git a/web/packages/wasm-core/index.ts b/web/packages/wasm-core/index.ts
index 1d784d052d..c68df8eca7 100644
--- a/web/packages/wasm-core/index.ts
+++ b/web/packages/wasm-core/index.ts
@@ -1,5 +1,7 @@
/**
- * @file Cryptographic primitives for Ente.
+ * @file TypeScript wrapper for the `ente-core` Rust crate.
+ *
+ * See `rust/crates/core` for full documentation.
*
* Each function loads the WebAssembly backend on first use (so all are async)
* and runs on the calling thread; run heavy operations from a Web Worker to
diff --git a/web/packages/wasm-core/secretbox.ts b/web/packages/wasm-core/secretbox.ts
index 73e5ac8f30..05963c21af 100644
--- a/web/packages/wasm-core/secretbox.ts
+++ b/web/packages/wasm-core/secretbox.ts
@@ -1,29 +1,5 @@
/**
* @file Authenticated encryption with XSalsa20-Poly1305.
- *
- * SecretBox encrypts and authenticates a single, self-contained value under a
- * 256-bit key. It suits small, independent pieces of data such as a wrapped key
- * or a short field. Ente uses blob for data attached to an object (file or
- * collection metadata and the like), and stream for file contents, which are
- * encrypted in chunks.
- *
- * The name comes from libsodium, which exposes this same construction as
- * `crypto_secretbox`; the implementation here is pure Rust but wire-compatible
- * (recorded per function below).
- *
- * Every message takes a 192-bit nonce. The nonce is not secret, but it must
- * never be reused with the same key. The {@link secretboxEncrypt} and
- * {@link secretboxEncryptCombined} functions generate a fresh random nonce for
- * you, which is the safe default.
- *
- * Two payload shapes are offered, differing only in how the nonce travels:
- *
- * - Split ({@link secretboxEncrypt} / {@link secretboxDecrypt}): the ciphertext
- * (`MAC β€– ct`) and the nonce are returned separately.
- *
- * - Combined ({@link secretboxEncryptCombined} / {@link
- * secretboxDecryptCombined}): the nonce is prepended to the ciphertext to form
- * one self-contained buffer (`nonce β€– MAC β€– ct`).
*/
import { loadWasmCore } from "./load";
import { Key, Nonce } from "./types";
@@ -31,36 +7,15 @@ import { Key, Nonce } from "./types";
/**
* A secretbox ciphertext together with the nonce needed to open it, as returned
* by {@link secretboxEncrypt}.
- *
- * This is the split shape, with the nonce held separately from the ciphertext;
- * {@link secretboxEncryptCombined} produces the combined alternative.
*/
export interface EncryptedBox {
- /**
- * The Poly1305 tag followed by the ciphertext:
- * `MAC (16 bytes) β€– ciphertext`.
- */
+ /** The secretbox ciphertext. */
encryptedData: Uint8Array;
- /**
- * The nonce that encrypted {@link encryptedData}. Needed to decrypt, and not
- * secret, but never reused with the same key.
- */
+ /** The nonce needed to decrypt it. Not secret. */
nonce: Nonce;
}
-/**
- * Encrypt `data` under `key`, generating a fresh random nonce.
- *
- * This is the default choice for secretbox encryption: a new nonce is generated
- * on every call, so a (key, nonce) pair cannot be reused by mistake. The
- * plaintext is not padded, so the ciphertext length reveals the exact plaintext
- * length.
- *
- * Returns the ciphertext and the generated nonce; decrypt with
- * {@link secretboxDecrypt}.
- *
- * Wire-compatible with libsodium's `crypto_secretbox_easy`.
- */
+/** Encrypt `data` with `key` and a freshly generated random nonce. */
export const secretboxEncrypt = async (
data: Uint8Array,
key: Key,
@@ -77,20 +32,7 @@ export const secretboxEncrypt = async (
}
};
-/**
- * Decrypt a ciphertext produced by {@link secretboxEncrypt}.
- *
- * Pass the same `nonce` and `key` that encrypted it. `data` is
- * `MAC (16 bytes) β€– ciphertext`. The Poly1305 tag is verified before any
- * plaintext is returned, so a successful result is also proof that the
- * ciphertext was not altered.
- *
- * Throws if `data` is smaller than the tag, or if the tag does not verify,
- * which is the case whenever the key, nonce, or ciphertext is wrong or the data
- * was tampered with.
- *
- * Wire-compatible with libsodium's `crypto_secretbox_open_easy`.
- */
+/** Decrypt a ciphertext produced by {@link secretboxEncrypt}. */
export const secretboxDecrypt = async (
data: Uint8Array,
nonce: Nonce,
@@ -100,19 +42,7 @@ export const secretboxDecrypt = async (
return wasm.secretboxDecrypt(data, nonce.bytes, key.bytes);
};
-/**
- * Encrypt `data` under `key` into one self-contained buffer.
- *
- * Like {@link secretboxEncrypt}, but prepends a fresh random nonce to the
- * ciphertext, returning `nonce (24 bytes) β€– MAC (16 bytes) β€– ciphertext`:
- * everything needed to decrypt except the key. Prefer this when a single opaque
- * blob is easier to store or pass around than a separate ciphertext and nonce.
- * Decrypt with {@link secretboxDecryptCombined}.
- *
- * The `MAC β€– ciphertext` body is wire-compatible with libsodium's
- * `crypto_secretbox_easy`; prepending the nonce is an Ente convention, not part
- * of libsodium itself.
- */
+/** Encrypt `data` with `key` into one self-contained buffer. */
export const secretboxEncryptCombined = async (
data: Uint8Array,
key: Key,
@@ -121,18 +51,7 @@ export const secretboxEncryptCombined = async (
return wasm.secretboxEncryptCombined(data, key.bytes);
};
-/**
- * Decrypt a combined buffer produced by {@link secretboxEncryptCombined}.
- *
- * Splits the leading nonce from the `MAC β€– ciphertext` body, then verifies the
- * tag and decrypts as {@link secretboxDecrypt} does.
- *
- * Throws if `data` is too short to hold a nonce and tag, otherwise the same as
- * {@link secretboxDecrypt}.
- *
- * The body is wire-compatible with libsodium's `crypto_secretbox_open_easy`;
- * the leading nonce is an Ente convention.
- */
+/** Decrypt a combined buffer produced by {@link secretboxEncryptCombined}. */
export const secretboxDecryptCombined = async (
data: Uint8Array,
key: Key,
diff --git a/web/packages/wasm-core/types.ts b/web/packages/wasm-core/types.ts
index d2a505e002..489b95aec9 100644
--- a/web/packages/wasm-core/types.ts
+++ b/web/packages/wasm-core/types.ts
@@ -1,9 +1,5 @@
/**
* @file Typed key material and related fixed-size values.
- *
- * These types carry their size in the type, so length validation happens once,
- * at the boundary where raw bytes enter ({@link Key.fromBytes} and friends),
- * rather than inside every operation.
*/
const sized = (bytes: Uint8Array, length: number, name: string): Uint8Array => {
@@ -15,14 +11,7 @@ const sized = (bytes: Uint8Array, length: number, name: string): Uint8Array => {
return bytes;
};
-/**
- * A 256-bit symmetric encryption key.
- *
- * One key type serves the secretbox, blob and stream operations: both algorithm
- * families take 256-bit keys, and Ente's data model shares key material across
- * them (e.g. a collection key wraps file keys via secretbox and encrypts
- * collection metadata via blob).
- */
+/** A 256-bit symmetric encryption key. */
export class Key {
private constructor(private readonly _bytes: Uint8Array) {}
@@ -79,13 +68,7 @@ export class Salt {
}
}
-/**
- * A 192-bit SecretStream decryption header. Not secret.
- *
- * While the exact contents are an implementation detail of the secretstream
- * construction, it effectively contains the random nonce generated during
- * encryption, and is required for decryption.
- */
+/** A 192-bit SecretStream decryption header. Not secret. */
export class Header {
private constructor(private readonly _bytes: Uint8Array) {}