Skip to content

Commit e811d59

Browse files
committed
fix: sealed box works
1 parent d7cc500 commit e811d59

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

src/common/crypto/aes-sealed.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ describe('aes Encryption and Decryption', () => {
5353
const dataFromKey = await crypto.subtle.exportKey('raw', key)
5454
expect(toBase64(dataFromKey)).toMatchInlineSnapshot(`"UDl7buu/Zn/UxCIEp55MOTOKcDHvb959P+eyozok7BA="`)
5555

56-
const sample = new Uint8Array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
57-
// const encryptedData = await hxEncrypt(sample, key)
58-
// expect(toBase64(encryptedData)).toMatchInlineSnapshot(
59-
// `"WbwhO/CaS3W4VxiIIG3Zs4WdEa08CzEFgaQLs0TrRDejZ/8G0yu0S1JIV+3bn79O7SeyCfI0"`,
60-
// )
56+
// Two ways
57+
// const sample = new Uint8Array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
58+
// const encryptedData2 = await hxEncrypt(sample, key)
59+
// expect(toBase64(encryptedData2)).toMatchInlineSnapshot(`"Eo7AexzlYXmV8gEZUE1ai68ZetTwfh8KSQ6kKffhvHAOemZ4C1c="`)
6160

61+
// From Swift sample
6262
const encryptedData = fromBase64('AQIDBAUGBwgJCgsMqZ0hGHIvSPG5x0d12q42zezDEJVu8Ic3yB4=')
6363
const decryptedData = await hxDecrypt(encryptedData, key)
6464
expect(decryptedData).toMatchInlineSnapshot(`

src/common/crypto/aes-sealed.ts

+11-18
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
1-
export async function hxEncrypt(data: Uint8Array, key: CryptoKey, tag?: Uint8Array): Promise<Uint8Array> {
2-
const iv = crypto.getRandomValues(new Uint8Array(12)) // AES-GCM requires a 12-byte IV
3-
if (!tag) {
4-
tag = crypto.getRandomValues(new Uint8Array(16))
5-
}
1+
const tagLength = 128
2+
const authenticating_default = new Uint8Array()
63

4+
export async function hxEncrypt(data: Uint8Array, key: CryptoKey, authenticating: Uint8Array = authenticating_default): Promise<Uint8Array> {
5+
const iv = crypto.getRandomValues(new Uint8Array(12)) // AES-GCM requires a 12-byte IV
76
const encrypted = await crypto.subtle.encrypt(
87
{
98
name: 'AES-GCM',
109
iv,
11-
tagLength: 128,
12-
additionalData: tag,
10+
tagLength,
11+
additionalData: authenticating,
1312
},
1413
key,
1514
data,
1615
)
1716

1817
const encryptedArray = new Uint8Array(encrypted)
19-
const combined = new Uint8Array(iv.length + encryptedArray.length + tag.length)
18+
const combined = new Uint8Array(iv.length + encryptedArray.length)
2019
combined.set(iv)
2120
combined.set(encryptedArray, iv.length)
22-
combined.set(tag, encryptedArray.length + iv.length)
2321
return combined
2422
}
2523

26-
export async function hxDecrypt(data: Uint8Array, key: CryptoKey): Promise<Uint8Array> {
27-
// The data layout of the combined representation is nonce, ciphertext, then tag.
28-
// The nonce is 12 bytes, the tag is 16 bytes, and the ciphertext is the rest of the data.
24+
export async function hxDecrypt(data: Uint8Array, key: CryptoKey, authenticating: Uint8Array = authenticating_default): Promise<Uint8Array> {
2925
const iv = data.slice(0, 12) // nonce is the first 12 bytes
30-
const encrypted = data.slice(12) // The ciphertext is everything between the nonce and the tag.
31-
// const tag = data.slice(-16) // The authentication tag has a length of 16 bytes.
32-
console.log({ iv, encrypted, data })
33-
26+
const encrypted = data.slice(12) // ciphertext and tag of 128 bits
3427
const decrypted = await crypto.subtle.decrypt({
3528
name: 'AES-GCM',
3629
iv,
37-
// tagLength: 32, // tag.length * 8,
38-
additionalData: new Uint8Array(),
30+
tagLength,
31+
additionalData: authenticating,
3932
}, key, encrypted)
4033
return new Uint8Array(decrypted)
4134
}

0 commit comments

Comments
 (0)