Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support decrypting empty ciphertext #374

Merged
merged 3 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/jwe/compact/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export async function compactDecrypt(

const decrypted = await flattenedDecrypt(
{
ciphertext: <string>(ciphertext || undefined),
ciphertext,
iv: <string>(iv || undefined),
protected: protectedHeader || undefined,
tag: <string>(tag || undefined),
Expand Down
13 changes: 12 additions & 1 deletion test/jwe/compact.decrypt.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'

const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto'
const { compactDecrypt } = await import(root)
const { CompactEncrypt, compactDecrypt } = await import(root)

test('JWE format validation', async (t) => {
await t.throwsAsync(compactDecrypt(null, new Uint8Array(0)), {
Expand All @@ -13,3 +13,14 @@ test('JWE format validation', async (t) => {
code: 'ERR_JWE_INVALID',
})
})

test('decrypt empty data', async (t) => {
const jwe = await new CompactEncrypt(new Uint8Array(0))
.setProtectedHeader({ alg: 'dir', enc: 'A128GCM' })
.encrypt(new Uint8Array(16))

t.is(jwe.split('.')[3], '')

const { plaintext } = await compactDecrypt(jwe, new Uint8Array(16))
t.is(plaintext.byteLength, 0)
})
11 changes: 11 additions & 0 deletions test/jwe/flattened.decrypt.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,14 @@ test('AES CBC + HMAC', async (t) => {
})
}
})

test('decrypt empty data', async (t) => {
const jwe = await new FlattenedEncrypt(new Uint8Array(0))
.setProtectedHeader({ alg: 'dir', enc: 'A128GCM' })
.encrypt(new Uint8Array(16))

t.is(jwe.ciphertext, '')

const { plaintext } = await flattenedDecrypt(jwe, new Uint8Array(16))
t.is(plaintext.byteLength, 0)
})
12 changes: 12 additions & 0 deletions test/jwe/general.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,15 @@ test('General JWE format validation', async (t) => {
await t.notThrowsAsync(generalDecrypt(jwe, t.context.secret))
}
})

test('decrypt empty data', async (t) => {
const jwe = await new GeneralEncrypt(new Uint8Array(0))
.setProtectedHeader({ alg: 'dir', enc: 'A128GCM' })
.addRecipient(new Uint8Array(16))
.encrypt()

t.is(jwe.ciphertext, '')

const { plaintext } = await generalDecrypt(jwe, new Uint8Array(16))
t.is(plaintext.byteLength, 0)
})