@@ -11,10 +11,12 @@ import {
11
11
SymmetricCryptoKey ,
12
12
} from "@bitwarden/common/platform/models/domain/symmetric-crypto-key" ;
13
13
import { CsprngArray } from "@bitwarden/common/types/csprng" ;
14
+ import { PureCrypto } from "@bitwarden/sdk-internal" ;
14
15
15
16
import { makeStaticByteArray } from "../../../../spec" ;
16
17
import { DefaultFeatureFlagValue , FeatureFlag } from "../../../enums/feature-flag.enum" ;
17
18
import { ServerConfig } from "../../../platform/abstractions/config/server-config" ;
19
+ import { SdkLoadService } from "../../../platform/abstractions/sdk/sdk-load.service" ;
18
20
19
21
import { EncryptServiceImplementation } from "./encrypt.service.implementation" ;
20
22
@@ -343,6 +345,24 @@ describe("EncryptService", () => {
343
345
) ;
344
346
} ) ;
345
347
348
+ it ( "calls PureCrypto when useSDKForDecryption is true" , async ( ) => {
349
+ ( encryptService as any ) . useSDKForDecryption = true ;
350
+ const decryptedBytes = makeStaticByteArray ( 10 , 200 ) ;
351
+ Object . defineProperty ( SdkLoadService , "Ready" , {
352
+ value : Promise . resolve ( ) ,
353
+ configurable : true ,
354
+ } ) ;
355
+ jest . spyOn ( PureCrypto , "symmetric_decrypt_array_buffer" ) . mockReturnValue ( decryptedBytes ) ;
356
+
357
+ const actual = await encryptService . decryptToBytes ( encBuffer , key ) ;
358
+
359
+ expect ( PureCrypto . symmetric_decrypt_array_buffer ) . toHaveBeenCalledWith (
360
+ encBuffer . buffer ,
361
+ key . toEncoded ( ) ,
362
+ ) ;
363
+ expect ( actual ) . toEqualBuffer ( decryptedBytes ) ;
364
+ } ) ;
365
+
346
366
it ( "decrypts data with provided key for Aes256CbcHmac" , async ( ) => {
347
367
const decryptedBytes = makeStaticByteArray ( 10 , 200 ) ;
348
368
@@ -450,6 +470,25 @@ describe("EncryptService", () => {
450
470
) ;
451
471
} ) ;
452
472
473
+ it ( "calls PureCrypto when useSDKForDecryption is true" , async ( ) => {
474
+ ( encryptService as any ) . useSDKForDecryption = true ;
475
+ const key = new SymmetricCryptoKey ( makeStaticByteArray ( 64 , 0 ) ) ;
476
+ const encString = new EncString ( EncryptionType . AesCbc256_HmacSha256_B64 , "data" , "iv" , "mac" ) ;
477
+ Object . defineProperty ( SdkLoadService , "Ready" , {
478
+ value : Promise . resolve ( ) ,
479
+ configurable : true ,
480
+ } ) ;
481
+ jest . spyOn ( PureCrypto , "symmetric_decrypt" ) . mockReturnValue ( "data" ) ;
482
+
483
+ const actual = await encryptService . decryptToUtf8 ( encString , key ) ;
484
+
485
+ expect ( actual ) . toEqual ( "data" ) ;
486
+ expect ( PureCrypto . symmetric_decrypt ) . toHaveBeenCalledWith (
487
+ encString . encryptedString ,
488
+ key . toEncoded ( ) ,
489
+ ) ;
490
+ } ) ;
491
+
453
492
it ( "decrypts data with provided key for AesCbc256_HmacSha256" , async ( ) => {
454
493
const key = new SymmetricCryptoKey ( makeStaticByteArray ( 64 , 0 ) ) ;
455
494
const encString = new EncString ( EncryptionType . AesCbc256_HmacSha256_B64 , "data" , "iv" , "mac" ) ;
0 commit comments