From d331593f7be42e7f8c02447af82adced865b6a54 Mon Sep 17 00:00:00 2001 From: Bernd Schoolmann Date: Wed, 23 Apr 2025 10:05:49 +0200 Subject: [PATCH] Rename encryptToBytes to encryptFileData --- .../crypto/abstractions/encrypt.service.ts | 6 +++--- .../crypto/services/encrypt.service.implementation.ts | 6 +++--- .../crypto/services/encrypt.service.spec.ts | 10 +++++----- libs/common/src/tools/send/services/send.service.ts | 2 +- libs/common/src/vault/services/cipher.service.spec.ts | 2 +- libs/common/src/vault/services/cipher.service.ts | 7 +++++-- 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/libs/common/src/key-management/crypto/abstractions/encrypt.service.ts b/libs/common/src/key-management/crypto/abstractions/encrypt.service.ts index 5f21d86bc6a..d13b1685875 100644 --- a/libs/common/src/key-management/crypto/abstractions/encrypt.service.ts +++ b/libs/common/src/key-management/crypto/abstractions/encrypt.service.ts @@ -14,11 +14,11 @@ export abstract class EncryptService { */ abstract encrypt(plainValue: string | Uint8Array, key: SymmetricCryptoKey): Promise; /** - * Encrypts a value to a Uint8Array - * @param plainValue - The value to encrypt + * Encrypts a file and returns an EncArrayBuffer + * @param fileData - The file data to encrypt * @param key - The key to encrypt the value with */ - abstract encryptToBytes(plainValue: Uint8Array, key: SymmetricCryptoKey): Promise; + abstract encryptFileData(fileData: Uint8Array, key: SymmetricCryptoKey): Promise; /** * Wraps a decapsulation key (Private key) with a symmetric key diff --git a/libs/common/src/key-management/crypto/services/encrypt.service.implementation.ts b/libs/common/src/key-management/crypto/services/encrypt.service.implementation.ts index f4318840515..70ef61d36a7 100644 --- a/libs/common/src/key-management/crypto/services/encrypt.service.implementation.ts +++ b/libs/common/src/key-management/crypto/services/encrypt.service.implementation.ts @@ -135,7 +135,7 @@ export class EncryptServiceImplementation implements EncryptService { } } - async encryptToBytes(plainValue: Uint8Array, key: SymmetricCryptoKey): Promise { + async encryptFileData(fileData: Uint8Array, key: SymmetricCryptoKey): Promise { if (key == null) { throw new Error("No encryption key provided."); } @@ -148,7 +148,7 @@ export class EncryptServiceImplementation implements EncryptService { const innerKey = key.inner(); if (innerKey.type === EncryptionType.AesCbc256_HmacSha256_B64) { - const encValue = await this.aesEncrypt(plainValue, innerKey); + const encValue = await this.aesEncrypt(fileData, innerKey); const macLen = encValue.mac.length; const encBytes = new Uint8Array( 1 + encValue.iv.byteLength + macLen + encValue.data.byteLength, @@ -159,7 +159,7 @@ export class EncryptServiceImplementation implements EncryptService { encBytes.set(new Uint8Array(encValue.data), 1 + encValue.iv.byteLength + macLen); return new EncArrayBuffer(encBytes); } else if (innerKey.type === EncryptionType.AesCbc256_B64) { - const encValue = await this.aesEncryptLegacy(plainValue, innerKey); + const encValue = await this.aesEncryptLegacy(fileData, innerKey); const encBytes = new Uint8Array(1 + encValue.iv.byteLength + encValue.data.byteLength); encBytes.set([innerKey.type]); encBytes.set(new Uint8Array(encValue.iv), 1); diff --git a/libs/common/src/key-management/crypto/services/encrypt.service.spec.ts b/libs/common/src/key-management/crypto/services/encrypt.service.spec.ts index 6b2851ad116..3f18c2cecc9 100644 --- a/libs/common/src/key-management/crypto/services/encrypt.service.spec.ts +++ b/libs/common/src/key-management/crypto/services/encrypt.service.spec.ts @@ -225,7 +225,7 @@ describe("EncryptService", () => { const plainValue = makeStaticByteArray(16, 1); it("throws if no key is provided", () => { - return expect(encryptService.encryptToBytes(plainValue, null)).rejects.toThrow( + return expect(encryptService.encryptFileData(plainValue, null)).rejects.toThrow( "No encryption key", ); }); @@ -240,11 +240,11 @@ describe("EncryptService", () => { encryptionKey: mock32Key.key, }); - await expect(encryptService.encryptToBytes(plainValue, key)).rejects.toThrow( + await expect(encryptService.encryptFileData(plainValue, key)).rejects.toThrow( "Type 0 encryption is not supported.", ); - await expect(encryptService.encryptToBytes(plainValue, mock32Key)).rejects.toThrow( + await expect(encryptService.encryptFileData(plainValue, mock32Key)).rejects.toThrow( "Type 0 encryption is not supported.", ); }); @@ -256,7 +256,7 @@ describe("EncryptService", () => { cryptoFunctionService.randomBytes.mockResolvedValue(iv as CsprngArray); cryptoFunctionService.aesEncrypt.mockResolvedValue(cipherText); - const actual = await encryptService.encryptToBytes(plainValue, key); + const actual = await encryptService.encryptFileData(plainValue, key); const expectedBytes = new Uint8Array(1 + iv.byteLength + cipherText.byteLength); expectedBytes.set([EncryptionType.AesCbc256_B64]); expectedBytes.set(iv, 1); @@ -274,7 +274,7 @@ describe("EncryptService", () => { cryptoFunctionService.aesEncrypt.mockResolvedValue(cipherText); cryptoFunctionService.hmac.mockResolvedValue(mac); - const actual = await encryptService.encryptToBytes(plainValue, key); + const actual = await encryptService.encryptFileData(plainValue, key); const expectedBytes = new Uint8Array( 1 + iv.byteLength + mac.byteLength + cipherText.byteLength, ); diff --git a/libs/common/src/tools/send/services/send.service.ts b/libs/common/src/tools/send/services/send.service.ts index 8d6e62e3b8c..b4120dfb035 100644 --- a/libs/common/src/tools/send/services/send.service.ts +++ b/libs/common/src/tools/send/services/send.service.ts @@ -330,7 +330,7 @@ export class SendService implements InternalSendServiceAbstraction { key = await this.keyService.getUserKey(); } const encFileName = await this.encryptService.encrypt(fileName, key); - const encFileData = await this.encryptService.encryptToBytes(new Uint8Array(data), key); + const encFileData = await this.encryptService.encryptFileData(new Uint8Array(data), key); return [encFileName, encFileData]; } diff --git a/libs/common/src/vault/services/cipher.service.spec.ts b/libs/common/src/vault/services/cipher.service.spec.ts index 57df5f2a376..18bead36e87 100644 --- a/libs/common/src/vault/services/cipher.service.spec.ts +++ b/libs/common/src/vault/services/cipher.service.spec.ts @@ -131,7 +131,7 @@ describe("Cipher Service", () => { let cipherObj: Cipher; beforeEach(() => { - encryptService.encryptToBytes.mockReturnValue(Promise.resolve(ENCRYPTED_BYTES)); + encryptService.encryptFileData.mockReturnValue(Promise.resolve(ENCRYPTED_BYTES)); encryptService.encrypt.mockReturnValue(Promise.resolve(new EncString(ENCRYPTED_TEXT))); (window as any).bitwardenContainerService = new ContainerService(keyService, encryptService); diff --git a/libs/common/src/vault/services/cipher.service.ts b/libs/common/src/vault/services/cipher.service.ts index 455be3babea..1ecf67a2354 100644 --- a/libs/common/src/vault/services/cipher.service.ts +++ b/libs/common/src/vault/services/cipher.service.ts @@ -898,7 +898,7 @@ export class CipherService implements CipherServiceAbstraction { const encFileName = await this.encryptService.encrypt(filename, cipherEncKey); const dataEncKey = await this.keyService.makeDataEncKey(cipherEncKey); - const encData = await this.encryptService.encryptToBytes(new Uint8Array(data), dataEncKey[0]); + const encData = await this.encryptService.encryptFileData(new Uint8Array(data), dataEncKey[0]); const response = await this.cipherFileUploadService.upload( cipher, @@ -1499,7 +1499,10 @@ export class CipherService implements CipherServiceAbstraction { const dataEncKey = await this.keyService.makeDataEncKey(encKey); const encFileName = await this.encryptService.encrypt(attachmentView.fileName, encKey); - const encData = await this.encryptService.encryptToBytes(new Uint8Array(decBuf), dataEncKey[0]); + const encData = await this.encryptService.encryptFileData( + new Uint8Array(decBuf), + dataEncKey[0], + ); const fd = new FormData(); try {