-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathdecrypt.test.ts
290 lines (266 loc) · 9.42 KB
/
decrypt.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/* eslint-env mocha */
import * as chai from 'chai'
// @ts-ignore
import chaiAsPromised from 'chai-as-promised'
import {
AlgorithmSuiteIdentifier,
CommitmentPolicy,
} from '@aws-crypto/material-management-node'
import { buildDecrypt } from '../src/index'
import * as fixtures from './fixtures'
chai.use(chaiAsPromised)
const { expect } = chai
// @ts-ignore
import from from 'from2'
const { decrypt } = buildDecrypt(CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT)
describe('decrypt', () => {
it('string with encoding', async () => {
const { plaintext: test, messageHeader } = await decrypt(
fixtures.decryptKeyring(),
fixtures.base64CiphertextAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384(),
{ encoding: 'base64' }
)
expect(messageHeader.suiteId).to.equal(
AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384
)
expect(messageHeader.encryptionContext).to.deep.equal(
fixtures.encryptionContext()
)
expect(test.toString('base64')).to.equal(fixtures.base64Plaintext())
})
it('buffer', async () => {
const { plaintext: test, messageHeader } = await decrypt(
fixtures.decryptKeyring(),
Buffer.from(
fixtures.base64CiphertextAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384(),
'base64'
)
)
expect(messageHeader.suiteId).to.equal(
AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384
)
expect(messageHeader.encryptionContext).to.deep.equal(
fixtures.encryptionContext()
)
expect(test.toString('base64')).to.equal(fixtures.base64Plaintext())
})
it('stream', async () => {
const ciphertext = Buffer.from(
fixtures.base64CiphertextAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384(),
'base64'
)
/* Pushing 1 byte at time is annoying.
* But there can be state that can get reset
* from a 1 byte push when the buffering
* is internal to the stream.
* So while 1 byte will hit the beginning
* boundary conditions,
* and the exiting boundary conditions,
* but it will never span any boundary conditions.
* The prime example of this is
* the `VerifyStream`.
* It has 4 byte boundary in the `_transform` function.
* The body header, the body, the auth tag, and signature.
* By sending 1 byte
* as the code transitions from the body header
* to the body
* the code handling the exiting
* of the body header has no way to interact
* with the body,
* because the 1 byte of data
* completely isolates these code branches.
* So I push at different sizes to hit
* as many boundary conditions as possible.
* Why 20 as a max?
* Permuting every possible combination
* would take too long
* and so anything short of _everything_
* is a compromise.
* 20 seems large enough.
* I did not do an initial offset
* because that just moves me closer
* to permuting every option.
* An alternative would be a variable chunk size.
* Doing this randomly is a bad idea.
* Tests that only fail _sometimes_
* are bad tests.
* It is too easy to try again,
* and when everything passes just move on.
* And again, doing every permutation
* is too expensive at this time.
*/
const results = await Promise.all(
[
{ size: 1 },
{ size: 2 },
{ size: 3 },
{ size: 4 },
{ size: 5 },
{ size: 6 },
{ size: 7 },
{ size: 8 },
{ size: 9 },
{ size: 10 },
{ size: 11 },
{ size: 12 },
{ size: 13 },
{ size: 14 },
{ size: 15 },
{ size: 16 },
{ size: 17 },
{ size: 18 },
{ size: 19 },
{ size: 20 },
].map(async (op) =>
decrypt(
fixtures.decryptKeyring(),
chunkCipherTextStream(ciphertext, op)
)
)
)
results.map(({ plaintext: test, messageHeader }) => {
expect(messageHeader.suiteId).to.equal(
AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384
)
expect(messageHeader.encryptionContext).to.deep.equal(
fixtures.encryptionContext()
)
expect(test.toString('base64')).to.equal(fixtures.base64Plaintext())
})
})
it('Precondition: The sequence number is required to monotonically increase, starting from 1.', async () => {
return expect(
decrypt(fixtures.decryptKeyring(), fixtures.frameSequenceOutOfOrder(), {
encoding: 'base64',
})
).to.rejectedWith(Error, 'Encrypted body sequence out of order.')
})
it('Postcondition: The signature must be valid.', async () => {
await expect(
decrypt(
fixtures.decryptKeyring(),
fixtures.invalidSignatureCiphertextAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384(),
{ encoding: 'base64' }
)
).to.rejectedWith(Error, 'Invalid Signature')
})
it('can decrypt maxBodySize message with a single final frame.', async () => {
const { plaintext: test } = await decrypt(
fixtures.decryptKeyring(),
fixtures.base64Ciphertext4BytesWith4KFrameLength(),
{ encoding: 'base64', maxBodySize: 4 }
)
expect(test).to.deep.equal(Buffer.from('asdf'))
})
it('will not decrypt data that exceeds maxBodySize.', async () => {
return expect(
decrypt(
fixtures.decryptKeyring(),
fixtures.base64Ciphertext4BytesWith4KFrameLength(),
{ encoding: 'base64', maxBodySize: 3 }
)
).to.rejectedWith(Error, 'maxBodySize exceeded.')
})
it('can decrypt data with less than maxEncryptedDataKeys', async () => {
const { decrypt } = buildDecrypt({
commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
maxEncryptedDataKeys: 3,
})
const { plaintext } = await decrypt(
fixtures.decryptKeyring(),
fixtures.twoEdksMessage(),
{ encoding: 'base64' }
)
expect(plaintext).to.deep.equal(Buffer.from('asdf'))
})
it('can decrypt data with exactly maxEncryptedDataKeys', async () => {
const { decrypt } = buildDecrypt({
commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
maxEncryptedDataKeys: 3,
})
const { plaintext } = await decrypt(
fixtures.decryptKeyring(),
fixtures.threeEdksMessage(),
{ encoding: 'base64' }
)
expect(plaintext).to.deep.equal(Buffer.from('asdf'))
})
it('will not decrypt data with more than maxEncryptedDataKeys', async () => {
const { decrypt } = buildDecrypt({
commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
maxEncryptedDataKeys: 3,
})
await expect(
decrypt(fixtures.decryptKeyring(), fixtures.fourEdksMessage(), {
encoding: 'base64',
})
).to.rejectedWith(Error, 'maxEncryptedDataKeys exceeded.')
})
it('will fail to decrypt ciphertext with high utf8 codepoints and no utf8 sorting on keyring', async () => {
const { decrypt } = buildDecrypt({
commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
maxEncryptedDataKeys: 3,
})
const hkeyring = fixtures.hKeyring(false)
const ciphertext = fixtures.hierarchyMessageWithHighUtf8CodePoints()
await expect(
decrypt(hkeyring, ciphertext, {
encoding: 'base64',
})
).to.rejectedWith(Error, 'Unsupported state or unable to authenticate data')
})
it('will decrypt ciphertext with high utf8 codepoints with a keyring configured to do utf8 sorting', async () => {
const { decrypt } = buildDecrypt({
commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
maxEncryptedDataKeys: 3,
})
const hkeyring = fixtures.hKeyring(true)
const ciphertext = fixtures.hierarchyMessageWithHighUtf8CodePoints()
const { plaintext } = await decrypt(hkeyring, ciphertext, {
encoding: 'base64',
})
expect(plaintext).to.deep.equal(Buffer.from('Hello World'))
})
it('will decrypt ciphertext with high utf8 codepoints with a multikeyring with both utf8 and no utf8 sorting', async () => {
const { decrypt } = buildDecrypt({
commitmentPolicy: CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT,
maxEncryptedDataKeys: 3,
})
const hkeyringUtf8 = fixtures.hKeyring(true)
const hkeyringNoUtf8 = fixtures.hKeyring(false)
const multikeyring = fixtures.multiKeyring(hkeyringUtf8, hkeyringNoUtf8)
const ciphertext = fixtures.hierarchyMessageWithHighUtf8CodePoints()
const { plaintext } = await decrypt(multikeyring, ciphertext, {
encoding: 'base64',
})
expect(plaintext).to.deep.equal(Buffer.from('Hello World'))
})
})
function chunkCipherTextStream(ciphertext: Buffer, { size }: { size: number }) {
const i = ciphertext.values()
return from(
(
_: number,
next: (err: Error | null, chunk: Uint8Array | null) => void
) => {
const { value, done } = eat(i, size)
if (done) return queueMicrotask(() => next(null, null))
queueMicrotask(() => next(null, new Uint8Array(value)))
}
)
function eat(i: IterableIterator<number>, size: number) {
return Array(size)
.fill(1)
.reduce<IteratorResult<number[], any>>(
({ value }) => {
const { value: item, done } = i.next()
if (done && value.length) return { value, done: false }
if (!done) value.push(item)
return { value, done }
},
{ value: <number[]>[], done: false }
)
}
}