Skip to content

Commit

Permalink
Refactor EcdsaPublicKey and EcdsaPrivateKey Class (#162)
Browse files Browse the repository at this point in the history
* refactor: update ecdsa-public-key ecdsa-private-key

* style: format dart code

* fix: resolve dart:ui failed tests
  • Loading branch information
HamdaanAliQuatil authored Oct 22, 2024
1 parent 0a4a542 commit 8ac145b
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 100 deletions.
6 changes: 6 additions & 0 deletions lib/src/impl_ffi/impl_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ final class _WebCryptoImpl implements WebCryptoImpl {
@override
final ecdhPublicKey = const _StaticEcdhPublicKeyImpl();

@override
final ecdsaPrivateKey = const _StaticEcdsaPrivateKeyImpl();

@override
final ecdsaPublicKey = const _StaticEcdsaPublicKeyImpl();

@override
final rsaOaepPrivateKey = const _StaticRsaOaepPrivateKeyImpl();

Expand Down
86 changes: 67 additions & 19 deletions lib/src/impl_ffi/impl_ffi.ecdsa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,51 +32,52 @@ String _ecdsaCurveToJwkAlg(EllipticCurve curve) {
throw UnsupportedError('curve "$curve" is not supported');
}

Future<EcdsaPrivateKey> ecdsaPrivateKey_importPkcs8Key(
Future<EcdsaPrivateKeyImpl> ecdsaPrivateKey_importPkcs8Key(
List<int> keyData,
EllipticCurve curve,
) async =>
_EcdsaPrivateKey(_importPkcs8EcPrivateKey(keyData, curve));
_EcdsaPrivateKeyImpl(_importPkcs8EcPrivateKey(keyData, curve));

Future<EcdsaPrivateKey> ecdsaPrivateKey_importJsonWebKey(
Future<EcdsaPrivateKeyImpl> ecdsaPrivateKey_importJsonWebKey(
Map<String, dynamic> jwk,
EllipticCurve curve,
) async =>
_EcdsaPrivateKey(_importJwkEcPrivateOrPublicKey(
_EcdsaPrivateKeyImpl(_importJwkEcPrivateOrPublicKey(
JsonWebKey.fromJson(jwk),
curve,
isPrivateKey: true,
expectedUse: 'sig',
expectedAlg: _ecdsaCurveToJwkAlg(curve),
));

Future<KeyPair<EcdsaPrivateKey, EcdsaPublicKey>> ecdsaPrivateKey_generateKey(
Future<KeyPair<EcdsaPrivateKeyImpl, EcdsaPublicKeyImpl>>
ecdsaPrivateKey_generateKey(
EllipticCurve curve,
) async {
final p = _generateEcKeyPair(curve);
return createKeyPair(
_EcdsaPrivateKey(p.privateKey),
_EcdsaPublicKey(p.publicKey),
_EcdsaPrivateKeyImpl(p.privateKey),
_EcdsaPublicKeyImpl(p.publicKey),
);
}

Future<EcdsaPublicKey> ecdsaPublicKey_importRawKey(
Future<EcdsaPublicKeyImpl> ecdsaPublicKey_importRawKey(
List<int> keyData,
EllipticCurve curve,
) async =>
_EcdsaPublicKey(_importRawEcPublicKey(keyData, curve));
_EcdsaPublicKeyImpl(_importRawEcPublicKey(keyData, curve));

Future<EcdsaPublicKey> ecdsaPublicKey_importSpkiKey(
Future<EcdsaPublicKeyImpl> ecdsaPublicKey_importSpkiKey(
List<int> keyData,
EllipticCurve curve,
) async =>
_EcdsaPublicKey(_importSpkiEcPublicKey(keyData, curve));
_EcdsaPublicKeyImpl(_importSpkiEcPublicKey(keyData, curve));

Future<EcdsaPublicKey> ecdsaPublicKey_importJsonWebKey(
Future<EcdsaPublicKeyImpl> ecdsaPublicKey_importJsonWebKey(
Map<String, dynamic> jwk,
EllipticCurve curve,
) async =>
_EcdsaPublicKey(_importJwkEcPrivateOrPublicKey(
_EcdsaPublicKeyImpl(_importJwkEcPrivateOrPublicKey(
JsonWebKey.fromJson(jwk),
curve,
isPrivateKey: false,
Expand Down Expand Up @@ -180,14 +181,40 @@ Uint8List? _convertEcdsaWebCryptoSignatureToDerSignature(
});
}

class _EcdsaPrivateKey implements EcdsaPrivateKey {
final class _StaticEcdsaPrivateKeyImpl implements StaticEcdsaPrivateKeyImpl {
const _StaticEcdsaPrivateKeyImpl();

@override
Future<EcdsaPrivateKeyImpl> importPkcs8Key(
List<int> keyData, EllipticCurve curve) =>
ecdsaPrivateKey_importPkcs8Key(keyData, curve);

@override
Future<EcdsaPrivateKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk,
EllipticCurve curve,
) =>
ecdsaPrivateKey_importJsonWebKey(jwk, curve);

@override
Future<(EcdsaPrivateKeyImpl, EcdsaPublicKeyImpl)> generateKey(
EllipticCurve curve,
) async {
final KeyPair<EcdsaPrivateKeyImpl, EcdsaPublicKeyImpl> keyPair =
await ecdsaPrivateKey_generateKey(curve);

return (keyPair.privateKey, keyPair.publicKey);
}
}

final class _EcdsaPrivateKeyImpl implements EcdsaPrivateKeyImpl {
final _EvpPKey _key;

_EcdsaPrivateKey(this._key);
_EcdsaPrivateKeyImpl(this._key);

@override
String toString() {
return 'Instance of \'EcdsaPrivateKey\'';
return 'Instance of \'EcdsaPrivateKeyImpl\'';
}

@override
Expand All @@ -209,14 +236,35 @@ class _EcdsaPrivateKey implements EcdsaPrivateKey {
Future<Uint8List> exportPkcs8Key() async => _exportPkcs8Key(_key);
}

class _EcdsaPublicKey implements EcdsaPublicKey {
final class _StaticEcdsaPublicKeyImpl implements StaticEcdsaPublicKeyImpl {
const _StaticEcdsaPublicKeyImpl();

@override
Future<EcdsaPublicKeyImpl> importRawKey(
List<int> keyData, EllipticCurve curve) =>
ecdsaPublicKey_importRawKey(keyData, curve);

@override
Future<EcdsaPublicKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk,
EllipticCurve curve,
) =>
ecdsaPublicKey_importJsonWebKey(jwk, curve);

@override
Future<EcdsaPublicKeyImpl> importSpkiKey(
List<int> keyData, EllipticCurve curve) =>
ecdsaPublicKey_importSpkiKey(keyData, curve);
}

final class _EcdsaPublicKeyImpl implements EcdsaPublicKeyImpl {
final _EvpPKey _key;

_EcdsaPublicKey(this._key);
_EcdsaPublicKeyImpl(this._key);

@override
String toString() {
return 'Instance of \'EcdsaPublicKey\'';
return 'Instance of \'EcdsaPublicKeyImpl\'';
}

@override
Expand Down
3 changes: 3 additions & 0 deletions lib/src/impl_interface/impl_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ part 'impl_interface.hmac.dart';
part 'impl_interface.pbkdf2.dart';
part 'impl_interface.aesgcm.dart';
part 'impl_interface.ecdh.dart';
part 'impl_interface.ecdsa.dart';
part 'impl_interface.rsaoaep.dart';

/// A key-pair as returned from key generation.
Expand Down Expand Up @@ -83,6 +84,8 @@ abstract interface class WebCryptoImpl {
StaticPbkdf2SecretKeyImpl get pbkdf2SecretKey;
StaticEcdhPrivateKeyImpl get ecdhPrivateKey;
StaticEcdhPublicKeyImpl get ecdhPublicKey;
StaticEcdsaPrivateKeyImpl get ecdsaPrivateKey;
StaticEcdsaPublicKeyImpl get ecdsaPublicKey;
StaticRsaOaepPrivateKeyImpl get rsaOaepPrivateKey;
StaticRsaOaepPublicKeyImpl get rsaOaepPublicKey;
}
49 changes: 49 additions & 0 deletions lib/src/impl_interface/impl_interface.ecdsa.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

part of 'impl_interface.dart';

abstract interface class StaticEcdsaPrivateKeyImpl {
Future<EcdsaPrivateKeyImpl> importPkcs8Key(
List<int> keyData, EllipticCurve curve);
Future<EcdsaPrivateKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk, EllipticCurve curve);
Future<(EcdsaPrivateKeyImpl, EcdsaPublicKeyImpl)> generateKey(
EllipticCurve curve);
}

abstract interface class EcdsaPrivateKeyImpl {
Future<Uint8List> signBytes(List<int> data, Hash hash);
Future<Uint8List> signStream(Stream<List<int>> data, Hash hash);
Future<Uint8List> exportPkcs8Key();
Future<Map<String, dynamic>> exportJsonWebKey();
}

abstract interface class StaticEcdsaPublicKeyImpl {
Future<EcdsaPublicKeyImpl> importRawKey(
List<int> keyData, EllipticCurve curve);
Future<EcdsaPublicKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk, EllipticCurve curve);
Future<EcdsaPublicKeyImpl> importSpkiKey(
List<int> keyData, EllipticCurve curve);
}

abstract interface class EcdsaPublicKeyImpl {
Future<bool> verifyBytes(List<int> signature, List<int> data, Hash hash);
Future<bool> verifyStream(
List<int> signature, Stream<List<int>> data, Hash hash);
Future<Uint8List> exportRawKey();
Future<Uint8List> exportSpkiKey();
Future<Map<String, dynamic>> exportJsonWebKey();
}
6 changes: 6 additions & 0 deletions lib/src/impl_js/impl_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ final class _WebCryptoImpl implements WebCryptoImpl {
@override
final ecdhPublicKey = const _StaticEcdhPublicKeyImpl();

@override
final ecdsaPrivateKey = const _StaticEcdsaPrivateKeyImpl();

@override
final ecdsaPublicKey = const _StaticEcdsaPublicKeyImpl();

@override
final rsaOaepPrivateKey = const _StaticRsaOaepPrivateKeyImpl();

Expand Down
Loading

0 comments on commit 8ac145b

Please sign in to comment.