11import 'dart:async' ;
2- import 'dart:html' ;
32import 'dart:js_util' as jsutil;
43import 'dart:typed_data' ;
54
5+ import 'package:web/web.dart' as web;
6+
67import 'crypto.dart' as crypto;
78import 'e2ee.logger.dart' ;
89import 'e2ee.utils.dart' ;
910
11+ const KEYRING_SIZE = 16 ;
12+
1013class KeyOptions {
1114 KeyOptions ({
1215 required this .sharedKey,
1316 required this .ratchetSalt,
1417 required this .ratchetWindowSize,
1518 this .uncryptedMagicBytes,
1619 this .failureTolerance = - 1 ,
20+ this .keyRingSze = KEYRING_SIZE ,
21+ this .discardFrameWhenCryptorNotReady = false ,
1722 });
1823 bool sharedKey;
1924 Uint8List ratchetSalt;
2025 int ratchetWindowSize = 0 ;
2126 int failureTolerance;
2227 Uint8List ? uncryptedMagicBytes;
28+ int keyRingSze;
29+ bool discardFrameWhenCryptorNotReady;
2330
2431 @override
2532 String toString () {
@@ -29,7 +36,7 @@ class KeyOptions {
2936
3037class KeyProvider {
3138 KeyProvider (this .worker, this .id, this .keyProviderOptions);
32- final DedicatedWorkerGlobalScope worker;
39+ final web. DedicatedWorkerGlobalScope worker;
3340 final String id;
3441 final KeyOptions keyProviderOptions;
3542 var participantKeys = < String , ParticipantKeyHandler > {};
@@ -76,31 +83,34 @@ class KeyProvider {
7683 }
7784}
7885
79- const KEYRING_SIZE = 16 ;
80-
8186class KeySet {
8287 KeySet (this .material, this .encryptionKey);
83- CryptoKey material;
84- CryptoKey encryptionKey;
88+ web. CryptoKey material;
89+ web. CryptoKey encryptionKey;
8590}
8691
8792class ParticipantKeyHandler {
8893 ParticipantKeyHandler ({
8994 required this .worker,
9095 required this .keyOptions,
9196 required this .participantIdentity,
92- });
97+ }) {
98+ if (keyOptions.keyRingSze <= 0 || keyOptions.keyRingSze > 255 ) {
99+ throw Exception ('Invalid key ring size' );
100+ }
101+ cryptoKeyRing = List .filled (keyOptions.keyRingSze, null );
102+ }
93103 int currentKeyIndex = 0 ;
94104
95- List <KeySet ?> cryptoKeyRing = List . filled ( KEYRING_SIZE , null ) ;
105+ late List <KeySet ?> cryptoKeyRing;
96106
97107 bool _hasValidKey = false ;
98108
99109 bool get hasValidKey => _hasValidKey;
100110
101111 final KeyOptions keyOptions;
102112
103- final DedicatedWorkerGlobalScope worker;
113+ final web. DedicatedWorkerGlobalScope worker;
104114
105115 final String participantIdentity;
106116
@@ -157,8 +167,8 @@ class ParticipantKeyHandler {
157167 return newKey;
158168 }
159169
160- Future <CryptoKey > ratchetMaterial (
161- CryptoKey currentMaterial, ByteBuffer newKeyBuffer) async {
170+ Future <web. CryptoKey > ratchetMaterial (
171+ web. CryptoKey currentMaterial, ByteBuffer newKeyBuffer) async {
162172 var newMaterial = await jsutil.promiseToFuture (crypto.importKey (
163173 'raw' ,
164174 newKeyBuffer,
@@ -194,14 +204,14 @@ class ParticipantKeyHandler {
194204
195205 /// Derives a set of keys from the master key.
196206 /// See https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.1
197- Future <KeySet > deriveKeys (CryptoKey material, Uint8List salt) async {
207+ Future <KeySet > deriveKeys (web. CryptoKey material, Uint8List salt) async {
198208 var algorithmOptions =
199209 getAlgoOptions ((material.algorithm as crypto.Algorithm ).name, salt);
200210
201211 // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#HKDF
202212 // https://developer.mozilla.org/en-US/docs/Web/API/HkdfParams
203213 var encryptionKey =
204- await jsutil.promiseToFuture <CryptoKey >(crypto.deriveKey (
214+ await jsutil.promiseToFuture< web. CryptoKey > (crypto.deriveKey (
205215 jsutil.jsify (algorithmOptions),
206216 material,
207217 jsutil.jsify ({'name' : 'AES-GCM' , 'length' : 128 }),
@@ -215,7 +225,7 @@ class ParticipantKeyHandler {
215225 /// Ratchets a key. See
216226 /// https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.5.1
217227
218- Future <Uint8List > ratchet (CryptoKey material, Uint8List salt) async {
228+ Future <Uint8List > ratchet (web. CryptoKey material, Uint8List salt) async {
219229 var algorithmOptions = getAlgoOptions ('PBKDF2' , salt);
220230
221231 // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits
0 commit comments