SessionCipher class
Encrypts and decrypts messages using an established Signal Protocol session.
The SessionCipher handles the Double Ratchet algorithm for message encryption and decryption. Before using SessionCipher, a session must be established using SessionBuilder.
Example usage:
final cipher = SessionCipher(
localAddress: myAddress,
sessionStore: mySessionStore,
identityKeyStore: myIdentityKeyStore,
preKeyStore: myPreKeyStore,
signedPreKeyStore: mySignedPreKeyStore,
kyberPreKeyStore: myKyberPreKeyStore,
);
// Encrypt a message to Bob
final ciphertext = await cipher.encrypt(bobAddress, utf8.encode('Hello!'));
// Decrypt a message from Alice
final plaintext = await cipher.decrypt(aliceAddress, ciphertext);
Ratchet state: what the library guarantees, and what you must do
Every operation here advances the Double Ratchet, and the library persists
the advanced state through your stores before it hands back the
ciphertext or plaintext: storeSession — plus saveIdentity,
removePreKey and markKyberPreKeyUsed where they apply — are awaited
first. The ordering "persist, then release the output" therefore holds by
construction, on this and every other entry point in this package.
Two things are still yours to get right, and both are silent failures:
-
Durability. Your store callbacks must not complete their futures until the write is durable, or you must commit a transaction opened around this call before you send the ciphertext / act on the plaintext. Otherwise a crash after the callback returned but before the bytes reached stable storage rewinds the ratchet, and the next send re-derives a message key and IV that were already used.
-
Serialization. Never run two operations for the same remote address concurrently. encrypt loads the session, advances it and stores it back; two overlapping calls both start from the same record and derive the same message key, with no crash needed. A lock inside the store does not help, because the whole load → ratchet → store cycle is the critical section:
// One map per store instance — it is the store that needs serializing, // so do not scope it to a single (possibly short-lived) SessionCipher. final locks = <String, Lock>{}; // package:synchronized Future<CiphertextMessage> send(ProtocolAddress to, Uint8List body) { final key = '${to.name()}:${to.deviceId()}'; return locks .putIfAbsent(key, Lock.new) .synchronized(() => cipher.encrypt(to, body)); }
See SessionStore and SECURITY.md for the full contract.
Constructors
- SessionCipher({required ProtocolAddress localAddress, required SessionStore sessionStore, required IdentityKeyStore identityKeyStore, required PreKeyStore preKeyStore, required SignedPreKeyStore signedPreKeyStore, required KyberPreKeyStore kyberPreKeyStore})
- Creates a new SessionCipher with the given stores.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
decrypt(
ProtocolAddress remoteAddress, CiphertextMessage ciphertext) → Future< Uint8List> - Decrypts a message from the remote user.
-
decryptPreKeyMessage(
ProtocolAddress remoteAddress, Uint8List ciphertext) → Future< Uint8List> - Decrypts a pre-key Signal message (for session establishment).
-
decryptSignalMessage(
ProtocolAddress remoteAddress, Uint8List ciphertext) → Future< Uint8List> - Decrypts a Signal message (regular message, not pre-key).
-
encrypt(
ProtocolAddress remoteAddress, Uint8List plaintext) → Future< CiphertextMessage> - Encrypts a message for the remote user.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited