Skip to content

Commit bee7918

Browse files
Refactor AesCtrSecretKey Class (#142)
* refactor: update aescbc-secret-key * refactor: add webCryptImpl for consistent webcrypto api * Update lib/src/impl_interface/impl_interface.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * Update lib/src/impl_ffi/impl_ffi.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * Update lib/src/impl_ffi/impl_ffi.aescbc.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * Update lib/src/impl_ffi/impl_ffi.aescbc.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * Update lib/src/impl_ffi/impl_ffi.aescbc.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * Update lib/src/impl_interface/impl_interface.aescbc.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * Update lib/src/impl_js/impl_js.aescbc.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * Update lib/src/impl_js/impl_js.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * Update lib/src/impl_stub/impl_stub.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * Update lib/src/webcrypto/webcrypto.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * test: add license headers * refactor: update aesctr-secret-key --------- Co-authored-by: Jonas Finnemann Jensen <[email protected]>
1 parent a5857ed commit bee7918

16 files changed

+387
-78
lines changed

lib/src/impl_ffi/impl_ffi.aescbc.dart

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616

1717
part of 'impl_ffi.dart';
1818

19-
Future<AesCbcSecretKey> aesCbc_importRawKey(List<int> keyData) async =>
20-
_AesCbcSecretKey(_aesImportRawKey(keyData));
19+
Future<AesCbcSecretKeyImpl> aesCbc_importRawKey(List<int> keyData) async =>
20+
_AesCbcSecretKeyImpl(_aesImportRawKey(keyData));
2121

22-
Future<AesCbcSecretKey> aesCbc_importJsonWebKey(
22+
Future<AesCbcSecretKeyImpl> aesCbc_importJsonWebKey(
2323
Map<String, dynamic> jwk,
2424
) async =>
25-
_AesCbcSecretKey(_aesImportJwkKey(
25+
_AesCbcSecretKeyImpl(_aesImportJwkKey(
2626
jwk,
2727
expectedJwkAlgSuffix: 'CBC',
2828
));
2929

30-
Future<AesCbcSecretKey> aesCbc_generateKey(int length) async =>
31-
_AesCbcSecretKey(_aesGenerateKey(length));
30+
Future<AesCbcSecretKeyImpl> aesCbc_generateKey(int length) async =>
31+
_AesCbcSecretKeyImpl(_aesGenerateKey(length));
3232

3333
Stream<Uint8List> _aesCbcEncryptOrDecrypt(
3434
Uint8List key,
@@ -93,9 +93,30 @@ Stream<Uint8List> _aesCbcEncryptOrDecrypt(
9393
});
9494
}
9595

96-
class _AesCbcSecretKey implements AesCbcSecretKey {
96+
final class _StaticAesCbcSecretKeyImpl implements StaticAesCbcSecretKeyImpl {
97+
const _StaticAesCbcSecretKeyImpl();
98+
99+
@override
100+
Future<AesCbcSecretKeyImpl> importRawKey(List<int> keyData) async {
101+
// TODO: Move implementation into this method in a follow up PR
102+
// TODO: Move implementation into this method in a follow up PR
103+
return await aesCbc_importRawKey(keyData);
104+
}
105+
106+
@override
107+
Future<AesCbcSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk) async {
108+
return await aesCbc_importJsonWebKey(jwk);
109+
}
110+
111+
@override
112+
Future<AesCbcSecretKeyImpl> generateKey(int length) async {
113+
return await aesCbc_generateKey(length);
114+
}
115+
}
116+
117+
final class _AesCbcSecretKeyImpl extends AesCbcSecretKeyImpl {
97118
final Uint8List _key;
98-
_AesCbcSecretKey(this._key);
119+
_AesCbcSecretKeyImpl(this._key);
99120

100121
@override
101122
String toString() {

lib/src/impl_ffi/impl_ffi.aesctr.dart

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616

1717
part of 'impl_ffi.dart';
1818

19-
Future<AesCtrSecretKey> aesCtr_importRawKey(List<int> keyData) async =>
20-
_AesCtrSecretKey(_aesImportRawKey(keyData));
19+
Future<AesCtrSecretKeyImpl> aesCtr_importRawKey(List<int> keyData) async =>
20+
_AesCtrSecretKeyImpl(_aesImportRawKey(keyData));
2121

22-
Future<AesCtrSecretKey> aesCtr_importJsonWebKey(
22+
Future<AesCtrSecretKeyImpl> aesCtr_importJsonWebKey(
2323
Map<String, dynamic> jwk,
2424
) async =>
25-
_AesCtrSecretKey(_aesImportJwkKey(
25+
_AesCtrSecretKeyImpl(_aesImportJwkKey(
2626
jwk,
2727
expectedJwkAlgSuffix: 'CTR',
2828
));
2929

30-
Future<AesCtrSecretKey> aesCtr_generateKey(int length) async =>
31-
_AesCtrSecretKey(_aesGenerateKey(length));
30+
Future<AesCtrSecretKeyImpl> aesCtr_generateKey(int length) async =>
31+
_AesCtrSecretKeyImpl(_aesGenerateKey(length));
3232

3333
BigInt _parseBigEndian(List<int> data, [int? bitLength]) {
3434
bitLength ??= data.length * 8;
@@ -200,9 +200,30 @@ Stream<Uint8List> _aesCtrEncryptOrDecrypt(
200200
});
201201
}
202202

203-
class _AesCtrSecretKey implements AesCtrSecretKey {
203+
final class _StaticAesCtrSecretKeyImpl implements StaticAesCtrSecretKeyImpl {
204+
const _StaticAesCtrSecretKeyImpl();
205+
206+
@override
207+
Future<AesCtrSecretKeyImpl> importRawKey(List<int> keyData) async {
208+
return await aesCtr_importRawKey(keyData);
209+
}
210+
211+
@override
212+
Future<AesCtrSecretKeyImpl> importJsonWebKey(
213+
Map<String, dynamic> jwk,
214+
) async {
215+
return await aesCtr_importJsonWebKey(jwk);
216+
}
217+
218+
@override
219+
Future<AesCtrSecretKeyImpl> generateKey(int length) async {
220+
return await aesCtr_generateKey(length);
221+
}
222+
}
223+
224+
final class _AesCtrSecretKeyImpl extends AesCtrSecretKeyImpl {
204225
final Uint8List _key;
205-
_AesCtrSecretKey(this._key);
226+
_AesCtrSecretKeyImpl(this._key);
206227

207228
@override
208229
String toString() {

lib/src/impl_ffi/impl_ffi.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import 'package:webcrypto/src/third_party/boringssl/generated_bindings.dart';
2727

2828
import '../jsonwebkey.dart' show JsonWebKey;
2929
import '../webcrypto/webcrypto.dart';
30+
import '../impl_interface/impl_interface.dart';
3031
import '../boringssl/lookup/lookup.dart' show ssl, ERR_GET_LIB, ERR_GET_REASON;
3132

3233
part 'impl_ffi.aescbc.dart';
@@ -67,3 +68,16 @@ class _KeyPair<S, T> implements KeyPair<S, T> {
6768

6869
_KeyPair({required this.privateKey, required this.publicKey});
6970
}
71+
72+
73+
const WebCryptoImpl webCryptImpl = _WebCryptoImpl();
74+
75+
final class _WebCryptoImpl implements WebCryptoImpl {
76+
const _WebCryptoImpl();
77+
78+
@override
79+
final aesCbcSecretKey = const _StaticAesCbcSecretKeyImpl();
80+
81+
@override
82+
final aesCtrSecretKey = const _StaticAesCtrSecretKeyImpl();
83+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
part of 'impl_interface.dart';
16+
17+
abstract interface class StaticAesCbcSecretKeyImpl {
18+
Future<AesCbcSecretKeyImpl> importRawKey(List<int> keyData);
19+
Future<AesCbcSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk);
20+
Future<AesCbcSecretKeyImpl> generateKey(int length);
21+
}
22+
23+
abstract class AesCbcSecretKeyImpl {
24+
Future<Uint8List> encryptBytes(List<int> data, List<int> iv);
25+
Future<Uint8List> decryptBytes(List<int> data, List<int> iv);
26+
Stream<Uint8List> encryptStream(Stream<List<int>> data, List<int> iv);
27+
Stream<Uint8List> decryptStream(Stream<List<int>> data, List<int> iv);
28+
Future<Uint8List> exportRawKey();
29+
Future<Map<String, dynamic>> exportJsonWebKey();
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
part of 'impl_interface.dart';
16+
17+
abstract interface class StaticAesCtrSecretKeyImpl {
18+
Future<AesCtrSecretKeyImpl> importRawKey(List<int> keyData);
19+
Future<AesCtrSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk);
20+
Future<AesCtrSecretKeyImpl> generateKey(int length);
21+
}
22+
23+
abstract class AesCtrSecretKeyImpl {
24+
Future<Uint8List> encryptBytes(List<int> data, List<int> counter, int length);
25+
Future<Uint8List> decryptBytes(List<int> data, List<int> counter, int length);
26+
Stream<Uint8List> encryptStream(Stream<List<int>> data, List<int> counter, int length);
27+
Stream<Uint8List> decryptStream(Stream<List<int>> data, List<int> counter, int length);
28+
Future<Uint8List> exportRawKey();
29+
Future<Map<String, dynamic>> exportJsonWebKey();
30+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
library impl_stub;
16+
17+
import 'dart:typed_data';
18+
import 'dart:async';
19+
20+
21+
part 'impl_interface.aescbc.dart';
22+
part 'impl_interface.aesctr.dart';
23+
24+
/// Interface to be provided by platform implementations.
25+
///
26+
/// A platform implementation of `package:webcrypto` must define a
27+
/// constant `webCryptImpl` as follows:
28+
/// ```dart
29+
/// const WebCryptoImpl webCryptImpl = const _MyPlatformImplemetation();
30+
/// ```
31+
///
32+
/// The only platform implementations are:
33+
/// * `lib/src/impl_ffi/impl_ffi.dart`,
34+
/// * `lib/src/impl_js/impl_js.dart`, and,
35+
/// * `lib/src/impl_stub/impl_stub.dart`.
36+
///
37+
/// These interfaces are not public and should not be implemented
38+
/// outside this package. Should platform implementations ever become
39+
/// plugable these interfaces will be renamed.
40+
abstract interface class WebCryptoImpl {
41+
StaticAesCbcSecretKeyImpl get aesCbcSecretKey;
42+
StaticAesCtrSecretKeyImpl get aesCtrSecretKey;
43+
}

lib/src/impl_js/impl_js.aescbc.dart

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ part of 'impl_js.dart';
1818

1919
const _aesCbcAlgorithm = subtle.Algorithm(name: 'AES-CBC');
2020

21-
Future<AesCbcSecretKey> aesCbc_importRawKey(List<int> keyData) async {
22-
return _AesCbcSecretKey(await _importKey(
21+
Future<AesCbcSecretKeyImpl> aesCbc_importRawKey(List<int> keyData) async {
22+
return _AesCbcSecretKeyImpl(await _importKey(
2323
'raw',
2424
keyData,
2525
_aesCbcAlgorithm,
@@ -28,28 +28,47 @@ Future<AesCbcSecretKey> aesCbc_importRawKey(List<int> keyData) async {
2828
));
2929
}
3030

31-
Future<AesCbcSecretKey> aesCbc_importJsonWebKey(
31+
Future<AesCbcSecretKeyImpl> aesCbc_importJsonWebKey(
3232
Map<String, dynamic> jwk,
3333
) async {
34-
return _AesCbcSecretKey(await _importJsonWebKey(
34+
return _AesCbcSecretKeyImpl(await _importJsonWebKey(
3535
jwk,
3636
_aesCbcAlgorithm,
3737
_usagesEncryptDecrypt,
3838
'secret',
3939
));
4040
}
4141

42-
Future<AesCbcSecretKey> aesCbc_generateKey(int length) async {
43-
return _AesCbcSecretKey(await _generateKey(
42+
Future<AesCbcSecretKeyImpl> aesCbc_generateKey(int length) async {
43+
return _AesCbcSecretKeyImpl(await _generateKey(
4444
_aesCbcAlgorithm.update(length: length),
4545
_usagesEncryptDecrypt,
4646
'secret',
4747
));
4848
}
4949

50-
class _AesCbcSecretKey implements AesCbcSecretKey {
50+
final class _StaticAesCbcSecretKeyImpl implements StaticAesCbcSecretKeyImpl {
51+
const _StaticAesCbcSecretKeyImpl();
52+
53+
@override
54+
Future<AesCbcSecretKeyImpl> importRawKey(List<int> keyData) async {
55+
return await aesCbc_importRawKey(keyData);
56+
}
57+
58+
@override
59+
Future<AesCbcSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk) async {
60+
return await aesCbc_importJsonWebKey(jwk);
61+
}
62+
63+
@override
64+
Future<AesCbcSecretKeyImpl> generateKey(int length) async {
65+
return await aesCbc_generateKey(length);
66+
}
67+
}
68+
69+
final class _AesCbcSecretKeyImpl implements AesCbcSecretKeyImpl {
5170
final subtle.JSCryptoKey _key;
52-
_AesCbcSecretKey(this._key);
71+
_AesCbcSecretKeyImpl(this._key);
5372

5473
@override
5574
String toString() {

lib/src/impl_js/impl_js.aesctr.dart

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ part of 'impl_js.dart';
1818

1919
const _aesCtrAlgorithm = subtle.Algorithm(name: 'AES-CTR');
2020

21-
Future<AesCtrSecretKey> aesCtr_importRawKey(List<int> keyData) async {
22-
return _AesCtrSecretKey(await _importKey(
21+
Future<AesCtrSecretKeyImpl> aesCtr_importRawKey(List<int> keyData) async {
22+
return _AesCtrSecretKeyImpl(await _importKey(
2323
'raw',
2424
keyData,
2525
_aesCtrAlgorithm,
@@ -28,28 +28,47 @@ Future<AesCtrSecretKey> aesCtr_importRawKey(List<int> keyData) async {
2828
));
2929
}
3030

31-
Future<AesCtrSecretKey> aesCtr_importJsonWebKey(
31+
Future<AesCtrSecretKeyImpl> aesCtr_importJsonWebKey(
3232
Map<String, dynamic> jwk,
3333
) async {
34-
return _AesCtrSecretKey(await _importJsonWebKey(
34+
return _AesCtrSecretKeyImpl(await _importJsonWebKey(
3535
jwk,
3636
_aesCtrAlgorithm,
3737
_usagesEncryptDecrypt,
3838
'secret',
3939
));
4040
}
4141

42-
Future<AesCtrSecretKey> aesCtr_generateKey(int length) async {
43-
return _AesCtrSecretKey(await _generateKey(
42+
Future<AesCtrSecretKeyImpl> aesCtr_generateKey(int length) async {
43+
return _AesCtrSecretKeyImpl(await _generateKey(
4444
_aesCtrAlgorithm.update(length: length),
4545
_usagesEncryptDecrypt,
4646
'secret',
4747
));
4848
}
4949

50-
class _AesCtrSecretKey implements AesCtrSecretKey {
50+
final class _StaticAesCtrSecretKeyImpl implements StaticAesCtrSecretKeyImpl {
51+
const _StaticAesCtrSecretKeyImpl();
52+
53+
@override
54+
Future<AesCtrSecretKeyImpl> importRawKey(List<int> keyData) async {
55+
return await aesCtr_importRawKey(keyData);
56+
}
57+
58+
@override
59+
Future<AesCtrSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk) async {
60+
return await aesCtr_importJsonWebKey(jwk);
61+
}
62+
63+
@override
64+
Future<AesCtrSecretKeyImpl> generateKey(int length) async {
65+
return await aesCtr_generateKey(length);
66+
}
67+
}
68+
69+
final class _AesCtrSecretKeyImpl extends AesCtrSecretKeyImpl {
5170
final subtle.JSCryptoKey _key;
52-
_AesCtrSecretKey(this._key);
71+
_AesCtrSecretKeyImpl(this._key);
5372

5473
@override
5574
String toString() {

0 commit comments

Comments
 (0)