omemo_dart/test/x3dh_test.dart

76 lines
2.4 KiB
Dart
Raw Normal View History

2022-08-02 13:40:26 +00:00
import 'dart:convert';
2022-08-01 20:08:38 +00:00
import 'package:cryptography/cryptography.dart';
import 'package:omemo_dart/omemo_dart.dart';
2022-08-02 13:10:31 +00:00
import 'package:test/test.dart';
2022-08-01 20:08:38 +00:00
2022-06-30 12:00:18 +00:00
void main() {
2022-08-02 13:40:26 +00:00
test('X3DH with correct signature', () async {
// Generate keys
final ikAlice = await OmemoKeyPair.generateNewPair(KeyPairType.ed25519);
final ikBob = await OmemoKeyPair.generateNewPair(KeyPairType.ed25519);
final spkBob = await OmemoKeyPair.generateNewPair(KeyPairType.x25519);
final opkBob = await OmemoKeyPair.generateNewPair(KeyPairType.x25519);
final bundleBob = OmemoBundle(
'alice@some.server',
2022-08-04 14:57:12 +00:00
1,
await spkBob.pk.asBase64(),
2022-08-04 14:57:12 +00:00
3,
2022-08-02 13:40:26 +00:00
base64Encode(
await sig(ikBob, await spkBob.pk.getBytes()),
),
//'Q5in+/L4kJixEX692h6mJkPMyp4I3SlQ84L0E7ipPzqfPHOMiraUlqG2vG/O8wvFjLsKYZpPBraga9IvwhqVDA==',
await ikBob.pk.asBase64(),
{
2022-08-04 14:57:12 +00:00
2: await opkBob.pk.asBase64(),
},
);
2023-06-12 17:20:43 +00:00
// Alice does X3DH
final resultAliceRaw = await x3dhFromBundle(bundleBob, ikAlice);
final resultAlice = resultAliceRaw.get<X3DHAliceResult>();
2022-08-01 20:08:38 +00:00
// Alice sends the inital message to Bob
// ...
2023-06-12 17:20:43 +00:00
// Bob does X3DH
2022-08-02 13:26:00 +00:00
final resultBob = await x3dhFromInitialMessage(
X3DHMessage(
ikAlice.pk,
resultAlice.ek.pk,
2022-08-04 14:57:12 +00:00
2,
),
2022-08-01 20:08:38 +00:00
spkBob,
opkBob,
2022-08-01 20:08:38 +00:00
ikBob,
);
2023-06-12 17:20:43 +00:00
2022-08-02 13:26:00 +00:00
expect(resultAlice.sk, resultBob.sk);
expect(resultAlice.ad, resultBob.ad);
2022-06-30 12:00:18 +00:00
});
2022-08-02 13:40:26 +00:00
test('X3DH with incorrect signature', () async {
// Generate keys
final ikAlice = await OmemoKeyPair.generateNewPair(KeyPairType.ed25519);
final ikBob = await OmemoKeyPair.generateNewPair(KeyPairType.ed25519);
final spkBob = await OmemoKeyPair.generateNewPair(KeyPairType.x25519);
final opkBob = await OmemoKeyPair.generateNewPair(KeyPairType.x25519);
final bundleBob = OmemoBundle(
'bob@some.server',
2022-08-04 14:57:12 +00:00
1,
2022-08-02 13:40:26 +00:00
await spkBob.pk.asBase64(),
2022-08-04 14:57:12 +00:00
3,
2022-08-02 13:40:26 +00:00
// NOTE: A bit flakey, but it is highly unlikely that the same keypair as this one
// gets generated.
'Q5in+/L4kJixEX692h6mJkPMyp4I3SlQ84L0E7ipPzqfPHOMiraUlqG2vG/O8wvFjLsKYZpPBraga9IvwhqVDA==',
await ikBob.pk.asBase64(),
{
2022-08-04 14:57:12 +00:00
2: await opkBob.pk.asBase64(),
2022-08-02 13:40:26 +00:00
},
);
2023-06-12 17:20:43 +00:00
2022-08-02 13:40:26 +00:00
// Alice does X3DH
final result = await x3dhFromBundle(bundleBob, ikAlice);
expect(result.isType<InvalidKeyExchangeSignatureError>(), isTrue);
2022-08-02 13:40:26 +00:00
});
2022-06-30 12:00:18 +00:00
}