-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathaad_factory.ts
58 lines (53 loc) · 1.65 KB
/
aad_factory.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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/*
* This public interface for constructing the additional authenticated data (AAD)
* is provided for the use of the Encryption SDK for JavaScript only. It can be used
* as a reference but is not intended to be use by any packages other than the
* Encryption SDK for JavaScript.
*
* This AAD is used for the Body section
* See: https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/body-aad-reference.html
*/
import BN from 'bn.js'
import { ContentAADString, ContentType } from './identifiers'
import { BinaryData } from './types'
import { concatBuffers } from './concat_buffers'
import { uInt32BE } from './uint_util'
export function aadFactory(fromUtf8: (input: string) => Uint8Array) {
return {
messageAADContentString,
messageAAD,
}
function messageAADContentString({
contentType,
isFinalFrame,
}: {
contentType: ContentType
isFinalFrame: boolean
}) {
switch (contentType) {
case ContentType.NO_FRAMING:
return ContentAADString.NON_FRAMED_STRING_ID
case ContentType.FRAMED_DATA:
return isFinalFrame
? ContentAADString.FINAL_FRAME_STRING_ID
: ContentAADString.FRAME_STRING_ID
default:
throw new Error('Unrecognized content type')
}
}
function messageAAD(
messageId: BinaryData,
aadContentString: ContentAADString,
seqNum: number,
contentLength: number
) {
return concatBuffers(
messageId,
fromUtf8(aadContentString),
uInt32BE(seqNum),
new Uint8Array(new BN(contentLength).toArray('be', 8))
)
}
}