forked from aws/aws-encryption-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm_suites.ts
376 lines (353 loc) · 14.6 KB
/
algorithm_suites.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// 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.
*
* Here we describe the overall shape of the Algorithm Suites used by the AWS Encryption
* SDK for JavaScript. Specific details for Node.js and WebCrypto can be found
* in the respective files
*/
import { immutableClass, readOnlyProperty } from './immutable_class'
import { needs } from './needs'
/* References to https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/algorithms-reference.html
* These define the possible parameters for algorithm specifications that correspond
* to the Node.js or WebCrypto environment.
* These parameters are composed into an algorithm suite specification for each
* environment in the respective files.
*/
export enum AlgorithmSuiteIdentifier {
'ALG_AES128_GCM_IV12_TAG16' = 0x0014,
'ALG_AES192_GCM_IV12_TAG16' = 0x0046,
'ALG_AES256_GCM_IV12_TAG16' = 0x0078,
'ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256' = 0x0114,
'ALG_AES192_GCM_IV12_TAG16_HKDF_SHA256' = 0x0146,
'ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256' = 0x0178,
'ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256' = 0x0214,
'ALG_AES192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384' = 0x0346,
'ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384' = 0x0378,
'ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY' = 0x0478,
'ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384' = 0x0578,
}
Object.freeze(AlgorithmSuiteIdentifier)
export enum CommitmentPolicy {
'FORBID_ENCRYPT_ALLOW_DECRYPT' = 'FORBID_ENCRYPT_ALLOW_DECRYPT',
'REQUIRE_ENCRYPT_ALLOW_DECRYPT' = 'REQUIRE_ENCRYPT_ALLOW_DECRYPT',
'REQUIRE_ENCRYPT_REQUIRE_DECRYPT' = 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT',
}
Object.freeze(CommitmentPolicy)
export enum SignaturePolicy {
'ALLOW_ENCRYPT_ALLOW_DECRYPT' = 'ALLOW_ENCRYPT_ALLOW_DECRYPT',
'ALLOW_ENCRYPT_FORBID_DECRYPT' = 'ALLOW_ENCRYPT_FORBID_DECRYPT',
}
Object.freeze(SignaturePolicy)
/* Typescript enums are useful, but tricky.
* I have to use Declaration Merging
* to make everything work.
* First I pluck off the elements I do not want,
* from AlgorithmSuiteIdentifier.
* The rest param then includes "everything else".
* I make sure to pull both the enum name and value,
* because typescript enums compile to lookup in both directions.
* This gives us the compile side of the enum.
* It does *not* have the name or id of
* any unsupported suites.
* This means that on encrypt,
* if a customer is passing a literal value,
* if will fail the check.
* e.g.
* needs(!AlgorithmSuiteIdentifier[0x0014],
* 'wait, this is not supported for encrypt')
*
* All this resolves the object
* that is emitted by TypeScript.
* But the "type side" still needs to work.
* e.g.
* const id: AlgorithmSuiteIdentifier = NonCommittingAlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY
* Otherwise when passing these values around
* from the new enums they will not behave
* like values in the parent enum.
*
*/
export type NonSigningAlgorithmSuiteIdentifier = Exclude<
AlgorithmSuiteIdentifier,
| typeof AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256
| typeof AlgorithmSuiteIdentifier.ALG_AES192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384
| typeof AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384
| typeof AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384
>
export const NonSigningAlgorithmSuiteIdentifier = (() => {
const {
ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256,
ALG_AES192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
// Both the name side above, and the id side below
[0x0214]: NAME_ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256,
[0x0346]: NAME_ALG_AES192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
[0x0378]: NAME_ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
[0x0578]: NAME_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
...NonSigningAlgorithmSuiteIdentifier
} = AlgorithmSuiteIdentifier
return NonSigningAlgorithmSuiteIdentifier
})()
export const SignaturePolicySuites = Object.freeze({
isDecryptEnabled(
signaturePolicy: SignaturePolicy,
suite: AlgorithmSuiteIdentifier | AlgorithmSuite,
messageId: string
): void {
const id = (suite as AlgorithmSuite).id || suite
const name = (suite as AlgorithmSuite).name || AlgorithmSuiteIdentifier[id]
let decryption_client_name = 'decryptStream'
let signature_description = 'signed'
if (signaturePolicy === SignaturePolicy.ALLOW_ENCRYPT_FORBID_DECRYPT) {
decryption_client_name = 'decryptUnsignedMessageStream'
signature_description = 'un-signed'
}
/* Precondition: Only handle DecryptionMaterial for algorithm suites supported in signaturePolicy. */
needs(
this[signaturePolicy].decryptEnabledSuites[id],
`Configuration conflict. ` +
`Cannot process message with ID ${messageId} ` +
`due to client method ${decryption_client_name} ` +
`requiring only ${signature_description} messages. ` +
`Algorithm ID was ${name}. ` +
`See: https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#digital-sigs`
)
},
[SignaturePolicy.ALLOW_ENCRYPT_ALLOW_DECRYPT]: Object.freeze({
decryptEnabledSuites: AlgorithmSuiteIdentifier,
defaultAlgorithmSuite:
AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
}),
[SignaturePolicy.ALLOW_ENCRYPT_FORBID_DECRYPT]: Object.freeze({
decryptEnabledSuites: NonSigningAlgorithmSuiteIdentifier,
defaultAlgorithmSuite:
AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY,
}),
})
export type NonCommittingAlgorithmSuiteIdentifier = Exclude<
AlgorithmSuiteIdentifier,
| typeof AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY
| typeof AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384
>
export const NonCommittingAlgorithmSuiteIdentifier = (() => {
const {
ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY,
ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
// Both the name side above, and the id side below
[0x0478]: NAME_ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY,
[0x0578]: NAME_ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
...NonCommittingAlgorithmSuiteIdentifier
} = AlgorithmSuiteIdentifier
return NonCommittingAlgorithmSuiteIdentifier
})()
export type CommittingAlgorithmSuiteIdentifier = Extract<
AlgorithmSuiteIdentifier,
| typeof AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY
| typeof AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384
| (typeof AlgorithmSuiteIdentifier)[0x0478]
| (typeof AlgorithmSuiteIdentifier)[0x0578]
>
export const CommittingAlgorithmSuiteIdentifier = (() => {
const {
ALG_AES128_GCM_IV12_TAG16,
ALG_AES192_GCM_IV12_TAG16,
ALG_AES256_GCM_IV12_TAG16,
ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256,
ALG_AES192_GCM_IV12_TAG16_HKDF_SHA256,
ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256,
ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256,
ALG_AES192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
// Both the name side above, and the id side below
[0x0014]: NAME_ALG_AES128_GCM_IV12_TAG16,
[0x0046]: NAME_ALG_AES192_GCM_IV12_TAG16,
[0x0078]: NAME_ALG_AES256_GCM_IV12_TAG16,
[0x0114]: NAME_ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256,
[0x0146]: NAME_ALG_AES192_GCM_IV12_TAG16_HKDF_SHA256,
[0x0178]: NAME_ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256,
[0x0214]: NAME_ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256,
[0x0346]: NAME_ALG_AES192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
[0x0378]: NAME_ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
...CommittingAlgorithmSuiteIdentifier
} = AlgorithmSuiteIdentifier
return CommittingAlgorithmSuiteIdentifier
})()
export const CommitmentPolicySuites = Object.freeze({
isEncryptEnabled(
commitmentPolicy: CommitmentPolicy,
suite?: AlgorithmSuiteIdentifier | AlgorithmSuite
) {
if (!suite) return
const id = (suite as AlgorithmSuite).id || suite
const name = (suite as AlgorithmSuite).name || AlgorithmSuiteIdentifier[id]
/* Precondition: Only handle EncryptionMaterial for algorithm suites supported in commitmentPolicy. */
needs(
this[commitmentPolicy].encryptEnabledSuites[id],
`Configuration conflict. ` +
`Cannot encrypt due to CommitmentPolicy ${commitmentPolicy} ` +
`requiring only non-committed messages. ` +
`Algorithm ID was ${name}. ` +
`See: https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/troubleshooting-migration.html`
)
},
isDecryptEnabled(
commitmentPolicy: CommitmentPolicy,
suite: AlgorithmSuiteIdentifier | AlgorithmSuite,
messageId: string
) {
const id = (suite as AlgorithmSuite).id || suite
const name = (suite as AlgorithmSuite).name || AlgorithmSuiteIdentifier[id]
/* Precondition: Only handle DecryptionMaterial for algorithm suites supported in commitmentPolicy. */
needs(
this[commitmentPolicy].decryptEnabledSuites[id],
`Configuration conflict. ` +
`Cannot process message with ID ${messageId} ` +
`due to CommitmentPolicy ${commitmentPolicy} ` +
`requiring only committed messages. ` +
`Algorithm ID was ${name}. ` +
`See: https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/troubleshooting-migration.html`
)
},
[CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT]: Object.freeze({
encryptEnabledSuites: NonCommittingAlgorithmSuiteIdentifier,
decryptEnabledSuites: AlgorithmSuiteIdentifier,
defaultAlgorithmSuite:
NonCommittingAlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
}),
[CommitmentPolicy.REQUIRE_ENCRYPT_ALLOW_DECRYPT]: Object.freeze({
encryptEnabledSuites: CommittingAlgorithmSuiteIdentifier,
decryptEnabledSuites: AlgorithmSuiteIdentifier,
defaultAlgorithmSuite:
CommittingAlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
}),
[CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT]: Object.freeze({
encryptEnabledSuites: CommittingAlgorithmSuiteIdentifier,
decryptEnabledSuites: CommittingAlgorithmSuiteIdentifier,
defaultAlgorithmSuite:
CommittingAlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
}),
})
export type AlgorithmSuiteName = keyof typeof AlgorithmSuiteIdentifier
export type AlgorithmSuiteTypeNode = 'node'
export type AlgorithmSuiteTypeWebCrypto = 'webCrypto'
export type NodeEncryption = 'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm'
export type WebCryptoEncryption = 'AES-GCM'
export type KDF = 'HKDF'
export type Commitment = 'NONE' | 'KEY'
export type NodeHash = 'sha256' | 'sha384' | 'sha512'
export type WebCryptoHash = 'SHA-256' | 'SHA-384' | 'SHA-512'
export type NodeECDHCurve = 'prime256v1' | 'secp384r1'
export type WebCryptoECDHCurve = 'P-256' | 'P-384'
export type KeyLength = 128 | 192 | 256
export type CommitmentLength = 256
export type HKDFSaltLengthBytes = 32
export type IvLength = 12
export type TagLength = 128
export type SuiteDataLength = 32
export enum MessageFormat {
V1 = 0x01,
V2 = 0x02,
}
Object.freeze(MessageFormat)
export interface AesGcm {
id: AlgorithmSuiteIdentifier
messageFormat: MessageFormat
encryption: NodeEncryption | WebCryptoEncryption
keyLength: KeyLength
ivLength: IvLength
tagLength: TagLength
cacheSafe: boolean
kdf?: KDF
commitment: Commitment
}
export interface AlgBasic extends AesGcm {
messageFormat: MessageFormat.V1
cacheSafe: false
commitment: 'NONE'
}
export interface AlgKdf extends AesGcm {
messageFormat: MessageFormat.V1
cacheSafe: true
kdf: 'HKDF'
kdfHash: NodeHash | WebCryptoHash
commitment: 'NONE'
}
export interface AlgKdfSigned extends AlgKdf {
messageFormat: MessageFormat.V1
signatureCurve: NodeECDHCurve | WebCryptoECDHCurve
signatureHash: NodeHash | WebCryptoHash
}
export interface AlgCommitted extends AesGcm {
messageFormat: MessageFormat.V2
cacheSafe: true
kdf: 'HKDF'
kdfHash: NodeHash | WebCryptoHash
commitment: 'KEY'
suiteDataLength: SuiteDataLength
commitmentHash: NodeHash | WebCryptoHash
commitmentLength: CommitmentLength
saltLengthBytes: HKDFSaltLengthBytes
}
export interface AlgCommittedSigned extends AlgCommitted {
signatureCurve: NodeECDHCurve | WebCryptoECDHCurve
signatureHash: NodeHash | WebCryptoHash
}
type AlgUnion =
| AlgBasic
| AlgKdf
| AlgKdfSigned
| AlgCommitted
| AlgCommittedSigned
type AlgorithmSuiteValues = AesGcm &
Partial<Omit<AlgBasic, keyof AesGcm>> &
Partial<Omit<AlgKdf, keyof AesGcm>> &
Partial<Omit<AlgKdfSigned, keyof AesGcm>> &
Partial<Omit<AlgCommitted, keyof AesGcm>> &
Partial<Omit<AlgCommittedSigned, keyof AesGcm>>
export abstract class AlgorithmSuite implements AlgorithmSuiteValues {
declare id: AlgorithmSuiteIdentifier
declare name: AlgorithmSuiteName
declare messageFormat: MessageFormat
declare encryption: NodeEncryption | WebCryptoEncryption
declare keyLength: KeyLength
declare keyLengthBytes: number
declare ivLength: IvLength
declare tagLength: TagLength
declare cacheSafe: boolean
declare kdf?: KDF
declare kdfHash?: NodeHash | WebCryptoHash
declare signatureCurve?: NodeECDHCurve | WebCryptoECDHCurve
declare signatureHash?: NodeHash | WebCryptoHash
declare type: AlgorithmSuiteTypeNode | AlgorithmSuiteTypeWebCrypto
declare suiteDataLength?: SuiteDataLength
declare commitmentHash?: NodeHash | WebCryptoHash
declare commitmentLength?: CommitmentLength
declare saltLengthBytes?: HKDFSaltLengthBytes
declare commitment: Commitment
constructor(suiteValues: AlgUnion) {
needs(
this.constructor !== AlgorithmSuite,
'new AlgorithmSuite is not allowed'
)
/* Precondition: A algorithm suite specification must be passed. */
needs(suiteValues, 'Algorithm specification not set.')
/* Precondition: The Algorithm Suite Identifier must exist. */
needs(
AlgorithmSuiteIdentifier[suiteValues.id],
'No suite by that identifier exists.'
)
Object.assign(this, suiteValues)
readOnlyProperty(this, 'keyLengthBytes', this.keyLength / 8)
readOnlyProperty(
this,
'name',
AlgorithmSuiteIdentifier[this.id] as AlgorithmSuiteName
)
}
}
immutableClass(AlgorithmSuite)