436
|
1 |
<?php
|
|
2 |
|
|
3 |
/*
|
|
4 |
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
|
|
5 |
* Version 1.1.2 (Caoineag alpha 2)
|
|
6 |
* Copyright (C) 2006-2007 Dan Fuhry
|
|
7 |
* diffiehellman.php - Diffie Hellman key exchange and supporting functions
|
|
8 |
*
|
|
9 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
|
|
10 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
|
|
11 |
*
|
|
12 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
|
|
13 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
|
|
14 |
*/
|
|
15 |
|
|
16 |
/**
|
|
17 |
* The Diffie-Hellman key exchange protocol
|
|
18 |
*/
|
|
19 |
|
467
e4bbd6fb8df3
Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
diff
changeset
|
20 |
$GLOBALS['_math'] = enanomath_create();
|
436
|
21 |
// Our prime number as a base for operations.
|
|
22 |
$GLOBALS['dh_prime'] = '82818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321';
|
|
23 |
|
|
24 |
// g, a primitive root used as an exponent
|
|
25 |
// (2 and 5 are acceptable, but BigInt is faster with odd numbers)
|
|
26 |
$GLOBALS['dh_g'] = '5';
|
|
27 |
|
|
28 |
/**
|
|
29 |
* Generates a Diffie-Hellman private key
|
|
30 |
* @return string(BigInt)
|
|
31 |
*/
|
|
32 |
|
|
33 |
function dh_gen_private()
|
|
34 |
{
|
|
35 |
global $_math;
|
|
36 |
return $_math->random(256);
|
|
37 |
}
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Calculates the public key from the private key
|
|
41 |
* @param string(BigInt)
|
|
42 |
* @return string(BigInt)
|
|
43 |
*/
|
|
44 |
|
|
45 |
function dh_gen_public($b)
|
|
46 |
{
|
|
47 |
global $_math, $dh_g, $dh_prime;
|
|
48 |
return $_math->powmod($dh_g, $b, $dh_prime);
|
|
49 |
}
|
|
50 |
|
|
51 |
/**
|
|
52 |
* Calculates the shared secret.
|
|
53 |
* @param string(BigInt) Our private key
|
|
54 |
* @param string(BigInt) Remote party's public key
|
|
55 |
* @return string(BigInt)
|
|
56 |
*/
|
|
57 |
|
|
58 |
function dh_gen_shared_secret($a, $B)
|
|
59 |
{
|
|
60 |
global $_math, $dh_g, $dh_prime;
|
|
61 |
return $_math->powmod($B, $a, $dh_prime);
|
|
62 |
}
|
|
63 |
|
|
64 |
/*
|
|
65 |
SHA-256 algorithm - ported from Javascript
|
|
66 |
|
|
67 |
Copyright (c) 2003-2004, Angel Marin
|
|
68 |
All rights reserved.
|
|
69 |
Portions copyright (c) 2008 Dan Fuhry.
|
|
70 |
|
|
71 |
Redistribution and use in source and binary forms, with or without modification,
|
|
72 |
are permitted provided that the following conditions are met:
|
|
73 |
|
|
74 |
* Redistributions of source code must retain the above copyright notice, this
|
|
75 |
list of conditions and the following disclaimer.
|
|
76 |
* Redistributions in binary form must reproduce the above copyright notice,
|
|
77 |
this list of conditions and the following disclaimer in the documentation
|
|
78 |
and/or other materials provided with the distribution.
|
|
79 |
* Neither the name of the <ORGANIZATION> nor the names of its contributors may
|
|
80 |
be used to endorse or promote products derived from this software without
|
|
81 |
specific prior written permission.
|
|
82 |
|
|
83 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
84 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
85 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
86 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
87 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
88 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
89 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
90 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
91 |
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
92 |
OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
93 |
*/
|
|
94 |
class SHA256
|
|
95 |
{
|
|
96 |
var $chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
|
|
97 |
|
|
98 |
function safe_add ($x, $y) {
|
|
99 |
$lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
|
|
100 |
$msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
|
|
101 |
return ($msw << 16) | ($lsw & 0xFFFF);
|
|
102 |
}
|
|
103 |
function rshz($X, $n)
|
|
104 |
{
|
|
105 |
// equivalent to $X >>> $n in javascript
|
|
106 |
// pulled from http://www.tapouillo.com/firefox_extension/sourcecode.txt, public domain
|
|
107 |
$z = hexdec(80000000);
|
|
108 |
if ($z & $X)
|
|
109 |
{
|
|
110 |
$X = ($X>>1);
|
|
111 |
$X &= (~$z);
|
|
112 |
$X |= 0x40000000;
|
|
113 |
$X = ($X>>($n-1));
|
|
114 |
}
|
|
115 |
else
|
|
116 |
{
|
|
117 |
$X = ($X>>$n);
|
|
118 |
}
|
|
119 |
return $X;
|
|
120 |
}
|
|
121 |
function S ($X, $n) {return ( $this->rshz($X, $n) ) | ($X << (32 - $n));}
|
|
122 |
function R ($X, $n) {return ( $this->rshz($X, $n) );}
|
|
123 |
function Ch($x, $y, $z) {return (($x & $y) ^ ((~$x) & $z));}
|
|
124 |
function Maj($x, $y, $z) {return (($x & $y) ^ ($x & $z) ^ ($y & $z));}
|
|
125 |
function Sigma0256($x) {return ($this->S($x, 2) ^ $this->S($x, 13) ^ $this->S($x, 22));}
|
|
126 |
function Sigma1256($x) {return ($this->S($x, 6) ^ $this->S($x, 11) ^ $this->S($x, 25));}
|
|
127 |
function Gamma0256($x) {return ($this->S($x, 7) ^ $this->S($x, 18) ^ $this->R($x, 3));}
|
|
128 |
function Gamma1256($x) {return ($this->S($x, 17) ^ $this->S($x, 19) ^ $this->R($x, 10));}
|
|
129 |
function core_sha256 ($m, $l) {
|
|
130 |
$K = Array(0x428A2F98,0x71374491,0xB5C0FBCF,0xE9B5DBA5,0x3956C25B,0x59F111F1,0x923F82A4,0xAB1C5ED5,0xD807AA98,0x12835B01,0x243185BE,0x550C7DC3,0x72BE5D74,0x80DEB1FE,0x9BDC06A7,0xC19BF174,0xE49B69C1,0xEFBE4786,0xFC19DC6,0x240CA1CC,0x2DE92C6F,0x4A7484AA,0x5CB0A9DC,0x76F988DA,0x983E5152,0xA831C66D,0xB00327C8,0xBF597FC7,0xC6E00BF3,0xD5A79147,0x6CA6351,0x14292967,0x27B70A85,0x2E1B2138,0x4D2C6DFC,0x53380D13,0x650A7354,0x766A0ABB,0x81C2C92E,0x92722C85,0xA2BFE8A1,0xA81A664B,0xC24B8B70,0xC76C51A3,0xD192E819,0xD6990624,0xF40E3585,0x106AA070,0x19A4C116,0x1E376C08,0x2748774C,0x34B0BCB5,0x391C0CB3,0x4ED8AA4A,0x5B9CCA4F,0x682E6FF3,0x748F82EE,0x78A5636F,0x84C87814,0x8CC70208,0x90BEFFFA,0xA4506CEB,0xBEF9A3F7,0xC67178F2);
|
|
131 |
$HASH = Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19);
|
|
132 |
$W = Array(64);
|
|
133 |
/* append padding */
|
|
134 |
$m[$l >> 5] |= 0x80 << (24 - $l % 32);
|
|
135 |
$m[(($l + 64 >> 9) << 4) + 15] = $l;
|
|
136 |
for ( $i = 0; $i<count($m); $i+=16 ) {
|
|
137 |
$a = $HASH[0];
|
|
138 |
$b = $HASH[1];
|
|
139 |
$c = $HASH[2];
|
|
140 |
$d = $HASH[3];
|
|
141 |
$e = $HASH[4];
|
|
142 |
$f = $HASH[5];
|
|
143 |
$g = $HASH[6];
|
|
144 |
$h = $HASH[7];
|
|
145 |
for ( $j = 0; $j<64; $j++)
|
|
146 |
{
|
|
147 |
if ( $j < 16 )
|
|
148 |
{
|
|
149 |
$W[$j] = ( isset($m[$j + $i]) ) ? $m[$j + $i] : 0;
|
|
150 |
}
|
|
151 |
else
|
|
152 |
{
|
|
153 |
$W[$j] = $this->safe_add(
|
|
154 |
$this->safe_add(
|
|
155 |
$this->safe_add(
|
|
156 |
$this->Gamma1256($W[$j - 2]), $W[$j - 7]),
|
|
157 |
$this->Gamma0256($W[$j - 15])),
|
|
158 |
$W[$j - 16]);
|
|
159 |
}
|
|
160 |
$T1 = $this->safe_add(
|
|
161 |
$this->safe_add(
|
|
162 |
$this->safe_add(
|
|
163 |
$this->safe_add($h, $this->Sigma1256($e)
|
|
164 |
),
|
|
165 |
$this->Ch($e, $f, $g)),
|
|
166 |
$K[$j]),
|
|
167 |
$W[$j]);
|
|
168 |
$T2 = $this->safe_add($this->Sigma0256($a), $this->Maj($a, $b, $c));
|
|
169 |
$h = $g;
|
|
170 |
$g = $f;
|
|
171 |
$f = $e;
|
|
172 |
$e = $this->safe_add($d, $T1);
|
|
173 |
$d = $c;
|
|
174 |
$c = $b;
|
|
175 |
$b = $a;
|
|
176 |
$a = $this->safe_add($T1, $T2);
|
|
177 |
}
|
|
178 |
$HASH[0] = $this->safe_add($a, $HASH[0]);
|
|
179 |
$HASH[1] = $this->safe_add($b, $HASH[1]);
|
|
180 |
$HASH[2] = $this->safe_add($c, $HASH[2]);
|
|
181 |
$HASH[3] = $this->safe_add($d, $HASH[3]);
|
|
182 |
$HASH[4] = $this->safe_add($e, $HASH[4]);
|
|
183 |
$HASH[5] = $this->safe_add($f, $HASH[5]);
|
|
184 |
$HASH[6] = $this->safe_add($g, $HASH[6]);
|
|
185 |
$HASH[7] = $this->safe_add($h, $HASH[7]);
|
|
186 |
}
|
|
187 |
return $HASH;
|
|
188 |
}
|
|
189 |
function str2binb ($str) {
|
|
190 |
$bin = Array();
|
|
191 |
for ( $i = 0; $i < strlen($str); $i++ )
|
|
192 |
{
|
|
193 |
$byte = ord($str{$i});
|
|
194 |
$block = floor($i / 4);
|
|
195 |
$stage = $i % 4;
|
|
196 |
if ( $stage == 0 )
|
|
197 |
{
|
|
198 |
$bin[$block] = $byte;
|
|
199 |
}
|
|
200 |
else
|
|
201 |
{
|
|
202 |
$bin[$block] <<= 8;
|
|
203 |
$bin[$block] |= $byte;
|
|
204 |
}
|
|
205 |
}
|
|
206 |
while ( $stage < 3 )
|
|
207 |
{
|
|
208 |
$stage++;
|
|
209 |
$bin[$block] <<= 8;
|
|
210 |
}
|
|
211 |
return $bin;
|
|
212 |
}
|
|
213 |
function byte2hex($byte)
|
|
214 |
{
|
|
215 |
$b = dechex(ord($byte));
|
|
216 |
return ( strlen($b) < 2 ) ? "0$b" : $b;
|
|
217 |
}
|
|
218 |
function binb2hex ($binarray) {
|
|
219 |
$hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
|
|
220 |
$hex_tab = $hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
|
|
221 |
$str = "";
|
|
222 |
foreach ( $binarray as $bytes )
|
|
223 |
{
|
|
224 |
$str .= implode('', array(
|
|
225 |
$this->byte2hex(chr(( $bytes >> 24 ) & 0xFF)),
|
|
226 |
$this->byte2hex(chr(( $bytes >> 16 ) & 0xFF)),
|
|
227 |
$this->byte2hex(chr(( $bytes >> 8 ) & 0xFF)),
|
|
228 |
$this->byte2hex(chr($bytes & 0xFF))
|
|
229 |
));
|
|
230 |
}
|
|
231 |
return $str;
|
|
232 |
}
|
|
233 |
function hex_sha256 ( $s )
|
|
234 |
{
|
|
235 |
return $this->binb2hex(
|
|
236 |
$this->core_sha256(
|
|
237 |
$this->str2binb($s),
|
|
238 |
strlen($s) * $this->chrsz)
|
|
239 |
);
|
|
240 |
}
|
|
241 |
}
|
|
242 |
|
|
243 |
if ( !function_exists('sha256') )
|
|
244 |
{
|
|
245 |
function sha256($text)
|
|
246 |
{
|
|
247 |
static $sha_obj = false;
|
|
248 |
if ( !is_object($sha_obj) )
|
|
249 |
$sha_obj = new SHA256();
|
|
250 |
return $sha_obj->hex_sha256($text);
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
?>
|