File sharing

This commit is contained in:
2026-10-29 20:44:26 -03:00
parent b65d8efad8
commit de1ad70068
18 changed files with 582 additions and 59 deletions
+27 -15
View File
@@ -29,26 +29,36 @@ function chunk_iv(index: number, last: boolean) {
return iv;
}
async function encrypt_metadata(master_key: CryptoKey, metadata: Record<string, unknown>) {
// Encrypts with a random IV, which goes in front of the ciphertext
export async function seal(key: CryptoKey, plaintext: Uint8Array<ArrayBuffer>, aad: Uint8Array<ArrayBuffer>) {
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
const plaintext = new TextEncoder().encode(JSON.stringify(metadata));
const ciphertext = await crypto.subtle.encrypt(
{ name: "AES-GCM", iv, additionalData: METADATA_AAD },
master_key,
plaintext,
);
plaintext.fill(0);
const ciphertext = await crypto.subtle.encrypt({ name: "AES-GCM", iv, additionalData: aad }, key, plaintext);
return to_base64(concat(iv, new Uint8Array(ciphertext)));
}
// Throws if the metadata wasn't encrypted with this master key
export async function decrypt_metadata(master_key: CryptoKey, metadata: string): Promise<FileMetadata> {
const bytes = from_base64(metadata);
// Throws if it wasn't sealed with this key and aad
export async function unseal(key: CryptoKey, sealed: string, aad: Uint8Array<ArrayBuffer>) {
const bytes = from_base64(sealed);
const plaintext = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv: bytes.slice(0, IV_LENGTH), additionalData: METADATA_AAD },
master_key,
{ name: "AES-GCM", iv: bytes.slice(0, IV_LENGTH), additionalData: aad },
key,
bytes.slice(IV_LENGTH),
);
return new Uint8Array(plaintext);
}
// Throws if the metadata wasn't encrypted with this master key
export async function decrypt_metadata(master_key: CryptoKey, metadata: string) {
return await parse_metadata(await unseal(master_key, metadata, METADATA_AAD));
}
// The file metadata decrypted as is, so it can be encrypted again for a share link
export function unseal_metadata(master_key: CryptoKey, metadata: string) {
return unseal(master_key, metadata, METADATA_AAD);
}
// Throws if the plaintext isn't valid metadata
export async function parse_metadata(plaintext: Uint8Array): Promise<FileMetadata> {
const data = JSON.parse(new TextDecoder().decode(plaintext));
if (
data.version !== METADATA_VERSION || typeof data.name !== "string" || typeof data.type !== "string" ||
@@ -70,14 +80,16 @@ export async function encrypt_file(
): Promise<EncryptedFile> {
const key = await crypto.subtle.generateKey({ name: "AES-GCM", length: 256 }, true, ["encrypt"]);
const raw_key = new Uint8Array(await crypto.subtle.exportKey("raw", key));
const metadata = await encrypt_metadata(master_key, {
const plaintext = new TextEncoder().encode(JSON.stringify({
version: METADATA_VERSION,
name: file.name,
type: file.type,
size: file.size,
chunk_size: CHUNK_SIZE,
key: to_base64(raw_key),
});
}));
const metadata = await seal(master_key, plaintext, METADATA_AAD);
plaintext.fill(0);
raw_key.fill(0);
// Empty files still get one chunk, so the last chunk flag is always checked