p-hatan/ mirror

pub repository

mirror.p-hatan.ir

pointycastle

2.0.0

A Dart library implementing cryptographic algorithms and primitives, modeled on the BouncyCastle library.

Download archive

Readme

Pointy Castle

Build Status

A Dart library for encryption and decryption. In this release, most of the classes are ports of Bouncy Castle from Java to Dart. The porting is almost always direct except for some classes that had been added to ease the use of low level data.

To make sure nothing fails, tests and benchmarks for every algorithm are provided. The expected results are taken from the Bouncy Castle Java version and also from standards, and matched against the results got from Pointy Castle.

This library was adopted from the original project at https://github.com/PointyCastle/pointycastle at the request of the authors to help support ongoing development. A list of major contributors is provided at contributors.md

Algorithms

In this release, the following algorithms are implemented.

(Most of the below can be used directly with the registry, which is an easy way to instantiate classes in PointyCastle. See "Using the Registry" for more).

(The relevant parameter type is provided for all the algorithms. To initialize an algorithm, call algorithm.init(paramsHere).)

AEAD ciphers: To use with the registry, instantiate like this AEADCipher('ChaCha20-Poly1305'). Ciphers use AEADParameters to initialize.

  • 'ChaCha20-Poly1305'

Block ciphers: To use with the registry, instantiate like this PaddedBlockCipher('AES/SomeBlockModeHere/SomePaddingHere') or like this StreamCipher('AES/SomeStreamModeHere').

  • 'AES'
  • Note that block ciphers can be used in stream cipher modes of operation

Block modes of operation: Most modes use ParametersWithIV to initialize. ECB uses KeyParameter and GCM uses AEADParameters.

  • 'CBC' (Cipher Block Chaining mode)
  • 'ECB' (Electronic Code Book mode)
  • 'CFB-64' (Cipher Feedback mode, using blocks)
  • 'GCTR' (GOST 28147 OFB counter mode, using blocks)
  • 'OFB-64' (Output FeedBack mode, using blocks)
  • 'CTR'/'SIC' (Counter mode, using blocks)
  • Authenticated block modes of operation
    • 'GCM' (Galois-Counter mode)

Stream modes of operation: All modes use ParametersWithIV to initialize.

  • 'CTR'/'SIC' (Counter mode, as a traditional stream)

Paddings:

  • 'PKCS7'
  • 'ISO7816-4'

Asymmetric block ciphers: Instantiate using the registry like this AsymmetricBlockCipher('RSA/SomeEncodingHere'). Initialization requires a RSAPrivateKey or RSAPublicKey.

  • 'RSA'

Asymmetric block cipher encodings:

  • 'PKCS1'
  • 'OAEP'

Stream ciphers: Instantiation using registry is like this StreamCipher('ChaCha20/20'). Initialization requires a ParametersWithIV.

  • 'Salsa20'
  • 'ChaCha20/(# of rounds)' (original implementation)
  • 'ChaCha7539/(# of rounds)' (RFC-7539 implementation)
  • If you don't know how many ChaCha rounds to use, use 20.

Digests: Instantiate using registry like this Digest('Keccak/384'). No initialization is necessary.

  • 'Blake2b'
  • 'MD2'
  • 'MD4'
  • 'MD5'
  • 'RIPEMD-128|160|256|320'
  • 'SHA-1'
  • 'SHA-224|256|384|512'
  • 'SHA-512/t' (t=8 to 376 and 392 to 504 in multiples of 8)
  • 'Keccak/224|256|384|512'
  • 'SHA-3/224|256|384|512'
  • 'Tiger'
  • 'Whirlpool'

