fix: Make decryption work

This commit is contained in:
PapaTutuWawa 2022-08-04 16:30:09 +02:00
parent 31d3897995
commit b745973188
3 changed files with 80 additions and 4 deletions

View File

@ -5,4 +5,5 @@ export 'src/errors.dart';
export 'src/helpers.dart'; export 'src/helpers.dart';
export 'src/keys.dart'; export 'src/keys.dart';
export 'src/omemo/bundle.dart'; export 'src/omemo/bundle.dart';
export 'src/omemo/sessionmanager.dart';
export 'src/x3dh/x3dh.dart'; export 'src/x3dh/x3dh.dart';

View File

@ -5,6 +5,7 @@ import 'package:omemo_dart/src/crypto.dart';
import 'package:omemo_dart/src/double_ratchet/double_ratchet.dart'; import 'package:omemo_dart/src/double_ratchet/double_ratchet.dart';
import 'package:omemo_dart/src/errors.dart'; import 'package:omemo_dart/src/errors.dart';
import 'package:omemo_dart/src/helpers.dart'; import 'package:omemo_dart/src/helpers.dart';
import 'package:omemo_dart/src/omemo/bundle.dart';
import 'package:omemo_dart/src/omemo/device.dart'; import 'package:omemo_dart/src/omemo/device.dart';
import 'package:synchronized/synchronized.dart'; import 'package:synchronized/synchronized.dart';
@ -36,6 +37,7 @@ class OmemoSessionManager {
/// Generate a new cryptographic identity. /// Generate a new cryptographic identity.
static Future<OmemoSessionManager> generateNewIdentity({ int opkAmount = 100 }) async { static Future<OmemoSessionManager> generateNewIdentity({ int opkAmount = 100 }) async {
assert(opkAmount > 0, 'opkAmount must be bigger than 0.');
final device = await Device.generateNewDevice(opkAmount: opkAmount); final device = await Device.generateNewDevice(opkAmount: opkAmount);
return OmemoSessionManager(device); return OmemoSessionManager(device);
@ -73,6 +75,10 @@ class OmemoSessionManager {
}); });
} }
Future<void> addSessionFromBundle(String jid, String deviceId, OmemoBundle bundle) async {
// TODO(PapaTutuWawa): Do
}
/// Encrypt the key [plaintext] for all known bundles of [jid]. Returns a map that /// Encrypt the key [plaintext] for all known bundles of [jid]. Returns a map that
/// maps the Bundle Id to the ciphertext of [plaintext]. /// maps the Bundle Id to the ciphertext of [plaintext].
Future<EncryptionResult> encryptToJid(String jid, String plaintext) async { Future<EncryptionResult> encryptToJid(String jid, String plaintext) async {
@ -87,7 +93,7 @@ class OmemoSessionManager {
keys.iv, keys.iv,
); );
final hmac = await truncatedHmac(ciphertext, keys.authenticationKey); final hmac = await truncatedHmac(ciphertext, keys.authenticationKey);
final concatKey = concat([keys.encryptionKey, hmac]); final concatKey = concat([key, hmac]);
await _lock.synchronized(() async { await _lock.synchronized(() async {
// We assume that the user already checked if the session exists // We assume that the user already checked if the session exists
@ -127,7 +133,7 @@ class OmemoSessionManager {
final message = OMEMOMessage.fromBuffer(authMessage.message); final message = OMEMOMessage.fromBuffer(authMessage.message);
final ratchet = _ratchetMap[senderDeviceId]!; final ratchet = _ratchetMap[senderDeviceId]!;
final keyAndHmac = await ratchet.ratchetDecrypt(message, message.ciphertext); final keyAndHmac = await ratchet.ratchetDecrypt(message, decodedRawKey);
final key = keyAndHmac.sublist(0, 32); final key = keyAndHmac.sublist(0, 32);
final hmac = keyAndHmac.sublist(32, 48); final hmac = keyAndHmac.sublist(32, 48);
final derivedKeys = await deriveEncryptionKeys(key, omemoPayloadInfoString); final derivedKeys = await deriveEncryptionKeys(key, omemoPayloadInfoString);

69
test/omemo_test.dart Normal file
View File

@ -0,0 +1,69 @@
import 'dart:convert';
import 'package:omemo_dart/omemo_dart.dart';
import 'package:test/test.dart';
void main() {
test('Test using OMEMO sessions with only one device per user', () async {
const aliceJid = 'alice@server.example';
const bobJid = 'bob@other.server.example';
// Alice and Bob generate their sessions
final aliceSession = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
final bobSession = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
// Perform the X3DH
final x3dhAliceResult = await x3dhFromBundle(
await bobSession.device.toBundle(),
aliceSession.device.ik,
);
final x3dhBobResult = await x3dhFromInitialMessage(
X3DHMessage(
aliceSession.device.ik.pk,
x3dhAliceResult.ek.pk,
'2',
),
bobSession.device.spk,
bobSession.device.opks.values.elementAt(0),
bobSession.device.ik,
);
// Build the ratchets
final aliceRatchet = await OmemoDoubleRatchet.initiateNewSession(
bobSession.device.spk.pk,
x3dhAliceResult.sk,
x3dhAliceResult.ad,
);
final bobRatchet = await OmemoDoubleRatchet.acceptNewSession(
bobSession.device.spk,
x3dhBobResult.sk,
x3dhBobResult.ad,
);
// Add the ratchets to the session managers
await aliceSession.addSession(bobJid, bobSession.device.id, aliceRatchet);
await bobSession.addSession(aliceJid, aliceSession.device.id, bobRatchet);
// Alice encrypts a message for Bob
const messagePlaintext = 'Hello Bob!';
final aliceMessage = await aliceSession.encryptToJid(bobJid, messagePlaintext);
expect(aliceMessage.encryptedKeys.length, 1);
// Alice sends the message to Bob
// ...
// Bob decrypts it
final bobMessage = await bobSession.decryptMessage(
aliceMessage.ciphertext,
aliceJid,
aliceSession.device.id,
[
EncryptedKey(
bobSession.device.id,
base64.encode(aliceMessage.encryptedKeys[bobSession.device.id]!),
),
],
);
expect(messagePlaintext, bobMessage);
});
}