Skip to content

Commit 9dfa857

Browse files
authored
feat: Node.js Typescript version dependency (#146)
resolves #135 The AWS Encryption SDK should track the Node.js LTS policy. For developers using Typescript and @types/node@8 the ESDK should “just work”. Because some packages export Node.js types like `CipherGCM` developers would be forced to install an unwanted type version. This pulls in the needed parts of the needed types into the packages. It is only the types that differ in this case. Regarding KeyObjects, rsa support is removed, but will be added back in when #74 is addressed.
1 parent 0f4dd7e commit 9dfa857

File tree

6 files changed

+53
-25
lines changed

6 files changed

+53
-25
lines changed

modules/decrypt-node/src/decipher_stream.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
// @ts-ignore
1717
import { Transform as PortableTransform } from 'readable-stream'
1818
import { Transform } from 'stream' // eslint-disable-line no-unused-vars
19-
import { DecipherGCM } from 'crypto' // eslint-disable-line no-unused-vars
20-
import { needs } from '@aws-crypto/material-management-node'
19+
import {
20+
needs,
21+
GetDecipher, // eslint-disable-line no-unused-vars
22+
AwsEsdkJsDecipherGCM // eslint-disable-line no-unused-vars
23+
} from '@aws-crypto/material-management-node'
2124
import {
2225
aadFactory,
2326
ContentType // eslint-disable-line no-unused-vars
@@ -31,12 +34,12 @@ const PortableTransformWithType = (<new (...args: any[]) => Transform>PortableTr
3134
export interface DecipherInfo {
3235
messageId: Buffer
3336
contentType: ContentType
34-
getDecipher: (iv: Uint8Array) => DecipherGCM
37+
getDecipher: GetDecipher
3538
dispose: () => void
3639
}
3740

3841
interface DecipherState {
39-
decipher: DecipherGCM
42+
decipher: AwsEsdkJsDecipherGCM
4043
content: Buffer[]
4144
contentLength: number
4245
}

modules/decrypt-node/src/verify_stream.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
// @ts-ignore
1717
import { Transform as PortableTransform } from 'readable-stream'
1818
import { Transform } from 'stream' // eslint-disable-line no-unused-vars
19-
import { DecipherGCM } from 'crypto' // eslint-disable-line no-unused-vars
2019
import {
2120
needs,
22-
GetVerify // eslint-disable-line no-unused-vars
21+
GetVerify, // eslint-disable-line no-unused-vars
22+
GetDecipher // eslint-disable-line no-unused-vars
2323
} from '@aws-crypto/material-management-node'
2424
import {
2525
deserializeSignature,
@@ -35,7 +35,7 @@ const PortableTransformWithType = (<new (...args: any[]) => Transform>PortableTr
3535

3636
export interface VerifyInfo {
3737
headerInfo: HeaderInfo
38-
getDecipher: (iv: Uint8Array) => DecipherGCM
38+
getDecipher: GetDecipher
3939
dispose: () => void
4040
verify?: AWSVerify
4141
}

modules/encrypt-node/src/framed_encrypt_stream.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ import {
1919
} from '@aws-crypto/serialize'
2020
// @ts-ignore
2121
import { Transform as PortableTransform } from 'readable-stream'
22-
import { CipherGCM } from 'crypto' // eslint-disable-line no-unused-vars
2322
import { Transform } from 'stream' // eslint-disable-line no-unused-vars
24-
import { needs } from '@aws-crypto/material-management-node'
23+
import {
24+
GetCipher, // eslint-disable-line no-unused-vars
25+
AwsEsdkJsCipherGCM, // eslint-disable-line no-unused-vars
26+
needs
27+
} from '@aws-crypto/material-management-node'
2528

2629
const fromUtf8 = (input: string) => Buffer.from(input, 'utf8')
2730
const serialize = serializeFactory(fromUtf8)
@@ -38,7 +41,7 @@ interface EncryptFrame {
3841
content: Buffer[]
3942
bodyHeader: Buffer
4043
headerSent?: boolean
41-
cipher: CipherGCM,
44+
cipher: AwsEsdkJsCipherGCM,
4245
isFinalFrame: boolean
4346
}
4447

@@ -165,8 +168,6 @@ export function getFramedEncryptStream (getCipher: GetCipher, messageHeader: Mes
165168
})()
166169
}
167170

168-
type GetCipher = (iv: Uint8Array) => CipherGCM
169-
170171
type EncryptFrameInput = {
171172
pendingFrame: AccumulatingFrame,
172173
messageHeader: MessageHeader,

modules/material-management-node/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export {
1818
getEncryptHelper,
1919
getDecryptionHelper,
2020
GetSigner,
21-
GetVerify
21+
GetVerify,
22+
GetCipher,
23+
GetDecipher,
24+
AwsEsdkJsCipherGCM,
25+
AwsEsdkJsDecipherGCM
2226
} from './material_helpers'
2327
export {
2428
NodeDecryptionMaterial, NodeEncryptionMaterial, NodeAlgorithmSuite,

modules/material-management-node/src/material_helpers.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,45 @@ import {
1818
NodeHash // eslint-disable-line no-unused-vars
1919
} from '@aws-crypto/material-management'
2020
import {
21-
CipherGCM, DecipherGCM, Signer, Verify, // eslint-disable-line no-unused-vars
21+
Signer, Verify, // eslint-disable-line no-unused-vars
2222
createCipheriv, createDecipheriv, createSign, createVerify
2323
} from 'crypto'
2424
import { HKDF } from '@aws-crypto/hkdf-node'
2525

26+
export interface AwsEsdkJsCipherGCM {
27+
update(data: Buffer): Buffer
28+
final(): Buffer
29+
getAuthTag(): Buffer
30+
setAAD(aad: Buffer): this
31+
}
32+
33+
export interface AwsEsdkJsDecipherGCM {
34+
update(data: Buffer): Buffer
35+
final(): Buffer
36+
setAuthTag(buffer: Buffer): this
37+
setAAD(aad: Buffer): this
38+
}
39+
2640
type KDFIndex = Readonly<{[K in NodeHash]: ReturnType<typeof HKDF>}>
2741
const kdfIndex: KDFIndex = Object.freeze({
2842
sha256: HKDF('sha256' as NodeHash),
2943
sha384: HKDF('sha384' as NodeHash)
3044
})
3145

3246
export interface GetCipher {
33-
(info?: Uint8Array) : (iv: Uint8Array) => CipherGCM
47+
(iv: Uint8Array): AwsEsdkJsCipherGCM
48+
}
49+
50+
export interface CurryGetCipher {
51+
(info?: Uint8Array): GetCipher
3452
}
3553

3654
export interface GetSigner {
3755
() : Signer & {awsCryptoSign: () => Buffer}
3856
}
3957

4058
export interface NodeEncryptionMaterialHelper {
41-
kdfGetCipher: GetCipher
59+
kdfGetCipher: CurryGetCipher
4260
getSigner?: GetSigner
4361
dispose: () => void
4462
}
@@ -56,7 +74,7 @@ export const getEncryptHelper: GetEncryptHelper = (material: NodeEncryptionMater
5674
* Function overloads "works" but then I can not export
5775
* the function and have eslint be happy (Multiple exports of name)
5876
*/
59-
const kdfGetCipher = <GetCipher>getCryptoStream(material)
77+
const kdfGetCipher = <CurryGetCipher>getCryptoStream(material)
6078
return Object.freeze({
6179
kdfGetCipher,
6280
getSigner: signatureHash ? getSigner : undefined,
@@ -93,14 +111,17 @@ export const getEncryptHelper: GetEncryptHelper = (material: NodeEncryptionMater
93111
}
94112

95113
export interface GetDecipher {
96-
(info?: Uint8Array) : (iv: Uint8Array) => DecipherGCM
114+
(iv: Uint8Array): AwsEsdkJsDecipherGCM
115+
}
116+
export interface CurryGetDecipher {
117+
(info?: Uint8Array) : GetDecipher
97118
}
98119
export interface GetVerify {
99120
() : Verify & {awsCryptoVerify: (signature: Buffer) => boolean}
100121
}
101122

102123
export interface NodeDecryptionMaterialHelper {
103-
kdfGetDecipher: GetDecipher
124+
kdfGetDecipher: CurryGetDecipher
104125
getVerify?: GetVerify
105126
dispose: () => void
106127
}
@@ -119,7 +140,7 @@ export const getDecryptionHelper: GetDecryptionHelper = (material: NodeDecryptio
119140
* Function overloads "works" but then I can not export
120141
* the function and have eslint be happy (Multiple exports of name)
121142
*/
122-
const kdfGetDecipher = <GetDecipher>getCryptoStream(material)
143+
const kdfGetDecipher = <CurryGetDecipher>getCryptoStream(material)
123144
return Object.freeze({
124145
kdfGetDecipher,
125146
getVerify: signatureHash ? getVerify : undefined,
@@ -158,7 +179,7 @@ export function getCryptoStream (material: NodeEncryptionMaterial|NodeDecryption
158179

159180
return (info?: Uint8Array) => {
160181
const derivedKey = nodeKdf(material, info)
161-
return (iv: Uint8Array) => {
182+
return (iv: Uint8Array): AwsEsdkJsCipherGCM|AwsEsdkJsDecipherGCM => {
162183
/* Precondition: The length of the IV must match the algorithm suite specification. */
163184
needs(iv.byteLength === ivLength, 'Iv length does not match algorithm suite specification')
164185
/* Precondition: The material must have not been zeroed.

modules/raw-rsa-keyring-node/src/raw_rsa_keyring_node.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ import {
3030
constants,
3131
publicEncrypt,
3232
privateDecrypt,
33-
randomBytes,
34-
KeyObject // eslint-disable-line no-unused-vars
33+
randomBytes
3534
} from 'crypto'
3635

3736
import {
@@ -52,8 +51,8 @@ import {
5251
* or more complicated options... Thoughts?
5352
*/
5453
interface RsaKey {
55-
publicKey?: string | Buffer | KeyObject
56-
privateKey?: string | Buffer | KeyObject
54+
publicKey?: string | Buffer
55+
privateKey?: string | Buffer
5756
}
5857

5958
export type RawRsaKeyringNodeInput = {

0 commit comments

Comments
 (0)