Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Dec 11, 2024
1 parent 82cd02b commit 2bdba30
Show file tree
Hide file tree
Showing 72 changed files with 1,012 additions and 340 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,6 @@

## 1.5.5 (2024-12-6)
- Fix read public key list ignore last index

## 2.0.0 (2024-12-11)
- Release to major version 2
6 changes: 4 additions & 2 deletions lib/src/common/armor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class Armor {
static const endOfLine = '-----\n';

static const splitPattern = r'^-----[^-]+-----$';
static const emptyLinePattern = r'^[ \f\r\t\u00a0\u2000-\u200a\u202f\u205f\u3000]*$';
static const emptyLinePattern =
r'^[ \f\r\t\u00a0\u2000-\u200a\u202f\u205f\u3000]*$';
static const headerPattern = r'^([^\s:]|[^\s:][^:]*[^\s:]): .+$';

static const base64Chunk = 76;
Expand Down Expand Up @@ -105,7 +106,8 @@ class Armor {
final text = textLines.join('\r\n');
final data = base64.decode(dataLines.join().trim());

if ((checksum != _crc24Checksum(data)) && (checksum.isNotEmpty || Config.checksumRequired)) {
if ((checksum != _crc24Checksum(data)) &&
(checksum.isNotEmpty || Config.checksumRequired)) {
throw AssertionError('Ascii armor integrity check failed');
}

Expand Down
3 changes: 2 additions & 1 deletion lib/src/common/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ final class Config {

static SymmetricAlgorithm preferredSymmetric = SymmetricAlgorithm.aes128;

static CompressionAlgorithm preferredCompression = CompressionAlgorithm.uncompressed;
static CompressionAlgorithm preferredCompression =
CompressionAlgorithm.uncompressed;

static AeadAlgorithm preferredAead = AeadAlgorithm.ocb;
}
3 changes: 2 additions & 1 deletion lib/src/common/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ extension BigIntExt on BigInt {

if (sign > 0) {
rawSize = (bitLength + 7) >> 3;
needsPaddingByte = ((this >> (rawSize - 1) * 8) & negativeFlag) == negativeFlag ? 1 : 0;
needsPaddingByte =
((this >> (rawSize - 1) * 8) & negativeFlag) == negativeFlag ? 1 : 0;
} else {
needsPaddingByte = 0;
rawSize = (bitLength + 8) >> 3;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/cryptor/asymmetric/dsa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class DSASigner implements Signer {
@override
DSASignature generateSignature(final Uint8List message) {
if (!_forSigning) {
throw AssertionError('DSA signer not initialised for signature generation');
throw AssertionError(
'DSA signer not initialised for signature generation');
}

final pri = _key as DSAPrivateKey;
Expand Down
13 changes: 9 additions & 4 deletions lib/src/cryptor/asymmetric/elgamal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ class ElGamalEngine implements AsymmetricBlockCipher {

/// Return the maximum size for an input block to this engine.
@override
get inputBlockSize => _forEncryption ? (_bitSize - 1) ~/ 8 : 2 * ((_bitSize + 7) >> 3);
get inputBlockSize =>
_forEncryption ? (_bitSize - 1) ~/ 8 : 2 * ((_bitSize + 7) >> 3);

/// Return the maximum size for an output block to this engine.
@override
get outputBlockSize => _forEncryption ? 2 * ((_bitSize + 7) >> 3) : (_bitSize - 1) ~/ 8;
get outputBlockSize =>
_forEncryption ? 2 * ((_bitSize + 7) >> 3) : (_bitSize - 1) ~/ 8;

@override
process(final Uint8List data) {
Expand Down Expand Up @@ -89,14 +91,17 @@ class ElGamalEngine implements AsymmetricBlockCipher {
final phi = input.sublist(inLength ~/ 2).toBigIntWithSign(1);

final priv = _key as ElGamalPrivateKey;
final m = (gamma.modPow(prime - (BigInt.one + priv.x), prime) * phi) % prime;
final m =
(gamma.modPow(prime - (BigInt.one + priv.x), prime) * phi) % prime;
output.setAll(
outOff,
m.toUnsignedBytes().sublist(0, output.length - outOff),
);
} else {
/// encryption
final block = (inOff != 0 || inLength != input.length) ? input.sublist(0, inLength) : input;
final block = (inOff != 0 || inLength != input.length)
? input.sublist(0, inLength)
: input;
final inp = block.toBigIntWithSign(1);

if (inp > prime) {
Expand Down
45 changes: 36 additions & 9 deletions lib/src/cryptor/math/fp448.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,14 @@ final class Fp448 {
final t7 = y7 + v7;

final f0 = x0 * y0;
final f8 = x7 * y1 + x6 * y2 + x5 * y3 + x4 * y4 + x3 * y5 + x2 * y6 + x1 * y7;
final f8 =
x7 * y1 + x6 * y2 + x5 * y3 + x4 * y4 + x3 * y5 + x2 * y6 + x1 * y7;
final g0 = u0 * v0;
final g8 = u7 * v1 + u6 * v2 + u5 * v3 + u4 * v4 + u3 * v5 + u2 * v6 + u1 * v7;
final g8 =
u7 * v1 + u6 * v2 + u5 * v3 + u4 * v4 + u3 * v5 + u2 * v6 + u1 * v7;
final h0 = s0 * t0;
final h8 = s7 * t1 + s6 * t2 + s5 * t3 + s4 * t4 + s3 * t5 + s2 * t6 + s1 * t7;
final h8 =
s7 * t1 + s6 * t2 + s5 * t3 + s4 * t4 + s3 * t5 + s2 * t6 + s1 * t7;

var c = f0 + g0 + h8 - f8;
var z0 = c & m28;
Expand Down Expand Up @@ -246,11 +249,14 @@ final class Fp448 {
var z13 = d & m28;
d >>>= 28;

final f6 = x6 * y0 + x5 * y1 + x4 * y2 + x3 * y3 + x2 * y4 + x1 * y5 + x0 * y6;
final f6 =
x6 * y0 + x5 * y1 + x4 * y2 + x3 * y3 + x2 * y4 + x1 * y5 + x0 * y6;
final f14 = x7 * y7;
final g6 = u6 * v0 + u5 * v1 + u4 * v2 + u3 * v3 + u2 * v4 + u1 * v5 + u0 * v6;
final g6 =
u6 * v0 + u5 * v1 + u4 * v2 + u3 * v3 + u2 * v4 + u1 * v5 + u0 * v6;
final g14 = u7 * v7;
final h6 = s6 * t0 + s5 * t1 + s4 * t2 + s3 * t3 + s2 * t4 + s1 * t5 + s0 * t6;
final h6 =
s6 * t0 + s5 * t1 + s4 * t2 + s3 * t3 + s2 * t4 + s1 * t5 + s0 * t6;
final h14 = s7 * t7;

c += f6 + g6 + h14 - f14;
Expand All @@ -260,9 +266,30 @@ final class Fp448 {
var z14 = d & m28;
d >>>= 28;

final f7 = x7 * y0 + x6 * y1 + x5 * y2 + x4 * y3 + x3 * y4 + x2 * y5 + x1 * y6 + x0 * y7;
final g7 = u7 * v0 + u6 * v1 + u5 * v2 + u4 * v3 + u3 * v4 + u2 * v5 + u1 * v6 + u0 * v7;
final h7 = s7 * t0 + s6 * t1 + s5 * t2 + s4 * t3 + s3 * t4 + s2 * t5 + s1 * t6 + s0 * t7;
final f7 = x7 * y0 +
x6 * y1 +
x5 * y2 +
x4 * y3 +
x3 * y4 +
x2 * y5 +
x1 * y6 +
x0 * y7;
final g7 = u7 * v0 +
u6 * v1 +
u5 * v2 +
u4 * v3 +
u3 * v4 +
u2 * v5 +
u1 * v6 +
u0 * v7;
final h7 = s7 * t0 +
s6 * t1 +
s5 * t2 +
s4 * t3 +
s3 * t4 +
s2 * t5 +
s1 * t6 +
s0 * t7;

c += f7 + g7;
var z7 = c & m28;
Expand Down
4 changes: 3 additions & 1 deletion lib/src/cryptor/symmetric/blowfish.dart
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ class BlowfishEngine extends BaseEngine {
}

int _f(final int x) {
return ((_s0[(x >> 24) & 0xff] + _s1[(x >> 16) & 0xff]) ^ _s2[(x >> 8) & 0xff]) + _s3[x & 0xff];
return ((_s0[(x >> 24) & 0xff] + _s1[(x >> 16) & 0xff]) ^
_s2[(x >> 8) & 0xff]) +
_s3[x & 0xff];
}

/// apply the encryption cycle to each value pair in the table.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/cryptor/symmetric/buffered_cipher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,4 @@ class BufferedCipher {
_bufOff = 0;
_underlyingCipher.reset();
}
}
}
48 changes: 32 additions & 16 deletions lib/src/cryptor/symmetric/camellia.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,14 @@ class CamelliaEngine extends BaseEngine {
List<int> ko,
int outOff,
) {
ko[outOff] = _shiftLeft32(ki[inOff], rot) | _shiftRight32(ki[1 + inOff], 32 - rot);
ko[1 + outOff] = _shiftLeft32(ki[1 + inOff], rot) | _shiftRight32(ki[2 + inOff], 32 - rot);
ko[2 + outOff] = _shiftLeft32(ki[2 + inOff], rot) | _shiftRight32(ki[3 + inOff], 32 - rot);
ko[3 + outOff] = _shiftLeft32(ki[3 + inOff], rot) | _shiftRight32(ki[inOff], 32 - rot);
ko[outOff] =
_shiftLeft32(ki[inOff], rot) | _shiftRight32(ki[1 + inOff], 32 - rot);
ko[1 + outOff] = _shiftLeft32(ki[1 + inOff], rot) |
_shiftRight32(ki[2 + inOff], 32 - rot);
ko[2 + outOff] = _shiftLeft32(ki[2 + inOff], rot) |
_shiftRight32(ki[3 + inOff], 32 - rot);
ko[3 + outOff] =
_shiftLeft32(ki[3 + inOff], rot) | _shiftRight32(ki[inOff], 32 - rot);
ki[inOff] = ko[outOff];
ki[1 + inOff] = ko[1 + outOff];
ki[2 + inOff] = ko[2 + outOff];
Expand All @@ -295,10 +299,14 @@ class CamelliaEngine extends BaseEngine {
List<int> ko,
int outOff,
) {
ko[2 + outOff] = _shiftLeft32(ki[inOff], rot) | _shiftRight32(ki[1 + inOff], 32 - rot);
ko[3 + outOff] = _shiftLeft32(ki[1 + inOff], rot) | _shiftRight32(ki[2 + inOff], 32 - rot);
ko[outOff] = _shiftLeft32(ki[2 + inOff], rot) | _shiftRight32(ki[3 + inOff], 32 - rot);
ko[1 + outOff] = _shiftLeft32(ki[3 + inOff], rot) | _shiftRight32(ki[inOff], 32 - rot);
ko[2 + outOff] =
_shiftLeft32(ki[inOff], rot) | _shiftRight32(ki[1 + inOff], 32 - rot);
ko[3 + outOff] = _shiftLeft32(ki[1 + inOff], rot) |
_shiftRight32(ki[2 + inOff], 32 - rot);
ko[outOff] = _shiftLeft32(ki[2 + inOff], rot) |
_shiftRight32(ki[3 + inOff], 32 - rot);
ko[1 + outOff] =
_shiftLeft32(ki[3 + inOff], rot) | _shiftRight32(ki[inOff], 32 - rot);
ki[inOff] = ko[2 + outOff];
ki[1 + inOff] = ko[3 + outOff];
ki[2 + inOff] = ko[outOff];
Expand All @@ -312,10 +320,14 @@ class CamelliaEngine extends BaseEngine {
List<int> ko,
int outOff,
) {
ko[outOff] = _shiftLeft32(ki[1 + inOff], rot - 32) | _shiftRight32(ki[2 + inOff], 64 - rot);
ko[1 + outOff] = _shiftLeft32(ki[2 + inOff], rot - 32) | _shiftRight32(ki[3 + inOff], 64 - rot);
ko[2 + outOff] = _shiftLeft32(ki[3 + inOff], rot - 32) | _shiftRight32(ki[inOff], 64 - rot);
ko[3 + outOff] = _shiftLeft32(ki[inOff], rot - 32) | _shiftRight32(ki[1 + inOff], 64 - rot);
ko[outOff] = _shiftLeft32(ki[1 + inOff], rot - 32) |
_shiftRight32(ki[2 + inOff], 64 - rot);
ko[1 + outOff] = _shiftLeft32(ki[2 + inOff], rot - 32) |
_shiftRight32(ki[3 + inOff], 64 - rot);
ko[2 + outOff] = _shiftLeft32(ki[3 + inOff], rot - 32) |
_shiftRight32(ki[inOff], 64 - rot);
ko[3 + outOff] = _shiftLeft32(ki[inOff], rot - 32) |
_shiftRight32(ki[1 + inOff], 64 - rot);
ki[inOff] = ko[outOff];
ki[1 + inOff] = ko[1 + outOff];
ki[2 + inOff] = ko[2 + outOff];
Expand All @@ -329,10 +341,14 @@ class CamelliaEngine extends BaseEngine {
List<int> ko,
int outOff,
) {
ko[2 + outOff] = _shiftLeft32(ki[1 + inOff], rot - 32) | _shiftRight32(ki[2 + inOff], 64 - rot);
ko[3 + outOff] = _shiftLeft32(ki[2 + inOff], rot - 32) | _shiftRight32(ki[3 + inOff], 64 - rot);
ko[outOff] = _shiftLeft32(ki[3 + inOff], rot - 32) | _shiftRight32(ki[inOff], 64 - rot);
ko[1 + outOff] = _shiftLeft32(ki[inOff], rot - 32) | _shiftRight32(ki[1 + inOff], 64 - rot);
ko[2 + outOff] = _shiftLeft32(ki[1 + inOff], rot - 32) |
_shiftRight32(ki[2 + inOff], 64 - rot);
ko[3 + outOff] = _shiftLeft32(ki[2 + inOff], rot - 32) |
_shiftRight32(ki[3 + inOff], 64 - rot);
ko[outOff] = _shiftLeft32(ki[3 + inOff], rot - 32) |
_shiftRight32(ki[inOff], 64 - rot);
ko[1 + outOff] = _shiftLeft32(ki[inOff], rot - 32) |
_shiftRight32(ki[1 + inOff], 64 - rot);
ki[inOff] = ko[2 + outOff];
ki[1 + inOff] = ko[3 + outOff];
ki[2 + inOff] = ko[outOff];
Expand Down
Loading

0 comments on commit 2bdba30

Please sign in to comment.