Skip to content

Commit ff78f94

Browse files
authored
fix: encrypt/decrypt interface should be the same (#189)
resolves #182 The encrypt/decrypt browser and node interfaces were slightly different. Make all the interfaces the same. The language is chosen to be similar to the Python ESDK
1 parent d2b352c commit ff78f94

File tree

11 files changed

+54
-54
lines changed

11 files changed

+54
-54
lines changed

modules/client-browser/Readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ const plainText = new Uint8Array([1, 2, 3, 4, 5])
6969
* the Encryption SDK returns an "encrypted message" that includes the ciphertext,
7070
* the encryption context, and the encrypted data keys.
7171
*/
72-
const { cipherMessage } = await encrypt(keyring, plainText, { encryptionContext: context })
72+
const { ciphertext } = await encrypt(keyring, plainText, { encryptionContext: context })
7373

7474
/* Decrypt the ciphertext using the same keyring */
75-
const { clearMessage, messageHeader } = await decrypt(keyring, cipherMessage)
75+
const { plaintext, messageHeader } = await decrypt(keyring, ciphertext)
7676

7777
/* Get the encryption context */
7878
const { encryptionContext } = messageHeader
@@ -87,8 +87,8 @@ Object
8787
})
8888

8989
/* If the encryption context is verified, log the plaintext. */
90-
document.write('</br>Decrypted:' + clearMessage)
91-
console.log(clearMessage)
90+
document.write('</br>Decrypted:' + plaintext)
91+
console.log(plaintext)
9292

