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

Add OAEP encoding that uses SHA-256 #98

Closed
wants to merge 6 commits into from
Closed
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
28 changes: 23 additions & 5 deletions lib/asymmetric/oaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import 'package:pointycastle/src/registry/registry.dart';
import 'package:pointycastle/src/impl/base_asymmetric_block_cipher.dart';
import 'package:pointycastle/random/fortuna_random.dart';
import 'package:pointycastle/digests/sha1.dart';
import 'package:pointycastle/digests/sha256.dart';

typedef DigestFactory = Digest Function();

/// RSAES-OAEP v2.0
///
Expand All @@ -27,7 +30,7 @@ import 'package:pointycastle/digests/sha1.dart';
///
/// Currently, this implementation has the following restrictions:
///
/// - the hash function is hard-coded to be SHA-1;
/// - the hash function is hard-coded to be SHA-1 or SHA-256;
/// - the mask generation function is hard-coded to MGF1; and
/// - it cannot accept any _encoding parameters_ (that is, _P_ is always empty)

Expand All @@ -42,7 +45,7 @@ class OAEPEncoding extends BaseAsymmetricBlockCipher {
});

/// Hash function used by the EME-OAEP (Encoding Method for Encryption OAEP).
Digest hash = SHA1Digest();
Digest hash;

/// Hash function used by the MGF1 Mask Generation Function.
late Digest mgf1Hash;
Expand All @@ -51,16 +54,31 @@ class OAEPEncoding extends BaseAsymmetricBlockCipher {
///
/// Note: in this implementation the encoding parameters is always zero
/// octets. There is no mechanism to provide encoding parameters.
Uint8List defHash = Uint8List(SHA1Digest().digestSize);
Uint8List defHash;

final AsymmetricBlockCipher _engine;
late SecureRandom _random;
late bool _forEncryption;

OAEPEncoding(this._engine) {
SHA1Digest().doFinal(defHash, 0);
OAEPEncoding._(DigestFactory digestFactory, this._engine)
: hash = digestFactory(),
defHash = Uint8List(digestFactory().digestSize) {
digestFactory().doFinal(defHash, 0);
}

factory OAEPEncoding(AsymmetricBlockCipher engine) =>
OAEPEncoding.withSHA1(engine);

factory OAEPEncoding.withSHA1(AsymmetricBlockCipher engine) =>
OAEPEncoding._(() => SHA1Digest(), engine);

factory OAEPEncoding.withSHA256(AsymmetricBlockCipher engine) =>
OAEPEncoding._(() => SHA256Digest(), engine);

factory OAEPEncoding.withCustomDigest(
DigestFactory digestFactory, AsymmetricBlockCipher engine) =>
OAEPEncoding._(digestFactory, engine);

@override
String get algorithmName => '${_engine.algorithmName}/OAEP';

Expand Down