From 3376929c243d5bb0b01e3b1cc7ad882916d711f5 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Mon, 12 Jun 2023 19:37:45 +0200 Subject: [PATCH] style: Formattiing issues --- lib/src/crypto.dart | 20 +++++-- lib/src/double_ratchet/crypto.dart | 16 +++-- lib/src/double_ratchet/double_ratchet.dart | 48 +++++++++++---- lib/src/helpers.dart | 5 +- lib/src/keys.dart | 32 ++++++---- lib/src/omemo/device.dart | 6 +- lib/src/omemo/encryption_result.dart | 8 ++- lib/src/omemo/events.dart | 7 ++- lib/src/omemo/omemomanager.dart | 69 +++++++++++++++------- lib/src/trust/btbv.dart | 11 +++- lib/src/x3dh/x3dh.dart | 16 +++-- test/omemomanager_test.dart | 14 +++-- test/x3dh_test.dart | 7 ++- 13 files changed, 188 insertions(+), 71 deletions(-) diff --git a/lib/src/crypto.dart b/lib/src/crypto.dart index f0a7d66..ce421c2 100644 --- a/lib/src/crypto.dart +++ b/lib/src/crypto.dart @@ -7,7 +7,10 @@ import 'package:omemo_dart/src/keys.dart'; /// is the identity key. This is needed since the identity key pair/public key is /// an Ed25519 key, but we need them as X25519 keys for DH. Future> omemoDH( - OmemoKeyPair kp, OmemoPublicKey pk, int identityKey,) async { + OmemoKeyPair kp, + OmemoPublicKey pk, + int identityKey, +) async { var ckp = kp; var cpk = pk; @@ -62,13 +65,19 @@ Future deriveEncryptionKeys(List input, String info) async { final bytes = await result.extractBytes(); return HkdfKeyResult( - bytes.sublist(0, 32), bytes.sublist(32, 64), bytes.sublist(64, 80),); + bytes.sublist(0, 32), + bytes.sublist(32, 64), + bytes.sublist(64, 80), + ); } /// A small helper function to make AES-256-CBC easier. Encrypt [plaintext] using [key] as /// the encryption key and [iv] as the IV. Returns the ciphertext. Future> aes256CbcEncrypt( - List plaintext, List key, List iv,) async { + List plaintext, + List key, + List iv, +) async { final algorithm = AesCbc.with256bits( macAlgorithm: MacAlgorithm.empty, ); @@ -84,7 +93,10 @@ Future> aes256CbcEncrypt( /// A small helper function to make AES-256-CBC easier. Decrypt [ciphertext] using [key] as /// the encryption key and [iv] as the IV. Returns the ciphertext. Future> aes256CbcDecrypt( - List ciphertext, List key, List iv,) async { + List ciphertext, + List key, + List iv, +) async { final algorithm = AesCbc.with256bits( macAlgorithm: MacAlgorithm.empty, ); diff --git a/lib/src/double_ratchet/crypto.dart b/lib/src/double_ratchet/crypto.dart index 9ec990c..42033ed 100644 --- a/lib/src/double_ratchet/crypto.dart +++ b/lib/src/double_ratchet/crypto.dart @@ -10,8 +10,12 @@ const encryptHkdfInfoString = 'OMEMO Message Key Material'; /// Signals ENCRYPT function as specified by OMEMO 0.8.3. /// Encrypt [plaintext] using the message key [mk], given associated_data [associatedData] /// and the AD output from the X3DH [sessionAd]. -Future> encrypt(List mk, List plaintext, - List associatedData, List sessionAd,) async { +Future> encrypt( + List mk, + List plaintext, + List associatedData, + List sessionAd, +) async { // Generate encryption, authentication key and IV final keys = await deriveEncryptionKeys(mk, encryptHkdfInfoString); final ciphertext = @@ -32,8 +36,12 @@ Future> encrypt(List mk, List plaintext, /// Signals DECRYPT function as specified by OMEMO 0.8.3. /// Decrypt [ciphertext] with the message key [mk], given the associated_data [associatedData] /// and the AD output from the X3DH. -Future> decrypt(List mk, List ciphertext, - List associatedData, List sessionAd,) async { +Future> decrypt( + List mk, + List ciphertext, + List associatedData, + List sessionAd, +) async { // Generate encryption, authentication key and IV final keys = await deriveEncryptionKeys(mk, encryptHkdfInfoString); diff --git a/lib/src/double_ratchet/double_ratchet.dart b/lib/src/double_ratchet/double_ratchet.dart index 902080a..b997d4a 100644 --- a/lib/src/double_ratchet/double_ratchet.dart +++ b/lib/src/double_ratchet/double_ratchet.dart @@ -179,8 +179,13 @@ class OmemoDoubleRatchet { /// Create an OMEMO session using the Signed Pre Key [spk], the shared secret [sk] that /// was obtained using a X3DH and the associated data [ad] that was also obtained through /// a X3DH. [ik] refers to Bob's (the receiver's) IK public key. - static Future initiateNewSession(OmemoPublicKey spk, - OmemoPublicKey ik, List sk, List ad, int timestamp,) async { + static Future initiateNewSession( + OmemoPublicKey spk, + OmemoPublicKey ik, + List sk, + List ad, + int timestamp, + ) async { final dhs = await OmemoKeyPair.generateNewPair(KeyPairType.x25519); final dhr = spk; final rk = await kdfRk(sk, await omemoDH(dhs, dhr, 0)); @@ -208,8 +213,13 @@ class OmemoDoubleRatchet { /// Pre Key keypair [spk], the shared secret [sk] that was obtained through a X3DH and /// the associated data [ad] that was also obtained through a X3DH. [ik] refers to /// Alice's (the initiator's) IK public key. - static Future acceptNewSession(OmemoKeyPair spk, - OmemoPublicKey ik, List sk, List ad, int kexTimestamp,) async { + static Future acceptNewSession( + OmemoKeyPair spk, + OmemoPublicKey ik, + List sk, + List ad, + int kexTimestamp, + ) async { return OmemoDoubleRatchet( spk, null, @@ -264,7 +274,9 @@ class OmemoDoubleRatchet { } Future?> _trySkippedMessageKeys( - OmemoMessage header, List ciphertext,) async { + OmemoMessage header, + List ciphertext, + ) async { final key = SkippedKey( OmemoPublicKey.fromBytes(header.dhPub!, KeyPairType.x25519), header.n!, @@ -273,8 +285,12 @@ class OmemoDoubleRatchet { final mk = mkSkipped[key]!; mkSkipped.remove(key); - return decrypt(mk, ciphertext, - concat([sessionAd, header.writeToBuffer()]), sessionAd,); + return decrypt( + mk, + ciphertext, + concat([sessionAd, header.writeToBuffer()]), + sessionAd, + ); } return null; @@ -326,8 +342,12 @@ class OmemoDoubleRatchet { return RatchetStep( header, - await encrypt(mk, plaintext, concat([sessionAd, header.writeToBuffer()]), - sessionAd,), + await encrypt( + mk, + plaintext, + concat([sessionAd, header.writeToBuffer()]), + sessionAd, + ), ); } @@ -336,7 +356,9 @@ class OmemoDoubleRatchet { /// /// Throws an SkippingTooManyMessagesException if too many messages were to be skipped. Future> ratchetDecrypt( - OmemoMessage header, List ciphertext,) async { + OmemoMessage header, + List ciphertext, + ) async { // Check if we skipped too many messages final plaintext = await _trySkippedMessageKeys(header, ciphertext); if (plaintext != null) { @@ -359,7 +381,11 @@ class OmemoDoubleRatchet { nr++; return decrypt( - mk, ciphertext, concat([sessionAd, header.writeToBuffer()]), sessionAd,); + mk, + ciphertext, + concat([sessionAd, header.writeToBuffer()]), + sessionAd, + ); } OmemoDoubleRatchet clone() { diff --git a/lib/src/helpers.dart b/lib/src/helpers.dart index 253014c..4f23632 100644 --- a/lib/src/helpers.dart +++ b/lib/src/helpers.dart @@ -44,7 +44,10 @@ int generateRandom32BitNumber() { } OmemoPublicKey? decodeKeyIfNotNull( - Map map, String key, KeyPairType type,) { + Map map, + String key, + KeyPairType type, +) { if (map[key] == null) return null; return OmemoPublicKey.fromBytes( diff --git a/lib/src/keys.dart b/lib/src/keys.dart index 398c103..1a064e8 100644 --- a/lib/src/keys.dart +++ b/lib/src/keys.dart @@ -31,8 +31,10 @@ class OmemoPublicKey { Future asBase64() async => base64Encode(_pubkey.bytes); Future toCurve25519() async { - assert(type == KeyPairType.ed25519, - 'Cannot convert non-Ed25519 public key to X25519',); + assert( + type == KeyPairType.ed25519, + 'Cannot convert non-Ed25519 public key to X25519', + ); final pkc = Uint8List(publicKeyLength); TweetNaClExt.crypto_sign_ed25519_pk_to_x25519_pk( @@ -41,7 +43,8 @@ class OmemoPublicKey { ); return OmemoPublicKey( - SimplePublicKey(List.from(pkc), type: KeyPairType.x25519),); + SimplePublicKey(List.from(pkc), type: KeyPairType.x25519), + ); } SimplePublicKey asPublicKey() => _pubkey; @@ -64,8 +67,10 @@ class OmemoPrivateKey { Future> getBytes() async => _privkey; Future toCurve25519() async { - assert(type == KeyPairType.ed25519, - 'Cannot convert non-Ed25519 private key to X25519',); + assert( + type == KeyPairType.ed25519, + 'Cannot convert non-Ed25519 private key to X25519', + ); final skc = Uint8List(privateKeyLength); TweetNaClExt.crypto_sign_ed25519_sk_to_x25519_sk( @@ -93,7 +98,10 @@ class OmemoKeyPair { /// Create an OmemoKeyPair just from a [type] and the bytes of the private and public /// key. factory OmemoKeyPair.fromBytes( - List publicKey, List privateKey, KeyPairType type,) { + List publicKey, + List privateKey, + KeyPairType type, + ) { return OmemoKeyPair( OmemoPublicKey.fromBytes( publicKey, @@ -110,8 +118,10 @@ class OmemoKeyPair { /// Generate a completely new random OmemoKeyPair of type [type]. [type] must be either /// KeyPairType.ed25519 or KeyPairType.x25519. static Future generateNewPair(KeyPairType type) async { - assert(type == KeyPairType.ed25519 || type == KeyPairType.x25519, - 'Keypair must be either Ed25519 or X25519',); + assert( + type == KeyPairType.ed25519 || type == KeyPairType.x25519, + 'Keypair must be either Ed25519 or X25519', + ); SimpleKeyPair kp; if (type == KeyPairType.ed25519) { @@ -140,8 +150,10 @@ class OmemoKeyPair { /// Return the bytes that comprise the public key. Future toCurve25519() async { - assert(type == KeyPairType.ed25519, - 'Cannot convert non-Ed25519 keypair to X25519',); + assert( + type == KeyPairType.ed25519, + 'Cannot convert non-Ed25519 keypair to X25519', + ); return OmemoKeyPair( await pk.toCurve25519(), diff --git a/lib/src/omemo/device.dart b/lib/src/omemo/device.dart index 1b34226..354c795 100644 --- a/lib/src/omemo/device.dart +++ b/lib/src/omemo/device.dart @@ -93,8 +93,10 @@ class OmemoDevice { } /// Generate a completely new device, i.e. cryptographic identity. - static Future generateNewDevice(String jid, - {int opkAmount = 100,}) async { + static Future generateNewDevice( + String jid, { + int opkAmount = 100, + }) async { final id = generateRandom32BitNumber(); final ik = await OmemoKeyPair.generateNewPair(KeyPairType.ed25519); final spk = await OmemoKeyPair.generateNewPair(KeyPairType.x25519); diff --git a/lib/src/omemo/encryption_result.dart b/lib/src/omemo/encryption_result.dart index 55787cd..cb2494a 100644 --- a/lib/src/omemo/encryption_result.dart +++ b/lib/src/omemo/encryption_result.dart @@ -5,8 +5,12 @@ import 'package:omemo_dart/src/omemo/ratchet_map_key.dart'; @immutable class EncryptionResult { - const EncryptionResult(this.ciphertext, this.encryptedKeys, - this.deviceEncryptionErrors, this.jidEncryptionErrors,); + const EncryptionResult( + this.ciphertext, + this.encryptedKeys, + this.deviceEncryptionErrors, + this.jidEncryptionErrors, + ); /// The actual message that was encrypted. final List? ciphertext; diff --git a/lib/src/omemo/events.dart b/lib/src/omemo/events.dart index 2a3a80e..5999125 100644 --- a/lib/src/omemo/events.dart +++ b/lib/src/omemo/events.dart @@ -6,7 +6,12 @@ abstract class OmemoEvent {} /// Triggered when a ratchet has been modified class RatchetModifiedEvent extends OmemoEvent { RatchetModifiedEvent( - this.jid, this.deviceId, this.ratchet, this.added, this.replaced,); + this.jid, + this.deviceId, + this.ratchet, + this.added, + this.replaced, + ); final String jid; final int deviceId; final OmemoDoubleRatchet ratchet; diff --git a/lib/src/omemo/omemomanager.dart b/lib/src/omemo/omemomanager.dart index 1f3fa2b..20cc3e2 100644 --- a/lib/src/omemo/omemomanager.dart +++ b/lib/src/omemo/omemomanager.dart @@ -33,8 +33,10 @@ class _InternalDecryptionResult { this.ratchetCreated, this.ratchetReplaced, this.payload, - ) : assert(!ratchetCreated || !ratchetReplaced, - 'Ratchet must be either replaced or created',); + ) : assert( + !ratchetCreated || !ratchetReplaced, + 'Ratchet must be either replaced or created', + ); final bool ratchetCreated; final bool ratchetReplaced; final String? payload; @@ -132,7 +134,9 @@ class OmemoManager { } Future _decryptAndVerifyHmac( - List? ciphertext, List keyAndHmac,) async { + List? ciphertext, + List keyAndHmac, + ) async { // Empty OMEMO messages should just have the key decrypted and/or session set up. if (ciphertext == null) { return null; @@ -149,7 +153,10 @@ class OmemoManager { return utf8.decode( await aes256CbcDecrypt( - ciphertext, derivedKeys.encryptionKey, derivedKeys.iv,), + ciphertext, + derivedKeys.encryptionKey, + derivedKeys.iv, + ), ); } @@ -185,7 +192,10 @@ class OmemoManager { /// from the key exchange [kex]. In case [kex] contains an unknown Signed Prekey /// identifier an UnknownSignedPrekeyException will be thrown. Future _addSessionFromKeyExchange( - String jid, int deviceId, OmemoKeyExchange kex,) async { + String jid, + int deviceId, + OmemoKeyExchange kex, + ) async { // Pick the correct SPK final device = await getDevice(); OmemoKeyPair spk; @@ -225,7 +235,10 @@ class OmemoManager { /// [deviceId] from the bundle [bundle]. @visibleForTesting Future addSessionFromBundle( - String jid, int deviceId, OmemoBundle bundle,) async { + String jid, + int deviceId, + OmemoBundle bundle, + ) async { final device = await getDevice(); final kexResult = await x3dhFromBundle( bundle, @@ -255,7 +268,8 @@ class OmemoManager { /// NOTE: Must be called from within the ratchet critical section void _restoreRatchet(RatchetMapKey mapKey, OmemoDoubleRatchet oldRatchet) { _log.finest( - 'Restoring ratchet ${mapKey.jid}:${mapKey.deviceId} to ${oldRatchet.nr}',); + 'Restoring ratchet ${mapKey.jid}:${mapKey.deviceId} to ${oldRatchet.nr}', + ); _ratchetMap[mapKey] = oldRatchet; // Commit the ratchet @@ -283,11 +297,12 @@ class OmemoManager { /// will return null as there is no message to be decrypted. This, however, is used /// to set up sessions or advance the ratchets. Future<_InternalDecryptionResult> _decryptMessage( - List? ciphertext, - String senderJid, - int senderDeviceId, - List keys, - int timestamp,) async { + List? ciphertext, + String senderJid, + int senderDeviceId, + List keys, + int timestamp, + ) async { // Try to find a session we can decrypt with. var device = await getDevice(); final rawKey = keys.firstWhereOrNull((key) => key.rid == device.id); @@ -312,7 +327,8 @@ class OmemoManager { // Guard against old key exchanges if (oldRatchet != null) { _log.finest( - 'KEX for existent ratchet ${ratchetKey.toJsonKey()}. ${oldRatchet.kexTimestamp} > $timestamp: ${oldRatchet.kexTimestamp > timestamp}',); + 'KEX for existent ratchet ${ratchetKey.toJsonKey()}. ${oldRatchet.kexTimestamp} > $timestamp: ${oldRatchet.kexTimestamp > timestamp}', + ); if (oldRatchet.kexTimestamp > timestamp) { throw InvalidKeyExchangeException(); } @@ -461,7 +477,9 @@ class OmemoManager { /// the result will be null as well. /// NOTE: Must be called within the ratchet critical section Future _encryptToJids( - List jids, String? plaintext,) async { + List jids, + String? plaintext, + ) async { final encryptedKeys = List.empty(growable: true); var ciphertext = const []; @@ -564,7 +582,8 @@ class OmemoManager { } else { // The ratchet is not acked but we don't have the old key exchange _log.warning( - 'Ratchet for $jid:$deviceId is not acked but the kex attribute is null',); + 'Ratchet for $jid:$deviceId is not acked but the kex attribute is null', + ); encryptedKeys.add( EncryptedKey( jid, @@ -632,7 +651,9 @@ class OmemoManager { // Check if the ratchet is acked final ratchet = getRatchet(ratchetKey); assert( - ratchet != null, 'We decrypted the message, so the ratchet must exist',); + ratchet != null, + 'We decrypted the message, so the ratchet must exist', + ); if (ratchet!.acknowledged) { // Ratchet is acknowledged @@ -704,8 +725,11 @@ class OmemoManager { } /// Mark the ratchet for device [deviceId] from [jid] as acked. - Future ratchetAcknowledged(String jid, int deviceId, - {bool enterCriticalSection = true,}) async { + Future ratchetAcknowledged( + String jid, + int deviceId, { + bool enterCriticalSection = true, + }) async { if (enterCriticalSection) await _enterRatchetCriticalSection(jid); final key = RatchetMapKey(jid, deviceId); @@ -717,7 +741,8 @@ class OmemoManager { .add(RatchetModifiedEvent(jid, deviceId, ratchet, false, false)); } else { _log.severe( - 'Attempted to acknowledge ratchet ${key.toJsonKey()}, even though it does not exist',); + 'Attempted to acknowledge ratchet ${key.toJsonKey()}, even though it does not exist', + ); } if (enterCriticalSection) await _leaveRatchetCriticalSection(jid); @@ -788,8 +813,10 @@ class OmemoManager { _eventStreamController.add(DeviceListModifiedEvent(_deviceList)); } - void initialize(Map ratchetMap, - Map> deviceList,) { + void initialize( + Map ratchetMap, + Map> deviceList, + ) { _deviceList = deviceList; _ratchetMap = ratchetMap; } diff --git a/lib/src/trust/btbv.dart b/lib/src/trust/btbv.dart index 9787df7..7ea4304 100644 --- a/lib/src/trust/btbv.dart +++ b/lib/src/trust/btbv.dart @@ -147,7 +147,10 @@ abstract class BlindTrustBeforeVerificationTrustManager extends TrustManager { /// Sets the trust of [jid]'s device with identifier [deviceId] to [state]. Future setDeviceTrust( - String jid, int deviceId, BTBVTrustState state,) async { + String jid, + int deviceId, + BTBVTrustState state, + ) async { await _lock.synchronized(() async { trustCache[RatchetMapKey(jid, deviceId)] = state; @@ -205,7 +208,8 @@ abstract class BlindTrustBeforeVerificationTrustManager extends TrustManager { /// From a serialized version of a BTBV trust manager, extract the trust cache. /// NOTE: This is needed as Dart cannot just cast a List to List and so on. static Map trustCacheFromJson( - Map json,) { + Map json, + ) { return (json['trust']! as Map) .map( (key, value) => MapEntry( @@ -218,7 +222,8 @@ abstract class BlindTrustBeforeVerificationTrustManager extends TrustManager { /// From a serialized version of a BTBV trust manager, extract the enable cache. /// NOTE: This is needed as Dart cannot just cast a List to List and so on. static Map enableCacheFromJson( - Map json,) { + Map json, + ) { return (json['enable']! as Map).map( (key, value) => MapEntry( RatchetMapKey.fromJsonKey(key), diff --git a/lib/src/x3dh/x3dh.dart b/lib/src/x3dh/x3dh.dart index c66c1ee..670a805 100644 --- a/lib/src/x3dh/x3dh.dart +++ b/lib/src/x3dh/x3dh.dart @@ -37,7 +37,9 @@ class X3DHBobResult { /// a Ed25519 keypair. Future> sig(OmemoKeyPair keyPair, List message) async { assert( - keyPair.type == KeyPairType.ed25519, 'Signature keypair must be Ed25519',); + keyPair.type == KeyPairType.ed25519, + 'Signature keypair must be Ed25519', + ); final signature = await Ed25519().sign( message, keyPair: await keyPair.asKeyPair(), @@ -69,7 +71,9 @@ Future> kdf(List km) async { /// Alice builds a session with Bob using his bundle [bundle] and Alice's identity key /// pair [ik]. Future x3dhFromBundle( - OmemoBundle bundle, OmemoKeyPair ik,) async { + OmemoBundle bundle, + OmemoKeyPair ik, +) async { // Check the signature first final signatureValue = await Ed25519().verify( await bundle.spk.getBytes(), @@ -107,8 +111,12 @@ Future x3dhFromBundle( /// Bob builds the X3DH shared secret from the inital message [msg], the SPK [spk], the /// OPK [opk] that was selected by Alice and our IK [ik]. Returns the shared secret. -Future x3dhFromInitialMessage(X3DHMessage msg, OmemoKeyPair spk, - OmemoKeyPair opk, OmemoKeyPair ik,) async { +Future x3dhFromInitialMessage( + X3DHMessage msg, + OmemoKeyPair spk, + OmemoKeyPair opk, + OmemoKeyPair ik, +) async { final dh1 = await omemoDH(spk, msg.ik, 2); final dh2 = await omemoDH(ik, msg.ek, 1); final dh3 = await omemoDH(spk, msg.ek, 0); diff --git a/test/omemomanager_test.dart b/test/omemomanager_test.dart index 10be4fe..9ce7cc4 100644 --- a/test/omemomanager_test.dart +++ b/test/omemomanager_test.dart @@ -813,9 +813,10 @@ void main() { expect(aliceResult.isSuccess(1), false); expect( - aliceResult.jidEncryptionErrors[bobJid] - is NoKeyMaterialAvailableException, - true,); + aliceResult.jidEncryptionErrors[bobJid] + is NoKeyMaterialAvailableException, + true, + ); }); test('Test sending a message two two JIDs with failed lookups', () async { @@ -866,9 +867,10 @@ void main() { expect(aliceResult.isSuccess(2), true); expect( - aliceResult.jidEncryptionErrors[cocoJid] - is NoKeyMaterialAvailableException, - true,); + aliceResult.jidEncryptionErrors[cocoJid] + is NoKeyMaterialAvailableException, + true, + ); // Bob decrypts it final bobResult = await bobManager.onIncomingStanza( diff --git a/test/x3dh_test.dart b/test/x3dh_test.dart index 611ddde..68b249d 100644 --- a/test/x3dh_test.dart +++ b/test/x3dh_test.dart @@ -73,8 +73,11 @@ void main() { await x3dhFromBundle(bundleBob, ikAlice); } catch (e) { exception = true; - expect(e is InvalidSignatureException, true, - reason: 'Expected InvalidSignatureException, but got $e',); + expect( + e is InvalidSignatureException, + true, + reason: 'Expected InvalidSignatureException, but got $e', + ); } expect(exception, true, reason: 'Expected test failure');