@@ -97,12 +97,24 @@ describe('decorateCryptographicMaterial', () => {
97
97
expect ( ( ) => test . unencryptedDataKeyLength ) . to . throw ( )
98
98
} )
99
99
100
- it ( 'Precondition: If the unencryptedDataKey has not been set, it should not be settable later.' , ( ) => {
100
+ it ( 'Precondition: the unencryptedDataKey must not be Zeroed out.' , ( ) => {
101
+ const suite = new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16 )
102
+ const test = decorateCryptographicMaterial ( ( < any > { suite, keyringTrace : [ ] } ) , KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY )
103
+ const dataKey = new Uint8Array ( suite . keyLengthBytes ) . fill ( 1 )
104
+ const trace = { keyNamespace : 'k' , keyName : 'k' , flags : KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY }
105
+ test . setUnencryptedDataKey ( dataKey , trace )
106
+ test . zeroUnencryptedDataKey ( )
107
+ expect ( ( ) => test . unencryptedDataKeyLength ) . to . throw ( 'unencryptedDataKey has been zeroed.' )
108
+ } )
109
+
110
+ it ( `Precondition: If the unencryptedDataKey has not been set, it should not be settable later.
111
+ Precondition: If the udkForVerification has not been set, it should not be settable later.` , ( ) => {
101
112
const suite = new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16 )
102
113
const test = decorateCryptographicMaterial ( ( < any > { suite, keyringTrace : [ ] } ) , KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY )
103
114
test . zeroUnencryptedDataKey ( )
104
115
const dataKey = new Uint8Array ( suite . keyLengthBytes ) . fill ( 1 )
105
116
const trace = { keyNamespace : 'k' , keyName : 'k' , flags : KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY }
117
+ // It is very hard to test this perfectly. However, this tests the spirit.
106
118
expect ( ( ) => test . setUnencryptedDataKey ( dataKey , trace ) ) . to . throw ( )
107
119
} )
108
120
@@ -116,8 +128,32 @@ describe('decorateCryptographicMaterial', () => {
116
128
const suite = new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16 )
117
129
const test = decorateCryptographicMaterial ( ( < any > { suite, keyringTrace : [ ] } ) , KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY )
118
130
const dataKey = new Uint8Array ( suite . keyLengthBytes ) . fill ( 1 )
119
- test . setUnencryptedDataKey ( dataKey , { keyNamespace : 'k' , keyName : 'k' , flags : KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY } )
120
- expect ( ( ) => test . setUnencryptedDataKey ( dataKey ) ) . to . throw ( )
131
+ const trace = { keyNamespace : 'k' , keyName : 'k' , flags : KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY }
132
+ test . setUnencryptedDataKey ( dataKey , trace )
133
+ expect ( ( ) => test . setUnencryptedDataKey ( dataKey , trace ) ) . to . throw ( 'unencryptedDataKey has already been set' )
134
+ } )
135
+
136
+ it ( 'Precondition: dataKey should have an ArrayBuffer that *only* stores the key.' , ( ) => {
137
+ const suite = new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16 )
138
+ const test = decorateCryptographicMaterial ( ( < any > { suite, keyringTrace : [ ] } ) , KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY )
139
+ const dataKey = new Uint8Array ( new ArrayBuffer ( suite . keyLengthBytes + 10 ) , 5 , suite . keyLengthBytes ) . fill ( 1 )
140
+ const trace = { keyNamespace : 'k' , keyName : 'k' , flags : KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY }
141
+ expect ( ( ) => test . setUnencryptedDataKey ( dataKey , trace ) ) . to . throw ( 'Unencrypted Master Key must be an isolated buffer.' )
142
+ } )
143
+
144
+ it ( 'Precondition: Trace must be set, and the flag must indicate that the data key was generated.' , ( ) => {
145
+ const suite = new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16 )
146
+ const test = decorateCryptographicMaterial ( ( < any > { suite, keyringTrace : [ ] } ) , KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY )
147
+ const dataKey = new Uint8Array ( suite . keyLengthBytes ) . fill ( 1 )
148
+ expect ( ( ) => test . setUnencryptedDataKey ( dataKey , { } as any ) ) . to . throw ( 'Malformed KeyringTrace' )
149
+ } )
150
+
151
+ it ( 'Precondition: On set the required KeyringTraceFlag must be set.' , ( ) => {
152
+ const suite = new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16 )
153
+ const test = decorateCryptographicMaterial ( ( < any > { suite, keyringTrace : [ ] } ) , KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY )
154
+ const dataKey = new Uint8Array ( suite . keyLengthBytes ) . fill ( 1 )
155
+ const trace = { keyNamespace : 'k' , keyName : 'k' , flags : KeyringTraceFlag . WRAPPING_KEY_SIGNED_ENC_CTX }
156
+ expect ( ( ) => test . setUnencryptedDataKey ( dataKey , trace ) ) . to . throw ( 'Required KeyringTraceFlag not set' )
121
157
} )
122
158
123
159
it ( 'Precondition: The unencryptedDataKey must not have been modified.' , ( ) => {
@@ -172,6 +208,13 @@ describe('decorateEncryptionMaterial', () => {
172
208
expect ( ( ) => test . addEncryptedDataKey ( edk , KeyringTraceFlag . WRAPPING_KEY_ENCRYPTED_DATA_KEY ) ) . to . throw ( )
173
209
} )
174
210
211
+ it ( 'Precondition: flags must indicate that the key was encrypted.' , ( ) => {
212
+ const suite = new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16 )
213
+ const test : any = decorateEncryptionMaterial ( ( < any > { suite, keyringTrace : [ ] , hasUnencryptedDataKey : true } ) )
214
+ const edk = new EncryptedDataKey ( { providerId : 'p' , providerInfo : 'p' , encryptedDataKey : new Uint8Array ( 3 ) } )
215
+ expect ( ( ) => test . addEncryptedDataKey ( edk , KeyringTraceFlag . WRAPPING_KEY_VERIFIED_ENC_CTX ) ) . to . throw ( 'Encrypted data key flag must be set.' )
216
+ } )
217
+
175
218
it ( 'Precondition: The SignatureKey stored must agree with the algorithm specification.' , ( ) => {
176
219
const suiteWithSig = new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384 )
177
220
const suite = new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16 )
@@ -309,6 +352,27 @@ describe('decorateWebCryptoMaterial', () => {
309
352
expect ( ( ) => test . setCryptoKey ( key2 , trace ) ) . to . throw ( )
310
353
} )
311
354
355
+ it ( 'Precondition: If the CryptoKey is the only version, the trace information must be set here.' , ( ) => {
356
+ const suite = new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256 )
357
+ const test : any = decorateWebCryptoMaterial ( ( < any > { suite, validUsages : [ 'deriveKey' ] , keyringTrace : [ ] } ) , KeyringTraceFlag . WRAPPING_KEY_DECRYPTED_DATA_KEY )
358
+ decorateCryptographicMaterial ( test , KeyringTraceFlag . WRAPPING_KEY_DECRYPTED_DATA_KEY )
359
+
360
+ const key : any = { type : 'secret' , algorithm : { name : 'HKDF' } , usages : [ 'deriveKey' ] , extractable : false }
361
+ expect ( ( ) => test . setCryptoKey ( key , { keyNamespace : 'k' , flags : KeyringTraceFlag . WRAPPING_KEY_DECRYPTED_DATA_KEY } ) ) . to . throw ( 'Malformed KeyringTrace' )
362
+ expect ( ( ) => test . setCryptoKey ( key , { keyName : 'k' , flags : KeyringTraceFlag . WRAPPING_KEY_DECRYPTED_DATA_KEY } ) ) . to . throw ( 'Malformed KeyringTrace' )
363
+ expect ( ( ) => test . setCryptoKey ( key ) ) . to . throw ( 'Malformed KeyringTrace' )
364
+ } )
365
+
366
+ it ( 'Precondition: On setting the CryptoKey the required KeyringTraceFlag must be set.' , ( ) => {
367
+ const suite = new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256 )
368
+ const test : any = decorateWebCryptoMaterial ( ( < any > { suite, validUsages : [ 'deriveKey' ] , keyringTrace : [ ] } ) , KeyringTraceFlag . WRAPPING_KEY_DECRYPTED_DATA_KEY )
369
+ decorateCryptographicMaterial ( test , KeyringTraceFlag . WRAPPING_KEY_DECRYPTED_DATA_KEY )
370
+
371
+ const key : any = { type : 'secret' , algorithm : { name : 'HKDF' } , usages : [ 'deriveKey' ] , extractable : false }
372
+ const trace = { keyNamespace : 'k' , keyName : 'k' , flags : KeyringTraceFlag . WRAPPING_KEY_SIGNED_ENC_CTX }
373
+ expect ( ( ) => test . setCryptoKey ( key , trace ) ) . to . throw ( 'Required KeyringTraceFlag not set' )
374
+ } )
375
+
312
376
it ( 'Precondition: dataKey must be a supported type.' , ( ) => {
313
377
const test : any = decorateWebCryptoMaterial ( ( < any > { } ) , KeyringTraceFlag . WRAPPING_KEY_DECRYPTED_DATA_KEY )
314
378
const key : any = { }
0 commit comments