Skip to content

Commit 4d4bd36

Browse files
fix!: remove cyclic imports and migrate keypair class
1 parent ec05a3d commit 4d4bd36

19 files changed

+104
-102
lines changed

lib/src/impl_ffi/impl_ffi.dart

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,6 @@ class _OperationError extends Error implements OperationError {
5858
String toString() => _message;
5959
}
6060

61-
/// Implementation of [KeyPair].
62-
class _KeyPair<S, T> implements KeyPair<S, T> {
63-
@override
64-
final S privateKey;
65-
66-
@override
67-
final T publicKey;
68-
69-
_KeyPair({required this.privateKey, required this.publicKey});
70-
}
71-
72-
7361
const WebCryptoImpl webCryptImpl = _WebCryptoImpl();
7462

7563
final class _WebCryptoImpl implements WebCryptoImpl {

lib/src/impl_ffi/impl_ffi.ec_common.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,9 @@ KeyPair<_EvpPKey, _EvpPKey> _generateEcKeyPair(
351351
final pubKey = _EvpPKey();
352352
_checkOpIsOne(ssl.EVP_PKEY_set1_EC_KEY.invoke(pubKey, ecPub));
353353

354-
return _KeyPair(
355-
privateKey: privKey,
356-
publicKey: pubKey,
354+
return createKeyPair(
355+
privKey,
356+
pubKey,
357357
);
358358
});
359359
}

