Skip to content

armgit / ente

Public Imported from GitHub
0

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

kp

Manav Rathi8519183013db
diff --git a/web/packages/accounts/services/user.ts b/web/packages/accounts/services/user.ts
index 130c4020b1..1cd6e67766 100644
--- a/web/packages/accounts/services/user.ts
+++ b/web/packages/accounts/services/user.ts
@@ -148,7 +148,7 @@ export interface KeyAttributes {
*
* Base64 encoded.
*
- * [Note: Public and secret key nomenclature]
+ * [Note: privateKey and secretKey]
*
* The nomenclature for the key pair follows libsodium's conventions
* (https://doc.libsodium.org/public-key_cryptography/authenticated_encryption#key-pair-generation),
diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.ts
index a1e07bccfc..ad56a0c599 100644
--- a/web/packages/base/crypto/ente-impl.ts
+++ b/web/packages/base/crypto/ente-impl.ts
@@ -33,6 +33,7 @@ export const _chunkHashFinal = libsodium.chunkHashFinal;
export const _generateKeyPair = libsodium.generateKeyPair;
export const _boxSeal = libsodium.boxSeal;
export const _boxSealOpen = libsodium.boxSealOpen;
+export const _boxSealOpenBytes = libsodium.boxSealOpenBytes;
export const _generateDeriveKeySalt = libsodium.generateDeriveKeySalt;
export const _deriveKey = libsodium.deriveKey;
export const _deriveSensitiveKey = libsodium.deriveSensitiveKey;
diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.ts
index cfb2717521..9de7798a0f 100644
--- a/web/packages/base/crypto/index.ts
+++ b/web/packages/base/crypto/index.ts
@@ -99,6 +99,7 @@ import type {
EncryptedFile,
InitChunkDecryptionResult,
InitChunkEncryptionResult,
+ KeyPair,
SodiumStateAddress,
} from "./types";
import type { CryptoWorker } from "./worker";
@@ -407,10 +408,7 @@ export const decryptMetadataJSON = (
/**
* Generate a new public/private keypair.
*/
-export const generateKeyPair = (): Promise<{
- publicKey: string;
- privateKey: string;
-}> =>
+export const generateKeyPair = (): Promise<KeyPair> =>
inWorker()
? ei._generateKeyPair()
: sharedWorker().then((w) => w.generateKeyPair());
@@ -436,6 +434,19 @@ export const boxSealOpen = (
: sharedWorker().then((w) =>
w.boxSealOpen(encryptedData, publicKey, secretKey),
);
+/**
+ * Variant of {@link boxSealOpen} that returns the decrypted bytes as it is
+ * (without encoding them to base64).
+ */
+export const boxSealOpenBytes = (
+ encryptedData: string,
+ keyPair: KeyPair,
+): Promise<string> =>
+ inWorker()
+ ? ei._boxSealOpenBytes(encryptedData, keyPair)
+ : sharedWorker().then((w) =>
+ w._boxSealOpenBytes(encryptedData, keyPair),
+ );
/**
* Return a new randomly generated 128-bit salt (as a base64 string).
diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.ts
index c627db171e..f629106607 100644
--- a/web/packages/base/crypto/libsodium.ts
+++ b/web/packages/base/crypto/libsodium.ts
@@ -21,6 +21,7 @@ import type {
EncryptedFile,
InitChunkDecryptionResult,
InitChunkEncryptionResult,
+ KeyPair,
SodiumStateAddress,
} from "./types";
@@ -765,14 +766,13 @@ export const boxSeal = async (data: string, publicKey: string) => {
*/
export const boxSealOpenBytes = async (
encryptedData: string,
- publicKey: string,
- secretKey: string,
+ { publicKey, privateKey }: KeyPair,
) => {
await sodium.ready;
return sodium.crypto_box_seal_open(
await fromB64(encryptedData),
await fromB64(publicKey),
- await fromB64(secretKey),
+ await fromB64(privateKey),
);
};
diff --git a/web/packages/base/crypto/types.ts b/web/packages/base/crypto/types.ts
index f884c1fc71..0ceb9be1a2 100644
--- a/web/packages/base/crypto/types.ts
+++ b/web/packages/base/crypto/types.ts
@@ -172,6 +172,24 @@ export interface InitChunkDecryptionResult {
decryptionChunkSize: number;
}
+/**
+ * A pair of public and private keys.
+ */
+export interface KeyPair {
+ /**
+ * The public key of the keypair, as a base64 encoded string.
+ */
+ publicKey: string;
+ /**
+ * The private key of the keypair, as a base64 encoded string.
+ *
+ * Some places also refer to it as the "secret key".
+ *
+ * See: [Note: privateKey and secretKey].
+ */
+ privateKey: string;
+}
+
/**
* A key derived from a user provided passphrase, and the various attributes
* that were used during the key derivation.
diff --git a/web/packages/base/crypto/worker.ts b/web/packages/base/crypto/worker.ts
index 82f4400bf8..4214855b3c 100644
--- a/web/packages/base/crypto/worker.ts
+++ b/web/packages/base/crypto/worker.ts
@@ -41,6 +41,7 @@ export class CryptoWorker {
generateKeyPair = ei._generateKeyPair;
boxSeal = ei._boxSeal;
boxSealOpen = ei._boxSealOpen;
+ boxSealOpenBytes = ei._boxSealOpenBytes;
generateDeriveKeySalt = ei._generateDeriveKeySalt;
deriveKey = ei._deriveKey;
deriveSensitiveKey = ei._deriveSensitiveKey;