Skip to content

Commit

Permalink
fix: migrate rsaoaep keypair to tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
HamdaanAliQuatil committed Oct 18, 2024
1 parent 91a4f9e commit 2991d1e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
14 changes: 9 additions & 5 deletions lib/src/impl_ffi/impl_ffi.rsaoaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ Future<KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl>>
final h = _Hash.fromHash(hash);
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
return createKeyPair(
_RsaOaepPrivateKey(keys.privateKey, h),
_RsaOaepPublicKey(keys.publicKey, h),
_RsaOaepPrivateKeyImpl(keys.privateKey, h),
_RsaOaepPublicKeyImpl(keys.publicKey, h),
);
}

Expand Down Expand Up @@ -182,12 +182,16 @@ final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl
rsaOaepPrivateKey_importJsonWebKey(jwk, hash);

@override
Future<KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl>> generateKey(
Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(
int modulusLength,
BigInt publicExponent,
Hash hash,
) =>
rsaOaepPrivateKey_generateKey(modulusLength, publicExponent, hash);
) async {
final KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl> keyPair =
await rsaOaepPrivateKey_generateKey(modulusLength, publicExponent, hash);

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

final class _RsaOaepPrivateKeyImpl implements RsaOaepPrivateKeyImpl {
Expand Down
2 changes: 1 addition & 1 deletion 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.rsaoaep.dart';

/// A key-pair as returned from key generation.
class KeyPair<S, T> {
Expand Down Expand Up @@ -57,7 +58,6 @@ enum EllipticCurve {
/// [1]: https://bugs.webkit.org/show_bug.cgi?id=216755
p521,
}
part 'impl_interface.rsaoaep.dart';

/// Interface to be provided by platform implementations.
///
Expand Down
2 changes: 1 addition & 1 deletion lib/src/impl_interface/impl_interface.rsaoaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ part of 'impl_interface.dart';
abstract interface class StaticRsaOaepPrivateKeyImpl {
Future<RsaOaepPrivateKeyImpl> importPkcs8Key(List<int> keyData, Hash hash);
Future<RsaOaepPrivateKeyImpl> importJsonWebKey(Map<String, dynamic> jwk, Hash hash);
Future<KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl>> generateKey(int modulusLength, BigInt publicExponent, Hash hash);
Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(int modulusLength, BigInt publicExponent, Hash hash);
}

abstract interface class RsaOaepPrivateKeyImpl {
Expand Down
10 changes: 6 additions & 4 deletions lib/src/impl_js/impl_js.rsaoaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ Future<KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl>>
_usagesEncryptDecrypt,
);
return createKeyPair(
_RsaOaepPrivateKey(pair.privateKey),
_RsaOaepPublicKey(pair.publicKey),
_RsaOaepPrivateKeyImpl(pair.privateKey),
_RsaOaepPublicKeyImpl(pair.publicKey),
);
}

Expand Down Expand Up @@ -115,8 +115,10 @@ final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl
}

@override
Future<KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl>> generateKey(int modulusLength, BigInt publicExponent, Hash hash) {
return rsaOaepPrivateKey_generateKey(modulusLength, publicExponent, hash);
Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(int modulusLength, BigInt publicExponent, Hash hash) async {
final KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl> keyPair = await rsaOaepPrivateKey_generateKey(modulusLength, publicExponent, hash);

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

Expand Down
2 changes: 1 addition & 1 deletion lib/src/impl_stub/impl_stub.rsaoaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl
}

@override
Future<KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl>> generateKey(int modulusLength, BigInt publicExponent, Hash hash) {
Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(int modulusLength, BigInt publicExponent, Hash hash) {
throw UnimplementedError('Not implemented');
}
}
Expand Down
16 changes: 14 additions & 2 deletions lib/src/webcrypto/webcrypto.rsaoaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ final class RsaOaepPrivateKey {

RsaOaepPrivateKey._(this._impl); // keep the constructor private.

factory RsaOaepPrivateKey(RsaOaepPrivateKeyImpl impl) {
return RsaOaepPrivateKey._(impl);
}

/// Import RSAES-OAEP private key in PKCS #8 format.
///
/// Creates an [RsaOaepPrivateKey] from [keyData] given as the DER
Expand Down Expand Up @@ -219,8 +223,12 @@ final class RsaOaepPrivateKey {
BigInt publicExponent,
Hash hash,
) async {
final pair = await webCryptImpl.rsaOaepPrivateKey.generateKey(modulusLength, publicExponent, hash);
return pair as KeyPair<RsaOaepPrivateKey, RsaOaepPublicKey>;
final (privateKeyImpl, publicKeyImpl) = await webCryptImpl.rsaOaepPrivateKey.generateKey(modulusLength, publicExponent, hash);

final privateKey = RsaOaepPrivateKey(privateKeyImpl);
final publicKey = RsaOaepPublicKey(publicKeyImpl);

return createKeyPair(privateKey, publicKey);
}

/// Decrypt [data] encrypted with [RsaOaepPublicKey.encryptBytes] from the
Expand Down Expand Up @@ -348,6 +356,10 @@ final class RsaOaepPublicKey {

RsaOaepPublicKey._(this._impl); // keep the constructor private.

factory RsaOaepPublicKey(RsaOaepPublicKeyImpl impl) {
return RsaOaepPublicKey._(impl);
}

/// Import RSAES-OAEP public key in SPKI format.
///
/// Creates an [RsaOaepPublicKey] from [keyData] given as the DER
Expand Down

0 comments on commit 2991d1e

Please sign in to comment.