-
Notifications
You must be signed in to change notification settings - Fork 5
/
Signer.php
277 lines (240 loc) · 7.38 KB
/
Signer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
namespace baibaratsky\WebMoney;
/**
* WebMoney Signer: a native PHP implementation of the WMSigner module
* @package baibaratsky\WebMoney
*/
class Signer
{
const MB_ENCODING = '8bit'; // 8bit is the fastest suitable encoding for mb_* methods
private $power;
private $modulus;
/**
* @param string $wmid Signer WMID
* @param string $key Full path to the key file or a string containing key
* @param string $keyPassword Key file password
*
* @throws \Exception
*/
public function __construct($wmid, $key, $keyPassword)
{
if (empty($wmid)) {
throw new \Exception('WMID not provided.');
}
if (strpos($key, "\0") === false) { // It’s a file path
if (!is_file($key)) {
throw new \Exception('Key file not found: ' . $key);
}
$key = file_get_contents($key);
if ($key === false) {
throw new \Exception('Error reading from the key file.');
}
}
$keyData = unpack('vreserved/vsignFlag/a16hash/Vlength/a*buffer', $key);
$keyBuffer = self::readKeyBuffer($keyData, $wmid, $keyPassword);
if ($keyBuffer === false) {
// Try one more time using only the first half of the password
$keyPassword = mb_substr(
$keyPassword,
0,
floor(mb_strlen($keyPassword, self::MB_ENCODING) / 2),
self::MB_ENCODING
);
$keyBuffer = self::readKeyBuffer($keyData, $wmid, $keyPassword);
if ($keyBuffer === false) {
throw new \Exception('Hash check failed. Key file seems to be corrupted.');
}
}
$this->initSignVariables($keyBuffer);
}
/**
* Create a signature for the given data
*
* @param string $data
*
* @return string
*/
public function sign($data)
{
// Make data hash (16 bytes)
$base = hash('md4', $data, true);
// Add 40 random bytes
for ($i = 0; $i < 10; ++$i) {
$base .= pack('V', mt_rand());
}
// Add the length of the base (56 = 16 + 40) as the first 2 bytes
$base = pack('v', mb_strlen($base, self::MB_ENCODING)) . $base;
// Modular exponentiation
$dec = bcpowmod(self::reverseToDecimal($base), $this->power, $this->modulus, 0);
// Convert to hexadecimal
$hex = self::dec2hex($dec);
// Fill empty bytes with zeros
$hex = str_repeat('0', 132 - mb_strlen($hex, self::MB_ENCODING)) . $hex;
// Reverse byte order
$hexReversed = '';
for ($i = 0; $i < mb_strlen($hex, self::MB_ENCODING) / 4; ++$i) {
$hexReversed = mb_substr($hex, $i * 4, 4, self::MB_ENCODING) . $hexReversed;
}
return mb_strtolower($hexReversed, self::MB_ENCODING);
}
/**
* Initialize the power and the modulus
*
* @param string $keyBuffer
*/
private function initSignVariables($keyBuffer)
{
$data = unpack('Vreserved/vpowerLength', $keyBuffer);
$data = unpack('Vreserved/vpowerLength/a' . $data['powerLength'] . 'power/vmodulusLength', $keyBuffer);
$data = unpack('Vreserved/vpowerLength/a' . $data['powerLength'] . 'power/vmodulusLength/a'
. $data['modulusLength'] . 'modulus', $keyBuffer);
$this->power = self::reverseToDecimal($data['power']);
$this->modulus = self::reverseToDecimal($data['modulus']);
}
/**
* Check and return the key buffer
*
* @param array $keyData
* @param string $wmid
* @param string $keyPassword
*
* @return string|false The key buffer, or false if the hash doesn't match
*/
private static function readKeyBuffer($keyData, $wmid, $keyPassword)
{
$keyData['buffer'] = self::encryptKey($keyData['buffer'], $wmid, $keyPassword);
return self::verifyHash($keyData) ? $keyData['buffer'] : false;
}
/**
* Encrypt the key using the hash of the WMID and the key password
*
* @param string $keyBuffer
* @param string $wmid
* @param string $keyPassword
*
* @return string
*/
private static function encryptKey($keyBuffer, $wmid, $keyPassword)
{
$hash = hash('md4', $wmid . $keyPassword, true);
return self::xorStrings($keyBuffer, $hash, 6);
}
/**
* XOR operation on two strings
*
* @param string $subject
* @param string $modifier
* @param int $shift
*
* @return string
*/
private static function xorStrings($subject, $modifier, $shift = 0)
{
$modifierLength = mb_strlen($modifier, self::MB_ENCODING);
$i = $shift;
$j = 0;
while ($i < mb_strlen($subject, self::MB_ENCODING)) {
$subject[$i] = chr(ord($subject[$i]) ^ ord($modifier[$j]));
++$i;
if (++$j >= $modifierLength) {
$j = 0;
}
}
return $subject;
}
/**
* Verify the hash of the key
*
* @param array $keyData
*
* @return bool
*/
private static function verifyHash($keyData)
{
$verificationString = pack('v', $keyData['reserved'])
. pack('v', 0)
. pack('V4', 0, 0, 0, 0)
. pack('V', $keyData['length'])
. $keyData['buffer'];
$hash = hash('md4', $verificationString, true);
return strcmp($hash, $keyData['hash']) == 0;
}
/**
* Reverse byte order and convert binary data to decimal string
*
* @param string $binaryData
*
* @return string
*/
private static function reverseToDecimal($binaryData)
{
return self::hex2dec(bin2hex(strrev($binaryData)));
}
/**
* Convert a hexadecimal string to a decimal one
*
* @param string $hex
*
* @return string
*/
private static function hex2dec($hex)
{
if (extension_loaded('gmp')) {
return gmp_strval('0x' . $hex);
}
return self::hex2decBC($hex);
}
/**
* Convert a hexadecimal string to a decimal one using BCMath
*
* @param string $hex
*
* @return string
*/
private static function hex2decBC($hex) {
$dec = '0';
$len = mb_strlen($hex, self::MB_ENCODING);
for ($i = 1; $i <= $len; $i++) {
$dec = bcadd(
$dec,
bcmul(
strval(hexdec($hex[$i - 1])),
bcpow('16', strval($len - $i), 0),
0
),
0
);
}
return $dec;
}
/**
* Convert a decimal string to a hexadecimal one
*
* @param string $dec
*
* @return string
*/
private static function dec2hex($dec)
{
if (extension_loaded('gmp')) {
return gmp_strval($dec, 16);
}
return self::dec2hexBC($dec);
}
/**
* Convert a decimal string to a hexadecimal one using BCMath
*
* @param string $dec
*
* @return string
*/
private static function dec2hexBC($dec) {
$hex = '';
while ($dec) {
$modulus = bcmod($dec, '16');
$hex = dechex($modulus) . $hex;
$dec = bcdiv(bcsub($dec, $modulus, 0), '16', 0);
}
return $hex;
}
}