Skip to content

armgit / ente

Public Imported from GitHub
0

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

Enforce WASM import boundary

Manav Rathi69a4ecda173c
diff --git a/web/apps/cast/src/services/pair.ts b/web/apps/cast/src/services/pair.ts
index 1fa039a243..ae6c22054f 100644
--- a/web/apps/cast/src/services/pair.ts
+++ b/web/apps/cast/src/services/pair.ts
@@ -1,7 +1,11 @@
import { ensureOk, publicRequestHeaders } from "ente-base/http";
import log from "ente-base/log";
import { apiURL } from "ente-base/origins";
-import type { CastReceiver } from "ente-cast-wasm";
+import {
+ createCastReceiver,
+ openCastPayload,
+ type CastReceiver,
+} from "ente-cast-wasm";
import { wait } from "ente-utils/promise";
import { nullToUndefined } from "ente-utils/transform";
import { z } from "zod";
@@ -12,8 +16,7 @@ export interface Registration {
}
export const register = async (): Promise<Registration> => {
- const { CastReceiver } = await import("ente-cast-wasm");
- const receiver = new CastReceiver();
+ const receiver = await createCastReceiver();
let pairingCode: string | undefined;
while (true) {
@@ -57,12 +60,7 @@ export const getCastPayload = async (
const encryptedCastData = await getEncryptedCastData(pairingCode);
if (!encryptedCastData) return undefined;
- const payload = receiver.openPayload(encryptedCastData);
- return {
- castToken: payload.castToken,
- collectionID: Number(payload.collectionID),
- collectionKey: payload.collectionKey,
- };
+ return openCastPayload(receiver, encryptedCastData);
};
const getEncryptedCastData = async (code: string) => {
diff --git a/web/apps/ensu/src/services/chat/crypto.ts b/web/apps/ensu/src/services/chat/crypto.ts
index f79a3c6c5a..58ad544e53 100644
--- a/web/apps/ensu/src/services/chat/crypto.ts
+++ b/web/apps/ensu/src/services/chat/crypto.ts
@@ -1,6 +1,6 @@
import { isTauriRuntime } from "@/services/tauri-runtime";
+import * as wasmCrypto from "ente-ensu-wasm";
-type EnsuWasmModule = typeof import("ente-ensu-wasm");
type TauriCoreModule = typeof import("@tauri-apps/api/core");
export interface EncryptedChatPayload {
@@ -8,8 +8,6 @@ export interface EncryptedChatPayload {
header: string;
}
-const ensuWasm = () => import("ente-ensu-wasm");
-
const tauriInvoke = async <T>(
...args: Parameters<TauriCoreModule["invoke"]>
) => {
@@ -17,18 +15,6 @@ const tauriInvoke = async <T>(
return invoke<T>(...args);
};
-const encryptedChatPayloadValue = (
- payload: ReturnType<EnsuWasmModule["encryptChatPayload"]>,
-) => {
- try {
- return { encryptedData: payload.encryptedData, header: payload.header };
- } finally {
- payload.free();
- }
-};
-
-const wasmBytes = (bytes: Uint8Array) => bytes as Uint8Array<ArrayBuffer>;
-
const invokeAttachmentCommand = async (
command: string,
data: Uint8Array,
@@ -44,7 +30,7 @@ const invokeAttachmentCommand = async (
export const generateChatKey = async () =>
isTauriRuntime()
? tauriInvoke<string>("chat_crypto_generate_key")
- : (await ensuWasm()).generateChatKey();
+ : wasmCrypto.generateChatKey();
export const encryptChatPayload = async (
payload: unknown,
@@ -56,9 +42,7 @@ export const encryptChatPayload = async (
input: { value, keyB64: chatKeyB64 },
});
}
- return encryptedChatPayloadValue(
- (await ensuWasm()).encryptChatPayload(value, chatKeyB64),
- );
+ return wasmCrypto.encryptChatPayload(value, chatKeyB64);
};
export const decryptChatPayload = async (
@@ -73,7 +57,7 @@ export const decryptChatPayload = async (
keyB64: chatKeyB64,
},
})
- : (await ensuWasm()).decryptChatPayload(
+ : await wasmCrypto.decryptChatPayload(
encryptedData,
header,
chatKeyB64,
@@ -89,7 +73,7 @@ export const encryptChatField = async (
? tauriInvoke("chat_crypto_encrypt_field", {
input: { value, keyB64: chatKeyB64 },
})
- : (await ensuWasm()).encryptChatField(value, chatKeyB64);
+ : wasmCrypto.encryptChatField(value, chatKeyB64);
export const decryptChatField = async (
value: string,
@@ -99,13 +83,13 @@ export const decryptChatField = async (
? tauriInvoke("chat_crypto_decrypt_field", {
input: { value, keyB64: chatKeyB64 },
})
- : (await ensuWasm()).decryptChatField(value, chatKeyB64);
+ : wasmCrypto.decryptChatField(value, chatKeyB64);
export const encryptAttachmentBytes = async (
data: Uint8Array,
chatKeyB64: string,
sessionUuid: string,
-): Promise<Uint8Array<ArrayBuffer>> =>
+): Promise<Uint8Array> =>
isTauriRuntime()
? invokeAttachmentCommand(
"chat_crypto_encrypt_attachment",
@@ -113,19 +97,13 @@ export const encryptAttachmentBytes = async (
chatKeyB64,
sessionUuid,
)
- : wasmBytes(
- (await ensuWasm()).encryptChatAttachment(
- data,
- chatKeyB64,
- sessionUuid,
- ),
- );
+ : wasmCrypto.encryptChatAttachment(data, chatKeyB64, sessionUuid);
export const decryptAttachmentBytes = async (
data: Uint8Array,
chatKeyB64: string,
sessionUuid: string,
-): Promise<Uint8Array<ArrayBuffer>> =>
+): Promise<Uint8Array> =>
isTauriRuntime()
? invokeAttachmentCommand(
"chat_crypto_decrypt_attachment",
@@ -133,10 +111,4 @@ export const decryptAttachmentBytes = async (
chatKeyB64,
sessionUuid,
)
- : wasmBytes(
- (await ensuWasm()).decryptChatAttachment(
- data,
- chatKeyB64,
- sessionUuid,
- ),
- );
+ : wasmCrypto.decryptChatAttachment(data, chatKeyB64, sessionUuid);
diff --git a/web/apps/ensu/src/services/chat/store.ts b/web/apps/ensu/src/services/chat/store.ts
index 7ee10b7328..affd39f540 100644
--- a/web/apps/ensu/src/services/chat/store.ts
+++ b/web/apps/ensu/src/services/chat/store.ts
@@ -1,5 +1,6 @@
import type { SourceCitation } from "@/services/knowledge";
import { isTauriRuntime } from "@/services/tauri-runtime";
+import { ensureArrayBufferBacked } from "ente-base/bytes";
import { getKV, removeKV, setKV } from "ente-base/kv";
import log from "ente-base/log";
import { openDB, type DBSchema, type IDBPDatabase } from "idb";
@@ -857,10 +858,7 @@ const attachmentPath = async (id: string) => {
return join(dir, id);
};
-export const writeAttachmentBytes = async (
- id: string,
- data: Uint8Array<ArrayBuffer>,
-) => {
+export const writeAttachmentBytes = async (id: string, data: Uint8Array) => {
if (isTauriRuntime()) {
const { writeFile } = await import("@tauri-apps/plugin-fs");
await writeFile(await attachmentPath(id), data);
@@ -868,7 +866,10 @@ export const writeAttachmentBytes = async (
}
const db = await chatDb();
- await db.put("attachmentBytes", { id, data });
+ await db.put("attachmentBytes", {
+ id,
+ data: ensureArrayBufferBacked(data),
+ });
};
export const deleteAttachmentBytes = async (id: string) => {
@@ -918,7 +919,7 @@ export const readDecryptedAttachmentBytes = async (
id: string,
chatKey: string,
sessionUuid: string,
-): Promise<Uint8Array<ArrayBuffer>> => {
+): Promise<Uint8Array> => {
const encrypted = await readAttachmentBytes(id);
return decryptAttachmentBytes(encrypted, chatKey, sessionUuid);
};
diff --git a/web/apps/locker/src/services/crypto.ts b/web/apps/locker/src/services/crypto.ts
deleted file mode 100644
index ce76a93c7c..0000000000
--- a/web/apps/locker/src/services/crypto.ts
+++ /dev/null
@@ -1,239 +0,0 @@
-import { ensureArrayBufferBacked } from "ente-base/bytes";
-import type {
- EncryptedBlob,
- EncryptedBox,
- EncryptedFile,
- KeyPair,
-} from "ente-base/crypto/types";
-import { isNamedError } from "ente-base/error";
-
-const shouldFallbackToLegacyBlobDecrypt = (error: unknown): boolean =>
- isNamedError(error, "stream_truncated");
-
-const toB64String = (v: Uint8Array | string): string => {
- if (typeof v === "string") return v;
- let binary = "";
- for (const byte of v) {
- binary += String.fromCharCode(byte);
- }
- return btoa(binary);
-};
-
-const fromB64String = (b64: string): Uint8Array<ArrayBuffer> => {
- const binary = atob(b64);
- const bytes = new Uint8Array(binary.length);
- for (let i = 0; i < binary.length; i++) {
- bytes[i] = binary.charCodeAt(i);
- }
- return bytes;
-};
-
-export const decryptBox = async (
- box: EncryptedBox,
- key: Uint8Array | string,
-): Promise<string> => {
- const wasm = await import("ente-core-wasm");
- return wasm.crypto_decrypt_box(
- toB64String(box.encryptedData),
- toB64String(box.nonce),
- toB64String(key),
- );
-};
-
-export const decryptBoxBytes = async (
- box: EncryptedBox,
- key: Uint8Array | string,
-): Promise<Uint8Array> => {
- const b64 = await decryptBox(box, key);
- return fromB64String(b64);
-};
-
-export const decryptMetadataJSON = async (
- blob: EncryptedBlob,
- key: Uint8Array | string,
-): Promise<unknown> => {
- const wasm = await import("ente-core-wasm");
- const encryptedData = toB64String(blob.encryptedData);
- const decryptionHeader = toB64String(blob.decryptionHeader);
- const keyB64 = toB64String(key);
- let plaintextB64: string;
- try {
- plaintextB64 = wasm.crypto_decrypt_blob(
- encryptedData,
- decryptionHeader,
- keyB64,
- );
- } catch (error) {
- if (!shouldFallbackToLegacyBlobDecrypt(error)) {
- throw error;
- }
- plaintextB64 = wasm.crypto_decrypt_blob_legacy(
- encryptedData,
- decryptionHeader,
- keyB64,
- );
- }
- return JSON.parse(new TextDecoder().decode(fromB64String(plaintextB64)));
-};
-
-export const boxSealOpen = async (
- encryptedData: string,
- keyPair: KeyPair,
-): Promise<string> => {
- const wasm = await import("ente-core-wasm");
- return wasm.crypto_box_seal_open(
- encryptedData,
- keyPair.publicKey,
- keyPair.privateKey,
- );
-};
-
-export const boxSeal = async (
- dataB64: string,
- publicKeyB64: string,
-): Promise<string> => {
- const wasm = await import("ente-core-wasm");
- return wasm.crypto_box_seal(dataB64, publicKeyB64);
-};
-
-export const generateKey = async (): Promise<string> => {
- const wasm = await import("ente-core-wasm");
- return wasm.crypto_generate_key();
-};
-
-export const md5Base64 = async (data: Uint8Array): Promise<string> => {
- const wasm = await import("ente-core-wasm");
- return wasm.crypto_md5_base64(data);
-};
-
-export const encryptBox = async (
- dataB64: string,
- keyB64: string,
-): Promise<{ encryptedData: string; nonce: string }> => {
- const wasm = await import("ente-core-wasm");
- const result = wasm.crypto_encrypt_box(dataB64, keyB64);
- return { encryptedData: result.encrypted_data, nonce: result.nonce };
-};
-
-export const encryptBlob = async (
- dataB64: string,
- keyB64: string,
-): Promise<{ encryptedData: string; decryptionHeader: string }> => {
- const wasm = await import("ente-core-wasm");
- const result = wasm.crypto_encrypt_blob(dataB64, keyB64);
- return {
- encryptedData: result.encrypted_data,
- decryptionHeader: result.decryption_header,
- };
-};
-
-export const stringToB64 = (s: string): string => {
- const encoder = new TextEncoder();
- const bytes = encoder.encode(s);
- return toB64String(bytes);
-};
-
-export { fromB64String, toB64String };
-
-export interface StreamEncryptorHandle {
- key: string;
- decryptionHeader: string;
- encryptChunk: (
- data: Uint8Array,
- isFinal: boolean,
- ) => Promise<Uint8Array<ArrayBuffer>>;
- free: () => void;
-}
-
-export interface StreamDecryptorHandle {
- decryptionChunkSize: number;
- decryptChunk: (data: Uint8Array) => Promise<Uint8Array>;
- isFinalized: () => boolean;
- free: () => void;
-}
-
-export const createStreamEncryptor =
- async (): Promise<StreamEncryptorHandle> => {
- const wasm = await import("ente-core-wasm");
- const encryptor = new wasm.CryptoStreamEncryptor();
-
- return {
- key: encryptor.key,
- decryptionHeader: encryptor.decryption_header,
- encryptChunk: (data: Uint8Array, isFinal: boolean) =>
- Promise.resolve(
- ensureArrayBufferBacked(
- encryptor.encrypt_chunk(data, isFinal),
- ),
- ),
- free: () => encryptor.free(),
- };
- };
-
-export const createStreamDecryptor = async (
- decryptionHeader: string,
- keyB64: string,
-): Promise<StreamDecryptorHandle> => {
- const wasm = await import("ente-core-wasm");
- const decryptor = new wasm.CryptoStreamDecryptor(decryptionHeader, keyB64);
-
- return {
- decryptionChunkSize: decryptor.decryption_chunk_size,
- decryptChunk: (data: Uint8Array) =>
- Promise.resolve(decryptor.decrypt_chunk(data)),
- isFinalized: () => decryptor.is_finalized,
- free: () => decryptor.free(),
- };
-};
-
-export interface EncryptedFileResult {
- encryptedData: string;
- decryptionHeader: string;
- md5Hash: string;
- key: string;
-}
-
-export const encryptFileStream = async (
- dataB64: string,
-): Promise<EncryptedFileResult> => {
- const wasm = await import("ente-core-wasm");
- const result = wasm.crypto_encrypt_stream(dataB64);
- return {
- encryptedData: result.encrypted_data,
- decryptionHeader: result.decryption_header,
- md5Hash: result.md5_hash,
- key: result.key,
- };
-};
-
-export const encryptFileStreamWithKey = async (
- dataB64: string,
- keyB64: string,
-): Promise<EncryptedFileResult> => {
- const wasm = await import("ente-core-wasm");
- const result = wasm.crypto_encrypt_stream_with_key(dataB64, keyB64);
- return {
- encryptedData: result.encrypted_data,
- decryptionHeader: result.decryption_header,
- md5Hash: result.md5_hash,
- key: result.key,
- };
-};
-
-export const bytesToB64 = (bytes: Uint8Array): string => toB64String(bytes);
-
-export const b64ToBytes = (b64: string): Uint8Array<ArrayBuffer> =>
- fromB64String(b64);
-
-export const decryptStreamBytes = async (
- file: EncryptedFile,
- key: Uint8Array | string,
-): Promise<Uint8Array> => {
- const wasm = await import("ente-core-wasm");
- const plaintextB64 = wasm.crypto_decrypt_stream(
- toB64String(file.encryptedData),
- toB64String(file.decryptionHeader),
- toB64String(key),
- );
- return fromB64String(plaintextB64);
-};
diff --git a/web/apps/locker/src/services/remote-collection-sharing.ts b/web/apps/locker/src/services/remote-collection-sharing.ts
index 631281b374..7b69c00f6c 100644
--- a/web/apps/locker/src/services/remote-collection-sharing.ts
+++ b/web/apps/locker/src/services/remote-collection-sharing.ts
@@ -2,8 +2,8 @@ import type { LockerCollectionParticipant } from "@/types";
import { getPublicKey } from "ente-accounts/services/user";
import { authenticatedRequestHeaders, ensureOk } from "ente-base/http";
import { apiURL } from "ente-base/origins";
+import { boxSeal } from "ente-core-wasm";
import { z } from "zod";
-import { boxSeal } from "./crypto";
import {
RemoteCollectionUserSchema,
toLockerCollectionParticipant,
diff --git a/web/apps/locker/src/services/remote-collections.ts b/web/apps/locker/src/services/remote-collections.ts
index 00a5a5bb5b..b607c0db99 100644
--- a/web/apps/locker/src/services/remote-collections.ts
+++ b/web/apps/locker/src/services/remote-collections.ts
@@ -1,6 +1,6 @@
import { authenticatedRequestHeaders, ensureOk } from "ente-base/http";
import { apiURL } from "ente-base/origins";
-import { encryptBox, generateKey, stringToB64 } from "./crypto";
+import { encryptBox, generateKey, stringToB64 } from "ente-core-wasm";
import { RemoteCollectionCreateResponseSchema } from "./remote-types";
interface CollectionRecordLike {
diff --git a/web/apps/locker/src/services/remote-read.ts b/web/apps/locker/src/services/remote-read.ts
index 8bc800d4a7..0c54960e2e 100644
--- a/web/apps/locker/src/services/remote-read.ts
+++ b/web/apps/locker/src/services/remote-read.ts
@@ -11,7 +11,6 @@ import { fetchFile } from "ente-base/file-download";
import { authenticatedRequestHeaders, ensureOk } from "ente-base/http";
import log from "ente-base/log";
import { apiURL, customAPIOrigin } from "ente-base/origins";
-import { z } from "zod";
import {
boxSealOpen,
createStreamDecryptor,
@@ -20,7 +19,8 @@ import {
decryptMetadataJSON,
encryptBox,
stringToB64,
-} from "./crypto";
+} from "ente-core-wasm";
+import { z } from "zod";
import { fromInfoTypeWireValue } from "./info-type-wire";
import {
type StoredTrashFileRecord,
diff --git a/web/apps/locker/src/services/remote-uploads.ts b/web/apps/locker/src/services/remote-uploads.ts
index afae899abe..f25a181923 100644
--- a/web/apps/locker/src/services/remote-uploads.ts
+++ b/web/apps/locker/src/services/remote-uploads.ts
@@ -5,7 +5,6 @@ import {
retryEnsuringHTTPOk,
} from "ente-base/http";
import { apiURL } from "ente-base/origins";
-import { z } from "zod";
import {
b64ToBytes,
createStreamEncryptor,
@@ -14,7 +13,8 @@ import {
encryptFileStreamWithKey,
md5Base64,
stringToB64,
-} from "./crypto";
+} from "ente-core-wasm";
+import { z } from "zod";
import {
RemoteIDResponseSchema,
RemoteUploadURLResponseSchema,
@@ -309,7 +309,7 @@ export const uploadLockerFileWithDeps = async <TCollectionRecord>(
if (plaintextChunkCount >= MULTIPART_CHUNKS_PER_PART) {
const parts: Uint8Array<ArrayBuffer>[] = [];
const partMd5s: string[] = [];
- let pendingEncryptedChunks: Uint8Array<ArrayBuffer>[] = [];
+ let pendingEncryptedChunks: Uint8Array[] = [];
for (
let chunkIndex = 0;
@@ -383,7 +383,7 @@ export const uploadLockerFileWithDeps = async <TCollectionRecord>(
);
encryptedFileObjectKey = multipartUpload.objectKey;
} else {
- const encryptedChunks: Uint8Array<ArrayBuffer>[] = [];
+ const encryptedChunks: Uint8Array[] = [];
for (
let chunkIndex = 0;
diff --git a/web/apps/locker/src/services/remote.ts b/web/apps/locker/src/services/remote.ts
index e038daf29a..0d9770c9f6 100644
--- a/web/apps/locker/src/services/remote.ts
+++ b/web/apps/locker/src/services/remote.ts
@@ -12,7 +12,6 @@ import {
import { authenticatedRequestHeaders, ensureOk } from "ente-base/http";
import log from "ente-base/log";
import { apiURL } from "ente-base/origins";
-import { z } from "zod";
import {
b64ToBytes,
boxSeal,
@@ -23,7 +22,8 @@ import {
encryptBox,
generateKey,
stringToB64,
-} from "./crypto";
+} from "ente-core-wasm";
+import { z } from "zod";
import {
clearLockerCache,
findCollectionByType,
diff --git a/web/apps/photos/src/services/cast.ts b/web/apps/photos/src/services/cast.ts
index 96aede4a38..462085cd46 100644
--- a/web/apps/photos/src/services/cast.ts
+++ b/web/apps/photos/src/services/cast.ts
@@ -1,6 +1,7 @@
import { namedError } from "ente-base/error";
import { authenticatedRequestHeaders, ensureOk } from "ente-base/http";
import { apiURL } from "ente-base/origins";
+import { prepareCastPayload } from "ente-cast-wasm";
import type { Collection } from "ente-media/collection";
import { z } from "zod";
@@ -32,11 +33,10 @@ export const publishCastPayload = async (
throw namedError("cast_device_not_found", "Unknown device code");
}
- const { preparePayload } = await import("ente-cast-wasm");
- const { castToken, encryptedPayload } = preparePayload(
+ const { castToken, encryptedPayload } = await prepareCastPayload(
publicKeys.publicKey,
publicKeys.pqPublicKey,
- BigInt(collection.id),
+ collection.id,
collection.key,
);
const res = await fetch(await apiURL("/cast/cast-data"), {
diff --git a/web/apps/space/src/services/bootstrap-auth.ts b/web/apps/space/src/services/bootstrap-auth.ts
index b34a7907f6..d9885572de 100644
--- a/web/apps/space/src/services/bootstrap-auth.ts
+++ b/web/apps/space/src/services/bootstrap-auth.ts
@@ -7,6 +7,7 @@ import type { SRPSetupAttributes } from "ente-accounts/services/srp";
import type { KeyAttributes } from "ente-accounts/services/user";
import { ensureOk, publicRequestHeaders } from "ente-base/http";
import { apiURL } from "ente-base/origins";
+import { createSRPSession } from "ente-core-wasm";
import { z } from "zod";
export const spaceBootstrapAuthHeaders = (authToken: string) => ({
@@ -54,8 +55,7 @@ export const setupSpaceSignupSRP = async (
{ srpSalt, srpUserID, srpVerifier, loginSubKey }: SRPSetupAttributes,
authToken: string,
) => {
- const wasm = await import("ente-core-wasm");
- const session = new wasm.SrpSession(srpUserID, srpSalt, loginSubKey);
+ const session = await createSRPSession(srpSalt, srpUserID, loginSubKey);
const setupRes = await fetch(await apiURL("/users/srp/setup"), {
method: "POST",
diff --git a/web/apps/space/src/services/persistent-session.ts b/web/apps/space/src/services/persistent-session.ts
index 196630c8ea..78dd30bb6b 100644
--- a/web/apps/space/src/services/persistent-session.ts
+++ b/web/apps/space/src/services/persistent-session.ts
@@ -14,6 +14,10 @@ import { ensureOk, publicRequestHeaders } from "ente-base/http";
import log from "ente-base/log";
import { apiURL } from "ente-base/origins";
import { removeAuthToken } from "ente-base/token";
+import {
+ decryptSpaceRootEntityKey,
+ encryptSpaceRootEntityKey,
+} from "ente-space-wasm";
import { spaceBootstrapAuthHeaders } from "services/bootstrap-auth";
import {
clearSpaceSecureSessionStorage,
@@ -259,10 +263,8 @@ export const getOrCreateSpaceRootKey = async (
masterKey: string,
authToken: string,
) => {
- const { decryptSpaceRootEntityKey, encryptSpaceRootEntityKey } =
- await import("ente-space-wasm");
const candidate = await generateKey();
- const encryptedKey = encryptSpaceRootEntityKey(candidate, masterKey);
+ const encryptedKey = await encryptSpaceRootEntityKey(candidate, masterKey);
const splitKey = await splitSpaceEntityKey(encryptedKey);
const res = await fetch(await apiURL("/user-entity/key/ensure"), {
method: "POST",
@@ -274,7 +276,7 @@ export const getOrCreateSpaceRootKey = async (
});
ensureOk(res);
const ensured = SpaceEntityKeyResponse.parse(await res.json());
- return decryptSpaceRootEntityKey(
+ return await decryptSpaceRootEntityKey(
await combineSpaceEntityKey(ensured),
masterKey,
);
diff --git a/web/apps/space/src/services/profile.ts b/web/apps/space/src/services/profile.ts
index d64ff68cc7..dbc468d632 100644
--- a/web/apps/space/src/services/profile.ts
+++ b/web/apps/space/src/services/profile.ts
@@ -2,7 +2,10 @@ import { savedPartialLocalUser } from "ente-accounts/services/accounts-db";
import { clientPackageName, desktopAppVersion, isDesktop } from "ente-base/app";
import { isNamedError } from "ente-base/error";
import { apiOrigin, apiURL } from "ente-base/origins";
-import type { SpaceAccountCtxHandle } from "ente-space-wasm";
+import {
+ openSpaceAccountContext,
+ type SpaceAccountCtxHandle,
+} from "ente-space-wasm";
import type {
SetupProfile,
SetupProfileInput,
@@ -101,14 +104,7 @@ const defaultOwnedSpace = (spaces: OwnedSpace[]) => spaces[0];
const currentSpaceContextConfig = async () => {
const sessionRestore = restoreSpaceBrowserSessionIfNeeded();
- const spaceWasm = savedSpaceSessionToken()
- ? import("ente-space-wasm")
- : undefined;
- const [, baseUrl, prefetchedSpaceWasm] = await Promise.all([
- sessionRestore,
- apiOrigin(),
- spaceWasm,
- ]);
+ const [, baseUrl] = await Promise.all([sessionRestore, apiOrigin()]);
const spaceRootKeyB64 = spaceRootKeyFromSpaceSession();
const user = savedPartialLocalUser();
const spaceSessionToken = savedSpaceSessionToken();
@@ -116,9 +112,6 @@ const currentSpaceContextConfig = async () => {
if (!spaceRootKeyB64 || !user?.id || !spaceSessionToken) {
return undefined;
}
- const { spaceOpenAccountCtx } =
- prefetchedSpaceWasm ?? (await import("ente-space-wasm"));
-
return {
cacheKey: [user.id, baseUrl, spaceSessionToken].join(":"),
input: {
@@ -129,7 +122,6 @@ const currentSpaceContextConfig = async () => {
spaceRootKeyB64,
spaceSessionToken,
},
- spaceOpenAccountCtx,
};
};
@@ -161,7 +153,7 @@ export const openCurrentSpaceContext = async () => {
const config = await currentSpaceContextConfig();
if (!config) return undefined;
- return await config.spaceOpenAccountCtx(config.input);
+ return await openSpaceAccountContext(config.input);
};
export const clearCurrentSpaceContext = () => {
@@ -193,8 +185,7 @@ export const ensureCurrentSpaceContext = async () => {
}
const generation = currentSpaceContextGeneration;
- const promise = config
- .spaceOpenAccountCtx(config.input)
+ const promise = openSpaceAccountContext(config.input)
.then((ctx) => {
if (
currentSpaceContextGeneration != generation ||
diff --git a/web/apps/space/src/services/space.ts b/web/apps/space/src/services/space.ts
index 0169dea155..08d6b2f178 100644
--- a/web/apps/space/src/services/space.ts
+++ b/web/apps/space/src/services/space.ts
@@ -3,9 +3,10 @@ import { clientPackageName, desktopAppVersion, isDesktop } from "ente-base/app";
import { isNamedError } from "ente-base/error";
import log from "ente-base/log";
import { apiOrigin } from "ente-base/origins";
-import type {
- SpaceAccountCtxHandle,
- SpaceLinkCtxHandle,
+import {
+ openSpaceLinkContext,
+ type SpaceAccountCtxHandle,
+ type SpaceLinkCtxHandle,
} from "ente-space-wasm";
import type { PendingSpaceInvite } from "services/invite";
import {
@@ -723,13 +724,9 @@ export const openPublicSpaceLink = async (
spaceUsername: string,
accessKey: string,
): Promise<PublicSpaceLinkSession> => {
- const [{ spaceOpenLinkCtx }, baseUrl] = await Promise.all([
- import("ente-space-wasm"),
- apiOrigin(),
- ]);
- const ctx = await spaceOpenLinkCtx({
+ const ctx = await openSpaceLinkContext({
accessKey,
- baseUrl,
+ baseUrl: await apiOrigin(),
clientPackage: clientPackageName,
clientVersion: isDesktop ? desktopAppVersion : undefined,
spaceUsername,
diff --git a/web/package.json b/web/package.json
index 9f7ce9c8ec..3bee38bffc 100644
--- a/web/package.json
+++ b/web/package.json
@@ -33,6 +33,7 @@
"build:space-wasm": "npm run build --workspace ente-space-wasm",
"build:twoof3": "npm exec --workspace twoof3 -- next build --webpack",
"build:wasm": "npm run build:core-wasm && npm run build:cast-wasm && npm run build:ensu-wasm && npm run build:legacy-wasm && npm run build:locker-wasm && npm run build:paste-wasm && npm run build:photos-wasm && npm run build:space-wasm",
+ "check:wasm-imports": "eslint --config scripts/wasm-imports/eslint.config.mjs --parser @typescript-eslint/parser --no-inline-config --max-warnings 0 apps packages && node scripts/wasm-imports/entrypoints.mjs && node --test scripts/wasm-imports/test.mjs",
"dev": "npm run dev:photos",
"dev:accounts": "npm run build:core-wasm && npm exec --workspace accounts -- next dev --webpack -p 3001",
"dev:albums": "npm run build:core-wasm && npm exec --workspace albums -- next dev --webpack -p 3002",
@@ -49,7 +50,7 @@
"dev:share": "npm run build:core-wasm && npm exec --workspace share -- next dev --webpack -p 3005",
"dev:space": "npm run build:core-wasm && npm run build:space-wasm && npm exec --workspace space -- next dev --webpack -p 3013",
"dev:twoof3": "npm exec --workspace twoof3 -- next dev --webpack -p 3012",
- "lint": "concurrently --names 'prettier,eslint,tsc' \"prettier --check --log-level warn .\" \"npm exec --workspaces -- eslint --max-warnings 0\" \"npm exec --workspaces -- tsc\"",
+ "lint": "concurrently --names 'prettier,eslint,tsc,wasm-imports' \"prettier --check --log-level warn .\" \"npm exec --workspaces -- eslint --max-warnings 0\" \"npm exec --workspaces -- tsc\" \"npm run check:wasm-imports\"",
"lint:fix": "concurrently --names 'prettier,eslint,tsc' \"prettier --write --log-level warn .\" \"npm exec --workspaces -- eslint --fix --max-warnings 0\" \"npm exec --workspaces -- tsc\"",
"test": "npm run test --workspace ente-core-wasm && npm run test --workspace ente-paste-wasm && npm run test --workspace ente-contacts && npm run test --workspace locker && npm run test --workspace photos"
},
diff --git a/web/packages/accounts/services/crypto/index.ts b/web/packages/accounts/services/crypto/index.ts
index 233463e7ee..69bc497245 100644
--- a/web/packages/accounts/services/crypto/index.ts
+++ b/web/packages/accounts/services/crypto/index.ts
@@ -4,25 +4,20 @@ import { ComlinkWorker } from "ente-base/worker/comlink-worker";
import type { DerivedKey } from "./kdf";
import * as kdf from "./kdf";
import type { KDFWorker } from "./kdf.worker";
+export {
+ boxSealOpenBytes,
+ decryptBox,
+ deriveSubKeyBytes,
+ encryptBox,
+ generateKey,
+ generateKeyPair,
+ generateSRPSetup,
+ recoveryKeyFromMnemonicOrHex,
+} from "ente-core-wasm";
+export type { EncryptedBoxB64 as EncryptedBox, KeyPair } from "ente-core-wasm";
export type { DerivedKey } from "./kdf";
-export interface EncryptedBox {
- encryptedData: string;
- nonce: string;
-}
-
-export interface KeyPair {
- publicKey: string;
- privateKey: string;
-}
-
-interface WasmGeneratedSRPSetup {
- srp_salt: string;
- srp_verifier: string;
- login_sub_key: string;
-}
-
const b64ToBinary = (b64: string) => atob(b64);
export const fromB64 = (b64String: string): Promise<Uint8Array> => {
@@ -84,88 +79,3 @@ export const deriveInteractiveKey = (password: string): Promise<DerivedKey> =>
inWorker()
? kdf.deriveInteractiveKey(password)
: withKDFWorker((worker) => worker.deriveInteractiveKey(password));
-
-export const generateKey = async (): Promise<string> => {
- const wasm = await import("ente-core-wasm");
- return wasm.crypto_generate_key();
-};
-
-export const generateKeyPair = async (): Promise<KeyPair> => {
- const wasm = await import("ente-core-wasm");
- const keyPair = wasm.crypto_generate_keypair();
- return { publicKey: keyPair.public_key, privateKey: keyPair.secret_key };
-};
-
-export const encryptBox = async (
- dataB64: string,
- keyB64: string,
-): Promise<EncryptedBox> => {
- const wasm = await import("ente-core-wasm");
- const box = wasm.crypto_encrypt_box(dataB64, keyB64);
- return { encryptedData: box.encrypted_data, nonce: box.nonce };
-};
-
-export const decryptBox = async (
- box: EncryptedBox,
- keyB64: string,
-): Promise<string> => {
- const wasm = await import("ente-core-wasm");
- return wasm.crypto_decrypt_box(box.encryptedData, box.nonce, keyB64);
-};
-
-export const boxSealOpenBytes = async (
- encryptedData: string,
- keyPair: KeyPair,
-): Promise<Uint8Array> => {
- const wasm = await import("ente-core-wasm");
- return fromB64(
- wasm.crypto_box_seal_open(
- encryptedData,
- keyPair.publicKey,
- keyPair.privateKey,
- ),
- );
-};
-
-export const deriveSubKeyBytes = async (
- keyB64: string,
- subKeyLength: number,
- subKeyID: number,
- context: string,
-) => {
- const wasm = await import("ente-core-wasm");
- return fromB64(
- wasm.crypto_derive_subkey(
- keyB64,
- subKeyLength,
- BigInt(subKeyID),
- context,
- ),
- );
-};
-
-export const generateSRPSetupAttributesRust = async (
- kekB64: string,
- srpUserID: string,
-) => {
- const wasm = await import("ente-core-wasm");
- const setup = wasm.auth_generate_srp_setup(
- kekB64,
- srpUserID,
- ) as WasmGeneratedSRPSetup;
- return {
- srpSalt: setup.srp_salt,
- srpVerifier: setup.srp_verifier,
- loginSubKey: setup.login_sub_key,
- };
-};
-
-export const recoveryKeyFromMnemonicOrHex = async (value: string) => {
- const wasm = await import("ente-core-wasm");
- return wasm.auth_recovery_key_from_mnemonic_or_hex(value);
-};
-
-export const recoveryKeyToMnemonicRust = async (recoveryKey: string) => {
- const wasm = await import("ente-core-wasm");
- return wasm.auth_recovery_key_to_mnemonic(recoveryKey);
-};
diff --git a/web/packages/accounts/services/crypto/kdf.ts b/web/packages/accounts/services/crypto/kdf.ts
index 7a3d878825..af4bbc395a 100644
--- a/web/packages/accounts/services/crypto/kdf.ts
+++ b/web/packages/accounts/services/crypto/kdf.ts
@@ -1,44 +1,6 @@
-export interface DerivedKey {
- key: string;
- salt: string;
- opsLimit: number;
- memLimit: number;
-}
-
-interface WasmDerivedKey {
- key: string;
- salt: string;
- ops_limit: number;
- mem_limit: number;
-}
-
-export const deriveKey = async (
- password: string,
- saltB64: string,
- opsLimit: number,
- memLimit: number,
-) => {
- const wasm = await import("ente-core-wasm");
- return wasm.auth_derive_kek(password, saltB64, memLimit, opsLimit);
-};
-
-const normalizeDerivedKey = (result: WasmDerivedKey): DerivedKey => ({
- key: result.key,
- salt: result.salt,
- opsLimit: result.ops_limit,
- memLimit: result.mem_limit,
-});
-
-export const deriveSensitiveKey = async (
- password: string,
-): Promise<DerivedKey> => {
- const wasm = await import("ente-core-wasm");
- return normalizeDerivedKey(wasm.auth_generate_sensitive_kek(password));
-};
-
-export const deriveInteractiveKey = async (
- password: string,
-): Promise<DerivedKey> => {
- const wasm = await import("ente-core-wasm");
- return normalizeDerivedKey(wasm.auth_generate_interactive_kek(password));
-};
+export {
+ deriveInteractiveKey,
+ deriveKey,
+ deriveSensitiveKey,
+} from "ente-core-wasm";
+export type { DerivedKey } from "ente-core-wasm";
diff --git a/web/packages/accounts/services/recovery-key.ts b/web/packages/accounts/services/recovery-key.ts
index aec21ba94f..da46189002 100644
--- a/web/packages/accounts/services/recovery-key.ts
+++ b/web/packages/accounts/services/recovery-key.ts
@@ -4,20 +4,18 @@ import {
encryptBox,
generateKey,
recoveryKeyFromMnemonicOrHex,
- recoveryKeyToMnemonicRust,
} from "ente-accounts/services/crypto";
import { ensureMasterKeyFromSession } from "ente-accounts/services/session-storage";
import { saveKeyAttributes } from "./accounts-db";
import { putUserRecoveryKeyAttributes, type KeyAttributes } from "./user";
+export { recoveryKeyToMnemonic } from "ente-core-wasm";
+
// For legacy compatibility, the hex representation of the recovery key is
// accepted in addition to the 24 word BIP-39 mnemonic.
export const recoveryKeyFromMnemonic = (recoveryKeyMnemonicOrHex: string) =>
recoveryKeyFromMnemonicOrHex(recoveryKeyMnemonicOrHex);
-export const recoveryKeyToMnemonic = async (recoveryKey: string) =>
- recoveryKeyToMnemonicRust(recoveryKey);
-
export const getUserRecoveryKey = async () => {
const masterKey = await ensureMasterKeyFromSession();
diff --git a/web/packages/accounts/services/srp.ts b/web/packages/accounts/services/srp.ts
index e576133b01..c8cec611e7 100644
--- a/web/packages/accounts/services/srp.ts
+++ b/web/packages/accounts/services/srp.ts
@@ -1,6 +1,6 @@
import {
deriveSubKeyBytes,
- generateSRPSetupAttributesRust,
+ generateSRPSetup,
toB64,
} from "ente-accounts/services/crypto";
import { namedError } from "ente-base/error";
@@ -10,6 +10,7 @@ import {
publicRequestHeaders,
} from "ente-base/http";
import { apiURL } from "ente-base/origins";
+import { createSRPSession } from "ente-core-wasm";
import { ensure } from "ente-utils/ensure";
import { v4 as uuidv4 } from "uuid";
import { z } from "zod";
@@ -62,10 +63,7 @@ export const generateSRPSetupAttributes = async (
kek: string,
): Promise<SRPSetupAttributes> => {
const srpUserID = uuidv4();
- return {
- srpUserID,
- ...(await generateSRPSetupAttributesRust(kek, srpUserID)),
- };
+ return { srpUserID, ...(await generateSRPSetup(kek, srpUserID)) };
};
export const setupSRP = async (srpSetupAttributes: SRPSetupAttributes) =>
@@ -100,15 +98,6 @@ const srpSetupOrReconfigure = async (
session.verify_m2(srpM2);
};
-const createSRPSession = async (
- srpSalt: string,
- srpUserID: string,
- loginSubKey: string,
-) => {
- const wasm = await import("ente-core-wasm");
- return new wasm.SrpSession(srpUserID, srpSalt, loginSubKey);
-};
-
interface SetupSRPRequest {
srpUserID: string;
srpSalt: string;
diff --git a/web/packages/contacts/wasm.test.ts b/web/packages/contacts/wasm.test.ts
index 37bade7cea..0052c24019 100644
--- a/web/packages/contacts/wasm.test.ts
+++ b/web/packages/contacts/wasm.test.ts
@@ -1,9 +1,9 @@
import {
- crypto_box_seal_open,
- crypto_encrypt_blob,
- crypto_encrypt_box,
- crypto_generate_key,
- crypto_generate_keypair,
+ boxSealOpen,
+ encryptBlob,
+ encryptBox,
+ generateKey,
+ generateKeyPair,
} from "ente-core-wasm";
import * as legacy from "ente-legacy-wasm/authenticated";
import * as locker from "ente-locker-wasm";
@@ -18,7 +18,7 @@ for (const [name, api] of [
] as const) {
describe(name, () => {
test("returns decrypted contacts and picture bytes as plain values", async () => {
- const fixture = encryptedContact();
+ const fixture = await encryptedContact();
mockFetch((request) => {
switch (new URL(request.url).pathname) {
case "/contacts/diff":
@@ -94,7 +94,7 @@ for (const [name, api] of [
const session = await api.openSession({
baseUrl: "http://localhost",
authToken: "test-token",
- masterKeyB64: crypto_generate_key(),
+ masterKeyB64: await generateKey(),
});
try {
expect(
@@ -177,7 +177,7 @@ describe("Legacy", () => {
const session = await legacy.openSession({
baseUrl: "http://localhost",
authToken: "token",
- masterKeyB64: crypto_generate_key(),
+ masterKeyB64: await generateKey(),
});
try {
expect(await legacy.getInfo(session)).toStrictEqual(info);
@@ -199,10 +199,10 @@ describe("Legacy", () => {
});
test("uses the typed key attributes to share a decryptable recovery key", async () => {
- const masterKey = crypto_generate_key();
- const recoveryKey = crypto_generate_key();
- const encryptedRecoveryKey = crypto_encrypt_box(recoveryKey, masterKey);
- const recipient = crypto_generate_keypair();
+ const masterKey = await generateKey();
+ const recoveryKey = await generateKey();
+ const encryptedRecoveryKey = await encryptBox(recoveryKey, masterKey);
+ const recipient = await generateKeyPair();
const keyAttributes = {
kekSalt: "",
encryptedKey: "",
@@ -213,14 +213,14 @@ describe("Legacy", () => {
memLimit: 0,
opsLimit: 0,
recoveryKeyEncryptedWithMasterKey:
- encryptedRecoveryKey.encrypted_data,
+ encryptedRecoveryKey.encryptedData,
recoveryKeyDecryptionNonce: encryptedRecoveryKey.nonce,
};
let sharedRecoveryKey: string | undefined;
mockFetch(async (request) => {
switch (new URL(request.url).pathname) {
case "/users/public-key":
- return Response.json({ publicKey: recipient.public_key });
+ return Response.json({ publicKey: recipient.publicKey });
case "/emergency-contacts/add": {
const body = (await request.json()) as {
email: string;
@@ -229,10 +229,9 @@ describe("Legacy", () => {
};
expect(body.email).toBe("[email protected]");
expect(body.recoveryNoticeInDays).toBe(30);
- sharedRecoveryKey = crypto_box_seal_open(
+ sharedRecoveryKey = await boxSealOpen(
body.encryptedKey,
- recipient.public_key,
- recipient.secret_key,
+ recipient,
);
return new Response(null, { status: 204 });
}
@@ -255,8 +254,6 @@ describe("Legacy", () => {
expect(sharedRecoveryKey).toBe(recoveryKey);
} finally {
session.free();
- encryptedRecoveryKey.free();
- recipient.free();
}
});
});
@@ -271,27 +268,27 @@ const mockFetch = (
return response;
});
-const encryptedContact = () => {
- const masterKey = crypto_generate_key();
- const rootKey = crypto_generate_key();
- const contactKey = crypto_generate_key();
- const wrappedRootKey = crypto_encrypt_box(rootKey, masterKey);
- const wrappedContactKey = crypto_encrypt_box(contactKey, rootKey);
- const data = crypto_encrypt_blob(
+const encryptedContact = async () => {
+ const masterKey = await generateKey();
+ const rootKey = await generateKey();
+ const contactKey = await generateKey();
+ const wrappedRootKey = await encryptBox(rootKey, masterKey);
+ const wrappedContactKey = await encryptBox(contactKey, rootKey);
+ const data = await encryptBlob(
Buffer.from(
JSON.stringify({ contactUserId: 42, name: "ZoΓ« πŸ¦‹" }),
).toString("base64"),
contactKey,
);
const picture = Uint8Array.from({ length: 4096 }, (_, i) => i % 256);
- const encryptedPicture = crypto_encrypt_blob(
+ const encryptedPicture = await encryptBlob(
Buffer.from(picture).toString("base64"),
contactKey,
);
const fixture = {
masterKey,
wrappedRootContactKey: {
- encryptedKey: wrappedRootKey.encrypted_data,
+ encryptedKey: wrappedRootKey.encryptedData,
header: wrappedRootKey.nonce,
},
contact: {
@@ -301,11 +298,11 @@ const encryptedContact = () => {
profilePictureAttachmentID: "att_test",
encryptedKey: combined(
wrappedContactKey.nonce,
- wrappedContactKey.encrypted_data,
+ wrappedContactKey.encryptedData,
).toString("base64"),
encryptedData: combined(
- data.decryption_header,
- data.encrypted_data,
+ data.decryptionHeader,
+ data.encryptedData,
).toString("base64"),
isDeleted: false,
createdAt: 100,
@@ -313,14 +310,10 @@ const encryptedContact = () => {
},
picture,
encryptedPicture: combined(
- encryptedPicture.decryption_header,
- encryptedPicture.encrypted_data,
+ encryptedPicture.decryptionHeader,
+ encryptedPicture.encryptedData,
),
};
- wrappedRootKey.free();
- wrappedContactKey.free();
- data.free();
- encryptedPicture.free();
return fixture;
};
diff --git a/web/packages/wasm/cast/index.ts b/web/packages/wasm/cast/index.ts
index 2aa868ce0e..13b2acad40 100644
--- a/web/packages/wasm/cast/index.ts
+++ b/web/packages/wasm/cast/index.ts
@@ -1 +1,57 @@
-export * from "./pkg/ente_cast_wasm";
+const wasm = () => import("./pkg/ente_cast_wasm");
+
+export type CastReceiver = import("./pkg/ente_cast_wasm").CastReceiver;
+
+export interface CastPayload {
+ castToken: string;
+ collectionID: number;
+ collectionKey: string;
+}
+
+export interface PreparedCastPayload {
+ castToken: string;
+ encryptedPayload: string;
+}
+
+export const createCastReceiver = async (): Promise<CastReceiver> =>
+ new (await wasm()).CastReceiver();
+
+export const openCastPayload = (
+ receiver: CastReceiver,
+ encryptedPayload: string,
+): CastPayload =>
+ plainValue(receiver.openPayload(encryptedPayload), (payload) => ({
+ castToken: payload.castToken,
+ collectionID: Number(payload.collectionID),
+ collectionKey: payload.collectionKey,
+ }));
+
+export const prepareCastPayload = async (
+ publicKey: string,
+ pqPublicKey: string | undefined,
+ collectionID: number,
+ collectionKey: string,
+): Promise<PreparedCastPayload> =>
+ plainValue(
+ (await wasm()).preparePayload(
+ publicKey,
+ pqPublicKey,
+ BigInt(collectionID),
+ collectionKey,
+ ),
+ (payload) => ({
+ castToken: payload.castToken,
+ encryptedPayload: payload.encryptedPayload,
+ }),
+ );
+
+const plainValue = <T extends { free: () => void }, U>(
+ value: T,
+ read: (value: T) => U,
+) => {
+ try {
+ return read(value);
+ } finally {
+ value.free();
+ }
+};
diff --git a/web/packages/wasm/core/index.ts b/web/packages/wasm/core/index.ts
new file mode 100644
index 0000000000..69ba3dc159
--- /dev/null
+++ b/web/packages/wasm/core/index.ts
@@ -0,0 +1,316 @@
+import type { SrpSession } from "./pkg/ente_core_wasm";
+
+export type { SrpSession };
+
+const wasm = () => import("./pkg/ente_core_wasm");
+
+type BytesOrB64 = Uint8Array | string;
+
+export interface DerivedKey {
+ key: string;
+ salt: string;
+ opsLimit: number;
+ memLimit: number;
+}
+
+export interface EncryptedBlob {
+ encryptedData: BytesOrB64;
+ decryptionHeader: BytesOrB64;
+}
+
+export interface EncryptedBlobB64 {
+ encryptedData: string;
+ decryptionHeader: string;
+}
+
+export interface EncryptedBox {
+ encryptedData: BytesOrB64;
+ nonce: BytesOrB64;
+}
+
+export interface EncryptedBoxB64 {
+ encryptedData: string;
+ nonce: string;
+}
+
+export interface KeyPair {
+ publicKey: string;
+ privateKey: string;
+}
+
+export interface SRPSetupAttributes {
+ srpSalt: string;
+ srpVerifier: string;
+ loginSubKey: string;
+}
+
+export const deriveKey = async (
+ password: string,
+ saltB64: string,
+ opsLimit: number,
+ memLimit: number,
+) => (await wasm()).auth_derive_kek(password, saltB64, memLimit, opsLimit);
+
+export const deriveSensitiveKey = async (
+ password: string,
+): Promise<DerivedKey> =>
+ plainValue(
+ (await wasm()).auth_generate_sensitive_kek(password),
+ derivedKeyValue,
+ );
+
+export const deriveInteractiveKey = async (
+ password: string,
+): Promise<DerivedKey> =>
+ plainValue(
+ (await wasm()).auth_generate_interactive_kek(password),
+ derivedKeyValue,
+ );
+
+export const generateSRPSetup = async (
+ kekB64: string,
+ srpUserID: string,
+): Promise<SRPSetupAttributes> =>
+ plainValue(
+ (await wasm()).auth_generate_srp_setup(kekB64, srpUserID),
+ (setup) => ({
+ srpSalt: setup.srp_salt,
+ srpVerifier: setup.srp_verifier,
+ loginSubKey: setup.login_sub_key,
+ }),
+ );
+
+export const recoveryKeyFromMnemonicOrHex = async (value: string) =>
+ (await wasm()).auth_recovery_key_from_mnemonic_or_hex(value);
+
+export const recoveryKeyToMnemonic = async (recoveryKeyB64: string) =>
+ (await wasm()).auth_recovery_key_to_mnemonic(recoveryKeyB64);
+
+export const createSRPSession = async (
+ srpSaltB64: string,
+ srpUserID: string,
+ loginKeyB64: string,
+): Promise<SrpSession> =>
+ new (await wasm()).SrpSession(srpUserID, srpSaltB64, loginKeyB64);
+
+export const generateKey = async () => (await wasm()).crypto_generate_key();
+
+export const generateKeyPair = async (): Promise<KeyPair> =>
+ plainValue((await wasm()).crypto_generate_keypair(), (keyPair) => ({
+ publicKey: keyPair.public_key,
+ privateKey: keyPair.secret_key,
+ }));
+
+export const encryptBox = async (
+ dataB64: string,
+ keyB64: string,
+): Promise<EncryptedBoxB64> =>
+ plainValue((await wasm()).crypto_encrypt_box(dataB64, keyB64), (box) => ({
+ encryptedData: box.encrypted_data,
+ nonce: box.nonce,
+ }));
+
+export const decryptBox = async (
+ box: EncryptedBox,
+ key: Uint8Array | string,
+): Promise<string> =>
+ (await wasm()).crypto_decrypt_box(
+ toB64String(box.encryptedData),
+ toB64String(box.nonce),
+ toB64String(key),
+ );
+
+export const decryptBoxBytes = async (
+ box: EncryptedBox,
+ key: Uint8Array | string,
+): Promise<Uint8Array<ArrayBuffer>> =>
+ fromB64String(await decryptBox(box, key));
+
+export const encryptBlob = async (
+ dataB64: string,
+ keyB64: string,
+): Promise<EncryptedBlobB64> =>
+ plainValue((await wasm()).crypto_encrypt_blob(dataB64, keyB64), (blob) => ({
+ encryptedData: blob.encrypted_data,
+ decryptionHeader: blob.decryption_header,
+ }));
+
+export const decryptMetadataJSON = async (
+ blob: EncryptedBlob,
+ key: Uint8Array | string,
+): Promise<unknown> => {
+ const wasmModule = await wasm();
+ const encryptedData = toB64String(blob.encryptedData);
+ const decryptionHeader = toB64String(blob.decryptionHeader);
+ const keyB64 = toB64String(key);
+ let plaintextB64: string;
+ try {
+ plaintextB64 = wasmModule.crypto_decrypt_blob(
+ encryptedData,
+ decryptionHeader,
+ keyB64,
+ );
+ } catch (error) {
+ if (!(error instanceof Error && error.name == "stream_truncated")) {
+ throw error;
+ }
+ plaintextB64 = wasmModule.crypto_decrypt_blob_legacy(
+ encryptedData,
+ decryptionHeader,
+ keyB64,
+ );
+ }
+ return JSON.parse(new TextDecoder().decode(fromB64String(plaintextB64)));
+};
+
+export const boxSeal = async (
+ dataB64: string,
+ publicKeyB64: string,
+): Promise<string> => (await wasm()).crypto_box_seal(dataB64, publicKeyB64);
+
+export const boxSealOpen = async (
+ encryptedData: string,
+ keyPair: KeyPair,
+): Promise<string> =>
+ (await wasm()).crypto_box_seal_open(
+ encryptedData,
+ keyPair.publicKey,
+ keyPair.privateKey,
+ );
+
+export const boxSealOpenBytes = async (
+ encryptedData: string,
+ keyPair: KeyPair,
+): Promise<Uint8Array<ArrayBuffer>> =>
+ fromB64String(await boxSealOpen(encryptedData, keyPair));
+
+export const deriveSubKeyBytes = async (
+ keyB64: string,
+ subKeyLength: number,
+ subKeyID: number,
+ context: string,
+): Promise<Uint8Array<ArrayBuffer>> =>
+ fromB64String(
+ (await wasm()).crypto_derive_subkey(
+ keyB64,
+ subKeyLength,
+ BigInt(subKeyID),
+ context,
+ ),
+ );
+
+export const md5Base64 = async (data: Uint8Array) =>
+ (await wasm()).crypto_md5_base64(data);
+
+export interface StreamEncryptorHandle {
+ key: string;
+ decryptionHeader: string;
+ encryptChunk: (data: Uint8Array, isFinal: boolean) => Promise<Uint8Array>;
+ free: () => void;
+}
+
+export const createStreamEncryptor =
+ async (): Promise<StreamEncryptorHandle> => {
+ const encryptor = new (await wasm()).CryptoStreamEncryptor();
+ return {
+ key: encryptor.key,
+ decryptionHeader: encryptor.decryption_header,
+ encryptChunk: (data, isFinal) =>
+ Promise.resolve(encryptor.encrypt_chunk(data, isFinal)),
+ free: () => encryptor.free(),
+ };
+ };
+
+export interface StreamDecryptorHandle {
+ decryptionChunkSize: number;
+ decryptChunk: (data: Uint8Array) => Promise<Uint8Array>;
+ isFinalized: () => boolean;
+ free: () => void;
+}
+
+export const createStreamDecryptor = async (
+ decryptionHeaderB64: string,
+ keyB64: string,
+): Promise<StreamDecryptorHandle> => {
+ const decryptor = new (await wasm()).CryptoStreamDecryptor(
+ decryptionHeaderB64,
+ keyB64,
+ );
+ return {
+ decryptionChunkSize: decryptor.decryption_chunk_size,
+ decryptChunk: (data) => Promise.resolve(decryptor.decrypt_chunk(data)),
+ isFinalized: () => decryptor.is_finalized,
+ free: () => decryptor.free(),
+ };
+};
+
+export interface EncryptedFileResult {
+ encryptedData: string;
+ decryptionHeader: string;
+ md5Hash: string;
+ key: string;
+}
+
+export const encryptFileStreamWithKey = async (
+ dataB64: string,
+ keyB64: string,
+): Promise<EncryptedFileResult> =>
+ plainValue(
+ (await wasm()).crypto_encrypt_stream_with_key(dataB64, keyB64),
+ encryptedFileValue,
+ );
+
+export const stringToB64 = (value: string): string =>
+ toB64String(new TextEncoder().encode(value));
+
+export const b64ToBytes = (value: string): Uint8Array<ArrayBuffer> =>
+ fromB64String(value);
+
+const derivedKeyValue = (key: {
+ key: string;
+ salt: string;
+ ops_limit: number;
+ mem_limit: number;
+}): DerivedKey => ({
+ key: key.key,
+ salt: key.salt,
+ opsLimit: key.ops_limit,
+ memLimit: key.mem_limit,
+});
+
+const encryptedFileValue = (file: {
+ encrypted_data: string;
+ decryption_header: string;
+ key: string;
+ md5_hash: string;
+}): EncryptedFileResult => ({
+ encryptedData: file.encrypted_data,
+ decryptionHeader: file.decryption_header,
+ key: file.key,
+ md5Hash: file.md5_hash,
+});
+
+const plainValue = <T extends { free: () => void }, U>(
+ value: T,
+ read: (value: T) => U,
+) => {
+ try {
+ return read(value);
+ } finally {
+ value.free();
+ }
+};
+
+const toB64String = (value: Uint8Array | string): string => {
+ if (typeof value == "string") return value;
+ let binary = "";
+ for (const byte of value) binary += String.fromCharCode(byte);
+ return btoa(binary);
+};
+
+const fromB64String = (value: string): Uint8Array<ArrayBuffer> => {
+ const binary = atob(value);
+ const bytes = new Uint8Array(binary.length);
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
+ return bytes;
+};
diff --git a/web/packages/wasm/core/package.json b/web/packages/wasm/core/package.json
index 1974ae1388..d5d5eddd9b 100644
--- a/web/packages/wasm/core/package.json
+++ b/web/packages/wasm/core/package.json
@@ -4,12 +4,12 @@
"private": true,
"exports": {
".": {
- "types": "./pkg/ente_core_wasm.d.ts",
- "import": "./pkg/ente_core_wasm.js"
+ "types": "./index.ts",
+ "import": "./index.ts"
}
},
- "module": "./pkg/ente_core_wasm.js",
- "types": "./pkg/ente_core_wasm.d.ts",
+ "module": "./index.ts",
+ "types": "./index.ts",
"scripts": {
"build": "wasm-pack build ../../../../rust/bindings/wasm/core-wasm --target bundler --out-dir ../../../../web/packages/wasm/core/pkg --no-pack",
"test": "vitest --run"
diff --git a/web/packages/wasm/ensu/index.ts b/web/packages/wasm/ensu/index.ts
index 4018bdf7f0..2e2a31358a 100644
--- a/web/packages/wasm/ensu/index.ts
+++ b/web/packages/wasm/ensu/index.ts
@@ -1 +1,38 @@
-export * from "./pkg/ente_ensu_wasm";
+const wasm = () => import("./pkg/ente_ensu_wasm");
+
+export const generateChatKey = async () => (await wasm()).generateChatKey();
+
+export const encryptChatPayload = async (value: string, keyB64: string) => {
+ const payload = (await wasm()).encryptChatPayload(value, keyB64);
+ try {
+ return { encryptedData: payload.encryptedData, header: payload.header };
+ } finally {
+ payload.free();
+ }
+};
+
+export const decryptChatPayload = async (
+ encryptedDataB64: string,
+ headerB64: string,
+ keyB64: string,
+) => (await wasm()).decryptChatPayload(encryptedDataB64, headerB64, keyB64);
+
+export const encryptChatField = async (value: string, keyB64: string) =>
+ (await wasm()).encryptChatField(value, keyB64);
+
+export const decryptChatField = async (value: string, keyB64: string) =>
+ (await wasm()).decryptChatField(value, keyB64);
+
+export const encryptChatAttachment = async (
+ data: Uint8Array,
+ keyB64: string,
+ sessionUUID: string,
+): Promise<Uint8Array> =>
+ (await wasm()).encryptChatAttachment(data, keyB64, sessionUUID);
+
+export const decryptChatAttachment = async (
+ data: Uint8Array,
+ keyB64: string,
+ sessionUUID: string,
+): Promise<Uint8Array> =>
+ (await wasm()).decryptChatAttachment(data, keyB64, sessionUUID);
diff --git a/web/packages/wasm/space/index.ts b/web/packages/wasm/space/index.ts
index ecf98f15d1..fae0607a36 100644
--- a/web/packages/wasm/space/index.ts
+++ b/web/packages/wasm/space/index.ts
@@ -1 +1,41 @@
-export * from "./pkg/ente_space_wasm";
+const wasm = () => import("./pkg/ente_space_wasm");
+
+export type SpaceAccountCtxHandle =
+ import("./pkg/ente_space_wasm").SpaceAccountCtxHandle;
+export type SpaceLinkCtxHandle =
+ import("./pkg/ente_space_wasm").SpaceLinkCtxHandle;
+
+export interface OpenSpaceAccountContextInput {
+ baseUrl: string;
+ clientPackage: string;
+ clientVersion?: string;
+ ownedSpaces?: unknown[];
+ spaceRootKeyB64: string;
+ spaceSessionToken: string;
+}
+
+export interface OpenSpaceLinkContextInput {
+ accessKey: string;
+ baseUrl: string;
+ clientPackage: string;
+ clientVersion?: string;
+ spaceUsername: string;
+}
+
+export const encryptSpaceRootEntityKey = async (
+ spaceRootKeyB64: string,
+ masterKeyB64: string,
+) => (await wasm()).encryptSpaceRootEntityKey(spaceRootKeyB64, masterKeyB64);
+
+export const decryptSpaceRootEntityKey = async (
+ encryptedKeyB64: string,
+ masterKeyB64: string,
+) => (await wasm()).decryptSpaceRootEntityKey(encryptedKeyB64, masterKeyB64);
+
+export const openSpaceAccountContext = async (
+ input: OpenSpaceAccountContextInput,
+): Promise<SpaceAccountCtxHandle> => (await wasm()).spaceOpenAccountCtx(input);
+
+export const openSpaceLinkContext = async (
+ input: OpenSpaceLinkContextInput,
+): Promise<SpaceLinkCtxHandle> => (await wasm()).spaceOpenLinkCtx(input);
diff --git a/web/scripts/wasm-imports/entrypoints.mjs b/web/scripts/wasm-imports/entrypoints.mjs
new file mode 100644
index 0000000000..83987beaa0
--- /dev/null
+++ b/web/scripts/wasm-imports/entrypoints.mjs
@@ -0,0 +1,23 @@
+import { globSync, readFileSync } from "node:fs";
+import { rawWasmPath } from "./eslint.config.mjs";
+
+const payload = new RegExp(rawWasmPath);
+for (const path of globSync("packages/wasm/*/package.json")) {
+ const manifest = JSON.parse(readFileSync(path, "utf8"));
+ const target = ["main", "module", "browser", "exports"]
+ .flatMap((key) => runtimeTargets(manifest[key]))
+ .find((target) => payload.test(target));
+ if (target) {
+ console.error(
+ `${path}: runtime entrypoint ${target} bypasses the wrapper`,
+ );
+ process.exitCode = 1;
+ }
+}
+
+function runtimeTargets(value) {
+ if (typeof value === "string") return [value];
+ return Object.entries(value ?? {}).flatMap(([key, target]) =>
+ key === "types" ? [] : runtimeTargets(target),
+ );
+}
diff --git a/web/scripts/wasm-imports/eslint.config.mjs b/web/scripts/wasm-imports/eslint.config.mjs
new file mode 100644
index 0000000000..57b8cef0a0
--- /dev/null
+++ b/web/scripts/wasm-imports/eslint.config.mjs
@@ -0,0 +1,79 @@
+export const rawWasmPath = String.raw`(?:^|/)pkg(?:/|$)|\.wasm(?:[?#].*)?$`;
+
+const selectorRawWasmPath = rawWasmPath.replaceAll("/", "\\u002F");
+const payloadImport = `ImportExpression[source.value=/${selectorRawWasmPath}/]`;
+const exportedPayloadLoader = [
+ "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > ArrowFunctionExpression > ImportExpression",
+ "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > ArrowFunctionExpression > AwaitExpression > ImportExpression",
+ "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > ArrowFunctionExpression > BlockStatement > ReturnStatement > ImportExpression",
+ "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > ArrowFunctionExpression > BlockStatement > ReturnStatement > AwaitExpression > ImportExpression",
+ "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > FunctionExpression > BlockStatement > ReturnStatement > ImportExpression",
+ "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > FunctionExpression > BlockStatement > ReturnStatement > AwaitExpression > ImportExpression",
+ "ExportNamedDeclaration > FunctionDeclaration > BlockStatement > ReturnStatement > ImportExpression",
+ "ExportNamedDeclaration > FunctionDeclaration > BlockStatement > ReturnStatement > AwaitExpression > ImportExpression",
+ ":matches(ExportNamedDeclaration, ExportDefaultDeclaration) > ClassDeclaration > ClassBody > PropertyDefinition[static=false]:not([accessibility=private]):not([key.type=PrivateIdentifier]) > ImportExpression",
+]
+ .map((selector) => `${selector}[source.value=/${selectorRawWasmPath}/]`)
+ .join(",");
+
+export default [
+ {
+ ignores: [
+ "**/pkg/**",
+ "**/.next*/**",
+ "**/dist/**",
+ "**/out/**",
+ "**/public/**",
+ "**/*.d.ts",
+ "**/*.test.*",
+ ],
+ },
+ {
+ files: ["**/*.{js,jsx,mjs,cjs,ts,tsx,mts,cts}"],
+ rules: {
+ "no-restricted-imports": [
+ "error",
+ {
+ patterns: [
+ {
+ regex: rawWasmPath,
+ allowTypeImports: true,
+ message:
+ "Use an import-safe wrapper that loads generated WASM on demand.",
+ },
+ ],
+ },
+ ],
+ "no-restricted-syntax": [
+ "error",
+ {
+ selector: payloadImport,
+ message:
+ "Load generated WASM through a wrapper in web/packages/wasm.",
+ },
+ ],
+ },
+ },
+ {
+ files: ["packages/wasm/**"],
+ rules: {
+ "no-restricted-syntax": [
+ "error",
+ {
+ selector:
+ payloadImport +
+ ":not(:matches(FunctionDeclaration, FunctionExpression, ArrowFunctionExpression) ImportExpression)" +
+ ":not(PropertyDefinition[static=false] > .value)" +
+ ":not(PropertyDefinition[static=false] > .value ImportExpression)",
+ message:
+ "Defer loading generated WASM until an operation needs it.",
+ },
+ {
+ selector: exportedPayloadLoader,
+ message:
+ "Export operations instead of the generated WASM module.",
+ },
+ ],
+ },
+ },
+];
diff --git a/web/scripts/wasm-imports/test.mjs b/web/scripts/wasm-imports/test.mjs
new file mode 100644
index 0000000000..a278c6a09a
--- /dev/null
+++ b/web/scripts/wasm-imports/test.mjs
@@ -0,0 +1,202 @@
+import assert from "node:assert/strict";
+import { spawnSync } from "node:child_process";
+import {
+ mkdirSync,
+ mkdtempSync,
+ rmSync,
+ symlinkSync,
+ writeFileSync,
+} from "node:fs";
+import { tmpdir } from "node:os";
+import { join, resolve } from "node:path";
+import test from "node:test";
+
+const dependencies = resolve(import.meta.dirname, "../../node_modules");
+const eslint = [
+ join(dependencies, "eslint/bin/eslint.js"),
+ "--config",
+ join(import.meta.dirname, "eslint.config.mjs"),
+ "--parser",
+ "@typescript-eslint/parser",
+ "--no-inline-config",
+ "--max-warnings",
+ "0",
+ "apps",
+ "packages",
+];
+const entrypoints = [join(import.meta.dirname, "entrypoints.mjs")];
+
+test("WASM inner boundary", async (t) => {
+ const root = mkdtempSync(join(tmpdir(), "ente-wasm-check-"));
+ t.after(() => rmSync(root, { recursive: true }));
+ const web = join(root, "web");
+ const wrapper = join(web, "packages/wasm/new-package");
+ mkdirSync(wrapper, { recursive: true });
+ mkdirSync(join(web, "apps"));
+ symlinkSync(dependencies, join(web, "node_modules"), "dir");
+
+ const cases = [
+ ["new package, static caller, and type exports", {}],
+ [
+ "public raw module loader",
+ {
+ source: 'export const loadWasm = () => import("./pkg/arbitrary-name");',
+ error: "no-restricted-syntax",
+ },
+ ],
+ [
+ "block-bodied public raw module loader",
+ {
+ source: 'export const loadWasm = () => { return import("./pkg/arbitrary-name"); };',
+ error: "no-restricted-syntax",
+ },
+ ],
+ [
+ "arrow operation with a private nested loader",
+ {
+ source: [
+ "export const operation = () => {",
+ ' const wasm = () => import("./pkg/arbitrary-name");',
+ " return wasm().then((module) => module.operation());",
+ "};",
+ ].join("\n"),
+ },
+ ],
+ [
+ "function operation with a private nested loader",
+ {
+ source: [
+ "export function operation() {",
+ ' function wasm() { return import("./pkg/arbitrary-name"); }',
+ " return wasm().then((module) => module.operation());",
+ "}",
+ ].join("\n"),
+ },
+ ],
+ [
+ "eager dynamic import",
+ {
+ source: 'export const wasm = import("./pkg/arbitrary-name");',
+ error: "no-restricted-syntax",
+ },
+ ],
+ [
+ "static payload import",
+ {
+ source: 'import * as wasm from "./pkg/arbitrary-name";',
+ error: "no-restricted-imports",
+ },
+ ],
+ [
+ "raw binding re-export",
+ {
+ source: 'export * from "./pkg/arbitrary-name";',
+ error: "no-restricted-imports",
+ },
+ ],
+ [
+ "inline suppression cannot disable the check",
+ {
+ source: '/* eslint-disable no-restricted-imports */\nexport * from "./pkg/arbitrary-name";',
+ error: "no-restricted-imports",
+ },
+ ],
+ [
+ "app bypasses the wrapper",
+ {
+ caller: 'export const bypass = () => import("../packages/wasm/new-package/pkg/arbitrary-name");',
+ error: "no-restricted-syntax",
+ },
+ ],
+ [
+ "app imports a WASM payload",
+ {
+ caller: 'export const bypass = () => import("../packages/wasm/new-package/arbitrary.wasm?url");',
+ error: "no-restricted-syntax",
+ },
+ ],
+ [
+ "private lazy instance initialization",
+ {
+ source: 'export class Client { #wasm = import("./pkg/arbitrary-name"); }',
+ },
+ ],
+ [
+ "public raw module field",
+ {
+ source: 'export class Client { wasm = import("./pkg/arbitrary-name"); }',
+ error: "no-restricted-syntax",
+ },
+ ],
+ [
+ "eager static initialization",
+ {
+ source: 'export class Client { static wasm = import("./pkg/arbitrary-name"); }',
+ error: "no-restricted-syntax",
+ },
+ ],
+ [
+ "runtime manifest entry bypass",
+ {
+ manifest: { module: "./arbitrary.wasm" },
+ error: "bypasses the wrapper",
+ },
+ ],
+ [
+ "conditional subpath bypass",
+ {
+ manifest: {
+ exports: {
+ "./raw": {
+ browser: { import: "./pkg/arbitrary-name.js" },
+ },
+ },
+ },
+ error: "bypasses the wrapper",
+ },
+ ],
+ ];
+ for (const [name, { source, caller, manifest, error }] of cases) {
+ await t.test(name, () => {
+ writeFileSync(
+ join(wrapper, "index.ts"),
+ source ??
+ [
+ 'export type { Result } from "./pkg/arbitrary-name";',
+ 'const wasm = () => import("./pkg/arbitrary-name");',
+ "export const operation = async () => (await wasm()).operation();",
+ ].join("\n"),
+ );
+ writeFileSync(
+ join(web, "apps/consumer.ts"),
+ caller ?? 'import "../packages/wasm/new-package/index";',
+ );
+ writeFileSync(
+ join(wrapper, "package.json"),
+ JSON.stringify({
+ name: "unlisted-wasm-package",
+ module: "./index.ts",
+ exports: {
+ ".": {
+ types: "./pkg/arbitrary-name.d.ts",
+ import: "./index.ts",
+ },
+ },
+ ...manifest,
+ }),
+ );
+ for (const [args, expected] of [
+ [eslint, manifest ? undefined : error],
+ [entrypoints, manifest ? error : undefined],
+ ]) {
+ const result = spawnSync(process.execPath, args, {
+ cwd: web,
+ encoding: "utf8",
+ });
+ const output = result.stdout + result.stderr;
+ assert.equal(result.status, expected ? 1 : 0, output);
+ if (expected) assert.match(output, new RegExp(expected));
+ }
+ });
+ }
+});