Skip to content

armgit / ente

Public Imported from GitHub
0

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

Match the types

Manav Rathif3e947f47e5f
diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.ts
index bb7c6d77a2..156ee30094 100644
--- a/web/packages/base/crypto/ente-impl.ts
+++ b/web/packages/base/crypto/ente-impl.ts
@@ -1,13 +1,13 @@
/** Careful when adding add other imports! */
import * as libsodium from "./libsodium";
import type {
+ BytesOrB64,
DecryptBlobB64,
- DecryptBoxB64,
- DecryptBoxBytes,
EncryptB64,
EncryptBytes,
EncryptedBlobB64,
EncryptedBlobBytes,
+ EncryptedBox2,
EncryptJSON,
} from "./types";
@@ -43,18 +43,10 @@ export const _encryptMetadataJSON = ({ jsonValue, keyB64 }: EncryptJSON) =>
keyB64,
});
-const DecryptBoxB64ToBytes = async ({
- encryptedDataB64,
- nonceB64,
- keyB64,
-}: DecryptBoxB64): Promise<DecryptBoxBytes> => ({
- encryptedData: await libsodium.fromB64(encryptedDataB64),
- nonceB64,
- keyB64,
-});
+export const _decryptBox = libsodium.decryptBox2;
-export const _decryptBoxB64 = (r: DecryptBoxB64) =>
- DecryptBoxB64ToBytes(r).then((rb) => libsodium.decryptBox(rb));
+export const _decryptBoxB64 = (b: EncryptedBox2, k: BytesOrB64) =>
+ _decryptBox(b, k).then(libsodium.toB64);
export const _decryptAssociatedData = libsodium.decryptBlob;
diff --git a/web/packages/base/crypto/ente.ts b/web/packages/base/crypto/ente.ts
index 11f1c41878..d2657bb19f 100644
--- a/web/packages/base/crypto/ente.ts
+++ b/web/packages/base/crypto/ente.ts
@@ -52,11 +52,12 @@ import { assertionFailed } from "../assert";
import { inWorker } from "../env";
import * as ei from "./ente-impl";
import type {
+ BytesOrB64,
DecryptBlobB64,
DecryptBlobBytes,
- DecryptBoxB64,
EncryptB64,
EncryptBytes,
+ EncryptedBox2,
EncryptJSON,
} from "./types";
@@ -141,17 +142,21 @@ export const encryptMetadataJSON = async (r: EncryptJSON) =>
: sharedCryptoWorker().then((w) => w.encryptMetadataJSON(r));
/**
- * Decrypt arbitrary data, provided as a base64 string, using the given key and
- * the provided nonce, and return the base64
- *
- * This is the sibling of {@link encryptBoxB64}.
- *
- * See {@link decryptBox} for the implementation details.
+ * Decrypt a box encrypted using {@link encryptBox}.
+ */
+export const decryptBox = (b: EncryptedBox2, k: BytesOrB64) =>
+ inWorker()
+ ? ei._decryptBox(b, k)
+ : sharedCryptoWorker().then((w) => w.decryptBox(b, k));
+
+/**
+ * Variant of {@link decryptBox} that returns the decrypted data as a base64
+ * string.
*/
-export const decryptBoxB64 = (r: DecryptBoxB64) =>
+export const decryptBoxB64 = (b: EncryptedBox2, k: BytesOrB64) =>
inWorker()
- ? ei._decryptBoxB64(r)
- : sharedCryptoWorker().then((w) => w.decryptBoxB64(r));
+ ? ei._decryptBoxB64(b, k)
+ : sharedCryptoWorker().then((w) => w.decryptBoxB64(b, k));
/**
* Decrypt arbitrary data associated with an Ente object (file, collection or
diff --git a/web/packages/base/crypto/types.ts b/web/packages/base/crypto/types.ts
index 1e6144d059..e7ed032acb 100644
--- a/web/packages/base/crypto/types.ts
+++ b/web/packages/base/crypto/types.ts
@@ -171,10 +171,6 @@ export interface EncryptedBox2 {
* kept secret.
*/
nonce: BytesOrB64;
- /**
- * The encryption key.
- */
- key: BytesOrB64;
}
/**
diff --git a/web/packages/base/crypto/worker.ts b/web/packages/base/crypto/worker.ts
index 72ae1fb332..13e36c546d 100644
--- a/web/packages/base/crypto/worker.ts
+++ b/web/packages/base/crypto/worker.ts
@@ -15,6 +15,7 @@ export class CryptoWorker {
encryptBoxB64 = ei._encryptBoxB64;
encryptThumbnail = ei._encryptThumbnail;
encryptMetadataJSON = ei._encryptMetadataJSON;
+ decryptBox = ei._decryptBox;
decryptBoxB64 = ei._decryptBoxB64;
decryptThumbnail = ei._decryptThumbnail;
decryptAssociatedDataB64 = ei._decryptAssociatedDataB64;
diff --git a/web/packages/new/photos/services/user-entity.ts b/web/packages/new/photos/services/user-entity.ts
index 07fe126dd2..5902573088 100644
--- a/web/packages/new/photos/services/user-entity.ts
+++ b/web/packages/new/photos/services/user-entity.ts
@@ -1,4 +1,3 @@
-import { sharedCryptoWorker } from "@/base/crypto";
import { decryptAssociatedDataB64, decryptBoxB64 } from "@/base/crypto/ente";
import { authenticatedRequestHeaders, ensureOk, HTTPError } from "@/base/http";
import { getKV, getKVN, setKV } from "@/base/kv";
@@ -241,12 +240,14 @@ const decryptEntityKey = async ({
encryptedKey,
header,
}: RemoteUserEntityKey) =>
- decryptBoxB64({
- encryptedDataB64: encryptedKey,
- // Remote calls it the header, but it really is the nonce.
- nonceB64: header,
- keyB64: await masterKeyB64FromSession(),
- });
+ decryptBoxB64(
+ {
+ encryptedData: encryptedKey,
+ // Remote calls it the header, but it really is the nonce.
+ nonce: header,
+ },
+ await masterKeyB64FromSession(),
+ );
/**
* Fetch the encryption key for the given user entity {@link type} from remote.