Skip to content

Commit d7cc500

Browse files
committed
fix: aes-sealed matched swift much better now
1 parent 2353071 commit d7cc500

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

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

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { beforeAll, describe, expect, it } from 'vitest'
22
import { deriveKeyPbkdf2 } from '../crypto'
3-
import { toBase64 } from '../data/bin'
3+
import { fromBase64, toBase64 } from '../data/bin'
44
import { hxDecrypt, hxEncrypt } from './aes-sealed'
55

66
describe('aes Encryption and Decryption', () => {
@@ -46,20 +46,34 @@ describe('aes Encryption and Decryption', () => {
4646
})
4747

4848
it('should decrypt a sample that was generated by Swift code', async () => {
49-
const key = await deriveKeyPbkdf2(new Uint8Array([1, 2, 3]), {
50-
salt: new Uint8Array([1, 2, 3]),
49+
const key = await deriveKeyPbkdf2(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), {
50+
salt: new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
5151
iterations: 100000,
5252
})
5353
const dataFromKey = await crypto.subtle.exportKey('raw', key)
54-
expect(toBase64(dataFromKey)).toMatchInlineSnapshot(`"+EZYmOnuVat0rvokPjZxkwdGacOiFvR2oaukTeVa84M="`)
54+
expect(toBase64(dataFromKey)).toMatchInlineSnapshot(`"UDl7buu/Zn/UxCIEp55MOTOKcDHvb959P+eyozok7BA="`)
5555

5656
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-
)
61-
// const encryptedData = fromBase64('br6sc+pnZaIXcV1fTygAs/UJlDZIIBY50i56MMGNampZTcSakt0=')
62-
// const decryptedData = await hxDecrypt(encryptedData, key)
63-
// expect(decryptedData).toMatchInlineSnapshot()
57+
// const encryptedData = await hxEncrypt(sample, key)
58+
// expect(toBase64(encryptedData)).toMatchInlineSnapshot(
59+
// `"WbwhO/CaS3W4VxiIIG3Zs4WdEa08CzEFgaQLs0TrRDejZ/8G0yu0S1JIV+3bn79O7SeyCfI0"`,
60+
// )
61+
62+
const encryptedData = fromBase64('AQIDBAUGBwgJCgsMqZ0hGHIvSPG5x0d12q42zezDEJVu8Ic3yB4=')
63+
const decryptedData = await hxDecrypt(encryptedData, key)
64+
expect(decryptedData).toMatchInlineSnapshot(`
65+
Uint8Array [
66+
9,
67+
8,
68+
7,
69+
6,
70+
5,
71+
4,
72+
3,
73+
2,
74+
1,
75+
0,
76+
]
77+
`)
6478
})
6579
})

src/common/crypto/aes-sealed.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ export async function hxDecrypt(data: Uint8Array, key: CryptoKey): Promise<Uint8
2727
// The data layout of the combined representation is nonce, ciphertext, then tag.
2828
// The nonce is 12 bytes, the tag is 16 bytes, and the ciphertext is the rest of the data.
2929
const iv = data.slice(0, 12) // nonce is the first 12 bytes
30-
const encrypted = data.slice(12, -16) // 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, tag })
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 })
3333

3434
const decrypted = await crypto.subtle.decrypt({
3535
name: 'AES-GCM',
3636
iv,
37-
tagLength: 128,
38-
additionalData: tag,
37+
// tagLength: 32, // tag.length * 8,
38+
additionalData: new Uint8Array(),
3939
}, key, encrypted)
4040
return new Uint8Array(decrypted)
4141
}

0 commit comments

Comments
 (0)