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

Commit

Permalink
Dynamic Registry implementation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stevenroose committed Jan 24, 2016
1 parent b8d5170 commit 2da75e5
Show file tree
Hide file tree
Showing 121 changed files with 1,901 additions and 1,215 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ pubspec.lock
*.iml

# dartdoc
doc/
doc/

.pub/
5 changes: 3 additions & 2 deletions lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import "dart:typed_data";

import "package:bignum/bignum.dart";

import "registry/registry.dart";
export "registry/registry.dart";
import "src/registry/registry.dart";

part "src/api/algorithm.dart";
part "src/api/registry_factory_exception.dart";
part "src/api/assymetric_block_cipher.dart";
part "src/api/assymetric_key.dart";
part "src/api/assymetric_key_pair.dart";
Expand Down
12 changes: 11 additions & 1 deletion lib/asymmetric/pkcs1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.asymmetric.pkcs1;
library cipher.asymmetric_block_cipher.pkcs1;

import "dart:typed_data";

import "package:cipher/api.dart";
import "package:cipher/src/registry/registry.dart";
import "package:cipher/src/impl/base_asymmetric_block_cipher.dart";

class PKCS1Encoding extends BaseAsymmetricBlockCipher {

/// Intended for internal use.
static final FactoryConfig FACTORY_CONFIG =
new DynamicFactoryConfig.suffix("/PKCS1", (final String algorithmName, _) => () {
int sep = algorithmName.lastIndexOf("/");
AsymmetricBlockCipher underlyingCipher =
new AsymmetricBlockCipher(algorithmName.substring(0, sep));
return new PKCS1Encoding(underlyingCipher);
});

static const _HEADER_LENGTH = 10;

final AsymmetricBlockCipher _engine;
Expand Down
7 changes: 5 additions & 2 deletions lib/asymmetric/rsa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.asymmetric.rsa;
library cipher.asymmetric_block_cipher.rsa;

import "dart:typed_data";

import "package:bignum/bignum.dart";

import "package:cipher/api.dart";
import "package:cipher/src/impl/base_asymmetric_block_cipher.dart";
import "package:cipher/asymmetric/api.dart";
import "package:cipher/src/impl/base_asymmetric_block_cipher.dart";
import "package:cipher/src/registry/registry.dart";

class RSAEngine extends BaseAsymmetricBlockCipher {

static final FactoryConfig FACTORY_CONFIG = new StaticFactoryConfig("RSA");

bool _forEncryption;
RSAAsymmetricKey _key;
BigInteger _dP;
Expand Down
5 changes: 4 additions & 1 deletion lib/block/aes_fast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.block.aes_fast;
library cipher.block_cipher.aes_fast;

import "dart:typed_data";

import "package:cipher/api.dart";
import "package:cipher/src/impl/base_block_cipher.dart";
import "package:cipher/src/ufixnum.dart";
import "package:cipher/src/registry/registry.dart";

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

static final FactoryConfig FACTORY_CONFIG = new StaticFactoryConfig("AES");

static const _BLOCK_SIZE = 16;

bool _forEncryption;
Expand Down
11 changes: 10 additions & 1 deletion lib/modes/cbc.dart → lib/block/modes/cbc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.modes.cbc;
library cipher.block_cipher.modes.cbc;

import "dart:typed_data";

import "package:cipher/api.dart";
import "package:cipher/src/registry/registry.dart";
import "package:cipher/src/impl/base_block_cipher.dart";

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

/// Intended for internal use.
static final FactoryConfig FACTORY_CONFIG =
new DynamicFactoryConfig.suffix("/CBC", (final String algorithmName, _) => () {
int sep = algorithmName.lastIndexOf("/");
BlockCipher underlying = new BlockCipher(algorithmName.substring(0, sep));
return new CBCBlockCipher(underlying);
});

final BlockCipher _underlyingCipher;

Uint8List _IV;
Expand Down
11 changes: 10 additions & 1 deletion lib/modes/cfb.dart → lib/block/modes/cfb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.modes.cfb;
library cipher.block_cipher.modes.cfb;

import "dart:typed_data";

import "package:cipher/api.dart";
import "package:cipher/src/registry/registry.dart";
import "package:cipher/src/impl/base_block_cipher.dart";

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

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

final int blockSize;

final BlockCipher _underlyingCipher;
Expand Down
28 changes: 28 additions & 0 deletions lib/block/modes/ctr.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2013-present, Iván Zaera Avellón - izaera@gmail.com

// This library is dually licensed under LGPL 3 and MPL 2.0. See file LICENSE for more information.

// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.block_cipher.modes.ctr;

import "package:cipher/api.dart";
import "package:cipher/adapters/stream_cipher_as_block_cipher.dart";
import "package:cipher/stream/ctr.dart";
import "package:cipher/src/registry/registry.dart";

class CTRBlockCipher extends StreamCipherAsBlockCipher {

/// Intended for internal use.
static final FactoryConfig FACTORY_CONFIG =
new DynamicFactoryConfig.suffix("/CTR", (final String algorithmName, _) => () {
int sep = algorithmName.lastIndexOf("/");
BlockCipher underlying = new BlockCipher(algorithmName.substring(0, sep));
return new CTRBlockCipher(underlying.blockSize, new CTRStreamCipher(underlying));
});

CTRBlockCipher(int blockSize, StreamCipher underlyingCipher)
: super(blockSize, underlyingCipher);

}
11 changes: 10 additions & 1 deletion lib/modes/ecb.dart → lib/block/modes/ecb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.modes.ecb;
library cipher.block_cipher.modes.ecb;

import "dart:typed_data";

import "package:cipher/api.dart";
import "package:cipher/src/registry/registry.dart";
import "package:cipher/src/impl/base_block_cipher.dart";

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

/// Intended for internal use.
static final FactoryConfig FACTORY_CONFIG =
new DynamicFactoryConfig.suffix("/ECB", (final String algorithmName, _) => () {
int sep = algorithmName.lastIndexOf("/");
BlockCipher underlying = new BlockCipher(algorithmName.substring(0, sep));
return new ECBBlockCipher(underlying);
});

final BlockCipher _underlyingCipher;

ECBBlockCipher(this._underlyingCipher);
Expand Down
11 changes: 10 additions & 1 deletion lib/modes/gctr.dart → lib/block/modes/gctr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.modes.gctr;
library cipher.block_cipher.modes.gctr;

import "dart:typed_data";

import "package:cipher/api.dart";
import "package:cipher/src/registry/registry.dart";
import "package:cipher/src/impl/base_block_cipher.dart";
import "package:cipher/src/ufixnum.dart";

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

/// Intended for internal use.
static final FactoryConfig FACTORY_CONFIG =
new DynamicFactoryConfig.suffix("/GCTR", (final String algorithmName, _) => () {
int sep = algorithmName.lastIndexOf("/");
BlockCipher underlying = new BlockCipher(algorithmName.substring(0, sep));
return new GCTRBlockCipher(underlying);
});

static const C1 = 16843012; //00000001000000010000000100000100
static const C2 = 16843009; //00000001000000010000000100000001

Expand Down
11 changes: 10 additions & 1 deletion lib/modes/ofb.dart → lib/block/modes/ofb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.modes.ofb;
library cipher.block_cipher.modes.ofb;

import "dart:typed_data";

import "package:cipher/api.dart";
import "package:cipher/src/registry/registry.dart";
import "package:cipher/src/impl/base_block_cipher.dart";

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

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

final int blockSize;

final BlockCipher _underlyingCipher;
Expand Down
28 changes: 28 additions & 0 deletions lib/block/modes/sic.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2013-present, Iván Zaera Avellón - izaera@gmail.com

// This library is dually licensed under LGPL 3 and MPL 2.0. See file LICENSE for more information.

// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.block_cipher.modes.sic;

import "package:cipher/api.dart";
import "package:cipher/adapters/stream_cipher_as_block_cipher.dart";
import "package:cipher/stream/sic.dart";
import "package:cipher/src/registry/registry.dart";

class SICBlockCipher extends StreamCipherAsBlockCipher {

/// Intended for internal use.
static final FactoryConfig FACTORY_CONFIG =
new DynamicFactoryConfig.suffix("/SIC", (final String algorithmName, _) => () {
int sep = algorithmName.lastIndexOf("/");
BlockCipher underlying = new BlockCipher(algorithmName.substring(0, sep));
return new SICBlockCipher(underlying.blockSize, new SICStreamCipher(underlying));
});

SICBlockCipher(int blockSize, StreamCipher underlyingCipher)
: super(blockSize, underlyingCipher);

}
6 changes: 5 additions & 1 deletion lib/digests/md2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.digests.md2;
library cipher.digest.md2;

import "dart:typed_data";

import "package:cipher/src/impl/base_digest.dart";
import "package:cipher/src/registry/registry.dart";

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

static final FactoryConfig FACTORY_CONFIG = new StaticFactoryConfig("MD2");
//TODO reindent

static const _DIGEST_LENGTH = 16;

/* X buffer */
Expand Down
7 changes: 5 additions & 2 deletions lib/digests/md4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.digests.md4;
library cipher.digest.md4;

import "dart:typed_data";

import "package:cipher/api.dart";
import "package:cipher/src/ufixnum.dart";
import "package:cipher/src/impl/md4_family_digest.dart";
import "package:cipher/src/registry/registry.dart";

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

static const _DIGEST_LENGTH = 16;
static final FactoryConfig FACTORY_CONFIG = new StaticFactoryConfig("MD4");

static const _DIGEST_LENGTH = 16;

MD4Digest() :
super(Endianness.LITTLE_ENDIAN, 4, 16);
Expand Down
5 changes: 4 additions & 1 deletion lib/digests/md5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.digests.md5;
library cipher.digest.md5;

import "dart:typed_data";

import "package:cipher/api.dart";
import "package:cipher/src/ufixnum.dart";
import "package:cipher/src/impl/md4_family_digest.dart";
import "package:cipher/src/registry/registry.dart";

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

static final FactoryConfig FACTORY_CONFIG = new StaticFactoryConfig("MD5");

static const _DIGEST_LENGTH = 16;

MD5Digest() :
Expand Down
5 changes: 4 additions & 1 deletion lib/digests/ripemd128.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.

library cipher.digests.ripemd128;
library cipher.digest.ripemd128;

import "dart:typed_data";

import "package:cipher/api.dart";
import "package:cipher/src/ufixnum.dart";
import "package:cipher/src/impl/md4_family_digest.dart";
import "package:cipher/src/registry/registry.dart";

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

static final FactoryConfig FACTORY_CONFIG = new StaticFactoryConfig("RIPEMD-128");

static const _DIGEST_LENGTH = 16;

RIPEMD128Digest() :
Expand Down
Loading

0 comments on commit 2da75e5

Please sign in to comment.