3 * An abstract class for simplifing creation of cipher modes
7 * @category PHPCryptLib
10 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
11 * @copyright 2011 The Authors
12 * @license http://www.opensource.org/licenses/mit-license.html MIT License
13 * @version Build @@version@@
16 namespace CryptLib\Cipher\Block
;
19 * An abstract class for simplifing creation of cipher modes
21 * @category PHPCryptLib
25 abstract class AbstractMode
implements \CryptLib\Cipher\Block\Mode
{
28 * @var string Additional data to authenticate with
30 protected $adata = '';
33 * @var Cipher The cipher to use for this mode instance
35 protected $cipher = null;
38 * @var string The initialization Vector to use for this mode
40 protected $initv = '';
43 * @var string The mode name for the current instance
48 * @var array Mode specific options
50 protected $options = array();
53 * @var string The internal state of the mode
55 protected $state = '';
58 * Perform the decryption of the current block
60 * @param string $data The data to decrypt
62 * @return string The decrypted data
64 abstract protected function decryptBlock($data);
67 * Perform the encryption of the current block
69 * @param string $data The data to encrypt
71 * @return string The encrypted data
73 abstract protected function encryptBlock($data);
76 * Build the instance of the cipher mode
78 * @param Cipher $cipher The cipher to use for encryption/decryption
79 * @param string $initv The initialization vector (empty if not needed)
80 * @param array $options An array of mode-specific options
82 public function __construct(
83 \CryptLib\Cipher\Block\Cipher
$cipher,
85 array $options = array()
87 $class = strtolower(get_class($this));
88 $class = substr($class, strrpos($class, '\\') +
1);
90 $this->options
= $options +
$this->options
;
91 $this->cipher
= $cipher;
92 $this->initv
= $initv;
97 * Decrypt the data using the supplied key, cipher and initialization vector
99 * @param string $data The data to decrypt
101 * @return string The decrypted data
103 public function decrypt($data) {
104 $this->enforceBlockSize($data);
105 return $this->decryptBlock($data);
109 * Encrypt the data using the supplied key, cipher and initialization vector
111 * @param string $data The data to encrypt
113 * @return string The encrypted data
115 public function encrypt($data) {
116 $this->enforceBlockSize($data);
117 return $this->encryptBlock($data);
121 * Finish the mode and append any additional data necessary
123 * @return string Any additional data
125 public function finish() {
130 * Get the name of the current mode implementation
132 * @return string The current mode name
134 public function getMode() {
139 * Reset the mode to start over (destroying any intermediate state)
143 public function reset() {
144 $this->state
= $this->initv
;
148 * Enforce the data block is the correct size for the cipher
150 * @param string $data The data to check
153 * @throws InvalidArgumentException if the block size is not correct
155 protected function enforceBlockSize($data) {
156 if (strlen($data) != $this->cipher
->getBlockSize()) {
157 throw new \
InvalidArgumentException(
159 'The data block must match the block size [%d:%d]',
161 $this->cipher
->getBlockSize()