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 90cdaeb commit 35fb963
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
14 changes: 10 additions & 4 deletions lib/src/packet/secret_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class SecretKeyPacket extends ContainedPacket implements KeyPacket {

final key = await s2k.produceKey(passphrase, symmetric.keySizeInByte);
final cipher = PaddedBlockCipherImpl(
PKCS7Padding(),
Padding('PKCS7'),
symmetric.cfbCipherEngine,
);
cipher.init(
Expand Down Expand Up @@ -255,22 +255,28 @@ class SecretKeyPacket extends ContainedPacket implements KeyPacket {
symmetric.keySizeInByte,
) ??
Uint8List(symmetric.keySizeInByte);
final blockSize = symmetric.blockSize;
final padding = Padding('PKCS7');
final cipher = PaddedBlockCipherImpl(
PKCS7Padding(),
padding,
symmetric.cfbCipherEngine,
);
cipher.init(
false,
PaddedBlockCipherParameters(
ParametersWithIV(
KeyParameter(key),
iv ?? Uint8List(symmetric.blockSize),
iv ?? Uint8List(blockSize),
),
null,
),
);

final clearTextWithHash = cipher.process(keyData);
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);
clearText = clearTextWithHash.sublist(
0,
clearTextWithHash.length - HashAlgorithm.sha1.digestSize,
Expand Down
13 changes: 10 additions & 3 deletions lib/src/packet/sym_encrypted_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SymEncryptedDataPacket extends ContainedPacket {
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
}) async {
final cipher = PaddedBlockCipherImpl(
PKCS7Padding(),
Padding('PKCS7'),
symmetric.cfbCipherEngine,
);
cipher.init(
Expand Down Expand Up @@ -101,8 +101,9 @@ class SymEncryptedDataPacket extends ContainedPacket {
throw StateError('Message is not authenticated.');
}
final blockSize = symmetric.blockSize;
final padding = Padding('PKCS7');
final cipher = PaddedBlockCipherImpl(
PKCS7Padding(),
padding,
symmetric.cfbCipherEngine,
);
cipher.init(
Expand All @@ -115,10 +116,16 @@ class SymEncryptedDataPacket extends ContainedPacket {
null,
),
);

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(encrypted.sublist(blockSize + 2)),
cipher.process(padded),
),
);
}
Expand Down
14 changes: 10 additions & 4 deletions lib/src/packet/sym_encrypted_integrity_protected_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class SymEncryptedIntegrityProtectedDataPacket extends ContainedPacket {
]);

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

final decrypted = cipher.process(encrypted);
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 realHash = decrypted.sublist(
decrypted.length - HashAlgorithm.sha1.digestSize,
);
Expand Down
17 changes: 12 additions & 5 deletions lib/src/packet/sym_encrypted_session_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket {
if (sessionKey != null) {
if (version == 5) {
final adata = Uint8List.fromList([
0xC0 | PacketTag.aeadEncryptedData.value,
0xc0 | PacketTag.symEncryptedSessionKey.value,
version,
symmetric.value,
aead.value,
Expand All @@ -142,7 +142,7 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket {
encrypted = cipher.encrypt(sessionKey.key, iv, adata);
} else {
final cipher = PaddedBlockCipherImpl(
PKCS7Padding(),
Padding('PKCS7'),
symmetric.cfbCipherEngine,
);
cipher.init(
Expand Down Expand Up @@ -196,21 +196,28 @@ 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(
PKCS7Padding(),
padding,
symmetric.cfbCipherEngine,
);
cipher.init(
false,
PaddedBlockCipherParameters(
ParametersWithIV(
KeyParameter(key),
Uint8List(symmetric.blockSize),
Uint8List(blockSize),
),
null,
),
);
final decrypted = cipher.process(encrypted);

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 sessionKeySymmetric = SymmetricAlgorithm.values.firstWhere(
(algo) => algo.value == decrypted[0],
);
Expand Down

0 comments on commit 35fb963

Please sign in to comment.