44/* eslint-env mocha */
55
66import * as chai from 'chai'
7+ // @ts -ignore
78import chaiAsPromised from 'chai-as-promised'
89import { buildDecrypt } from '../src/index'
910import { _decrypt } from '../src/decrypt'
@@ -44,14 +45,34 @@ describe('decrypt', () => {
4445
4546 it ( 'Precondition: _decrypt needs a valid commitmentPolicy.' , async ( ) => {
4647 await expect (
47- _decrypt ( 'fake_policy' as any , { } as any , { } as any )
48+ _decrypt (
49+ { commitmentPolicy : 'fake_policy' as any , maxEncryptedDataKeys : false } ,
50+ { } as any ,
51+ { } as any
52+ )
4853 ) . to . rejectedWith ( Error , 'Invalid commitment policy.' )
4954 } )
5055
56+ it ( 'Precondition: _decrypt needs a valid maxEncryptedDataKeys.' , async ( ) => {
57+ await expect (
58+ _decrypt (
59+ {
60+ commitmentPolicy : CommitmentPolicy . REQUIRE_ENCRYPT_REQUIRE_DECRYPT ,
61+ maxEncryptedDataKeys : 0 ,
62+ } ,
63+ { } as any ,
64+ { } as any
65+ )
66+ ) . to . rejectedWith ( Error , 'Invalid maxEncryptedDataKeys value.' )
67+ } )
68+
5169 it ( 'Precondition: The parsed header algorithmSuite in _decrypt must be supported by the commitmentPolicy.' , async ( ) => {
5270 await expect (
5371 _decrypt (
54- CommitmentPolicy . REQUIRE_ENCRYPT_REQUIRE_DECRYPT ,
72+ {
73+ commitmentPolicy : CommitmentPolicy . REQUIRE_ENCRYPT_REQUIRE_DECRYPT ,
74+ maxEncryptedDataKeys : false ,
75+ } ,
5576 fixtures . decryptKeyring ( ) ,
5677 fixtures . base64CiphertextAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384With4Frames ( )
5778 )
@@ -74,7 +95,10 @@ describe('decrypt', () => {
7495 } as any
7596 await expect (
7697 _decrypt (
77- CommitmentPolicy . REQUIRE_ENCRYPT_REQUIRE_DECRYPT ,
98+ {
99+ commitmentPolicy : CommitmentPolicy . REQUIRE_ENCRYPT_REQUIRE_DECRYPT ,
100+ maxEncryptedDataKeys : false ,
101+ } ,
78102 cmm ,
79103 fromBase64 ( fixtures . compatibilityVectors ( ) . tests [ 0 ] . ciphertext )
80104 )
@@ -142,6 +166,40 @@ describe('decrypt', () => {
142166 await expect ( decrypt ( keyring , data . slice ( 0 , i ) ) ) . to . rejectedWith ( Error )
143167 }
144168 } )
169+
170+ it ( 'can decrypt data with less than maxEncryptedDataKeys' , async ( ) => {
171+ const { decrypt } = buildDecrypt ( {
172+ commitmentPolicy : CommitmentPolicy . FORBID_ENCRYPT_ALLOW_DECRYPT ,
173+ maxEncryptedDataKeys : 3 ,
174+ } )
175+ const { plaintext } = await decrypt (
176+ fixtures . decryptKeyring ( ) ,
177+ fixtures . twoEdksMessage ( )
178+ )
179+ expect ( plaintext ) . to . deep . equal ( Buffer . from ( 'asdf' ) )
180+ } )
181+
182+ it ( 'can decrypt data with exactly maxEncryptedDataKeys' , async ( ) => {
183+ const { decrypt } = buildDecrypt ( {
184+ commitmentPolicy : CommitmentPolicy . FORBID_ENCRYPT_ALLOW_DECRYPT ,
185+ maxEncryptedDataKeys : 3 ,
186+ } )
187+ const { plaintext } = await decrypt (
188+ fixtures . decryptKeyring ( ) ,
189+ fixtures . threeEdksMessage ( )
190+ )
191+ expect ( plaintext ) . to . deep . equal ( Buffer . from ( 'asdf' ) )
192+ } )
193+
194+ it ( 'will not decrypt data with more than maxEncryptedDataKeys' , async ( ) => {
195+ const { decrypt } = buildDecrypt ( {
196+ commitmentPolicy : CommitmentPolicy . FORBID_ENCRYPT_ALLOW_DECRYPT ,
197+ maxEncryptedDataKeys : 3 ,
198+ } )
199+ await expect (
200+ decrypt ( fixtures . decryptKeyring ( ) , fixtures . fourEdksMessage ( ) )
201+ ) . to . rejectedWith ( Error , 'maxEncryptedDataKeys exceeded.' )
202+ } )
145203} )
146204
147205// prettier-ignore
0 commit comments