Skip to content

Commit

Permalink
Fix decryption code
Browse files Browse the repository at this point in the history
  • Loading branch information
fpoirotte committed May 14, 2017
1 parent d0908b8 commit 23f67c3
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,23 @@ protected static function checkSupport()

public function encrypt($iv, $key, $data, &$tag = null, $aad = '')
{
// Depending on the mode, the IV is sometimes called nonce.
$options = array("authdata" => $aad, 'iv' => $iv, 'tag' => null, 'nonce' => $iv);
$blockSize = $this->getBlockSize();
$missing = $blockSize - (strlen($data) % $blockSize);
$data .= $this->padding->getPaddingData($blockSize, $missing);

// Depending on the mode, the IV is sometimes called nonce.
$options = array("authdata" => $aad, 'iv' => $iv, 'tag' => null, 'nonce' => $iv);
$res = tomcrypt_cipher_encrypt($this->cipher, $key, $data, $this->mode, $options);
$tag = $options['tag'];

return $res;
}

public function decrypt($iv, $key, $data, $tag = null, $aad = '')
{
$blockSize = $this->getBlockSize();
$res = @mcrypt_decrypt($this->cipher, $key, $data, $this->mode, $iv);
$padLen = $this->padding->getPaddingSize($res, $blockSize);

// Depending on the mode, the IV is sometimes called nonce.
$options = array("authdata" => $aad, 'iv' => $iv, 'nonce' => $iv, 'tag' => $tag);
$blockSize = $this->getBlockSize();
$res = tomcrypt_cipher_decrypt($this->cipher, $key, $data, $this->mode, $options);

$padLen = $this->padding->getPaddingSize($res, $blockSize);
return $padLen ? (string) substr($res, 0, -$padLen) : $res;
}

Expand Down

0 comments on commit 23f67c3

Please sign in to comment.