Skip to content
This repository was archived by the owner on Dec 23, 2020. It is now read-only.

Commit 2da75e5

Browse files
committed
Dynamic Registry implementation
The implementation is based on Reflectable, a library that does efficient reflection in Dart. So, instead of registering all implemented algorithms manually, the implementation itself defines how it should be registered. The Registry class will then search through all imported cipher.* libraries to look for implementations to include. This allows users to import api.dart and cherry pick certain implementations and still be able to use the generic constructors. It also makes it easier to add new implementations, adding a new file is all that it takes. This also allows external libraries to add implementations to cipher, just by creating cipher.* libraries and importing them. A side change is that I had to create a separate class for all ec curves. This positive side of this is again that users that only need one curve, don't have to import all curves. Other changes: I moves the CTRStreamCipher and SICStreamCipher modes into the cipher.stream namespace and created CTRBlockCipher and SICBlockCipher using the StreamCipherAsBlockCipher class for completeness.
1 parent b8d5170 commit 2da75e5

File tree

121 files changed

+1901
-1215
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+1901
-1215
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ pubspec.lock
1010
*.iml
1111

1212
# dartdoc
13-
doc/
13+
doc/
14+
15+
.pub/

lib/api.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import "dart:typed_data";
1717

1818
import "package:bignum/bignum.dart";
1919

20-
import "registry/registry.dart";
21-
export "registry/registry.dart";
20+
import "src/registry/registry.dart";
2221

22+
part "src/api/algorithm.dart";
23+
part "src/api/registry_factory_exception.dart";
2324
part "src/api/assymetric_block_cipher.dart";
2425
part "src/api/assymetric_key.dart";
2526
part "src/api/assymetric_key_pair.dart";

lib/asymmetric/pkcs1.dart

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@
55
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
66
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
library cipher.asymmetric.pkcs1;
8+
library cipher.asymmetric_block_cipher.pkcs1;
99

1010
import "dart:typed_data";
1111

1212
import "package:cipher/api.dart";
13+
import "package:cipher/src/registry/registry.dart";
1314
import "package:cipher/src/impl/base_asymmetric_block_cipher.dart";
1415

