26
26
* The AAD (encryption context) is the same as the message.
27
27
*/
28
28
29
- import { concatBuffers , uInt32BE } from '@aws-crypto/serialize'
30
- import {
31
- AlgorithmSuite , // eslint-disable-line no-unused-vars
32
- EncryptedDataKey , // eslint-disable-line no-unused-vars
33
- needs
34
- } from '@aws-crypto/material-management'
35
-
36
- export function rawAesEncryptedDataKeyFactory (
37
- toUtf8 : ( input : Uint8Array ) => string ,
38
- fromUtf8 : ( input : any ) => Uint8Array
39
- ) {
40
- return { rawAesEncryptedDataKey }
41
-
42
- function rawAesEncryptedDataKey (
43
- keyNamespace : string ,
44
- keyName : string ,
45
- iv : Uint8Array ,
46
- ciphertext : Uint8Array ,
47
- authTag : Uint8Array
48
- ) : EncryptedDataKey {
49
- const ivLength = iv . byteLength
50
- const authTagBitLength = authTag . byteLength * 8
51
- const encryptedDataKey = concatBuffers ( ciphertext , authTag )
52
- const providerId = keyNamespace
53
- const rawInfo = concatBuffers ( fromUtf8 ( keyName ) , uInt32BE ( authTagBitLength ) , uInt32BE ( ivLength ) , iv )
54
- const providerInfo = toUtf8 ( rawInfo )
55
- return new EncryptedDataKey ( { encryptedDataKey, providerId, providerInfo, rawInfo } )
56
- }
57
- }
58
-
59
- export function rawAesEncryptedPartsFactory ( fromUtf8 : ( input : any ) => Uint8Array ) {
60
- return { rawAesEncryptedParts }
61
-
62
- function rawAesEncryptedParts ( suite : AlgorithmSuite , keyName : string , { encryptedDataKey, rawInfo } : EncryptedDataKey ) {
63
- if ( ! ( rawInfo instanceof Uint8Array ) ) throw new Error ( 'Malformed Encrypted Data Key.' )
64
- // see above for format, slice off the "string part"
65
- rawInfo = rawInfo . slice ( fromUtf8 ( keyName ) . byteLength )
66
- /* Uint8Array is a view on top of the underlying ArrayBuffer.
29
+ import { concatBuffers , uInt32BE } from '@aws-crypto/serialize'
30
+ import {
31
+ AlgorithmSuite , // eslint-disable-line no-unused-vars
32
+ EncryptedDataKey , // eslint-disable-line no-unused-vars
33
+ needs
34
+ } from '@aws-crypto/material-management'
35
+
36
+ export function rawAesEncryptedDataKeyFactory (
37
+ toUtf8 : ( input : Uint8Array ) => string ,
38
+ fromUtf8 : ( input : any ) => Uint8Array
39
+ ) {
40
+ return { rawAesEncryptedDataKey }
41
+
42
+ function rawAesEncryptedDataKey (
43
+ keyNamespace : string ,
44
+ keyName : string ,
45
+ iv : Uint8Array ,
46
+ ciphertext : Uint8Array ,
47
+ authTag : Uint8Array
48
+ ) : EncryptedDataKey {
49
+ const ivLength = iv . byteLength
50
+ const authTagBitLength = authTag . byteLength * 8
51
+ const encryptedDataKey = concatBuffers ( ciphertext , authTag )
52
+ const providerId = keyNamespace
53
+ const rawInfo = concatBuffers ( fromUtf8 ( keyName ) , uInt32BE ( authTagBitLength ) , uInt32BE ( ivLength ) , iv )
54
+ const providerInfo = toUtf8 ( rawInfo )
55
+ return new EncryptedDataKey ( { encryptedDataKey, providerId, providerInfo, rawInfo } )
56
+ }
57
+ }
58
+
59
+ export function rawAesEncryptedPartsFactory ( fromUtf8 : ( input : any ) => Uint8Array ) {
60
+ return { rawAesEncryptedParts }
61
+
62
+ function rawAesEncryptedParts ( suite : AlgorithmSuite , keyName : string , { encryptedDataKey, rawInfo } : EncryptedDataKey ) {
63
+ if ( ! ( rawInfo instanceof Uint8Array ) ) throw new Error ( 'Malformed Encrypted Data Key.' )
64
+ // see above for format, slice off the "string part"
65
+ rawInfo = rawInfo . slice ( fromUtf8 ( keyName ) . byteLength )
66
+ /* Uint8Array is a view on top of the underlying ArrayBuffer.
67
67
* This means that raw underlying memory stored in the ArrayBuffer
68
68
* may be larger than the Uint8Array. This is especially true of
69
69
* the Node.js Buffer object. The offset and length *must* be
70
70
* passed to the DataView otherwise I will get unexpected results.
71
71
*/
72
- const dataView = new DataView (
73
- rawInfo . buffer ,
74
- rawInfo . byteOffset ,
75
- rawInfo . byteLength
76
- )
77
- /* See above:
72
+ const dataView = new DataView (
73
+ rawInfo . buffer ,
74
+ rawInfo . byteOffset ,
75
+ rawInfo . byteLength
76
+ )
77
+ /* See above:
78
78
* uInt32BE(authTagBitLength),uInt32BE(ivLength), iv
79
79
*/
80
- const tagLengthBits = dataView . getUint32 ( 0 , false ) // big endian
81
- const ivLength = dataView . getUint32 ( 4 , false ) // big endian
82
- /* Precondition: The ivLength must match the algorith suite specification. */
83
- needs ( ivLength === suite . ivLength , 'Malformed providerInfo' )
84
- /* Precondition: The tagLength must match the algorith suite specification. */
85
- needs ( tagLengthBits === suite . tagLength , 'Malformed providerInfo' )
86
- /* Precondition: The byteLength of rawInfo should match the encoded length. */
87
- needs ( rawInfo . byteLength === 4 + 4 + ivLength , 'Malformed providerInfo' )
88
- const tagLength = tagLengthBits / 8
89
- /* Precondition: The encryptedDataKey byteLength must match the algorith suite specification and encoded length. */
90
- needs ( encryptedDataKey . byteLength === tagLength + suite . keyLengthBytes , 'Malformed providerInfo' )
91
- const iv = rawInfo . slice ( - ivLength )
92
- const authTag = encryptedDataKey . slice ( - tagLength )
93
- const ciphertext = encryptedDataKey . slice ( 0 , - tagLength )
94
-
95
- return { authTag, ciphertext, iv }
96
- }
97
- }
98
-
80
+ const tagLengthBits = dataView . getUint32 ( 0 , false ) // big endian
81
+ const ivLength = dataView . getUint32 ( 4 , false ) // big endian
82
+ /* Precondition: The ivLength must match the algorith suite specification. */
83
+ needs ( ivLength === suite . ivLength , 'Malformed providerInfo' )
84
+ /* Precondition: The tagLength must match the algorith suite specification. */
85
+ needs ( tagLengthBits === suite . tagLength , 'Malformed providerInfo' )
86
+ /* Precondition: The byteLength of rawInfo should match the encoded length. */
87
+ needs ( rawInfo . byteLength === 4 + 4 + ivLength , 'Malformed providerInfo' )
88
+ const tagLength = tagLengthBits / 8
89
+ /* Precondition: The encryptedDataKey byteLength must match the algorith suite specification and encoded length. */
90
+ needs ( encryptedDataKey . byteLength === tagLength + suite . keyLengthBytes , 'Malformed providerInfo' )
91
+ const iv = rawInfo . slice ( - ivLength )
92
+ const authTag = encryptedDataKey . slice ( - tagLength )
93
+ const ciphertext = encryptedDataKey . slice ( 0 , - tagLength )
94
+
95
+ return { authTag, ciphertext, iv }
96
+ }
97
+ }
0 commit comments