wp/wp-includes/sodium_compat/src/Compat.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
  3153         }
  3153         }
  3154         return ParagonIE_Sodium_Core_XChaCha20::streamXorIc($message, $nonce, $key);
  3154         return ParagonIE_Sodium_Core_XChaCha20::streamXorIc($message, $nonce, $key);
  3155     }
  3155     }
  3156 
  3156 
  3157     /**
  3157     /**
       
  3158      * DANGER! UNAUTHENTICATED ENCRYPTION!
       
  3159      *
       
  3160      * Unless you are following expert advice, do not use this feature.
       
  3161      *
       
  3162      * Algorithm: XChaCha20
       
  3163      *
       
  3164      * This DOES NOT provide ciphertext integrity.
       
  3165      *
       
  3166      * @param string $message Plaintext message
       
  3167      * @param string $nonce Number to be used Once; must be 24 bytes
       
  3168      * @param int $counter
       
  3169      * @param string $key Encryption key
       
  3170      * @return string         Encrypted text which is vulnerable to chosen-
       
  3171      *                        ciphertext attacks unless you implement some
       
  3172      *                        other mitigation to the ciphertext (i.e.
       
  3173      *                        Encrypt then MAC)
       
  3174      * @param bool $dontFallback
       
  3175      * @throws SodiumException
       
  3176      * @throws TypeError
       
  3177      * @psalm-suppress MixedArgument
       
  3178      */
       
  3179     public static function crypto_stream_xchacha20_xor_ic($message, $nonce, $counter, $key, $dontFallback = false)
       
  3180     {
       
  3181         /* Type checks: */
       
  3182         ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
       
  3183         ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
       
  3184         ParagonIE_Sodium_Core_Util::declareScalarType($counter, 'int', 3);
       
  3185         ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
       
  3186 
       
  3187         /* Input validation: */
       
  3188         if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_XCHACHA20_NONCEBYTES) {
       
  3189             throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_XCHACHA20_NONCEBYTES long.');
       
  3190         }
       
  3191         if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_XCHACHA20_KEYBYTES) {
       
  3192             throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_XCHACHA20_KEYBYTES long.');
       
  3193         }
       
  3194 
       
  3195         if (is_callable('sodium_crypto_stream_xchacha20_xor_ic') && !$dontFallback) {
       
  3196             return sodium_crypto_stream_xchacha20_xor_ic($message, $nonce, $counter, $key);
       
  3197         }
       
  3198 
       
  3199         $ic = ParagonIE_Sodium_Core_Util::store64_le($counter);
       
  3200         if (PHP_INT_SIZE === 4) {
       
  3201             return ParagonIE_Sodium_Core32_XChaCha20::streamXorIc($message, $nonce, $key, $ic);
       
  3202         }
       
  3203         return ParagonIE_Sodium_Core_XChaCha20::streamXorIc($message, $nonce, $key, $ic);
       
  3204     }
       
  3205 
       
  3206     /**
  3158      * Return a secure random key for use with crypto_stream_xchacha20
  3207      * Return a secure random key for use with crypto_stream_xchacha20
  3159      *
  3208      *
  3160      * @return string
  3209      * @return string
  3161      * @throws Exception
  3210      * @throws Exception
  3162      * @throws Error
  3211      * @throws Error
  3168 
  3217 
  3169     /**
  3218     /**
  3170      * Cache-timing-safe implementation of hex2bin().
  3219      * Cache-timing-safe implementation of hex2bin().
  3171      *
  3220      *
  3172      * @param string $string Hexadecimal string
  3221      * @param string $string Hexadecimal string
       
  3222      * @param string $ignore List of characters to ignore; useful for whitespace
  3173      * @return string        Raw binary string
  3223      * @return string        Raw binary string
  3174      * @throws SodiumException
  3224      * @throws SodiumException
  3175      * @throws TypeError
  3225      * @throws TypeError
  3176      * @psalm-suppress TooFewArguments
  3226      * @psalm-suppress TooFewArguments
  3177      * @psalm-suppress MixedArgument
  3227      * @psalm-suppress MixedArgument
  3178      */
  3228      */
  3179     public static function hex2bin($string)
  3229     public static function hex2bin($string, $ignore = '')
  3180     {
  3230     {
  3181         /* Type checks: */
  3231         /* Type checks: */
  3182         ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1);
  3232         ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1);
       
  3233         ParagonIE_Sodium_Core_Util::declareScalarType($ignore, 'string', 2);
  3183 
  3234 
  3184         if (self::useNewSodiumAPI()) {
  3235         if (self::useNewSodiumAPI()) {
  3185             if (is_callable('sodium_hex2bin')) {
  3236             if (is_callable('sodium_hex2bin')) {
  3186                 return (string) sodium_hex2bin($string);
  3237                 return (string) sodium_hex2bin($string, $ignore);
  3187             }
  3238             }
  3188         }
  3239         }
  3189         if (self::use_fallback('hex2bin')) {
  3240         if (self::use_fallback('hex2bin')) {
  3190             return (string) call_user_func('\\Sodium\\hex2bin', $string);
  3241             return (string) call_user_func('\\Sodium\\hex2bin', $string, $ignore);
  3191         }
  3242         }
  3192         return ParagonIE_Sodium_Core_Util::hex2bin($string);
  3243         return ParagonIE_Sodium_Core_Util::hex2bin($string, $ignore);
  3193     }
  3244     }
  3194 
  3245 
  3195     /**
  3246     /**
  3196      * Increase a string (little endian)
  3247      * Increase a string (little endian)
  3197      *
  3248      *