-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathencrypt.ts
212 lines (182 loc) · 6.81 KB
/
encrypt.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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
WebCryptoAlgorithmSuite,
WebCryptoDefaultCryptographicMaterialsManager,
WebCryptoEncryptionRequest,
EncryptionContext,
AlgorithmSuiteIdentifier,
getEncryptHelper,
KeyringWebCrypto,
needs,
WebCryptoMaterialsManager,
CommitmentPolicy,
CommitmentPolicySuites,
MessageFormat,
ClientOptions,
} from '@aws-crypto/material-management-browser'
import {
serializeFactory,
aadFactory,
concatBuffers,
MessageHeader,
serializeSignatureInfo,
FRAME_LENGTH,
raw2der,
Maximum,
MessageIdLength,
serializeMessageHeaderAuth,
} from '@aws-crypto/serialize'
import { fromUtf8 } from '@aws-sdk/util-utf8-browser'
import { getWebCryptoBackend } from '@aws-crypto/web-crypto-backend'
const serialize = serializeFactory(fromUtf8, { utf8Sorting: true })
const { messageAADContentString, messageAAD } = aadFactory(fromUtf8)
export interface EncryptInput {
suiteId?: AlgorithmSuiteIdentifier
encryptionContext?: EncryptionContext
frameLength?: number
// plaintextLength?: number // Subtle Crypto functions are all one-shot, so frames and length are === plaintext.byteLength
}
export interface EncryptResult {
messageHeader: MessageHeader
result: Uint8Array
}
export async function _encrypt(
{ commitmentPolicy, maxEncryptedDataKeys }: ClientOptions,
cmm: KeyringWebCrypto | WebCryptoMaterialsManager,
plaintext: Uint8Array,
{
suiteId,
encryptionContext = {},
frameLength = FRAME_LENGTH,
}: EncryptInput = {}
): Promise<EncryptResult> {
/* Precondition: _encrypt needs a valid commitmentPolicy. */
needs(CommitmentPolicy[commitmentPolicy], 'Invalid commitment policy.')
// buildEncrypt defaults this to false for backwards compatibility, so this is satisfied
/* Precondition: _encrypt needs a valid maxEncryptedDataKeys. */
needs(
maxEncryptedDataKeys === false || maxEncryptedDataKeys >= 1,
'Invalid maxEncryptedDataKeys value.'
)
/* Precondition: The frameLength must be less than the maximum frame size for browser encryption. */
needs(
frameLength > 0 && Maximum.FRAME_SIZE >= frameLength,
`frameLength out of bounds: 0 > frameLength >= ${Maximum.FRAME_SIZE}`
)
const backend = await getWebCryptoBackend()
if (!backend) throw new Error('No supported crypto backend')
/* If the cmm is a Keyring, wrap it with WebCryptoDefaultCryptographicMaterialsManager. */
cmm =
cmm instanceof KeyringWebCrypto
? new WebCryptoDefaultCryptographicMaterialsManager(cmm)
: cmm
// Subtle Crypto functions are all one-shot so all the plaintext needs to be available.
const plaintextLength = plaintext.byteLength
const suite = suiteId && new WebCryptoAlgorithmSuite(suiteId)
/* Precondition: Only request WebCryptoEncryptionMaterial for algorithm suites supported in commitmentPolicy. */
CommitmentPolicySuites.isEncryptEnabled(commitmentPolicy, suite)
const encryptionRequest: WebCryptoEncryptionRequest = {
suite,
encryptionContext,
plaintextLength,
commitmentPolicy,
}
const material = await cmm.getEncryptionMaterials(encryptionRequest)
/* Precondition: Only use WebCryptoEncryptionMaterial for algorithm suites supported in commitmentPolicy. */
CommitmentPolicySuites.isEncryptEnabled(commitmentPolicy, material.suite)
/* Precondition: _encrypt encryption materials must not exceed maxEncryptedDataKeys */
needs(
maxEncryptedDataKeys === false ||
material.encryptedDataKeys.length <= maxEncryptedDataKeys,
'maxEncryptedDataKeys exceeded.'
)
const { getEncryptInfo, subtleSign, dispose } = await getEncryptHelper(
material
)
const versionString = MessageFormat[material.suite.messageFormat] as any
const messageIdLength = parseInt(MessageIdLength[versionString], 10)
/* Precondition UNTESTED: WebCrypto suites must result is some messageIdLength. */
needs(messageIdLength > 0, 'Algorithm suite has unknown message format.')
const messageId = await backend.randomValues(messageIdLength)
const { ivLength } = material.suite
const { getSubtleEncrypt, keyCommitment } = await getEncryptInfo(messageId)
const messageHeader = serialize.buildMessageHeader({
suite: material.suite,
encryptedDataKeys: material.encryptedDataKeys,
encryptionContext: material.encryptionContext,
messageId,
frameLength,
suiteData: keyCommitment,
})
const header = serialize.serializeMessageHeader(messageHeader)
const headerIv = serialize.headerAuthIv(ivLength)
const headerAuthTag = new Uint8Array(
await getSubtleEncrypt(headerIv, header)(new Uint8Array(0))
)
// In the case of plaintextLength == 0 there still needs to be 1 frame.
const numberOfFrames = Math.max(Math.ceil(plaintextLength / frameLength), 1)
/* The final frame has a variable length.
* The value needs to be known, but should only be calculated once.
* So I calculate how much of a frame I should have at the end.
* This value will NEVER be larger than the frameLength.
*/
const finalFrameLength = plaintextLength - (numberOfFrames - 1) * frameLength
const bodyContent = []
for (
let sequenceNumber = 1;
numberOfFrames >= sequenceNumber;
sequenceNumber += 1
) {
const frameIv = serialize.frameIv(ivLength, sequenceNumber)
const isFinalFrame = sequenceNumber === numberOfFrames
const frameHeader = isFinalFrame
? serialize.finalFrameHeader(sequenceNumber, frameIv, finalFrameLength)
: serialize.frameHeader(sequenceNumber, frameIv)
const contentString = messageAADContentString({
contentType: messageHeader.contentType,
isFinalFrame,
})
const messageAdditionalData = messageAAD(
messageId,
contentString,
sequenceNumber,
isFinalFrame ? finalFrameLength : frameLength
)
/* Slicing an ArrayBuffer in a browser is suboptimal.
* It makes a copy.s
* So I just make a new view for the length of the frame.
*/
const framePlaintext = new Uint8Array(
plaintext.buffer,
(sequenceNumber - 1) * frameLength,
isFinalFrame ? finalFrameLength : frameLength
)
const cipherBufferAndAuthTag = await getSubtleEncrypt(
frameIv,
messageAdditionalData
)(framePlaintext)
bodyContent.push(frameHeader, cipherBufferAndAuthTag)
}
const result = concatBuffers(
header,
serializeMessageHeaderAuth({
headerIv,
headerAuthTag,
messageHeader,
}),
...bodyContent
)
dispose()
if (typeof subtleSign === 'function') {
const signatureArrayBuffer = await subtleSign(result)
const derSignature = raw2der(
new Uint8Array(signatureArrayBuffer),
material.suite
)
const signatureInfo = serializeSignatureInfo(derSignature)
return { result: concatBuffers(result, signatureInfo), messageHeader }
} else {
return { result: result, messageHeader }
}
}