1 <?php
2 
3 /**
4  * Pure-PHP implementation of AES.
5  *
6  * Uses mcrypt, if available/possible, and an internal implementation, otherwise.
7  *
8  * PHP version 5
9  *
10  * NOTE: Since AES.php is (for compatibility and phpseclib-historical reasons) virtually
11  * just a wrapper to Rijndael.php you may consider using Rijndael.php instead of
12  * to save one include_once().
13  *
14  * If {@link self::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
15  * {@link self::setKey() setKey()}.  ie. if the key is 128-bits, the key length will be 128-bits.  If it's 136-bits
16  * it'll be null-padded to 192-bits and 192 bits will be the key length until {@link self::setKey() setKey()}
17  * is called, again, at which point, it'll be recalculated.
18  *
19  * Since \phpseclib3\Crypt\AES extends \phpseclib3\Crypt\Rijndael, some functions are available to be called that, in the context of AES, don't
20  * make a whole lot of sense.  {@link self::setBlockLength() setBlockLength()}, for instance.  Calling that function,
21  * however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one).
22  *
23  * Here's a short example of how to use this library:
24  * <code>
25  * <?php
26  *    include 'vendor/autoload.php';
27  *
28  *    $aes = new \phpseclib3\Crypt\AES('ctr');
29  *
30  *    $aes->setKey('abcdefghijklmnop');
31  *
32  *    $size = 10 * 1024;
33  *    $plaintext = '';
34  *    for ($i = 0; $i < $size; $i++) {
35  *        $plaintext.= 'a';
36  *    }
37  *
38  *    echo $aes->decrypt($aes->encrypt($plaintext));
39  * ?>
40  * </code>
41  *
42  * @author    Jim Wigginton <terrafrost@php.net>
43  * @copyright 2008 Jim Wigginton
44  * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
45  * @link      http://phpseclib.sourceforge.net
46  */
47 
48 namespace phpseclib3\Crypt;
49 
50 /**
51  * Pure-PHP implementation of AES.
52  *
53  * @author  Jim Wigginton <terrafrost@php.net>
54  */
55 class AES extends Rijndael
56 {
57     /**
58      * Dummy function
59      *
60      * Since \phpseclib3\Crypt\AES extends \phpseclib3\Crypt\Rijndael, this function is, technically, available, but it doesn't do anything.
61      *
62      * @see \phpseclib3\Crypt\Rijndael::setBlockLength()
63      * @param int $length
64      * @throws \BadMethodCallException anytime it's called
65      */
66     public function setBlockLength($length)
67     {
68         throw new \BadMethodCallException('The block length cannot be set for AES.');
69     }
70 
71     /**
72      * Sets the key length
73      *
74      * Valid key lengths are 128, 192, and 256.  Set the link to bool(false) to disable a fixed key length
75      *
76      * @see \phpseclib3\Crypt\Rijndael:setKeyLength()
77      * @param int $length
78      * @throws \LengthException if the key length isn't supported
79      */
80     public function setKeyLength($length)
81     {
82         switch ($length) {
83             case 128:
84             case 192:
85             case 256:
86                 break;
87             default:
88                 throw new \LengthException('Key of size ' . $length . ' not supported by this algorithm. Only keys of sizes 128, 192 or 256 supported');
89         }
90         parent::setKeyLength($length);
91     }
92 
93     /**
94      * Sets the key.
95      *
96      * Rijndael supports five different key lengths, AES only supports three.
97      *
98      * @see \phpseclib3\Crypt\Rijndael:setKey()
99      * @see setKeyLength()
100      * @param string $key
101      * @throws \LengthException if the key length isn't supported
102      */
103     public function setKey($key)
104     {
105         switch (strlen($key)) {
106             case 16:
107             case 24:
108             case 32:
109                 break;
110             default:
111                 throw new \LengthException('Key of size ' . strlen($key) . ' not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported');
112         }
113 
114         parent::setKey($key);
115     }
116 }
117