9393
```
9494

modules/decrypt-browser/src/decrypt.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const { messageAADContentString, messageAAD } = aadFactory(fromUtf8)
4040

4141
export interface DecryptResult {
4242
messageHeader: MessageHeader
43-
clearMessage: Uint8Array
43+
plaintext: Uint8Array
4444
}
4545

4646
export async function decrypt (
@@ -67,7 +67,7 @@ export async function decrypt (
6767
// The tag is appended to the Data
6868
await getSubtleDecrypt(headerIv, rawHeader)(headerAuthTag) // will throw if invalid
6969

70-
const { clearMessage, readPos } = await bodyDecrypt({ buffer: ciphertext, getSubtleDecrypt, headerInfo })
70+
const { plaintext, readPos } = await bodyDecrypt({ buffer: ciphertext, getSubtleDecrypt, headerInfo })
7171

7272
dispose()
7373

@@ -81,9 +81,9 @@ export async function decrypt (
8181
const isValid = await subtleVerify(rawSignature, data)
8282
/* Postcondition: subtleVerify must validate the signature. */
8383
needs(isValid, 'Invalid Signature')
84-
return { messageHeader, clearMessage }
84+
return { messageHeader, plaintext }
8585
} else {
86-
return { messageHeader, clearMessage }
86+
return { messageHeader, plaintext }
8787
}
8888
}
8989

@@ -118,8 +118,8 @@ async function bodyDecrypt ({ buffer, getSubtleDecrypt, headerInfo }: BodyDecryp
118118
clearBuffers.push(clearBlob)
119119
readPos = frameInfo.readPos
120120
if (frameInfo.isFinalFrame) {
121-
const clearMessage = concatBuffers(...clearBuffers)
122-
return { clearMessage, readPos }
121+
const plaintext = concatBuffers(...clearBuffers)
122+
return { plaintext, readPos }
123123
}
124124
}
125125
}

modules/decrypt-browser/test/decrypt.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import * as fixtures from './fixtures'
2323

2424
describe('decrypt', () => {
2525
it('buffer', async () => {
26-
const { clearMessage: test, messageHeader } = await decrypt(
26+
const { plaintext: test, messageHeader } = await decrypt(
2727
fixtures.decryptKeyring(),
2828
fixtures.ciphertextAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384()
2929
)

modules/encrypt-browser/src/encrypt.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface EncryptInput {
5454

5555
export interface EncryptResult {
5656
messageHeader: MessageHeader
57-
cipherMessage: Uint8Array
57+
ciphertext: 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 cipherMessage = concatBuffers(
148+
const ciphertext = 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(cipherMessage)
158+
const signatureArrayBuffer = await subtleSign(ciphertext)
159159
const derSignature = raw2der(new Uint8Array(signatureArrayBuffer), material.suite)
160160
const signatureInfo = serializeSignatureInfo(derSignature)
161-
return { cipherMessage: concatBuffers(cipherMessage, signatureInfo), messageHeader }
161+
return { ciphertext: concatBuffers(ciphertext, signatureInfo), messageHeader }
162162
} else {
163-
return { cipherMessage, messageHeader }
163+
return { ciphertext, messageHeader }
164164
}
165165
}

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

Lines changed: 6 additions & 6 deletions
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 { cipherMessage, messageHeader } = await encrypt(keyRing, plaintext, { encryptionContext })
76+
const { ciphertext, 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(cipherMessage)
85+
const messageInfo = deserializeMessageHeader(ciphertext)
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 { cipherMessage } = await encrypt(keyRing, plaintext, { frameLength })
99+
const { ciphertext } = await encrypt(keyRing, plaintext, { frameLength })
100100

101-
const headerInfo = deserializeMessageHeader(cipherMessage)
101+
const headerInfo = deserializeMessageHeader(ciphertext)
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(cipherMessage, headerInfo, readPos)
110+
bodyHeader = decodeBodyHeader(ciphertext, 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 = cipherMessage.slice(readPos)
120+
const footerSection = ciphertext.slice(readPos)
121121
// This will throw if it does not deserialize correctly
122122
deserializeSignature(footerSection)
123123
})

modules/example-browser/src/aes_simple.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ 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 { cipherMessage } = await encrypt(keyring, plainText, { encryptionContext: context })
70+
const { ciphertext } = await encrypt(keyring, plainText, { encryptionContext: context })
7171

7272
/* Log the plain text
7373
* only for testing and to show that it works.
@@ -78,11 +78,11 @@ import { toBase64 } from '@aws-sdk/util-base64-browser'
7878
/* Log the base64-encoded ciphertext
7979
* so that you can try decrypting it with another AWS Encryption SDK implementation.
8080
*/
81-
const cipherMessageBase64 = toBase64(cipherMessage)
82-
console.log(cipherMessageBase64)
83-
document.write(cipherMessageBase64)
81+
const ciphertextBase64 = toBase64(ciphertext)
82+
console.log(ciphertextBase64)
83+
document.write(ciphertextBase64)
8484

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

8787
/* Grab the encryption context so you can verify it. */
8888
const { encryptionContext } = messageHeader
@@ -103,6 +103,6 @@ import { toBase64 } from '@aws-sdk/util-base64-browser'
103103
/* Log the clear message
104104
* only for testing and to show that it works.
105105
*/
106-
document.write('</br>clearMessage:' + clearMessage)
107-
console.log(clearMessage)
106+
document.write('</br>plaintext:' + plaintext)
107+
console.log(plaintext)
108108
})()

modules/example-browser/src/kms_simple.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ 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 { cipherMessage } = await encrypt(keyring, plainText, { encryptionContext: context })
95+
const { ciphertext } = await encrypt(keyring, plainText, { encryptionContext: context })
9696

9797
/* Log the plain text
9898
* only for testing and to show that it works.
@@ -103,11 +103,11 @@ declare const AWS_CREDENTIALS: {accessKeyId: string, secretAccessKey:string }
103103
/* Log the base64-encoded ciphertext
104104
* so that you can try decrypting it with another AWS Encryption SDK implementation.
105105
*/
106-
const cipherMessageBase64 = toBase64(cipherMessage)
107-
console.log(cipherMessageBase64)
108-
document.write(cipherMessageBase64)
106+
const ciphertextBase64 = toBase64(ciphertext)
107+
console.log(ciphertextBase64)
108+
document.write(ciphertextBase64)
109109

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

112112
/* Grab the encryption context so you can verify it. */
113113
const { encryptionContext } = messageHeader
@@ -128,6 +128,6 @@ declare const AWS_CREDENTIALS: {accessKeyId: string, secretAccessKey:string }
128128
/* Log the clear message
129129
* only for testing and to show that it works.
130130
*/
131-
document.write('</br>Decrypted:' + clearMessage)
132-
console.log(clearMessage)
131+
document.write('</br>Decrypted:' + plaintext)
132+
console.log(plaintext)
133133
})()

modules/example-browser/src/multi_keyring.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ 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 { cipherMessage } = await encrypt(keyring, plainText, { encryptionContext: context })
125+
const { ciphertext } = await encrypt(keyring, plainText, { encryptionContext: context })
126126

127127
/* Log the plain text
128128
* only for testing and to show that it works.
@@ -133,9 +133,9 @@ declare const AWS_CREDENTIALS: {accessKeyId: string, secretAccessKey:string }
133133
/* Log the base64-encoded ciphertext
134134
* so that you can try decrypting it with another AWS Encryption SDK implementation.
135135
*/
136-
const cipherMessageBase64 = toBase64(cipherMessage)
137-
console.log(cipherMessageBase64)
138-
document.write(cipherMessageBase64)
136+
const ciphertextBase64 = toBase64(ciphertext)
137+
console.log(ciphertextBase64)
138+
document.write(ciphertextBase64)
139139

140140
/* Decrypt the data.
141141
* This decrypt call could be done with **any** of the 3 keyrings.
@@ -144,7 +144,7 @@ declare const AWS_CREDENTIALS: {accessKeyId: string, secretAccessKey:string }
144144
* decrypt(aesKeyring, ciphertext)
145145
* would both work as well.
146146
*/
147-
const { clearMessage, messageHeader } = await decrypt(keyring, cipherMessage)
147+
const { plaintext, messageHeader } = await decrypt(keyring, ciphertext)
148148

149149
/* Grab the encryption context so you can verify it. */
150150
const { encryptionContext } = messageHeader
@@ -165,6 +165,6 @@ declare const AWS_CREDENTIALS: {accessKeyId: string, secretAccessKey:string }
165165
/* Log the clear message
166166
* only for testing and to show that it works.
167167
*/
168-
document.write('</br>Decrypted:' + clearMessage)
169-
console.log(clearMessage)
168+
document.write('</br>Decrypted:' + plaintext)
169+
console.log(plaintext)
170170
})()

modules/example-browser/src/rsa_simple.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ 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 { cipherMessage } = await encrypt(keyring, plainText, { encryptionContext: context })
72+
const { ciphertext } = await encrypt(keyring, plainText, { encryptionContext: context })
7373

7474
/* Log the plain text
7575
* only for testing and to show that it works.
@@ -80,11 +80,11 @@ import { toBase64 } from '@aws-sdk/util-base64-browser'
8080
/* Log the base64-encoded ciphertext
8181
* so that you can try decrypting it with another AWS Encryption SDK implementation.
8282
*/
83-
const cipherMessageBase64 = toBase64(cipherMessage)
84-
console.log(cipherMessageBase64)
85-
document.write(cipherMessageBase64)
83+
const ciphertextBase64 = toBase64(ciphertext)
84+
console.log(ciphertextBase64)
85+
document.write(ciphertextBase64)
8686

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

8989
/* Grab the encryption context so you can verify it. */
9090
const { encryptionContext } = messageHeader
@@ -105,6 +105,6 @@ import { toBase64 } from '@aws-sdk/util-base64-browser'
105105
/* Log the clear message
106106
* only for testing and to show that it works.
107107
*/
108-
document.write('</br>clearMessage:' + clearMessage)
109-
console.log(clearMessage)
108+
document.write('</br>plaintext:' + plaintext)
109+
console.log(plaintext)
110110
})()

modules/integration-browser/src/integration.decrypt.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ describe('browser decryption vectors', function () {
4040
const good = fromBase64(plainText)
4141
try {
4242
const cmm = await decryptMaterialsManagerWebCrypto(keysInfo)
43-
const { clearMessage } = await decrypt(cmm, cipher)
44-
expect(good).toEqual(clearMessage)
43+
const { plaintext } = await decrypt(cmm, cipher)
44+
expect(good).toEqual(plaintext)
4545
} catch (e) {
4646
if (!notSupportedMessages.includes(e.message)) throw e
4747
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ describe('browser encrypt tests', function () {
4242
const plainText = fromBase64(plainTextData)
4343
try {
4444
const cmm = await encryptMaterialsManagerWebCrypto(keysInfo)
45-
const { cipherMessage } = await encrypt(cmm, plainText, encryptOp)
45+
const { ciphertext } = await encrypt(cmm, plainText, encryptOp)
4646
const response = await fetch(decryptOracle, {
4747
method: 'POST',
4848
headers: {
4949
'Content-Type': 'application/octet-stream',
5050
'Accept': 'application/octet-stream'
5151
},
52-
body: cipherMessage
52+
body: ciphertext
5353
})
5454
const body = await response.arrayBuffer()
5555
needs(response.ok, `Failed to decrypt: ${toUtf8(body)}`)

0 commit comments

Comments
 (0)