Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions lib/src/impl_ffi/impl_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'dart:async';
import 'dart:ffi' show Allocator;
import 'dart:typed_data';
import 'dart:convert' show utf8, base64Url;
import 'dart:isolate';
import 'dart:ffi' as ffi;
import 'dart:math' as math;
import 'package:meta/meta.dart';
Expand Down
22 changes: 18 additions & 4 deletions lib/src/impl_ffi/impl_ffi.ec_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,15 @@ Map<String, dynamic> _exportJwkEcPrivateOrPublicKey(
});
}

KeyPair<_EvpPKey, _EvpPKey> _generateEcKeyPair(
Future<KeyPair<_EvpPKey, _EvpPKey>> _generateEcKeyPair(
EllipticCurve curve,
) {
return _Scope.sync((scope) {
) async {
return _Scope.async((scope) async {
final ecPriv = ssl.EC_KEY_new_by_curve_name(_ecCurveToNID(curve));
_checkOp(ecPriv.address != 0, fallback: 'internal failure to use curve');
scope.defer(() => ssl.EC_KEY_free(ecPriv));

_checkOpIsOne(ssl.EC_KEY_generate_key(ecPriv));
_checkOpIsOne(await _EC_generate_key(ecPriv));

final privKey = _EvpPKey();
_checkOpIsOne(ssl.EVP_PKEY_set1_EC_KEY.invoke(privKey, ecPriv));
Expand All @@ -357,3 +357,17 @@ KeyPair<_EvpPKey, _EvpPKey> _generateEcKeyPair(
);
});
}

/// Helper function to run `ssl.EC_KEY_generate_key` in an [Isolate]
/// using [Isolate.run].
///
/// Using this auxiliary function to wrap the call should reduce the risk that
/// unnecessary variables are copied into the closure passed to [Isolate.run].
// ignore: non_constant_identifier_names
Future<int> _EC_generate_key(
ffi.Pointer<EC_KEY> key,
) async =>
await Isolate.run(
() => ssl.EC_KEY_generate_key(key),
debugName: 'EC_KEY_generate_key',
);
2 changes: 1 addition & 1 deletion lib/src/impl_ffi/impl_ffi.ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>>
ecdhPrivateKey_generateKey(
EllipticCurve curve,
) async {
final p = _generateEcKeyPair(curve);
final p = await _generateEcKeyPair(curve);
return (
privateKey: _EcdhPrivateKeyImpl(p.privateKey),
publicKey: _EcdhPublicKeyImpl(p.publicKey),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/impl_ffi/impl_ffi.ecdsa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Future<KeyPair<EcdsaPrivateKeyImpl, EcdsaPublicKeyImpl>>
ecdsaPrivateKey_generateKey(
EllipticCurve curve,
) async {
final p = _generateEcKeyPair(curve);
final p = await _generateEcKeyPair(curve);
return (
privateKey: _EcdsaPrivateKeyImpl(p.privateKey),
publicKey: _EcdsaPublicKeyImpl(p.publicKey),
Expand Down
Loading