2013-05-20 08:19:38 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2014-12-17 01:16:54 +01:00
|
|
|
* Base Class for all \phpseclib\Crypt\* cipher classes
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2015-04-02 12:32:31 +02:00
|
|
|
* PHP version 5
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2013-06-26 06:50:40 +02:00
|
|
|
* Internally for phpseclib developers:
|
|
|
|
* If you plan to add a new cipher class, please note following rules:
|
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* - The new \phpseclib\Crypt\* cipher class should extend \phpseclib\Crypt\Common\SymmetricKey
|
2013-06-26 06:50:40 +02:00
|
|
|
*
|
|
|
|
* - Following methods are then required to be overridden/overloaded:
|
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* - encryptBlock()
|
2013-06-26 06:50:40 +02:00
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* - decryptBlock()
|
2013-06-26 06:50:40 +02:00
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* - setupKey()
|
2013-06-26 06:50:40 +02:00
|
|
|
*
|
|
|
|
* - All other methods are optional to be overridden/overloaded
|
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* - Look at the source code of the current ciphers how they extend \phpseclib\Crypt\Common\SymmetricKey
|
2013-06-26 06:50:40 +02:00
|
|
|
* and take one of them as a start up for the new cipher class.
|
|
|
|
*
|
|
|
|
* - Please read all the other comments/notes/hints here also for each class var/method
|
|
|
|
*
|
2013-12-10 20:10:37 +01:00
|
|
|
* @category Crypt
|
2014-12-17 01:16:54 +01:00
|
|
|
* @package Base
|
2013-12-10 20:10:37 +01:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @author Hans-Juergen Petrich <petrich@tronic-media.com>
|
2014-12-10 00:02:44 +01:00
|
|
|
* @copyright 2007 Jim Wigginton
|
2013-12-10 20:10:37 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
|
|
|
|
2016-07-31 05:18:06 +02:00
|
|
|
namespace phpseclib\Crypt\Common;
|
|
|
|
|
|
|
|
use phpseclib\Crypt\Hash;
|
|
|
|
use phpseclib\Common\Functions\Strings;
|
2016-10-19 14:45:42 +02:00
|
|
|
use phpseclib\Math\BigInteger;
|
2018-12-27 15:31:35 +01:00
|
|
|
use phpseclib\Math\BinaryField;
|
2016-05-12 22:28:40 +02:00
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
2014-12-17 01:16:54 +01:00
|
|
|
* Base Class for all \phpseclib\Crypt\* cipher classes
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @package Base
|
2013-05-20 08:19:38 +02:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @author Hans-Juergen Petrich <petrich@tronic-media.com>
|
|
|
|
*/
|
2016-07-31 05:18:06 +02:00
|
|
|
abstract class SymmetricKey
|
2013-12-03 19:34:41 +01:00
|
|
|
{
|
2014-12-09 19:46:30 +01:00
|
|
|
/**#@+
|
|
|
|
* @access public
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::encrypt()
|
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::decrypt()
|
2014-12-09 19:46:30 +01:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Counter mode.
|
|
|
|
*
|
|
|
|
* Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
|
|
|
|
*/
|
|
|
|
const MODE_CTR = -1;
|
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Electronic Code Book mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
|
2015-04-02 12:32:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const MODE_ECB = 1;
|
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Code Book Chaining mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
|
2015-04-02 12:32:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const MODE_CBC = 2;
|
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Cipher Feedback mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
|
2015-04-02 12:32:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const MODE_CFB = 3;
|
2017-10-04 12:30:53 +02:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Cipher Feedback mode (8bit)
|
|
|
|
*/
|
|
|
|
const MODE_CFB8 = 38;
|
2014-12-09 19:46:30 +01:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Output Feedback mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
|
2015-04-02 12:32:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const MODE_OFB = 4;
|
2018-12-27 15:31:35 +01:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using Galois/Counter mode.
|
|
|
|
*
|
|
|
|
* @link https://en.wikipedia.org/wiki/Galois/Counter_Mode
|
|
|
|
*/
|
|
|
|
const MODE_GCM = 5;
|
2014-12-09 19:46:30 +01:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using streaming mode.
|
2015-04-02 12:32:31 +02:00
|
|
|
*/
|
2018-12-27 15:31:35 +01:00
|
|
|
const MODE_STREAM = 6;
|
2014-12-09 19:46:30 +01:00
|
|
|
/**#@-*/
|
|
|
|
|
2017-06-28 05:34:36 +02:00
|
|
|
/**
|
|
|
|
* Mode Map
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::__construct()
|
|
|
|
*/
|
|
|
|
const MODE_MAP = [
|
2017-10-04 12:30:53 +02:00
|
|
|
'ctr' => self::MODE_CTR,
|
|
|
|
'ecb' => self::MODE_ECB,
|
|
|
|
'cbc' => self::MODE_CBC,
|
|
|
|
'cfb' => self::MODE_CFB,
|
|
|
|
'cfb8' => self::MODE_CFB8,
|
|
|
|
'ofb' => self::MODE_OFB,
|
2018-12-27 15:31:35 +01:00
|
|
|
'gcm' => self::MODE_GCM,
|
2017-06-28 05:34:36 +02:00
|
|
|
'stream' => self::MODE_STREAM
|
|
|
|
];
|
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
/**#@+
|
|
|
|
* @access private
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::__construct()
|
2015-04-02 12:32:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
/**
|
|
|
|
* Base value for the internal implementation $engine switch
|
2015-04-02 12:32:31 +02:00
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
const ENGINE_INTERNAL = 1;
|
2016-12-08 16:20:19 +01:00
|
|
|
/**
|
|
|
|
* Base value for the eval() implementation $engine switch
|
|
|
|
*/
|
|
|
|
const ENGINE_EVAL = 2;
|
2014-12-09 19:46:30 +01:00
|
|
|
/**
|
|
|
|
* Base value for the mcrypt implementation $engine switch
|
2015-04-02 12:32:31 +02:00
|
|
|
*/
|
2016-12-08 16:20:19 +01:00
|
|
|
const ENGINE_MCRYPT = 3;
|
2015-04-02 12:32:31 +02:00
|
|
|
/**
|
2018-12-27 15:31:35 +01:00
|
|
|
* Base value for the openssl implementation $engine switch
|
2015-04-02 12:32:31 +02:00
|
|
|
*/
|
2016-12-08 16:20:19 +01:00
|
|
|
const ENGINE_OPENSSL = 4;
|
2018-12-27 15:31:35 +01:00
|
|
|
/**
|
|
|
|
* Base value for the libsodium implementation $engine switch
|
|
|
|
*/
|
|
|
|
const ENGINE_LIBSODIUM = 5;
|
|
|
|
/**
|
|
|
|
* Base value for the openssl / gcm implementation $engine switch
|
|
|
|
*/
|
|
|
|
const ENGINE_OPENSSL_GCM = 6;
|
2014-12-09 19:46:30 +01:00
|
|
|
/**#@-*/
|
|
|
|
|
2017-06-28 05:34:36 +02:00
|
|
|
/**
|
|
|
|
* Engine Reverse Map
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::getEngine()
|
|
|
|
*/
|
|
|
|
const ENGINE_MAP = [
|
2018-12-27 15:31:35 +01:00
|
|
|
self::ENGINE_INTERNAL => 'PHP',
|
|
|
|
self::ENGINE_EVAL => 'Eval',
|
|
|
|
self::ENGINE_MCRYPT => 'mcrypt',
|
|
|
|
self::ENGINE_OPENSSL => 'OpenSSL',
|
|
|
|
self::ENGINE_LIBSODIUM => 'libsodium',
|
|
|
|
self::ENGINE_OPENSSL_GCM => 'OpenSSL (GCM)'
|
2017-06-28 05:34:36 +02:00
|
|
|
];
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
|
|
|
* The Encryption Mode
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::__construct()
|
|
|
|
* @var int
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $mode;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Block Length of the block cipher
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var int
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $block_size = 16;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Key
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::setKey()
|
|
|
|
* @var string
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $key = false;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Initialization Vector
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::setIV()
|
|
|
|
* @var string
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $iv = false;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A "sliding" Initialization Vector
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::enableContinuousBuffer()
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::clearBuffers()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var string
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $encryptIV;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A "sliding" Initialization Vector
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::enableContinuousBuffer()
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::clearBuffers()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var string
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $decryptIV;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Continuous Buffer status
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::enableContinuousBuffer()
|
|
|
|
* @var bool
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $continuousBuffer = false;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encryption buffer for CTR, OFB and CFB modes
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::clearBuffers()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var array
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $enbuffer;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decryption buffer for CTR, OFB and CFB modes
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::decrypt()
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::clearBuffers()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var array
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $debuffer;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* mcrypt resource for encryption
|
|
|
|
*
|
|
|
|
* The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
|
|
|
|
* Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
|
|
|
* @var resource
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $enmcrypt;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* mcrypt resource for decryption
|
|
|
|
*
|
|
|
|
* The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
|
|
|
|
* Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::decrypt()
|
|
|
|
* @var resource
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $demcrypt;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the enmcrypt resource need to be (re)initialized?
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\Twofish::setKey()
|
|
|
|
* @see \phpseclib\Crypt\Twofish::setIV()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var bool
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $enchanged = true;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the demcrypt resource need to be (re)initialized?
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\Twofish::setKey()
|
|
|
|
* @see \phpseclib\Crypt\Twofish::setIV()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var bool
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $dechanged = true;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* mcrypt resource for CFB mode
|
|
|
|
*
|
|
|
|
* mcrypt's CFB mode, in (and only in) buffered context,
|
|
|
|
* is broken, so phpseclib implements the CFB mode by it self,
|
|
|
|
* even when the mcrypt php extension is available.
|
|
|
|
*
|
|
|
|
* In order to do the CFB-mode work (fast) phpseclib
|
|
|
|
* use a separate ECB-mode mcrypt resource.
|
|
|
|
*
|
|
|
|
* @link http://phpseclib.sourceforge.net/cfb-demo.phps
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::decrypt()
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::setupMcrypt()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var resource
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $ecb;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Optimizing value while CFB-encrypting
|
|
|
|
*
|
|
|
|
* Only relevant if $continuousBuffer enabled
|
2014-12-09 19:46:30 +01:00
|
|
|
* and $engine == self::ENGINE_MCRYPT
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
|
|
|
* It's faster to re-init $enmcrypt if
|
|
|
|
* $buffer bytes > $cfb_init_len than
|
|
|
|
* using the $ecb resource furthermore.
|
|
|
|
*
|
2014-03-06 00:41:20 +01:00
|
|
|
* This value depends of the chosen cipher
|
2013-05-20 08:19:38 +02:00
|
|
|
* and the time it would be needed for it's
|
|
|
|
* initialization [by mcrypt_generic_init()]
|
|
|
|
* which, typically, depends on the complexity
|
|
|
|
* on its internaly Key-expanding algorithm.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
|
|
|
* @var int
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $cfb_init_len = 600;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Does internal cipher state need to be (re)initialized?
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::setKey()
|
|
|
|
* @see self::setIV()
|
|
|
|
* @see self::disableContinuousBuffer()
|
|
|
|
* @var bool
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $changed = true;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Padding status
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::enablePadding()
|
|
|
|
* @var bool
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $padding = true;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the mode one that is paddable?
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::__construct()
|
|
|
|
* @var bool
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $paddable = false;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds which crypt engine internaly should be use,
|
|
|
|
* which will be determined automatically on __construct()
|
|
|
|
*
|
|
|
|
* Currently available $engines are:
|
2018-12-27 15:31:35 +01:00
|
|
|
* - self::ENGINE_LIBSODIUM (very fast, php-extension: libsodium, extension_loaded('libsodium') required)
|
|
|
|
* - self::ENGINE_OPENSSL_GCM (very fast, php-extension: openssl, extension_loaded('openssl') required)
|
|
|
|
* - self::ENGINE_OPENSSL (very fast, php-extension: openssl, extension_loaded('openssl') required)
|
|
|
|
* - self::ENGINE_MCRYPT (fast, php-extension: mcrypt, extension_loaded('mcrypt') required)
|
|
|
|
* - self::ENGINE_EVAL (medium, pure php-engine, no php-extension required)
|
|
|
|
* - self::ENGINE_INTERNAL (slower, pure php-engine, no php-extension required)
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::setEngine()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::decrypt()
|
|
|
|
* @var int
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $engine;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2014-11-29 14:39:21 +01:00
|
|
|
/**
|
|
|
|
* Holds the preferred crypt engine
|
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::setEngine()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::setPreferredEngine()
|
|
|
|
* @var int
|
2014-11-29 14:39:21 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $preferredEngine;
|
2014-11-29 14:39:21 +01:00
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
|
|
|
* The mcrypt specific name of the cipher
|
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* Only used if $engine == self::ENGINE_MCRYPT
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
|
|
|
* @link http://www.php.net/mcrypt_module_open
|
|
|
|
* @link http://www.php.net/mcrypt_list_algorithms
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::setupMcrypt()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var string
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $cipher_name_mcrypt;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2014-11-29 14:39:21 +01:00
|
|
|
/**
|
|
|
|
* The openssl specific name of the cipher
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* Only used if $engine == self::ENGINE_OPENSSL
|
2014-11-29 14:39:21 +01:00
|
|
|
*
|
|
|
|
* @link http://www.php.net/openssl-get-cipher-methods
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var string
|
2014-11-29 14:39:21 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $cipher_name_openssl;
|
2015-04-03 05:39:47 +02:00
|
|
|
|
2014-11-29 14:39:21 +01:00
|
|
|
/**
|
|
|
|
* The openssl specific name of the cipher in ECB mode
|
|
|
|
*
|
|
|
|
* If OpenSSL does not support the mode we're trying to use (CTR)
|
|
|
|
* it can still be emulated with ECB mode.
|
|
|
|
*
|
|
|
|
* @link http://www.php.net/openssl-get-cipher-methods
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var string
|
2014-11-29 14:39:21 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $cipher_name_openssl_ecb;
|
2014-11-29 14:39:21 +01:00
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
|
|
|
* The default salt used by setPassword()
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::setPassword()
|
|
|
|
* @var string
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-09 22:51:31 +01:00
|
|
|
private $password_default_salt = 'phpseclib/salt';
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the performance-optimized callback function
|
|
|
|
*
|
|
|
|
* Used by encrypt() / decrypt()
|
2014-12-09 19:46:30 +01:00
|
|
|
* only if $engine == self::ENGINE_INTERNAL
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::decrypt()
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::setupInlineCrypt()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::$use_inline_crypt
|
2013-05-20 08:19:38 +02:00
|
|
|
* @var Callback
|
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $inline_crypt;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds whether performance-optimized $inline_crypt() can/should be used.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::decrypt()
|
|
|
|
* @see self::inline_crypt
|
2013-05-20 08:19:38 +02:00
|
|
|
* @var mixed
|
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $use_inline_crypt;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2014-11-29 14:39:21 +01:00
|
|
|
/**
|
|
|
|
* If OpenSSL can be used in ECB but not in CTR we can emulate CTR
|
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::openssl_ctr_process()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var bool
|
2014-11-29 14:39:21 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $openssl_emulate_ctr = false;
|
2014-11-29 16:48:45 +01:00
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
2016-04-30 23:23:35 +02:00
|
|
|
* Don't truncate / null pad key
|
2016-04-10 18:30:59 +02:00
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::clearBuffers()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var bool
|
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $skip_key_adjustment = false;
|
2016-04-10 18:30:59 +02:00
|
|
|
|
|
|
|
/**
|
2016-04-30 23:23:35 +02:00
|
|
|
* Has the key length explicitly been set or should it be derived from the key, itself?
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* @see self::setKeyLength()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var bool
|
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $explicit_key_length = false;
|
2016-04-10 18:30:59 +02:00
|
|
|
|
2018-12-27 15:31:35 +01:00
|
|
|
/**
|
|
|
|
* Hash subkey for GHASH
|
|
|
|
*
|
|
|
|
* @see self::setupGCM()
|
|
|
|
* @see self::ghash()
|
|
|
|
* @var BinaryField\Integer
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private $h;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Additional authenticated data
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
protected $aad = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authentication Tag produced after a round of encryption
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
protected $newtag = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authentication Tag to be verified during decryption
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
protected $oldtag = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GCM Binary Field
|
|
|
|
*
|
|
|
|
* @see self::initialize_static_variables()
|
|
|
|
* @see self::ghash()
|
|
|
|
* @var BinaryField
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private static $gcmField;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Original Initialization Vector
|
|
|
|
*
|
|
|
|
* GCM uses the nonce to build the IV but we want to be able to distinguish between nonce-derived
|
|
|
|
* IV's and user-set IV's
|
|
|
|
*
|
|
|
|
* @see self::setIV()
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private $origIV = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nonce
|
|
|
|
*
|
|
|
|
* Only used with GCM. We could re-use setIV() but nonce's can be of a different length and
|
|
|
|
* toggling between GCM and other modes could be more complicated if we re-used setIV()
|
|
|
|
*
|
|
|
|
* @see self::setNonce()
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
protected $nonce = false;
|
|
|
|
|
2016-04-10 18:30:59 +02:00
|
|
|
/**
|
|
|
|
* Default Constructor.
|
2016-04-10 18:25:31 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* $mode could be:
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - ecb
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - cbc
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - ctr
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - cfb
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2018-12-27 15:31:35 +01:00
|
|
|
* - cfb8
|
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - ofb
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2018-12-27 15:31:35 +01:00
|
|
|
* - gcm
|
|
|
|
*
|
2017-10-05 11:46:59 +02:00
|
|
|
* @param string $mode
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access public
|
2016-04-30 23:23:35 +02:00
|
|
|
* @throws \InvalidArgumentException if an invalid / unsupported mode is provided
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function __construct($mode)
|
2013-05-20 08:19:38 +02:00
|
|
|
{
|
2018-12-27 15:31:35 +01:00
|
|
|
if (!isset(self::$gcmField)) {
|
|
|
|
self::initialize_static_variables();
|
|
|
|
}
|
|
|
|
|
2017-06-28 05:34:36 +02:00
|
|
|
$mode = strtolower($mode);
|
2018-03-03 13:07:14 +01:00
|
|
|
// necessary because of 5.6 compatibility; we can't do isset(self::MODE_MAP[$mode]) in 5.6
|
2017-06-28 05:34:36 +02:00
|
|
|
$map = self::MODE_MAP;
|
|
|
|
if (!isset($map[$mode])) {
|
|
|
|
throw new \InvalidArgumentException('No valid mode has been specified');
|
|
|
|
}
|
|
|
|
|
|
|
|
$mode = self::MODE_MAP[$mode];
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
// $mode dependent settings
|
|
|
|
switch ($mode) {
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_ECB:
|
2016-04-30 23:23:35 +02:00
|
|
|
case self::MODE_CBC:
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->paddable = true;
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_CTR:
|
|
|
|
case self::MODE_CFB:
|
2017-10-04 12:30:53 +02:00
|
|
|
case self::MODE_CFB8:
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_OFB:
|
|
|
|
case self::MODE_STREAM:
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->paddable = false;
|
2013-05-20 08:19:38 +02:00
|
|
|
break;
|
2018-12-27 15:31:35 +01:00
|
|
|
case self::MODE_GCM:
|
|
|
|
if ($this->block_size != 16) {
|
|
|
|
throw new \InvalidArgumentException('GCM is only valid for block ciphers with a block size of 128 bits');
|
|
|
|
}
|
|
|
|
$this->paddable = false;
|
|
|
|
break;
|
2013-05-20 08:19:38 +02:00
|
|
|
default:
|
2016-04-30 23:23:35 +02:00
|
|
|
throw new \InvalidArgumentException('No valid mode has been specified');
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->mode = $mode;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2018-12-27 15:31:35 +01:00
|
|
|
/**
|
|
|
|
* Initialize static variables
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private static function initialize_static_variables()
|
|
|
|
{
|
|
|
|
self::$gcmField = new BinaryField(128, 7, 2, 1, 0);
|
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
2016-04-30 23:23:35 +02:00
|
|
|
* Sets the initialization vector.
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2018-12-27 15:31:35 +01:00
|
|
|
* setIV() is not required when ecb or gcm modes are being used.
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $iv
|
2016-04-30 23:23:35 +02:00
|
|
|
* @throws \LengthException if the IV length isn't equal to the block size
|
|
|
|
* @throws \InvalidArgumentException if an IV is provided when one shouldn't be
|
2014-12-13 21:20:25 +01:00
|
|
|
* @internal Can be overwritten by a sub class, but does not have to be
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function setIV($iv)
|
2013-05-20 08:19:38 +02:00
|
|
|
{
|
2016-01-25 20:03:02 +01:00
|
|
|
if ($this->mode == self::MODE_ECB) {
|
2016-04-30 23:23:35 +02:00
|
|
|
throw new \InvalidArgumentException('This mode does not require an IV.');
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:31:35 +01:00
|
|
|
if ($this->mode == self::MODE_GCM) {
|
|
|
|
throw new \InvalidArgumentException('Use setNonce instead');
|
|
|
|
}
|
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
if (!$this->usesIV()) {
|
2016-04-30 23:23:35 +02:00
|
|
|
throw new \InvalidArgumentException('This algorithm does not use an IV.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($iv) != $this->block_size) {
|
|
|
|
throw new \LengthException('Received initialization vector of size ' . strlen($iv) . ', but size ' . $this->block_size . ' is required');
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2018-12-27 15:31:35 +01:00
|
|
|
$this->iv = $this->origIV = $iv;
|
|
|
|
$this->changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the nonce.
|
|
|
|
*
|
|
|
|
* setNonce() is only required when gcm is used
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $nonce
|
|
|
|
* @throws \RuntimeException if an IV is provided when one shouldn't be
|
|
|
|
*/
|
|
|
|
public function setNonce($nonce)
|
|
|
|
{
|
|
|
|
if ($this->mode != self::MODE_GCM) {
|
|
|
|
throw new \RuntimeException('Nonces are only used in GCM mode.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->nonce = $nonce;
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->changed = true;
|
2018-12-27 15:31:35 +01:00
|
|
|
$this->setEngine();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets additional authenticated data
|
|
|
|
*
|
|
|
|
* setAAD() is only used by gcm
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $aad
|
|
|
|
* @throws \RuntimeException if mode isn't GCM
|
|
|
|
*/
|
|
|
|
public function setAAD($aad)
|
|
|
|
{
|
|
|
|
if ($this->mode != self::MODE_GCM) {
|
|
|
|
throw new \RuntimeException('Additional authenticated data is only utilized in GCM mode');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->aad = $aad;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2016-04-10 18:30:59 +02:00
|
|
|
/**
|
2016-04-30 23:23:35 +02:00
|
|
|
* Returns whether or not the algorithm uses an IV
|
2016-04-10 18:30:59 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2016-04-30 23:23:35 +02:00
|
|
|
* @return bool
|
2016-04-10 18:30:59 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function usesIV()
|
2016-04-10 18:30:59 +02:00
|
|
|
{
|
2018-12-27 15:31:35 +01:00
|
|
|
return $this->mode != self::MODE_GCM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the algorithm uses a nonce
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function usesNonce()
|
|
|
|
{
|
|
|
|
return $this->mode == self::MODE_GCM;
|
2016-04-10 18:30:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current key length in bits
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return int
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function getKeyLength()
|
2016-04-10 18:30:59 +02:00
|
|
|
{
|
|
|
|
return $this->key_length << 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current block length in bits
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return int
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function getBlockLength()
|
2016-04-10 18:30:59 +02:00
|
|
|
{
|
|
|
|
return $this->block_size << 3;
|
|
|
|
}
|
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
/**
|
|
|
|
* Returns the current block length in bytes
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getBlockLengthInBytes()
|
|
|
|
{
|
|
|
|
return $this->block_size;
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
/**
|
|
|
|
* Sets the key length.
|
|
|
|
*
|
|
|
|
* Keys with explicitly set lengths need to be treated accordingly
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param int $length
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function setKeyLength($length)
|
2016-04-30 23:23:35 +02:00
|
|
|
{
|
|
|
|
$this->explicit_key_length = $length >> 3;
|
|
|
|
|
|
|
|
if (is_string($this->key) && strlen($this->key) != $this->explicit_key_length) {
|
|
|
|
$this->key = false;
|
|
|
|
throw new \LengthException('Key has already been set and is not ' .$this->explicit_key_length . ' bytes long');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
|
|
|
* Sets the key.
|
|
|
|
*
|
|
|
|
* The min/max length(s) of the key depends on the cipher which is used.
|
|
|
|
* If the key not fits the length(s) of the cipher it will paded with null bytes
|
|
|
|
* up to the closest valid key length. If the key is more than max length,
|
|
|
|
* we trim the excess bits.
|
|
|
|
*
|
|
|
|
* If the key is not explicitly set, it'll be assumed to be all null bytes.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $key
|
2014-01-31 00:13:14 +01:00
|
|
|
* @internal Could, but not must, extend by the child Crypt_* class
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function setKey($key)
|
2013-05-20 08:19:38 +02:00
|
|
|
{
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($this->explicit_key_length !== false && strlen($key) != $this->explicit_key_length) {
|
|
|
|
throw new \LengthException('Key length has already been set to ' . $this->explicit_key_length . ' bytes and this key is ' . strlen($key) . ' bytes');
|
2016-04-10 18:30:59 +02:00
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->key = $key;
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->key_length = strlen($key);
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->changed = true;
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->setEngine();
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the password.
|
|
|
|
*
|
|
|
|
* Depending on what $method is set to, setPassword()'s (optional) parameters are as follows:
|
2014-06-13 15:54:30 +02:00
|
|
|
* {@link http://en.wikipedia.org/wiki/PBKDF2 pbkdf2} or pbkdf1:
|
2013-05-20 08:19:38 +02:00
|
|
|
* $hash, $salt, $count, $dkLen
|
|
|
|
*
|
2013-06-02 11:21:32 +02:00
|
|
|
* Where $hash (default = sha1) currently supports the following hashes: see: Crypt/Hash.php
|
|
|
|
*
|
|
|
|
* @see Crypt/Hash.php
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $password
|
|
|
|
* @param string $method
|
2017-11-21 09:36:28 +01:00
|
|
|
* @param $func_args[]
|
2016-04-30 23:23:35 +02:00
|
|
|
* @throws \LengthException if pbkdf1 is being used and the derived key length exceeds the hash length
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return bool
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access public
|
2014-01-31 00:13:14 +01:00
|
|
|
* @internal Could, but not must, extend by the child Crypt_* class
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2017-11-21 09:36:28 +01:00
|
|
|
public function setPassword($password, $method = 'pbkdf2', ...$func_args)
|
2013-05-20 08:19:38 +02:00
|
|
|
{
|
|
|
|
$key = '';
|
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
$method = strtolower($method);
|
2013-05-20 08:19:38 +02:00
|
|
|
switch ($method) {
|
2016-10-19 14:45:42 +02:00
|
|
|
case 'pkcs12': // from https://tools.ietf.org/html/rfc7292#appendix-B.2
|
|
|
|
case 'pbkdf1':
|
|
|
|
case 'pbkdf2':
|
2013-05-20 08:19:38 +02:00
|
|
|
// Hash function
|
2017-11-21 09:36:28 +01:00
|
|
|
$hash = isset($func_args[0]) ? strtolower($func_args[0]) : 'sha1';
|
2016-10-19 14:45:42 +02:00
|
|
|
$hashObj = new Hash();
|
|
|
|
$hashObj->setHash($hash);
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
// WPA and WPA2 use the SSID as the salt
|
2017-11-21 09:36:28 +01:00
|
|
|
$salt = isset($func_args[1]) ? $func_args[1] : $this->password_default_salt;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
// RFC2898#section-4.2 uses 1,000 iterations by default
|
|
|
|
// WPA and WPA2 use 4,096.
|
2017-11-21 09:36:28 +01:00
|
|
|
$count = isset($func_args[2]) ? $func_args[2] : 1000;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
// Keylength
|
2017-11-21 09:36:28 +01:00
|
|
|
if (isset($func_args[3])) {
|
2018-10-13 15:18:47 +02:00
|
|
|
if ($func_args[3] <= 0) {
|
|
|
|
throw new \LengthException('Derived key length cannot be longer 0 or less');
|
|
|
|
}
|
2017-11-21 09:36:28 +01:00
|
|
|
$dkLen = $func_args[3];
|
2014-06-16 16:57:04 +02:00
|
|
|
} else {
|
2016-04-30 23:23:35 +02:00
|
|
|
$key_length = $this->explicit_key_length !== false ? $this->explicit_key_length : $this->key_length;
|
|
|
|
$dkLen = $method == 'pbkdf1' ? 2 * $key_length : $key_length;
|
2014-06-13 17:39:57 +02:00
|
|
|
}
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2013-06-02 11:21:32 +02:00
|
|
|
switch (true) {
|
2016-10-19 14:45:42 +02:00
|
|
|
case $method == 'pkcs12':
|
|
|
|
/*
|
|
|
|
In this specification, however, all passwords are created from
|
|
|
|
BMPStrings with a NULL terminator. This means that each character in
|
|
|
|
the original BMPString is encoded in 2 bytes in big-endian format
|
|
|
|
(most-significant byte first). There are no Unicode byte order
|
|
|
|
marks. The 2 bytes produced from the last character in the BMPString
|
|
|
|
are followed by 2 additional bytes with the value 0x00.
|
|
|
|
|
|
|
|
-- https://tools.ietf.org/html/rfc7292#appendix-B.1
|
|
|
|
*/
|
|
|
|
$password = "\0". chunk_split($password, 1, "\0") . "\0";
|
|
|
|
|
|
|
|
/*
|
|
|
|
This standard specifies 3 different values for the ID byte mentioned
|
|
|
|
above:
|
|
|
|
|
|
|
|
1. If ID=1, then the pseudorandom bits being produced are to be used
|
|
|
|
as key material for performing encryption or decryption.
|
|
|
|
|
|
|
|
2. If ID=2, then the pseudorandom bits being produced are to be used
|
|
|
|
as an IV (Initial Value) for encryption or decryption.
|
|
|
|
|
|
|
|
3. If ID=3, then the pseudorandom bits being produced are to be used
|
|
|
|
as an integrity key for MACing.
|
|
|
|
*/
|
|
|
|
// Construct a string, D (the "diversifier"), by concatenating v/8
|
|
|
|
// copies of ID.
|
|
|
|
$blockLength = $hashObj->getBlockLengthInBytes();
|
|
|
|
$d1 = str_repeat(chr(1), $blockLength);
|
|
|
|
$d2 = str_repeat(chr(2), $blockLength);
|
|
|
|
$s = '';
|
|
|
|
if (strlen($salt)) {
|
|
|
|
while (strlen($s) < $blockLength) {
|
|
|
|
$s.= $salt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$s = substr($s, 0, $blockLength);
|
|
|
|
|
|
|
|
$p = '';
|
|
|
|
if (strlen($password)) {
|
|
|
|
while (strlen($p) < $blockLength) {
|
|
|
|
$p.= $password;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$p = substr($p, 0, $blockLength);
|
|
|
|
|
|
|
|
$i = $s . $p;
|
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->setKey(self::pkcs12helper($dkLen, $hashObj, $i, $d1, $count));
|
2016-10-19 14:45:42 +02:00
|
|
|
if ($this->usesIV()) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->setIV(self::pkcs12helper($this->block_size, $hashObj, $i, $d2, $count));
|
2016-10-19 14:45:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-06-13 15:54:30 +02:00
|
|
|
case $method == 'pbkdf1':
|
2016-10-19 14:45:42 +02:00
|
|
|
if ($dkLen > $hashObj->getLengthInBytes()) {
|
2016-04-30 23:23:35 +02:00
|
|
|
throw new \LengthException('Derived key length cannot be longer than the hash length');
|
2014-06-13 15:54:30 +02:00
|
|
|
}
|
|
|
|
$t = $password . $salt;
|
|
|
|
for ($i = 0; $i < $count; ++$i) {
|
|
|
|
$t = $hashObj->hash($t);
|
|
|
|
}
|
|
|
|
$key = substr($t, 0, $dkLen);
|
|
|
|
|
|
|
|
$this->setKey(substr($key, 0, $dkLen >> 1));
|
2016-10-19 14:45:42 +02:00
|
|
|
if ($this->usesIV()) {
|
|
|
|
$this->setIV(substr($key, $dkLen >> 1));
|
|
|
|
}
|
2014-06-13 15:54:30 +02:00
|
|
|
|
|
|
|
return true;
|
2013-06-02 11:21:32 +02:00
|
|
|
case !in_array($hash, hash_algos()):
|
|
|
|
$i = 1;
|
2016-10-19 14:45:42 +02:00
|
|
|
$hashObj->setKey($password);
|
2013-06-02 11:21:32 +02:00
|
|
|
while (strlen($key) < $dkLen) {
|
2016-10-19 14:45:42 +02:00
|
|
|
$f = $u = $hashObj->hash($salt . pack('N', $i++));
|
2013-06-02 11:21:32 +02:00
|
|
|
for ($j = 2; $j <= $count; ++$j) {
|
2016-10-19 14:45:42 +02:00
|
|
|
$u = $hashObj->hash($u);
|
2013-06-02 11:21:32 +02:00
|
|
|
$f^= $u;
|
|
|
|
}
|
|
|
|
$key.= $f;
|
|
|
|
}
|
|
|
|
$key = substr($key, 0, $dkLen);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$key = hash_pbkdf2($hash, $password, $salt, $count, $dkLen, true);
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2016-10-19 14:45:42 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException($method . ' is not a supported password hashing method');
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2013-06-02 11:21:32 +02:00
|
|
|
$this->setKey($key);
|
2014-06-13 15:54:30 +02:00
|
|
|
|
|
|
|
return true;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2016-10-19 14:45:42 +02:00
|
|
|
/**
|
|
|
|
* PKCS#12 KDF Helper Function
|
|
|
|
*
|
|
|
|
* As discussed here:
|
|
|
|
*
|
|
|
|
* {@link https://tools.ietf.org/html/rfc7292#appendix-B}
|
|
|
|
*
|
|
|
|
* @see self::setPassword()
|
|
|
|
* @access private
|
|
|
|
* @param int $n
|
|
|
|
* @param \phpseclib\Crypt\Hash $hashObj
|
|
|
|
* @param string $i
|
|
|
|
* @param string $d
|
|
|
|
* @param int $count
|
|
|
|
* @return string $a
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private static function pkcs12helper($n, $hashObj, $i, $d, $count)
|
2016-10-19 14:45:42 +02:00
|
|
|
{
|
|
|
|
static $one;
|
|
|
|
if (!isset($one)) {
|
|
|
|
$one = new BigInteger(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$blockLength = $hashObj->getBlockLength() >> 3;
|
|
|
|
|
|
|
|
$c = ceil($n / $hashObj->getLengthInBytes());
|
|
|
|
$a = '';
|
|
|
|
for ($j = 1; $j <= $c; $j++) {
|
|
|
|
$ai = $d . $i;
|
|
|
|
for ($k = 0; $k < $count; $k++) {
|
|
|
|
$ai = $hashObj->hash($ai);
|
|
|
|
}
|
|
|
|
$b = '';
|
|
|
|
while (strlen($b) < $blockLength) {
|
|
|
|
$b.= $ai;
|
|
|
|
}
|
|
|
|
$b = substr($b, 0, $blockLength);
|
|
|
|
$b = new BigInteger($b, 256);
|
|
|
|
$newi = '';
|
|
|
|
for ($k = 0; $k < strlen($i); $k+= $blockLength) {
|
|
|
|
$temp = substr($i, $k, $blockLength);
|
|
|
|
$temp = new BigInteger($temp, 256);
|
|
|
|
$temp->setPrecision($blockLength << 3);
|
|
|
|
$temp = $temp->add($b);
|
|
|
|
$temp = $temp->add($one);
|
|
|
|
$newi.= $temp->toBytes(false);
|
|
|
|
}
|
|
|
|
$i = $newi;
|
|
|
|
$a.= $ai;
|
|
|
|
}
|
|
|
|
|
|
|
|
return substr($a, 0, $n);
|
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
|
|
|
* Encrypts a message.
|
|
|
|
*
|
|
|
|
* $plaintext will be padded with additional bytes such that it's length is a multiple of the block size. Other cipher
|
|
|
|
* implementations may or may not pad in the same manner. Other common approaches to padding and the reasons why it's
|
|
|
|
* necessary are discussed in the following
|
|
|
|
* URL:
|
|
|
|
*
|
|
|
|
* {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
|
|
|
|
*
|
|
|
|
* An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
|
|
|
|
* strlen($plaintext) will still need to be a multiple of the block size, however, arbitrary values can be added to make it that
|
|
|
|
* length.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::decrypt()
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $plaintext
|
|
|
|
* @return string $ciphertext
|
2014-01-31 00:13:14 +01:00
|
|
|
* @internal Could, but not must, extend by the child Crypt_* class
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function encrypt($plaintext)
|
2013-05-20 08:19:38 +02:00
|
|
|
{
|
2014-11-29 14:39:21 +01:00
|
|
|
if ($this->paddable) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$plaintext = $this->pad($plaintext);
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
|
|
|
|
2018-12-27 15:31:35 +01:00
|
|
|
if ($this->mode == self::MODE_GCM) {
|
|
|
|
if ($this->changed) {
|
|
|
|
$this->genericSetup();
|
|
|
|
$this->changed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$oldIV = $this->iv;
|
|
|
|
Strings::increment_str($this->iv);
|
|
|
|
$cipher = new static('ctr');
|
|
|
|
$cipher->setKey($this->key);
|
|
|
|
$cipher->setIV($this->iv);
|
|
|
|
$ciphertext = $cipher->encrypt($plaintext);
|
|
|
|
|
|
|
|
$s = $this->ghash(
|
|
|
|
self::nullPad128($this->aad) .
|
|
|
|
self::nullPad128($ciphertext) .
|
|
|
|
self::len64($this->aad) .
|
|
|
|
self::len64($ciphertext)
|
|
|
|
);
|
|
|
|
$cipher->encryptIV = $this->iv = $this->encryptIV = $this->decryptIV = $oldIV;
|
|
|
|
$this->newtag = $cipher->encrypt($s);
|
|
|
|
return $ciphertext;
|
|
|
|
}
|
|
|
|
|
2015-04-02 12:32:31 +02:00
|
|
|
if ($this->engine === self::ENGINE_OPENSSL) {
|
2014-11-29 14:39:21 +01:00
|
|
|
if ($this->changed) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->clearBuffers();
|
2014-11-29 14:39:21 +01:00
|
|
|
$this->changed = false;
|
|
|
|
}
|
|
|
|
switch ($this->mode) {
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_STREAM:
|
2016-12-03 21:39:11 +01:00
|
|
|
return openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_ECB:
|
2016-12-03 21:39:11 +01:00
|
|
|
return openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_CBC:
|
2016-12-03 21:39:11 +01:00
|
|
|
$result = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $this->encryptIV);
|
2014-11-29 14:39:21 +01:00
|
|
|
if ($this->continuousBuffer) {
|
2015-02-01 02:11:32 +01:00
|
|
|
$this->encryptIV = substr($result, -$this->block_size);
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
2016-04-10 18:30:59 +02:00
|
|
|
return $result;
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_CTR:
|
2016-12-03 21:39:11 +01:00
|
|
|
return $this->openssl_ctr_process($plaintext, $this->encryptIV, $this->enbuffer);
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_CFB:
|
2014-11-29 14:39:21 +01:00
|
|
|
// cfb loosely routines inspired by openssl's:
|
|
|
|
// {@link http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1}
|
2014-11-29 23:18:17 +01:00
|
|
|
$ciphertext = '';
|
2014-11-29 14:39:21 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$iv = &$this->encryptIV;
|
|
|
|
$pos = &$this->enbuffer['pos'];
|
|
|
|
} else {
|
2015-01-01 07:27:30 +01:00
|
|
|
$iv = $this->encryptIV;
|
2014-11-29 14:39:21 +01:00
|
|
|
$pos = 0;
|
|
|
|
}
|
|
|
|
$len = strlen($plaintext);
|
|
|
|
$i = 0;
|
|
|
|
if ($pos) {
|
|
|
|
$orig_pos = $pos;
|
|
|
|
$max = $this->block_size - $pos;
|
|
|
|
if ($len >= $max) {
|
|
|
|
$i = $max;
|
|
|
|
$len-= $max;
|
|
|
|
$pos = 0;
|
|
|
|
} else {
|
|
|
|
$i = $len;
|
|
|
|
$pos+= $len;
|
|
|
|
$len = 0;
|
|
|
|
}
|
|
|
|
// ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize
|
|
|
|
$ciphertext = substr($iv, $orig_pos) ^ $plaintext;
|
|
|
|
$iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
|
|
|
|
$plaintext = substr($plaintext, $i);
|
|
|
|
}
|
|
|
|
|
|
|
|
$overflow = $len % $this->block_size;
|
|
|
|
|
|
|
|
if ($overflow) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$ciphertext.= openssl_encrypt(substr($plaintext, 0, -$overflow) . str_repeat("\0", $this->block_size), $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
|
|
|
|
$iv = Strings::pop($ciphertext, $this->block_size);
|
2014-11-29 14:39:21 +01:00
|
|
|
|
|
|
|
$size = $len - $overflow;
|
|
|
|
$block = $iv ^ substr($plaintext, -$overflow);
|
|
|
|
$iv = substr_replace($iv, $block, 0, $overflow);
|
|
|
|
$ciphertext.= $block;
|
|
|
|
$pos = $overflow;
|
2015-07-15 03:52:31 +02:00
|
|
|
} elseif ($len) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$ciphertext = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
|
2014-11-29 14:39:21 +01:00
|
|
|
$iv = substr($ciphertext, -$this->block_size);
|
|
|
|
}
|
|
|
|
|
2017-10-04 12:30:53 +02:00
|
|
|
return $ciphertext;
|
|
|
|
case self::MODE_CFB8:
|
|
|
|
$ciphertext = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $this->encryptIV);
|
|
|
|
if ($this->continuousBuffer) {
|
2017-10-10 12:02:55 +02:00
|
|
|
if (($len = strlen($ciphertext)) >= $this->block_size) {
|
|
|
|
$this->encryptIV = substr($ciphertext, -$this->block_size);
|
|
|
|
} else {
|
|
|
|
$this->encryptIV = substr($this->encryptIV, $len - $this->block_size) . substr($ciphertext, -$len);
|
|
|
|
}
|
2017-10-04 12:30:53 +02:00
|
|
|
}
|
2014-11-29 14:39:21 +01:00
|
|
|
return $ciphertext;
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_OFB:
|
2016-12-03 21:39:11 +01:00
|
|
|
return $this->openssl_ofb_process($plaintext, $this->encryptIV, $this->enbuffer);
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 12:32:31 +02:00
|
|
|
if ($this->engine === self::ENGINE_MCRYPT) {
|
2013-05-20 08:19:38 +02:00
|
|
|
if ($this->changed) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->setupMcrypt();
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->changed = false;
|
|
|
|
}
|
|
|
|
if ($this->enchanged) {
|
2016-12-03 21:39:11 +01:00
|
|
|
@mcrypt_generic_init($this->enmcrypt, $this->key, $this->getIV($this->encryptIV));
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->enchanged = false;
|
|
|
|
}
|
|
|
|
|
2013-05-25 06:22:25 +02:00
|
|
|
// re: {@link http://phpseclib.sourceforge.net/cfb-demo.phps}
|
2013-05-20 08:19:38 +02:00
|
|
|
// using mcrypt's default handing of CFB the above would output two different things. using phpseclib's
|
|
|
|
// rewritten CFB implementation the above outputs the same thing twice.
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($this->mode == self::MODE_CFB && $this->continuousBuffer) {
|
2013-05-20 08:19:38 +02:00
|
|
|
$block_size = $this->block_size;
|
|
|
|
$iv = &$this->encryptIV;
|
|
|
|
$pos = &$this->enbuffer['pos'];
|
|
|
|
$len = strlen($plaintext);
|
|
|
|
$ciphertext = '';
|
|
|
|
$i = 0;
|
|
|
|
if ($pos) {
|
|
|
|
$orig_pos = $pos;
|
|
|
|
$max = $block_size - $pos;
|
|
|
|
if ($len >= $max) {
|
|
|
|
$i = $max;
|
|
|
|
$len-= $max;
|
|
|
|
$pos = 0;
|
|
|
|
} else {
|
|
|
|
$i = $len;
|
|
|
|
$pos+= $len;
|
|
|
|
$len = 0;
|
|
|
|
}
|
|
|
|
$ciphertext = substr($iv, $orig_pos) ^ $plaintext;
|
|
|
|
$iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
|
|
|
|
$this->enbuffer['enmcrypt_init'] = true;
|
|
|
|
}
|
|
|
|
if ($len >= $block_size) {
|
|
|
|
if ($this->enbuffer['enmcrypt_init'] === false || $len > $this->cfb_init_len) {
|
|
|
|
if ($this->enbuffer['enmcrypt_init'] === true) {
|
2016-09-21 08:03:26 +02:00
|
|
|
@mcrypt_generic_init($this->enmcrypt, $this->key, $iv);
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->enbuffer['enmcrypt_init'] = false;
|
|
|
|
}
|
2016-09-21 08:03:26 +02:00
|
|
|
$ciphertext.= @mcrypt_generic($this->enmcrypt, substr($plaintext, $i, $len - $len % $block_size));
|
2013-05-20 08:19:38 +02:00
|
|
|
$iv = substr($ciphertext, -$block_size);
|
|
|
|
$len%= $block_size;
|
|
|
|
} else {
|
|
|
|
while ($len >= $block_size) {
|
2016-09-21 08:03:26 +02:00
|
|
|
$iv = @mcrypt_generic($this->ecb, $iv) ^ substr($plaintext, $i, $block_size);
|
2013-05-20 08:19:38 +02:00
|
|
|
$ciphertext.= $iv;
|
|
|
|
$len-= $block_size;
|
|
|
|
$i+= $block_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($len) {
|
2016-09-21 08:03:26 +02:00
|
|
|
$iv = @mcrypt_generic($this->ecb, $iv);
|
2013-05-20 08:19:38 +02:00
|
|
|
$block = $iv ^ substr($plaintext, -$len);
|
|
|
|
$iv = substr_replace($iv, $block, 0, $len);
|
|
|
|
$ciphertext.= $block;
|
|
|
|
$pos = $len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ciphertext;
|
|
|
|
}
|
|
|
|
|
2016-09-21 08:03:26 +02:00
|
|
|
$ciphertext = @mcrypt_generic($this->enmcrypt, $plaintext);
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
if (!$this->continuousBuffer) {
|
2016-12-03 21:39:11 +01:00
|
|
|
@mcrypt_generic_init($this->enmcrypt, $this->key, $this->getIV($this->encryptIV));
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $ciphertext;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->changed) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->setup();
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->changed = false;
|
|
|
|
}
|
2016-12-08 16:20:19 +01:00
|
|
|
if ($this->engine === self::ENGINE_EVAL) {
|
2013-05-20 08:19:38 +02:00
|
|
|
$inline = $this->inline_crypt;
|
2016-12-03 21:39:11 +01:00
|
|
|
return $inline('encrypt', $plaintext);
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$buffer = &$this->enbuffer;
|
|
|
|
$block_size = $this->block_size;
|
|
|
|
$ciphertext = '';
|
|
|
|
switch ($this->mode) {
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_ECB:
|
2013-05-20 08:19:38 +02:00
|
|
|
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$ciphertext.= $this->encryptBlock(substr($plaintext, $i, $block_size));
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_CBC:
|
2013-05-20 08:19:38 +02:00
|
|
|
$xor = $this->encryptIV;
|
|
|
|
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
|
|
|
|
$block = substr($plaintext, $i, $block_size);
|
2016-12-03 21:39:11 +01:00
|
|
|
$block = $this->encryptBlock($block ^ $xor);
|
2013-05-20 08:19:38 +02:00
|
|
|
$xor = $block;
|
|
|
|
$ciphertext.= $block;
|
|
|
|
}
|
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$this->encryptIV = $xor;
|
|
|
|
}
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_CTR:
|
2013-05-20 08:19:38 +02:00
|
|
|
$xor = $this->encryptIV;
|
2014-11-29 23:18:17 +01:00
|
|
|
if (strlen($buffer['ciphertext'])) {
|
2013-05-20 08:19:38 +02:00
|
|
|
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
|
|
|
|
$block = substr($plaintext, $i, $block_size);
|
2014-11-29 23:18:17 +01:00
|
|
|
if (strlen($block) > strlen($buffer['ciphertext'])) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$buffer['ciphertext'].= $this->encryptBlock($xor);
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2018-12-27 15:31:35 +01:00
|
|
|
Strings::increment_str($xor);
|
2016-07-31 05:18:06 +02:00
|
|
|
$key = Strings::shift($buffer['ciphertext'], $block_size);
|
2013-05-20 08:19:38 +02:00
|
|
|
$ciphertext.= $block ^ $key;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
|
|
|
|
$block = substr($plaintext, $i, $block_size);
|
2016-12-03 21:39:11 +01:00
|
|
|
$key = $this->encryptBlock($xor);
|
2018-12-27 15:31:35 +01:00
|
|
|
Strings::increment_str($xor);
|
2013-05-20 08:19:38 +02:00
|
|
|
$ciphertext.= $block ^ $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$this->encryptIV = $xor;
|
|
|
|
if ($start = strlen($plaintext) % $block_size) {
|
2014-11-29 23:18:17 +01:00
|
|
|
$buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_CFB:
|
2013-05-20 08:19:38 +02:00
|
|
|
// cfb loosely routines inspired by openssl's:
|
2013-05-25 06:22:25 +02:00
|
|
|
// {@link http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1}
|
2013-05-20 08:19:38 +02:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$iv = &$this->encryptIV;
|
|
|
|
$pos = &$buffer['pos'];
|
|
|
|
} else {
|
|
|
|
$iv = $this->encryptIV;
|
|
|
|
$pos = 0;
|
|
|
|
}
|
|
|
|
$len = strlen($plaintext);
|
|
|
|
$i = 0;
|
|
|
|
if ($pos) {
|
|
|
|
$orig_pos = $pos;
|
|
|
|
$max = $block_size - $pos;
|
|
|
|
if ($len >= $max) {
|
|
|
|
$i = $max;
|
|
|
|
$len-= $max;
|
|
|
|
$pos = 0;
|
|
|
|
} else {
|
|
|
|
$i = $len;
|
|
|
|
$pos+= $len;
|
|
|
|
$len = 0;
|
|
|
|
}
|
|
|
|
// ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize
|
|
|
|
$ciphertext = substr($iv, $orig_pos) ^ $plaintext;
|
|
|
|
$iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
|
|
|
|
}
|
|
|
|
while ($len >= $block_size) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$iv = $this->encryptBlock($iv) ^ substr($plaintext, $i, $block_size);
|
2013-05-20 08:19:38 +02:00
|
|
|
$ciphertext.= $iv;
|
|
|
|
$len-= $block_size;
|
|
|
|
$i+= $block_size;
|
|
|
|
}
|
|
|
|
if ($len) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$iv = $this->encryptBlock($iv);
|
2013-05-20 08:19:38 +02:00
|
|
|
$block = $iv ^ substr($plaintext, $i);
|
|
|
|
$iv = substr_replace($iv, $block, 0, $len);
|
|
|
|
$ciphertext.= $block;
|
|
|
|
$pos = $len;
|
|
|
|
}
|
|
|
|
break;
|
2017-10-04 12:30:53 +02:00
|
|
|
case self::MODE_CFB8:
|
|
|
|
$ciphertext = '';
|
|
|
|
$len = strlen($plaintext);
|
|
|
|
$iv = $this->encryptIV;
|
|
|
|
|
2017-10-10 12:02:55 +02:00
|
|
|
for ($i = 0; $i < $len; ++$i) {
|
2017-10-04 12:30:53 +02:00
|
|
|
$ciphertext .= ($c = $plaintext[$i] ^ $this->encryptBlock($iv));
|
2017-10-10 12:06:14 +02:00
|
|
|
$iv = substr($iv, 1) . $c;
|
2017-10-04 12:30:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->continuousBuffer) {
|
2017-10-10 12:06:14 +02:00
|
|
|
if ($len >= $block_size) {
|
|
|
|
$this->encryptIV = substr($ciphertext, -$block_size);
|
2017-10-04 12:30:53 +02:00
|
|
|
} else {
|
2017-10-10 12:06:14 +02:00
|
|
|
$this->encryptIV = substr($this->encryptIV, $len - $block_size) . substr($ciphertext, -$len);
|
2017-10-04 12:30:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_OFB:
|
2013-05-20 08:19:38 +02:00
|
|
|
$xor = $this->encryptIV;
|
|
|
|
if (strlen($buffer['xor'])) {
|
|
|
|
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
|
|
|
|
$block = substr($plaintext, $i, $block_size);
|
|
|
|
if (strlen($block) > strlen($buffer['xor'])) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$xor = $this->encryptBlock($xor);
|
2013-05-20 08:19:38 +02:00
|
|
|
$buffer['xor'].= $xor;
|
|
|
|
}
|
2016-07-31 05:18:06 +02:00
|
|
|
$key = Strings::shift($buffer['xor'], $block_size);
|
2013-05-20 08:19:38 +02:00
|
|
|
$ciphertext.= $block ^ $key;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$xor = $this->encryptBlock($xor);
|
2013-05-20 08:19:38 +02:00
|
|
|
$ciphertext.= substr($plaintext, $i, $block_size) ^ $xor;
|
|
|
|
}
|
|
|
|
$key = $xor;
|
|
|
|
}
|
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$this->encryptIV = $xor;
|
|
|
|
if ($start = strlen($plaintext) % $block_size) {
|
2016-04-10 18:30:59 +02:00
|
|
|
$buffer['xor'] = substr($key, $start) . $buffer['xor'];
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_STREAM:
|
2016-12-03 21:39:11 +01:00
|
|
|
$ciphertext = $this->encryptBlock($plaintext);
|
2013-05-20 08:19:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ciphertext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrypts a message.
|
|
|
|
*
|
|
|
|
* If strlen($ciphertext) is not a multiple of the block size, null bytes will be added to the end of the string until
|
|
|
|
* it is.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $ciphertext
|
|
|
|
* @return string $plaintext
|
2016-04-30 23:23:35 +02:00
|
|
|
* @throws \LengthException if we're inside a block cipher and the ciphertext length is not a multiple of the block size
|
2014-01-31 00:13:14 +01:00
|
|
|
* @internal Could, but not must, extend by the child Crypt_* class
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function decrypt($ciphertext)
|
2013-05-20 08:19:38 +02:00
|
|
|
{
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($this->paddable && strlen($ciphertext) % $this->block_size) {
|
|
|
|
throw new \LengthException('The ciphertext length (' . strlen($ciphertext) . ') needs to be a multiple of the block size (' . $this->block_size . ')');
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
|
|
|
|
2018-12-27 15:31:35 +01:00
|
|
|
if ($this->mode == self::MODE_GCM) {
|
|
|
|
if ($this->changed) {
|
|
|
|
$this->genericSetup();
|
|
|
|
$this->changed = false;
|
|
|
|
}
|
|
|
|
if ($this->oldtag === false) {
|
|
|
|
throw new \UnexpectedValueException('Authentication Tag has not been set');
|
|
|
|
}
|
|
|
|
|
|
|
|
$oldIV = $this->iv;
|
|
|
|
Strings::increment_str($this->iv);
|
|
|
|
$cipher = new static('ctr');
|
|
|
|
$cipher->setKey($this->key);
|
|
|
|
$cipher->setIV($this->iv);
|
|
|
|
$plaintext = $cipher->decrypt($ciphertext);
|
|
|
|
|
|
|
|
$s = $this->ghash(
|
|
|
|
self::nullPad128($this->aad) .
|
|
|
|
self::nullPad128($ciphertext) .
|
|
|
|
self::len64($this->aad) .
|
|
|
|
self::len64($ciphertext)
|
|
|
|
);
|
|
|
|
$cipher->encryptIV = $this->iv = $this->encryptIV = $this->decryptIV = $oldIV;
|
|
|
|
$newtag = $cipher->encrypt($s);
|
|
|
|
if ($this->oldtag != substr($newtag, 0, strlen($newtag))) {
|
|
|
|
$this->oldtag = false;
|
|
|
|
throw new \UnexpectedValueException('Derived authentication tag and supplied authentication tag do not match');
|
|
|
|
}
|
|
|
|
$this->oldtag = false;
|
|
|
|
return $plaintext;
|
|
|
|
}
|
|
|
|
|
2015-04-02 12:32:31 +02:00
|
|
|
if ($this->engine === self::ENGINE_OPENSSL) {
|
2014-11-29 14:39:21 +01:00
|
|
|
if ($this->changed) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->clearBuffers();
|
2014-11-29 14:39:21 +01:00
|
|
|
$this->changed = false;
|
|
|
|
}
|
|
|
|
switch ($this->mode) {
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_STREAM:
|
2016-12-03 21:39:11 +01:00
|
|
|
$plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
|
2014-12-14 17:40:40 +01:00
|
|
|
break;
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_ECB:
|
2016-12-03 21:39:11 +01:00
|
|
|
$plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
|
2014-11-29 14:39:21 +01:00
|
|
|
break;
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_CBC:
|
2016-12-03 21:39:11 +01:00
|
|
|
$offset = $this->block_size;
|
|
|
|
$plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $this->decryptIV);
|
2014-11-29 14:39:21 +01:00
|
|
|
if ($this->continuousBuffer) {
|
2016-04-10 18:30:59 +02:00
|
|
|
$this->decryptIV = substr($ciphertext, -$offset, $this->block_size);
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
|
|
|
break;
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_CTR:
|
2016-12-03 21:39:11 +01:00
|
|
|
$plaintext = $this->openssl_ctr_process($ciphertext, $this->decryptIV, $this->debuffer);
|
2014-11-29 14:39:21 +01:00
|
|
|
break;
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_CFB:
|
2014-11-29 14:39:21 +01:00
|
|
|
// cfb loosely routines inspired by openssl's:
|
|
|
|
// {@link http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1}
|
2014-11-29 23:18:17 +01:00
|
|
|
$plaintext = '';
|
2014-11-29 14:39:21 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$iv = &$this->decryptIV;
|
|
|
|
$pos = &$this->buffer['pos'];
|
|
|
|
} else {
|
|
|
|
$iv = $this->decryptIV;
|
|
|
|
$pos = 0;
|
|
|
|
}
|
|
|
|
$len = strlen($ciphertext);
|
|
|
|
$i = 0;
|
|
|
|
if ($pos) {
|
|
|
|
$orig_pos = $pos;
|
|
|
|
$max = $this->block_size - $pos;
|
|
|
|
if ($len >= $max) {
|
|
|
|
$i = $max;
|
|
|
|
$len-= $max;
|
|
|
|
$pos = 0;
|
|
|
|
} else {
|
|
|
|
$i = $len;
|
|
|
|
$pos+= $len;
|
|
|
|
$len = 0;
|
|
|
|
}
|
|
|
|
// ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $this->blocksize
|
|
|
|
$plaintext = substr($iv, $orig_pos) ^ $ciphertext;
|
|
|
|
$iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
|
|
|
|
$ciphertext = substr($ciphertext, $i);
|
|
|
|
}
|
|
|
|
$overflow = $len % $this->block_size;
|
|
|
|
if ($overflow) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$plaintext.= openssl_decrypt(substr($ciphertext, 0, -$overflow), $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
|
2014-11-29 14:39:21 +01:00
|
|
|
if ($len - $overflow) {
|
|
|
|
$iv = substr($ciphertext, -$overflow - $this->block_size, -$overflow);
|
|
|
|
}
|
2016-12-03 21:39:11 +01:00
|
|
|
$iv = openssl_encrypt(str_repeat("\0", $this->block_size), $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
|
2014-11-29 14:39:21 +01:00
|
|
|
$plaintext.= $iv ^ substr($ciphertext, -$overflow);
|
|
|
|
$iv = substr_replace($iv, substr($ciphertext, -$overflow), 0, $overflow);
|
|
|
|
$pos = $overflow;
|
2015-07-15 03:52:31 +02:00
|
|
|
} elseif ($len) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$plaintext.= openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
|
2014-11-29 14:39:21 +01:00
|
|
|
$iv = substr($ciphertext, -$this->block_size);
|
|
|
|
}
|
|
|
|
break;
|
2017-10-04 12:30:53 +02:00
|
|
|
case self::MODE_CFB8:
|
|
|
|
$plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $this->decryptIV);
|
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
if (($len = strlen($ciphertext)) >= $this->block_size) {
|
|
|
|
$this->decryptIV = substr($ciphertext, -$this->block_size);
|
|
|
|
} else {
|
|
|
|
$this->decryptIV = substr($this->decryptIV, $len - $this->block_size) . substr($ciphertext, -$len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_OFB:
|
2016-12-03 21:39:11 +01:00
|
|
|
$plaintext = $this->openssl_ofb_process($ciphertext, $this->decryptIV, $this->debuffer);
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
return $this->paddable ? $this->unpad($plaintext) : $plaintext;
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
|
|
|
|
2015-04-02 12:32:31 +02:00
|
|
|
if ($this->engine === self::ENGINE_MCRYPT) {
|
2013-05-20 08:19:38 +02:00
|
|
|
$block_size = $this->block_size;
|
|
|
|
if ($this->changed) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->setupMcrypt();
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->changed = false;
|
|
|
|
}
|
|
|
|
if ($this->dechanged) {
|
2016-12-03 21:39:11 +01:00
|
|
|
@mcrypt_generic_init($this->demcrypt, $this->key, $this->getIV($this->decryptIV));
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->dechanged = false;
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($this->mode == self::MODE_CFB && $this->continuousBuffer) {
|
2013-05-20 08:19:38 +02:00
|
|
|
$iv = &$this->decryptIV;
|
|
|
|
$pos = &$this->debuffer['pos'];
|
|
|
|
$len = strlen($ciphertext);
|
|
|
|
$plaintext = '';
|
|
|
|
$i = 0;
|
|
|
|
if ($pos) {
|
|
|
|
$orig_pos = $pos;
|
|
|
|
$max = $block_size - $pos;
|
|
|
|
if ($len >= $max) {
|
|
|
|
$i = $max;
|
|
|
|
$len-= $max;
|
|
|
|
$pos = 0;
|
|
|
|
} else {
|
|
|
|
$i = $len;
|
|
|
|
$pos+= $len;
|
|
|
|
$len = 0;
|
|
|
|
}
|
|
|
|
// ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize
|
|
|
|
$plaintext = substr($iv, $orig_pos) ^ $ciphertext;
|
|
|
|
$iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
|
|
|
|
}
|
|
|
|
if ($len >= $block_size) {
|
|
|
|
$cb = substr($ciphertext, $i, $len - $len % $block_size);
|
2016-09-21 08:03:26 +02:00
|
|
|
$plaintext.= @mcrypt_generic($this->ecb, $iv . $cb) ^ $cb;
|
2013-05-20 08:19:38 +02:00
|
|
|
$iv = substr($cb, -$block_size);
|
|
|
|
$len%= $block_size;
|
|
|
|
}
|
|
|
|
if ($len) {
|
2016-09-21 08:03:26 +02:00
|
|
|
$iv = @mcrypt_generic($this->ecb, $iv);
|
2013-05-20 08:19:38 +02:00
|
|
|
$plaintext.= $iv ^ substr($ciphertext, -$len);
|
|
|
|
$iv = substr_replace($iv, substr($ciphertext, -$len), 0, $len);
|
|
|
|
$pos = $len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $plaintext;
|
|
|
|
}
|
|
|
|
|
2016-09-21 08:03:26 +02:00
|
|
|
$plaintext = @mdecrypt_generic($this->demcrypt, $ciphertext);
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
if (!$this->continuousBuffer) {
|
2016-12-03 21:39:11 +01:00
|
|
|
@mcrypt_generic_init($this->demcrypt, $this->key, $this->getIV($this->decryptIV));
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
return $this->paddable ? $this->unpad($plaintext) : $plaintext;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->changed) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->setup();
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->changed = false;
|
|
|
|
}
|
2016-12-08 16:20:19 +01:00
|
|
|
if ($this->engine === self::ENGINE_EVAL) {
|
2013-05-20 08:19:38 +02:00
|
|
|
$inline = $this->inline_crypt;
|
2016-12-03 21:39:11 +01:00
|
|
|
return $inline('decrypt', $ciphertext);
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$block_size = $this->block_size;
|
|
|
|
|
|
|
|
$buffer = &$this->debuffer;
|
|
|
|
$plaintext = '';
|
|
|
|
switch ($this->mode) {
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_ECB:
|
2013-05-20 08:19:38 +02:00
|
|
|
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$plaintext.= $this->decryptBlock(substr($ciphertext, $i, $block_size));
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_CBC:
|
2013-05-20 08:19:38 +02:00
|
|
|
$xor = $this->decryptIV;
|
|
|
|
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
|
|
|
|
$block = substr($ciphertext, $i, $block_size);
|
2016-12-03 21:39:11 +01:00
|
|
|
$plaintext.= $this->decryptBlock($block) ^ $xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
$xor = $block;
|
|
|
|
}
|
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$this->decryptIV = $xor;
|
|
|
|
}
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_CTR:
|
2013-05-20 08:19:38 +02:00
|
|
|
$xor = $this->decryptIV;
|
|
|
|
if (strlen($buffer['ciphertext'])) {
|
|
|
|
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
|
|
|
|
$block = substr($ciphertext, $i, $block_size);
|
|
|
|
if (strlen($block) > strlen($buffer['ciphertext'])) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$buffer['ciphertext'].= $this->encryptBlock($xor);
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2018-12-27 15:31:35 +01:00
|
|
|
Strings::increment_str($xor);
|
2016-07-31 05:18:06 +02:00
|
|
|
$key = Strings::shift($buffer['ciphertext'], $block_size);
|
2013-05-20 08:19:38 +02:00
|
|
|
$plaintext.= $block ^ $key;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
|
|
|
|
$block = substr($ciphertext, $i, $block_size);
|
2016-12-03 21:39:11 +01:00
|
|
|
$key = $this->encryptBlock($xor);
|
2018-12-27 15:31:35 +01:00
|
|
|
Strings::increment_str($xor);
|
2013-05-20 08:19:38 +02:00
|
|
|
$plaintext.= $block ^ $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$this->decryptIV = $xor;
|
|
|
|
if ($start = strlen($ciphertext) % $block_size) {
|
|
|
|
$buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_CFB:
|
2013-05-20 08:19:38 +02:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$iv = &$this->decryptIV;
|
|
|
|
$pos = &$buffer['pos'];
|
|
|
|
} else {
|
|
|
|
$iv = $this->decryptIV;
|
|
|
|
$pos = 0;
|
|
|
|
}
|
|
|
|
$len = strlen($ciphertext);
|
|
|
|
$i = 0;
|
|
|
|
if ($pos) {
|
|
|
|
$orig_pos = $pos;
|
|
|
|
$max = $block_size - $pos;
|
|
|
|
if ($len >= $max) {
|
|
|
|
$i = $max;
|
|
|
|
$len-= $max;
|
|
|
|
$pos = 0;
|
|
|
|
} else {
|
|
|
|
$i = $len;
|
|
|
|
$pos+= $len;
|
|
|
|
$len = 0;
|
|
|
|
}
|
|
|
|
// ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize
|
|
|
|
$plaintext = substr($iv, $orig_pos) ^ $ciphertext;
|
|
|
|
$iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
|
|
|
|
}
|
|
|
|
while ($len >= $block_size) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$iv = $this->encryptBlock($iv);
|
2013-05-20 08:19:38 +02:00
|
|
|
$cb = substr($ciphertext, $i, $block_size);
|
|
|
|
$plaintext.= $iv ^ $cb;
|
|
|
|
$iv = $cb;
|
|
|
|
$len-= $block_size;
|
|
|
|
$i+= $block_size;
|
|
|
|
}
|
|
|
|
if ($len) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$iv = $this->encryptBlock($iv);
|
2013-05-20 08:19:38 +02:00
|
|
|
$plaintext.= $iv ^ substr($ciphertext, $i);
|
|
|
|
$iv = substr_replace($iv, substr($ciphertext, $i), 0, $len);
|
|
|
|
$pos = $len;
|
|
|
|
}
|
|
|
|
break;
|
2017-10-04 12:30:53 +02:00
|
|
|
case self::MODE_CFB8:
|
|
|
|
$plaintext = '';
|
|
|
|
$len = strlen($ciphertext);
|
|
|
|
$iv = $this->decryptIV;
|
|
|
|
|
2017-10-10 12:02:55 +02:00
|
|
|
for ($i = 0; $i < $len; ++$i) {
|
2017-10-04 12:30:53 +02:00
|
|
|
$plaintext .= $ciphertext[$i] ^ $this->encryptBlock($iv);
|
2017-10-10 12:06:14 +02:00
|
|
|
$iv = substr($iv, 1) . $ciphertext[$i];
|
2017-10-04 12:30:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->continuousBuffer) {
|
2017-10-10 12:06:14 +02:00
|
|
|
if ($len >= $block_size) {
|
|
|
|
$this->decryptIV = substr($ciphertext, -$block_size);
|
2017-10-04 12:30:53 +02:00
|
|
|
} else {
|
2017-10-10 12:06:14 +02:00
|
|
|
$this->decryptIV = substr($this->decryptIV, $len - $block_size) . substr($ciphertext, -$len);
|
2017-10-04 12:30:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_OFB:
|
2013-05-20 08:19:38 +02:00
|
|
|
$xor = $this->decryptIV;
|
|
|
|
if (strlen($buffer['xor'])) {
|
|
|
|
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
|
|
|
|
$block = substr($ciphertext, $i, $block_size);
|
|
|
|
if (strlen($block) > strlen($buffer['xor'])) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$xor = $this->encryptBlock($xor);
|
2013-05-20 08:19:38 +02:00
|
|
|
$buffer['xor'].= $xor;
|
|
|
|
}
|
2016-07-31 05:18:06 +02:00
|
|
|
$key = Strings::shift($buffer['xor'], $block_size);
|
2013-05-20 08:19:38 +02:00
|
|
|
$plaintext.= $block ^ $key;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$xor = $this->encryptBlock($xor);
|
2013-05-20 08:19:38 +02:00
|
|
|
$plaintext.= substr($ciphertext, $i, $block_size) ^ $xor;
|
|
|
|
}
|
|
|
|
$key = $xor;
|
|
|
|
}
|
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$this->decryptIV = $xor;
|
|
|
|
if ($start = strlen($ciphertext) % $block_size) {
|
2016-04-10 18:30:59 +02:00
|
|
|
$buffer['xor'] = substr($key, $start) . $buffer['xor'];
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_STREAM:
|
2016-12-03 21:39:11 +01:00
|
|
|
$plaintext = $this->decryptBlock($ciphertext);
|
2013-05-20 08:19:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-12-03 21:39:11 +01:00
|
|
|
return $this->paddable ? $this->unpad($plaintext) : $plaintext;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2018-12-27 15:31:35 +01:00
|
|
|
/**
|
|
|
|
* Get the authentication tag
|
|
|
|
*
|
|
|
|
* Only used in GCM mode
|
|
|
|
*
|
|
|
|
* @see self::encrypt()
|
|
|
|
* @param int $length optional
|
|
|
|
* @return string
|
|
|
|
* @access public
|
|
|
|
* @throws \LengthException if $length isn't of a sufficient length
|
|
|
|
* @throws \RuntimeException if GCM mode isn't being used
|
|
|
|
*/
|
|
|
|
public function getTag($length = 16)
|
|
|
|
{
|
|
|
|
if ($this->mode != self::MODE_GCM) {
|
|
|
|
throw new \RuntimeException('Only GCM mode utilizes authentication tags');
|
|
|
|
}
|
|
|
|
|
|
|
|
// the tag is basically a single encrypted block of a 128-bit cipher. it can't be greater than 16
|
|
|
|
// bytes because that's bigger than a block is. if it were 0 you might as well be doing CTR and
|
|
|
|
// less than 4 provides minimal security that could be trivially easily brute forced.
|
|
|
|
// see https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf#page=36
|
|
|
|
// for more info
|
|
|
|
if ($length < 4 || $length > 16) {
|
|
|
|
throw new \LengthException('The authentication tag must be between 4 and 16 bytes long');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $length == 16 ?
|
|
|
|
$this->newtag :
|
|
|
|
substr($this->newtag, 0, $length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the authentication tag
|
|
|
|
*
|
|
|
|
* Only used in GCM mode
|
|
|
|
*
|
|
|
|
* @see self::decrypt()
|
|
|
|
* @param string $tag
|
|
|
|
* @access public
|
|
|
|
* @throws \LengthException if $length isn't of a sufficient length
|
|
|
|
* @throws \RuntimeException if GCM mode isn't being used
|
|
|
|
*/
|
|
|
|
public function setTag($tag)
|
|
|
|
{
|
|
|
|
if ($this->mode != self::MODE_GCM) {
|
|
|
|
throw new \RuntimeException('Only GCM mode utilizes authentication tags');
|
|
|
|
}
|
|
|
|
|
|
|
|
$length = strlen($tag);
|
|
|
|
if ($length < 4 || $length > 16) {
|
|
|
|
throw new \LengthException('The authentication tag must be between 4 and 16 bytes long');
|
|
|
|
}
|
|
|
|
$this->oldtag = $tag;
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
/**
|
|
|
|
* Get the IV
|
|
|
|
*
|
|
|
|
* mcrypt requires an IV even if ECB is used
|
|
|
|
*
|
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::decrypt()
|
|
|
|
* @param string $iv
|
|
|
|
* @return string
|
|
|
|
* @access private
|
|
|
|
*/
|
2018-12-27 15:31:35 +01:00
|
|
|
protected function getIV($iv)
|
2016-04-30 23:23:35 +02:00
|
|
|
{
|
|
|
|
return $this->mode == self::MODE_ECB ? str_repeat("\0", $this->block_size) : $iv;
|
|
|
|
}
|
|
|
|
|
2014-11-29 14:39:21 +01:00
|
|
|
/**
|
|
|
|
* OpenSSL CTR Processor
|
|
|
|
*
|
|
|
|
* PHP's OpenSSL bindings do not operate in continuous mode so we'll wrap around it. Since the keystream
|
2016-07-31 05:18:06 +02:00
|
|
|
* for CTR is the same for both encrypting and decrypting this function is re-used by both SymmetricKey::encrypt()
|
|
|
|
* and SymmetricKey::decrypt(). Also, OpenSSL doesn't implement CTR for all of it's symmetric ciphers so this
|
2018-03-03 13:07:14 +01:00
|
|
|
* function will emulate CTR with ECB when necessary.
|
2014-11-29 14:39:21 +01:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::decrypt()
|
|
|
|
* @param string $plaintext
|
|
|
|
* @param string $encryptIV
|
|
|
|
* @param array $buffer
|
|
|
|
* @return string
|
2014-11-29 14:39:21 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private function openssl_ctr_process($plaintext, &$encryptIV, &$buffer)
|
2014-11-29 14:39:21 +01:00
|
|
|
{
|
|
|
|
$ciphertext = '';
|
|
|
|
|
|
|
|
$block_size = $this->block_size;
|
|
|
|
$key = $this->key;
|
|
|
|
|
|
|
|
if ($this->openssl_emulate_ctr) {
|
|
|
|
$xor = $encryptIV;
|
2014-11-29 23:18:17 +01:00
|
|
|
if (strlen($buffer['ciphertext'])) {
|
2014-11-29 14:39:21 +01:00
|
|
|
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
|
|
|
|
$block = substr($plaintext, $i, $block_size);
|
2014-11-29 23:18:17 +01:00
|
|
|
if (strlen($block) > strlen($buffer['ciphertext'])) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$buffer['ciphertext'].= openssl_encrypt($xor, $this->cipher_name_openssl_ecb, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
2018-12-27 15:31:35 +01:00
|
|
|
Strings::increment_str($xor);
|
2016-07-31 05:18:06 +02:00
|
|
|
$otp = Strings::shift($buffer['ciphertext'], $block_size);
|
2014-11-29 14:39:21 +01:00
|
|
|
$ciphertext.= $block ^ $otp;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
|
|
|
|
$block = substr($plaintext, $i, $block_size);
|
2016-12-03 21:39:11 +01:00
|
|
|
$otp = openssl_encrypt($xor, $this->cipher_name_openssl_ecb, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
|
2018-12-27 15:31:35 +01:00
|
|
|
Strings::increment_str($xor);
|
2014-11-29 14:39:21 +01:00
|
|
|
$ciphertext.= $block ^ $otp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$encryptIV = $xor;
|
|
|
|
if ($start = strlen($plaintext) % $block_size) {
|
2014-11-29 23:18:17 +01:00
|
|
|
$buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ciphertext;
|
|
|
|
}
|
|
|
|
|
2014-11-29 23:18:17 +01:00
|
|
|
if (strlen($buffer['ciphertext'])) {
|
2016-07-31 05:18:06 +02:00
|
|
|
$ciphertext = $plaintext ^ Strings::shift($buffer['ciphertext'], strlen($plaintext));
|
2014-11-29 14:39:21 +01:00
|
|
|
$plaintext = substr($plaintext, strlen($ciphertext));
|
|
|
|
|
|
|
|
if (!strlen($plaintext)) {
|
|
|
|
return $ciphertext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$overflow = strlen($plaintext) % $block_size;
|
|
|
|
if ($overflow) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$plaintext2 = Strings::pop($plaintext, $overflow); // ie. trim $plaintext to a multiple of $block_size and put rest of $plaintext in $plaintext2
|
|
|
|
$encrypted = openssl_encrypt($plaintext . str_repeat("\0", $block_size), $this->cipher_name_openssl, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $encryptIV);
|
|
|
|
$temp = Strings::pop($encrypted, $block_size);
|
2015-01-01 05:24:56 +01:00
|
|
|
$ciphertext.= $encrypted . ($plaintext2 ^ $temp);
|
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$buffer['ciphertext'] = substr($temp, $overflow);
|
|
|
|
$encryptIV = $temp;
|
|
|
|
}
|
2015-07-15 03:52:31 +02:00
|
|
|
} elseif (!strlen($buffer['ciphertext'])) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$ciphertext.= openssl_encrypt($plaintext . str_repeat("\0", $block_size), $this->cipher_name_openssl, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $encryptIV);
|
|
|
|
$temp = Strings::pop($ciphertext, $block_size);
|
2015-01-01 05:24:56 +01:00
|
|
|
if ($this->continuousBuffer) {
|
2015-01-01 06:38:55 +01:00
|
|
|
$encryptIV = $temp;
|
2015-01-01 05:24:56 +01:00
|
|
|
}
|
2014-12-07 06:36:26 +01:00
|
|
|
}
|
2015-01-01 05:24:56 +01:00
|
|
|
if ($this->continuousBuffer) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$encryptIV = openssl_decrypt($encryptIV, $this->cipher_name_openssl_ecb, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
|
2015-01-01 05:24:56 +01:00
|
|
|
if ($overflow) {
|
2018-12-27 15:31:35 +01:00
|
|
|
Strings::increment_str($encryptIV);
|
2015-01-01 05:24:56 +01:00
|
|
|
}
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $ciphertext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OpenSSL OFB Processor
|
|
|
|
*
|
|
|
|
* PHP's OpenSSL bindings do not operate in continuous mode so we'll wrap around it. Since the keystream
|
2016-07-31 05:18:06 +02:00
|
|
|
* for OFB is the same for both encrypting and decrypting this function is re-used by both SymmetricKey::encrypt()
|
|
|
|
* and SymmetricKey::decrypt().
|
2016-04-10 18:30:59 +02:00
|
|
|
*
|
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::decrypt()
|
|
|
|
* @param string $plaintext
|
|
|
|
* @param string $encryptIV
|
|
|
|
* @param array $buffer
|
|
|
|
* @return string
|
2014-11-29 14:39:21 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private function openssl_ofb_process($plaintext, &$encryptIV, &$buffer)
|
2014-11-29 14:39:21 +01:00
|
|
|
{
|
|
|
|
if (strlen($buffer['xor'])) {
|
|
|
|
$ciphertext = $plaintext ^ $buffer['xor'];
|
|
|
|
$buffer['xor'] = substr($buffer['xor'], strlen($ciphertext));
|
|
|
|
$plaintext = substr($plaintext, strlen($ciphertext));
|
|
|
|
} else {
|
|
|
|
$ciphertext = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$block_size = $this->block_size;
|
|
|
|
|
|
|
|
$len = strlen($plaintext);
|
|
|
|
$key = $this->key;
|
|
|
|
$overflow = $len % $block_size;
|
|
|
|
|
|
|
|
if (strlen($plaintext)) {
|
|
|
|
if ($overflow) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$ciphertext.= openssl_encrypt(substr($plaintext, 0, -$overflow) . str_repeat("\0", $block_size), $this->cipher_name_openssl, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $encryptIV);
|
|
|
|
$xor = Strings::pop($ciphertext, $block_size);
|
2014-11-29 14:39:21 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$encryptIV = $xor;
|
|
|
|
}
|
2016-07-31 05:18:06 +02:00
|
|
|
$ciphertext.= Strings::shift($xor, $overflow) ^ substr($plaintext, -$overflow);
|
2014-11-29 14:39:21 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$buffer['xor'] = $xor;
|
|
|
|
}
|
|
|
|
} else {
|
2016-12-03 21:39:11 +01:00
|
|
|
$ciphertext = openssl_encrypt($plaintext, $this->cipher_name_openssl, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $encryptIV);
|
2014-11-29 14:39:21 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$encryptIV = substr($ciphertext, -$block_size) ^ substr($plaintext, -$block_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ciphertext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* phpseclib <-> OpenSSL Mode Mapper
|
|
|
|
*
|
|
|
|
* May need to be overwritten by classes extending this one in some cases
|
|
|
|
*
|
2017-09-17 19:23:14 +02:00
|
|
|
* @return string
|
2014-11-29 14:39:21 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected function openssl_translate_mode()
|
2014-11-29 14:39:21 +01:00
|
|
|
{
|
|
|
|
switch ($this->mode) {
|
2015-04-02 14:01:37 +02:00
|
|
|
case self::MODE_ECB:
|
2014-11-29 14:39:21 +01:00
|
|
|
return 'ecb';
|
2015-04-02 14:01:37 +02:00
|
|
|
case self::MODE_CBC:
|
2014-11-29 14:39:21 +01:00
|
|
|
return 'cbc';
|
2015-04-02 14:01:37 +02:00
|
|
|
case self::MODE_CTR:
|
2018-12-27 15:31:35 +01:00
|
|
|
case self::MODE_GCM:
|
2014-11-29 14:39:21 +01:00
|
|
|
return 'ctr';
|
2015-04-02 14:01:37 +02:00
|
|
|
case self::MODE_CFB:
|
2014-11-29 14:39:21 +01:00
|
|
|
return 'cfb';
|
2017-10-04 12:30:53 +02:00
|
|
|
case self::MODE_CFB8:
|
|
|
|
return 'cfb8';
|
2015-04-02 14:01:37 +02:00
|
|
|
case self::MODE_OFB:
|
2014-11-29 14:39:21 +01:00
|
|
|
return 'ofb';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-25 04:05:24 +02:00
|
|
|
/**
|
|
|
|
* Pad "packets".
|
|
|
|
*
|
|
|
|
* Block ciphers working by encrypting between their specified [$this->]block_size at a time
|
|
|
|
* If you ever need to encrypt or decrypt something that isn't of the proper length, it becomes necessary to
|
|
|
|
* pad the input so that it is of the proper length.
|
|
|
|
*
|
|
|
|
* Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH,
|
|
|
|
* where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping
|
|
|
|
* away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is
|
|
|
|
* transmitted separately)
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::disablePadding()
|
2013-05-25 04:05:24 +02:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function enablePadding()
|
2013-05-25 04:05:24 +02:00
|
|
|
{
|
|
|
|
$this->padding = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do not pad packets.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::enablePadding()
|
2013-05-25 04:05:24 +02:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function disablePadding()
|
2013-05-25 04:05:24 +02:00
|
|
|
{
|
|
|
|
$this->padding = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Treat consecutive "packets" as if they are a continuous buffer.
|
|
|
|
*
|
|
|
|
* Say you have a 32-byte plaintext $plaintext. Using the default behavior, the two following code snippets
|
|
|
|
* will yield different outputs:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* echo $rijndael->encrypt(substr($plaintext, 0, 16));
|
|
|
|
* echo $rijndael->encrypt(substr($plaintext, 16, 16));
|
|
|
|
* </code>
|
|
|
|
* <code>
|
|
|
|
* echo $rijndael->encrypt($plaintext);
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
|
|
|
|
* another, as demonstrated with the following:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $rijndael->encrypt(substr($plaintext, 0, 16));
|
|
|
|
* echo $rijndael->decrypt($rijndael->encrypt(substr($plaintext, 16, 16)));
|
|
|
|
* </code>
|
|
|
|
* <code>
|
|
|
|
* echo $rijndael->decrypt($rijndael->encrypt(substr($plaintext, 16, 16)));
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
|
|
|
|
* outputs. The reason is due to the fact that the initialization vector's change after every encryption /
|
|
|
|
* decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* Put another way, when the continuous buffer is enabled, the state of the \phpseclib\Crypt\*() object changes after each
|
2013-05-25 04:05:24 +02:00
|
|
|
* encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
|
|
|
|
* continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
|
|
|
|
* however, they are also less intuitive and more likely to cause you problems.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::disableContinuousBuffer()
|
2013-05-25 04:05:24 +02:00
|
|
|
* @access public
|
2014-01-31 00:13:14 +01:00
|
|
|
* @internal Could, but not must, extend by the child Crypt_* class
|
2013-05-25 04:05:24 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function enableContinuousBuffer()
|
2013-05-25 04:05:24 +02:00
|
|
|
{
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($this->mode == self::MODE_ECB) {
|
2013-05-25 04:05:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:31:35 +01:00
|
|
|
if ($this->mode == self::MODE_GCM) {
|
|
|
|
throw new \RuntimeException('This mode does not run in continuous mode');
|
|
|
|
}
|
|
|
|
|
2013-05-25 04:05:24 +02:00
|
|
|
$this->continuousBuffer = true;
|
2014-12-31 21:10:53 +01:00
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->setEngine();
|
2013-05-25 04:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Treat consecutive packets as if they are a discontinuous buffer.
|
|
|
|
*
|
|
|
|
* The default behavior.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::enableContinuousBuffer()
|
2013-05-25 04:05:24 +02:00
|
|
|
* @access public
|
2014-01-31 00:13:14 +01:00
|
|
|
* @internal Could, but not must, extend by the child Crypt_* class
|
2013-05-25 04:05:24 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function disableContinuousBuffer()
|
2013-05-25 04:05:24 +02:00
|
|
|
{
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($this->mode == self::MODE_ECB) {
|
2013-05-25 04:05:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!$this->continuousBuffer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->continuousBuffer = false;
|
|
|
|
$this->changed = true;
|
2014-12-31 21:10:53 +01:00
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->setEngine();
|
2013-05-25 04:05:24 +02:00
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
2014-11-29 14:39:21 +01:00
|
|
|
* Test for engine validity
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::__construct()
|
|
|
|
* @param int $engine
|
2017-08-08 06:20:23 +02:00
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return bool
|
2014-11-29 14:39:21 +01:00
|
|
|
*/
|
2017-08-08 06:20:23 +02:00
|
|
|
protected function isValidEngineHelper($engine)
|
2014-11-29 14:39:21 +01:00
|
|
|
{
|
|
|
|
switch ($engine) {
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::ENGINE_OPENSSL:
|
|
|
|
if ($this->mode == self::MODE_STREAM && $this->continuousBuffer) {
|
2014-12-31 21:10:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-29 14:39:21 +01:00
|
|
|
$this->openssl_emulate_ctr = false;
|
2014-11-29 16:52:14 +01:00
|
|
|
$result = $this->cipher_name_openssl &&
|
2016-12-08 16:20:19 +01:00
|
|
|
extension_loaded('openssl');
|
2014-11-29 14:39:21 +01:00
|
|
|
if (!$result) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-29 16:48:45 +01:00
|
|
|
|
2014-11-29 14:39:21 +01:00
|
|
|
$methods = openssl_get_cipher_methods();
|
|
|
|
if (in_array($this->cipher_name_openssl, $methods)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// not all of openssl's symmetric cipher's support ctr. for those
|
|
|
|
// that don't we'll emulate it
|
|
|
|
switch ($this->mode) {
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::MODE_CTR:
|
2014-11-29 14:39:21 +01:00
|
|
|
if (in_array($this->cipher_name_openssl_ecb, $methods)) {
|
|
|
|
$this->openssl_emulate_ctr = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::ENGINE_MCRYPT:
|
2014-11-29 14:39:21 +01:00
|
|
|
return $this->cipher_name_mcrypt &&
|
|
|
|
extension_loaded('mcrypt') &&
|
2016-09-21 08:03:26 +02:00
|
|
|
in_array($this->cipher_name_mcrypt, @mcrypt_list_algorithms());
|
2016-12-08 16:20:19 +01:00
|
|
|
case self::ENGINE_EVAL:
|
|
|
|
return method_exists($this, 'setupInlineCrypt');
|
2015-04-02 12:32:31 +02:00
|
|
|
case self::ENGINE_INTERNAL:
|
2014-11-29 14:39:21 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-04-02 12:32:31 +02:00
|
|
|
|
|
|
|
return false;
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
|
|
|
|
2017-08-08 06:20:23 +02:00
|
|
|
/**
|
|
|
|
* Test for engine validity
|
|
|
|
*
|
|
|
|
* @see self::__construct()
|
|
|
|
* @param string $engine
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isValidEngine($engine)
|
|
|
|
{
|
|
|
|
static $reverseMap;
|
|
|
|
if (!isset($reverseMap)) {
|
|
|
|
$reverseMap = array_map('strtolower', self::ENGINE_MAP);
|
|
|
|
$reverseMap = array_flip($reverseMap);
|
|
|
|
}
|
|
|
|
$engine = strtolower($engine);
|
|
|
|
if (!isset($reverseMap[$engine])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->isValidEngineHelper($reverseMap[$engine]);
|
|
|
|
}
|
|
|
|
|
2014-11-29 14:39:21 +01:00
|
|
|
/**
|
|
|
|
* Sets the preferred crypt engine
|
2014-01-31 00:13:14 +01:00
|
|
|
*
|
|
|
|
* Currently, $engine could be:
|
|
|
|
*
|
2018-12-27 15:31:35 +01:00
|
|
|
* - libsodium[very fast]
|
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - OpenSSL [very fast]
|
2014-11-29 14:39:21 +01:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - mcrypt [fast]
|
2014-01-31 00:13:14 +01:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - Eval [slow]
|
2016-12-08 16:20:19 +01:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - PHP [slowest]
|
2014-01-31 00:13:14 +01:00
|
|
|
*
|
2014-11-29 14:39:21 +01:00
|
|
|
* If the preferred crypt engine is not available the fastest available one will be used
|
2014-01-31 00:13:14 +01:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::__construct()
|
2017-10-05 11:46:59 +02:00
|
|
|
* @param string $engine
|
2014-01-31 00:13:14 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function setPreferredEngine($engine)
|
2014-01-31 00:13:14 +01:00
|
|
|
{
|
2017-06-28 05:34:36 +02:00
|
|
|
static $reverseMap;
|
|
|
|
if (!isset($reverseMap)) {
|
|
|
|
$reverseMap = array_map('strtolower', self::ENGINE_MAP);
|
|
|
|
$reverseMap = array_flip($reverseMap);
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
2017-06-28 05:34:36 +02:00
|
|
|
$engine = strtolower($engine);
|
2018-12-27 15:31:35 +01:00
|
|
|
$this->preferredEngine = isset($reverseMap[$engine]) ? $reverseMap[$engine] : self::ENGINE_LIBSODIUM;
|
2014-11-29 14:39:21 +01:00
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->setEngine();
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the engine currently being utilized
|
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::setEngine()
|
2014-11-29 14:39:21 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function getEngine()
|
2014-11-29 14:39:21 +01:00
|
|
|
{
|
2017-06-28 05:34:36 +02:00
|
|
|
return self::ENGINE_MAP[$this->engine];
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the engine as appropriate
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::__construct()
|
2014-11-29 14:39:21 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected function setEngine()
|
2014-11-29 14:39:21 +01:00
|
|
|
{
|
2015-04-02 12:32:31 +02:00
|
|
|
$this->engine = null;
|
|
|
|
|
2016-11-30 22:19:23 +01:00
|
|
|
$candidateEngines = [
|
2015-04-02 12:32:31 +02:00
|
|
|
$this->preferredEngine,
|
2018-12-27 15:31:35 +01:00
|
|
|
self::ENGINE_LIBSODIUM,
|
|
|
|
self::ENGINE_OPENSSL_GCM,
|
2015-04-02 12:32:31 +02:00
|
|
|
self::ENGINE_OPENSSL,
|
2016-12-08 16:20:19 +01:00
|
|
|
self::ENGINE_MCRYPT,
|
|
|
|
self::ENGINE_EVAL
|
2016-11-30 22:19:23 +01:00
|
|
|
];
|
2015-04-02 12:32:31 +02:00
|
|
|
foreach ($candidateEngines as $engine) {
|
2017-08-08 06:20:23 +02:00
|
|
|
if ($this->isValidEngineHelper($engine)) {
|
2015-04-02 12:32:31 +02:00
|
|
|
$this->engine = $engine;
|
2014-11-29 14:39:21 +01:00
|
|
|
break;
|
2015-04-02 12:32:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$this->engine) {
|
|
|
|
$this->engine = self::ENGINE_INTERNAL;
|
2014-01-31 00:13:14 +01:00
|
|
|
}
|
|
|
|
|
2015-04-02 12:32:31 +02:00
|
|
|
if ($this->engine != self::ENGINE_MCRYPT && $this->enmcrypt) {
|
2014-01-31 00:13:14 +01:00
|
|
|
// Closing the current mcrypt resource(s). _mcryptSetup() will, if needed,
|
|
|
|
// (re)open them with the module named in $this->cipher_name_mcrypt
|
2016-09-21 08:03:26 +02:00
|
|
|
@mcrypt_module_close($this->enmcrypt);
|
|
|
|
@mcrypt_module_close($this->demcrypt);
|
2014-01-31 00:13:14 +01:00
|
|
|
$this->enmcrypt = null;
|
|
|
|
$this->demcrypt = null;
|
|
|
|
|
|
|
|
if ($this->ecb) {
|
2016-09-21 08:03:26 +02:00
|
|
|
@mcrypt_module_close($this->ecb);
|
2014-01-31 00:13:14 +01:00
|
|
|
$this->ecb = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->changed = true;
|
2013-05-25 04:05:24 +02:00
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
|
|
|
* Encrypts a block
|
|
|
|
*
|
2015-04-02 12:32:31 +02:00
|
|
|
* Note: Must be extended by the child \phpseclib\Crypt\* class
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $in
|
|
|
|
* @return string
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
abstract protected function encryptBlock($in);
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrypts a block
|
|
|
|
*
|
2015-04-02 12:32:31 +02:00
|
|
|
* Note: Must be extended by the child \phpseclib\Crypt\* class
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $in
|
|
|
|
* @return string
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
abstract protected function decryptBlock($in);
|
2013-06-26 06:50:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the key (expansion)
|
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* Only used if $engine == self::ENGINE_INTERNAL
|
2013-06-26 06:50:40 +02:00
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* Note: Must extend by the child \phpseclib\Crypt\* class
|
2013-06-26 06:50:40 +02:00
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::setup()
|
2013-06-26 06:50:40 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
abstract protected function setupKey();
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
2014-12-09 19:46:30 +01:00
|
|
|
* Setup the self::ENGINE_INTERNAL $engine
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
|
|
|
* (re)init, if necessary, the internal cipher $engine and flush all $buffers
|
2014-12-09 19:46:30 +01:00
|
|
|
* Used (only) if $engine == self::ENGINE_INTERNAL
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
|
|
|
* _setup() will be called each time if $changed === true
|
|
|
|
* typically this happens when using one or more of following public methods:
|
2013-05-25 13:10:20 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* - setKey()
|
2013-05-25 13:10:20 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* - setIV()
|
2013-05-25 13:10:20 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* - disableContinuousBuffer()
|
2013-05-25 13:10:20 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* - First run of encrypt() / decrypt() with no init-settings
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::setKey()
|
|
|
|
* @see self::setIV()
|
|
|
|
* @see self::disableContinuousBuffer()
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
2016-12-03 21:39:11 +01:00
|
|
|
* @internal setup() is always called before en/decryption.
|
2014-01-31 00:13:14 +01:00
|
|
|
* @internal Could, but not must, extend by the child Crypt_* class
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected function setup()
|
2013-05-20 08:19:38 +02:00
|
|
|
{
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->clearBuffers();
|
|
|
|
$this->setupKey();
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2016-12-08 16:20:19 +01:00
|
|
|
if ($this->engine === self::ENGINE_EVAL) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->setupInlineCrypt();
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:31:35 +01:00
|
|
|
/**
|
|
|
|
* Calls the appropriate setup method
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
protected function genericSetup()
|
|
|
|
{
|
|
|
|
switch ($this->engine) {
|
|
|
|
case self::ENGINE_MCRYPT:
|
|
|
|
$this->setupMcrypt();
|
|
|
|
break;
|
|
|
|
case self::ENGINE_EVAL:
|
|
|
|
case self::ENGINE_INTERNAL:
|
|
|
|
$this->setup();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->clearBuffers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
2014-12-09 19:46:30 +01:00
|
|
|
* Setup the self::ENGINE_MCRYPT $engine
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
|
|
|
* (re)init, if necessary, the (ext)mcrypt resources and flush all $buffers
|
2014-12-09 19:46:30 +01:00
|
|
|
* Used (only) if $engine = self::ENGINE_MCRYPT
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2013-05-25 09:08:17 +02:00
|
|
|
* _setupMcrypt() will be called each time if $changed === true
|
2013-05-20 08:19:38 +02:00
|
|
|
* typically this happens when using one or more of following public methods:
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* - setKey()
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* - setIV()
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* - disableContinuousBuffer()
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* - First run of encrypt() / decrypt()
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::setKey()
|
|
|
|
* @see self::setIV()
|
|
|
|
* @see self::disableContinuousBuffer()
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
2014-01-31 00:13:14 +01:00
|
|
|
* @internal Could, but not must, extend by the child Crypt_* class
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-09 22:51:31 +01:00
|
|
|
private function setupMcrypt()
|
2013-05-20 08:19:38 +02:00
|
|
|
{
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->clearBuffers();
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->enchanged = $this->dechanged = true;
|
|
|
|
|
|
|
|
if (!isset($this->enmcrypt)) {
|
2016-11-30 22:19:23 +01:00
|
|
|
static $mcrypt_modes = [
|
2014-12-09 19:46:30 +01:00
|
|
|
self::MODE_CTR => 'ctr',
|
|
|
|
self::MODE_ECB => MCRYPT_MODE_ECB,
|
|
|
|
self::MODE_CBC => MCRYPT_MODE_CBC,
|
|
|
|
self::MODE_CFB => 'ncfb',
|
2017-10-04 12:30:53 +02:00
|
|
|
self::MODE_CFB8 => MCRYPT_MODE_CFB,
|
2014-12-09 19:46:30 +01:00
|
|
|
self::MODE_OFB => MCRYPT_MODE_NOFB,
|
|
|
|
self::MODE_STREAM => MCRYPT_MODE_STREAM,
|
2016-11-30 22:19:23 +01:00
|
|
|
];
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2016-09-21 08:03:26 +02:00
|
|
|
$this->demcrypt = @mcrypt_module_open($this->cipher_name_mcrypt, '', $mcrypt_modes[$this->mode], '');
|
|
|
|
$this->enmcrypt = @mcrypt_module_open($this->cipher_name_mcrypt, '', $mcrypt_modes[$this->mode], '');
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2013-05-25 13:10:20 +02:00
|
|
|
// we need the $ecb mcrypt resource (only) in MODE_CFB with enableContinuousBuffer()
|
|
|
|
// to workaround mcrypt's broken ncfb implementation in buffered mode
|
|
|
|
// see: {@link http://phpseclib.sourceforge.net/cfb-demo.phps}
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($this->mode == self::MODE_CFB) {
|
2016-09-21 08:03:26 +02:00
|
|
|
$this->ecb = @mcrypt_module_open($this->cipher_name_mcrypt, '', MCRYPT_MODE_ECB, '');
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
} // else should mcrypt_generic_deinit be called?
|
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
if ($this->mode == self::MODE_CFB) {
|
2016-09-21 08:03:26 +02:00
|
|
|
@mcrypt_generic_init($this->ecb, $this->key, str_repeat("\0", $this->block_size));
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pads a string
|
|
|
|
*
|
|
|
|
* Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize.
|
|
|
|
* $this->block_size - (strlen($text) % $this->block_size) bytes are added, each of which is equal to
|
|
|
|
* chr($this->block_size - (strlen($text) % $this->block_size)
|
|
|
|
*
|
|
|
|
* If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless
|
|
|
|
* and padding will, hence forth, be enabled.
|
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::unpad()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $text
|
2016-04-30 23:23:35 +02:00
|
|
|
* @throws \LengthException if padding is disabled and the plaintext's length is not a multiple of the block size
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return string
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected function pad($text)
|
2013-05-20 08:19:38 +02:00
|
|
|
{
|
|
|
|
$length = strlen($text);
|
|
|
|
|
|
|
|
if (!$this->padding) {
|
|
|
|
if ($length % $this->block_size == 0) {
|
|
|
|
return $text;
|
|
|
|
} else {
|
2016-04-30 23:23:35 +02:00
|
|
|
throw new \LengthException("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size}). Try enabling padding.");
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$pad = $this->block_size - ($length % $this->block_size);
|
|
|
|
|
|
|
|
return str_pad($text, $length + $pad, chr($pad));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unpads a string.
|
|
|
|
*
|
|
|
|
* If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong
|
|
|
|
* and false will be returned.
|
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::pad()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $text
|
2016-04-30 23:23:35 +02:00
|
|
|
* @throws \LengthException if the ciphertext's length is not a multiple of the block size
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return string
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected function unpad($text)
|
2013-05-20 08:19:38 +02:00
|
|
|
{
|
|
|
|
if (!$this->padding) {
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
$length = ord($text[strlen($text) - 1]);
|
|
|
|
|
|
|
|
if (!$length || $length > $this->block_size) {
|
2016-04-30 23:23:35 +02:00
|
|
|
throw new \LengthException("The ciphertext has an invalid padding length ($length) compared to the block size ({$this->block_size})");
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return substr($text, 0, -$length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears internal buffers
|
|
|
|
*
|
|
|
|
* Clearing/resetting the internal buffers is done everytime
|
|
|
|
* after disableContinuousBuffer() or on cipher $engine (re)init
|
|
|
|
* ie after setKey() or setIV()
|
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* @access private
|
2014-01-31 00:13:14 +01:00
|
|
|
* @internal Could, but not must, extend by the child Crypt_* class
|
2016-04-30 23:23:35 +02:00
|
|
|
* @throws \UnexpectedValueException when an IV is required but not defined
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2018-12-27 15:31:35 +01:00
|
|
|
public function clearBuffers()
|
2013-05-20 08:19:38 +02:00
|
|
|
{
|
2016-11-30 22:19:23 +01:00
|
|
|
$this->enbuffer = $this->debuffer = ['ciphertext' => '', 'xor' => '', 'pos' => 0, 'enmcrypt_init' => true];
|
2018-12-27 15:31:35 +01:00
|
|
|
//$this->newtag = $this->oldtag = false;
|
|
|
|
|
|
|
|
if ($this->mode == self::MODE_GCM) {
|
|
|
|
if ($this->nonce === false) {
|
|
|
|
throw new \UnexpectedValueException('No nonce has been defined');
|
|
|
|
}
|
|
|
|
if (!in_array($this->engine, [self::ENGINE_LIBSODIUM, self::ENGINE_OPENSSL_GCM])) {
|
|
|
|
$this->setupGCM();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->iv = $this->origIV;
|
|
|
|
}
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2016-11-30 22:19:23 +01:00
|
|
|
if ($this->iv === false && !in_array($this->mode, [self::MODE_STREAM, self::MODE_ECB])) {
|
2018-12-27 15:31:35 +01:00
|
|
|
if ($this->mode != self::MODE_GCM || !in_array($this->engine, [self::ENGINE_LIBSODIUM, self::ENGINE_OPENSSL_GCM])) {
|
|
|
|
throw new \UnexpectedValueException('No IV has been defined');
|
|
|
|
}
|
2016-04-28 22:34:10 +02:00
|
|
|
}
|
2016-04-30 23:23:35 +02:00
|
|
|
|
2018-12-02 19:01:56 +01:00
|
|
|
if ($this->key === false) {
|
|
|
|
throw new \UnexpectedValueException('No key has been defined');
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->encryptIV = $this->decryptIV = $this->iv;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the performance-optimized function for de/encrypt()
|
|
|
|
*
|
|
|
|
* Stores the created (or existing) callback function-name
|
|
|
|
* in $this->inline_crypt
|
|
|
|
*
|
|
|
|
* Internally for phpseclib developers:
|
2013-05-25 04:05:24 +02:00
|
|
|
*
|
2013-05-25 09:08:17 +02:00
|
|
|
* _setupInlineCrypt() would be called only if:
|
2013-05-25 04:05:24 +02:00
|
|
|
*
|
2016-12-08 16:20:19 +01:00
|
|
|
* - $this->engine === self::ENGINE_EVAL
|
2013-06-02 11:21:32 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* - each time on _setup(), after(!) _setupKey()
|
|
|
|
*
|
2013-06-02 11:21:32 +02:00
|
|
|
*
|
2014-03-06 00:41:20 +01:00
|
|
|
* This ensures that _setupInlineCrypt() has always a
|
2013-05-20 08:19:38 +02:00
|
|
|
* full ready2go initializated internal cipher $engine state
|
2018-03-03 13:07:14 +01:00
|
|
|
* where, for example, the keys already expanded,
|
2013-05-20 08:19:38 +02:00
|
|
|
* keys/block_size calculated and such.
|
|
|
|
*
|
2013-05-25 09:08:17 +02:00
|
|
|
* It is, each time if called, the responsibility of _setupInlineCrypt():
|
2013-06-02 11:21:32 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* - to set $this->inline_crypt to a valid and fully working callback function
|
|
|
|
* as a (faster) replacement for encrypt() / decrypt()
|
|
|
|
*
|
|
|
|
* - NOT to create unlimited callback functions (for memory reasons!)
|
2013-05-25 09:08:17 +02:00
|
|
|
* no matter how often _setupInlineCrypt() would be called. At some
|
2013-05-20 08:19:38 +02:00
|
|
|
* point of amount they must be generic re-useable.
|
|
|
|
*
|
2013-05-25 09:08:17 +02:00
|
|
|
* - the code of _setupInlineCrypt() it self,
|
2013-05-20 08:19:38 +02:00
|
|
|
* and the generated callback code,
|
|
|
|
* must be, in following order:
|
|
|
|
* - 100% safe
|
|
|
|
* - 100% compatible to encrypt()/decrypt()
|
|
|
|
* - using only php5+ features/lang-constructs/php-extensions if
|
|
|
|
* compatibility (down to php4) or fallback is provided
|
|
|
|
* - readable/maintainable/understandable/commented and... not-cryptic-styled-code :-)
|
|
|
|
* - >= 10% faster than encrypt()/decrypt() [which is, by the way,
|
2013-05-25 09:08:17 +02:00
|
|
|
* the reason for the existence of _setupInlineCrypt() :-)]
|
2013-05-20 08:19:38 +02:00
|
|
|
* - memory-nice
|
|
|
|
* - short (as good as possible)
|
|
|
|
*
|
2013-06-26 06:50:40 +02:00
|
|
|
* Note: - _setupInlineCrypt() is using _createInlineCryptFunction() to create the full callback function code.
|
2014-12-17 01:16:54 +01:00
|
|
|
* - In case of using inline crypting, _setupInlineCrypt() must extend by the child \phpseclib\Crypt\* class.
|
2013-06-26 06:55:19 +02:00
|
|
|
* - The following variable names are reserved:
|
2013-06-26 06:50:40 +02:00
|
|
|
* - $_* (all variable names prefixed with an underscore)
|
|
|
|
* - $self (object reference to it self. Do not use $this, but $self instead)
|
|
|
|
* - $in (the content of $in has to en/decrypt by the generated code)
|
|
|
|
* - The callback function should not use the 'return' statement, but en/decrypt'ing the content of $in only
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::setup()
|
|
|
|
* @see self::createInlineCryptFunction()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::decrypt()
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
2014-01-31 00:13:14 +01:00
|
|
|
* @internal If a Crypt_* class providing inline crypting it must extend _setupInlineCrypt()
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-08 16:20:19 +01:00
|
|
|
//protected function setupInlineCrypt();
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the performance-optimized function for en/decrypt()
|
|
|
|
*
|
|
|
|
* Internally for phpseclib developers:
|
|
|
|
*
|
2013-05-25 04:05:24 +02:00
|
|
|
* _createInlineCryptFunction():
|
|
|
|
*
|
2013-05-25 09:08:17 +02:00
|
|
|
* - merge the $cipher_code [setup'ed by _setupInlineCrypt()]
|
2013-05-25 04:05:24 +02:00
|
|
|
* with the current [$this->]mode of operation code
|
|
|
|
*
|
|
|
|
* - create the $inline function, which called by encrypt() / decrypt()
|
|
|
|
* as its replacement to speed up the en/decryption operations.
|
|
|
|
*
|
|
|
|
* - return the name of the created $inline callback function
|
|
|
|
*
|
|
|
|
* - used to speed up en/decryption
|
|
|
|
*
|
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
|
|
|
* The main reason why can speed up things [up to 50%] this way are:
|
2013-05-25 04:05:24 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* - using variables more effective then regular.
|
|
|
|
* (ie no use of expensive arrays but integers $k_0, $k_1 ...
|
|
|
|
* or even, for example, the pure $key[] values hardcoded)
|
|
|
|
*
|
|
|
|
* - avoiding 1000's of function calls of ie _encryptBlock()
|
|
|
|
* but inlining the crypt operations.
|
|
|
|
* in the mode of operation for() loop.
|
|
|
|
*
|
|
|
|
* - full loop unroll the (sometimes key-dependent) rounds
|
|
|
|
* avoiding this way ++$i counters and runtime-if's etc...
|
|
|
|
*
|
|
|
|
* The basic code architectur of the generated $inline en/decrypt()
|
|
|
|
* lambda function, in pseudo php, is:
|
|
|
|
*
|
2013-05-25 04:05:24 +02:00
|
|
|
* <code>
|
|
|
|
* +----------------------------------------------------------------------------------------------+
|
|
|
|
* | callback $inline = create_function: |
|
|
|
|
* | lambda_function_0001_crypt_ECB($action, $text) |
|
|
|
|
* | { |
|
|
|
|
* | INSERT PHP CODE OF: |
|
|
|
|
* | $cipher_code['init_crypt']; // general init code. |
|
|
|
|
* | // ie: $sbox'es declarations used for |
|
|
|
|
* | // encrypt and decrypt'ing. |
|
|
|
|
* | |
|
|
|
|
* | switch ($action) { |
|
|
|
|
* | case 'encrypt': |
|
|
|
|
* | INSERT PHP CODE OF: |
|
|
|
|
* | $cipher_code['init_encrypt']; // encrypt sepcific init code. |
|
|
|
|
* | ie: specified $key or $box |
|
|
|
|
* | declarations for encrypt'ing. |
|
|
|
|
* | |
|
|
|
|
* | foreach ($ciphertext) { |
|
|
|
|
* | $in = $block_size of $ciphertext; |
|
|
|
|
* | |
|
|
|
|
* | INSERT PHP CODE OF: |
|
|
|
|
* | $cipher_code['encrypt_block']; // encrypt's (string) $in, which is always: |
|
|
|
|
* | // strlen($in) == $this->block_size |
|
|
|
|
* | // here comes the cipher algorithm in action |
|
|
|
|
* | // for encryption. |
|
2013-06-26 06:50:40 +02:00
|
|
|
* | // $cipher_code['encrypt_block'] has to |
|
|
|
|
* | // encrypt the content of the $in variable |
|
2013-05-25 04:05:24 +02:00
|
|
|
* | |
|
|
|
|
* | $plaintext .= $in; |
|
|
|
|
* | } |
|
|
|
|
* | return $plaintext; |
|
|
|
|
* | |
|
|
|
|
* | case 'decrypt': |
|
|
|
|
* | INSERT PHP CODE OF: |
|
|
|
|
* | $cipher_code['init_decrypt']; // decrypt sepcific init code |
|
|
|
|
* | ie: specified $key or $box |
|
|
|
|
* | declarations for decrypt'ing. |
|
|
|
|
* | foreach ($plaintext) { |
|
|
|
|
* | $in = $block_size of $plaintext; |
|
|
|
|
* | |
|
|
|
|
* | INSERT PHP CODE OF: |
|
|
|
|
* | $cipher_code['decrypt_block']; // decrypt's (string) $in, which is always |
|
|
|
|
* | // strlen($in) == $this->block_size |
|
|
|
|
* | // here comes the cipher algorithm in action |
|
|
|
|
* | // for decryption. |
|
2013-06-26 06:50:40 +02:00
|
|
|
* | // $cipher_code['decrypt_block'] has to |
|
|
|
|
* | // decrypt the content of the $in variable |
|
2013-05-25 04:05:24 +02:00
|
|
|
* | $ciphertext .= $in; |
|
|
|
|
* | } |
|
|
|
|
* | return $ciphertext; |
|
|
|
|
* | } |
|
|
|
|
* | } |
|
|
|
|
* +----------------------------------------------------------------------------------------------+
|
|
|
|
* </code>
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* See also the \phpseclib\Crypt\*::_setupInlineCrypt()'s for
|
2013-05-20 08:19:38 +02:00
|
|
|
* productive inline $cipher_code's how they works.
|
|
|
|
*
|
|
|
|
* Structure of:
|
2013-05-25 04:05:24 +02:00
|
|
|
* <code>
|
2016-11-30 22:19:23 +01:00
|
|
|
* $cipher_code = [
|
2013-05-20 08:19:38 +02:00
|
|
|
* 'init_crypt' => (string) '', // optional
|
|
|
|
* 'init_encrypt' => (string) '', // optional
|
|
|
|
* 'init_decrypt' => (string) '', // optional
|
|
|
|
* 'encrypt_block' => (string) '', // required
|
|
|
|
* 'decrypt_block' => (string) '' // required
|
2016-11-30 22:19:23 +01:00
|
|
|
* ];
|
2013-05-25 04:05:24 +02:00
|
|
|
* </code>
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see self::setupInlineCrypt()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::decrypt()
|
|
|
|
* @param array $cipher_code
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return string (the name of the created callback function)
|
2013-05-20 08:19:38 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected function createInlineCryptFunction($cipher_code)
|
2013-05-20 08:19:38 +02:00
|
|
|
{
|
|
|
|
$block_size = $this->block_size;
|
|
|
|
|
|
|
|
// optional
|
|
|
|
$init_crypt = isset($cipher_code['init_crypt']) ? $cipher_code['init_crypt'] : '';
|
|
|
|
$init_encrypt = isset($cipher_code['init_encrypt']) ? $cipher_code['init_encrypt'] : '';
|
|
|
|
$init_decrypt = isset($cipher_code['init_decrypt']) ? $cipher_code['init_decrypt'] : '';
|
|
|
|
// required
|
|
|
|
$encrypt_block = $cipher_code['encrypt_block'];
|
|
|
|
$decrypt_block = $cipher_code['decrypt_block'];
|
|
|
|
|
|
|
|
// Generating mode of operation inline code,
|
|
|
|
// merged with the $cipher_code algorithm
|
|
|
|
// for encrypt- and decryption.
|
|
|
|
switch ($this->mode) {
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_ECB:
|
2013-05-20 08:19:38 +02:00
|
|
|
$encrypt = $init_encrypt . '
|
2013-06-26 06:50:40 +02:00
|
|
|
$_ciphertext = "";
|
|
|
|
$_plaintext_len = strlen($_text);
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2013-06-26 06:50:40 +02:00
|
|
|
for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
|
|
|
|
$in = substr($_text, $_i, '.$block_size.');
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
$_ciphertext.= $in;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2013-06-26 06:50:40 +02:00
|
|
|
return $_ciphertext;
|
2013-05-20 08:19:38 +02:00
|
|
|
';
|
|
|
|
|
|
|
|
$decrypt = $init_decrypt . '
|
2013-06-26 06:50:40 +02:00
|
|
|
$_plaintext = "";
|
|
|
|
$_text = str_pad($_text, strlen($_text) + ('.$block_size.' - strlen($_text) % '.$block_size.') % '.$block_size.', chr(0));
|
|
|
|
$_ciphertext_len = strlen($_text);
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2013-06-26 06:50:40 +02:00
|
|
|
for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
|
|
|
|
$in = substr($_text, $_i, '.$block_size.');
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$decrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
$_plaintext.= $in;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
return $this->unpad($_plaintext);
|
2013-05-20 08:19:38 +02:00
|
|
|
';
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_CTR:
|
2013-05-20 08:19:38 +02:00
|
|
|
$encrypt = $init_encrypt . '
|
2013-06-26 06:50:40 +02:00
|
|
|
$_ciphertext = "";
|
|
|
|
$_plaintext_len = strlen($_text);
|
2016-12-03 21:39:11 +01:00
|
|
|
$_xor = $this->encryptIV;
|
|
|
|
$_buffer = &$this->enbuffer;
|
2014-11-30 05:26:42 +01:00
|
|
|
if (strlen($_buffer["ciphertext"])) {
|
2013-06-26 06:50:40 +02:00
|
|
|
for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
|
|
|
|
$_block = substr($_text, $_i, '.$block_size.');
|
2014-11-30 05:26:42 +01:00
|
|
|
if (strlen($_block) > strlen($_buffer["ciphertext"])) {
|
2014-11-29 14:39:21 +01:00
|
|
|
$in = $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2018-12-27 15:31:35 +01:00
|
|
|
\phpseclib\Common\Functions\Strings::increment_str($_xor);
|
2014-11-30 05:26:42 +01:00
|
|
|
$_buffer["ciphertext"].= $in;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2016-07-31 05:18:06 +02:00
|
|
|
$_key = \phpseclib\Common\Functions\Strings::shift($_buffer["ciphertext"], '.$block_size.');
|
2013-06-26 06:50:40 +02:00
|
|
|
$_ciphertext.= $_block ^ $_key;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-06-26 06:50:40 +02:00
|
|
|
for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
|
|
|
|
$_block = substr($_text, $_i, '.$block_size.');
|
2014-11-29 14:39:21 +01:00
|
|
|
$in = $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2018-12-27 15:31:35 +01:00
|
|
|
\phpseclib\Common\Functions\Strings::increment_str($_xor);
|
2013-06-26 06:50:40 +02:00
|
|
|
$_key = $in;
|
|
|
|
$_ciphertext.= $_block ^ $_key;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-03 21:39:11 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$this->encryptIV = $_xor;
|
2013-06-26 06:50:40 +02:00
|
|
|
if ($_start = $_plaintext_len % '.$block_size.') {
|
2014-11-30 05:26:42 +01:00
|
|
|
$_buffer["ciphertext"] = substr($_key, $_start) . $_buffer["ciphertext"];
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 06:50:40 +02:00
|
|
|
return $_ciphertext;
|
2013-05-20 08:19:38 +02:00
|
|
|
';
|
|
|
|
|
|
|
|
$decrypt = $init_encrypt . '
|
2013-06-26 06:50:40 +02:00
|
|
|
$_plaintext = "";
|
|
|
|
$_ciphertext_len = strlen($_text);
|
2016-12-03 21:39:11 +01:00
|
|
|
$_xor = $this->decryptIV;
|
|
|
|
$_buffer = &$this->debuffer;
|
2013-06-26 06:50:40 +02:00
|
|
|
|
|
|
|
if (strlen($_buffer["ciphertext"])) {
|
|
|
|
for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
|
|
|
|
$_block = substr($_text, $_i, '.$block_size.');
|
|
|
|
if (strlen($_block) > strlen($_buffer["ciphertext"])) {
|
2014-11-29 14:39:21 +01:00
|
|
|
$in = $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2018-12-27 15:31:35 +01:00
|
|
|
\phpseclib\Common\Functions\Strings::increment_str($_xor);
|
2013-06-26 06:50:40 +02:00
|
|
|
$_buffer["ciphertext"].= $in;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2016-07-31 05:18:06 +02:00
|
|
|
$_key = \phpseclib\Common\Functions\Strings::shift($_buffer["ciphertext"], '.$block_size.');
|
2013-06-26 06:50:40 +02:00
|
|
|
$_plaintext.= $_block ^ $_key;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-06-26 06:50:40 +02:00
|
|
|
for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
|
|
|
|
$_block = substr($_text, $_i, '.$block_size.');
|
2014-11-29 14:39:21 +01:00
|
|
|
$in = $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2018-12-27 15:31:35 +01:00
|
|
|
\phpseclib\Common\Functions\Strings::increment_str($_xor);
|
2013-06-26 06:50:40 +02:00
|
|
|
$_key = $in;
|
|
|
|
$_plaintext.= $_block ^ $_key;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-03 21:39:11 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$this->decryptIV = $_xor;
|
2013-06-26 06:50:40 +02:00
|
|
|
if ($_start = $_ciphertext_len % '.$block_size.') {
|
|
|
|
$_buffer["ciphertext"] = substr($_key, $_start) . $_buffer["ciphertext"];
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 06:50:40 +02:00
|
|
|
return $_plaintext;
|
2013-05-20 08:19:38 +02:00
|
|
|
';
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_CFB:
|
2013-05-20 08:19:38 +02:00
|
|
|
$encrypt = $init_encrypt . '
|
2013-06-26 06:50:40 +02:00
|
|
|
$_ciphertext = "";
|
2016-12-03 21:39:11 +01:00
|
|
|
$_buffer = &$this->enbuffer;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$_iv = &$this->encryptIV;
|
2013-06-26 06:50:40 +02:00
|
|
|
$_pos = &$_buffer["pos"];
|
2013-05-20 08:19:38 +02:00
|
|
|
} else {
|
2016-12-03 21:39:11 +01:00
|
|
|
$_iv = $this->encryptIV;
|
2013-06-26 06:50:40 +02:00
|
|
|
$_pos = 0;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
$_len = strlen($_text);
|
|
|
|
$_i = 0;
|
|
|
|
if ($_pos) {
|
|
|
|
$_orig_pos = $_pos;
|
|
|
|
$_max = '.$block_size.' - $_pos;
|
|
|
|
if ($_len >= $_max) {
|
|
|
|
$_i = $_max;
|
|
|
|
$_len-= $_max;
|
|
|
|
$_pos = 0;
|
2013-05-20 08:19:38 +02:00
|
|
|
} else {
|
2013-06-26 06:50:40 +02:00
|
|
|
$_i = $_len;
|
|
|
|
$_pos+= $_len;
|
|
|
|
$_len = 0;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
$_ciphertext = substr($_iv, $_orig_pos) ^ $_text;
|
|
|
|
$_iv = substr_replace($_iv, $_ciphertext, $_orig_pos, $_i);
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
while ($_len >= '.$block_size.') {
|
|
|
|
$in = $_iv;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.';
|
2013-06-26 06:50:40 +02:00
|
|
|
$_iv = $in ^ substr($_text, $_i, '.$block_size.');
|
|
|
|
$_ciphertext.= $_iv;
|
|
|
|
$_len-= '.$block_size.';
|
|
|
|
$_i+= '.$block_size.';
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
if ($_len) {
|
|
|
|
$in = $_iv;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
$_iv = $in;
|
|
|
|
$_block = $_iv ^ substr($_text, $_i);
|
|
|
|
$_iv = substr_replace($_iv, $_block, 0, $_len);
|
|
|
|
$_ciphertext.= $_block;
|
|
|
|
$_pos = $_len;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
return $_ciphertext;
|
2013-05-20 08:19:38 +02:00
|
|
|
';
|
|
|
|
|
|
|
|
$decrypt = $init_encrypt . '
|
2013-06-26 06:50:40 +02:00
|
|
|
$_plaintext = "";
|
2016-12-03 21:39:11 +01:00
|
|
|
$_buffer = &$this->debuffer;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$_iv = &$this->decryptIV;
|
2013-06-26 06:50:40 +02:00
|
|
|
$_pos = &$_buffer["pos"];
|
2013-05-20 08:19:38 +02:00
|
|
|
} else {
|
2016-12-03 21:39:11 +01:00
|
|
|
$_iv = $this->decryptIV;
|
2013-06-26 06:50:40 +02:00
|
|
|
$_pos = 0;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
$_len = strlen($_text);
|
|
|
|
$_i = 0;
|
|
|
|
if ($_pos) {
|
|
|
|
$_orig_pos = $_pos;
|
|
|
|
$_max = '.$block_size.' - $_pos;
|
|
|
|
if ($_len >= $_max) {
|
|
|
|
$_i = $_max;
|
|
|
|
$_len-= $_max;
|
|
|
|
$_pos = 0;
|
2013-05-20 08:19:38 +02:00
|
|
|
} else {
|
2013-06-26 06:50:40 +02:00
|
|
|
$_i = $_len;
|
|
|
|
$_pos+= $_len;
|
|
|
|
$_len = 0;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
$_plaintext = substr($_iv, $_orig_pos) ^ $_text;
|
|
|
|
$_iv = substr_replace($_iv, substr($_text, 0, $_i), $_orig_pos, $_i);
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
while ($_len >= '.$block_size.') {
|
|
|
|
$in = $_iv;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
$_iv = $in;
|
|
|
|
$cb = substr($_text, $_i, '.$block_size.');
|
|
|
|
$_plaintext.= $_iv ^ $cb;
|
|
|
|
$_iv = $cb;
|
|
|
|
$_len-= '.$block_size.';
|
|
|
|
$_i+= '.$block_size.';
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
if ($_len) {
|
|
|
|
$in = $_iv;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
$_iv = $in;
|
|
|
|
$_plaintext.= $_iv ^ substr($_text, $_i);
|
|
|
|
$_iv = substr_replace($_iv, substr($_text, $_i), 0, $_len);
|
|
|
|
$_pos = $_len;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 12:33:34 +02:00
|
|
|
return $_plaintext;
|
|
|
|
';
|
|
|
|
break;
|
|
|
|
case self::MODE_CFB8:
|
|
|
|
$encrypt = $init_encrypt . '
|
|
|
|
$_ciphertext = "";
|
|
|
|
$_len = strlen($_text);
|
|
|
|
$_iv = $this->encryptIV;
|
|
|
|
|
|
|
|
for ($_i = 0; $_i < $_len; ++$_i) {
|
|
|
|
$in = $_iv;
|
|
|
|
'.$encrypt_block.'
|
|
|
|
$_ciphertext .= ($_c = $_text[$_i] ^ $in);
|
2017-10-10 12:06:14 +02:00
|
|
|
$_iv = substr($_iv, 1) . $_c;
|
2017-10-04 12:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
if ($_len >= '.$block_size.') {
|
|
|
|
$this->encryptIV = substr($_ciphertext, -'.$block_size.');
|
|
|
|
} else {
|
|
|
|
$this->encryptIV = substr($this->encryptIV, $_len - '.$block_size.') . substr($_ciphertext, -$_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $_ciphertext;
|
|
|
|
';
|
|
|
|
$decrypt = $init_encrypt . '
|
|
|
|
$_plaintext = "";
|
|
|
|
$_len = strlen($_text);
|
|
|
|
$_iv = $this->decryptIV;
|
|
|
|
|
|
|
|
for ($_i = 0; $_i < $_len; ++$_i) {
|
|
|
|
$in = $_iv;
|
|
|
|
'.$encrypt_block.'
|
|
|
|
$_plaintext .= $_text[$_i] ^ $in;
|
2017-10-10 12:06:14 +02:00
|
|
|
$_iv = substr($_iv, 1) . $_text[$_i];
|
2017-10-04 12:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
if ($_len >= '.$block_size.') {
|
|
|
|
$this->decryptIV = substr($_text, -'.$block_size.');
|
|
|
|
} else {
|
|
|
|
$this->decryptIV = substr($this->decryptIV, $_len - '.$block_size.') . substr($_text, -$_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 06:50:40 +02:00
|
|
|
return $_plaintext;
|
2013-05-20 08:19:38 +02:00
|
|
|
';
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_OFB:
|
2013-05-20 08:19:38 +02:00
|
|
|
$encrypt = $init_encrypt . '
|
2013-06-26 06:50:40 +02:00
|
|
|
$_ciphertext = "";
|
|
|
|
$_plaintext_len = strlen($_text);
|
2016-12-03 21:39:11 +01:00
|
|
|
$_xor = $this->encryptIV;
|
|
|
|
$_buffer = &$this->enbuffer;
|
2013-06-26 06:50:40 +02:00
|
|
|
|
|
|
|
if (strlen($_buffer["xor"])) {
|
|
|
|
for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
|
|
|
|
$_block = substr($_text, $_i, '.$block_size.');
|
|
|
|
if (strlen($_block) > strlen($_buffer["xor"])) {
|
|
|
|
$in = $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
$_xor = $in;
|
|
|
|
$_buffer["xor"].= $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2016-07-31 05:18:06 +02:00
|
|
|
$_key = \phpseclib\Common\Functions\Strings::shift($_buffer["xor"], '.$block_size.');
|
2013-06-26 06:50:40 +02:00
|
|
|
$_ciphertext.= $_block ^ $_key;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-06-26 06:50:40 +02:00
|
|
|
for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
|
|
|
|
$in = $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
$_xor = $in;
|
|
|
|
$_ciphertext.= substr($_text, $_i, '.$block_size.') ^ $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
$_key = $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2016-12-03 21:39:11 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$this->encryptIV = $_xor;
|
2013-06-26 06:50:40 +02:00
|
|
|
if ($_start = $_plaintext_len % '.$block_size.') {
|
|
|
|
$_buffer["xor"] = substr($_key, $_start) . $_buffer["xor"];
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
return $_ciphertext;
|
2013-05-20 08:19:38 +02:00
|
|
|
';
|
|
|
|
|
|
|
|
$decrypt = $init_encrypt . '
|
2013-06-26 06:50:40 +02:00
|
|
|
$_plaintext = "";
|
|
|
|
$_ciphertext_len = strlen($_text);
|
2016-12-03 21:39:11 +01:00
|
|
|
$_xor = $this->decryptIV;
|
|
|
|
$_buffer = &$this->debuffer;
|
2013-06-26 06:50:40 +02:00
|
|
|
|
|
|
|
if (strlen($_buffer["xor"])) {
|
|
|
|
for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
|
|
|
|
$_block = substr($_text, $_i, '.$block_size.');
|
|
|
|
if (strlen($_block) > strlen($_buffer["xor"])) {
|
|
|
|
$in = $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
$_xor = $in;
|
|
|
|
$_buffer["xor"].= $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2016-07-31 05:18:06 +02:00
|
|
|
$_key = \phpseclib\Common\Functions\Strings::shift($_buffer["xor"], '.$block_size.');
|
2013-06-26 06:50:40 +02:00
|
|
|
$_plaintext.= $_block ^ $_key;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-06-26 06:50:40 +02:00
|
|
|
for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
|
|
|
|
$in = $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
$_xor = $in;
|
|
|
|
$_plaintext.= substr($_text, $_i, '.$block_size.') ^ $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
$_key = $_xor;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
2016-12-03 21:39:11 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$this->decryptIV = $_xor;
|
2013-06-26 06:50:40 +02:00
|
|
|
if ($_start = $_ciphertext_len % '.$block_size.') {
|
|
|
|
$_buffer["xor"] = substr($_key, $_start) . $_buffer["xor"];
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-26 06:50:40 +02:00
|
|
|
return $_plaintext;
|
2013-05-20 08:19:38 +02:00
|
|
|
';
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_STREAM:
|
2013-05-20 08:19:38 +02:00
|
|
|
$encrypt = $init_encrypt . '
|
2013-06-26 06:50:40 +02:00
|
|
|
$_ciphertext = "";
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
return $_ciphertext;
|
2013-05-20 08:19:38 +02:00
|
|
|
';
|
|
|
|
$decrypt = $init_decrypt . '
|
2013-06-26 06:50:40 +02:00
|
|
|
$_plaintext = "";
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$decrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
return $_plaintext;
|
2013-05-20 08:19:38 +02:00
|
|
|
';
|
|
|
|
break;
|
2014-12-09 19:46:30 +01:00
|
|
|
// case self::MODE_CBC:
|
2013-05-20 08:19:38 +02:00
|
|
|
default:
|
|
|
|
$encrypt = $init_encrypt . '
|
2013-06-26 06:50:40 +02:00
|
|
|
$_ciphertext = "";
|
|
|
|
$_plaintext_len = strlen($_text);
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
$in = $this->encryptIV;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2013-06-26 06:50:40 +02:00
|
|
|
for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
|
|
|
|
$in = substr($_text, $_i, '.$block_size.') ^ $in;
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$encrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
$_ciphertext.= $in;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$this->encryptIV = $in;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2013-06-26 06:50:40 +02:00
|
|
|
return $_ciphertext;
|
2013-05-20 08:19:38 +02:00
|
|
|
';
|
|
|
|
|
|
|
|
$decrypt = $init_decrypt . '
|
2013-06-26 06:50:40 +02:00
|
|
|
$_plaintext = "";
|
|
|
|
$_text = str_pad($_text, strlen($_text) + ('.$block_size.' - strlen($_text) % '.$block_size.') % '.$block_size.', chr(0));
|
|
|
|
$_ciphertext_len = strlen($_text);
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
$_iv = $this->decryptIV;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2013-06-26 06:50:40 +02:00
|
|
|
for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
|
|
|
|
$in = $_block = substr($_text, $_i, '.$block_size.');
|
2013-05-20 08:19:38 +02:00
|
|
|
'.$decrypt_block.'
|
2013-06-26 06:50:40 +02:00
|
|
|
$_plaintext.= $in ^ $_iv;
|
|
|
|
$_iv = $_block;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
if ($this->continuousBuffer) {
|
|
|
|
$this->decryptIV = $_iv;
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-03 21:39:11 +01:00
|
|
|
return $this->unpad($_plaintext);
|
2013-05-20 08:19:38 +02:00
|
|
|
';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-10-09 08:35:56 +02:00
|
|
|
// Before discrediting this, please read the following:
|
|
|
|
// @see https://github.com/phpseclib/phpseclib/issues/1293
|
|
|
|
// @see https://github.com/phpseclib/phpseclib/pull/1143
|
2016-12-03 21:39:11 +01:00
|
|
|
eval('$func = function ($_action, $_text) { ' . $init_crypt . 'if ($_action == "encrypt") { ' . $encrypt . ' } else { ' . $decrypt . ' }};');
|
|
|
|
|
2017-06-28 14:30:13 +02:00
|
|
|
return \Closure::bind($func, $this, static::class);
|
2016-12-03 21:39:11 +01:00
|
|
|
}
|
2017-12-17 21:52:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert float to int
|
|
|
|
*
|
2017-12-17 22:05:15 +01:00
|
|
|
* On ARM CPUs converting floats to ints doesn't always work
|
2017-12-17 21:52:43 +01:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param string $x
|
|
|
|
* @return int
|
|
|
|
*/
|
2017-12-17 23:25:30 +01:00
|
|
|
protected static function safe_intval($x)
|
2017-12-17 21:52:43 +01:00
|
|
|
{
|
|
|
|
switch (true) {
|
|
|
|
case is_int($x):
|
|
|
|
// PHP 5.3, per http://php.net/releases/5_3_0.php, introduced "more consistent float rounding"
|
2017-12-17 22:05:15 +01:00
|
|
|
case (php_uname('m') & "\xDF\xDF\xDF") != 'ARM':
|
2017-12-17 21:52:43 +01:00
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
return (fmod($x, 0x80000000) & 0x7FFFFFFF) |
|
|
|
|
((fmod(floor($x / 0x80000000), 2) & 1) << 31);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* eval()'able string for in-line float to int
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-12-17 23:25:30 +01:00
|
|
|
protected static function safe_intval_inline()
|
2017-12-17 21:52:43 +01:00
|
|
|
{
|
|
|
|
switch (true) {
|
|
|
|
case defined('PHP_INT_SIZE') && PHP_INT_SIZE == 8:
|
2017-12-17 22:05:15 +01:00
|
|
|
case (php_uname('m') & "\xDF\xDF\xDF") != 'ARM':
|
2017-12-17 21:52:43 +01:00
|
|
|
return '%s';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$safeint = '(is_int($temp = %s) ? $temp : (fmod($temp, 0x80000000) & 0x7FFFFFFF) | ';
|
|
|
|
return $safeint . '((fmod(floor($temp / 0x80000000), 2) & 1) << 31))';
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 15:31:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up GCM parameters
|
|
|
|
*
|
|
|
|
* See steps 1-2 of https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf#page=23
|
|
|
|
* for more info
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private function setupGCM()
|
|
|
|
{
|
|
|
|
// don't keep on re-calculating $this->h
|
|
|
|
if (!$this->h || $this->h->key != $this->key) {
|
|
|
|
$cipher = new static('ecb');
|
|
|
|
$cipher->setKey($this->key);
|
|
|
|
$cipher->disablePadding();
|
|
|
|
|
|
|
|
$this->h = self::$gcmField->newInteger(
|
|
|
|
Strings::switchEndianness($cipher->encrypt("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"))
|
|
|
|
);
|
|
|
|
$this->h->key = $this->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($this->nonce) == 12) {
|
|
|
|
$this->iv = $this->nonce . "\0\0\0\1";
|
|
|
|
} else {
|
|
|
|
$s = 16 * ceil(strlen($this->nonce) / 16) - strlen($this->nonce);
|
|
|
|
$this->iv = $this->ghash(
|
|
|
|
self::nullPad128($this->nonce) . str_repeat("\0", 8) . self::len64($this->nonce)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs GHASH operation
|
|
|
|
*
|
|
|
|
* See https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf#page=20
|
|
|
|
* for more info
|
|
|
|
*
|
|
|
|
* @see self::decrypt()
|
|
|
|
* @see self::encrypt()
|
|
|
|
* @access private
|
|
|
|
* @param string $x
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function ghash($x)
|
|
|
|
{
|
|
|
|
$h = $this->h;
|
|
|
|
$y = ["\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"];
|
|
|
|
$x = str_split($x, 16);
|
|
|
|
$n = 0;
|
|
|
|
// the switchEndianness calls are necessary because the multiplication algorithm in BinaryField/Integer
|
|
|
|
// interprets strings as polynomials in big endian order whereas in GCM they're interpreted in little
|
|
|
|
// endian order per https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf#page=19.
|
|
|
|
// big endian order is what binary field elliptic curves use per http://www.secg.org/sec1-v2.pdf#page=18.
|
|
|
|
|
|
|
|
// we could switchEndianness here instead of in the while loop but doing so in the while loop seems like it
|
|
|
|
// might be slightly more performant
|
|
|
|
//$x = Strings::switchEndianness($x);
|
|
|
|
foreach ($x as $xn) {
|
|
|
|
$xn = Strings::switchEndianness($xn);
|
|
|
|
$t = $y[$n] ^ $xn;
|
|
|
|
$temp = self::$gcmField->newInteger($t);
|
|
|
|
$y[++$n] = $temp->multiply($h)->toBytes();
|
|
|
|
$y[$n] = substr($y[$n], 1);
|
|
|
|
}
|
|
|
|
$y[$n] = Strings::switchEndianness($y[$n]);
|
|
|
|
return $y[$n];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the bit length of a string in a packed format
|
|
|
|
*
|
|
|
|
* @see self::decrypt()
|
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::setupGCM()
|
|
|
|
* @access private
|
|
|
|
* @param string $str
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function len64($str)
|
|
|
|
{
|
|
|
|
return "\0\0\0\0" . pack('N', 8 * strlen($str));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NULL pads a string to be a multiple of 128
|
|
|
|
*
|
|
|
|
* @see self::decrypt()
|
|
|
|
* @see self::encrypt()
|
|
|
|
* @see self::setupGCM()
|
|
|
|
* @access private
|
|
|
|
* @param string $str
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function nullPad128($str)
|
|
|
|
{
|
|
|
|
$len = strlen($str);
|
|
|
|
return $str . str_repeat("\0", 16 * ceil($len / 16) - $len);
|
|
|
|
}
|
2013-05-20 08:19:38 +02:00
|
|
|
}
|