/** * This method is called before the first test of this test class is run. * * @return void */ public static function setUpBeforeClass() { // Only run the test if the environment supports it. try { Crypto::RuntimeTest(); } catch (CryptoTestFailedException $e) { self::markTestSkipped('The environment cannot safely perform encryption with this cipher.'); } }
public static function Decrypt($ciphertext, $key) { Crypto::RuntimeTest(); // Extract the HMAC from the front of the ciphertext. if (self::our_strlen($ciphertext) <= self::MAC_BYTE_SIZE) { throw new InvalidCiphertextException(); } $hmac = self::our_substr($ciphertext, 0, self::MAC_BYTE_SIZE); if ($hmac === FALSE) { throw new CannotPerformOperationException(); } $ciphertext = self::our_substr($ciphertext, self::MAC_BYTE_SIZE); if ($ciphertext === FALSE) { throw new CannotPerformOperationException(); } // Regenerate the same authentication sub-key. $akey = self::HKDF(self::HASH_FUNCTION, $key, self::KEY_BYTE_SIZE, self::AUTHENTICATION_INFO); if (self::VerifyHMAC($hmac, $ciphertext, $akey)) { // Regenerate the same encryption sub-key. $keysize = self::KEY_BYTE_SIZE; $ekey = self::HKDF(self::HASH_FUNCTION, $key, $keysize, self::ENCRYPTION_INFO); // Extract the initialization vector from the ciphertext. self::EnsureFunctionExists("mcrypt_get_iv_size"); $ivsize = mcrypt_get_iv_size(self::CIPHER, self::CIPHER_MODE); if ($ivsize === FALSE || $ivsize <= 0) { throw new CannotPerformOperationException(); } if (self::our_strlen($ciphertext) <= $ivsize) { throw new InvalidCiphertextException(); } $iv = self::our_substr($ciphertext, 0, $ivsize); if ($iv === FALSE) { throw new CannotPerformOperationException(); } $ciphertext = self::our_substr($ciphertext, $ivsize); if ($ciphertext === FALSE) { throw new CannotPerformOperationException(); } $plaintext = self::PlainDecrypt($ciphertext, $ekey, $iv); return $plaintext; } else { /* * We throw an exception instead of returning FALSE because we want * a script that doesn't handle this condition to CRASH, instead * of thinking the ciphertext decrypted to the value FALSE. */ throw new InvalidCiphertextException(); } }
<?php // Set the encoding to something more "challenging." $ret = mb_internal_encoding('UTF-8'); if ($ret === FALSE) { echo "Couldn't set encoding."; exit(1); } // Dump out the settings / encoding for future reference. $val = ini_get("mbstring.func_overload"); echo "Settings: \n"; echo " func_overload: " . $val . "\n"; echo " mb_internal_encoding(): " . mb_internal_encoding() . "\n"; // Perform the tests. require_once 'Crypto.php'; try { Crypto::RuntimeTest(); echo "TEST PASSED!\n"; exit(0); } catch (CryptoTestFailedException $ex) { echo "TEST FAILED!\n"; var_dump($ex); exit(1); } catch (CannotPerformOperationException $ex) { echo "TEST FAILED\n"; var_dump($ex); exit(1); }