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    var $mod;
46    var $gen;
47    var $private;
48    var $lib = null;
49
50    function Auth_OpenID_DiffieHellman($mod = null, $gen = null,
51                                       $private = null, $lib = null)
52    {
53        if ($lib === null) {
54            $this->lib = Auth_OpenID_getMathLib();
55        } else {
56            $this->lib = $lib;
57        }
58
59        if ($mod === null) {
60            $this->mod = $this->lib->init(Auth_OpenID_getDefaultMod());
61        } else {
62            $this->mod = $mod;
63        }
64
65        if ($gen === null) {
66            $this->gen = $this->lib->init(Auth_OpenID_getDefaultGen());
67        } else {
68            $this->gen = $gen;
69        }
70
71        if ($private === null) {
72            $r = $this->lib->rand($this->mod);
73            $this->private = $this->lib->add($r, 1);
74        } else {
75            $this->private = $private;
76        }
77
78        $this->public = $this->lib->powmod($this->gen, $this->private,
79                                           $this->mod);
80    }
81
82    function getSharedSecret($composite)
83    {
84        return $this->lib->powmod($composite, $this->private, $this->mod);
85    }
86
87    function getPublicKey()
88    {
89        return $this->public;
90    }
91
92    function usingDefaultValues()
93    {
94        return ($this->mod == Auth_OpenID_getDefaultMod() &&
95                $this->gen == Auth_OpenID_getDefaultGen());
96    }
97
98    function xorSecret($composite, $secret, $hash_func)
99    {
100        $dh_shared = $this->getSharedSecret($composite);
101        $dh_shared_str = $this->lib->longToBinary($dh_shared);
102        $hash_dh_shared = $hash_func($dh_shared_str);
103
104        $xsecret = "";
105        for ($i = 0; $i < Auth_OpenID::bytes($secret); $i++) {
106            $xsecret .= chr(ord($secret[$i]) ^ ord($hash_dh_shared[$i]));
107        }
108
109        return $xsecret;
110    }
111}
112
113
114