]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/extlib/phpseclib/Crypt/AES.php
Latest phpseclib stuff and moved into its own dir.
[quix0rs-gnu-social.git] / plugins / OStatus / extlib / phpseclib / Crypt / AES.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 /**
5  * Pure-PHP implementation of AES.
6  *
7  * Uses mcrypt, if available/possible, and an internal implementation, otherwise.
8  *
9  * PHP versions 4 and 5
10  *
11  * If {@link Crypt_AES::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
12  * {@link Crypt_AES::setKey() setKey()}.  ie. if the key is 128-bits, the key length will be 128-bits.  If it's 136-bits
13  * it'll be null-padded to 192-bits and 192 bits will be the key length until {@link Crypt_AES::setKey() setKey()}
14  * is called, again, at which point, it'll be recalculated.
15  *
16  * Since Crypt_AES extends Crypt_Rijndael, some functions are available to be called that, in the context of AES, don't
17  * make a whole lot of sense.  {@link Crypt_AES::setBlockLength() setBlockLength()}, for instance.  Calling that function,
18  * however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one).
19  *
20  * Here's a short example of how to use this library:
21  * <code>
22  * <?php
23  *    include('Crypt/AES.php');
24  *
25  *    $aes = new Crypt_AES();
26  *
27  *    $aes->setKey('abcdefghijklmnop');
28  *
29  *    $size = 10 * 1024;
30  *    $plaintext = '';
31  *    for ($i = 0; $i < $size; $i++) {
32  *        $plaintext.= 'a';
33  *    }
34  *
35  *    echo $aes->decrypt($aes->encrypt($plaintext));
36  * ?>
37  * </code>
38  *
39  * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
40  * of this software and associated documentation files (the "Software"), to deal
41  * in the Software without restriction, including without limitation the rights
42  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43  * copies of the Software, and to permit persons to whom the Software is
44  * furnished to do so, subject to the following conditions:
45  *
46  * The above copyright notice and this permission notice shall be included in
47  * all copies or substantial portions of the Software.
48  *
49  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
55  * THE SOFTWARE.
56  *
57  * @category   Crypt
58  * @package    Crypt_AES
59  * @author     Jim Wigginton <terrafrost@php.net>
60  * @copyright  MMVIII Jim Wigginton
61  * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
62  * @link       http://phpseclib.sourceforge.net
63  */
64
65 /**
66  * Include Crypt_Rijndael
67  */
68 if (!class_exists('Crypt_Rijndael')) {
69     require_once('Rijndael.php');
70 }
71
72 /**#@+
73  * @access public
74  * @see Crypt_AES::encrypt()
75  * @see Crypt_AES::decrypt()
76  */
77 /**
78  * Encrypt / decrypt using the Counter mode.
79  *
80  * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
81  *
82  * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
83  */
84 define('CRYPT_AES_MODE_CTR', CRYPT_MODE_CTR);
85 /**
86  * Encrypt / decrypt using the Electronic Code Book mode.
87  *
88  * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
89  */
90 define('CRYPT_AES_MODE_ECB', CRYPT_MODE_ECB);
91 /**
92  * Encrypt / decrypt using the Code Book Chaining mode.
93  *
94  * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
95  */
96 define('CRYPT_AES_MODE_CBC', CRYPT_MODE_CBC);
97 /**
98  * Encrypt / decrypt using the Cipher Feedback mode.
99  *
100  * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
101  */
102 define('CRYPT_AES_MODE_CFB', CRYPT_MODE_CFB);
103 /**
104  * Encrypt / decrypt using the Cipher Feedback mode.
105  *
106  * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
107  */
108 define('CRYPT_AES_MODE_OFB', CRYPT_MODE_OFB);
109 /**#@-*/
110
111 /**#@+
112  * @access private
113  * @see Crypt_AES::Crypt_AES()
114  */
115 /**
116  * Toggles the internal implementation
117  */
118 define('CRYPT_AES_MODE_INTERNAL', CRYPT_MODE_INTERNAL);
119 /**
120  * Toggles the mcrypt implementation
121  */
122 define('CRYPT_AES_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
123 /**#@-*/
124
125 /**
126  * Pure-PHP implementation of AES.
127  *
128  * @author  Jim Wigginton <terrafrost@php.net>
129  * @version 0.1.0
130  * @access  public
131  * @package Crypt_AES
132  */
133 class Crypt_AES extends Crypt_Rijndael {
134     /**
135      * The namespace used by the cipher for its constants.
136      *
137      * @see Crypt_Base::const_namespace
138      * @var String
139      * @access private
140      */
141     var $const_namespace = 'AES';
142
143     /**
144      * Default Constructor.
145      *
146      * Determines whether or not the mcrypt extension should be used.
147      *
148      * $mode could be:
149      *
150      * - CRYPT_AES_MODE_ECB
151      *
152      * - CRYPT_AES_MODE_CBC
153      *
154      * - CRYPT_AES_MODE_CTR
155      *
156      * - CRYPT_AES_MODE_CFB
157      *
158      * - CRYPT_AES_MODE_OFB
159      *
160      * If not explictly set, CRYPT_AES_MODE_CBC will be used.
161      *
162      * @see Crypt_Rijndael::Crypt_Rijndael()
163      * @see Crypt_Base::Crypt_Base()
164      * @param optional Integer $mode
165      * @access public
166      */
167     function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
168     {
169         parent::Crypt_Rijndael($mode);
170     }
171
172     /**
173      * Dummy function
174      *
175      * Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
176      *
177      * @see Crypt_Rijndael::setBlockLength()
178      * @access public
179      * @param Integer $length
180      */
181     function setBlockLength($length)
182     {
183         return;
184     }
185 }
186
187 // vim: ts=4:sw=4:et:
188 // vim6: fdl=1: