Rev 846 | Rev 1042 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 846 | Rev 874 | ||
---|---|---|---|
Line 42... | Line 42... | ||
42 | * |
42 | * |
43 | * echo $rijndael->decrypt($rijndael->encrypt($plaintext)); |
43 | * echo $rijndael->decrypt($rijndael->encrypt($plaintext)); |
44 | * ?> |
44 | * ?> |
45 | * </code> |
45 | * </code> |
46 | * |
46 | * |
- | 47 | * @category Crypt |
|
- | 48 | * @package Rijndael |
|
47 | * @author Jim Wigginton <terrafrost@php.net> |
49 | * @author Jim Wigginton <terrafrost@php.net> |
48 | * @copyright 2008 Jim Wigginton |
50 | * @copyright 2008 Jim Wigginton |
49 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
51 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
50 | * @link http://phpseclib.sourceforge.net |
52 | * @link http://phpseclib.sourceforge.net |
51 | */ |
53 | */ |
Line 60... | Line 62... | ||
60 | use phpseclib3\Exception\InsufficientSetupException; |
62 | use phpseclib3\Exception\InsufficientSetupException; |
61 | 63 | ||
62 | /** |
64 | /** |
63 | * Pure-PHP implementation of Rijndael. |
65 | * Pure-PHP implementation of Rijndael. |
64 | * |
66 | * |
- | 67 | * @package Rijndael |
|
65 | * @author Jim Wigginton <terrafrost@php.net> |
68 | * @author Jim Wigginton <terrafrost@php.net> |
- | 69 | * @access public |
|
66 | */ |
70 | */ |
67 | class Rijndael extends BlockCipher |
71 | class Rijndael extends BlockCipher |
68 | { |
72 | { |
69 | /** |
73 | /** |
70 | * The mcrypt specific name of the cipher |
74 | * The mcrypt specific name of the cipher |
Line 76... | Line 80... | ||
76 | * |
80 | * |
77 | * @see \phpseclib3\Crypt\Common\SymmetricKey::cipher_name_mcrypt |
81 | * @see \phpseclib3\Crypt\Common\SymmetricKey::cipher_name_mcrypt |
78 | * @see \phpseclib3\Crypt\Common\SymmetricKey::engine |
82 | * @see \phpseclib3\Crypt\Common\SymmetricKey::engine |
79 | * @see self::isValidEngine() |
83 | * @see self::isValidEngine() |
80 | * @var string |
84 | * @var string |
- | 85 | * @access private |
|
81 | */ |
86 | */ |
82 | protected $cipher_name_mcrypt = 'rijndael-128'; |
87 | protected $cipher_name_mcrypt = 'rijndael-128'; |
83 | 88 | ||
84 | /** |
89 | /** |
85 | * The Key Schedule |
90 | * The Key Schedule |
86 | * |
91 | * |
87 | * @see self::setup() |
92 | * @see self::setup() |
88 | * @var array |
93 | * @var array |
- | 94 | * @access private |
|
89 | */ |
95 | */ |
90 | private $w; |
96 | private $w; |
91 | 97 | ||
92 | /** |
98 | /** |
93 | * The Inverse Key Schedule |
99 | * The Inverse Key Schedule |
94 | * |
100 | * |
95 | * @see self::setup() |
101 | * @see self::setup() |
96 | * @var array |
102 | * @var array |
- | 103 | * @access private |
|
97 | */ |
104 | */ |
98 | private $dw; |
105 | private $dw; |
99 | 106 | ||
100 | /** |
107 | /** |
101 | * The Block Length divided by 32 |
108 | * The Block Length divided by 32 |
Line 105... | Line 112... | ||
105 | * derive this from $block_size or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu |
112 | * derive this from $block_size or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu |
106 | * of that, we'll just precompute it once.} |
113 | * of that, we'll just precompute it once.} |
107 | * |
114 | * |
108 | * @see self::setBlockLength() |
115 | * @see self::setBlockLength() |
109 | * @var int |
116 | * @var int |
- | 117 | * @access private |
|
110 | */ |
118 | */ |
111 | private $Nb = 4; |
119 | private $Nb = 4; |
112 | 120 | ||
113 | /** |
121 | /** |
114 | * The Key Length (in bytes) |
122 | * The Key Length (in bytes) |
Line 118... | Line 126... | ||
118 | * derive this from $key_length or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu |
126 | * derive this from $key_length or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu |
119 | * of that, we'll just precompute it once.} |
127 | * of that, we'll just precompute it once.} |
120 | * |
128 | * |
121 | * @see self::setKeyLength() |
129 | * @see self::setKeyLength() |
122 | * @var int |
130 | * @var int |
- | 131 | * @access private |
|
123 | */ |
132 | */ |
124 | protected $key_length = 16; |
133 | protected $key_length = 16; |
125 | 134 | ||
126 | /** |
135 | /** |
127 | * The Key Length divided by 32 |
136 | * The Key Length divided by 32 |
128 | * |
137 | * |
129 | * @see self::setKeyLength() |
138 | * @see self::setKeyLength() |
130 | * @var int |
139 | * @var int |
- | 140 | * @access private |
|
131 | * @internal The max value is 256 / 32 = 8, the min value is 128 / 32 = 4 |
141 | * @internal The max value is 256 / 32 = 8, the min value is 128 / 32 = 4 |
132 | */ |
142 | */ |
133 | private $Nk = 4; |
143 | private $Nk = 4; |
134 | 144 | ||
135 | /** |
145 | /** |
136 | * The Number of Rounds |
146 | * The Number of Rounds |
137 | * |
147 | * |
138 | * {@internal The max value is 14, the min value is 10.} |
148 | * {@internal The max value is 14, the min value is 10.} |
139 | * |
149 | * |
140 | * @var int |
150 | * @var int |
- | 151 | * @access private |
|
141 | */ |
152 | */ |
142 | private $Nr; |
153 | private $Nr; |
143 | 154 | ||
144 | /** |
155 | /** |
145 | * Shift offsets |
156 | * Shift offsets |
146 | * |
157 | * |
147 | * @var array |
158 | * @var array |
- | 159 | * @access private |
|
148 | */ |
160 | */ |
149 | private $c; |
161 | private $c; |
150 | 162 | ||
151 | /** |
163 | /** |
152 | * Holds the last used key- and block_size information |
164 | * Holds the last used key- and block_size information |
153 | * |
165 | * |
154 | * @var array |
166 | * @var array |
- | 167 | * @access private |
|
155 | */ |
168 | */ |
156 | private $kl; |
169 | private $kl; |
157 | 170 | ||
158 | /** |
171 | /** |
159 | * Default Constructor. |
172 | * Default Constructor. |
160 | * |
173 | * |
161 | * @param string $mode |
174 | * @param string $mode |
- | 175 | * @access public |
|
162 | * @throws \InvalidArgumentException if an invalid / unsupported mode is provided |
176 | * @throws \InvalidArgumentException if an invalid / unsupported mode is provided |
163 | */ |
177 | */ |
164 | public function __construct($mode) |
178 | public function __construct($mode) |
165 | { |
179 | { |
166 | parent::__construct($mode); |
180 | parent::__construct($mode); |
Line 184... | Line 198... | ||
184 | * |
198 | * |
185 | * Additional: In case of 160- and 224-bit keys, phpseclib will/can, for that reason, not use |
199 | * Additional: In case of 160- and 224-bit keys, phpseclib will/can, for that reason, not use |
186 | * the mcrypt php extension, even if available. |
200 | * the mcrypt php extension, even if available. |
187 | * This results then in slower encryption. |
201 | * This results then in slower encryption. |
188 | * |
202 | * |
- | 203 | * @access public |
|
189 | * @throws \LengthException if the key length is invalid |
204 | * @throws \LengthException if the key length is invalid |
190 | * @param int $length |
205 | * @param int $length |
191 | */ |
206 | */ |
192 | public function setKeyLength($length) |
207 | public function setKeyLength($length) |
193 | { |
208 | { |
Line 210... | Line 225... | ||
210 | * Sets the key. |
225 | * Sets the key. |
211 | * |
226 | * |
212 | * Rijndael supports five different key lengths |
227 | * Rijndael supports five different key lengths |
213 | * |
228 | * |
214 | * @see setKeyLength() |
229 | * @see setKeyLength() |
- | 230 | * @access public |
|
215 | * @param string $key |
231 | * @param string $key |
216 | * @throws \LengthException if the key length isn't supported |
232 | * @throws \LengthException if the key length isn't supported |
217 | */ |
233 | */ |
218 | public function setKey($key) |
234 | public function setKey($key) |
219 | { |
235 | { |
Line 234... | Line 250... | ||
234 | /** |
250 | /** |
235 | * Sets the block length |
251 | * Sets the block length |
236 | * |
252 | * |
237 | * Valid block lengths are 128, 160, 192, 224, and 256. |
253 | * Valid block lengths are 128, 160, 192, 224, and 256. |
238 | * |
254 | * |
- | 255 | * @access public |
|
239 | * @param int $length |
256 | * @param int $length |
240 | */ |
257 | */ |
241 | public function setBlockLength($length) |
258 | public function setBlockLength($length) |
242 | { |
259 | { |
243 | switch ($length) { |
260 | switch ($length) { |
Line 262... | Line 279... | ||
262 | * |
279 | * |
263 | * This is mainly just a wrapper to set things up for \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine() |
280 | * This is mainly just a wrapper to set things up for \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine() |
264 | * |
281 | * |
265 | * @see \phpseclib3\Crypt\Common\SymmetricKey::__construct() |
282 | * @see \phpseclib3\Crypt\Common\SymmetricKey::__construct() |
266 | * @param int $engine |
283 | * @param int $engine |
- | 284 | * @access protected |
|
267 | * @return bool |
285 | * @return bool |
268 | */ |
286 | */ |
269 | protected function isValidEngineHelper($engine) |
287 | protected function isValidEngineHelper($engine) |
270 | { |
288 | { |
271 | switch ($engine) { |
289 | switch ($engine) { |
Line 304... | Line 322... | ||
304 | } |
322 | } |
305 | 323 | ||
306 | /** |
324 | /** |
307 | * Encrypts a block |
325 | * Encrypts a block |
308 | * |
326 | * |
- | 327 | * @access private |
|
309 | * @param string $in |
328 | * @param string $in |
310 | * @return string |
329 | * @return string |
311 | */ |
330 | */ |
312 | protected function encryptBlock($in) |
331 | protected function encryptBlock($in) |
313 | { |
332 | { |
Line 393... | Line 412... | ||
393 | } |
412 | } |
394 | 413 | ||
395 | /** |
414 | /** |
396 | * Decrypts a block |
415 | * Decrypts a block |
397 | * |
416 | * |
- | 417 | * @access private |
|
398 | * @param string $in |
418 | * @param string $in |
399 | * @return string |
419 | * @return string |
400 | */ |
420 | */ |
401 | protected function decryptBlock($in) |
421 | protected function decryptBlock($in) |
402 | { |
422 | { |
Line 492... | Line 512... | ||
492 | * {@internal Could, but not must, extend by the child Crypt_* class} |
512 | * {@internal Could, but not must, extend by the child Crypt_* class} |
493 | * |
513 | * |
494 | * @see self::setKey() |
514 | * @see self::setKey() |
495 | * @see self::setIV() |
515 | * @see self::setIV() |
496 | * @see self::disableContinuousBuffer() |
516 | * @see self::disableContinuousBuffer() |
- | 517 | * @access private |
|
497 | */ |
518 | */ |
498 | protected function setup() |
519 | protected function setup() |
499 | { |
520 | { |
500 | if (!$this->changed) { |
521 | if (!$this->changed) { |
501 | return; |
522 | return; |
Line 510... | Line 531... | ||
510 | 531 | ||
511 | /** |
532 | /** |
512 | * Setup the key (expansion) |
533 | * Setup the key (expansion) |
513 | * |
534 | * |
514 | * @see \phpseclib3\Crypt\Common\SymmetricKey::setupKey() |
535 | * @see \phpseclib3\Crypt\Common\SymmetricKey::setupKey() |
- | 536 | * @access private |
|
515 | */ |
537 | */ |
516 | protected function setupKey() |
538 | protected function setupKey() |
517 | { |
539 | { |
518 | // Each number in $rcon is equal to the previous number multiplied by two in Rijndael's finite field. |
540 | // Each number in $rcon is equal to the previous number multiplied by two in Rijndael's finite field. |
519 | // See http://en.wikipedia.org/wiki/Finite_field_arithmetic#Multiplicative_inverse |
541 | // See http://en.wikipedia.org/wiki/Finite_field_arithmetic#Multiplicative_inverse |
Line 622... | Line 644... | ||
622 | 644 | ||
623 | /** |
645 | /** |
624 | * Performs S-Box substitutions |
646 | * Performs S-Box substitutions |
625 | * |
647 | * |
626 | * @return array |
648 | * @return array |
- | 649 | * @access private |
|
627 | * @param int $word |
650 | * @param int $word |
628 | */ |
651 | */ |
629 | private function subWord($word) |
652 | private function subWord($word) |
630 | { |
653 | { |
631 | static $sbox; |
654 | static $sbox; |
Line 643... | Line 666... | ||
643 | * Provides the mixColumns and sboxes tables |
666 | * Provides the mixColumns and sboxes tables |
644 | * |
667 | * |
645 | * @see self::encryptBlock() |
668 | * @see self::encryptBlock() |
646 | * @see self::setupInlineCrypt() |
669 | * @see self::setupInlineCrypt() |
647 | * @see self::subWord() |
670 | * @see self::subWord() |
- | 671 | * @access private |
|
648 | * @return array &$tables |
672 | * @return array &$tables |
649 | */ |
673 | */ |
650 | protected function &getTables() |
674 | protected function &getTables() |
651 | { |
675 | { |
652 | static $tables; |
676 | static $tables; |
Line 731... | Line 755... | ||
731 | * Provides the inverse mixColumns and inverse sboxes tables |
755 | * Provides the inverse mixColumns and inverse sboxes tables |
732 | * |
756 | * |
733 | * @see self::decryptBlock() |
757 | * @see self::decryptBlock() |
734 | * @see self::setupInlineCrypt() |
758 | * @see self::setupInlineCrypt() |
735 | * @see self::setupKey() |
759 | * @see self::setupKey() |
- | 760 | * @access private |
|
736 | * @return array &$tables |
761 | * @return array &$tables |
737 | */ |
762 | */ |
738 | protected function &getInvTables() |
763 | protected function &getInvTables() |
739 | { |
764 | { |
740 | static $tables; |
765 | static $tables; |
Line 812... | Line 837... | ||
812 | 837 | ||
813 | /** |
838 | /** |
814 | * Setup the performance-optimized function for de/encrypt() |
839 | * Setup the performance-optimized function for de/encrypt() |
815 | * |
840 | * |
816 | * @see \phpseclib3\Crypt\Common\SymmetricKey::setupInlineCrypt() |
841 | * @see \phpseclib3\Crypt\Common\SymmetricKey::setupInlineCrypt() |
- | 842 | * @access private |
|
817 | */ |
843 | */ |
818 | protected function setupInlineCrypt() |
844 | protected function setupInlineCrypt() |
819 | { |
845 | { |
820 | $w = $this->w; |
846 | $w = $this->w; |
821 | $dw = $this->dw; |
847 | $dw = $this->dw; |
Line 954... | Line 980... | ||
954 | /** |
980 | /** |
955 | * Encrypts a message. |
981 | * Encrypts a message. |
956 | * |
982 | * |
957 | * @see self::decrypt() |
983 | * @see self::decrypt() |
958 | * @see parent::encrypt() |
984 | * @see parent::encrypt() |
- | 985 | * @access public |
|
959 | * @param string $plaintext |
986 | * @param string $plaintext |
960 | * @return string |
987 | * @return string |
961 | */ |
988 | */ |
962 | public function encrypt($plaintext) |
989 | public function encrypt($plaintext) |
963 | { |
990 | { |
Line 985... | Line 1012... | ||
985 | /** |
1012 | /** |
986 | * Decrypts a message. |
1013 | * Decrypts a message. |
987 | * |
1014 | * |
988 | * @see self::encrypt() |
1015 | * @see self::encrypt() |
989 | * @see parent::decrypt() |
1016 | * @see parent::decrypt() |
- | 1017 | * @access public |
|
990 | * @param string $ciphertext |
1018 | * @param string $ciphertext |
991 | * @return string |
1019 | * @return string |
992 | */ |
1020 | */ |
993 | public function decrypt($ciphertext) |
1021 | public function decrypt($ciphertext) |
994 | { |
1022 | { |