Skip to content

Commit

Permalink
Fix calculate number of byte processed of AeadEncryptedData
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 13, 2024
1 parent 245b6c5 commit 30a17e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
11 changes: 4 additions & 7 deletions src/Packet/AeadEncryptedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,9 @@ private static function crypt(
int $chunkSizeByte = 12,
string $iv = ""
): string {
$chunkSize = 1 << $chunkSizeByte + 6;
if ($fn === self::AEAD_DECRYPT) {
$chunkSize += $aead->tagLength();
}
$dataLength = strlen($data);
$tagLength = $fn === self::AEAD_ENCRYPT ? 0 : $aead->tagLength();
$chunkSize = (1 << $chunkSizeByte + 6) + $tagLength;

$aData = substr_replace(
str_repeat(Helper::ZERO_CHAR, 13),
Expand Down Expand Up @@ -268,15 +267,13 @@ private static function crypt(

// For encryption: empty final chunk
// For decryption: final authentication tag
$processed = $dataLength - $tagLength * ceil($dataLength / $chunkSize);
$aDataTag = substr_replace(
str_repeat(Helper::ZERO_CHAR, 21),
$aData,
0,
13
);
$processed = array_sum(
array_map(static fn($bytes) => strlen($bytes), $crypted)
);
$aDataTag = substr_replace($aDataTag, pack("N", $processed), 17, 4);
$crypted[] = $cipher->$fn(
$finalChunk,
Expand Down
18 changes: 10 additions & 8 deletions src/Packet/SymEncryptedIntegrityProtectedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
namespace OpenPGP\Packet;

use OpenPGP\Common\{Config, Helper};
use OpenPGP\Enum\{AeadAlgorithm, HashAlgorithm, PacketTag, SymmetricAlgorithm};
use OpenPGP\Enum\{
AeadAlgorithm,
HashAlgorithm,
PacketTag,
SymmetricAlgorithm
};
use OpenPGP\Type\{
AeadEncryptedDataPacketInterface,
PacketListInterface,
Expand Down Expand Up @@ -325,10 +330,9 @@ private static function aeadCrypt(
int $chunkSizeByte = 12,
string $salt = ""
): string {
$chunkSize = 1 << $chunkSizeByte + 6;
if ($fn === self::AEAD_DECRYPT) {
$chunkSize += $aead->tagLength();
}
$dataLength = strlen($data);
$tagLength = $fn === self::AEAD_ENCRYPT ? 0 : $aead->tagLength();
$chunkSize = (1 << $chunkSizeByte + 6) + $tagLength;

$aData = implode([
chr(
Expand Down Expand Up @@ -378,9 +382,7 @@ private static function aeadCrypt(

// For encryption: empty final chunk
// For decryption: final authentication tag
$processed = array_sum(
array_map(static fn($bytes) => strlen($bytes), $crypted)
);
$processed = $dataLength - $tagLength * ceil($dataLength / $chunkSize);
$aDataTag = implode([$aData, str_repeat(Helper::ZERO_CHAR, 8)]);
$aDataTag = substr_replace(
$aDataTag,
Expand Down

0 comments on commit 30a17e3

Please sign in to comment.