EncryptOpensslTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Unit Tests and KAT Tests for OpenSSL - which is considered as default encryption driver
  4. *
  5. * @group ko7
  6. * @group ko7.encrypt
  7. *
  8. * @package KO7/Encrypt
  9. * @category Test
  10. *
  11. * @copyright (c) 2007-2012 Kohana Team
  12. * @copyright (c) 2016-2018 Koseven Team
  13. * @license https://koseven.dev/LICENSE
  14. */
  15. class EncryptOpensslTest extends EncryptTestBase {
  16. /**
  17. * Setup class (should be created within every test)
  18. *
  19. * @return void
  20. * @throws KO7_Exception
  21. */
  22. public function setUp(): void
  23. {
  24. if (!extension_loaded('openssl')) {
  25. $this->markTestSkipped('The OpenSSL extension is not available.');
  26. }
  27. $this->set_config([
  28. 'type' => 'openssl',
  29. 'key' => EncryptTestBase::KEY32,
  30. 'cipher' => Encrypt_Engine_Openssl::AES_256_CBC
  31. ]);
  32. parent::setUp();
  33. }
  34. /**
  35. * Testing against KAT Vectors
  36. *
  37. * @dataProvider provider_kat
  38. *
  39. * @param array $vectors
  40. *
  41. * @return void
  42. * @throws KO7_Exception
  43. */
  44. public function test_kat(array $vectors)
  45. {
  46. // Init
  47. extract($vectors);
  48. $this->set_config([
  49. 'type' => 'openssl',
  50. 'key' => $key,
  51. 'cipher' => $cipher
  52. ]);
  53. // Test encryption with known answers
  54. $encrypt = Encrypt::instance();
  55. $encrypted = $encrypt->encode($plaintext, $iv);
  56. $this->assertEquals($ciphertext, $encrypted);
  57. $decrypted = $encrypt->decode($encrypted);
  58. $this->assertEquals($decrypted, $plaintext);
  59. }
  60. /**
  61. * Test decryption with invalid message
  62. *
  63. * @throws KO7_Exception
  64. */
  65. public function test_decrypt_invalid()
  66. {
  67. $encrypt = Encrypt::instance();
  68. $this->assertNull($encrypt->decode('invalid!'));
  69. }
  70. /**
  71. * Data source for kat
  72. *
  73. * @return array
  74. */
  75. public function provider_kat(): array
  76. {
  77. return [
  78. [
  79. [
  80. 'iv' => '0000000000000000',
  81. 'ciphertext' => 'eyJpdiI6Ik1EQXdNREF3TURBd01EQXdNREF3TUE9PSIsInZhbHVlIjoiTCtBemVKNXdKQ2FVVFNJNlwvdjdcL0VRPT0iLCJtYWMiOiJjNTQ5MWJiMWI5OTY2NWY2ZDNiYWZkMTllNjlkYzViZDFmZjU2NmI1ZGRmZWNlZjJlMWMxZDg3ODUxOTUzYmYzIn0=',
  82. 'plaintext' => 'test',
  83. 'cipher' => Encrypt_Engine_Openssl::AES_256_CBC,
  84. 'key' => EncryptTestBase::KEY32
  85. ]
  86. ],
  87. [
  88. [
  89. 'iv' => '1111111111111111',
  90. 'ciphertext' => 'eyJpdiI6Ik1URXhNVEV4TVRFeE1URXhNVEV4TVE9PSIsInZhbHVlIjoibUQzdFVadld5OG1mY2F1XC9hcTlaMnc9PSIsIm1hYyI6IjIzYzllM2NmZTdkMzAzYWUzNzE1OWNmZGQ4ZWM3OTgyMzZiOTk0NjI4YjBkMTIxYTdlMDQ5MTI2ODk1OTEwNGEifQ==',
  91. 'plaintext' => 'test2',
  92. 'cipher' => NULL,
  93. 'key' => EncryptTestBase::KEY32
  94. ]
  95. ],
  96. [
  97. [
  98. 'iv' => '2222222222222222',
  99. 'ciphertext' => 'eyJpdiI6Ik1qSXlNakl5TWpJeU1qSXlNakl5TWc9PSIsInZhbHVlIjoicmF6WE1Va0dKYkNJUCs5YlV4RzRZUT09IiwibWFjIjoiNDNhNTE1YWVhODA3OTMyMGZlMTBiZGZhNzA2NDRlOGQ0YmQyNjE1ZDFmMTVmYmFiMDk2ZDIyYzI0ZTVkN2FmNCJ9',
  100. 'plaintext' => 'test3',
  101. 'cipher' => Encrypt_Engine_Openssl::AES_128_CBC,
  102. 'key' => EncryptTestBase::KEY16
  103. ]
  104. ],
  105. [
  106. [
  107. 'iv' => '3333333333333333',
  108. 'ciphertext' => 'eyJpdiI6Ik16TXpNek16TXpNek16TXpNek16TXc9PSIsInZhbHVlIjoiUkRxemZGNnB5Z2JyTGZBd3NCS1N2QT09IiwibWFjIjoiZDJiMzI1NDY1YjU4YjVjYjA5ZWUyOGE2ZGY1NDgxZjcwNjc3ODg1YTZlNmJmOWY1NjFjYWYxOTZlNGNkY2QxMSJ9',
  109. 'plaintext' => 'test4',
  110. 'cipher' => Encrypt_Engine_Openssl::AES_128_CBC,
  111. 'key' => EncryptTestBase::KEY16
  112. ]
  113. ]
  114. ];
  115. }
  116. }