Skip to content

Commit

Permalink
Fix EAX mode on PHP 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
fpoirotte committed Jul 24, 2017
1 parent 6a7e024 commit a44e212
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Cryptal/Modes/EAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ public function encrypt($data, $context)
$H = isset($options['cryptal']['data']) ? (string) $options['cryptal']['data'] : '';
$blockSize = $this->cipher->getBlockSize();
$pad = str_repeat("\x00", $blockSize - 1);
$tN = (clone $this->omac)->update($pad . "\x00" . $this->nonce)->finish(true);
$tH = (clone $this->omac)->update($pad . "\x01" . $H)->finish(true);

$omac = clone $this->omac;
$tN = $omac->update($pad . "\x00" . $this->nonce)->finish(true);
$omac = clone $this->omac;
$tH = $omac->update($pad . "\x01" . $H)->finish(true);

$ctr = new CTR($this->cipher, $tN, $this->taglen);
$C = '';
foreach (str_split($data, $blockSize) as $block) {
$C .= $ctr->encrypt($block, null);
}

$tC = (clone $this->omac)->update($pad . "\x02" . $C)->finish(true);
$omac = clone $this->omac;
$tC = $omac->update($pad . "\x02" . $C)->finish(true);
stream_context_set_option($context, 'cryptal', 'tag', (string) substr($tN ^ $tH ^ $tC, 0, $this->taglen));
return $C;
}
Expand All @@ -56,9 +60,13 @@ public function decrypt($data, $context)
$T = isset($options['cryptal']['tag']) ? (string) $options['cryptal']['tag'] : '';
$blockSize = $this->cipher->getBlockSize();
$pad = str_repeat("\x00", $blockSize - 1);
$tN = (clone $this->omac)->update($pad . "\x00" . $this->nonce)->finish(true);
$tH = (clone $this->omac)->update($pad . "\x01" . $H)->finish(true);
$tC = (clone $this->omac)->update($pad . "\x02" . $data)->finish(true);

$omac = clone $this->omac;
$tN = $omac->update($pad . "\x00" . $this->nonce)->finish(true);
$omac = clone $this->omac;
$tH = $omac->update($pad . "\x01" . $H)->finish(true);
$omac = clone $this->omac;
$tC = $omac->update($pad . "\x02" . $data)->finish(true);
$T2 = (string) substr($tN ^ $tH ^ $tC, 0, $this->taglen);

if ($T2 !== $T) {
Expand Down

0 comments on commit a44e212

Please sign in to comment.