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