Skip to content

Commit

Permalink
API compatibility: Return false with compat APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Sep 28, 2017
1 parent 09a60d1 commit c2f615e
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 18 deletions.
78 changes: 70 additions & 8 deletions lib/php72compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ function sodium_compare($a, $b)
*/
function sodium_crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key)
{
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key);
try {
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key);
} catch (Error $ex) {
return false;
} catch (Exception $ex) {
return false;
}
}
}
if (!is_callable('sodium_crypto_aead_chacha20poly1305_encrypt')) {
Expand All @@ -120,7 +126,13 @@ function sodium_crypto_aead_chacha20poly1305_encrypt($message, $assocData, $nonc
*/
function sodium_crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key)
{
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key);
try {
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key);
} catch (Error $ex) {
return false;
} catch (Exception $ex) {
return false;
}
}
}
if (!is_callable('sodium_crypto_aead_chacha20poly1305_ietf_encrypt')) {
Expand All @@ -136,6 +148,38 @@ function sodium_crypto_aead_chacha20poly1305_ietf_encrypt($message, $assocData,
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key);
}
}
if (!is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_decrypt')) {
/**
* @param string $message
* @param string $assocData
* @param string $nonce
* @param string $key
* @return string
*/
function sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key)
{
try {
return ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key);
} catch (Error $ex) {
return false;
} catch (Exception $ex) {
return false;
}
}
}
if (!is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) {
/**
* @param string $message
* @param string $assocData
* @param string $nonce
* @param string $key
* @return string
*/
function sodium_crypto_aead_xchacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key)
{
return ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key);
}
}
if (!is_callable('sodium_crypto_auth')) {
/**
* @param string $message
Expand Down Expand Up @@ -196,11 +240,17 @@ function sodium_crypto_box_keypair_from_secretkey_and_publickey($sk, $pk)
* @param string $message
* @param string $nonce
* @param string $kp
* @return string
* @return string|bool
*/
function sodium_crypto_box_open($message, $nonce, $kp)
{
return ParagonIE_Sodium_Compat::crypto_box_open($message, $nonce, $kp);
try {
return ParagonIE_Sodium_Compat::crypto_box_open($message, $nonce, $kp);
} catch (Error $ex) {
return false;
} catch (Exception $ex) {
return false;
}
}
}
if (!is_callable('sodium_crypto_box_publickey')) {
Expand Down Expand Up @@ -446,11 +496,17 @@ function sodium_crypto_secretbox($message, $nonce, $key)
* @param string $message
* @param string $nonce
* @param string $key
* @return string
* @return string|bool
*/
function sodium_crypto_secretbox_open($message, $nonce, $key)
{
return ParagonIE_Sodium_Compat::crypto_secretbox_open($message, $nonce, $key);
try {
return ParagonIE_Sodium_Compat::crypto_secretbox_open($message, $nonce, $key);
} catch (Error $ex) {
return false;
} catch (Exception $ex) {
return false;
}
}
}
if (!is_callable('sodium_crypto_shorthash')) {
Expand Down Expand Up @@ -499,11 +555,17 @@ function sodium_crypto_sign_keypair()
/**
* @param string $signedMessage
* @param string $pk
* @return string
* @return string|bool
*/
function sodium_crypto_sign_open($signedMessage, $pk)
{
return ParagonIE_Sodium_Compat::crypto_sign_open($signedMessage, $pk);
try {
return ParagonIE_Sodium_Compat::crypto_sign_open($signedMessage, $pk);
} catch (Error $ex) {
return false;
} catch (Exception $ex) {
return false;
}
}
}
if (!is_callable('sodium_crypto_sign_publickey')) {
Expand Down
50 changes: 40 additions & 10 deletions lib/sodium_compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ function compare($a, $b)
* @param string $assocData
* @param string $nonce
* @param string $key
* @return string
* @return string|bool
*/
function crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key)
{
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key);
try {
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key);
} catch (Error $ex) {
return false;
} catch (Exception $ex) {
return false;
}
}
}
if (!is_callable('\\Sodium\\crypto_aead_chacha20poly1305_encrypt')) {
Expand All @@ -63,11 +69,17 @@ function crypto_aead_chacha20poly1305_encrypt($message, $assocData, $nonce, $key
* @param string $assocData
* @param string $nonce
* @param string $key
* @return string
* @return string|bool
*/
function crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key)
{
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key);
try {
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key);
} catch (Error $ex) {
return false;
} catch (Exception $ex) {
return false;
}
}
}
if (!is_callable('\\Sodium\\crypto_aead_chacha20poly1305_ietf_encrypt')) {
Expand Down Expand Up @@ -143,11 +155,17 @@ function crypto_box_keypair_from_secretkey_and_publickey($sk, $pk)
* @param string $message
* @param string $nonce
* @param string $kp
* @return string
* @return string|bool
*/
function crypto_box_open($message, $nonce, $kp)
{
return ParagonIE_Sodium_Compat::crypto_box_open($message, $nonce, $kp);
try {
return ParagonIE_Sodium_Compat::crypto_box_open($message, $nonce, $kp);
} catch (Error $ex) {
return false;
} catch (Exception $ex) {
return false;
}
}
}
if (!is_callable('\\Sodium\\crypto_box_publickey')) {
Expand Down Expand Up @@ -383,11 +401,17 @@ function crypto_secretbox($message, $nonce, $key)
* @param string $message
* @param string $nonce
* @param string $key
* @return string
* @return string|bool
*/
function crypto_secretbox_open($message, $nonce, $key)
{
return ParagonIE_Sodium_Compat::crypto_secretbox_open($message, $nonce, $key);
try {
return ParagonIE_Sodium_Compat::crypto_secretbox_open($message, $nonce, $key);
} catch (Error $ex) {
return false;
} catch (Exception $ex) {
return false;
}
}
}
if (!is_callable('\\Sodium\\crypto_shorthash')) {
Expand Down Expand Up @@ -436,11 +460,17 @@ function crypto_sign_keypair()
/**
* @param string $signedMessage
* @param string $pk
* @return string
* @return string|bool
*/
function crypto_sign_open($signedMessage, $pk)
{
return ParagonIE_Sodium_Compat::crypto_sign_open($signedMessage, $pk);
try {
return ParagonIE_Sodium_Compat::crypto_sign_open($signedMessage, $pk);
} catch (\Error $ex) {
return false;
} catch (\Exception $ex) {
return false;
}
}
}
if (!is_callable('\\Sodium\\crypto_sign_publickey')) {
Expand Down

0 comments on commit c2f615e

Please sign in to comment.