Skip to content

Commit

Permalink
feat: add keyRingSize/discardFrameWhenCryptorNotReady to KeyProviderO…
Browse files Browse the repository at this point in the history
…ptions.
  • Loading branch information
cloudwebrtc committed Apr 8, 2024
1 parent e15d496 commit a11a6de
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/src/e2ee.worker/e2ee.cryptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ class FrameCryptor {
if (!enabled ||
// skip for encryption for empty dtx frames
buffer.isEmpty) {
if (keyOptions.discardFrameWhenCryptorNotReady) {
return;
}
controller.enqueue(frame);
return;
}
Expand Down Expand Up @@ -405,6 +408,9 @@ class FrameCryptor {
// skip for encryption for empty dtx frames
buffer.isEmpty) {
sifGuard.recordUserFrame();
if (keyOptions.discardFrameWhenCryptorNotReady) {
return;
}
controller.enqueue(frame);
return;
}
Expand Down
17 changes: 13 additions & 4 deletions lib/src/e2ee.worker/e2ee.keyhandler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@ import 'crypto.dart' as crypto;
import 'e2ee.logger.dart';
import 'e2ee.utils.dart';

const KEYRING_SIZE = 16;

class KeyOptions {
KeyOptions({
required this.sharedKey,
required this.ratchetSalt,
required this.ratchetWindowSize,
this.uncryptedMagicBytes,
this.failureTolerance = -1,
this.keyRingSze = KEYRING_SIZE,
this.discardFrameWhenCryptorNotReady = false,
});
bool sharedKey;
Uint8List ratchetSalt;
int ratchetWindowSize = 0;
int failureTolerance;
Uint8List? uncryptedMagicBytes;
int keyRingSze;
bool discardFrameWhenCryptorNotReady;

@override
String toString() {
Expand Down Expand Up @@ -77,8 +83,6 @@ class KeyProvider {
}
}

const KEYRING_SIZE = 16;

class KeySet {
KeySet(this.material, this.encryptionKey);
web.CryptoKey material;
Expand All @@ -90,10 +94,15 @@ class ParticipantKeyHandler {
required this.worker,
required this.keyOptions,
required this.participantIdentity,
});
}) {
if (keyOptions.keyRingSze <= 0 || keyOptions.keyRingSze > 255) {
throw Exception('Invalid key ring size');
}
cryptoKeyRing = List.filled(keyOptions.keyRingSze, null);
}
int currentKeyIndex = 0;

List<KeySet?> cryptoKeyRing = List.filled(KEYRING_SIZE, null);
late List<KeySet?> cryptoKeyRing;

bool _hasValidKey = false;

Expand Down
5 changes: 4 additions & 1 deletion lib/src/e2ee.worker/e2ee.worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ void main() async {
uncryptedMagicBytes: options['uncryptedMagicBytes'] != null
? Uint8List.fromList(
base64Decode(options['uncryptedMagicBytes'] as String))
: null);
: null,
keyRingSze: options['keyRingSize'] ?? KEYRING_SIZE,
discardFrameWhenCryptorNotReady:
options['discardFrameWhenCryptorNotReady'] ?? false);
logger.config(
'Init with keyProviderOptions:\n ${keyProviderOptions.toString()}');

Expand Down
3 changes: 3 additions & 0 deletions lib/src/frame_cryptor_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ class KeyProviderImpl implements KeyProvider {
'ratchetWindowSize': options.ratchetWindowSize,
if (options.uncryptedMagicBytes != null)
'uncryptedMagicBytes': base64Encode(options.uncryptedMagicBytes!),
'keyRingSize': options.keyRingSize,
'discardFrameWhenCryptorNotReady':
options.discardFrameWhenCryptorNotReady,
},
})
]);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies:
platform_detect: ^2.0.7
synchronized: ^3.0.0+3
web: ^0.5.1
webrtc_interface: ^1.1.2
webrtc_interface: ^1.2.0

dev_dependencies:
build_runner: ^2.3.3
Expand Down
13 changes: 11 additions & 2 deletions web/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:typed_data';

import 'package:dart_webrtc/dart_webrtc.dart';
Expand Down Expand Up @@ -49,7 +50,8 @@ void loopBackTest() async {
sharedKey: false,
ratchetWindowSize: 16,
failureTolerance: -1,
ratchetSalt: Uint8List.fromList('testSalt'.codeUnits));
ratchetSalt: Uint8List.fromList('testSalt'.codeUnits),
discardFrameWhenCryptorNotReady: true);
var keyProvider =
await frameCryptorFactory.createDefaultKeyProvider(keyProviderOptions);

Expand All @@ -73,7 +75,14 @@ void loopBackTest() async {
receiver: event.receiver!,
algorithm: Algorithm.kAesGcm,
keyProvider: keyProvider);
await fc.setEnabled(true);
if (keyProviderOptions.discardFrameWhenCryptorNotReady) {
Timer(Duration(seconds: 2), () {
fc.setEnabled(true);
});
} else {
await fc.setEnabled(true);
}

await fc.setKeyIndex(0);
await fc.updateCodec('vp8');
pc2FrameCryptors.add(fc);
Expand Down

0 comments on commit a11a6de

Please sign in to comment.