cryptal
latest
Cryptography Abstraction Layer
Main Page
Classes
Files
File List
CTR.php
1
<?php
2
3
namespace
fpoirotte\Cryptal\Modes
;
4
5
use
fpoirotte\Cryptal\Implementers\CryptoInterface
;
6
use
fpoirotte\Cryptal\SymmetricModeInterface
;
7
11
class
CTR
implements
SymmetricModeInterface
12
{
14
protected
$cipher
;
15
17
protected
$counter
;
18
20
protected
$blockSize
;
21
22
public
function
__construct
(
CryptoInterface
$cipher
, $iv, $tagLength)
23
{
24
$ivSize = strlen($iv);
25
$blockSize
= $cipher->
getBlockSize
();
26
if
($ivSize !==
$blockSize
) {
27
throw
new \Exception(
"Invalid IV size (got $ivSize bytes; should be $blockSize)"
);
28
}
29
30
$this->cipher =
$cipher
;
31
$this->counter = $iv;
32
$this->blockSize =
$blockSize
;
33
}
34
36
protected
function
incrementCounter
()
37
{
38
$carry = 1;
39
for
($i = $this->blockSize - 1; $i >= 0; $i--) {
40
// chr() takes care of overflows automatically.
41
$this->counter[$i] = chr(ord($this->counter[$i]) + $carry);
42
$carry &= (
"\x00"
=== $this->counter[$i]);
43
}
44
}
45
46
public
function
encrypt
($data, $context)
47
{
48
$res = $this->cipher->encrypt(
''
, $this->counter);
49
$this->
incrementCounter
();
50
return
$res ^ $data;
51
}
52
}
CryptoInterface
fpoirotte\Cryptal\Implementers\CryptoInterface\getBlockSize
getBlockSize()
fpoirotte\Cryptal\Modes\CTR\$blockSize
$blockSize
Cipher block size.
Definition:
CTR.php:20
fpoirotte\Cryptal\Implementers\CryptoInterface
Definition:
CryptoInterface.php:13
fpoirotte\Cryptal\Modes\CTR\__construct
__construct(CryptoInterface $cipher, $iv, $tagLength)
Definition:
CTR.php:22
fpoirotte\Cryptal\Modes\CTR\encrypt
encrypt($data, $context)
Definition:
CTR.php:46
fpoirotte\Cryptal\Modes
Definition:
CBC.php:3
fpoirotte\Cryptal\Modes\CTR\$cipher
$cipher
Cipher.
Definition:
CTR.php:14
fpoirotte\Cryptal\Modes\CTR\$counter
$counter
Counter.
Definition:
CTR.php:17
fpoirotte\Cryptal\Modes\CTR
Definition:
CTR.php:11
fpoirotte\Cryptal\SymmetricModeInterface
Definition:
SymmetricModeInterface.php:11
SymmetricModeInterface
fpoirotte\Cryptal\Modes\CTR\incrementCounter
incrementCounter()
Increment the value of the counter by one.
Definition:
CTR.php:36
src
Cryptal
Modes
CTR.php
Generated on Sat Sep 29 2018 22:22:05 for cryptal by
1.8.11