EncryptSodiumTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * Unit Tests and KAT Tests for Sodium class. (Introduced in PHP7: libsodium
  4. *
  5. * @group ko7
  6. * @group ko7.encrypt
  7. *
  8. * @package KO7/Encrypt
  9. * @category Test
  10. * even Team
  11. * @copyright (c) Koseven Team
  12. * @license https://koseven.dev/LICENSE
  13. */
  14. class EncryptSodiumTest extends EncryptTestBase {
  15. /**
  16. * Setup class (should be created within every test)
  17. *
  18. * @return void
  19. * @throws KO7_Exception
  20. */
  21. public function setUp(): void
  22. {
  23. if (!extension_loaded('sodium')) {
  24. $this->markTestSkipped('The Sodium extension is not available.');
  25. }
  26. if (!sodium_crypto_aead_aes256gcm_is_available()) {
  27. $this->markTestSkipped('Sodium AEAD AES 256 GCM is not available.');
  28. }
  29. $this->set_config([
  30. 'type' => 'sodium',
  31. 'key' => EncryptTestBase::KEY32,
  32. 'cipher' => Encrypt_Engine_Sodium::AES_256_GCM
  33. ]);
  34. parent::setUp();
  35. }
  36. /**
  37. * Tests against known answers
  38. *
  39. * @dataProvider provider_kat
  40. * @covers KO7_Encrypt_Engine_Sodium::encrypt
  41. *
  42. * @param array $vectors Known Answers
  43. *
  44. * @throws KO7_Exception
  45. */
  46. public function test_kat(array $vectors)
  47. {
  48. // Init
  49. extract($vectors);
  50. $this->set_config([
  51. 'type' => 'sodium',
  52. 'key' => EncryptTestBase::KEY32,
  53. 'cipher' => $cipher
  54. ]);
  55. // Test encryption with known answers
  56. $encrypt = Encrypt::instance();
  57. $encrypted = $encrypt->encode($plaintext, $iv);
  58. $this->assertEquals($ciphertext, $encrypted);
  59. $decrypted = $encrypt->decode($encrypted);
  60. $this->assertEquals($decrypted, $plaintext);
  61. }
  62. /**
  63. * Tests decrypt function with invalid ciphertext
  64. */
  65. public function test_decrypt_invalid()
  66. {
  67. $encrypt = Encrypt::instance();
  68. $this->assertNull($encrypt->decode(':/invalid?1'));
  69. $this->assertNull($encrypt->decode(base64_encode('asdasd')));
  70. }
  71. /**
  72. * Test with invalid Key Length and expect exception
  73. *
  74. * @throws KO7_Exception
  75. */
  76. public function test_invalid_key_length()
  77. {
  78. $this->expectException(KO7_Exception::class);
  79. // Init
  80. $this->set_config([
  81. 'type' => 'sodium',
  82. 'key' => '1234',
  83. 'cipher' => Encrypt_Engine_Sodium::AES_256_GCM
  84. ]);
  85. // Expect Exception
  86. $encrypt = Encrypt::instance();
  87. }
  88. /**
  89. * Test with invalid / not set key and expect exception
  90. *
  91. * @throws KO7_Exception
  92. */
  93. public function test_invalid_key()
  94. {
  95. $this->expectException(KO7_Exception::class);
  96. // Init
  97. $this->set_config([
  98. 'type' => 'sodium',
  99. 'key' => NULL,
  100. 'cipher' => Encrypt_Engine_Sodium::AES_256_GCM
  101. ]);
  102. // Expect Exception
  103. $encrypt = Encrypt::instance();
  104. }
  105. /**
  106. * Test with non existent Engine Name and expect Exception
  107. *
  108. * @throws KO7_Exception
  109. */
  110. public function test_non_existent_engine()
  111. {
  112. $this->expectException(KO7_Exception::class);
  113. // Init
  114. $this->set_config([
  115. 'type' => 'doesnotexist',
  116. 'key' => EncryptTestBase::KEY32,
  117. 'cipher' => Encrypt_Engine_Sodium::AES_256_GCM
  118. ]);
  119. // Expect Exception
  120. $encrypt = Encrypt::instance();
  121. }
  122. /**
  123. * Testing to string function
  124. *
  125. * @throws KO7_Exception
  126. */
  127. public function test___toString()
  128. {
  129. $encrypt = Encrypt::instance();
  130. $this->assertSame("Encrypt_Engine_Sodium (default)", (string)$encrypt);
  131. }
  132. /**
  133. * Data source for kat
  134. *
  135. * @return array
  136. */
  137. public function provider_kat(): array
  138. {
  139. return [
  140. [
  141. [
  142. 'iv' => '000000000000',
  143. 'ciphertext' => 'eyJpdiI6Ik1EQXdNREF3TURBd01EQXciLCJ2YWx1ZSI6Inp2MncyTmdQWW5TcFdBclV2Y2xsblVTVzFQMD0ifQ==',
  144. 'plaintext' => 'test',
  145. 'cipher' => Encrypt_Engine_Sodium::AES_256_GCM
  146. ]
  147. ],
  148. [
  149. [
  150. 'iv' => '111111111111',
  151. 'ciphertext' => 'eyJpdiI6Ik1URXhNVEV4TVRFeE1URXgiLCJ2YWx1ZSI6ImxSVXgwWDBNQklkSXRUOGw5cGIwVmtMSm96XC9GIn0=',
  152. 'plaintext' => 'test2',
  153. 'cipher' => NULL
  154. ]
  155. ],
  156. [
  157. [
  158. 'iv' => '222222222222',
  159. 'ciphertext' => 'eyJpdiI6Ik1qSXlNakl5TWpJeU1qSXkiLCJ2YWx1ZSI6Ik5OWGxETGNmZmlhKzlZd2xOSDFvSzZCd1wvRmlBIn0=',
  160. 'plaintext' => 'test3',
  161. 'cipher' => NULL
  162. ]
  163. ],
  164. [
  165. [
  166. 'iv' => '33333333',
  167. 'ciphertext' => 'eyJpdiI6Ik16TXpNek16TXpNPSIsInZhbHVlIjoibGZBYjZ3eGdISGZmQ2o2aWduSE1sWlg5UEQ4aSJ9',
  168. 'plaintext' => 'test4',
  169. 'cipher' => Encrypt_Engine_Sodium::CHACHA_POLY
  170. ]
  171. ],
  172. [
  173. [
  174. 'iv' => '44444444',
  175. 'ciphertext' => 'eyJpdiI6Ik5EUTBORFEwTkRRPSIsInZhbHVlIjoiZ2xaVXRMaFJEN29KK2RLU3pxdjA4RlpwdFVNcyJ9',
  176. 'plaintext' => 'test5',
  177. 'cipher' => Encrypt_Engine_Sodium::CHACHA_POLY
  178. ]
  179. ],
  180. [
  181. [
  182. 'iv' => '55555555',
  183. 'ciphertext' => 'eyJpdiI6Ik5UVTFOVFUxTlRVPSIsInZhbHVlIjoiYTJNbjNXWFdRbVNhbnZuXC9XeTFQeHM3ZHJvXC9qIn0=',
  184. 'plaintext' => 'test6',
  185. 'cipher' => Encrypt_Engine_Sodium::CHACHA_POLY
  186. ]
  187. ],
  188. [
  189. [
  190. 'iv' => '666666666666',
  191. 'ciphertext' => 'eyJpdiI6Ik5qWTJOalkyTmpZMk5qWTIiLCJ2YWx1ZSI6IldNcDVFdXVZTGZuM3F1SFlBaE5jZlVGTkxcL1IxIn0=',
  192. 'plaintext' => 'test7',
  193. 'cipher' => Encrypt_Engine_Sodium::CHACHA_POLY_IETF
  194. ]
  195. ],
  196. [
  197. [
  198. 'iv' => '777777777777',
  199. 'ciphertext' => 'eyJpdiI6Ik56YzNOemMzTnpjM056YzMiLCJ2YWx1ZSI6ImFxd0ZMM1ZBXC8rUjI0Vzk4aHRTbkhOS29ibEE5In0=',
  200. 'plaintext' => 'test8',
  201. 'cipher' => Encrypt_Engine_Sodium::CHACHA_POLY_IETF
  202. ]
  203. ],
  204. [
  205. [
  206. 'iv' => '888888888888',
  207. 'ciphertext' => 'eyJpdiI6Ik9EZzRPRGc0T0RnNE9EZzQiLCJ2YWx1ZSI6InZGR0Q0Nnhac0NmMWlnQWd0dnM5cXpVZjA2eFoifQ==',
  208. 'plaintext' => 'test9',
  209. 'cipher' => Encrypt_Engine_Sodium::CHACHA_POLY_IETF
  210. ]
  211. ],
  212. [
  213. [
  214. 'iv' => '999999999999999999999999',
  215. 'ciphertext' => 'eyJpdiI6Ik9UazVPVGs1T1RrNU9UazVPVGs1T1RrNU9UazVPVGs1IiwidmFsdWUiOiJkVXRrZ3lzUG9SN3FMblhveU5mdTlPeG5uT3puOVE9PSJ9',
  216. 'plaintext' => 'test10',
  217. 'cipher' => Encrypt_Engine_Sodium::XCHACHA_POLY_IETF
  218. ]
  219. ],
  220. [
  221. [
  222. 'iv' => '000000000000000000000000',
  223. 'ciphertext' => 'eyJpdiI6Ik1EQXdNREF3TURBd01EQXdNREF3TURBd01EQXdNREF3IiwidmFsdWUiOiJmUyt3dzBQdENkUDlRZ1ZxeW1yN2Y4dkg0ZFlsMlE9PSJ9',
  224. 'plaintext' => 'test11',
  225. 'cipher' => Encrypt_Engine_Sodium::XCHACHA_POLY_IETF
  226. ]
  227. ],
  228. [
  229. [
  230. 'iv' => '111111111111111111111111',
  231. 'ciphertext' => 'eyJpdiI6Ik1URXhNVEV4TVRFeE1URXhNVEV4TVRFeE1URXhNVEV4IiwidmFsdWUiOiJPMU9oalE5UmVVdXRqM0pXbEhIWVp2aWN2SmtjTEE9PSJ9',
  232. 'plaintext' => 'test12',
  233. 'cipher' => Encrypt_Engine_Sodium::XCHACHA_POLY_IETF
  234. ]
  235. ]
  236. ];
  237. }
  238. }