2525 return ;
2626}
2727
28+ /**
29+ * @api
30+ */
2831class ParagonIE_Sodium_Compat
2932{
3033 /**
@@ -204,9 +207,6 @@ public static function base642bin(
204207
205208 /** @var string $encoded */
206209 $ encoded = (string ) $ encoded ;
207- if (ParagonIE_Sodium_Core_Util::strlen ($ encoded ) === 0 ) {
208- return '' ;
209- }
210210
211211 // Just strip before decoding
212212 if (!empty ($ ignore )) {
@@ -218,19 +218,19 @@ public static function base642bin(
218218 case self ::BASE64_VARIANT_ORIGINAL :
219219 return ParagonIE_Sodium_Core_Base64_Original::decode ($ encoded , true );
220220 case self ::BASE64_VARIANT_ORIGINAL_NO_PADDING :
221- return ParagonIE_Sodium_Core_Base64_Original::decode ($ encoded, false );
221+ return ParagonIE_Sodium_Core_Base64_Original::decodeNoPadding ($ encoded );
222222 case self ::BASE64_VARIANT_URLSAFE :
223223 return ParagonIE_Sodium_Core_Base64_UrlSafe::decode ($ encoded , true );
224224 case self ::BASE64_VARIANT_URLSAFE_NO_PADDING :
225- return ParagonIE_Sodium_Core_Base64_UrlSafe::decode ($ encoded, false );
225+ return ParagonIE_Sodium_Core_Base64_UrlSafe::decodeNoPadding ($ encoded );
226226 default :
227227 throw new SodiumException ('invalid base64 variant identifier ' );
228228 }
229229 } catch (Exception $ ex ) {
230230 if ($ ex instanceof SodiumException) {
231231 throw $ ex ;
232232 }
233- throw new SodiumException ('invalid base64 string ' );
233+ throw new SodiumException ('invalid base64 string ' , 0 , $ ex );
234234 }
235235 }
236236
@@ -356,7 +356,7 @@ public static function crypto_aead_aegis128l_decrypt(
356356
357357 /* Input validation: */
358358 if (ParagonIE_Sodium_Core_Util::strlen ($ nonce ) !== self ::CRYPTO_AEAD_AEGIS128L_NPUBBYTES ) {
359- throw new SodiumException ('Nonce must be CRYPTO_AEAD_AEGIS_128L_NPUBBYTES long ' );
359+ throw new SodiumException ('Nonce must be CRYPTO_AEAD_AEGIS128L_NPUBBYTES long ' );
360360 }
361361 if (ParagonIE_Sodium_Core_Util::strlen ($ key ) !== self ::CRYPTO_AEAD_AEGIS128L_KEYBYTES ) {
362362 throw new SodiumException ('Key must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long ' );
@@ -410,7 +410,7 @@ public static function crypto_aead_aegis128l_encrypt(
410410
411411 /* Input validation: */
412412 if (ParagonIE_Sodium_Core_Util::strlen ($ nonce ) !== self ::CRYPTO_AEAD_AEGIS128L_NPUBBYTES ) {
413- throw new SodiumException ('Nonce must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long ' );
413+ throw new SodiumException ('Nonce must be CRYPTO_AEAD_AEGIS128L_NPUBBYTES long ' );
414414 }
415415 if (ParagonIE_Sodium_Core_Util::strlen ($ key ) !== self ::CRYPTO_AEAD_AEGIS128L_KEYBYTES ) {
416416 throw new SodiumException ('Key must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long ' );
@@ -519,10 +519,10 @@ public static function crypto_aead_aegis256_encrypt(
519519
520520 /* Input validation: */
521521 if (ParagonIE_Sodium_Core_Util::strlen ($ nonce ) !== self ::CRYPTO_AEAD_AEGIS256_NPUBBYTES ) {
522- throw new SodiumException ('Nonce must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long ' );
522+ throw new SodiumException ('Nonce must be CRYPTO_AEAD_AEGIS256_NPUBBYTES long ' );
523523 }
524524 if (ParagonIE_Sodium_Core_Util::strlen ($ key ) !== self ::CRYPTO_AEAD_AEGIS256_KEYBYTES ) {
525- throw new SodiumException ('Key must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long ' );
525+ throw new SodiumException ('Key must be CRYPTO_AEAD_AEGIS256_KEYBYTES long ' );
526526 }
527527
528528 list ($ ct , $ tag ) = ParagonIE_Sodium_Core_AEGIS256::encrypt ($ plaintext , $ assocData , $ key , $ nonce );
@@ -562,6 +562,9 @@ public static function crypto_aead_aes256gcm_is_available()
562562 // OpenSSL doesn't support AEAD before 7.1.0
563563 return false ;
564564 }
565+ if (!extension_loaded ('openssl ' )) {
566+ return false ;
567+ }
565568 if (!is_callable ('openssl_encrypt ' ) || !is_callable ('openssl_decrypt ' )) {
566569 // OpenSSL isn't installed
567570 return false ;
@@ -615,6 +618,9 @@ public static function crypto_aead_aes256gcm_decrypt(
615618 if (ParagonIE_Sodium_Core_Util::strlen ($ ciphertext ) < self ::CRYPTO_AEAD_AES256GCM_ABYTES ) {
616619 throw new SodiumException ('Message must be at least CRYPTO_AEAD_AES256GCM_ABYTES long ' );
617620 }
621+ if (!extension_loaded ('openssl ' )) {
622+ throw new SodiumException ('The OpenSSL extension is not installed ' );
623+ }
618624 if (!is_callable ('openssl_decrypt ' )) {
619625 throw new SodiumException ('The OpenSSL extension is not installed, or openssl_decrypt() is not available ' );
620626 }
@@ -675,6 +681,9 @@ public static function crypto_aead_aes256gcm_encrypt(
675681 throw new SodiumException ('Key must be CRYPTO_AEAD_AES256GCM_KEYBYTES long ' );
676682 }
677683
684+ if (!extension_loaded ('openssl ' )) {
685+ throw new SodiumException ('The OpenSSL extension is not installed ' );
686+ }
678687 if (!is_callable ('openssl_encrypt ' )) {
679688 throw new SodiumException ('The OpenSSL extension is not installed, or openssl_encrypt() is not available ' );
680689 }
@@ -823,10 +832,10 @@ public static function crypto_aead_chacha20poly1305_encrypt(
823832
824833 /* Input validation: */
825834 if (ParagonIE_Sodium_Core_Util::strlen ($ nonce ) !== self ::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES ) {
826- throw new SodiumException ('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long ' );
835+ throw new SodiumException ('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long ' );
827836 }
828837 if (ParagonIE_Sodium_Core_Util::strlen ($ key ) !== self ::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES ) {
829- throw new SodiumException ('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long ' );
838+ throw new SodiumException ('Key must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES long ' );
830839 }
831840
832841 if (self ::useNewSodiumAPI ()) {
@@ -901,10 +910,10 @@ public static function crypto_aead_chacha20poly1305_ietf_decrypt(
901910 throw new SodiumException ('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long ' );
902911 }
903912 if (ParagonIE_Sodium_Core_Util::strlen ($ key ) !== self ::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES ) {
904- throw new SodiumException ('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long ' );
913+ throw new SodiumException ('Key must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES long ' );
905914 }
906915 if (ParagonIE_Sodium_Core_Util::strlen ($ ciphertext ) < self ::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES ) {
907- throw new SodiumException ('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long ' );
916+ throw new SodiumException ('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES long ' );
908917 }
909918
910919 if (self ::useNewSodiumAPI ()) {
@@ -2781,6 +2790,9 @@ public static function crypto_secretbox_xchacha20poly1305_open(
27812790 if (ParagonIE_Sodium_Core_Util::strlen ($ key ) !== self ::CRYPTO_SECRETBOX_KEYBYTES ) {
27822791 throw new SodiumException ('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long. ' );
27832792 }
2793+ if (ParagonIE_Sodium_Core_Util::strlen ($ ciphertext ) < self ::CRYPTO_SECRETBOX_MACBYTES ) {
2794+ throw new SodiumException ("Ciphertext must be at least CRYPTO_SECRETBOX_MACBYTES long " );
2795+ }
27842796
27852797 if (PHP_INT_SIZE === 4 ) {
27862798 return ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305_open ($ ciphertext , $ nonce , $ key );
@@ -3711,6 +3723,9 @@ public static function increment(
37113723 }
37123724
37133725 $ len = ParagonIE_Sodium_Core_Util::strlen ($ var );
3726+ if ($ len < 1 ) {
3727+ throw new SodiumException ('Argument 1 cannot be empty ' );
3728+ }
37143729 $ c = 1 ;
37153730 $ copy = '' ;
37163731 for ($ i = 0 ; $ i < $ len ; ++$ i ) {
0 commit comments