*/ include_once "@PHP_MODULE_FILE_NAME@"; /** * @coversDefaultClass VirgilSymmetricCipher */ class VirgilSymmetricCipher_Test extends PHPUnit\Framework\TestCase { /** * @covers VirgilSymmetricCipher::aes256 * @covers VirgilSymmetricCipher::blockSize * @covers VirgilSymmetricCipher::ivSize */ public function test_aes256_object_creation() { $cipher = new VirgilSymmetricCipher(VirgilSymmetricCipher::Algorithm_AES_256_GCM); $this->assertInstanceOf("VirgilSymmetricCipher", $cipher); $this->assertContains("AES-256", $cipher->name()); $this->assertEquals(256, $cipher->keySize()); $this->assertEquals(16, $cipher->blockSize()); return $cipher; } /** * @covers VirgilSymmetricCipher::setPadding * @covers VirgilSymmetricCipher::setIV * @covers VirgilSymmetricCipher::setEncryptionKey * @covers VirgilSymmetricCipher::reset * @covers VirgilSymmetricCipher::update * @covers VirgilSymmetricCipher::finish * @depends test_aes256_object_creation */ public function test_aes256_enc_dec(VirgilSymmetricCipher $cipher) { // Define test values $key = "passphrase"; $phrase = "This string will be encoded."; // Start encoding // Set padding. if ($cipher->isSupportPadding()) { $cipher->setPadding(VirgilSymmetricCipher::VirgilSymmetricCipherPadding_Zeros); } // Set IV. $cipher->setIV($this->generateIV($cipher->ivSize())); // Set key for encoding. $hash = new VirgilHash(VirgilHash::Algorithm_SHA256); $keyHash = $hash->hash($key); $cipher->setEncryptionKey($keyHash); // Finish cipher configuration for encoding. $cipher->reset(); // Encode test string $chunks = str_split($phrase, $cipher->blockSize()); $encodedPhrase = ""; foreach ($chunks as $chunk) { $encodedPhrase .= $cipher->update($chunk); } // Finish encoding. $encodedPhrase .= $cipher->finish(); // Start decoding // Set padding. if ($cipher->isSupportPadding()) { $cipher->setPadding(VirgilSymmetricCipher::VirgilSymmetricCipherPadding_Zeros); } // Set IV. $cipher->setIV($this->generateIV($cipher->ivSize())); // Set key for encoding. $hash = new VirgilHash(VirgilHash::Algorithm_SHA256); $keyHash = $hash->hash($key); $cipher->setDecryptionKey($keyHash); // Finish cipher configuration for encoding. $cipher->reset(); // Decode $chunks = str_split($encodedPhrase, $cipher->blockSize()); $decodedPhrase = ""; foreach ($chunks as $chunk) { $decodedPhrase .= $cipher->update($chunk); } // Finish decoding. $decodedPhrase .= $cipher->finish(); // Check result. $this->assertEquals($phrase, $decodedPhrase); } /** * @covers VirgilSymmetricCipher::aes256 * @covers VirgilSymmetricCipher::toAsn1 * @covers VirgilSymmetricCipher::fromAsn1 */ public function test_aes256_object_save_restore() { $key = "passphrase"; $hash = new VirgilHash(VirgilHash::Algorithm_SHA256); $keyHash = $hash->hash($key); $phrase = "This string will be encoded."; // Encrypt with initial cipher. // Create and configure cipher. $cipher = new VirgilSymmetricCipher(VirgilSymmetricCipher::Algorithm_AES_256_GCM); $cipher->setIV($this->generateIV($cipher->ivSize())); // Set key for encoding. $cipher->setEncryptionKey($keyHash); // Finish cipher configuration for encoding. $cipher->reset(); // Encode test string. $encodedPhrase = $cipher->update($phrase); // Finish encoding. $encodedPhrase .= $cipher->finish(); // Save cipher to the ASN.1 structure $cipherAsn1 = $cipher->toAsn1(); // Decrypt with restored cipher. // Restore cipher to the new instance. $restoredCipher = new VirgilSymmetricCipher(); $restoredCipher->fromAsn1($cipherAsn1); // Check ciphers names to be equal. $this->assertEquals($cipher->name(), $restoredCipher->name()); // Set key for decoding. $restoredCipher->setDecryptionKey($keyHash); // Finish restored cipher configuration for decoding. $restoredCipher->reset(); // Decode test string $decodedPhrase = $restoredCipher->update($encodedPhrase); // Finish encoding. $decodedPhrase .= $restoredCipher->finish(); // Compare initial phrase with decoded phrase. $this->assertEquals($phrase, $decodedPhrase); } private function generateIV($size) { $iv = ""; for ($i = 0; $i < $size; ++$i) { $iv .= pack("C", 0x55); } return $iv; } } ?>