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 versions 4 and 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 Crypt_AES extends 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 'Crypt/AES.php'; 27 * 28 * $aes = new Crypt_AES(); 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 * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy 43 * of this software and associated documentation files (the "Software"), to deal 44 * in the Software without restriction, including without limitation the rights 45 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 46 * copies of the Software, and to permit persons to whom the Software is 47 * furnished to do so, subject to the following conditions: 48 * 49 * The above copyright notice and this permission notice shall be included in 50 * all copies or substantial portions of the Software. 51 * 52 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 53 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 54 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 55 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 56 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 57 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 58 * THE SOFTWARE. 59 * 60 * @category Crypt 61 * @package Crypt_AES 62 * @author Jim Wigginton <terrafrost@php.net> 63 * @copyright 2008 Jim Wigginton 64 * @license http://www.opensource.org/licenses/mit-license.html MIT License 65 * @link http://phpseclib.sourceforge.net 66 */ 67 68/** 69 * Include Crypt_Rijndael 70 */ 71if (!class_exists('Crypt_Rijndael')) { 72 include_once 'Rijndael.php'; 73} 74 75/**#@+ 76 * @access public 77 * @see self::encrypt() 78 * @see self::decrypt() 79 */ 80/** 81 * Encrypt / decrypt using the Counter mode. 82 * 83 * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode. 84 * 85 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29 86 */ 87define('CRYPT_AES_MODE_CTR', CRYPT_MODE_CTR); 88/** 89 * Encrypt / decrypt using the Electronic Code Book mode. 90 * 91 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29 92 */ 93define('CRYPT_AES_MODE_ECB', CRYPT_MODE_ECB); 94/** 95 * Encrypt / decrypt using the Code Book Chaining mode. 96 * 97 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29 98 */ 99define('CRYPT_AES_MODE_CBC', CRYPT_MODE_CBC); 100/** 101 * Encrypt / decrypt using the Cipher Feedback mode. 102 * 103 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29 104 */ 105define('CRYPT_AES_MODE_CFB', CRYPT_MODE_CFB); 106/** 107 * Encrypt / decrypt using the Cipher Feedback mode. 108 * 109 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29 110 */ 111define('CRYPT_AES_MODE_OFB', CRYPT_MODE_OFB); 112/**#@-*/ 113 114/** 115 * Pure-PHP implementation of AES. 116 * 117 * @package Crypt_AES 118 * @author Jim Wigginton <terrafrost@php.net> 119 * @access public 120 */ 121class Crypt_AES extends Crypt_Rijndael 122{ 123 /** 124 * The namespace used by the cipher for its constants. 125 * 126 * @see Crypt_Base::const_namespace 127 * @var string 128 * @access private 129 */ 130 var $const_namespace = 'AES'; 131 132 /** 133 * Dummy function 134 * 135 * Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything. 136 * 137 * @see Crypt_Rijndael::setBlockLength() 138 * @access public 139 * @param int $length 140 */ 141 function setBlockLength($length) 142 { 143 return; 144 } 145 146 /** 147 * Sets the key length 148 * 149 * Valid key lengths are 128, 192, and 256. If the length is less than 128, it will be rounded up to 150 * 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount. 151 * 152 * @see Crypt_Rijndael:setKeyLength() 153 * @access public 154 * @param int $length 155 */ 156 function setKeyLength($length) 157 { 158 switch ($length) { 159 case 160: 160 $length = 192; 161 break; 162 case 224: 163 $length = 256; 164 } 165 parent::setKeyLength($length); 166 } 167 168 /** 169 * Sets the key. 170 * 171 * Rijndael supports five different key lengths, AES only supports three. 172 * 173 * @see Crypt_Rijndael:setKey() 174 * @see setKeyLength() 175 * @access public 176 * @param string $key 177 */ 178 function setKey($key) 179 { 180 parent::setKey($key); 181 182 if (!$this->explicit_key_length) { 183 $length = strlen($key); 184 switch (true) { 185 case $length <= 16: 186 $this->key_length = 16; 187 break; 188 case $length <= 24: 189 $this->key_length = 24; 190 break; 191 default: 192 $this->key_length = 32; 193 } 194 $this->_setEngine(); 195 } 196 } 197} 198