add methods to decrypt return data from router
[GitHub/Stricted/speedport-hybrid-php-api.git] / CryptLib / Cipher / Block / AbstractMode.php
1 <?php
2 /**
3 * An abstract class for simplifing creation of cipher modes
4 *
5 * PHP version 5.3
6 *
7 * @category PHPCryptLib
8 * @package Cipher
9 * @subpackage Block
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@@
14 */
15
16 namespace CryptLib\Cipher\Block;
17
18 /**
19 * An abstract class for simplifing creation of cipher modes
20 *
21 * @category PHPCryptLib
22 * @package Cipher
23 * @subpackage Block
24 */
25 abstract class AbstractMode implements \CryptLib\Cipher\Block\Mode {
26
27 /**
28 * @var string Additional data to authenticate with
29 */
30 protected $adata = '';
31
32 /**
33 * @var Cipher The cipher to use for this mode instance
34 */
35 protected $cipher = null;
36
37 /**
38 * @var string The initialization Vector to use for this mode
39 */
40 protected $initv = '';
41
42 /**
43 * @var string The mode name for the current instance
44 */
45 protected $mode = '';
46
47 /**
48 * @var array Mode specific options
49 */
50 protected $options = array();
51
52 /**
53 * @var string The internal state of the mode
54 */
55 protected $state = '';
56
57 /**
58 * Perform the decryption of the current block
59 *
60 * @param string $data The data to decrypt
61 *
62 * @return string The decrypted data
63 */
64 abstract protected function decryptBlock($data);
65
66 /**
67 * Perform the encryption of the current block
68 *
69 * @param string $data The data to encrypt
70 *
71 * @return string The encrypted data
72 */
73 abstract protected function encryptBlock($data);
74
75 /**
76 * Build the instance of the cipher mode
77 *
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
81 */
82 public function __construct(
83 \CryptLib\Cipher\Block\Cipher $cipher,
84 $initv,
85 array $options = array()
86 ) {
87 $class = strtolower(get_class($this));
88 $class = substr($class, strrpos($class, '\\') + 1);
89 $this->mode = $class;
90 $this->options = $options + $this->options;
91 $this->cipher = $cipher;
92 $this->initv = $initv;
93 $this->reset();
94 }
95
96 /**
97 * Decrypt the data using the supplied key, cipher and initialization vector
98 *
99 * @param string $data The data to decrypt
100 *
101 * @return string The decrypted data
102 */
103 public function decrypt($data) {
104 $this->enforceBlockSize($data);
105 return $this->decryptBlock($data);
106 }
107
108 /**
109 * Encrypt the data using the supplied key, cipher and initialization vector
110 *
111 * @param string $data The data to encrypt
112 *
113 * @return string The encrypted data
114 */
115 public function encrypt($data) {
116 $this->enforceBlockSize($data);
117 return $this->encryptBlock($data);
118 }
119
120 /**
121 * Finish the mode and append any additional data necessary
122 *
123 * @return string Any additional data
124 */
125 public function finish() {
126 return '';
127 }
128
129 /**
130 * Get the name of the current mode implementation
131 *
132 * @return string The current mode name
133 */
134 public function getMode() {
135 return $this->mode;
136 }
137
138 /**
139 * Reset the mode to start over (destroying any intermediate state)
140 *
141 * @return void
142 */
143 public function reset() {
144 $this->state = $this->initv;
145 }
146
147 /**
148 * Enforce the data block is the correct size for the cipher
149 *
150 * @param string $data The data to check
151 *
152 * @return void
153 * @throws InvalidArgumentException if the block size is not correct
154 */
155 protected function enforceBlockSize($data) {
156 if (strlen($data) != $this->cipher->getBlockSize()) {
157 throw new \InvalidArgumentException(
158 sprintf(
159 'The data block must match the block size [%d:%d]',
160 strlen($data),
161 $this->cipher->getBlockSize()
162 )
163 );
164 }
165 }
166 }