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 Nov 18, 2024
1 parent 4273f93 commit 5c1b76e
Showing 1 changed file with 34 additions and 39 deletions.
73 changes: 34 additions & 39 deletions lib/src/packet/aead_encrypted_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ class AeadEncryptedData extends ContainedPacket {
bool forEncryption,
Uint8List key,
Uint8List data, {
Uint8List? finalChunk,
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
AeadAlgorithm aead = AeadAlgorithm.gcm,
final chunkSizeByte = 0,
final Uint8List? iv,
Uint8List? finalChunk,
}) {
final cipher = aead.cipherEngine(key, symmetric);
final dataLength = data.length;
Expand All @@ -178,37 +178,30 @@ class AeadEncryptedData extends ContainedPacket {
final adataBuffer = Uint8List(13);

adataBuffer.setAll(
0,
Uint8List.fromList([
0xc0 | PacketTag.aeadEncryptedData.value,
version,
symmetric.value,
aead.value,
chunkSize,
]));
0,
Uint8List.fromList([
0xc0 | PacketTag.aeadEncryptedData.value,
version,
symmetric.value,
aead.value,
chunkSize,
]),
);

final processed = dataLength - tagLength * (dataLength / chunkSize).ceil();
final crypted = Uint8List(processed);
final List<Uint8List> crypted = List.empty(growable: true);
for (var chunkIndex = 0; chunkIndex == 0 || data.isNotEmpty;) {
final chunkIndexData = adataBuffer.sublist(5, 13);
final size = chunkSize < data.length ? chunkSize : data.length;
crypted.setAll(
chunkIndex * size,
crypted.add(
forEncryption
? cipher.encrypt(
data.sublist(0, size),
cipher.getNonce(
iv ?? Uint8List(aead.ivLength),
chunkIndexData,
),
cipher.getNonce(iv ?? Uint8List(aead.ivLength), chunkIndexData),
adataBuffer,
)
: cipher.decrypt(
data.sublist(0, size),
cipher.getNonce(
iv ?? Uint8List(aead.ivLength),
chunkIndexData,
),
cipher.getNonce(iv ?? Uint8List(aead.ivLength), chunkIndexData),
adataBuffer,
),
);
Expand All @@ -217,6 +210,7 @@ class AeadEncryptedData extends ContainedPacket {
data = data.sublist(size);
adataBuffer.setAll(9, (++chunkIndex).pack32());
}
final processed = dataLength - tagLength * (dataLength / chunkSize).ceil();

/// After the final chunk, we either encrypt a final, empty data
/// chunk to get the final authentication tag or validate that final
Expand All @@ -225,27 +219,28 @@ class AeadEncryptedData extends ContainedPacket {
final adataTagBuffer = Uint8List(21);
adataTagBuffer.setAll(0, adataBuffer);
adataTagBuffer.setAll(17, processed.pack32());
final finalCrypted = forEncryption
? cipher.encrypt(
finalChunk ?? Uint8List(0),
cipher.getNonce(
iv ?? Uint8List(aead.ivLength),
chunkIndexData,
),
adataTagBuffer,
)
: cipher.decrypt(
finalChunk ?? Uint8List(0),
cipher.getNonce(
iv ?? Uint8List(aead.ivLength),
chunkIndexData,
crypted.add(
forEncryption
? cipher.encrypt(
finalChunk ?? Uint8List(0),
cipher.getNonce(
iv ?? Uint8List(aead.ivLength),
chunkIndexData,
),
adataTagBuffer,
)
: cipher.decrypt(
finalChunk ?? Uint8List(0),
cipher.getNonce(
iv ?? Uint8List(aead.ivLength),
chunkIndexData,
),
adataTagBuffer,
),
adataTagBuffer,
);
);

return Uint8List.fromList([
...crypted,
...finalCrypted,
...crypted.expand((element) => element),
]);
}
}

0 comments on commit 5c1b76e

Please sign in to comment.