Skip to content

Commit

Permalink
Merge branch '1.1.x' into develop
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Jan 2, 2024
2 parents 2b5890f + e810486 commit 2425122
Show file tree
Hide file tree
Showing 132 changed files with 2,225 additions and 258 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Dart PG (Dart Privacy Guard) - The OpenPGP library in Dart language
===================================================================
Dart PG is an implementation of the OpenPGP standard in Dart language.
It implements [RFC4880](https://www.rfc-editor.org/rfc/rfc4880), [RFC6637](https://www.rfc-editor.org/rfc/rfc6637),
It implements [RFC4880](https://www.rfc-editor.org/rfc/rfc4880), [RFC5581](https://www.rfc-editor.org/rfc/rfc5581), [RFC6637](https://www.rfc-editor.org/rfc/rfc6637),
parts of [RFC4880bis](https://datatracker.ietf.org/doc/html/draft-ietf-openpgp-rfc4880bis).

## Features
* Dart PG allows to encrypt and sign data.
* Support data signing & encryption.
* Support key management: key generation, key reading, key decryption.
* Support public-key algorithms: [RSA](https://en.wikipedia.org/wiki/RSA_(cryptosystem)),
[DSA](https://en.wikipedia.org/wiki/Digital_Signature_Algorithm),
Expand All @@ -16,6 +16,7 @@ parts of [RFC4880bis](https://datatracker.ietf.org/doc/html/draft-ietf-openpgp-r
* Support symmetric ciphers: 3DES, IDEA (for backward compatibility), CAST5, Blowfish, Twofish,
[AES](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard),
[Camellia](https://en.wikipedia.org/wiki/Camellia_(cipher)).
* Support AEAD algorithms: [EAX](https://www.cs.ucdavis.edu/~rogaway/papers/eax.pdf), [OCB](https://tools.ietf.org/html/rfc7253), [GCM](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf).
* Support hash algorithms: MD5, SHA-1, RIPEMD-160, SHA-256, SHA-384, SHA-512, SHA-224.
* Support compression algorithms: ZIP, ZLIB.
* Support [ECC](https://en.wikipedia.org/wiki/Elliptic-curve_cryptography) curves:
Expand Down
10 changes: 8 additions & 2 deletions lib/src/armor/armor.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-present by Nguyen Van Nguyen <[email protected]>. All rights reserved.
// Copyright 2022-present by Dart Privacy Guard project. All rights reserved.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

Expand All @@ -9,8 +9,14 @@ import '../crypto/math/int_ext.dart';
import '../enum/armor_type.dart';
import '../helpers.dart';

/// ASCII Armor class
/// OpenPGP's Radix-64 encoding.
/// It is composed of two parts: a base64
/// encoding of the binary data and a checksum.
/// See https://www.rfc-editor.org/rfc/rfc4880#section-6
/// Author Nguyen Van Nguyen <[email protected]>
class Armor {
static const version = 'Dart PG v1.0.0';
static const version = 'Dart PG v1.2.0';
static const comment = 'Dart Privacy Guard';

static const messageBegin = '-----BEGIN PGP MESSAGE';
Expand Down
33 changes: 33 additions & 0 deletions lib/src/crypto/aead/base.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// Copyright 2023-present by Dart Privacy Guard project. All rights reserved.
/// For the full copyright and license information, please view the LICENSE
/// file that was distributed with this source code.
import 'dart:typed_data';

export 'eax.dart';
export 'ocb.dart';
export 'gcm.dart';

/// AEAD Authenticated-Encryption base class
/// Author Nguyen Van Nguyen <[email protected]>
abstract class Base {
/// Encrypt plaintext input.
Uint8List encrypt(
final Uint8List plaintext,
final Uint8List nonce,
final Uint8List adata,
);

/// Decrypt ciphertext input.
Uint8List decrypt(
final Uint8List ciphertext,
final Uint8List nonce,
final Uint8List adata,
);

/// Get aead nonce
Uint8List getNonce(
final Uint8List iv,
final Uint8List chunkIndex,
);
}
90 changes: 90 additions & 0 deletions lib/src/crypto/aead/eax.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/// Copyright 2023-present by Dart Privacy Guard project. All rights reserved.
/// For the full copyright and license information, please view the LICENSE
/// file that was distributed with this source code.
import 'dart:typed_data';
import 'package:pointycastle/export.dart';

import '../../enum/symmetric_algorithm.dart';

import 'base.dart';

/// EAX Authenticated-Encryption class
/// Author Nguyen Van Nguyen <[email protected]>
class Eax implements Base {
final Uint8List _key;
final SymmetricAlgorithm _symmetric;

Eax(this._key, this._symmetric);

@override
Uint8List encrypt(
final Uint8List plaintext,
final Uint8List nonce,
final Uint8List adata,
) {
final cipher = EAX(
_symmetric.cipherEngine,
)..init(
true,
AEADParameters(
KeyParameter(_key),
_symmetric.blockSize * 8,
nonce,
adata,
),
);

return _process(cipher, plaintext);
}

@override
Uint8List decrypt(
final Uint8List ciphertext,
final Uint8List nonce,
final Uint8List adata,
) {
final cipher = EAX(
_symmetric.cipherEngine,
)..init(
false,
AEADParameters(
KeyParameter(_key),
_symmetric.blockSize * 8,
nonce,
adata,
),
);

return _process(cipher, ciphertext);
}

@override
Uint8List getNonce(
final Uint8List iv,
final Uint8List chunkIndex,
) {
final nonce = iv.sublist(0);

for (var i = 0; i < chunkIndex.length; i++) {
nonce[8 + i] ^= chunkIndex[i];
}

return nonce;
}

static Uint8List _process(final AEADCipher cipher, final Uint8List input) {
final output = Uint8List(
cipher.getOutputSize(input.length),
);
final len = cipher.processBytes(
input,
0,
input.length,
output,
0,
);
final outLen = len + cipher.doFinal(output, len);
return Uint8List.view(output.buffer, 0, outLen);
}
}
73 changes: 73 additions & 0 deletions lib/src/crypto/aead/gcm.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/// Copyright 2023-present by Dart Privacy Guard project. All rights reserved.
/// For the full copyright and license information, please view the LICENSE
/// file that was distributed with this source code.
import 'dart:typed_data';

import 'package:pointycastle/export.dart';

import '../../enum/symmetric_algorithm.dart';
import 'base.dart';

/// GCM Authenticated-Encryption class
/// Author Nguyen Van Nguyen <[email protected]>
class Gcm implements Base {
final Uint8List _key;
final SymmetricAlgorithm _symmetric;

Gcm(this._key, this._symmetric);

@override
Uint8List encrypt(
final Uint8List ciphertext,
final Uint8List nonce,
final Uint8List adata,
) {
final cipher = GCMBlockCipher(
_symmetric.cipherEngine,
)..init(
true,
AEADParameters(
KeyParameter(_key),
_symmetric.blockSize * 8,
nonce,
adata,
),
);
return cipher.process(ciphertext);
}

@override
Uint8List decrypt(
final Uint8List plaintext,
final Uint8List nonce,
final Uint8List adata,
) {
final cipher = GCMBlockCipher(
_symmetric.cipherEngine,
)..init(
false,
AEADParameters(
KeyParameter(_key),
_symmetric.blockSize * 8,
nonce,
adata,
),
);
return cipher.process(plaintext);
}

@override
Uint8List getNonce(
final Uint8List iv,
final Uint8List chunkIndex,
) {
final nonce = iv.sublist(0);

for (var i = 0; i < chunkIndex.length; i++) {
nonce[4 + i] ^= chunkIndex[i];
}

return nonce;
}
}
78 changes: 78 additions & 0 deletions lib/src/crypto/aead/ocb.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/// Copyright 2023-present by Dart Privacy Guard project. All rights reserved.
/// For the full copyright and license information, please view the LICENSE
/// file that was distributed with this source code.
import 'dart:typed_data';

import 'package:pointycastle/api.dart';

import '../../enum/symmetric_algorithm.dart';
import '../modes/ocb_cipher.dart';
import 'base.dart';

/// OCB Authenticated-Encryption class
/// Author Nguyen Van Nguyen <[email protected]>
class Ocb implements Base {
final Uint8List _key;
final SymmetricAlgorithm _symmetric;

Ocb(this._key, this._symmetric);

@override
Uint8List encrypt(
final Uint8List plaintext,
final Uint8List nonce,
final Uint8List adata,
) {
final cipher = OCBCipher(
_symmetric.cipherEngine,
_symmetric.cipherEngine,
)..init(
true,
AEADParameters(
KeyParameter(_key),
_symmetric.blockSize * 8,
nonce,
adata,
),
);

return cipher.process(plaintext);
}

@override
Uint8List decrypt(
final Uint8List ciphertext,
final Uint8List nonce,
final Uint8List adata,
) {
final cipher = OCBCipher(
_symmetric.cipherEngine,
_symmetric.cipherEngine,
)..init(
false,
AEADParameters(
KeyParameter(_key),
_symmetric.blockSize * 8,
nonce,
adata,
),
);

return cipher.process(ciphertext);
}

@override
Uint8List getNonce(
final Uint8List iv,
final Uint8List chunkIndex,
) {
final nonce = iv.sublist(0);

for (var i = 0; i < chunkIndex.length; i++) {
nonce[7 + i] ^= chunkIndex[i];
}

return nonce;
}
}
3 changes: 2 additions & 1 deletion lib/src/crypto/asymmetric/elgamal.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-present by Nguyen Van Nguyen <[email protected]>. All rights reserved.
// Copyright 2022-present by Dart Privacy Guard project. All rights reserved.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

Expand All @@ -12,6 +12,7 @@ import '../math/byte_ext.dart';

/// Asymmetric block cipher using basic ElGamal algorithm.
/// Ported and modified from Bouncy Castle project
/// Author Nguyen Van Nguyen <[email protected]>
class ElGamalEngine implements AsymmetricBlockCipher {
late ElGamalAsymmetricKey? _key;

Expand Down
3 changes: 2 additions & 1 deletion lib/src/crypto/math/big_int.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2022-present by Nguyen Van Nguyen <[email protected]>. All rights reserved.
// Copyright 2022-present by Dart Privacy Guard project. All rights reserved.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

import 'dart:typed_data';

/// Author Nguyen Van Nguyen <[email protected]>
extension BigIntExt on BigInt {
int get byteLength => (bitLength + 7) >> 3;

Expand Down
3 changes: 2 additions & 1 deletion lib/src/crypto/math/byte_ext.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2022-present by Nguyen Van Nguyen <[email protected]>. All rights reserved.
// Copyright 2022-present by Dart Privacy Guard project. All rights reserved.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

import 'dart:typed_data';

/// Author Nguyen Van Nguyen <[email protected]>
extension Uint8ListExt on Uint8List {
int toIn16([
Endian endian = Endian.big,
Expand Down
3 changes: 2 additions & 1 deletion lib/src/crypto/math/int_ext.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright 2022-present by Nguyen Van Nguyen <[email protected]>. All rights reserved.
// Copyright 2022-present by Dart Privacy Guard project. All rights reserved.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

import 'dart:typed_data';

import 'package:fixnum/fixnum.dart';

/// Author Nguyen Van Nguyen <[email protected]>
extension IntExt on int {
Uint8List pack16([Endian endian = Endian.big]) => Uint8List(2)
..buffer.asByteData().setInt16(
Expand Down
Loading

0 comments on commit 2425122

Please sign in to comment.