DiffieHellman.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * The OpenID library's Diffie-Hellman implementation.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: See the COPYING file included in this distribution.
  8. *
  9. * @access private
  10. * @package OpenID
  11. * @author JanRain, Inc. <openid@janrain.com>
  12. * @copyright 2005-2008 Janrain, Inc.
  13. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  14. */
  15. require_once 'Auth/OpenID.php';
  16. require_once 'Auth/OpenID/BigMath.php';
  17. function Auth_OpenID_getDefaultMod()
  18. {
  19. return '155172898181473697471232257763715539915724801'.
  20. '966915404479707795314057629378541917580651227423'.
  21. '698188993727816152646631438561595825688188889951'.
  22. '272158842675419950341258706556549803580104870537'.
  23. '681476726513255747040765857479291291572334510643'.
  24. '245094715007229621094194349783925984760375594985'.
  25. '848253359305585439638443';
  26. }
  27. function Auth_OpenID_getDefaultGen()
  28. {
  29. return '2';
  30. }
  31. /**
  32. * The Diffie-Hellman key exchange class. This class relies on
  33. * {@link Auth_OpenID_MathLibrary} to perform large number operations.
  34. *
  35. * @access private
  36. * @package OpenID
  37. */
  38. class Auth_OpenID_DiffieHellman {
  39. var $mod;
  40. var $gen;
  41. var $private;
  42. var $lib = null;
  43. function Auth_OpenID_DiffieHellman($mod = null, $gen = null,
  44. $private = null, $lib = null)
  45. {
  46. if ($lib === null) {
  47. $this->lib = Auth_OpenID_getMathLib();
  48. } else {
  49. $this->lib = $lib;
  50. }
  51. if ($mod === null) {
  52. $this->mod = $this->lib->init(Auth_OpenID_getDefaultMod());
  53. } else {
  54. $this->mod = $mod;
  55. }
  56. if ($gen === null) {
  57. $this->gen = $this->lib->init(Auth_OpenID_getDefaultGen());
  58. } else {
  59. $this->gen = $gen;
  60. }
  61. if ($private === null) {
  62. $r = $this->lib->rand($this->mod);
  63. $this->private = $this->lib->add($r, 1);
  64. } else {
  65. $this->private = $private;
  66. }
  67. $this->public = $this->lib->powmod($this->gen, $this->private,
  68. $this->mod);
  69. }
  70. function getSharedSecret($composite)
  71. {
  72. return $this->lib->powmod($composite, $this->private, $this->mod);
  73. }
  74. function getPublicKey()
  75. {
  76. return $this->public;
  77. }
  78. function usingDefaultValues()
  79. {
  80. return ($this->mod == Auth_OpenID_getDefaultMod() &&
  81. $this->gen == Auth_OpenID_getDefaultGen());
  82. }
  83. function xorSecret($composite, $secret, $hash_func)
  84. {
  85. $dh_shared = $this->getSharedSecret($composite);
  86. $dh_shared_str = $this->lib->longToBinary($dh_shared);
  87. $hash_dh_shared = $hash_func($dh_shared_str);
  88. $xsecret = "";
  89. for ($i = 0; $i < Auth_OpenID::bytes($secret); $i++) {
  90. $xsecret .= chr(ord($secret[$i]) ^ ord($hash_dh_shared[$i]));
  91. }
  92. return $xsecret;
  93. }
  94. }