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 Oct 21, 2024
1 parent 35fb963 commit 273415c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 42 deletions.
10 changes: 10 additions & 0 deletions lib/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:math';
import 'dart:typed_data';

import 'package:pointycastle/api.dart' as pc;
import 'package:pointycastle/api.dart';

import 'crypto/math/byte_ext.dart';
import 'crypto/math/int_ext.dart';
Expand Down Expand Up @@ -104,4 +105,13 @@ class Helper {
} while (k.compareTo(min) <= 0 || k.compareTo(max) >= 0);
return k;
}

static Uint8List pad(Uint8List bytes, int blockSize) {
final padLength = blockSize - (bytes.length % blockSize);

final padded = Uint8List(bytes.length + padLength)..setAll(0, bytes);
Padding('PKCS7').addPadding(padded, bytes.length);

return padded;
}
}
27 changes: 14 additions & 13 deletions lib/src/packet/secret_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,15 @@ class SecretKeyPacket extends ContainedPacket implements KeyPacket {
);

final clearText = secretParams!.encode();
final cipherText = cipher.process(Uint8List.fromList([
...clearText,
...Helper.hashDigest(clearText, HashAlgorithm.sha1),
]));
final cipherText = cipher.process(
Helper.pad(
Uint8List.fromList([
...clearText,
...Helper.hashDigest(clearText, HashAlgorithm.sha1),
]),
symmetric.blockSize,
),
);

return SecretKeyPacket(
publicKey,
Expand All @@ -255,28 +260,24 @@ class SecretKeyPacket extends ContainedPacket implements KeyPacket {
symmetric.keySizeInByte,
) ??
Uint8List(symmetric.keySizeInByte);
final blockSize = symmetric.blockSize;
final padding = Padding('PKCS7');
final cipher = PaddedBlockCipherImpl(
padding,
Padding('PKCS7'),
symmetric.cfbCipherEngine,
);
cipher.init(
false,
PaddedBlockCipherParameters(
ParametersWithIV(
KeyParameter(key),
iv ?? Uint8List(blockSize),
iv ?? Uint8List(symmetric.blockSize),
),
null,
),
);

final padLength = blockSize - (keyData.length % blockSize);
final padded = Uint8List(keyData.length + padLength)..setAll(0, keyData);
padding.addPadding(padded, keyData.length);

final clearTextWithHash = cipher.process(padded);
final clearTextWithHash = cipher.process(
Helper.pad(keyData, symmetric.blockSize),
);
clearText = clearTextWithHash.sublist(
0,
clearTextWithHash.length - HashAlgorithm.sha1.digestSize,
Expand Down
22 changes: 13 additions & 9 deletions lib/src/packet/sym_encrypted_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ class SymEncryptedDataPacket extends ContainedPacket {
return SymEncryptedDataPacket(
Uint8List.fromList([
...prefix,
...cipher.process(packets.encode()),
...cipher.process(
Helper.pad(
packets.encode(),
symmetric.blockSize,
),
),
]),
packets: packets,
);
Expand Down Expand Up @@ -101,9 +106,8 @@ class SymEncryptedDataPacket extends ContainedPacket {
throw StateError('Message is not authenticated.');
}
final blockSize = symmetric.blockSize;
final padding = Padding('PKCS7');
final cipher = PaddedBlockCipherImpl(
padding,
Padding('PKCS7'),
symmetric.cfbCipherEngine,
);
cipher.init(
Expand All @@ -117,15 +121,15 @@ class SymEncryptedDataPacket extends ContainedPacket {
),
);

final data = encrypted.sublist(blockSize + 2);
final padLength = blockSize - (data.length % blockSize);
final padded = Uint8List(data.length + padLength)..setAll(0, data);
padding.addPadding(padded, data.length);

return SymEncryptedDataPacket(
encrypted,
packets: PacketList.packetDecode(
cipher.process(padded),
cipher.process(
Helper.pad(
encrypted.sublist(blockSize + 2),
blockSize,
),
),
),
);
}
Expand Down
16 changes: 6 additions & 10 deletions lib/src/packet/sym_encrypted_integrity_protected_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class SymEncryptedIntegrityProtectedDataPacket extends ContainedPacket {
);

return SymEncryptedIntegrityProtectedDataPacket(
cipher.process(plainText),
cipher.process(Helper.pad(plainText, symmetric.blockSize)),
packets: packets,
);
}
Expand Down Expand Up @@ -110,28 +110,24 @@ class SymEncryptedIntegrityProtectedDataPacket extends ContainedPacket {
final Uint8List key, {
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
}) async {
final blockSize = symmetric.blockSize;
final padding = Padding('PKCS7');
final cipher = PaddedBlockCipherImpl(
padding,
Padding('PKCS7'),
symmetric.cfbCipherEngine,
);
cipher.init(
false,
PaddedBlockCipherParameters(
ParametersWithIV(
KeyParameter(key),
Uint8List(blockSize),
Uint8List(symmetric.blockSize),
),
null,
),
);

final padLength = blockSize - (encrypted.length % blockSize);
final padded = Uint8List(encrypted.length + padLength)..setAll(0, encrypted);
padding.addPadding(padded, encrypted.length);

final decrypted = cipher.process(padded);
final decrypted = cipher.process(
Helper.pad(encrypted, symmetric.blockSize),
);
final realHash = decrypted.sublist(
decrypted.length - HashAlgorithm.sha1.digestSize,
);
Expand Down
18 changes: 8 additions & 10 deletions lib/src/packet/sym_encrypted_session_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket {
),
);
iv = Uint8List(0);
encrypted = cipher.process(sessionKey.encode());
encrypted = cipher.process(
Helper.pad(sessionKey.encode(), symmetric.blockSize),
);
}
} else {
iv = Uint8List(0);
Expand Down Expand Up @@ -196,28 +198,24 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket {
final decrypted = cipher.decrypt(encrypted, iv, adata);
sessionKey = SessionKey(decrypted, symmetric);
} else {
final blockSize = symmetric.blockSize;
final padding = Padding('PKCS7');
final cipher = PaddedBlockCipherImpl(
padding,
Padding('PKCS7'),
symmetric.cfbCipherEngine,
);
cipher.init(
false,
PaddedBlockCipherParameters(
ParametersWithIV(
KeyParameter(key),
Uint8List(blockSize),
Uint8List(symmetric.blockSize),
),
null,
),
);

final padLength = blockSize - (encrypted.length % blockSize);
final padded = Uint8List(encrypted.length + padLength)..setAll(0, encrypted);
padding.addPadding(padded, encrypted.length);

final decrypted = cipher.process(padded);
final decrypted = cipher.process(
Helper.pad(encrypted, symmetric.blockSize),
);
final sessionKeySymmetric = SymmetricAlgorithm.values.firstWhere(
(algo) => algo.value == decrypted[0],
);
Expand Down

0 comments on commit 273415c

Please sign in to comment.