Libs changes for php 8.x
This commit is contained in:
@@ -14,11 +14,27 @@ class Crypto
|
||||
* @param bool $raw_binary
|
||||
*
|
||||
* @throws Ex\EnvironmentIsBrokenException
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function encrypt($plaintext, Key $key, $raw_binary = false)
|
||||
public static function encrypt($plaintext, $key, $raw_binary = false)
|
||||
{
|
||||
if (!\is_string($plaintext)) {
|
||||
throw new \TypeError(
|
||||
'String expected for argument 1. ' . \ucfirst(\gettype($plaintext)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
if (!($key instanceof Key)) {
|
||||
throw new \TypeError(
|
||||
'Key expected for argument 2. ' . \ucfirst(\gettype($key)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
if (!\is_bool($raw_binary)) {
|
||||
throw new \TypeError(
|
||||
'Boolean expected for argument 3. ' . \ucfirst(\gettype($raw_binary)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
return self::encryptInternal(
|
||||
$plaintext,
|
||||
KeyOrPassword::createFromKey($key),
|
||||
@@ -35,11 +51,32 @@ class Crypto
|
||||
* @param bool $raw_binary
|
||||
*
|
||||
* @throws Ex\EnvironmentIsBrokenException
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function encryptWithPassword($plaintext, $password, $raw_binary = false)
|
||||
public static function encryptWithPassword(
|
||||
$plaintext,
|
||||
#[\SensitiveParameter]
|
||||
$password,
|
||||
$raw_binary = false
|
||||
)
|
||||
{
|
||||
if (!\is_string($plaintext)) {
|
||||
throw new \TypeError(
|
||||
'String expected for argument 1. ' . \ucfirst(\gettype($plaintext)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
if (!\is_string($password)) {
|
||||
throw new \TypeError(
|
||||
'String expected for argument 2. ' . \ucfirst(\gettype($password)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
if (!\is_bool($raw_binary)) {
|
||||
throw new \TypeError(
|
||||
'Boolean expected for argument 3. ' . \ucfirst(\gettype($raw_binary)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
return self::encryptInternal(
|
||||
$plaintext,
|
||||
KeyOrPassword::createFromPassword($password),
|
||||
@@ -54,13 +91,29 @@ class Crypto
|
||||
* @param Key $key
|
||||
* @param bool $raw_binary
|
||||
*
|
||||
* @throws \TypeError
|
||||
* @throws Ex\EnvironmentIsBrokenException
|
||||
* @throws Ex\WrongKeyOrModifiedCiphertextException
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function decrypt($ciphertext, Key $key, $raw_binary = false)
|
||||
public static function decrypt($ciphertext, $key, $raw_binary = false)
|
||||
{
|
||||
if (!\is_string($ciphertext)) {
|
||||
throw new \TypeError(
|
||||
'String expected for argument 1. ' . \ucfirst(\gettype($ciphertext)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
if (!($key instanceof Key)) {
|
||||
throw new \TypeError(
|
||||
'Key expected for argument 2. ' . \ucfirst(\gettype($key)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
if (!\is_bool($raw_binary)) {
|
||||
throw new \TypeError(
|
||||
'Boolean expected for argument 3. ' . \ucfirst(\gettype($raw_binary)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
return self::decryptInternal(
|
||||
$ciphertext,
|
||||
KeyOrPassword::createFromKey($key),
|
||||
@@ -78,11 +131,32 @@ class Crypto
|
||||
*
|
||||
* @throws Ex\EnvironmentIsBrokenException
|
||||
* @throws Ex\WrongKeyOrModifiedCiphertextException
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function decryptWithPassword($ciphertext, $password, $raw_binary = false)
|
||||
public static function decryptWithPassword(
|
||||
$ciphertext,
|
||||
#[\SensitiveParameter]
|
||||
$password,
|
||||
$raw_binary = false
|
||||
)
|
||||
{
|
||||
if (!\is_string($ciphertext)) {
|
||||
throw new \TypeError(
|
||||
'String expected for argument 1. ' . \ucfirst(\gettype($ciphertext)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
if (!\is_string($password)) {
|
||||
throw new \TypeError(
|
||||
'String expected for argument 2. ' . \ucfirst(\gettype($password)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
if (!\is_bool($raw_binary)) {
|
||||
throw new \TypeError(
|
||||
'Boolean expected for argument 3. ' . \ucfirst(\gettype($raw_binary)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
return self::decryptInternal(
|
||||
$ciphertext,
|
||||
KeyOrPassword::createFromPassword($password),
|
||||
@@ -98,11 +172,27 @@ class Crypto
|
||||
*
|
||||
* @throws Ex\EnvironmentIsBrokenException
|
||||
* @throws Ex\WrongKeyOrModifiedCiphertextException
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function legacyDecrypt($ciphertext, $key)
|
||||
public static function legacyDecrypt(
|
||||
$ciphertext,
|
||||
#[\SensitiveParameter]
|
||||
$key
|
||||
)
|
||||
{
|
||||
if (!\is_string($ciphertext)) {
|
||||
throw new \TypeError(
|
||||
'String expected for argument 1. ' . \ucfirst(\gettype($ciphertext)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
if (!\is_string($key)) {
|
||||
throw new \TypeError(
|
||||
'String expected for argument 2. ' . \ucfirst(\gettype($key)) . ' given instead.'
|
||||
);
|
||||
}
|
||||
|
||||
RuntimeTests::runtimeTest();
|
||||
|
||||
// Extract the HMAC from the front of the ciphertext.
|
||||
@@ -111,14 +201,16 @@ class Crypto
|
||||
'Ciphertext is too short.'
|
||||
);
|
||||
}
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
$hmac = Core::ourSubstr($ciphertext, 0, Core::LEGACY_MAC_BYTE_SIZE);
|
||||
if ($hmac === false) {
|
||||
throw new Ex\EnvironmentIsBrokenException();
|
||||
}
|
||||
$ciphertext = Core::ourSubstr($ciphertext, Core::LEGACY_MAC_BYTE_SIZE);
|
||||
if ($ciphertext === false) {
|
||||
throw new Ex\EnvironmentIsBrokenException();
|
||||
}
|
||||
Core::ensureTrue(\is_string($hmac));
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
$messageCiphertext = Core::ourSubstr($ciphertext, Core::LEGACY_MAC_BYTE_SIZE);
|
||||
Core::ensureTrue(\is_string($messageCiphertext));
|
||||
|
||||
// Regenerate the same authentication sub-key.
|
||||
$akey = Core::HKDF(
|
||||
@@ -129,7 +221,7 @@ class Crypto
|
||||
null
|
||||
);
|
||||
|
||||
if (self::verifyHMAC($hmac, $ciphertext, $akey)) {
|
||||
if (self::verifyHMAC($hmac, $messageCiphertext, $akey)) {
|
||||
// Regenerate the same encryption sub-key.
|
||||
$ekey = Core::HKDF(
|
||||
Core::LEGACY_HASH_FUNCTION_NAME,
|
||||
@@ -140,22 +232,25 @@ class Crypto
|
||||
);
|
||||
|
||||
// Extract the IV from the ciphertext.
|
||||
if (Core::ourStrlen($ciphertext) <= Core::LEGACY_BLOCK_BYTE_SIZE) {
|
||||
if (Core::ourStrlen($messageCiphertext) <= Core::LEGACY_BLOCK_BYTE_SIZE) {
|
||||
throw new Ex\WrongKeyOrModifiedCiphertextException(
|
||||
'Ciphertext is too short.'
|
||||
);
|
||||
}
|
||||
$iv = Core::ourSubstr($ciphertext, 0, Core::LEGACY_BLOCK_BYTE_SIZE);
|
||||
if ($iv === false) {
|
||||
throw new Ex\EnvironmentIsBrokenException();
|
||||
}
|
||||
$ciphertext = Core::ourSubstr($ciphertext, Core::LEGACY_BLOCK_BYTE_SIZE);
|
||||
if ($ciphertext === false) {
|
||||
throw new Ex\EnvironmentIsBrokenException();
|
||||
}
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
$iv = Core::ourSubstr($messageCiphertext, 0, Core::LEGACY_BLOCK_BYTE_SIZE);
|
||||
Core::ensureTrue(\is_string($iv));
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
$actualCiphertext = Core::ourSubstr($messageCiphertext, Core::LEGACY_BLOCK_BYTE_SIZE);
|
||||
Core::ensureTrue(\is_string($actualCiphertext));
|
||||
|
||||
// Do the decryption.
|
||||
$plaintext = self::plainDecrypt($ciphertext, $ekey, $iv, Core::LEGACY_CIPHER_METHOD);
|
||||
$plaintext = self::plainDecrypt($actualCiphertext, $ekey, $iv, Core::LEGACY_CIPHER_METHOD);
|
||||
return $plaintext;
|
||||
} else {
|
||||
throw new Ex\WrongKeyOrModifiedCiphertextException(
|
||||
@@ -226,6 +321,7 @@ class Crypto
|
||||
}
|
||||
|
||||
// Get and check the version header.
|
||||
/** @var string $header */
|
||||
$header = Core::ourSubstr($ciphertext, 0, Core::HEADER_VERSION_SIZE);
|
||||
if ($header !== Core::CURRENT_VERSION) {
|
||||
throw new Ex\WrongKeyOrModifiedCiphertextException(
|
||||
@@ -234,36 +330,34 @@ class Crypto
|
||||
}
|
||||
|
||||
// Get the salt.
|
||||
/** @var string $salt */
|
||||
$salt = Core::ourSubstr(
|
||||
$ciphertext,
|
||||
Core::HEADER_VERSION_SIZE,
|
||||
Core::SALT_BYTE_SIZE
|
||||
);
|
||||
if ($salt === false) {
|
||||
throw new Ex\EnvironmentIsBrokenException();
|
||||
}
|
||||
Core::ensureTrue(\is_string($salt));
|
||||
|
||||
// Get the IV.
|
||||
/** @var string $iv */
|
||||
$iv = Core::ourSubstr(
|
||||
$ciphertext,
|
||||
Core::HEADER_VERSION_SIZE + Core::SALT_BYTE_SIZE,
|
||||
Core::BLOCK_BYTE_SIZE
|
||||
);
|
||||
if ($iv === false) {
|
||||
throw new Ex\EnvironmentIsBrokenException();
|
||||
}
|
||||
Core::ensureTrue(\is_string($iv));
|
||||
|
||||
// Get the HMAC.
|
||||
/** @var string $hmac */
|
||||
$hmac = Core::ourSubstr(
|
||||
$ciphertext,
|
||||
Core::ourStrlen($ciphertext) - Core::MAC_BYTE_SIZE,
|
||||
Core::MAC_BYTE_SIZE
|
||||
);
|
||||
if ($hmac === false) {
|
||||
throw new Ex\EnvironmentIsBrokenException();
|
||||
}
|
||||
Core::ensureTrue(\is_string($hmac));
|
||||
|
||||
// Get the actual encrypted ciphertext.
|
||||
/** @var string $encrypted */
|
||||
$encrypted = Core::ourSubstr(
|
||||
$ciphertext,
|
||||
Core::HEADER_VERSION_SIZE + Core::SALT_BYTE_SIZE +
|
||||
@@ -271,9 +365,7 @@ class Crypto
|
||||
Core::ourStrlen($ciphertext) - Core::MAC_BYTE_SIZE - Core::SALT_BYTE_SIZE -
|
||||
Core::BLOCK_BYTE_SIZE - Core::HEADER_VERSION_SIZE
|
||||
);
|
||||
if ($encrypted === false) {
|
||||
throw new Ex\EnvironmentIsBrokenException();
|
||||
}
|
||||
Core::ensureTrue(\is_string($encrypted));
|
||||
|
||||
// Derive the separate encryption and authentication keys from the key
|
||||
// or password, whichever it is.
|
||||
@@ -300,10 +392,17 @@ class Crypto
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function plainEncrypt($plaintext, $key, $iv)
|
||||
protected static function plainEncrypt(
|
||||
$plaintext,
|
||||
#[\SensitiveParameter]
|
||||
$key,
|
||||
#[\SensitiveParameter]
|
||||
$iv
|
||||
)
|
||||
{
|
||||
Core::ensureConstantExists('OPENSSL_RAW_DATA');
|
||||
Core::ensureFunctionExists('openssl_encrypt');
|
||||
/** @var string $ciphertext */
|
||||
$ciphertext = \openssl_encrypt(
|
||||
$plaintext,
|
||||
Core::CIPHER_METHOD,
|
||||
@@ -312,11 +411,7 @@ class Crypto
|
||||
$iv
|
||||
);
|
||||
|
||||
if ($ciphertext === false) {
|
||||
throw new Ex\EnvironmentIsBrokenException(
|
||||
'openssl_encrypt() failed.'
|
||||
);
|
||||
}
|
||||
Core::ensureTrue(\is_string($ciphertext), 'openssl_encrypt() failed');
|
||||
|
||||
return $ciphertext;
|
||||
}
|
||||
@@ -333,10 +428,19 @@ class Crypto
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function plainDecrypt($ciphertext, $key, $iv, $cipherMethod)
|
||||
protected static function plainDecrypt(
|
||||
$ciphertext,
|
||||
#[\SensitiveParameter]
|
||||
$key,
|
||||
#[\SensitiveParameter]
|
||||
$iv,
|
||||
$cipherMethod
|
||||
)
|
||||
{
|
||||
Core::ensureConstantExists('OPENSSL_RAW_DATA');
|
||||
Core::ensureFunctionExists('openssl_decrypt');
|
||||
|
||||
/** @var string $plaintext */
|
||||
$plaintext = \openssl_decrypt(
|
||||
$ciphertext,
|
||||
$cipherMethod,
|
||||
@@ -344,11 +448,7 @@ class Crypto
|
||||
OPENSSL_RAW_DATA,
|
||||
$iv
|
||||
);
|
||||
if ($plaintext === false) {
|
||||
throw new Ex\EnvironmentIsBrokenException(
|
||||
'openssl_decrypt() failed.'
|
||||
);
|
||||
}
|
||||
Core::ensureTrue(\is_string($plaintext), 'openssl_decrypt() failed.');
|
||||
|
||||
return $plaintext;
|
||||
}
|
||||
@@ -356,7 +456,7 @@ class Crypto
|
||||
/**
|
||||
* Verifies an HMAC without leaking information through side-channels.
|
||||
*
|
||||
* @param string $correct_hmac
|
||||
* @param string $expected_hmac
|
||||
* @param string $message
|
||||
* @param string $key
|
||||
*
|
||||
@@ -364,9 +464,14 @@ class Crypto
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected static function verifyHMAC($correct_hmac, $message, $key)
|
||||
protected static function verifyHMAC(
|
||||
$expected_hmac,
|
||||
$message,
|
||||
#[\SensitiveParameter]
|
||||
$key
|
||||
)
|
||||
{
|
||||
$message_hmac = \hash_hmac(Core::HASH_FUNCTION_NAME, $message, $key, true);
|
||||
return Core::hashEquals($correct_hmac, $message_hmac);
|
||||
return Core::hashEquals($message_hmac, $expected_hmac);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user