Skip to content

Commit c1ca4f4

Browse files
refactor: update hkdf-secret-key (#164)
1 parent 01a7504 commit c1ca4f4

File tree

10 files changed

+99
-18
lines changed

10 files changed

+99
-18
lines changed

lib/src/impl_ffi/impl_ffi.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,7 @@ final class _WebCryptoImpl implements WebCryptoImpl {
9595

9696
@override
9797
final rsaOaepPublicKey = const _StaticRsaOaepPublicKeyImpl();
98+
99+
@override
100+
final hkdfSecretKey = const _StaticHkdfSecretKeyImpl();
98101
}

lib/src/impl_ffi/impl_ffi.hkdf.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@
1616

1717
part of 'impl_ffi.dart';
1818

19-
Future<HkdfSecretKey> hkdfSecretKey_importRawKey(List<int> keyData) async =>
20-
_HkdfSecretKey(Uint8List.fromList(keyData));
19+
Future<HkdfSecretKeyImpl> hkdfSecretKey_importRawKey(List<int> keyData) async =>
20+
_HkdfSecretKeyImpl(Uint8List.fromList(keyData));
2121

22-
class _HkdfSecretKey implements HkdfSecretKey {
22+
final class _StaticHkdfSecretKeyImpl implements StaticHkdfSecretKeyImpl {
23+
const _StaticHkdfSecretKeyImpl();
24+
25+
@override
26+
Future<HkdfSecretKeyImpl> importRawKey(List<int> keyData) async {
27+
return hkdfSecretKey_importRawKey(keyData);
28+
}
29+
}
30+
31+
final class _HkdfSecretKeyImpl implements HkdfSecretKeyImpl {
2332
final Uint8List _key;
2433

25-
_HkdfSecretKey(this._key);
34+
_HkdfSecretKeyImpl(this._key);
2635

2736
@override
2837
String toString() {

lib/src/impl_interface/impl_interface.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ part 'impl_interface.aesgcm.dart';
2727
part 'impl_interface.ecdh.dart';
2828
part 'impl_interface.ecdsa.dart';
2929
part 'impl_interface.rsaoaep.dart';
30+
part 'impl_interface.hkdf.dart';
3031

3132
/// A key-pair as returned from key generation.
3233
class KeyPair<S, T> {
@@ -88,4 +89,5 @@ abstract interface class WebCryptoImpl {
8889
StaticEcdsaPublicKeyImpl get ecdsaPublicKey;
8990
StaticRsaOaepPrivateKeyImpl get rsaOaepPrivateKey;
9091
StaticRsaOaepPublicKeyImpl get rsaOaepPublicKey;
92+
StaticHkdfSecretKeyImpl get hkdfSecretKey;
9193
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 StaticHkdfSecretKeyImpl {
18+
Future<HkdfSecretKeyImpl> importRawKey(List<int> keyData);
19+
}
20+
21+
abstract interface class HkdfSecretKeyImpl {
22+
Future<Uint8List> deriveBits(
23+
int length,
24+
Hash hash,
25+
List<int> salt,
26+
List<int> info,
27+
);
28+
}

lib/src/impl_js/impl_js.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,7 @@ final class _WebCryptoImpl implements WebCryptoImpl {
8282

8383
@override
8484
final rsaOaepPublicKey = const _StaticRsaOaepPublicKeyImpl();
85+
86+
@override
87+
final hkdfSecretKey = const _StaticHkdfSecretKeyImpl();
8588
}

lib/src/impl_js/impl_js.hkdf.dart

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

1919
const _hkdfAlgorithmName = 'HKDF';
2020

21-
Future<HkdfSecretKey> hkdfSecretKey_importRawKey(List<int> keyData) async {
22-
return _HkdfSecretKey(await _importKey(
21+
Future<HkdfSecretKeyImpl> hkdfSecretKey_importRawKey(List<int> keyData) async {
22+
return _HkdfSecretKeyImpl(await _importKey(
2323
'raw',
2424
keyData,
2525
const subtle.Algorithm(name: _hkdfAlgorithmName),
2626
_usagesDeriveBits,
2727
'secret',
28-
// Unlike all other key types it makes no sense to HkdfSecretKey to be
28+
// Unlike all other key types it makes no sense to HkdfSecretKeyImpl to be
2929
// exported, and indeed webcrypto requires `extractable: false`.
3030
extractable: false,
3131
));
3232
}
3333

34-
class _HkdfSecretKey implements HkdfSecretKey {
34+
final class _StaticHkdfSecretKeyImpl implements StaticHkdfSecretKeyImpl {
35+
const _StaticHkdfSecretKeyImpl();
36+
37+
@override
38+
Future<HkdfSecretKeyImpl> importRawKey(List<int> keyData) async {
39+
return await hkdfSecretKey_importRawKey(keyData);
40+
}
41+
}
42+
43+
final class _HkdfSecretKeyImpl implements HkdfSecretKeyImpl {
3544
final subtle.JSCryptoKey _key;
36-
_HkdfSecretKey(this._key);
45+
_HkdfSecretKeyImpl(this._key);
3746

3847
@override
3948
String toString() {

lib/src/impl_stub.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,4 @@ Future<RsaPssPublicKey> rsaPssPublicKey_importJsonWebKey(
126126

127127
//---------------------- HKDF
128128

129-
Future<HkdfSecretKey> hkdfSecretKey_importRawKey(List<int> keyData) =>
130-
throw _notImplemented;
131-
132129
//---------------------- PBKDF2

lib/src/impl_stub/impl_stub.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ part 'impl_stub.pbkdf2.dart';
2626
part 'impl_stub.ecdh.dart';
2727
part 'impl_stub.ecdsa.dart';
2828
part 'impl_stub.rsaoaep.dart';
29+
part 'impl_stub.hkdf.dart';
2930

3031
const WebCryptoImpl webCryptImpl = _WebCryptoImpl();
3132

@@ -64,4 +65,7 @@ final class _WebCryptoImpl implements WebCryptoImpl {
6465

6566
@override
6667
final rsaOaepPublicKey = const _StaticRsaOaepPublicKeyImpl();
68+
69+
@override
70+
final hkdfSecretKey = const _StaticHkdfSecretKeyImpl();
6771
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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_stub.dart';
16+
17+
final class _StaticHkdfSecretKeyImpl implements StaticHkdfSecretKeyImpl {
18+
const _StaticHkdfSecretKeyImpl();
19+
20+
@override
21+
Future<HkdfSecretKeyImpl> importRawKey(List<int> keyData) =>
22+
throw UnimplementedError('Not implemented');
23+
}

lib/src/webcrypto/webcrypto.hkdf.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,19 @@ part of 'webcrypto.dart';
5050
/// [1]: https://tools.ietf.org/html/rfc5869
5151
// TODO: It might be wise to use a random salt, then suggest that the non-secret
5252
// salt is stored or exchanged...
53-
@sealed
54-
abstract class HkdfSecretKey {
55-
HkdfSecretKey._(); // keep the constructor private.
53+
final class HkdfSecretKey {
54+
final HkdfSecretKeyImpl _impl;
55+
56+
HkdfSecretKey._(this._impl); // keep the constructor private.
5657

5758
/// Import [HkdfSecretKey] from raw [keyData].
5859
///
5960
/// Creates a [HkdfSecretKey] for key derivation using [keyData].
6061
///
6162
/// {@macro HkdfSecretKey:example}
62-
static Future<HkdfSecretKey> importRawKey(List<int> keyData) {
63-
return impl.hkdfSecretKey_importRawKey(keyData);
63+
static Future<HkdfSecretKey> importRawKey(List<int> keyData) async {
64+
final impl = await webCryptImpl.hkdfSecretKey.importRawKey(keyData);
65+
return HkdfSecretKey._(impl);
6466
}
6567

6668
/// Derive key from [salt], [info] and password specified as `keyData` in
@@ -89,5 +91,6 @@ abstract class HkdfSecretKey {
8991
Hash hash,
9092
List<int> salt,
9193
List<int> info,
92-
);
94+
) =>
95+
_impl.deriveBits(length, hash, salt, info);
9396
}

0 commit comments

Comments
 (0)