Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/data-cryptor-ratchet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Fix data packet decryption after the sender ratchets its key: `DataCryptor` now retries with the ratcheted key instead of the stored one
43 changes: 42 additions & 1 deletion src/e2ee/worker/DataCryptor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('DataCryptor', () => {
const plainData = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);

// Sender ratchets their key forward
await senderKeys.ratchetKey(1, false);
await senderKeys.ratchetKey(1);

// Sender encrypts data with the ratcheted key
const encrypted = await DataCryptor.encrypt(plainData, senderKeys);
Expand All @@ -142,6 +142,47 @@ describe('DataCryptor', () => {
expect(decrypted.payload).toEqual(plainData);
});

it('keeps the ratcheted key after decrypting a packet from a ratcheted sender', async () => {
const senderKeys = prepareParticipantTestKeys('sender', {
ratchetWindowSize: 4,
});
const receiverKeys = prepareParticipantTestKeys('receiver', {
ratchetWindowSize: 4,
});

const initialMaterial = await createKeyMaterialFromString('test-key');
await senderKeys.setKey(initialMaterial, 1);
await receiverKeys.setKey(initialMaterial, 1);
const receiverInitialKeySet = receiverKeys.getKeySet(1);

// Sender ratchets once, so the receiver is one ratchet step behind
await senderKeys.ratchetKey(1);

const first = new Uint8Array([1, 2, 3, 4]);
const encryptedFirst = await DataCryptor.encrypt(first, senderKeys);
const decryptedFirst = await DataCryptor.decrypt(
encryptedFirst.payload,
encryptedFirst.iv,
receiverKeys,
encryptedFirst.keyIndex,
);
expect(decryptedFirst.payload).toEqual(first);

// The receiver now stores the ratcheted key, so a later packet decrypts without ratcheting
expect(receiverKeys.getKeySet(1)).not.toBe(receiverInitialKeySet);
const ratchetSpy = vitest.spyOn(receiverKeys, 'ratchetKey');
const second = new Uint8Array([5, 6, 7, 8]);
const encryptedSecond = await DataCryptor.encrypt(second, senderKeys);
const decryptedSecond = await DataCryptor.decrypt(
encryptedSecond.payload,
encryptedSecond.iv,
receiverKeys,
encryptedSecond.keyIndex,
);
expect(decryptedSecond.payload).toEqual(second);
expect(ratchetSpy).not.toHaveBeenCalled();
});

it('respects ratchet window size limit', async () => {
// Create a scenario where we have valid encrypted data that requires ratcheting but it's disabled
const senderKeys = prepareParticipantTestKeys('sender', {
Expand Down
4 changes: 2 additions & 2 deletions src/e2ee/worker/DataCryptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class DataCryptor {
name: ENCRYPTION_ALGORITHM,
iv,
},
keySet.encryptionKey,
ratchetOpts.encryptionKey ?? keySet.encryptionKey,
new Uint8Array(data),
);
return {
Expand Down Expand Up @@ -103,7 +103,7 @@ export class DataCryptor {
iv,
keys,
keyIndex,
initialMaterial,
initialMaterial ?? keySet,
{
ratchetCount: ratchetOpts.ratchetCount + 1,
encryptionKey: ratchetedKeySet?.encryptionKey,
Expand Down
Loading