1516
class PKCS1Encoding extends BaseAsymmetricBlockCipher {
1617

18+
/// Intended for internal use.
19+
static final FactoryConfig FACTORY_CONFIG =
20+
new DynamicFactoryConfig.suffix("/PKCS1", (final String algorithmName, _) => () {
21+
int sep = algorithmName.lastIndexOf("/");
22+
AsymmetricBlockCipher underlyingCipher =
23+
new AsymmetricBlockCipher(algorithmName.substring(0, sep));
24+
return new PKCS1Encoding(underlyingCipher);
25+
});
26+
1727
static const _HEADER_LENGTH = 10;
1828

1929
final AsymmetricBlockCipher _engine;

lib/asymmetric/rsa.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
66
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
library cipher.asymmetric.rsa;
8+
library cipher.asymmetric_block_cipher.rsa;
99

1010
import "dart:typed_data";
1111

1212
import "package:bignum/bignum.dart";
1313

1414
import "package:cipher/api.dart";
15-
import "package:cipher/src/impl/base_asymmetric_block_cipher.dart";
1615
import "package:cipher/asymmetric/api.dart";
16+
import "package:cipher/src/impl/base_asymmetric_block_cipher.dart";
17+
import "package:cipher/src/registry/registry.dart";
1718

1819
class RSAEngine extends BaseAsymmetricBlockCipher {
1920

21+
static final FactoryConfig FACTORY_CONFIG = new StaticFactoryConfig("RSA");
22+
2023
bool _forEncryption;
2124
RSAAsymmetricKey _key;
2225
BigInteger _dP;

lib/block/aes_fast.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
66
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
library cipher.block.aes_fast;
8+
library cipher.block_cipher.aes_fast;
99

1010
import "dart:typed_data";
1111

1212
import "package:cipher/api.dart";
1313
import "package:cipher/src/impl/base_block_cipher.dart";
1414
import "package:cipher/src/ufixnum.dart";
15+
import "package:cipher/src/registry/registry.dart";
1516

1617
/**
1718
* An implementation of the AES (Rijndael), from FIPS-197.
@@ -39,6 +40,8 @@ import "package:cipher/src/ufixnum.dart";
3940
*/
4041
class AESFastEngine extends BaseBlockCipher {
4142

43+
static final FactoryConfig FACTORY_CONFIG = new StaticFactoryConfig("AES");
44+
4245
static const _BLOCK_SIZE = 16;
4346

4447
bool _forEncryption;

lib/modes/cbc.dart lib/block/modes/cbc.dart

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,25 @@
55
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
66
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
library cipher.modes.cbc;
8+
library cipher.block_cipher.modes.cbc;
99

1010
import "dart:typed_data";
1111

1212
import "package:cipher/api.dart";
13+
import "package:cipher/src/registry/registry.dart";
1314
import "package:cipher/src/impl/base_block_cipher.dart";
1415

1516
/// Implementation of Cipher-Block-Chaining (CBC) mode on top of a [BlockCipher].
1617
class CBCBlockCipher extends BaseBlockCipher {
1718

19+
/// Intended for internal use.
20+
static final FactoryConfig FACTORY_CONFIG =
21+
new DynamicFactoryConfig.suffix("/CBC", (final String algorithmName, _) => () {
22+
int sep = algorithmName.lastIndexOf("/");
23+
BlockCipher underlying = new BlockCipher(algorithmName.substring(0, sep));
24+
return new CBCBlockCipher(underlying);
25+
});
26+
1827
final BlockCipher _underlyingCipher;
1928

2029
Uint8List _IV;

lib/modes/cfb.dart lib/block/modes/cfb.dart

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,25 @@
55
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
66
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
library cipher.modes.cfb;
8+
library cipher.block_cipher.modes.cfb;
99

1010
import "dart:typed_data";
1111

1212
import "package:cipher/api.dart";
13+
import "package:cipher/src/registry/registry.dart";
1314
import "package:cipher/src/impl/base_block_cipher.dart";
1415

1516
/// Implementation of Cipher Feedback Mode (CFB) on top of a [BlockCipher].
1617
class CFBBlockCipher extends BaseBlockCipher {
1718

19+
/// Intended for internal use.
20+
static final FactoryConfig FACTORY_CONFIG =
21+
new DynamicFactoryConfig.regex(r"^(.+)/CFB-([0-9]+)$", (_, final Match match) => () {
22+
BlockCipher underlying = new BlockCipher(match.group(1));
23+
int blockSize = int.parse(match.group(2));
24+
return new CFBBlockCipher(underlying, blockSize);
25+
});
26+
1827
final int blockSize;
1928

2029
final BlockCipher _underlyingCipher;

lib/block/modes/ctr.dart

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2013-present, Iván Zaera Avellón - izaera@gmail.com
2+
3+
// This library is dually licensed under LGPL 3 and MPL 2.0. See file LICENSE for more information.
4+
5+
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
6+
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
7+
8+
library cipher.block_cipher.modes.ctr;
9+
10+
import "package:cipher/api.dart";
11+
import "package:cipher/adapters/stream_cipher_as_block_cipher.dart";
12+
import "package:cipher/stream/ctr.dart";
13+
import "package:cipher/src/registry/registry.dart";
14+
15+
class CTRBlockCipher extends StreamCipherAsBlockCipher {
16+
17+
/// Intended for internal use.
18+
static final FactoryConfig FACTORY_CONFIG =
19+
new DynamicFactoryConfig.suffix("/CTR", (final String algorithmName, _) => () {
20+
int sep = algorithmName.lastIndexOf("/");
21+
BlockCipher underlying = new BlockCipher(algorithmName.substring(0, sep));
22+
return new CTRBlockCipher(underlying.blockSize, new CTRStreamCipher(underlying));
23+
});
24+
25+
CTRBlockCipher(int blockSize, StreamCipher underlyingCipher)
26+
: super(blockSize, underlyingCipher);
27+
28+
}

lib/modes/ecb.dart lib/block/modes/ecb.dart

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,25 @@
55
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
66
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
library cipher.modes.ecb;
8+
library cipher.block_cipher.modes.ecb;
99

1010
import "dart:typed_data";
1111

1212
import "package:cipher/api.dart";
13+
import "package:cipher/src/registry/registry.dart";
1314
import "package:cipher/src/impl/base_block_cipher.dart";
1415

1516
/// Implementation of Electronic Code Book (ECB) mode on top of a [BlockCipher].
1617
class ECBBlockCipher extends BaseBlockCipher {
1718

19+
/// Intended for internal use.
20+
static final FactoryConfig FACTORY_CONFIG =
21+
new DynamicFactoryConfig.suffix("/ECB", (final String algorithmName, _) => () {
22+
int sep = algorithmName.lastIndexOf("/");
23+
BlockCipher underlying = new BlockCipher(algorithmName.substring(0, sep));
24+
return new ECBBlockCipher(underlying);
25+
});
26+
1827
final BlockCipher _underlyingCipher;
1928

2029
ECBBlockCipher(this._underlyingCipher);

lib/modes/gctr.dart lib/block/modes/gctr.dart

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@
55
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
66
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
library cipher.modes.gctr;
8+
library cipher.block_cipher.modes.gctr;
99

1010
import "dart:typed_data";
1111

1212
import "package:cipher/api.dart";
13+
import "package:cipher/src/registry/registry.dart";
1314
import "package:cipher/src/impl/base_block_cipher.dart";
1415
import "package:cipher/src/ufixnum.dart";
1516

1617
/// Implementation of GOST 28147 OFB counter mode (GCTR) on top of a [BlockCipher].
1718
class GCTRBlockCipher extends BaseBlockCipher {
1819

20+
/// Intended for internal use.
21+
static final FactoryConfig FACTORY_CONFIG =
22+
new DynamicFactoryConfig.suffix("/GCTR", (final String algorithmName, _) => () {
23+
int sep = algorithmName.lastIndexOf("/");
24+
BlockCipher underlying = new BlockCipher(algorithmName.substring(0, sep));
25+
return new GCTRBlockCipher(underlying);
26+
});
27+
1928
static const C1 = 16843012; //00000001000000010000000100000100
2029
static const C2 = 16843009; //00000001000000010000000100000001
2130

lib/modes/ofb.dart lib/block/modes/ofb.dart

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,25 @@
55
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
66
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
library cipher.modes.ofb;
8+
library cipher.block_cipher.modes.ofb;
99

1010
import "dart:typed_data";
1111

1212
import "package:cipher/api.dart";
13+
import "package:cipher/src/registry/registry.dart";
1314
import "package:cipher/src/impl/base_block_cipher.dart";
1415

1516
/// Implementation of Output FeedBack mode (OFB) on top of a [BlockCipher].
1617
class OFBBlockCipher extends BaseBlockCipher {
1718

19+
/// Intended for internal use.
20+
static final FactoryConfig FACTORY_CONFIG =
21+
new DynamicFactoryConfig.regex(r"^(.+)/OFB-([0-9]+)$", (_, final Match match) => () {
22+
BlockCipher underlying = new BlockCipher(match.group(1));
23+
int blockSize = int.parse(match.group(2));
24+
return new OFBBlockCipher(underlying, blockSize);
25+
});
26+
1827
final int blockSize;
1928

2029
final BlockCipher _underlyingCipher;

lib/block/modes/sic.dart

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2013-present, Iván Zaera Avellón - izaera@gmail.com
2+
3+
// This library is dually licensed under LGPL 3 and MPL 2.0. See file LICENSE for more information.
4+
5+
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
6+
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
7+
8+
library cipher.block_cipher.modes.sic;
9+
10+
import "package:cipher/api.dart";
11+
import "package:cipher/adapters/stream_cipher_as_block_cipher.dart";
12+
import "package:cipher/stream/sic.dart";
13+
import "package:cipher/src/registry/registry.dart";
14+
15+
class SICBlockCipher extends StreamCipherAsBlockCipher {
16+
17+
/// Intended for internal use.
18+
static final FactoryConfig FACTORY_CONFIG =
19+
new DynamicFactoryConfig.suffix("/SIC", (final String algorithmName, _) => () {
20+
int sep = algorithmName.lastIndexOf("/");
21+
BlockCipher underlying = new BlockCipher(algorithmName.substring(0, sep));
22+
return new SICBlockCipher(underlying.blockSize, new SICStreamCipher(underlying));
23+
});
24+
25+
SICBlockCipher(int blockSize, StreamCipher underlyingCipher)
26+
: super(blockSize, underlyingCipher);
27+
28+
}

lib/digests/md2.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
66
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
library cipher.digests.md2;
8+
library cipher.digest.md2;
99

1010
import "dart:typed_data";
1111

1212
import "package:cipher/src/impl/base_digest.dart";
13+
import "package:cipher/src/registry/registry.dart";
1314

1415
/// Implementation of MD2 as outlined in RFC1319 by B.Kaliski from RSA Laboratories April 1992
1516
class MD2Digest extends BaseDigest {
1617

18+
static final FactoryConfig FACTORY_CONFIG = new StaticFactoryConfig("MD2");
19+
//TODO reindent
20+
1721
static const _DIGEST_LENGTH = 16;
1822

1923
/* X buffer */

lib/digests/md4.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
66
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
library cipher.digests.md4;
8+
library cipher.digest.md4;
99

1010
import "dart:typed_data";
1111

1212
import "package:cipher/api.dart";
1313
import "package:cipher/src/ufixnum.dart";
1414
import "package:cipher/src/impl/md4_family_digest.dart";
15+
import "package:cipher/src/registry/registry.dart";
1516

1617
/// Implementation of MD4 digest
1718
class MD4Digest extends MD4FamilyDigest implements Digest {
1819

19-
static const _DIGEST_LENGTH = 16;
20+
static final FactoryConfig FACTORY_CONFIG = new StaticFactoryConfig("MD4");
21+
22+
static const _DIGEST_LENGTH = 16;
2023

2124
MD4Digest() :
2225
super(Endianness.LITTLE_ENDIAN, 4, 16);

lib/digests/md5.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
66
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
library cipher.digests.md5;
8+
library cipher.digest.md5;
99

1010
import "dart:typed_data";
1111

1212
import "package:cipher/api.dart";
1313
import "package:cipher/src/ufixnum.dart";
1414
import "package:cipher/src/impl/md4_family_digest.dart";
15+
import "package:cipher/src/registry/registry.dart";
1516

1617
/// Implementation of MD5 digest
1718
class MD5Digest extends MD4FamilyDigest implements Digest {
1819

20+
static final FactoryConfig FACTORY_CONFIG = new StaticFactoryConfig("MD5");
21+
1922
static const _DIGEST_LENGTH = 16;
2023

2124
MD5Digest() :

lib/digests/ripemd128.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
66
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
library cipher.digests.ripemd128;
8+
library cipher.digest.ripemd128;
99

1010
import "dart:typed_data";
1111

1212
import "package:cipher/api.dart";
1313
import "package:cipher/src/ufixnum.dart";
1414
import "package:cipher/src/impl/md4_family_digest.dart";
15+
import "package:cipher/src/registry/registry.dart";
1516

1617
/// Implementation of RIPEMD-128 digest
1718
class RIPEMD128Digest extends MD4FamilyDigest implements Digest {
1819

20+
static final FactoryConfig FACTORY_CONFIG = new StaticFactoryConfig("RIPEMD-128");
21+
1922
static const _DIGEST_LENGTH = 16;
2023

2124
RIPEMD128Digest() :

0 commit comments

Comments
 (0)