Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor EcdhPublicKey and EcdhPrivateKey Class #145

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -80,4 +80,10 @@ final class _WebCryptoImpl implements WebCryptoImpl {

@override
final aesCtrSecretKey = const _StaticAesCtrSecretKeyImpl();

@override
final ecdhPrivateKey = const _StaticEcdhPrivateKeyImpl();

@override
final ecdhPublicKey = const _StaticEcdhPublicKeyImpl();
}
72 changes: 52 additions & 20 deletions lib/src/impl_ffi/impl_ffi.ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,75 +16,91 @@

part of 'impl_ffi.dart';

Future<EcdhPrivateKey> ecdhPrivateKey_importPkcs8Key(
Future<EcdhPrivateKeyImpl> ecdhPrivateKey_importPkcs8Key(
List<int> keyData,
EllipticCurve curve,
) async =>
_EcdhPrivateKey(_importPkcs8EcPrivateKey(keyData, curve));
_EcdhPrivateKeyImpl(_importPkcs8EcPrivateKey(keyData, curve));

Future<EcdhPrivateKey> ecdhPrivateKey_importJsonWebKey(
Future<EcdhPrivateKeyImpl> ecdhPrivateKey_importJsonWebKey(
Map<String, dynamic> jwk,
EllipticCurve curve,
) async =>
_EcdhPrivateKey(_importJwkEcPrivateOrPublicKey(
_EcdhPrivateKeyImpl(_importJwkEcPrivateOrPublicKey(
JsonWebKey.fromJson(jwk),
curve,
isPrivateKey: true,
expectedUse: 'enc',
expectedAlg: null, // ECDH has no validation of 'jwk.alg'
));

Future<KeyPair<EcdhPrivateKey, EcdhPublicKey>> ecdhPrivateKey_generateKey(
Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> ecdhPrivateKey_generateKey(
EllipticCurve curve,
) async {
final p = _generateEcKeyPair(curve);
return _KeyPair(
privateKey: _EcdhPrivateKey(p.privateKey),
publicKey: _EcdhPublicKey(p.publicKey),
privateKey: _EcdhPrivateKeyImpl(p.privateKey),
publicKey: _EcdhPublicKeyImpl(p.publicKey),
);
}

Future<EcdhPublicKey> ecdhPublicKey_importRawKey(
Future<EcdhPublicKeyImpl> ecdhPublicKey_importRawKey(
List<int> keyData,
EllipticCurve curve,
) async =>
_EcdhPublicKey(_importRawEcPublicKey(keyData, curve));
_EcdhPublicKeyImpl(_importRawEcPublicKey(keyData, curve));

Future<EcdhPublicKey> ecdhPublicKey_importSpkiKey(
Future<EcdhPublicKeyImpl> ecdhPublicKey_importSpkiKey(
List<int> keyData,
EllipticCurve curve,
) async =>
_EcdhPublicKey(_importSpkiEcPublicKey(keyData, curve));
_EcdhPublicKeyImpl(_importSpkiEcPublicKey(keyData, curve));

Future<EcdhPublicKey> ecdhPublicKey_importJsonWebKey(
Future<EcdhPublicKeyImpl> ecdhPublicKey_importJsonWebKey(
Map<String, dynamic> jwk,
EllipticCurve curve,
) async =>
_EcdhPublicKey(_importJwkEcPrivateOrPublicKey(
_EcdhPublicKeyImpl(_importJwkEcPrivateOrPublicKey(
JsonWebKey.fromJson(jwk),
curve,
isPrivateKey: false,
expectedUse: 'enc',
expectedAlg: null, // ECDH has no validation of 'jwk.alg'
));

class _EcdhPrivateKey implements EcdhPrivateKey {
final class _StaticEcdhPrivateKeyImpl implements StaticEcdhPrivateKeyImpl {
const _StaticEcdhPrivateKeyImpl();

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

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

@override
Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> generateKey(EllipticCurve curve) =>
ecdhPrivateKey_generateKey(curve);
}

final class _EcdhPrivateKeyImpl implements EcdhPrivateKeyImpl {
final _EvpPKey _key;

_EcdhPrivateKey(this._key);
_EcdhPrivateKeyImpl(this._key);

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

@override
Future<Uint8List> deriveBits(int length, EcdhPublicKey publicKey) async {
if (publicKey is! _EcdhPublicKey) {
Future<Uint8List> deriveBits(int length, EcdhPublicKeyImpl publicKey) async {
if (publicKey is! _EcdhPublicKeyImpl) {
throw ArgumentError.value(
publicKey,
'publicKey',
'custom implementations of EcdhPublicKey is not supported',
'custom implementations of EcdhPublicKeyImpl is not supported',
);
}
if (length <= 0) {
Expand Down Expand Up @@ -167,10 +183,26 @@ class _EcdhPrivateKey implements EcdhPrivateKey {
Future<Uint8List> exportPkcs8Key() async => _exportPkcs8Key(_key);
}

class _EcdhPublicKey implements EcdhPublicKey {
final class _StaticEcdhPublicKeyImpl implements StaticEcdhPublicKeyImpl {
const _StaticEcdhPublicKeyImpl();

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

@override
Future<EcdhPublicKeyImpl> importSpkiKey(List<int> keyData, EllipticCurve curve) =>
ecdhPublicKey_importSpkiKey(keyData, curve);

@override
Future<EcdhPublicKeyImpl> importJsonWebKey(Map<String, dynamic> jwk, EllipticCurve curve) =>
ecdhPublicKey_importJsonWebKey(jwk, curve);
}

final class _EcdhPublicKeyImpl implements EcdhPublicKeyImpl {
final _EvpPKey _key;

_EcdhPublicKey(this._key);
_EcdhPublicKeyImpl(this._key);

@override
String toString() {
Expand Down
5 changes: 5 additions & 0 deletions lib/src/impl_interface/impl_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ library impl_stub;
import 'dart:typed_data';
import 'dart:async';

import '../webcrypto/webcrypto.dart';
HamdaanAliQuatil marked this conversation as resolved.
Show resolved Hide resolved

part 'impl_interface.aescbc.dart';
part 'impl_interface.aesctr.dart';
part 'impl_interface.ecdh.dart';


/// Interface to be provided by platform implementations.
///
Expand All @@ -40,4 +43,6 @@ part 'impl_interface.aesctr.dart';
abstract interface class WebCryptoImpl {
StaticAesCbcSecretKeyImpl get aesCbcSecretKey;
StaticAesCtrSecretKeyImpl get aesCtrSecretKey;
StaticEcdhPrivateKeyImpl get ecdhPrivateKey;
StaticEcdhPublicKeyImpl get ecdhPublicKey;
}
39 changes: 39 additions & 0 deletions lib/src/impl_interface/impl_interface.ecdh.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 StaticEcdhPrivateKeyImpl {
Future<EcdhPrivateKeyImpl> importPkcs8Key(List<int> keyData, EllipticCurve curve);
Future<EcdhPrivateKeyImpl> importJsonWebKey(Map<String, dynamic> jwk, EllipticCurve curve);
Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> generateKey(EllipticCurve curve);
HamdaanAliQuatil marked this conversation as resolved.
Show resolved Hide resolved
}

abstract interface class EcdhPrivateKeyImpl {
Future<Uint8List> deriveBits(int length, EcdhPublicKeyImpl publicKey);
Future<Uint8List> exportPkcs8Key();
Future<Map<String, dynamic>> exportJsonWebKey();
}

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

abstract interface class EcdhPublicKeyImpl {
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 @@ -66,4 +66,10 @@ final class _WebCryptoImpl implements WebCryptoImpl {

@override
final aesCtrSecretKey = const _StaticAesCtrSecretKeyImpl();

@override
final ecdhPrivateKey = const _StaticEcdhPrivateKeyImpl();

@override
final ecdhPublicKey = const _StaticEcdhPublicKeyImpl();
}
76 changes: 54 additions & 22 deletions lib/src/impl_js/impl_js.ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ part of 'impl_js.dart';

const _ecdhAlgorithmName = 'ECDH';

Future<EcdhPrivateKey> ecdhPrivateKey_importPkcs8Key(
Future<EcdhPrivateKeyImpl> ecdhPrivateKey_importPkcs8Key(
List<int> keyData,
EllipticCurve curve,
) async {
return _EcdhPrivateKey(await _importKey(
return _EcdhPrivateKeyImpl(await _importKey(
'pkcs8',
keyData,
subtle.Algorithm(
Expand All @@ -34,7 +34,7 @@ Future<EcdhPrivateKey> ecdhPrivateKey_importPkcs8Key(
));
}

Future<EcdhPrivateKey> ecdhPrivateKey_importJsonWebKey(
Future<EcdhPrivateKeyImpl> ecdhPrivateKey_importJsonWebKey(
Map<String, dynamic> jwk,
EllipticCurve curve,
) async {
Expand All @@ -44,7 +44,7 @@ Future<EcdhPrivateKey> ecdhPrivateKey_importJsonWebKey(
// See: https://crbug.com/641499
jwk = Map.fromEntries(jwk.entries.where((e) => e.key != 'use'));
}
return _EcdhPrivateKey(await _importJsonWebKey(
return _EcdhPrivateKeyImpl(await _importJsonWebKey(
jwk,
subtle.Algorithm(
name: _ecdhAlgorithmName,
Expand All @@ -55,7 +55,7 @@ Future<EcdhPrivateKey> ecdhPrivateKey_importJsonWebKey(
));
}

Future<KeyPair<EcdhPrivateKey, EcdhPublicKey>> ecdhPrivateKey_generateKey(
Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> ecdhPrivateKey_generateKey(
EllipticCurve curve,
) async {
final pair = await _generateKeyPair(
Expand All @@ -66,16 +66,16 @@ Future<KeyPair<EcdhPrivateKey, EcdhPublicKey>> ecdhPrivateKey_generateKey(
_usagesDeriveBits,
);
return _KeyPair(
privateKey: _EcdhPrivateKey(pair.privateKey),
publicKey: _EcdhPublicKey(pair.publicKey),
privateKey: _EcdhPrivateKeyImpl(pair.privateKey),
publicKey: _EcdhPublicKeyImpl(pair.publicKey),
);
}

Future<EcdhPublicKey> ecdhPublicKey_importRawKey(
Future<EcdhPublicKeyImpl> ecdhPublicKey_importRawKey(
List<int> keyData,
EllipticCurve curve,
) async {
return _EcdhPublicKey(await _importKey(
return _EcdhPublicKeyImpl(await _importKey(
'raw',
keyData,
subtle.Algorithm(
Expand All @@ -87,11 +87,11 @@ Future<EcdhPublicKey> ecdhPublicKey_importRawKey(
));
}

Future<EcdhPublicKey> ecdhPublicKey_importSpkiKey(
Future<EcdhPublicKeyImpl> ecdhPublicKey_importSpkiKey(
List<int> keyData,
EllipticCurve curve,
) async {
return _EcdhPublicKey(await _importKey(
return _EcdhPublicKeyImpl(await _importKey(
'spki',
keyData,
subtle.Algorithm(
Expand All @@ -103,7 +103,7 @@ Future<EcdhPublicKey> ecdhPublicKey_importSpkiKey(
));
}

Future<EcdhPublicKey> ecdhPublicKey_importJsonWebKey(
Future<EcdhPublicKeyImpl> ecdhPublicKey_importJsonWebKey(
Map<String, dynamic> jwk,
EllipticCurve curve,
) async {
Expand All @@ -113,7 +113,7 @@ Future<EcdhPublicKey> ecdhPublicKey_importJsonWebKey(
// See: https://crbug.com/641499
jwk = Map.fromEntries(jwk.entries.where((e) => e.key != 'use'));
}
return _EcdhPublicKey(await _importJsonWebKey(
return _EcdhPublicKeyImpl(await _importJsonWebKey(
jwk,
subtle.Algorithm(
name: _ecdhAlgorithmName,
Expand All @@ -124,22 +124,38 @@ Future<EcdhPublicKey> ecdhPublicKey_importJsonWebKey(
));
}

class _EcdhPrivateKey implements EcdhPrivateKey {
final class _StaticEcdhPrivateKeyImpl implements StaticEcdhPrivateKeyImpl {
const _StaticEcdhPrivateKeyImpl();

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

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

@override
Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> generateKey(EllipticCurve curve) =>
ecdhPrivateKey_generateKey(curve);
}

final class _EcdhPrivateKeyImpl implements EcdhPrivateKeyImpl {
final subtle.JSCryptoKey _key;
_EcdhPrivateKey(this._key);
_EcdhPrivateKeyImpl(this._key);

@override
String toString() {
return 'Instance of \'EcdhPrivateKey\'';
return 'Instance of \'EcdhPrivateKeyImpl\'';
}

@override
Future<Uint8List> deriveBits(int length, EcdhPublicKey publicKey) async {
if (publicKey is! _EcdhPublicKey) {
Future<Uint8List> deriveBits(int length, EcdhPublicKeyImpl publicKey) async {
if (publicKey is! _EcdhPublicKeyImpl) {
throw ArgumentError.value(
publicKey,
'publicKey',
'custom implementations of EcdhPublicKey is not supported',
'custom implementations of EcdhPublicKeyImpl is not supported',
);
}
final lengthInBytes = (length / 8).ceil();
Expand Down Expand Up @@ -176,13 +192,29 @@ class _EcdhPrivateKey implements EcdhPrivateKey {
}
}

class _EcdhPublicKey implements EcdhPublicKey {
final class _StaticEcdhPublicKeyImpl implements StaticEcdhPublicKeyImpl {
const _StaticEcdhPublicKeyImpl();

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

@override
Future<EcdhPublicKeyImpl> importSpkiKey(List<int> keyData, EllipticCurve curve) =>
ecdhPublicKey_importSpkiKey(keyData, curve);

@override
Future<EcdhPublicKeyImpl> importJsonWebKey(Map<String, dynamic> jwk, EllipticCurve curve) =>
ecdhPublicKey_importJsonWebKey(jwk, curve);
}

final class _EcdhPublicKeyImpl implements EcdhPublicKeyImpl {
final subtle.JSCryptoKey _key;
_EcdhPublicKey(this._key);
_EcdhPublicKeyImpl(this._key);

@override
String toString() {
return 'Instance of \'EcdhPublicKey\'';
return 'Instance of \'EcdhPublicKeyImpl\'';
}

@override
Expand Down
Loading
Loading