Skip to content

armgit / ente

Public Imported from GitHub
0

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

types

Manav Rathi4be5ac878078
diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.ts
index a43c7673d7..0862454d19 100644
--- a/web/packages/base/crypto/index.ts
+++ b/web/packages/base/crypto/index.ts
@@ -88,7 +88,6 @@
* dealing with them.
*/
import { ComlinkWorker } from "ente-base/worker/comlink-worker";
-import { type StateAddress } from "libsodium-wrappers-sumo";
import { inWorker } from "../env";
import * as ei from "./ente-impl";
import type {
@@ -100,6 +99,9 @@ import type {
EncryptedBox,
EncryptedBoxB64,
EncryptedFile,
+ InitChunkDecryptionResult,
+ InitChunkEncryptionResult,
+ SodiumStateAddress,
} from "./types";
import type { CryptoWorker } from "./worker";
@@ -297,7 +299,9 @@ export const encryptStreamBytes = (
/**
* Prepare for chunked streaming encryption using {@link encryptStreamChunk}.
*/
-export const initChunkEncryption = async (key: BytesOrB64) =>
+export const initChunkEncryption = (
+ key: BytesOrB64,
+): Promise<InitChunkEncryptionResult> =>
inWorker()
? ei._initChunkEncryption(key)
: sharedWorker().then((w) => w.initChunkEncryption(key));
@@ -307,7 +311,7 @@ export const initChunkEncryption = async (key: BytesOrB64) =>
*/
export const encryptStreamChunk = (
data: Uint8Array,
- state: StateAddress,
+ state: SodiumStateAddress,
isFinalChunk: boolean,
): Promise<Uint8Array> =>
inWorker()
@@ -392,7 +396,10 @@ export const decryptStreamBytes = (
* Prepare to decrypt the encrypted result produced using {@link initChunkEncryption} and
* {@link encryptStreamChunk}.
*/
-export const initChunkDecryption = async (header: string, key: BytesOrB64) =>
+export const initChunkDecryption = (
+ header: string,
+ key: BytesOrB64,
+): Promise<InitChunkDecryptionResult> =>
inWorker()
? ei._initChunkDecryption(header, key)
: sharedWorker().then((w) => w.initChunkDecryption(header, key));
@@ -404,7 +411,7 @@ export const initChunkDecryption = async (header: string, key: BytesOrB64) =>
*/
export const decryptStreamChunk = (
data: Uint8Array,
- state: StateAddress,
+ state: SodiumStateAddress,
): Promise<Uint8Array> =>
inWorker()
? ei._decryptStreamChunk(data, state)
diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.ts
index f2305398f3..77b1e517c2 100644
--- a/web/packages/base/crypto/libsodium.ts
+++ b/web/packages/base/crypto/libsodium.ts
@@ -9,7 +9,7 @@
* To see where this code fits, see [Note: Crypto code hierarchy].
*/
import { mergeUint8Arrays } from "ente-utils/array";
-import sodium, { type StateAddress } from "libsodium-wrappers-sumo";
+import sodium from "libsodium-wrappers-sumo";
import type {
BytesOrB64,
DerivedKey,
@@ -19,6 +19,9 @@ import type {
EncryptedBox,
EncryptedBoxB64,
EncryptedFile,
+ InitChunkDecryptionResult,
+ InitChunkEncryptionResult,
+ SodiumStateAddress,
} from "./types";
/**
@@ -448,7 +451,9 @@ export const encryptStreamBytes = async (
* to subsequent calls to {@link encryptStreamChunk} along with the chunks's
* contents.
*/
-export const initChunkEncryption = async (key: BytesOrB64) => {
+export const initChunkEncryption = async (
+ key: BytesOrB64,
+): Promise<InitChunkEncryptionResult> => {
await sodium.ready;
const keyBytes = await bytes(key);
const { state, header } =
@@ -476,7 +481,7 @@ export const initChunkEncryption = async (key: BytesOrB64) => {
*/
export const encryptStreamChunk = async (
data: Uint8Array,
- pushState: sodium.StateAddress,
+ pushState: SodiumStateAddress,
isFinalChunk: boolean,
): Promise<Uint8Array> => {
await sodium.ready;
@@ -623,7 +628,7 @@ export const decryptStreamBytes = async (
export const initChunkDecryption = async (
decryptionHeader: string,
key: BytesOrB64,
-) => {
+): Promise<InitChunkDecryptionResult> => {
await sodium.ready;
const pullState = sodium.crypto_secretstream_xchacha20poly1305_init_pull(
await fromB64(decryptionHeader),
@@ -646,7 +651,7 @@ export const initChunkDecryption = async (
*/
export const decryptStreamChunk = async (
data: Uint8Array,
- pullState: StateAddress,
+ pullState: SodiumStateAddress,
): Promise<Uint8Array> => {
await sodium.ready;
const pullResult = sodium.crypto_secretstream_xchacha20poly1305_pull(
@@ -679,12 +684,6 @@ export async function generateKeyAndEncryptToB64(data: string) {
return await encryptToB64(data, await toB64(key));
}
-/**
- * An opaque object meant to be threaded through {@link chunkHashInit},
- * {@link chunkHashUpdate} and {@link chunkHashFinal}.
- */
-export type ChunkHashState = sodium.StateAddress;
-
/**
* Initialize and return new state that can be used to hash the chunks of data
* in a streaming manner.
@@ -701,7 +700,7 @@ export type ChunkHashState = sodium.StateAddress;
* (along with the data to hash) to {@link chunkHashUpdate}, and the final hash
* obtained using {@link chunkHashFinal}.
*/
-export const chunkHashInit = async (): Promise<ChunkHashState> => {
+export const chunkHashInit = async (): Promise<SodiumStateAddress> => {
await sodium.ready;
return sodium.crypto_generichash_init(
null,
@@ -719,7 +718,7 @@ export const chunkHashInit = async (): Promise<ChunkHashState> => {
* @param chunk The data (bytes) to hash.
*/
export const chunkHashUpdate = async (
- hashState: ChunkHashState,
+ hashState: SodiumStateAddress,
chunk: Uint8Array,
) => {
await sodium.ready;
@@ -736,7 +735,7 @@ export const chunkHashUpdate = async (
*
* @returns The hash of all the chunks (as a base64 string).
*/
-export const chunkHashFinal = async (hashState: ChunkHashState) => {
+export const chunkHashFinal = async (hashState: SodiumStateAddress) => {
await sodium.ready;
const hash = sodium.crypto_generichash_final(
hashState,
diff --git a/web/packages/base/crypto/types.ts b/web/packages/base/crypto/types.ts
index 64b29248b5..f884c1fc71 100644
--- a/web/packages/base/crypto/types.ts
+++ b/web/packages/base/crypto/types.ts
@@ -1,3 +1,11 @@
+import { type StateAddress } from "libsodium-wrappers-sumo";
+
+/**
+ * An opaque object meant to be threaded through various functions that deal
+ * with resumable chunk based processing.
+ */
+export type SodiumStateAddress = StateAddress;
+
/**
* Data provided either as bytes ({@link Uint8Array}) or their base64 string
* representation.
@@ -130,6 +138,40 @@ export interface EncryptedFile {
decryptionHeader: string;
}
+/**
+ * An object returned by the init function of chunked encryption routines.
+ */
+export interface InitChunkEncryptionResult {
+ /**
+ * A base64 string containing the decryption header.
+ *
+ * While the exact contents of the header are libsodium's internal details,
+ * it effectively contains a random nonce generated by libsodium. It does
+ * not need to be secret, but it is required to decrypt the data.
+ */
+ decryptionHeader: string;
+ /**
+ * An opaque value that refers to the internal state used by the resumable
+ * calls in the encryption sequence.
+ */
+ pushState: SodiumStateAddress;
+}
+
+/**
+ * An object returned by the init function of chunked decryption routines.
+ */
+export interface InitChunkDecryptionResult {
+ /**
+ * An opaque value that refers to the internal state used by the resumable
+ * calls in the decryption sequence.
+ */
+ pullState: SodiumStateAddress;
+ /**
+ * The expected size of each chunk.
+ */
+ decryptionChunkSize: number;
+}
+
/**
* A key derived from a user provided passphrase, and the various attributes
* that were used during the key derivation.