MACs: Instantiate using registry like this Mac('SomeBlockCipher/CMAC') or Mac('SomeDigest/HMAC) or Mac(SomeBlockCipher/Poly1305). CMAC and HMAC require a KeyParameter and Poly1305 requires a ParametersWithIV.

  • 'HMAC'
  • 'CMAC'
  • 'Poly1305'

Signatures: Instantiate using registry like this Signer('SomeDigestHere/(DET-)ECDSA') or Signer('SomeDigestHere/RSA')

  • '(DET-)ECDSA'
  • 'RSA'

Password based key derivators: Instantiation using registry like this KeyDerivator('SomeDigestHere/HMAC/PBKDF2') or KeyDerivator('scrypt'). To initialize, you'll need a Pbkdf2Parameters or ScryptParameters.

  • 'PBKDF2'
  • 'scrypt'

HMAC based key derivators: Instantiate using registry like this KeyDerivator('SomeDigestHere/HKDF'). To initialize, use an HkdfParameters.

  • 'HKDF'

Asymmetric key generators Instantiate using registry like this KeyDerivator('RSA'). To initialize, use ECKeyGeneratorParameters or RSAKeyGeneratorParameters.

  • 'ECDSA'
  • 'RSA'

Secure PRNGs:

  • Based on block cipher in CTR mode
  • Based on block cipher in CTR mode with auto reseed (for forward security)
  • Based on Fortuna algorithm

Instantiating implementation objects

There are two ways to instantiate objects that implement the algorithms:

  • using the registry, or
  • without the registry.

Using the registry

Using the registry, the algorithm name is provided to high-level class factories.

This is especially convenient when an algorithm involves multiple algorithm implementation classes to implement. All the necessary classes can all be instantiated with a single name (e.g. "SHA-256/HMAC" or "SHA-1/HMAC/PBKDF2" or "AES/CBC/PKCS7"), and they are automatically combined together with the correct values.

For example,

final sha256 = Digest("SHA-256");
final sha1 = Digest("SHA-1");
final md5 = Digest("MD5");

final hmacSha256 = Mac("SHA-256/HMAC");
final hmacSha1 = Mac("SHA-1/HMAC");
final hmacMd5 = Mac("MD5/HMAC");

final derivator = KeyDerivator("SHA-1/HMAC/PBKDF2");

final signer = Signer("SHA-256/RSA");

Without the registry

Without the registry, each implementation class must be instantiated using its constructor.

If an algorithm involves multiple algorithm implementation classes, they each have to be individually instantiated and combined together with the correct values.

For example,

final sha256 = SHA256Digest();
final sha1 = SHA1Digest();
final md5 = MD5Digest();

final hmacSha256 = HMac(SHA256Digest(), 64);
final hmacSha512 = HMac(SHA512Digest(), 128);
final hmacMd5 = HMac(MD5Digest(), 64);

final derivator = PBKDF2KeyDerivator(HMac(SHA256Digest(), 64));

final signer = RSASigner(SHA256Digest(), '0609608648016503040201');

Registry vs without registry

Using the registry means that all algorithms will be imported by default, which can increase the compiled size of your program.

To avoid this, instantiate all classes directly by using the constructors. But which classes can be instantiated with its constructor will depend on which libraries have been imported.

Importing libraries

A program can take one of these three approaches for importing Point Castle libraries:

  • only import pointycastle.dart;
  • only import exports.dart; or
  • import api.dart and individual libraries as needed.

Only import pointycastle.dart

The "pointycastle.dart" file exports:

  • the high-level API; and
  • implementations of the interfaces.

But it does not export any of the algorithm implementation classes.

import "package:pointycastle/pointycastle.dart";

With this import, none of the implementation classes can be instantiated directly. The program can only use the registry.

For example,

final sha256 = Digest("SHA-256");
// final md5 = MD5Digest(); // not available
final p = Padding("PKCS7");
// final s = FortunaRandom(); // not available

Only import exports.dart

The "export.dart" file exports:

  • the high-level API,
  • implementations of the interfaces; and
  • every algorithm implementation class.

That is, everything!

import "package:pointycastle/export.dart";

With this import, all of the implementation classes can be instantiated directly. The program can also use the registry.

For example, this works without any additional imports:

final sha256 = Digest("SHA-256");
final md5 = MD5Digest();
final p = Padding("PKCS7");
final s = FortunaRandom();

Import api.dart and individual libraries

The "api.dart" exports only:

  • the high-level API.

It does not include the implementations of the interfaces, nor any algorithm implementation class.

import "package:pointycastle/api.dart";
// additional imports will be needed

With this import, only some of the implementation classes can be instantiated directly (i.e. those that are also explicitly imported). The program can also use the registry.

For example, the following only works because of the additional imports:

// In addition to "package:pointycastle/api.dart":
import "package:pointycastle/digests/sha256.dart";
import "package:pointycastle/digests/md5.dart"
import 'package:pointycastle/paddings/pkcs7.dart';

final sha256 = Digest("SHA-256");
final md5 = MD5Digest();
final p = Padding("PKCS7");
// final s = FortunaRandom(); // not available without 'package:pointycastle/random/fortuna_random.dart'

Tutorials

Some articles on how to use some of Pointy Castle's features can be found under the tutorials directory in the sources.

  • Calculating a digest - calculating a hash or digest (e.g. SHA-256, SHA-1, MD5)
  • Calculating a HMAC - calculating a hash-based message authentication code (e.g. HMAC-SHA256, HMAC-SHA1)
  • Using AES-CBC - block encryption and decryption with AES-CBC
  • Using RSA - key generation, signing/verifying, and encryption/decryption
  • Some tips on using Pointy Castle

Note: the above links are to the most recent versions on the master branch on GitHub. They may be different from the version on pub.dev.

Changelog

Changelog

Version 2.0.0 (2020-10-02)

  • No changes from 2.0.0-rc2

Version 2.0.0-rc2 (2020-09-25)

  • Linter Fixes
  • Updates to ASN1 API

Version 2.0.0-rc1 (2020-09-11) (Dart SDK version 2.1.1)::q!

  • 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, 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
  • Add CMAC support ("AES/CMAC")
  • Add ISO7816-4 padding support
  • Fixes in CBCBlockCipherMac and CMac

Version 1.0.1 (2019-02-20)

  • Add Blake2b support

Version 1.0.0 (2018-12-17) (Dart SDK version 2.0)

  • Support Dart 2 and Strong Mode
  • Migrate from package:bignum.BigInteger to dart:core.BigInt
  • Remove Quiver and fixnum dependency
  • OAEP encoding for block ciphers

Version 0.10.0 (2016-02-04) (Dart SDK version 0.14.0)

  • First Pointy Castle release.

  • Reorganised file structure.

  • Completely new Registry implementation that dynamically loads imported implementations using reflection. It is explained in this commit.

  • Migrated from unittest to test package.

cipher releases

Version 0.8.0 (2014-??-??) (Dart SDK version ???)

  • [bug 80] PaddedBlockCipher doesn't add padding when data length is a multiple of the block size. This fix introduces a BREAKING CHANGE in PaddedBlockCipher specification. Read its API documentation to know about the changes.

Version 0.7.0 (2014-03-22) (Dart SDK version 1.3.0-dev.5.2)

  • [enh 15] Implement stream cipher benchmarks.

  • [enh 64] Benchmark and optimize digests.

  • [enh 74] Make SHA-3 usable in terms of speed.

  • [bug 67] Removed some unused code.

  • [bug 68] Fix process() method of PaddedBlockCipher.

  • [bug 75] Remove a registry dependency in the Scrypt algorithm.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add pointycastle
pubspec.yaml
name: pointycastle
version: 2.0.0
author: Legion of the Bouncy Castle Inc.
description: A Dart library implementing cryptographic algorithms and primitives, modeled on the BouncyCastle library.
homepage: https://github.com/bcgit/pc-dart
environment:
  sdk: ">=2.1.1 <3.0.0"
#documentation: http://pointycastle.github.io/docs/

dev_dependencies:
  benchmark_harness: ">=1.0.5 <2.0.0"
  matcher: ">=0.12.0 <0.13.0"
  test: ">=0.12.30 <1.4.0"
  pedantic: ^1.9.0

Version history

VersionPublishedArchiveSHA-256
0.10.0-dev2016-01-26T15:47:08.882ZDownload1b2b97198aeed2251b671d495798b7fb64bf4205fe4e34ce2b9445b553ad04dc
0.10.02016-02-03T23:34:47.717ZDownload4cb6b300ba8e3531d1578977feebe343b249f0de7e078ea4c37c46cd6d8e095a
0.10.0+12016-02-12T14:22:16.128ZDownloadb43cba511aabb7c4061c3fc32e76e453ef93b823cf1245a16a36fa6a92d0e1bf
0.10.0+22016-12-07T23:15:11.612ZDownload9735b0d79142d653f6a23063d450a7b00ee78688853e0f8336b8608068458e30
0.10.0+32016-12-08T03:11:52.527ZDownloada73600c8146bee453cbb97a403fd07271c150d4734b5d5a2f46812d840be7df1
0.10.0+42016-12-08T03:38:10.100ZDownload694d84dcabee80272e194fca256ff9c0f369f00a472c228711471dffe18e8316
0.10.0+52016-12-08T14:37:25.638ZDownload517b1d0964789842b1cd0fba4036d53fe394517b5f8207fd53293cba653e0288
0.11.02018-01-28T16:53:46.490291ZDownloadb0cf5404db85ca5dfe6c07d30d64bebf2868fc6722e999af755a2e95e9a016ba
0.11.12018-01-28T17:36:36.534841ZDownloadfb90ac5dba503b1c211f361fc299d8066747fcd740b8410c9425a1dc2fd72945
1.0.0-rc12018-05-28T16:28:03.989127ZDownload6171dd1511be99d91e00951a79c512ed5ce4da19d9255f2e6c08dba3011daad7
1.0.0-rc22018-06-21T09:43:47.185855ZDownloade7d2ce935341921c504f5d868e476617cda97b076c5c713aee38f110ea84f7ec
1.0.0-rc32018-09-14T20:09:41.086681ZDownloadc7f2351a141fd0045977ebc4ea234ac527a2edbcda7f9fe2d6bca2bf7060fc8e
1.0.0-rc42018-10-04T15:36:41.436233ZDownload96f41a629286d3a9e6cd715f0a9afa48659f7a47666a62b76a16292d25b584e7
1.0.02018-12-17T15:42:10.534222ZDownloadf30c2cfd43a2c5f49031caeeb6fd3724626ba01fc53e14845c24d0493263488e
1.0.12019-02-20T16:14:41.639259ZDownload3c8bca6b347c1276b515d3df0b89cefb097f6d98bb74858f57aab5ee9305c538
1.0.22019-11-15T16:38:46.136833ZDownload4ed9ae487542d265a99f96bb160f76b5cee725376caa6b365aa0df060d64b2db
2.0.0-rc12020-09-12T05:16:21.495202ZDownload7871a8791a004e46fb72ca2dff54d462afc9097fe4e656ba299dec3cc98f758d
2.0.0-rc22020-09-25T07:44:36.309749ZDownload052009b51281946bc59971bf3e887be791e2a128e181908de569892767ed9488
2.0.02020-10-02T04:18:17.009196ZDownloadab5b4252c713b546d462b638ccdb2c94181229c7474a74edde8292ba10b850ba
2.0.12021-01-16T05:15:08.063360ZDownload8c8d9941ab022153b2abdc40ae882dd3e6e8b482688b9581612d56c5d59fa787
3.0.0-nullsafety.22021-02-05T00:27:05.663280ZDownload666796a2673d20ad06c6b6641994bd54f94d12857c0b93a64e1a42252c2f7e4a
3.0.12021-03-24T03:51:36.476467ZDownloada74ed4039c152dd319514f142768d93ad3973860b240d90160cec727c8155f65
3.1.12021-06-04T00:35:49.869661ZDownload48883722393e27548a4ebbd9cef569622485799da1ab144ab2fe5d7e4242843d
3.1.22021-06-17T22:50:45.737361ZDownloadfa7c2f46543a04be3181ab8133de00c676a8c8f75e78d2668816b65405dd053b
3.1.32021-06-29T00:51:41.878311ZDownload416d025f33ef8d69313e0a14d725de4c9bff016551d3d14d244784ac33659304
3.2.0-rc02021-07-05T08:27:57.999331ZDownloaded0cc2e91e0996a6582e87e73fb13cf8e858346d725049740afe8ca9125ebbf4
3.2.02021-07-29T03:42:19.386424ZDownloadadd6af10b663c4460e03dac2ac3a966c062723847967702f4403098608eedbfb
3.3.02021-08-12T09:43:35.064804ZDownload521144963c6488c74ef7b3ce3c28cbcf29db67d5da791a10e89caeca69a380e7
3.3.12021-08-18T06:22:46.887605ZDownloadbe45a0ba99757eb82a18ab2a92fe756e37e06e5318360d0c57b2941ac83ed8cb
3.3.22021-08-27T03:55:13.977419ZDownloadada035ad3c8fdbdb125127ab87c47d5034a9ae700dccdbf1b60b32af52322e4f
3.3.32021-09-03T07:34:51.828926ZDownload99374bc84c43b3d065d88a845591a8e3a4ead5613b56fdb53f01e602b7ecc1df
3.3.42021-09-07T03:53:26.088801ZDownload60da06ea36aa0c411a71b80a80eaef3435394f50b10258673c0e3affaac6e5b4
3.3.52021-10-27T08:23:33.420974ZDownloadda190ccbf664766046c51cbae3abca61aeef369e4d80d2706989ae434b2e90f7
3.4.0-rc12021-11-03T10:13:16.121400ZDownloadbf7616a3ae4e7efc92c9cfeac170defdfceffa7e2ca342b010a92abb713dc05a
3.4.0-rc22021-11-04T07:33:44.535502ZDownloadcb3307bc6532f11a433569b8658acec2ea9bc826b7f8ccbc171ec4373b41f553
3.4.02021-11-09T23:30:15.505651ZDownloadd0d95ef66e5327394d2dab33cbcb2cde3a9fb275abe75ebfedd0f54392878df7
3.5.02021-12-30T09:51:41.732206ZDownload21cc8c830d228dd84c84a9139394702d766622bacd3ea7a2bdcb1f00b280e9a4
3.5.12022-02-08T10:19:48.695746ZDownloade95353c00588645b7e9dd048e705a94186ef90ed2e2f6d96e1f8319abca02df2
3.5.22022-03-07T06:26:59.810689ZDownloadd3031d8b58b62352176b13a734905ebc53d25498c7a5e161e7f3da581f2eb03f
3.6.02022-04-27T06:39:00.710249ZDownload0edfe20fdabaf4cd97e234f66a509f8dc7dc9c55ee2c226027be7be86de612c3
3.6.12022-07-19T06:37:40.752036ZDownload041b249f5c70e8983ebbaff4fdea252c2af8d4912ba64b649edb17615349afef
3.6.22022-09-09T03:35:11.361114ZDownloaddb7306cf0249f838d1a24af52b5a5887c5bf7f31d8bb4e827d071dc0939ad346
3.7.02023-03-16T20:35:02.229325ZDownload57b6b78df14175658f09c5dfcfc51a46ad9561a3504fe679913dab404d0cc0f2
3.7.12023-03-21T19:43:23.159471ZDownloadae73e842cdd27a3467a71d70cefd9b198538aab4fc7dde1d0e8c78c96225abf0
3.7.22023-03-23T18:38:12.847491ZDownloadc3120a968135aead39699267f4c74bc9a08e4e909e86bc1b0af5bfd78691123c
3.7.32023-04-14T16:02:59.714861ZDownload7c1e5f0d23c9016c5bbd8b1473d0d3fb3fc851b876046039509e18e0c7485f2c
3.7.42024-01-15T20:53:27.969113ZDownload43ac87de6e10afabc85c445745a7b799e04de84cebaa4fd7bf55a5e1e9604d29
3.8.02024-04-05T11:22:16.105522ZDownload70fe966348fe08c34bf929582f1d8247d9d9408130723206472b4687227e4333
3.9.02024-04-19T10:19:15.148969ZDownload79fbafed02cfdbe85ef3fd06c7f4bc2cbcba0177e61b765264853d4253b21744
3.9.12024-05-07T18:22:31.393199ZDownload4be0097fcf3fd3e8449e53730c631200ebc7b88016acecab2b0da2f0149222fe
4.0.02025-02-19T10:56:42.595398ZDownload92aa3841d083cc4b0f4709b5c74fd6409a3e6ba833ffc7dc6a8fee096366acf5

Dependencies

No direct dependencies declared.