2022-08-01 20:08:38 +00:00
|
|
|
import 'package:cryptography/cryptography.dart';
|
2022-06-30 12:00:18 +00:00
|
|
|
import 'package:test/test.dart';
|
2022-08-01 20:08:38 +00:00
|
|
|
import 'package:omemo_dart/src/x3dh/x3dh.dart';
|
|
|
|
|
|
|
|
Future<List<int>> publicKeyBytes(SimpleKeyPair kp) async {
|
|
|
|
final pk = await kp.extractPublicKey();
|
|
|
|
return pk.bytes;
|
|
|
|
}
|
|
|
|
|
2022-06-30 12:00:18 +00:00
|
|
|
void main() {
|
2022-08-01 20:08:38 +00:00
|
|
|
test("X3DH", () async {
|
|
|
|
final ed = Ed25519();
|
|
|
|
final x = Cryptography.instance.x25519();
|
|
|
|
|
|
|
|
// Generate IKs for Alice and Bob
|
2022-08-01 21:20:31 +00:00
|
|
|
final ikAlice = await ed.newKeyPair();
|
|
|
|
final ikBob = await ed.newKeyPair();
|
2022-08-01 20:08:38 +00:00
|
|
|
|
|
|
|
// Generate SPKs for Alice and Bob
|
|
|
|
final spkAlice = await x.newKeyPair();
|
|
|
|
final spkSigAlice = await sig(ikAlice, await publicKeyBytes(spkAlice));
|
|
|
|
final spkBob = await x.newKeyPair();
|
|
|
|
final spkSigBob = await sig(ikBob, await publicKeyBytes(spkBob));
|
|
|
|
|
|
|
|
// Generate an OPK for Alice and Bob
|
|
|
|
final opkAlice = await x.newKeyPair();
|
|
|
|
final opkBob = await x.newKeyPair();
|
|
|
|
|
|
|
|
|
|
|
|
// Perform X3DH
|
|
|
|
final aliceMessage = await x3dhFromPrekeyBundle(
|
|
|
|
await ikBob.extractPublicKey(),
|
|
|
|
await spkBob.extractPublicKey(),
|
|
|
|
await opkBob.extractPublicKey(),
|
|
|
|
ikAlice,
|
|
|
|
);
|
2022-06-30 12:00:18 +00:00
|
|
|
|
2022-08-01 20:08:38 +00:00
|
|
|
final bobDh = await x3dhFromInitialMessage(
|
|
|
|
await ikAlice.extractPublicKey(),
|
|
|
|
await aliceMessage.epk.extractPublicKey(),
|
|
|
|
opkBob,
|
|
|
|
spkBob,
|
|
|
|
ikBob,
|
|
|
|
);
|
2022-06-30 12:00:18 +00:00
|
|
|
|
2022-08-01 20:08:38 +00:00
|
|
|
expect(aliceMessage.sharedSecret, bobDh);
|
2022-06-30 12:00:18 +00:00
|
|
|
});
|
|
|
|
}
|