Skip to content

Commit 03061d1

Browse files
authored
fix: Encrypt name to result (#211)
Javascript stimulates named parameters with objects. The previous version used `ciphertext` to denote the usable result from a call to `encrypt`. However, this caused some cogitative load. Ciphertext is used to denote encrypted data, however the result of a call to `encrypt` in not _only_ encrypted data. The AWS Encryption SDK uses envelop encryption, so the result of a call to `encrypt` include not only the encrypted data, but also, the encrypted data keys, metadata, and the encryption context. By returning both the `messageHeader` *and* the `ciphertext` users are confused about what exactly was *in* `ciphertext`. This is compounded by returning `messageHeader` which is just a parsed version of the header that is already returned by the result of `encrypt` The named parameter is now call `result`. This makes it clear that this is the important return value from `encrypt` that is the only value needed for `decrypt`.
1 parent c50dfa1 commit 03061d1

File tree

14 files changed

+71
-71
lines changed

14 files changed

+71
-71
lines changed

modules/encrypt-browser/src/encrypt.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface EncryptInput {
5454

5555
export interface EncryptResult {
5656
messageHeader: MessageHeader
57-
ciphertext: Uint8Array
57+
result: Uint8Array
5858
}
5959

6060
export async function encrypt (
@@ -145,7 +145,7 @@ export async function encrypt (
145145
bodyContent.push(frameHeader, cipherBufferAndAuthTag)
146146
}
147147

148-
const ciphertext = concatBuffers(
148+
const result = concatBuffers(
149149
header,
150150
headerAuthIv,
151151
headerAuthTag,
@@ -155,11 +155,11 @@ export async function encrypt (
155155
dispose()
156156

157157
if (typeof subtleSign === 'function') {
158-
const signatureArrayBuffer = await subtleSign(ciphertext)
158+
const signatureArrayBuffer = await subtleSign(result)
159159
const derSignature = raw2der(new Uint8Array(signatureArrayBuffer), material.suite)
160160
const signatureInfo = serializeSignatureInfo(derSignature)
161-
return { ciphertext: concatBuffers(ciphertext, signatureInfo), messageHeader }
161+
return { result: concatBuffers(result, signatureInfo), messageHeader }
162162
} else {
163-
return { ciphertext, messageHeader }
163+
return { result: result, messageHeader }
164164
}
165165
}

modules/encrypt-browser/test/encrypt.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('encrypt structural testing', () => {
7373
const encryptionContext = { simple: 'context' }
7474

7575
const plaintext = fromUtf8('asdf')
76-
const { ciphertext, messageHeader } = await encrypt(keyRing, plaintext, { encryptionContext })
76+
const { result, messageHeader } = await encrypt(keyRing, plaintext, { encryptionContext })
7777

7878
/* The default algorithm suite will add a signature key to the context.
7979
* So I only check that the passed context elements exist.
@@ -82,7 +82,7 @@ describe('encrypt structural testing', () => {
8282
expect(messageHeader.encryptedDataKeys).lengthOf(1)
8383
expect(messageHeader.encryptedDataKeys[0]).to.deep.equal(edk)
8484

85-
const messageInfo = deserializeMessageHeader(ciphertext)
85+
const messageInfo = deserializeMessageHeader(result)
8686
if (!messageInfo) throw new Error('I should never see this error')
8787

8888
expect(messageHeader).to.deep.equal(messageInfo.messageHeader)
@@ -96,9 +96,9 @@ describe('encrypt structural testing', () => {
9696
it('can fully parse a framed message', async () => {
9797
const plaintext = fromUtf8('asdf')
9898
const frameLength = 1
99-
const { ciphertext } = await encrypt(keyRing, plaintext, { frameLength })
99+
const { result } = await encrypt(keyRing, plaintext, { frameLength })
100100

101-
const headerInfo = deserializeMessageHeader(ciphertext)
101+
const headerInfo = deserializeMessageHeader(result)
102102
if (!headerInfo) throw new Error('this should never happen')
103103

104104
const tagLength = headerInfo.algorithmSuite.tagLength / 8
@@ -107,7 +107,7 @@ describe('encrypt structural testing', () => {
107107
let bodyHeader: any
108108
// for every frame...
109109
for (; i < 4; i++) {
110-
bodyHeader = decodeBodyHeader(ciphertext, headerInfo, readPos)
110+
bodyHeader = decodeBodyHeader(result, headerInfo, readPos)
111111
if (!bodyHeader) throw new Error('this should never happen')
112112
readPos = bodyHeader.readPos + bodyHeader.contentLength + tagLength
113113
}
@@ -117,7 +117,7 @@ describe('encrypt structural testing', () => {
117117

118118
// This implicitly tests that I have consumed all the data,
119119
// because otherwise the footer section will be too large
120-
const footerSection = ciphertext.slice(readPos)
120+
const footerSection = result.slice(readPos)
121121
// This will throw if it does not deserialize correctly
122122
deserializeSignature(footerSection)
123123
})

modules/encrypt-node/src/encrypt.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface EncryptInput extends EncryptStreamInput {
1717
}
1818

1919
export interface EncryptOutput {
20-
ciphertext: Buffer
20+
result: Buffer
2121
messageHeader: MessageHeader
2222
}
2323

@@ -29,11 +29,11 @@ export async function encrypt (
2929
const stream = encryptStream(cmm, op)
3030
const { encoding } = op
3131

32-
const ciphertext: Buffer[] = []
32+
const result: Buffer[] = []
3333
let messageHeader: MessageHeader|false = false
3434
stream
3535
.once('MessageHeader', header => { messageHeader = header })
36-
.on('data', (chunk: Buffer) => ciphertext.push(chunk))
36+
.on('data', (chunk: Buffer) => result.push(chunk))
3737

3838
// This will check both Uint8Array|Buffer
3939
if (plaintext instanceof Uint8Array) {
@@ -50,7 +50,7 @@ export async function encrypt (
5050
if (!messageHeader) throw new Error('Unknown format')
5151

5252
return {
53-
ciphertext: Buffer.concat(ciphertext),
53+
result: Buffer.concat(result),
5454
messageHeader
5555
}
5656
}

modules/encrypt-node/test/encrypt.test.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ describe('encrypt structural testing', () => {
7878
const suiteId = AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16
7979

8080
const plaintext = 'asdf'
81-
const { ciphertext, messageHeader } = await encrypt(keyRing, plaintext, { suiteId })
81+
const { result, messageHeader } = await encrypt(keyRing, plaintext, { suiteId })
8282

8383
expect(messageHeader.suiteId).to.equal(suiteId)
8484
expect(messageHeader.encryptionContext).to.deep.equal({})
8585
expect(messageHeader.encryptedDataKeys).lengthOf(1)
8686
expect(messageHeader.encryptedDataKeys[0]).to.deep.equal(edk)
8787

88-
const messageInfo = deserializeMessageHeader(ciphertext)
88+
const messageInfo = deserializeMessageHeader(result)
8989
if (!messageInfo) throw new Error('I should never see this error')
9090

9191
expect(messageHeader).to.deep.equal(messageInfo.messageHeader)
@@ -95,7 +95,7 @@ describe('encrypt structural testing', () => {
9595
const encryptionContext = { simple: 'context' }
9696

9797
const plaintext = Buffer.from('asdf')
98-
const { ciphertext, messageHeader } = await encrypt(keyRing, plaintext, { encryptionContext })
98+
const { result, messageHeader } = await encrypt(keyRing, plaintext, { encryptionContext })
9999

100100
/* The default algorithm suite will add a signature key to the context.
101101
* So I only check that the passed context elements exist.
@@ -104,7 +104,7 @@ describe('encrypt structural testing', () => {
104104
expect(messageHeader.encryptedDataKeys).lengthOf(1)
105105
expect(messageHeader.encryptedDataKeys[0]).to.deep.equal(edk)
106106

107-
const messageInfo = deserializeMessageHeader(ciphertext)
107+
const messageInfo = deserializeMessageHeader(result)
108108
if (!messageInfo) throw new Error('I should never see this error')
109109

110110
expect(messageHeader).to.deep.equal(messageInfo.messageHeader)
@@ -120,7 +120,7 @@ describe('encrypt structural testing', () => {
120120
next(null, 'asdf')
121121
})
122122

123-
const { ciphertext, messageHeader } = await encrypt(keyRing, plaintext, { encryptionContext })
123+
const { result, messageHeader } = await encrypt(keyRing, plaintext, { encryptionContext })
124124

125125
/* The default algorithm suite will add a signature key to the context.
126126
* So I only check that the passed context elements exist.
@@ -129,7 +129,7 @@ describe('encrypt structural testing', () => {
129129
expect(messageHeader.encryptedDataKeys).lengthOf(1)
130130
expect(messageHeader.encryptedDataKeys[0]).to.deep.equal(edk)
131131

132-
const messageInfo = deserializeMessageHeader(ciphertext)
132+
const messageInfo = deserializeMessageHeader(result)
133133
if (!messageInfo) throw new Error('I should never see this error')
134134

135135
expect(messageHeader).to.deep.equal(messageInfo.messageHeader)
@@ -172,7 +172,7 @@ describe('encrypt structural testing', () => {
172172

173173
if (!messageHeader) throw new Error('I should never see this error')
174174

175-
const ciphertext = Buffer.concat(buffer)
175+
const result = Buffer.concat(buffer)
176176

177177
/* The default algorithm suite will add a signature key to the context.
178178
* So I only check that the passed context elements exist.
@@ -181,7 +181,7 @@ describe('encrypt structural testing', () => {
181181
expect(messageHeader.encryptedDataKeys).lengthOf(1)
182182
expect(messageHeader.encryptedDataKeys[0]).to.deep.equal(edk)
183183

184-
const messageInfo = deserializeMessageHeader(ciphertext)
184+
const messageInfo = deserializeMessageHeader(result)
185185
if (!messageInfo) throw new Error('I should never see this error')
186186

187187
expect(messageHeader).to.deep.equal(messageInfo.messageHeader)
@@ -195,9 +195,9 @@ describe('encrypt structural testing', () => {
195195
it('can fully parse a framed message', async () => {
196196
const plaintext = 'asdf'
197197
const frameLength = 1
198-
const { ciphertext } = await encrypt(keyRing, plaintext, { frameLength })
198+
const { result } = await encrypt(keyRing, plaintext, { frameLength })
199199

200-
const headerInfo = deserializeMessageHeader(ciphertext)
200+
const headerInfo = deserializeMessageHeader(result)
201201
if (!headerInfo) throw new Error('this should never happen')
202202

203203
const tagLength = headerInfo.algorithmSuite.tagLength / 8
@@ -206,7 +206,7 @@ describe('encrypt structural testing', () => {
206206
let bodyHeader: any
207207
// for every frame...
208208
for (; i < 5; i++) {
209-
bodyHeader = decodeBodyHeader(ciphertext, headerInfo, readPos)
209+
bodyHeader = decodeBodyHeader(result, headerInfo, readPos)
210210
if (!bodyHeader) throw new Error('this should never happen')
211211
readPos = bodyHeader.readPos + bodyHeader.contentLength + tagLength
212212
}
@@ -216,7 +216,7 @@ describe('encrypt structural testing', () => {
216216

217217
// This implicitly tests that I have consumed all the data,
218218
// because otherwise the footer section will be too large
219-
const footerSection = ciphertext.slice(readPos)
219+
const footerSection = result.slice(readPos)
220220
// This will throw if it does not deserialize correctly
221221
deserializeSignature(footerSection)
222222
})

modules/example-browser/src/aes_simple.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,22 @@ import { toBase64 } from '@aws-sdk/util-base64-browser'
6767
const plainText = new Uint8Array([1, 2, 3, 4, 5])
6868

6969
/* Encrypt the data. */
70-
const { ciphertext } = await encrypt(keyring, plainText, { encryptionContext: context })
70+
const { result } = await encrypt(keyring, plainText, { encryptionContext: context })
7171

7272
/* Log the plain text
7373
* only for testing and to show that it works.
7474
*/
7575
console.log('plainText:', plainText)
7676
document.write('</br>plainText:' + plainText + '</br>')
7777

78-
/* Log the base64-encoded ciphertext
78+
/* Log the base64-encoded result
7979
* so that you can try decrypting it with another AWS Encryption SDK implementation.
8080
*/
81-
const ciphertextBase64 = toBase64(ciphertext)
82-
console.log(ciphertextBase64)
83-
document.write(ciphertextBase64)
81+
const resultBase64 = toBase64(result)
82+
console.log(resultBase64)
83+
document.write(resultBase64)
8484

85-
const { plaintext, messageHeader } = await decrypt(keyring, ciphertext)
85+
const { plaintext, messageHeader } = await decrypt(keyring, result)
8686

8787
/* Grab the encryption context so you can verify it. */
8888
const { encryptionContext } = messageHeader

modules/example-browser/src/kms_simple.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,22 @@ declare const AWS_CREDENTIALS: {accessKeyId: string, secretAccessKey:string }
9292
const plainText = new Uint8Array([1, 2, 3, 4, 5])
9393

9494
/* Encrypt the data. */
95-
const { ciphertext } = await encrypt(keyring, plainText, { encryptionContext: context })
95+
const { result } = await encrypt(keyring, plainText, { encryptionContext: context })
9696

9797
/* Log the plain text
9898
* only for testing and to show that it works.
9999
*/
100100
console.log('plainText:', plainText)
101101
document.write('</br>plainText:' + plainText + '</br>')
102102

103-
/* Log the base64-encoded ciphertext
103+
/* Log the base64-encoded result
104104
* so that you can try decrypting it with another AWS Encryption SDK implementation.
105105
*/
106-
const ciphertextBase64 = toBase64(ciphertext)
107-
console.log(ciphertextBase64)
108-
document.write(ciphertextBase64)
106+
const resultBase64 = toBase64(result)
107+
console.log(resultBase64)
108+
document.write(resultBase64)
109109

110-
const { plaintext, messageHeader } = await decrypt(keyring, ciphertext)
110+
const { plaintext, messageHeader } = await decrypt(keyring, result)
111111

112112
/* Grab the encryption context so you can verify it. */
113113
const { encryptionContext } = messageHeader

modules/example-browser/src/multi_keyring.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -122,29 +122,29 @@ declare const AWS_CREDENTIALS: {accessKeyId: string, secretAccessKey:string }
122122
const plainText = new Uint8Array([1, 2, 3, 4, 5])
123123

124124
/* Encrypt the data. */
125-
const { ciphertext } = await encrypt(keyring, plainText, { encryptionContext: context })
125+
const { result } = await encrypt(keyring, plainText, { encryptionContext: context })
126126

127127
/* Log the plain text
128128
* only for testing and to show that it works.
129129
*/
130130
console.log('plainText:', plainText)
131131
document.write('</br>plainText:' + plainText + '</br>')
132132

133-
/* Log the base64-encoded ciphertext
133+
/* Log the base64-encoded result
134134
* so that you can try decrypting it with another AWS Encryption SDK implementation.
135135
*/
136-
const ciphertextBase64 = toBase64(ciphertext)
137-
console.log(ciphertextBase64)
138-
document.write(ciphertextBase64)
136+
const resultBase64 = toBase64(result)
137+
console.log(resultBase64)
138+
document.write(resultBase64)
139139

140140
/* Decrypt the data.
141141
* This decrypt call could be done with **any** of the 3 keyrings.
142142
* Here we use the multi-keyring, but
143-
* decrypt(kmsKeyring, ciphertext)
144-
* decrypt(aesKeyring, ciphertext)
143+
* decrypt(kmsKeyring, result)
144+
* decrypt(aesKeyring, result)
145145
* would both work as well.
146146
*/
147-
const { plaintext, messageHeader } = await decrypt(keyring, ciphertext)
147+
const { plaintext, messageHeader } = await decrypt(keyring, result)
148148

149149
/* Grab the encryption context so you can verify it. */
150150
const { encryptionContext } = messageHeader

modules/example-browser/src/rsa_simple.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,22 @@ import { toBase64 } from '@aws-sdk/util-base64-browser'
6969
const plainText = new Uint8Array([1, 2, 3, 4, 5])
7070

7171
/* Encrypt the data. */
72-
const { ciphertext } = await encrypt(keyring, plainText, { encryptionContext: context })
72+
const { result } = await encrypt(keyring, plainText, { encryptionContext: context })
7373

7474
/* Log the plain text
7575
* only for testing and to show that it works.
7676
*/
7777
console.log('plainText:', plainText)
7878
document.write('</br>plainText:' + plainText + '</br>')
7979

80-
/* Log the base64-encoded ciphertext
80+
/* Log the base64-encoded result
8181
* so that you can try decrypting it with another AWS Encryption SDK implementation.
8282
*/
83-
const ciphertextBase64 = toBase64(ciphertext)
84-
console.log(ciphertextBase64)
85-
document.write(ciphertextBase64)
83+
const resultBase64 = toBase64(result)
84+
console.log(resultBase64)
85+
document.write(resultBase64)
8686

87-
const { plaintext, messageHeader } = await decrypt(keyring, ciphertext)
87+
const { plaintext, messageHeader } = await decrypt(keyring, result)
8888

8989
/* Grab the encryption context so you can verify it. */
9090
const { encryptionContext } = messageHeader

modules/example-node/src/aes_simple.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ export async function aesTest () {
5757
const cleartext = 'asdf'
5858

5959
/* Encrypt the data. */
60-
const { ciphertext } = await encrypt(keyring, cleartext, { encryptionContext: context })
60+
const { result } = await encrypt(keyring, cleartext, { encryptionContext: context })
6161
/* Decrypt the data. */
62-
const { plaintext, messageHeader } = await decrypt(keyring, ciphertext)
62+
const { plaintext, messageHeader } = await decrypt(keyring, result)
6363

6464
/* Grab the encryption context so you can verify it. */
6565
const { encryptionContext } = messageHeader
@@ -78,5 +78,5 @@ export async function aesTest () {
7878
})
7979

8080
/* Return the values so the code can be tested. */
81-
return { plaintext, ciphertext, cleartext }
81+
return { plaintext, result, cleartext }
8282
}

modules/example-node/src/kms_simple.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ export async function kmsSimpleTest () {
5252
const cleartext = 'asdf'
5353

5454
/* Encrypt the data. */
55-
const { ciphertext } = await encrypt(keyring, cleartext, { encryptionContext: context })
55+
const { result } = await encrypt(keyring, cleartext, { encryptionContext: context })
5656

5757
/* Decrypt the data. */
58-
const { plaintext, messageHeader } = await decrypt(keyring, ciphertext)
58+
const { plaintext, messageHeader } = await decrypt(keyring, result)
5959

6060
/* Grab the encryption context so you can verify it. */
6161
const { encryptionContext } = messageHeader
@@ -74,5 +74,5 @@ export async function kmsSimpleTest () {
7474
})
7575

7676
/* Return the values so the code can be tested. */
77-
return { plaintext, ciphertext, cleartext, messageHeader }
77+
return { plaintext, result, cleartext, messageHeader }
7878
}

0 commit comments

Comments
 (0)