EncryptMcryptTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Unit Tests and KAT Tests for MCRYPT class which is deprecated since 4.0
  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 EncryptMcryptTest 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('mcrypt')) {
  25. $this->markTestSkipped('The Mcrypt extension is not available.');
  26. }
  27. $this->set_config([
  28. 'type' => 'mcrypt',
  29. 'key' => EncryptTestBase::KEY32,
  30. 'cipher' => constant('MCRYPT_RIJNDAEL_128'),
  31. 'mode' => constant('MCRYPT_MODE_CBC'),
  32. ]);
  33. parent::setUp();
  34. }
  35. /**
  36. * Testing against KAT Vectors
  37. *
  38. * @dataProvider provider_kat
  39. *
  40. * @param array $vectors
  41. *
  42. * @return void
  43. * @throws KO7_Exception
  44. */
  45. public function test_kat(array $vectors)
  46. {
  47. // Init
  48. extract($vectors);
  49. if ($cipher !== NULL)
  50. {
  51. $cipher = constant($cipher);
  52. }
  53. if ($mode !== NULL)
  54. {
  55. $mode = constant($mode);
  56. }
  57. $this->set_config([
  58. 'type' => 'mcrypt',
  59. 'key' => $key,
  60. 'cipher' => $cipher,
  61. 'mode' => $mode,
  62. ]);
  63. // Test encryption with known answers
  64. $encrypt = Encrypt::instance();
  65. $encrypted = $encrypt->encode($plaintext, $iv);
  66. $this->assertEquals($ciphertext, $encrypted);
  67. $decrypted = $encrypt->decode($encrypted);
  68. $this->assertEquals($decrypted, $plaintext);
  69. }
  70. /**
  71. * Tests decrypt function with invalid ciphertext
  72. */
  73. public function test_decrypt_invalid()
  74. {
  75. $encrypt = Encrypt::instance();
  76. $this->assertNull($encrypt->decode(':/invalid?1'));
  77. $this->assertNull($encrypt->decode(base64_encode('asdasd')));
  78. }
  79. /**
  80. * Data source for kat
  81. * @deprecated MCRYPT
  82. * @return array
  83. */
  84. public function provider_kat(): array
  85. {
  86. return [
  87. // [
  88. // [
  89. // 'iv' => '0000000000000000',
  90. // 'ciphertext' => 'MDAwMDAwMDAwMDAwMDAwMOzjS0qd+IDZxiED7C1haC0=',
  91. // 'plaintext' => 'test',
  92. // 'cipher' => 'MCRYPT_RIJNDAEL_128',
  93. // 'key' => EncryptTestBase::KEY32,
  94. // 'mode' => 'MCRYPT_MODE_CBC'
  95. // ]
  96. // ],
  97. // [
  98. // [
  99. // 'iv' => '1111111111111111',
  100. // 'ciphertext' => 'MTExMTExMTExMTExMTExMc6vwMFD',
  101. // 'plaintext' => 'test2',
  102. // 'cipher' => NULL,
  103. // 'key' => EncryptTestBase::KEY32,
  104. // 'mode' => 'MCRYPT_MODE_CFB'
  105. // ]
  106. // ],
  107. // [
  108. // [
  109. // 'iv' => '2222222222222222',
  110. // 'ciphertext' => 'MjIyMjIyMjIyMjIyMjIyMi3rgfz1csVLEu+1LQD2+8c=',
  111. // 'plaintext' => 'test3',
  112. // 'cipher' => 'MCRYPT_RIJNDAEL_128',
  113. // 'key' => EncryptTestBase::KEY16,
  114. // 'mode' => 'MCRYPT_MODE_ECB'
  115. // ]
  116. // ],
  117. // [
  118. // [
  119. // 'iv' => '3333333333333333',
  120. // 'ciphertext' => 'MzMzMzMzMzMzMzMzMzMzM9NUlFYkvOEiSXWSYhwjkxg=',
  121. // 'plaintext' => 'test4',
  122. // 'cipher' => 'MCRYPT_RIJNDAEL_128',
  123. // 'key' => EncryptTestBase::KEY16,
  124. // 'mode' => NULL
  125. // ]
  126. // ]
  127. ];
  128. }
  129. }