diff --git a/lib/src/double_ratchet.dart b/lib/src/double_ratchet.dart index 04cf144..8a0c3e3 100644 --- a/lib/src/double_ratchet.dart +++ b/lib/src/double_ratchet.dart @@ -113,9 +113,9 @@ class OmemoDoubleRatchet { cks = newCks; final header = OMEMOMessage() - ..n = ns + ..dhPub = await dhs.pk.getBytes() ..pn = pn - ..dhPub = await dhs.pk.getBytes(); + ..n = ns; ns++; @@ -127,8 +127,7 @@ class OmemoDoubleRatchet { Future?> trySkippedMessageKeys(OMEMOMessage header, List ciphertext) async { final key = SkippedKey( - // TODO(PapaTutuWawa): Is this correct - OmemoPublicKey.fromBytes(header.dhPub, KeyPairType.ed25519), + OmemoPublicKey.fromBytes(header.dhPub, KeyPairType.x25519), header.n, ); if (mkSkipped.containsKey(key)) { @@ -162,13 +161,13 @@ class OmemoDoubleRatchet { pn = header.n; ns = 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; ckr = newRk; 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; cks = newNewRk; } diff --git a/test/double_ratchet_test.dart b/test/double_ratchet_test.dart index ac464eb..5c527ee 100644 --- a/test/double_ratchet_test.dart +++ b/test/double_ratchet_test.dart @@ -1,3 +1,4 @@ +// ignore_for_file: avoid_print import 'dart:convert'; import 'package:cryptography/cryptography.dart'; import 'package:omemo_dart/omemo_dart.dart'; @@ -33,7 +34,6 @@ void main() { expect(decrypted, plaintext); }); - /* test('Test the Double Ratchet', () async { // Generate keys final ikAlice = await OmemoKeyPair.generateNewPair(KeyPairType.ed25519); @@ -90,20 +90,35 @@ void main() { //expect(await alicesRatchet.dhr.getBytes(), await ikBob.pk.getBytes()); // 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'); // Alice sends it to Bob // ... // Bob tries to decrypt it - final bobRatchetResult = await bobsRatchet.ratchetDecrypt( - aliceRatchetResult.header, - aliceRatchetResult.ciphertext, + final bobRatchetResult1 = await bobsRatchet.ratchetDecrypt( + aliceRatchetResult1.header, + aliceRatchetResult1.ciphertext, ); 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); }); - */ }