Skip to content

Commit 0a4a542

Browse files
Refactor RsaOaepPrivateKey and RsaOaepPublicKey Class (#153)
* refactor: update rsaoaep-private-key rsaoaep-public-key * fix: migrate rsaoaep keypair to tuples * chore: format dart code
1 parent 22d6751 commit 0a4a542

File tree

10 files changed

+288
-90
lines changed

10 files changed

+288
-90
lines changed

lib/src/impl_ffi/impl_ffi.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,10 @@ final class _WebCryptoImpl implements WebCryptoImpl {
8383

8484
@override
8585
final ecdhPublicKey = const _StaticEcdhPublicKeyImpl();
86+
87+
@override
88+
final rsaOaepPrivateKey = const _StaticRsaOaepPrivateKeyImpl();
89+
90+
@override
91+
final rsaOaepPublicKey = const _StaticRsaOaepPublicKeyImpl();
8692
}

lib/src/impl_ffi/impl_ffi.rsaoaep.dart

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ String _rsaOaepJwkAlgFromHash(_Hash hash) {
3333
throw UnsupportedError('hash is not supported');
3434
}
3535

36-
Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importPkcs8Key(
36+
Future<RsaOaepPrivateKeyImpl> rsaOaepPrivateKey_importPkcs8Key(
3737
List<int> keyData,
3838
Hash hash,
3939
) async {
4040
// Get hash first, to avoid a leak of EVP_PKEY if _Hash.fromHash throws
4141
final h = _Hash.fromHash(hash);
42-
return _RsaOaepPrivateKey(_importPkcs8RsaPrivateKey(keyData), h);
42+
return _RsaOaepPrivateKeyImpl(_importPkcs8RsaPrivateKey(keyData), h);
4343
}
4444

45-
Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importJsonWebKey(
45+
Future<RsaOaepPrivateKeyImpl> rsaOaepPrivateKey_importJsonWebKey(
4646
Map<String, dynamic> jwk,
4747
Hash hash,
4848
) async {
4949
// Get hash first, to avoid a leak of EVP_PKEY if _Hash.fromHash throws
5050
final h = _Hash.fromHash(hash);
51-
return _RsaOaepPrivateKey(
51+
return _RsaOaepPrivateKeyImpl(
5252
_importJwkRsaPrivateOrPublicKey(
5353
JsonWebKey.fromJson(jwk),
5454
isPrivateKey: true,
@@ -59,7 +59,7 @@ Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importJsonWebKey(
5959
);
6060
}
6161

62-
Future<KeyPair<RsaOaepPrivateKey, RsaOaepPublicKey>>
62+
Future<KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl>>
6363
rsaOaepPrivateKey_generateKey(
6464
int modulusLength,
6565
BigInt publicExponent,
@@ -69,27 +69,27 @@ Future<KeyPair<RsaOaepPrivateKey, RsaOaepPublicKey>>
6969
final h = _Hash.fromHash(hash);
7070
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
7171
return createKeyPair(
72-
_RsaOaepPrivateKey(keys.privateKey, h),
73-
_RsaOaepPublicKey(keys.publicKey, h),
72+
_RsaOaepPrivateKeyImpl(keys.privateKey, h),
73+
_RsaOaepPublicKeyImpl(keys.publicKey, h),
7474
);
7575
}
7676

77-
Future<RsaOaepPublicKey> rsaOaepPublicKey_importSpkiKey(
77+
Future<RsaOaepPublicKeyImpl> rsaOaepPublicKey_importSpkiKey(
7878
List<int> keyData,
7979
Hash hash,
8080
) async {
8181
// Get hash first, to avoid a leak of EVP_PKEY if _Hash.fromHash throws
8282
final h = _Hash.fromHash(hash);
83-
return _RsaOaepPublicKey(_importSpkiRsaPublicKey(keyData), h);
83+
return _RsaOaepPublicKeyImpl(_importSpkiRsaPublicKey(keyData), h);
8484
}
8585

86-
Future<RsaOaepPublicKey> rsaOaepPublicKey_importJsonWebKey(
86+
Future<RsaOaepPublicKeyImpl> rsaOaepPublicKey_importJsonWebKey(
8787
Map<String, dynamic> jwk,
8888
Hash hash,
8989
) async {
9090
// Get hash first, to avoid a leak of EVP_PKEY if _Hash.fromHash throws
9191
final h = _Hash.fromHash(hash);
92-
return _RsaOaepPublicKey(
92+
return _RsaOaepPublicKeyImpl(
9393
_importJwkRsaPrivateOrPublicKey(
9494
JsonWebKey.fromJson(jwk),
9595
isPrivateKey: false,
@@ -167,11 +167,40 @@ Future<Uint8List> _rsaOaepeEncryptOrDecryptBytes(
167167
});
168168
}
169169

170-
class _RsaOaepPrivateKey implements RsaOaepPrivateKey {
170+
final class _StaticRsaOaepPrivateKeyImpl
171+
implements StaticRsaOaepPrivateKeyImpl {
172+
const _StaticRsaOaepPrivateKeyImpl();
173+
174+
@override
175+
Future<RsaOaepPrivateKeyImpl> importPkcs8Key(List<int> keyData, Hash hash) =>
176+
rsaOaepPrivateKey_importPkcs8Key(keyData, hash);
177+
178+
@override
179+
Future<RsaOaepPrivateKeyImpl> importJsonWebKey(
180+
Map<String, dynamic> jwk,
181+
Hash hash,
182+
) =>
183+
rsaOaepPrivateKey_importJsonWebKey(jwk, hash);
184+
185+
@override
186+
Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(
187+
int modulusLength,
188+
BigInt publicExponent,
189+
Hash hash,
190+
) async {
191+
final KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl> keyPair =
192+
await rsaOaepPrivateKey_generateKey(
193+
modulusLength, publicExponent, hash);
194+
195+
return (keyPair.privateKey, keyPair.publicKey);
196+
}
197+
}
198+
199+
final class _RsaOaepPrivateKeyImpl implements RsaOaepPrivateKeyImpl {
171200
final _EvpPKey _key;
172201
final _Hash _hash;
173202

174-
_RsaOaepPrivateKey(this._key, this._hash);
203+
_RsaOaepPrivateKeyImpl(this._key, this._hash);
175204

176205
@override
177206
String toString() {
@@ -203,11 +232,29 @@ class _RsaOaepPrivateKey implements RsaOaepPrivateKey {
203232
Future<Uint8List> exportPkcs8Key() async => _exportPkcs8Key(_key);
204233
}
205234

206-
class _RsaOaepPublicKey implements RsaOaepPublicKey {
235+
final class _StaticRsaOaepPublicKeyImpl implements StaticRsaOaepPublicKeyImpl {
236+
const _StaticRsaOaepPublicKeyImpl();
237+
238+
@override
239+
Future<RsaOaepPublicKeyImpl> importSpkiKey(
240+
List<int> keyData,
241+
Hash hash,
242+
) =>
243+
rsaOaepPublicKey_importSpkiKey(keyData, hash);
244+
245+
@override
246+
Future<RsaOaepPublicKeyImpl> importJsonWebKey(
247+
Map<String, dynamic> jwk,
248+
Hash hash,
249+
) =>
250+
rsaOaepPublicKey_importJsonWebKey(jwk, hash);
251+
}
252+
253+
final class _RsaOaepPublicKeyImpl implements RsaOaepPublicKeyImpl {
207254
final _EvpPKey _key;
208255
final _Hash _hash;
209256

210-
_RsaOaepPublicKey(this._key, this._hash);
257+
_RsaOaepPublicKeyImpl(this._key, this._hash);
211258

212259
@override
213260
String toString() {

lib/src/impl_interface/impl_interface.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ part 'impl_interface.hmac.dart';
2525
part 'impl_interface.pbkdf2.dart';
2626
part 'impl_interface.aesgcm.dart';
2727
part 'impl_interface.ecdh.dart';
28+
part 'impl_interface.rsaoaep.dart';
2829

2930
/// A key-pair as returned from key generation.
3031
class KeyPair<S, T> {
@@ -82,4 +83,6 @@ abstract interface class WebCryptoImpl {
8283
StaticPbkdf2SecretKeyImpl get pbkdf2SecretKey;
8384
StaticEcdhPrivateKeyImpl get ecdhPrivateKey;
8485
StaticEcdhPublicKeyImpl get ecdhPublicKey;
86+
StaticRsaOaepPrivateKeyImpl get rsaOaepPrivateKey;
87+
StaticRsaOaepPublicKeyImpl get rsaOaepPublicKey;
8588
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
part of 'impl_interface.dart';
16+
17+
abstract interface class StaticRsaOaepPrivateKeyImpl {
18+
Future<RsaOaepPrivateKeyImpl> importPkcs8Key(List<int> keyData, Hash hash);
19+
Future<RsaOaepPrivateKeyImpl> importJsonWebKey(
20+
Map<String, dynamic> jwk, Hash hash);
21+
Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(
22+
int modulusLength, BigInt publicExponent, Hash hash);
23+
}
24+
25+
abstract interface class RsaOaepPrivateKeyImpl {
26+
Future<Uint8List> decryptBytes(List<int> data, {List<int>? label});
27+
Future<Uint8List> exportPkcs8Key();
28+
Future<Map<String, dynamic>> exportJsonWebKey();
29+
}
30+
31+
abstract interface class StaticRsaOaepPublicKeyImpl {
32+
Future<RsaOaepPublicKeyImpl> importSpkiKey(List<int> keyData, Hash hash);
33+
Future<RsaOaepPublicKeyImpl> importJsonWebKey(
34+
Map<String, dynamic> jwk, Hash hash);
35+
}
36+
37+
abstract interface class RsaOaepPublicKeyImpl {
38+
Future<Uint8List> encryptBytes(List<int> data, {List<int>? label});
39+
Future<Uint8List> exportSpkiKey();
40+
Future<Map<String, dynamic>> exportJsonWebKey();
41+
}

lib/src/impl_js/impl_js.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,10 @@ final class _WebCryptoImpl implements WebCryptoImpl {
7070

7171
@override
7272
final ecdhPublicKey = const _StaticEcdhPublicKeyImpl();
73+
74+
@override
75+
final rsaOaepPrivateKey = const _StaticRsaOaepPrivateKeyImpl();
76+
77+
@override
78+
final rsaOaepPublicKey = const _StaticRsaOaepPublicKeyImpl();
7379
}

lib/src/impl_js/impl_js.rsaoaep.dart

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ part of 'impl_js.dart';
1818

1919
const _rsaOaepAlgorithmName = 'RSA-OAEP';
2020

21-
Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importPkcs8Key(
21+
Future<RsaOaepPrivateKeyImpl> rsaOaepPrivateKey_importPkcs8Key(
2222
List<int> keyData,
2323
Hash hash,
2424
) async {
25-
return _RsaOaepPrivateKey(await _importKey(
25+
return _RsaOaepPrivateKeyImpl(await _importKey(
2626
'pkcs8',
2727
keyData,
2828
subtle.Algorithm(
@@ -34,11 +34,11 @@ Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importPkcs8Key(
3434
));
3535
}
3636

37-
Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importJsonWebKey(
37+
Future<RsaOaepPrivateKeyImpl> rsaOaepPrivateKey_importJsonWebKey(
3838
Map<String, dynamic> jwk,
3939
Hash hash,
4040
) async {
41-
return _RsaOaepPrivateKey(await _importJsonWebKey(
41+
return _RsaOaepPrivateKeyImpl(await _importJsonWebKey(
4242
jwk,
4343
subtle.Algorithm(
4444
name: _rsaOaepAlgorithmName,
@@ -49,7 +49,7 @@ Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importJsonWebKey(
4949
));
5050
}
5151

52-
Future<KeyPair<RsaOaepPrivateKey, RsaOaepPublicKey>>
52+
Future<KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl>>
5353
rsaOaepPrivateKey_generateKey(
5454
int modulusLength,
5555
BigInt publicExponent,
@@ -65,16 +65,16 @@ Future<KeyPair<RsaOaepPrivateKey, RsaOaepPublicKey>>
6565
_usagesEncryptDecrypt,
6666
);
6767
return createKeyPair(
68-
_RsaOaepPrivateKey(pair.privateKey),
69-
_RsaOaepPublicKey(pair.publicKey),
68+
_RsaOaepPrivateKeyImpl(pair.privateKey),
69+
_RsaOaepPublicKeyImpl(pair.publicKey),
7070
);
7171
}
7272

73-
Future<RsaOaepPublicKey> rsaOaepPublicKey_importSpkiKey(
73+
Future<RsaOaepPublicKeyImpl> rsaOaepPublicKey_importSpkiKey(
7474
List<int> keyData,
7575
Hash hash,
7676
) async {
77-
return _RsaOaepPublicKey(await _importKey(
77+
return _RsaOaepPublicKeyImpl(await _importKey(
7878
'spki',
7979
keyData,
8080
subtle.Algorithm(
@@ -86,11 +86,11 @@ Future<RsaOaepPublicKey> rsaOaepPublicKey_importSpkiKey(
8686
));
8787
}
8888

89-
Future<RsaOaepPublicKey> rsaOaepPublicKey_importJsonWebKey(
89+
Future<RsaOaepPublicKeyImpl> rsaOaepPublicKey_importJsonWebKey(
9090
Map<String, dynamic> jwk,
9191
Hash hash,
9292
) async {
93-
return _RsaOaepPublicKey(await _importJsonWebKey(
93+
return _RsaOaepPublicKeyImpl(await _importJsonWebKey(
9494
jwk,
9595
subtle.Algorithm(
9696
name: _rsaOaepAlgorithmName,
@@ -101,13 +101,39 @@ Future<RsaOaepPublicKey> rsaOaepPublicKey_importJsonWebKey(
101101
));
102102
}
103103

104-
class _RsaOaepPrivateKey implements RsaOaepPrivateKey {
104+
final class _StaticRsaOaepPrivateKeyImpl
105+
implements StaticRsaOaepPrivateKeyImpl {
106+
const _StaticRsaOaepPrivateKeyImpl();
107+
108+
@override
109+
Future<RsaOaepPrivateKeyImpl> importPkcs8Key(List<int> keyData, Hash hash) {
110+
return rsaOaepPrivateKey_importPkcs8Key(keyData, hash);
111+
}
112+
113+
@override
114+
Future<RsaOaepPrivateKeyImpl> importJsonWebKey(
115+
Map<String, dynamic> jwk, Hash hash) {
116+
return rsaOaepPrivateKey_importJsonWebKey(jwk, hash);
117+
}
118+
119+
@override
120+
Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(
121+
int modulusLength, BigInt publicExponent, Hash hash) async {
122+
final KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl> keyPair =
123+
await rsaOaepPrivateKey_generateKey(
124+
modulusLength, publicExponent, hash);
125+
126+
return (keyPair.privateKey, keyPair.publicKey);
127+
}
128+
}
129+
130+
final class _RsaOaepPrivateKeyImpl implements RsaOaepPrivateKeyImpl {
105131
final subtle.JSCryptoKey _key;
106-
_RsaOaepPrivateKey(this._key);
132+
_RsaOaepPrivateKeyImpl(this._key);
107133

108134
@override
109135
String toString() {
110-
return 'Instance of \'RsaOaepPrivateKey\'';
136+
return 'Instance of \'RsaOaepPrivateKeyImpl\'';
111137
}
112138

113139
@override
@@ -135,13 +161,28 @@ class _RsaOaepPrivateKey implements RsaOaepPrivateKey {
135161
}
136162
}
137163

138-
class _RsaOaepPublicKey implements RsaOaepPublicKey {
164+
final class _StaticRsaOaepPublicKeyImpl implements StaticRsaOaepPublicKeyImpl {
165+
const _StaticRsaOaepPublicKeyImpl();
166+
167+
@override
168+
Future<RsaOaepPublicKeyImpl> importSpkiKey(List<int> keyData, Hash hash) {
169+
return rsaOaepPublicKey_importSpkiKey(keyData, hash);
170+
}
171+
172+
@override
173+
Future<RsaOaepPublicKeyImpl> importJsonWebKey(
174+
Map<String, dynamic> jwk, Hash hash) {
175+
return rsaOaepPublicKey_importJsonWebKey(jwk, hash);
176+
}
177+
}
178+
179+
final class _RsaOaepPublicKeyImpl implements RsaOaepPublicKeyImpl {
139180
final subtle.JSCryptoKey _key;
140-
_RsaOaepPublicKey(this._key);
181+
_RsaOaepPublicKeyImpl(this._key);
141182

142183
@override
143184
String toString() {
144-
return 'Instance of \'RsaOaepPublicKey\'';
185+
return 'Instance of \'RsaOaepPublicKeyImpl\'';
145186
}
146187

147188
@override

0 commit comments

Comments
 (0)