Skip to content

MatrixRTC: Refactor | Introduce a new Encryption manager (used with experimental to device transport) #4799

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

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from

Conversation

BillCarsonFr
Copy link
Member

@BillCarsonFr BillCarsonFr commented Apr 11, 2025

Add a new simplified EncryptionManager that will rotate the key for any membership change (join or leave or same)

There is no specific experimental flag to use it, it will use it for to device transport

Checklist

  • Tests written for new code (and old code if feasible).
  • New or updated public/exported symbols have accurate TSDoc documentation.
  • Linter and other CI checks pass.
  • Sign-off given on the changes (see CONTRIBUTING.md).

@BillCarsonFr BillCarsonFr added the T-Task Tasks for the team like planning label Apr 11, 2025
participantId: ParticipantId,
) => void,
) {
this.logger = logger.getChild("BasicEncryptionManager");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the parent logger concept here (pass the logger down via a prop)

* A simple encryption manager.
* This manager is basic becasue it will rotate the keys for any membership change.
* There is no ratchetting, or time based rotation.
* It works with to-device transport.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should work with both right: room & toDevice

/**
* There is a possibility that keys arrive in wrong order.
* For example after a quick join/leave/join, there will be 2 keys of index 0 distributed and
* it they are received in wrong order the stream won't be decryptable.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* it they are received in wrong order the stream won't be decryptable.
* if they are received in wrong order the stream won't be decryptable.

Comment on lines 84 to 87
public getEncryptionKeys(): Map<string, Array<{ key: Uint8Array; timestamp: number }>> {
// This is deprecated should be ignored. Only use by tests?
return new Map();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deprecation could already be added to IEncryptionManager (@deprecated)

}
}

private nextKeyId(): number {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private nextKeyId(): number {
private nextKeyIndex(): number {

}, this.ttl);

const existing = entry.keys.get(item.keyId);
if (existing && existing.creationTS > item.creationTS) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

creationTS, timestamp, send_ts is all the same thing right?

Is it possible to call them the same (or maybe sendTs + send_ts)
(edit: oh its: sent_ts... Is that what we use in the spec.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they are the same. CreationTS would be the time when the key is created, sent_ts is the local time when we sent it (for new joiner without rotation they would be different).
yes sent_ts is from the spec.
Maybe we need both? CreationTS is better to disambiguate keys, but sent_ts is what we want for statistics to have an estimate of the time to transport

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the spec we should make sent_ts -> creation_ts then right? the spec should focus on the disambiguation instead of the possibility to collect reasonable stats.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure the time to live is the correct concept here. It would not work if the out of order receive offset time is larger than 1s.
Can we just use a "latest key buffer"? Of course we would end up with more memory use since a 100ppl call would have 100 entries where the ttl approach probably would delete them quick enough so it would never exceed a handful.

The latest key buffer would just store the most recent (by timestamp) key for the highest (considering wrap around) index.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I think we could do without it complitely as it is probably very edge casy. We also want to avoid keeping key in memory.
Only keeping the latest (1 key) might not be enough. If there was 3 keys 0, 1, 0' that are received in 0' 1 , 0, the latest would be 1 but we want 0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only keeping the latest (1 key) might not be enough. If there was 3 keys 0, 1, 0' that are received in 0' 1 , 0, the latest would be 1 but we want 0

Yes covering all cases requies us to store all keys ever received.
What about we store all timestamps ever received? That is enough right. The disambigution is then just a "is it the most recent key we want to use" check.
Could be called keyRecencyMap.isMostRecentKeyForParticipant(userId, key.creationTs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-Task Tasks for the team like planning
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants