Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of https://github.com/zendframework/zf2 into ad…
Browse files Browse the repository at this point in the history
…d_maxlength_to_param_container
  • Loading branch information
Show file tree
Hide file tree
Showing 51 changed files with 327 additions and 300 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
},
"require": {
"php": ">=5.3.3",
"php": ">=5.3.23",
"zendframework/zend-math": "self.version",
"zendframework/zend-stdlib": "self.version",
"zendframework/zend-servicemanager": "self.version"
Expand All @@ -23,8 +23,8 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.2-dev",
"dev-develop": "2.3-dev"
"dev-master": "2.3-dev",
"dev-develop": "2.4-dev"
}
},
"autoload-dev": {
Expand Down
103 changes: 73 additions & 30 deletions src/BlockCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Crypt;

use Zend\Crypt\Hmac;
use Zend\Crypt\Key\Derivation\Pbkdf2;
use Zend\Crypt\Symmetric\SymmetricInterface;
use Zend\Crypt\Utils;
use Zend\Math\Rand;

/**
* Encrypt using a symmetric cipher then authenticate using HMAC (SHA-256)
*/
class BlockCipher
{
const KEY_DERIV_HMAC = 'sha256';
/**
* Hash algorithm for Pbkdf2
*
* @var string
*/
protected $pbkdf2Hash = 'sha256';

/**
* Symmetric cipher
Expand All @@ -37,7 +40,7 @@ class BlockCipher
protected static $symmetricPlugins = null;

/**
* Hash algorithm fot HMAC
* Hash algorithm for HMAC
*
* @var string
*/
Expand Down Expand Up @@ -74,7 +77,7 @@ class BlockCipher
/**
* Constructor
*
* @param SymmetricInterface $cipher
* @param SymmetricInterface $cipher
*/
public function __construct(SymmetricInterface $cipher)
{
Expand Down Expand Up @@ -162,7 +165,7 @@ public function getCipher()
/**
* Set the number of iterations for Pbkdf2
*
* @param int $num
* @param int $num
* @return BlockCipher
*/
public function setKeyIteration($num)
Expand All @@ -185,7 +188,7 @@ public function getKeyIteration()
/**
* Set the salt (IV)
*
* @param string $salt
* @param string $salt
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
Expand Down Expand Up @@ -224,7 +227,7 @@ public function getOriginalSalt()
/**
* Enable/disable the binary output
*
* @param bool $value
* @param bool $value
* @return BlockCipher
*/
public function setBinaryOutput($value)
Expand Down Expand Up @@ -274,7 +277,7 @@ public function getKey()
/**
* Set algorithm of the symmetric cipher
*
* @param string $algo
* @param string $algo
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
Expand Down Expand Up @@ -323,7 +326,7 @@ public function getCipherSupportedAlgorithms()
/**
* Set the hash algorithm for HMAC authentication
*
* @param string $hash
* @param string $hash
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
Expand All @@ -349,18 +352,58 @@ public function getHashAlgorithm()
return $this->hash;
}

/**
* Set the hash algorithm for the Pbkdf2
*
* @param string $hash
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
public function setPbkdf2HashAlgorithm($hash)
{
if (!Hash::isSupported($hash)) {
throw new Exception\InvalidArgumentException(
"The specified hash algorithm '{$hash}' is not supported by Zend\Crypt\Hash"
);
}
$this->pbkdf2Hash = $hash;

return $this;
}

/**
* Get the Pbkdf2 hash algorithm
*
* @return string
*/
public function getPbkdf2HashAlgorithm()
{
return $this->pbkdf2Hash;
}

/**
* Encrypt then authenticate using HMAC
*
* @param string $data
* @param string $data
* @return string
* @throws Exception\InvalidArgumentException
*/
public function encrypt($data)
{
if (empty($data)) {
// 0 (as integer), 0.0 (as float) & '0' (as string) will return false, though these should be allowed
// Must be a string, integer, or float in order to encrypt
if ((is_string($data) && $data === '')
|| is_array($data)
|| is_object($data)
) {
throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty');
}

// Cast to string prior to encrypting
if (!is_string($data)) {
$data = (string) $data;
}

if (empty($this->cipher)) {
throw new Exception\InvalidArgumentException('No symmetric cipher specified');
}
Expand All @@ -373,21 +416,21 @@ public function encrypt($data)
$this->cipher->setSalt(Rand::getBytes($this->cipher->getSaltSize(), true));
}
// generate the encryption key and the HMAC key for the authentication
$hash = Pbkdf2::calc(self::KEY_DERIV_HMAC,
$this->getKey(),
$this->getSalt(),
$this->keyIteration,
$keySize * 2);
$hash = Pbkdf2::calc(
$this->getPbkdf2HashAlgorithm(),
$this->getKey(),
$this->getSalt(),
$this->keyIteration,
$keySize * 2
);
// set the encryption key
$this->cipher->setKey(substr($hash, 0, $keySize));
// set the key for HMAC
$keyHmac = substr($hash, $keySize);
// encryption
$ciphertext = $this->cipher->encrypt($data);
// HMAC
$hmac = Hmac::compute($keyHmac,
$this->hash,
$this->cipher->getAlgorithm() . $ciphertext);
$hmac = Hmac::compute($keyHmac, $this->hash, $this->cipher->getAlgorithm() . $ciphertext);
if (!$this->binaryOutput) {
$ciphertext = base64_encode($ciphertext);
}
Expand All @@ -398,7 +441,7 @@ public function encrypt($data)
/**
* Decrypt
*
* @param string $data
* @param string $data
* @return string|bool
* @throws Exception\InvalidArgumentException
*/
Expand All @@ -425,18 +468,18 @@ public function decrypt($data)
$iv = substr($ciphertext, 0, $this->cipher->getSaltSize());
$keySize = $this->cipher->getKeySize();
// generate the encryption key and the HMAC key for the authentication
$hash = Pbkdf2::calc(self::KEY_DERIV_HMAC,
$this->getKey(),
$iv,
$this->keyIteration,
$keySize * 2);
$hash = Pbkdf2::calc(
$this->getPbkdf2HashAlgorithm(),
$this->getKey(),
$iv,
$this->keyIteration,
$keySize * 2
);
// set the decryption key
$this->cipher->setKey(substr($hash, 0, $keySize));
// set the key for HMAC
$keyHmac = substr($hash, $keySize);
$hmacNew = Hmac::compute($keyHmac,
$this->hash,
$this->cipher->getAlgorithm() . $ciphertext);
$hmacNew = Hmac::compute($keyHmac, $this->hash, $this->cipher->getAlgorithm() . $ciphertext);
if (!Utils::compareStrings($hmacNew, $hmac)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
6 changes: 2 additions & 4 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand All @@ -12,8 +12,6 @@
/**
* Invalid argument exception
*/
class InvalidArgumentException
extends \InvalidArgumentException
implements ExceptionInterface
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
}
6 changes: 2 additions & 4 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand All @@ -12,8 +12,6 @@
/**
* Runtime argument exception
*/
class RuntimeException
extends \RuntimeException
implements ExceptionInterface
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Hmac.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
5 changes: 3 additions & 2 deletions src/Key/Derivation/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand All @@ -12,4 +12,5 @@
use Zend\Crypt\Exception\ExceptionInterface as Exception;

interface ExceptionInterface extends Exception
{}
{
}
5 changes: 3 additions & 2 deletions src/Key/Derivation/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand All @@ -16,4 +16,5 @@
*/
class InvalidArgumentException extends Exception\InvalidArgumentException implements
ExceptionInterface
{}
{
}
5 changes: 3 additions & 2 deletions src/Key/Derivation/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand All @@ -16,4 +16,5 @@
*/
class RuntimeException extends Exception\RuntimeException implements
ExceptionInterface
{}
{
}
2 changes: 1 addition & 1 deletion src/Key/Derivation/Pbkdf2.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
4 changes: 2 additions & 2 deletions src/Key/Derivation/SaltedS2k.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down Expand Up @@ -53,7 +53,7 @@ class SaltedS2k
public static function calc($hash, $password, $salt, $bytes)
{
if (!in_array($hash, array_keys(static::$supportedMhashAlgos))) {
throw new Exception\InvalidArgumentException("The hash algorihtm $hash is not supported by " . __CLASS__);
throw new Exception\InvalidArgumentException("The hash algorithm $hash is not supported by " . __CLASS__);
}
if (strlen($salt)<8) {
throw new Exception\InvalidArgumentException('The salt size must be at least of 8 bytes');
Expand Down
Loading

0 comments on commit f00c160

Please sign in to comment.