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 5c1b76e commit 950a204
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions lib/src/packet/aead_encrypted_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,17 @@ class AeadEncryptedData extends ContainedPacket {
version,
symmetric.value,
aead.value,
chunkSize,
chunkSizeByte,
]),
);

final List<Uint8List> crypted = List.empty(growable: true);
final processed = dataLength - tagLength * (dataLength / chunkSize).ceil();
final crypted = Uint8List(processed);
for (var chunkIndex = 0; chunkIndex == 0 || data.isNotEmpty;) {
final chunkIndexData = adataBuffer.sublist(5, 13);
final size = chunkSize < data.length ? chunkSize : data.length;
crypted.add(
crypted.setAll(
chunkIndex * size,
forEncryption
? cipher.encrypt(
data.sublist(0, size),
Expand All @@ -210,7 +212,6 @@ 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 @@ -219,28 +220,24 @@ class AeadEncryptedData extends ContainedPacket {
final adataTagBuffer = Uint8List(21);
adataTagBuffer.setAll(0, adataBuffer);
adataTagBuffer.setAll(17, processed.pack32());
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,
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,
),
adataTagBuffer,
);

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

0 comments on commit 950a204

Please sign in to comment.