-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathweb_crypto_algorithms.ts
338 lines (321 loc) · 11.5 KB
/
web_crypto_algorithms.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/*
* This file contains information about particular algorithm suites used
* within the encryption SDK. In most cases, end-users don't need to
* manipulate this structure, but it can occasionally be needed for more
* advanced use cases, such as writing keyrings.
*
* These are the WebCrypto specific values the AWS Encryption SDK for JavaScript
* Algorithm Suites.
*/
import {
AlgorithmSuite,
AlgorithmSuiteIdentifier,
WebCryptoEncryption,
WebCryptoHash,
WebCryptoECDHCurve,
AlgorithmSuiteTypeWebCrypto,
MessageFormat,
Commitment,
AesGcm,
AlgBasic,
AlgKdf,
AlgKdfSigned,
AlgCommitted,
AlgCommittedSigned,
} from './algorithm_suites'
import { needs } from './needs'
interface WebCryptoAesGcm extends AesGcm {
encryption: WebCryptoEncryption
}
interface WebCryptoAlgBasic extends AlgBasic {
encryption: WebCryptoEncryption
}
interface WebCryptoAlgKdf extends AlgKdf {
encryption: WebCryptoEncryption
kdfHash: WebCryptoHash
}
interface WebCryptoAlgKdfSigned extends AlgKdfSigned {
encryption: WebCryptoEncryption
kdfHash: WebCryptoHash
signatureCurve: WebCryptoECDHCurve
signatureHash: WebCryptoHash
}
interface WebCryptoAlgCommitted extends AlgCommitted {
encryption: WebCryptoEncryption
kdfHash: WebCryptoHash
commitmentHash: WebCryptoHash
}
interface WebCryptoAlgCommittedSigned extends AlgCommittedSigned {
encryption: WebCryptoEncryption
kdfHash: WebCryptoHash
signatureCurve: WebCryptoECDHCurve
signatureHash: WebCryptoHash
}
type WebCryptoAlgUnion = Readonly<
| WebCryptoAlgBasic
| WebCryptoAlgKdf
| WebCryptoAlgKdfSigned
| WebCryptoAlgCommitted
| WebCryptoAlgCommittedSigned
>
type WebCryptoAlgorithmSuiteValues = WebCryptoAesGcm &
Partial<Omit<AlgBasic, keyof WebCryptoAesGcm>> &
Partial<Omit<AlgKdf, keyof WebCryptoAesGcm>> &
Partial<Omit<AlgKdfSigned, keyof WebCryptoAesGcm>> &
Partial<Omit<AlgCommitted, keyof WebCryptoAesGcm>> &
Partial<Omit<AlgCommittedSigned, keyof WebCryptoAesGcm>>
/* References to https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/algorithms-reference.html
* These are the composed parameters for each algorithm suite specification for
* for the WebCrypto environment.
*/
const webCryptoAlgAes128GcmIv12Tag16: WebCryptoAlgBasic = {
id: AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16,
messageFormat: MessageFormat.V1,
encryption: 'AES-GCM',
keyLength: 128,
ivLength: 12,
tagLength: 128,
cacheSafe: false,
commitment: 'NONE',
}
/* Web browsers do not support 192 bit key lengths at this time. */
const webCryptoAlgAes192GcmIv12Tag16: WebCryptoAlgBasic = {
id: AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16,
messageFormat: MessageFormat.V1,
encryption: 'AES-GCM',
keyLength: 192,
ivLength: 12,
tagLength: 128,
cacheSafe: false,
commitment: 'NONE',
}
const webCryptoAlgAes256GcmIv12Tag16: WebCryptoAlgBasic = {
id: AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16,
messageFormat: MessageFormat.V1,
encryption: 'AES-GCM',
keyLength: 256,
ivLength: 12,
tagLength: 128,
cacheSafe: false,
commitment: 'NONE',
}
const webCryptoAlgAes128GcmIv12Tag16HkdfSha256: WebCryptoAlgKdf = {
id: AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256,
messageFormat: MessageFormat.V1,
encryption: 'AES-GCM',
keyLength: 128,
ivLength: 12,
tagLength: 128,
kdf: 'HKDF',
kdfHash: 'SHA-256',
cacheSafe: true,
commitment: 'NONE',
}
/* Web browsers do not support 192 bit key lengths at this time. */
const webCryptoAlgAes192GcmIv12Tag16HkdfSha256: WebCryptoAlgKdf = {
id: AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16_HKDF_SHA256,
messageFormat: MessageFormat.V1,
encryption: 'AES-GCM',
keyLength: 192,
ivLength: 12,
tagLength: 128,
kdf: 'HKDF',
kdfHash: 'SHA-256',
cacheSafe: true,
commitment: 'NONE',
}
const webCryptoAlgAes256GcmIv12Tag16HkdfSha256: WebCryptoAlgKdf = {
id: AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256,
messageFormat: MessageFormat.V1,
encryption: 'AES-GCM',
keyLength: 256,
ivLength: 12,
tagLength: 128,
kdf: 'HKDF',
kdfHash: 'SHA-256',
cacheSafe: true,
commitment: 'NONE',
}
const webCryptoAlgAes128GcmIv12Tag16HkdfSha256EcdsaP256: WebCryptoAlgKdfSigned =
{
id: AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256,
messageFormat: MessageFormat.V1,
encryption: 'AES-GCM',
keyLength: 128,
ivLength: 12,
tagLength: 128,
kdf: 'HKDF',
kdfHash: 'SHA-256',
cacheSafe: true,
signatureCurve: 'P-256',
signatureHash: 'SHA-256',
commitment: 'NONE',
}
/* Web browsers do not support 192 bit key lengths at this time. */
const webCryptoAlgAes192GcmIv12Tag16HkdfSha384EcdsaP384: WebCryptoAlgKdfSigned =
{
id: AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
messageFormat: MessageFormat.V1,
encryption: 'AES-GCM',
keyLength: 192,
ivLength: 12,
tagLength: 128,
kdf: 'HKDF',
kdfHash: 'SHA-384',
cacheSafe: true,
signatureCurve: 'P-384',
signatureHash: 'SHA-384',
commitment: 'NONE',
}
const webCryptoAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384: WebCryptoAlgKdfSigned =
{
id: AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
messageFormat: MessageFormat.V1,
encryption: 'AES-GCM',
keyLength: 256,
ivLength: 12,
tagLength: 128,
kdf: 'HKDF',
kdfHash: 'SHA-384',
cacheSafe: true,
signatureCurve: 'P-384',
signatureHash: 'SHA-384',
commitment: 'NONE',
}
const webCryptoAlgAes256GcmHkdfSha512Committing: WebCryptoAlgCommitted = {
id: AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY,
messageFormat: MessageFormat.V2,
encryption: 'AES-GCM',
keyLength: 256,
ivLength: 12,
tagLength: 128,
kdf: 'HKDF',
kdfHash: 'SHA-512',
cacheSafe: true,
commitment: 'KEY',
commitmentHash: 'SHA-512',
suiteDataLength: 32,
commitmentLength: 256,
saltLengthBytes: 32,
}
const webCryptoAlgAes256GcmHkdfSha512CommittingEcdsaP384: WebCryptoAlgCommittedSigned =
{
id: AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
messageFormat: MessageFormat.V2,
encryption: 'AES-GCM',
keyLength: 256,
ivLength: 12,
tagLength: 128,
kdf: 'HKDF',
kdfHash: 'SHA-512',
cacheSafe: true,
signatureCurve: 'P-384',
signatureHash: 'SHA-384',
commitment: 'KEY',
commitmentHash: 'SHA-512',
suiteDataLength: 32,
commitmentLength: 256,
saltLengthBytes: 32,
}
type WebCryptoAlgorithms = Readonly<{
[id in AlgorithmSuiteIdentifier]: WebCryptoAlgUnion
}>
const webCryptoAlgorithms: WebCryptoAlgorithms = Object.freeze({
[AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16]: Object.freeze(
webCryptoAlgAes128GcmIv12Tag16
),
[AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16]: Object.freeze(
webCryptoAlgAes192GcmIv12Tag16
),
[AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16]: Object.freeze(
webCryptoAlgAes256GcmIv12Tag16
),
[AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256]:
Object.freeze(webCryptoAlgAes128GcmIv12Tag16HkdfSha256),
[AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16_HKDF_SHA256]:
Object.freeze(webCryptoAlgAes192GcmIv12Tag16HkdfSha256),
[AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256]:
Object.freeze(webCryptoAlgAes256GcmIv12Tag16HkdfSha256),
[AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256]:
Object.freeze(webCryptoAlgAes128GcmIv12Tag16HkdfSha256EcdsaP256),
[AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384]:
Object.freeze(webCryptoAlgAes192GcmIv12Tag16HkdfSha384EcdsaP384),
[AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384]:
Object.freeze(webCryptoAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384),
[AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY]:
Object.freeze(webCryptoAlgAes256GcmHkdfSha512Committing),
[AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384]:
Object.freeze(webCryptoAlgAes256GcmHkdfSha512CommittingEcdsaP384),
})
/* Web browsers do not support 192 bit key lengths at this time.
* To maintain type compatibility and TypeScript happiness between Algorithm Suites
* I need to have the same list of AlgorithmSuiteIdentifier.
* This list is maintained here to make sure that the error message is helpful.
*/
type WebCryptoAlgorithmSuiteIdentifier = Exclude<
Exclude<
Exclude<
AlgorithmSuiteIdentifier,
AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16
>,
AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16_HKDF_SHA256
>,
AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384
>
type SupportedWebCryptoAlgorithms = Readonly<{
[id in WebCryptoAlgorithmSuiteIdentifier]: WebCryptoAlgUnion
}>
const supportedWebCryptoAlgorithms: SupportedWebCryptoAlgorithms =
Object.freeze({
[AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16]: Object.freeze(
webCryptoAlgAes128GcmIv12Tag16
),
// [AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16]: Object.freeze(webCryptoAlgAes192GcmIv12Tag16),
[AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16]: Object.freeze(
webCryptoAlgAes256GcmIv12Tag16
),
[AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256]:
Object.freeze(webCryptoAlgAes128GcmIv12Tag16HkdfSha256),
// [AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16_HKDF_SHA256]: Object.freeze(webCryptoAlgAes192GcmIv12Tag16HkdfSha256),
[AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256]:
Object.freeze(webCryptoAlgAes256GcmIv12Tag16HkdfSha256),
[AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256]:
Object.freeze(webCryptoAlgAes128GcmIv12Tag16HkdfSha256EcdsaP256),
// [AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384]: Object.freeze(webCryptoAlgAes192GcmIv12Tag16HkdfSha384EcdsaP384),
[AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384]:
Object.freeze(webCryptoAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384),
[AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY]:
Object.freeze(webCryptoAlgAes256GcmHkdfSha512Committing),
[AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384]:
Object.freeze(webCryptoAlgAes256GcmHkdfSha512CommittingEcdsaP384),
})
export class WebCryptoAlgorithmSuite
extends AlgorithmSuite
implements WebCryptoAlgorithmSuiteValues
{
declare messageFormat: MessageFormat
declare encryption: WebCryptoEncryption
declare commitment: Commitment
declare kdfHash?: WebCryptoHash
declare signatureCurve?: WebCryptoECDHCurve
declare signatureHash?: WebCryptoHash
type: AlgorithmSuiteTypeWebCrypto = 'webCrypto'
declare commitmentHash?: WebCryptoHash
constructor(id: AlgorithmSuiteIdentifier) {
super(webCryptoAlgorithms[id])
/* Precondition: Browsers do not support 192 bit keys so the AlgorithmSuiteIdentifier is removed.
* This is primarily an error in decrypt but this make it clear.
* The error can manifest deep in the decrypt loop making it hard to debug.
*/
needs(
Object.prototype.hasOwnProperty.call(supportedWebCryptoAlgorithms, id),
'192-bit AES keys are not supported'
)
Object.setPrototypeOf(this, WebCryptoAlgorithmSuite.prototype)
Object.freeze(this)
}
}
Object.freeze(WebCryptoAlgorithmSuite.prototype)
Object.freeze(WebCryptoAlgorithmSuite)