Skip to content

Commit

Permalink
#45
Browse files Browse the repository at this point in the history
Added well named getters for public and private exponents and associated tests.
Added public key exponent field in private key and associated tests.
Updated equality methods, added associated tests.
Updated change log.
  • Loading branch information
mwcw committed Sep 11, 2020
1 parent 167aa6a commit 6576189
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 21 deletions.
21 changes: 17 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
Changelog
=========

#### Version 2.0.0-rc1 (2020-08-05) (Dart SDK version 2.1.1)
#### Version 2.0.0-rc1 (2020-09-11) (Dart SDK version 2.1.1)

* Fixed OAEPEncoding and PKCS1Encoding to use provided output offset value.
* Fixed RSA block length and offset checks in RSAEngine.processBlock.
* Fixed RSASigner.verifySignature to return false when signature is bad.
* Add HKDF support (IETF RFC 5869)
* Add Poly1305, ChaCha20, AES-GCM, SHA3, Keccak, RSA/PSS
* Add Poly1305, ChaCha20, ChaCha7539, AES-GCM, SHA3, Keccak, RSA/PSS
* Add CSHAKE, SHAKE
* Fixed randomly occurring bug with OAEP decoding.
* Added NormalizedECDSASigner that wraps ECDSASigner to guarantee an ecdsa signature in lower-s form. (Enforcement on verification supported).
* Reduce copies in CBC mode.
* Linter issues fixed.
* FixedSecureRandom to use seed only once.


* ASN1 - BOOLEAN, INTEGER, BIT_STRING, OCTET_STRING, NULL, OBJECT_IDENTIFIER,
ENUMERATED, UTF8_STRING, SEQUENCE, SET, PRINTABLE_STRING, IA5_STRING & UTC_TIME
* ASN1 Encoding - DER & BER
* RSA Keys - Private Key carries public key exponent, added publicExponent and privateExponent where necessary
and deprecated single variable getters in for those values.

##### Thanks, Steven
At this release the Point Castle Crypto API has been fully handed over to the
Legion of the Bouncy Castle Inc. Steven Roose, it is no small thing to single headedly
manage a cryptography API and your effort is rightfully respected by the Pointy Castle user
base. We would like to thank you for your trust in us to carry the project forward, and we
wish you all the best!


#### Version 1.0.2 (2019-11-15)

* Add non-Keccak SHA3 support
Expand Down
37 changes: 27 additions & 10 deletions lib/asymmetric/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,32 @@ class RSAPrivateKey extends RSAAsymmetricKey implements PrivateKey {
// The secret prime factors of n
final BigInt p;
final BigInt q;
final BigInt pubExponent;

/// Create an RSA private key for the given parameters.
RSAPrivateKey(BigInt modulus, BigInt exponent, this.p, this.q)
: super(modulus, exponent);
RSAPrivateKey(BigInt modulus, BigInt privateExponent, this.p, this.q,
[BigInt this.pubExponent])
: super(modulus, privateExponent);

/// Get private exponent [d] = e^-1
@Deprecated('Use privateExponent.')
BigInt get d => exponent;

/// Get the private exponent (d)
BigInt get privateExponent => exponent;

/// Get the public exponent (e)
BigInt get publicExponent => pubExponent;

bool operator ==(other) {
if (other == null) return false;
if (other is! RSAPrivateKey) return false;
return (other.n == this.n) && (other.d == this.d);
if (other is RSAPrivateKey) {
return other.privateExponent == this.privateExponent &&
other.modulus == this.modulus;
}
return false;
}

int get hashCode => modulus.hashCode + exponent.hashCode;
int get hashCode => modulus.hashCode + privateExponent.hashCode;
}

/// Public keys in RSA
Expand All @@ -47,15 +58,21 @@ class RSAPublicKey extends RSAAsymmetricKey implements PublicKey {
RSAPublicKey(BigInt modulus, BigInt exponent) : super(modulus, exponent);

/// Get public exponent [e]
@Deprecated('Use get publicExponent')
BigInt get e => exponent;

/// Get the public exponent.
BigInt get publicExponent => exponent;

bool operator ==(other) {
if (other == null) return false;
if (other is! RSAPublicKey) return false;
return (other.n == this.n) && (other.e == this.e);
if (other is RSAPublicKey) {
return (other.modulus == this.modulus) &&
(other.publicExponent == this.publicExponent);
}
return false;
}

int get hashCode => modulus.hashCode + exponent.hashCode;
int get hashCode => modulus.hashCode + publicExponent.hashCode;
}

/// A [Signature] created with RSA.
Expand Down
4 changes: 2 additions & 2 deletions lib/key_generators/rsa_key_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RSAKeyGenerator implements KeyGenerator {
}

AsymmetricKeyPair generateKeyPair() {
var p, q, n, e;
BigInt p, q, n, e;

// p and q values should have a length of half the strength in bits
var strength = _params.bitStrength;
Expand Down Expand Up @@ -118,7 +118,7 @@ class RSAKeyGenerator implements KeyGenerator {
var d = e.modInverse(phi);

return new AsymmetricKeyPair(
new RSAPublicKey(n, e), new RSAPrivateKey(n, d, p, q));
new RSAPublicKey(n, e), new RSAPrivateKey(n, d, p, q, e));
}
}

Expand Down
11 changes: 11 additions & 0 deletions lib/src/api/asymmetric_key_pair.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@ class AsymmetricKeyPair<B extends PublicKey, V extends PrivateKey> {
final V privateKey;

AsymmetricKeyPair(this.publicKey, this.privateKey);

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is AsymmetricKeyPair &&
runtimeType == other.runtimeType &&
publicKey == other.publicKey &&
privateKey == other.privateKey;

@override
int get hashCode => publicKey.hashCode ^ privateKey.hashCode;
}
2 changes: 1 addition & 1 deletion test/asymmetric/oaep_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void rsaOaepStandardTests() {
// Instantiate the RSA key pair objects

final publicKey = RSAPublicKey(n, e);
final privateKey = RSAPrivateKey(n, privateExponent, p, q);
final privateKey = RSAPrivateKey(n, privateExponent, p, q, e);

//----------------

Expand Down
2 changes: 1 addition & 1 deletion test/asymmetric/rsa_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {
// Qinv = 55230685559710427662641419127391743688498562899704993951405017803347590263833235040975158906547337992492501961820323076137293399600978620698955537627140829092176376250413837145798422716237647195575076578845234764700372691872271724982949541941613766324969755382993478116416238708238612702107474463263158193372

var pubk = RSAPublicKey(modulus, publicExponent);
var privk = RSAPrivateKey(modulus, privateExponent, p, q);
var privk = RSAPrivateKey(modulus, privateExponent, p, q, publicExponent);

var pubpar = () => PublicKeyParameter<RSAPublicKey>(pubk);
var privpar = () => PrivateKeyParameter<RSAPrivateKey>(privk);
Expand Down
Loading

0 comments on commit 6576189

Please sign in to comment.