-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nguyen Van Nguyen <[email protected]>
- Loading branch information
Showing
132 changed files
with
2,225 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
@@ -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'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
@@ -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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
|
Oops, something went wrong.