feat: IMPLEMENT THE DOUBLE RATCHET WITH X3DH

This commit is contained in:
PapaTutuWawa 2022-08-03 15:29:13 +02:00
parent 4e3e20f08c
commit 1a0f470ada
2 changed files with 28 additions and 14 deletions

View File

@ -113,9 +113,9 @@ class OmemoDoubleRatchet {
cks = newCks; cks = newCks;
final header = OMEMOMessage() final header = OMEMOMessage()
..n = ns ..dhPub = await dhs.pk.getBytes()
..pn = pn ..pn = pn
..dhPub = await dhs.pk.getBytes(); ..n = ns;
ns++; ns++;
@ -127,8 +127,7 @@ class OmemoDoubleRatchet {
Future<List<int>?> trySkippedMessageKeys(OMEMOMessage header, List<int> ciphertext) async { Future<List<int>?> trySkippedMessageKeys(OMEMOMessage header, List<int> ciphertext) async {
final key = SkippedKey( final key = SkippedKey(
// TODO(PapaTutuWawa): Is this correct OmemoPublicKey.fromBytes(header.dhPub, KeyPairType.x25519),
OmemoPublicKey.fromBytes(header.dhPub, KeyPairType.ed25519),
header.n, header.n,
); );
if (mkSkipped.containsKey(key)) { if (mkSkipped.containsKey(key)) {
@ -162,13 +161,13 @@ class OmemoDoubleRatchet {
pn = header.n; pn = header.n;
ns = 0; ns = 0;
nr = 0; nr = 0;
dhr = OmemoPublicKey.fromBytes(header.dhPub, KeyPairType.ed25519); dhr = OmemoPublicKey.fromBytes(header.dhPub, KeyPairType.x25519);
final newRk = await kdfRk(rk, await dh(dhs, dhr!, 2)); final newRk = await kdfRk(rk, await dh(dhs, dhr!, 0));
rk = newRk; rk = newRk;
ckr = newRk; ckr = newRk;
dhs = await OmemoKeyPair.generateNewPair(KeyPairType.x25519); dhs = await OmemoKeyPair.generateNewPair(KeyPairType.x25519);
final newNewRk = await kdfRk(rk, await dh(dhs, dhr!, 2)); final newNewRk = await kdfRk(rk, await dh(dhs, dhr!, 0));
rk = newNewRk; rk = newNewRk;
cks = newNewRk; cks = newNewRk;
} }

View File

@ -1,3 +1,4 @@
// ignore_for_file: avoid_print
import 'dart:convert'; import 'dart:convert';
import 'package:cryptography/cryptography.dart'; import 'package:cryptography/cryptography.dart';
import 'package:omemo_dart/omemo_dart.dart'; import 'package:omemo_dart/omemo_dart.dart';
@ -33,7 +34,6 @@ void main() {
expect(decrypted, plaintext); expect(decrypted, plaintext);
}); });
/*
test('Test the Double Ratchet', () async { test('Test the Double Ratchet', () async {
// Generate keys // Generate keys
final ikAlice = await OmemoKeyPair.generateNewPair(KeyPairType.ed25519); final ikAlice = await OmemoKeyPair.generateNewPair(KeyPairType.ed25519);
@ -90,20 +90,35 @@ void main() {
//expect(await alicesRatchet.dhr.getBytes(), await ikBob.pk.getBytes()); //expect(await alicesRatchet.dhr.getBytes(), await ikBob.pk.getBytes());
// Alice encrypts a message // Alice encrypts a message
final aliceRatchetResult = await alicesRatchet.ratchetEncrypt(utf8.encode('Hello Bob')); final aliceRatchetResult1 = await alicesRatchet.ratchetEncrypt(utf8.encode('Hello Bob'));
print('Alice sent the message'); print('Alice sent the message');
// Alice sends it to Bob // Alice sends it to Bob
// ... // ...
// Bob tries to decrypt it // Bob tries to decrypt it
final bobRatchetResult = await bobsRatchet.ratchetDecrypt( final bobRatchetResult1 = await bobsRatchet.ratchetDecrypt(
aliceRatchetResult.header, aliceRatchetResult1.header,
aliceRatchetResult.ciphertext, aliceRatchetResult1.ciphertext,
); );
print('Bob decrypted the message'); print('Bob decrypted the message');
expect(utf8.encode('Hello Bob'), bobRatchetResult); expect(utf8.encode('Hello Bob'), bobRatchetResult1);
// Bob sends a message to Alice
final bobRatchetResult2 = await bobsRatchet.ratchetEncrypt(utf8.encode('Hello Alice'));
print('Bob sent the message');
// Bobs sends it to Alice
// ...
// Alice tries to decrypt it
final aliceRatchetResult2 = await alicesRatchet.ratchetDecrypt(
bobRatchetResult2.header,
bobRatchetResult2.ciphertext,
);
print('Alice decrypted the message');
expect(utf8.encode('Hello Alice'), aliceRatchetResult2);
}); });
*/
} }