public function testSignFail() { $alice = Asymmetric::generateKeys(Key::CRYPTO_SIGN); $message = 'test message'; $signature = Asymmetric::sign($message, $alice->getSecretKey(), true); $this->assertFalse(Asymmetric::verify('wrongmessage', $alice->getPublicKey(), $signature, true)); $_signature = $signature; // Let's flip one bit, randomly: $r = \Sodium\randombytes_uniform(\mb_strlen($signature, '8bit')); $_signature[$r] = \chr(\ord($_signature[$r]) ^ 1 << \Sodium\randombytes_uniform(8)); $this->assertFalse(Asymmetric::verify($message, $alice->getPublicKey(), $_signature, true)); }
public function testEncryptFail() { $key = new \ParagonIE\Halite\Key(\str_repeat('A', 32)); $message = Symmetric::encrypt('test message', $key, true); $r = \Sodium\randombytes_uniform(\mb_strlen($message, '8bit')); $message[$r] = \chr(\ord($message[$r]) ^ 1 << \Sodium\randombytes_uniform(8)); try { $plain = Symmetric::decrypt($message, $key, true); $this->assertEquals($plain, $message); throw new Exception('ERROR: THIS SHOULD ALWAYS FAIL'); } catch (CryptoException\InvalidMessage $e) { $this->assertTrue($e instanceof CryptoException\InvalidMessage); } }
public function testEncryptFail() { $key = new EncryptionKey(\str_repeat('A', 32)); $message = Symmetric::encrypt('test message', $key, true); $r = \Sodium\randombytes_uniform(\mb_strlen($message, '8bit')); $message[$r] = \chr(\ord($message[$r]) ^ 1 << \Sodium\randombytes_uniform(8)); try { $plain = Symmetric::decrypt($message, $key, true); $this->assertEquals($plain, $message); $this->fail('This should have thrown an InvalidMessage exception!'); } catch (CryptoException\InvalidMessage $e) { $this->assertTrue($e instanceof CryptoException\InvalidMessage); } }
/** * @covers Symmetric::unpackMessageForDecryption() */ public function testUnpack() { $key = new EncryptionKey(new HiddenString(\str_repeat('A', 32))); // Randomly sized plaintext $size = \Sodium\randombytes_uniform(1023) + 1; $plaintext = \Sodium\randombytes_buf($size); $message = Symmetric::encrypt(new HiddenString($plaintext), $key, true); // Let's unpack our message $unpacked = Symmetric::unpackMessageForDecryption($message); // Now to test our expected results! $this->assertSame(Util::safeStrlen($unpacked[0]), Halite::VERSION_TAG_LEN); $this->assertTrue($unpacked[1] instanceof \ParagonIE\Halite\Symmetric\Config); $config = $unpacked[1]; if ($config instanceof \ParagonIE\Halite\Symmetric\Config) { $this->assertSame(Util::safeStrlen($unpacked[2]), $config->HKDF_SALT_LEN); $this->assertSame(Util::safeStrlen($unpacked[3]), \Sodium\CRYPTO_STREAM_NONCEBYTES); $this->assertSame(Util::safeStrlen($unpacked[4]), Util::safeStrlen($message) - (Halite::VERSION_TAG_LEN + $config->HKDF_SALT_LEN + \Sodium\CRYPTO_STREAM_NONCEBYTES + $config->MAC_SIZE)); $this->assertSame(Util::safeStrlen($unpacked[5]), $config->MAC_SIZE); } else { $this->fail('Cannot continue'); } }
/** * Returns a random integer to the client. * * @param int $range Upper limit of random numbers to return to the client. * @return int * @throws Exceptions\InvalidTypeException * @throws Exceptions\OutOfRangeException */ static function integer($range = Constants::RANGE) { # Test the length for validity. Helpers::rangeCheck($range, Constants::RANGE_MAX, Constants::RANGE_MIN, 'Entropy', 'integer'); return \Sodium\randombytes_uniform($range) + 1; }