PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/trunk/tests/Zend/Crypt/DiffieHellmanTest.php

http://firephp.googlecode.com/
PHP | 156 lines | 103 code | 19 blank | 34 comment | 3 complexity | ef63b655bed7353e1d046e78fc58b1e7 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Crypt
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: DiffieHellmanTest.php 23772 2011-02-28 21:35:29Z ralph $
  21. */
  22. require_once 'Zend/Crypt/DiffieHellman.php';
  23. require_once 'Zend/Crypt/Math/BigInteger.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Crypt
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Crypt
  31. */
  32. class Zend_Crypt_DiffieHellmanTest extends PHPUnit_Framework_TestCase
  33. {
  34. public function setUp()
  35. {
  36. try {
  37. $math = new Zend_Crypt_Math_BigInteger();
  38. } catch (Zend_Crypt_Math_BigInteger_Exception $e) {
  39. if (strpos($e->getMessage(), 'big integer precision math support not detected') !== false) {
  40. $this->markTestSkipped($e->getMessage());
  41. } else {
  42. throw $e;
  43. }
  44. }
  45. }
  46. public function testDiffieWithSpec()
  47. {
  48. $aliceOptions = array(
  49. 'prime'=>'563',
  50. 'generator'=>'5',
  51. 'private'=>'9'
  52. );
  53. $bobOptions = array(
  54. 'prime'=>'563',
  55. 'generator'=>'5',
  56. 'private'=>'14'
  57. );
  58. Zend_Crypt_DiffieHellman::$useOpenssl = false;
  59. $alice = new Zend_Crypt_DiffieHellman($aliceOptions['prime'], $aliceOptions['generator'], $aliceOptions['private']);
  60. $bob = new Zend_Crypt_DiffieHellman($bobOptions['prime'], $bobOptions['generator'], $bobOptions['private']);
  61. $alice->generateKeys();
  62. $bob->generateKeys();
  63. $this->assertEquals('78', $alice->getPublicKey());
  64. $this->assertEquals('534', $bob->getPublicKey());
  65. $aliceSecretKey = $alice->computeSecretKey($bob->getPublicKey());
  66. $bobSecretKey = $bob->computeSecretKey($alice->getPublicKey());
  67. // both Alice and Bob should now have the same secret key
  68. $this->assertEquals('117', $aliceSecretKey);
  69. $this->assertEquals('117', $bobSecretKey);
  70. }
  71. public function testDiffieWithBinaryFormsAndLargeIntegers()
  72. {
  73. $aliceOptions = array(
  74. 'prime' => '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443',
  75. 'generator'=>'2',
  76. 'private' => '99209314066572595236408569591967988557141249561494267486251808035535396332278620143536317681312712891672623072630995180324388841681491857745515696789091127409515009250358965816666146342049838178521379132153348139908016819196219448310107072632515749339055798122538615135104828702523796951800575031871051678091'
  77. );
  78. $bobOptions = array(
  79. 'prime' => '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443',
  80. 'generator'=>'2',
  81. 'private' => '33411735792639558625733635717892563612548180650402161151077478314841463707948899786103588912325634730410551946772752880177868972816963551821740386700076034213408153924692562543117963464733156600545484510833072427003474207064650714831083304497737160382097083356876078146231616972608703322302585471319261275664'
  82. );
  83. Zend_Crypt_DiffieHellman::$useOpenssl = false;
  84. $alice = new Zend_Crypt_DiffieHellman($aliceOptions['prime'], $aliceOptions['generator'], $aliceOptions['private']);
  85. $bob = new Zend_Crypt_DiffieHellman($bobOptions['prime'], $bobOptions['generator'], $bobOptions['private']);
  86. $alice->generateKeys();
  87. $bob->generateKeys();
  88. //0DmJUe9dr02pAtVoGyLHdC+rfBU3mDCelKGPXRDFHofx6mFfN2gcZCmp/ab4ezDXfpIBOatpVdbn2fTNUGo64DtKE2WGTsZCl90RgrGUv8XW/4WDPXeE7g5u7KWHBG/LCE5+XsilE5P5/GIyqr9gsiudTmk+H/hiYZl9Smar9k0=
  89. $this->assertEquals('ANA5iVHvXa9NqQLVaBsix3Qvq3wVN5gwnpShj10QxR6H8ephXzdoHGQpqf2m+Hsw136SATmraVXW59n0zVBqOuA7ShNlhk7GQpfdEYKxlL/F1v+Fgz13hO4ObuylhwRvywhOfl7IpROT+fxiMqq/YLIrnU5pPh/4YmGZfUpmq/ZN', base64_encode($alice->getPublicKey(Zend_Crypt_DiffieHellman::BINARY)));
  90. //v8puCBaHdch0stxmkyS/sZvZHyB5f0AVkopAQ5wKSZIyEHHcGn7DXXH2u4WdCL+kMr8BcRpxRBJ0TDwfZPpu53nFNEjUd81WlfaKk95e4a/DC4dhlfBkQMebleobhedQPFAo7F9SkHN7uTLa/glxG+3T9DTb+ikcOVPoH3A1G6g=
  91. $this->assertEquals('AL/KbggWh3XIdLLcZpMkv7Gb2R8geX9AFZKKQEOcCkmSMhBx3Bp+w11x9ruFnQi/pDK/AXEacUQSdEw8H2T6bud5xTRI1HfNVpX2ipPeXuGvwwuHYZXwZEDHm5XqG4XnUDxQKOxfUpBze7ky2v4JcRvt0/Q02/opHDlT6B9wNRuo', base64_encode($bob->getPublicKey(Zend_Crypt_DiffieHellman::BINARY)));
  92. $aliceSecretKey = $alice->computeSecretKey($bob->getPublicKey(Zend_Crypt_DiffieHellman::BINARY), Zend_Crypt_DiffieHellman::BINARY, Zend_Crypt_DiffieHellman::BINARY);
  93. $bobSecretKey = $bob->computeSecretKey($alice->getPublicKey(Zend_Crypt_DiffieHellman::BINARY), Zend_Crypt_DiffieHellman::BINARY, Zend_Crypt_DiffieHellman::BINARY);
  94. // both Alice and Bob should now have the same secret key
  95. $expectedSharedSecret = base64_decode('FAAkw7NN1+raX9K1+dR3nqX2LZcDYYuZH13lpasaDIM4/ZXqbzdgiHZ86SILN27BjmJObtNQG/SNHfhxMalLMtLv+v0JFte/6+pIvMG9tAoPFsVh2BAvBuNpLY5W5gusgQ2p4pvJK0wz9YJ8iFdOHEOnhzYuN7LS/YXx2rBOz0Q=');
  96. $this->assertEquals($expectedSharedSecret, $aliceSecretKey);
  97. $this->assertEquals($expectedSharedSecret, $bobSecretKey);
  98. }
  99. public function testDiffieWithBinaryFormsAndLargeIntegers_OpensslTest()
  100. {
  101. // skip this test if openssl DH support is not available
  102. if (!function_exists('openssl_dh_compute_key')) {
  103. $this->markTestSkipped(
  104. 'An openssl extension with Diffie-Hellman support is not available.'
  105. );
  106. }
  107. $aliceOptions = array(
  108. 'prime' => '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443',
  109. 'generator'=>'2',
  110. 'private' => '99209314066572595236408569591967988557141249561494267486251808035535396332278620143536317681312712891672623072630995180324388841681491857745515696789091127409515009250358965816666146342049838178521379132153348139908016819196219448310107072632515749339055798122538615135104828702523796951800575031871051678091'
  111. );
  112. $bobOptions = array(
  113. 'prime' => '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443',
  114. 'generator'=>'2',
  115. 'private' => '33411735792639558625733635717892563612548180650402161151077478314841463707948899786103588912325634730410551946772752880177868972816963551821740386700076034213408153924692562543117963464733156600545484510833072427003474207064650714831083304497737160382097083356876078146231616972608703322302585471319261275664'
  116. );
  117. $alice = new Zend_Crypt_DiffieHellman($aliceOptions['prime'], $aliceOptions['generator'], $aliceOptions['private']);
  118. $bob = new Zend_Crypt_DiffieHellman($bobOptions['prime'], $bobOptions['generator'], $bobOptions['private']);
  119. $alice->generateKeys();
  120. $bob->generateKeys();
  121. $this->assertEquals('ANA5iVHvXa9NqQLVaBsix3Qvq3wVN5gwnpShj10QxR6H8ephXzdoHGQpqf2m+Hsw136SATmraVXW59n0zVBqOuA7ShNlhk7GQpfdEYKxlL/F1v+Fgz13hO4ObuylhwRvywhOfl7IpROT+fxiMqq/YLIrnU5pPh/4YmGZfUpmq/ZN', base64_encode($alice->getPublicKey(Zend_Crypt_DiffieHellman::BINARY)));
  122. $this->assertEquals('AL/KbggWh3XIdLLcZpMkv7Gb2R8geX9AFZKKQEOcCkmSMhBx3Bp+w11x9ruFnQi/pDK/AXEacUQSdEw8H2T6bud5xTRI1HfNVpX2ipPeXuGvwwuHYZXwZEDHm5XqG4XnUDxQKOxfUpBze7ky2v4JcRvt0/Q02/opHDlT6B9wNRuo', base64_encode($bob->getPublicKey(Zend_Crypt_DiffieHellman::BINARY)));
  123. $aliceSecretKey = $alice->computeSecretKey($bob->getPublicKey(Zend_Crypt_DiffieHellman::BINARY), Zend_Crypt_DiffieHellman::BINARY, Zend_Crypt_DiffieHellman::BINARY);
  124. $bobSecretKey = $bob->computeSecretKey($alice->getPublicKey(Zend_Crypt_DiffieHellman::BINARY), Zend_Crypt_DiffieHellman::BINARY, Zend_Crypt_DiffieHellman::BINARY);
  125. // both Alice and Bob should now have the same secret key
  126. $expectedSharedSecret = base64_decode('FAAkw7NN1+raX9K1+dR3nqX2LZcDYYuZH13lpasaDIM4/ZXqbzdgiHZ86SILN27BjmJObtNQG/SNHfhxMalLMtLv+v0JFte/6+pIvMG9tAoPFsVh2BAvBuNpLY5W5gusgQ2p4pvJK0wz9YJ8iFdOHEOnhzYuN7LS/YXx2rBOz0Q=');
  127. $this->assertEquals($expectedSharedSecret, $aliceSecretKey);
  128. $this->assertEquals($expectedSharedSecret, $bobSecretKey);
  129. }
  130. public function testGenerateKeysWithUnsetPrivateKey()
  131. {
  132. $dh = new Zend_Crypt_DiffieHellman(563, 5);
  133. $dh->generateKeys();
  134. $privateKey = $dh->getPrivateKey();
  135. $this->assertNotNull($privateKey);
  136. }
  137. }