lib/src/impl_ffi/impl_ffi.ecdh.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> ecdhPrivateKey_generateKe
3838
EllipticCurve curve,
3939
) async {
4040
final p = _generateEcKeyPair(curve);
41-
return _KeyPair(
42-
privateKey: _EcdhPrivateKeyImpl(p.privateKey),
43-
publicKey: _EcdhPublicKeyImpl(p.publicKey),
41+
return createKeyPair(
42+
_EcdhPrivateKeyImpl(p.privateKey),
43+
_EcdhPublicKeyImpl(p.publicKey),
4444
);
4545
}
4646

@@ -80,8 +80,11 @@ final class _StaticEcdhPrivateKeyImpl implements StaticEcdhPrivateKeyImpl {
8080
ecdhPrivateKey_importJsonWebKey(jwk, curve);
8181

8282
@override
83-
Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> generateKey(EllipticCurve curve) =>
84-
ecdhPrivateKey_generateKey(curve);
83+
Future<(EcdhPrivateKeyImpl, EcdhPublicKeyImpl)> generateKey(EllipticCurve curve) async {
84+
final KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl> keyPair = await ecdhPrivateKey_generateKey(curve);
85+
86+
return (keyPair.privateKey, keyPair.publicKey);
87+
}
8588
}
8689

8790
final class _EcdhPrivateKeyImpl implements EcdhPrivateKeyImpl {
@@ -100,7 +103,7 @@ final class _EcdhPrivateKeyImpl implements EcdhPrivateKeyImpl {
100103
throw ArgumentError.value(
101104
publicKey,
102105
'publicKey',
103-
'custom implementations of EcdhPublicKeyImpl is not supported',
106+
'custom implementations of EcdhPublicKey is not supported',
104107
);
105108
}
106109
if (length <= 0) {

lib/src/impl_ffi/impl_ffi.ecdsa.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ Future<KeyPair<EcdsaPrivateKey, EcdsaPublicKey>> ecdsaPrivateKey_generateKey(
5454
EllipticCurve curve,
5555
) async {
5656
final p = _generateEcKeyPair(curve);
57-
return _KeyPair(
58-
privateKey: _EcdsaPrivateKey(p.privateKey),
59-
publicKey: _EcdsaPublicKey(p.publicKey),
57+
return createKeyPair(
58+
_EcdsaPrivateKey(p.privateKey),
59+
_EcdsaPublicKey(p.publicKey),
6060
);
6161
}
6262

lib/src/impl_ffi/impl_ffi.rsa_common.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ Map<String, dynamic> _exportJwkRsaPrivateOrPublicKey(
229229
});
230230
}
231231

232-
_KeyPair<_EvpPKey, _EvpPKey> _generateRsaKeyPair(
232+
KeyPair<_EvpPKey, _EvpPKey> _generateRsaKeyPair(
233233
int modulusLength,
234234
BigInt publicExponent,
235235
) {
@@ -278,9 +278,9 @@ _KeyPair<_EvpPKey, _EvpPKey> _generateRsaKeyPair(
278278
final pubKey = _EvpPKey();
279279
_checkOp(ssl.EVP_PKEY_set1_RSA.invoke(pubKey, pubRSA) == 1);
280280

281-
return _KeyPair(
282-
privateKey: privKey,
283-
publicKey: pubKey,
281+
return createKeyPair(
282+
privKey,
283+
pubKey,
284284
);
285285
});
286286
}

lib/src/impl_ffi/impl_ffi.rsaoaep.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ Future<KeyPair<RsaOaepPrivateKey, RsaOaepPublicKey>>
6868
// Get hash first, to avoid a leak of EVP_PKEY if _Hash.fromHash throws
6969
final h = _Hash.fromHash(hash);
7070
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
71-
return _KeyPair(
72-
privateKey: _RsaOaepPrivateKey(keys.privateKey, h),
73-
publicKey: _RsaOaepPublicKey(keys.publicKey, h),
71+
return createKeyPair(
72+
_RsaOaepPrivateKey(keys.privateKey, h),
73+
_RsaOaepPublicKey(keys.publicKey, h),
7474
);
7575
}
7676

lib/src/impl_ffi/impl_ffi.rsapss.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ Future<KeyPair<RsaPssPrivateKey, RsaPssPublicKey>> rsaPssPrivateKey_generateKey(
6767
// Validate and get hash function
6868
final h = _Hash.fromHash(hash);
6969
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
70-
return _KeyPair(
71-
privateKey: _RsaPssPrivateKey(keys.privateKey, h),
72-
publicKey: _RsaPssPublicKey(keys.publicKey, h),
70+
return createKeyPair(
71+
_RsaPssPrivateKey(keys.privateKey, h),
72+
_RsaPssPublicKey(keys.publicKey, h),
7373
);
7474
}
7575

lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ Future<KeyPair<RsassaPkcs1V15PrivateKey, RsassaPkcs1V15PublicKey>>
6868
// Get hash first, to avoid a leak of EVP_PKEY if _Hash.fromHash throws
6969
final h = _Hash.fromHash(hash);
7070
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
71-
return _KeyPair(
72-
privateKey: _RsassaPkcs1V15PrivateKey(keys.privateKey, h),
73-
publicKey: _RsassaPkcs1V15PublicKey(keys.publicKey, h),
71+
return createKeyPair(
72+
_RsassaPkcs1V15PrivateKey(keys.privateKey, h),
73+
_RsassaPkcs1V15PublicKey(keys.publicKey, h),
7474
);
7575
}
7676

lib/src/impl_interface/impl_interface.dart

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ library impl_stub;
1717
import 'dart:typed_data';
1818
import 'dart:async';
1919

20-
import 'package:webcrypto/webcrypto.dart';
21-
20+
import 'package:webcrypto/webcrypto.dart' show Hash;
2221

2322
part 'impl_interface.aescbc.dart';
2423
part 'impl_interface.aesctr.dart';
@@ -27,6 +26,37 @@ part 'impl_interface.pbkdf2.dart';
2726
part 'impl_interface.aesgcm.dart';
2827
part 'impl_interface.ecdh.dart';
2928

29+
/// A key-pair as returned from key generation.
30+
class KeyPair<S, T> {
31+
KeyPair._(this.privateKey, this.publicKey); // keep the constructor private.
32+
33+
/// Private key for [publicKey].
34+
final S privateKey;
35+
36+
/// Public key matching [privateKey].
37+
final T publicKey;
38+
}
39+
40+
/// Factory method to create KeyPair instance
41+
KeyPair<S, T> createKeyPair<S, T>(S privateKey, T publicKey) {
42+
return KeyPair._(privateKey, publicKey);
43+
}
44+
45+
/// Elliptic curves supported by ECDSA and ECDH.
46+
///
47+
/// > [!NOTE]
48+
/// > Additional values may be added to this enum in the future.
49+
enum EllipticCurve {
50+
p256,
51+
p384,
52+
53+
///
54+
///
55+
/// P-521 is **not supported on Safari**, see [bug 216755 (bugs.webkit.org)][1].
56+
///
57+
/// [1]: https://bugs.webkit.org/show_bug.cgi?id=216755
58+
p521,
59+
}
3060

3161
/// Interface to be provided by platform implementations.
3262
///

lib/src/impl_interface/impl_interface.ecdh.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ part of 'impl_interface.dart';
1717
abstract interface class StaticEcdhPrivateKeyImpl {
1818
Future<EcdhPrivateKeyImpl> importPkcs8Key(List<int> keyData, EllipticCurve curve);
1919
Future<EcdhPrivateKeyImpl> importJsonWebKey(Map<String, dynamic> jwk, EllipticCurve curve);
20-
Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> generateKey(EllipticCurve curve);
20+
Future<(EcdhPrivateKeyImpl, EcdhPublicKeyImpl)> generateKey(EllipticCurve curve);
2121
}
2222

2323
abstract interface class EcdhPrivateKeyImpl {

0 commit comments

Comments
 (0)