This commit is contained in:
Manav Rathi 2024-11-26 07:25:57 +05:30
parent 2b36a3e82a
commit e36aad9f7c
No known key found for this signature in database
4 changed files with 44 additions and 20 deletions

View File

@ -83,3 +83,9 @@ export const _decryptMetadataJSON = async (r: {
},
r.keyB64,
);
export const _generateKeyPair = libsodium.generateKeyPair;
export const _boxSeal = libsodium.boxSeal;
export const _boxSealOpen = libsodium.boxSealOpen;

View File

@ -1,6 +1,9 @@
/**
* @file Higher level functions that use the ontology of Ente's requirements.
*
* For more detailed documentation of specific functions, see the corresponding
* function in `libsodium.ts`.
*
* [Note: Crypto code hierarchy]
*
* 1. @/base/crypto (Crypto API for our code)
@ -172,8 +175,6 @@ export const encryptThumbnail = (data: BytesOrB64, key: BytesOrB64) =>
/**
* Encrypt the given data using chunked streaming encryption, but process all
* the chunks in one go.
*
* For more details, see {@link encryptStreamBytes} in `libsodium.ts`.
*/
export const encryptStreamBytes = async (data: Uint8Array, key: BytesOrB64) =>
inWorker()
@ -182,8 +183,6 @@ export const encryptStreamBytes = async (data: Uint8Array, key: BytesOrB64) =>
/**
* Prepare for chunked streaming encryption using {@link encryptStreamChunk}.
*
* For more details, see {@link initChunkEncryption} in `libsodium.ts`.
*/
export const initChunkEncryption = async (key: BytesOrB64) =>
inWorker()
@ -192,8 +191,6 @@ export const initChunkEncryption = async (key: BytesOrB64) =>
/**
* Encrypt a chunk as part of a chunked streaming encryption.
*
* For more details, see {@link encryptStreamChunk} in `libsodium.ts`.
*/
export const encryptStreamChunk = async (
data: Uint8Array,
@ -346,3 +343,33 @@ export const decryptMetadataJSON = (r: {
inWorker()
? ei._decryptMetadataJSON(r)
: sharedCryptoWorker().then((w) => w.decryptMetadataJSON(r));
/**
* Generate a new public/private keypair for use with the boxSeal* functions.
*/
export const generateKeyPair = async () =>
inWorker()
? ei._generateKeyPair()
: sharedCryptoWorker().then((w) => w.generateKeyPair());
/**
* Public key encryption.
*/
export const boxSeal = async (data: string, publicKey: string) =>
inWorker()
? ei._boxSeal(data, publicKey)
: sharedCryptoWorker().then((w) => w.boxSeal(data, publicKey));
/**
* Decrypt the result of {@link boxSeal}.
*/
export const boxSealOpen = async (
encryptedData: string,
publicKey: string,
secretKey: string,
) =>
inWorker()
? ei._boxSealOpen(encryptedData, publicKey, secretKey)
: sharedCryptoWorker().then((w) =>
w.boxSealOpen(encryptedData, publicKey, secretKey),
);

View File

@ -707,14 +707,14 @@ export const boxSeal = async (data: string, publicKey: string) => {
* underlying data.
*/
export const boxSealOpen = async (
input: string,
encryptedData: string,
publicKey: string,
secretKey: string,
) => {
await sodium.ready;
return toB64(
sodium.crypto_box_seal_open(
await fromB64(input),
await fromB64(encryptedData),
await fromB64(publicKey),
await fromB64(secretKey),
),

View File

@ -33,6 +33,9 @@ export class CryptoWorker {
decryptStreamChunk = ei._decryptStreamChunk;
decryptMetadataJSON_New = ei._decryptMetadataJSON_New;
decryptMetadataJSON = ei._decryptMetadataJSON;
generateKeyPair = ei._generateKeyPair;
boxSeal = ei._boxSeal;
boxSealOpen = ei._boxSealOpen;
// TODO: -- AUDIT BELOW --
@ -93,18 +96,6 @@ export class CryptoWorker {
return libsodium.generateSaltToDeriveKey();
}
async generateKeyPair() {
return libsodium.generateKeyPair();
}
async boxSealOpen(input: string, publicKey: string, secretKey: string) {
return libsodium.boxSealOpen(input, publicKey, secretKey);
}
async boxSeal(input: string, publicKey: string) {
return libsodium.boxSeal(input, publicKey);
}
async generateSubKey(
key: string,
subKeyLength: number,