-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractSRPHandler.php
More file actions
328 lines (268 loc) · 8.11 KB
/
AbstractSRPHandler.php
File metadata and controls
328 lines (268 loc) · 8.11 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
declare(strict_types=1);
namespace Windwalker\SRP;
use Brick\Math\BigInteger;
use Brick\Math\BigNumber;
use Brick\Math\Exception\NumberFormatException;
use Windwalker\SRP\Exception\InvalidSRPParameterException;
abstract class AbstractSRPHandler
{
public const SECRET_128BIT = 16;
public const SECRET_192BIT = 24;
public const SECRET_256BIT = 32;
public const SECRET_512BIT = 64;
public const SECRET_1024BIT = 128;
public const DEFAULT_PRIME = '217661744586174357731910088918027537819076683742555385111446432246898862353838409572109' .
'090130860564015713997172358072665816496064721484102914133641521973644771808873956554837381150726774022351017625' .
'219015698207402931495296204193332662620734710545483687360395197024862265062488610602569718029849535611214426801' .
'576680007614299882224570904138739739701719270939921147517651680636147611196154762334220964427831179712363716473' .
'338714143358957734746673089670508070055093204247996784170368679283167612722742303140675482911335824795830614395' .
'77559347101961771406173684378522703483495337037655006751328447510550299250924469288819';
public const DEFAULT_GENERATOR = '2';
public const DEFAULT_KEY = '5b9e8ef059c6b32ea59fc1d322d37f04aa30bae5aa9003b8321e21ddb04e300';
protected int $length = self::SECRET_256BIT;
protected bool $padEnabled = true;
protected string|\Closure $hasher = 'sha256';
public static function bigInteger(string|BigInteger $num, int $from = 10): BigInteger
{
if ($num instanceof BigInteger) {
return $num;
}
return BigInteger::fromBase($num, $from);
}
public static function createFromConfig(array $config): static
{
$handler = static::create(
$config['prime'] ?? null,
$config['generator'] ?? null,
$config['key'] ?? null,
);
if ($config['hasher'] ?? null) {
$handler->setHasher($config['hasher']);
}
if ($config['size'] ?? null) {
$handler->setSize($config['size']);
}
return $handler;
}
public static function create(
BigInteger|string|null $prime = null,
BigInteger|string|null $generator = null,
BigInteger|string|null $key = null
): static {
$prime ??= static::bigInteger(static::DEFAULT_PRIME);
$generator ??= static::bigInteger(static::DEFAULT_GENERATOR);
$key ??= static::bigInteger(static::DEFAULT_KEY, 16);
return new static(
static::bigInteger($prime, 16),
static::bigInteger($generator, 16),
static::bigInteger($key, 16),
);
}
public function __construct(
protected BigInteger $prime,
protected BigInteger $generator,
protected BigInteger $key
) {
//
}
/**
* Generate random [a] or [b]
*
* (a or b = random())
*
* @return BigInteger [a] or [b]
*
* @throws NumberFormatException
* @throws \Exception
*/
public function generateRandomSecret(): BigInteger
{
return BigInteger::fromBase(
bin2hex(random_bytes($this->getLength())),
16
);
}
public function getHasher(): string
{
return $this->hasher;
}
public function setHasher(string|\Closure $hasher): static
{
$this->hasher = $hasher;
return $this;
}
public function getLength(): int
{
return $this->length;
}
public function setLength(int $length): static
{
$this->length = $length;
return $this;
}
public function setSize(int $length): static
{
return $this->setLength((int) ($length / 8));
}
/**
* [u] = H(PAD(A) | PAD(B))
*
* @param BigInteger $A
* @param BigInteger $B
*
* @return BigInteger
*/
public function generateCommonSecret(BigInteger $A, BigInteger $B): BigInteger
{
static::checkNotEmpty($A, 'A');
static::checkNotEmpty($B, 'B');
$u = $this->hash($this->pad($A), $this->pad($B));
if ($u->isZero()) {
throw new InvalidSRPParameterException('The computed [u] value should not be 0.');
}
return $u;
}
/**
* [M1]
*
* (M1 = H(H(N) xor H(g), H(I), s, A, B, K))
*
* @param string $identity
* @param BigInteger $salt
* @param BigInteger $A
* @param BigInteger $B
* @param BigInteger $K
*
* @return BigInteger
*/
public function generateClientSessionProof(
string $identity,
BigInteger $salt,
BigInteger $A,
BigInteger $B,
BigInteger $K
): BigInteger {
return $this->hash(
$this->hash($this->getPrime())
->xor($this->hash($this->getGenerator())),
$this->hash($identity),
$salt, // s
$A,
$B,
$K
);
}
/**
* [M2]
*
* (H(A | M | K))
*
* @param BigInteger $A
* @param BigInteger $M
* @param BigInteger $K
*
* @return BigInteger
*/
public function generateServerSessionProof(
BigInteger $A,
BigInteger $M,
BigInteger $K
): BigInteger {
return $this->hash($A, $M, $K);
}
/**
* [N].
*
* @return BigInteger
*/
public function getPrime(): BigInteger
{
return $this->prime;
}
/**
* [g].
*
* @return BigInteger
*/
public function getGenerator(): BigInteger
{
return $this->generator;
}
/**
* [k].
*
* @return BigInteger
*/
public function getKey(): BigInteger
{
return $this->key;
}
public function hash(\Stringable|string ...$args): BigInteger
{
// All hashing string must be binary
$args = array_map(
static function ($arg) {
if ($arg instanceof BigInteger) {
return hex2bin(SRPUtils::bt2hex($arg));
}
return $arg;
},
$args
);
return static::bigInteger($this->hashToString(implode('', $args)), 16);
}
protected function hashToString(\Stringable|string $str): string
{
$hasher = $this->getHasher();
$str = (string) $str;
if ($hasher instanceof \Closure) {
return (string) $hasher($str);
}
$algo = strtolower($hasher);
return match ($algo) {
'sha1', 'sha256', 'sha384', 'sha512' => hash($algo, $str),
'blake2s-256' => $this->blake($str, 'blake2s', 256),
'blake2b-224' => $this->blake($str, 'blake2b', 224),
'blake2b-256' => $this->blake($str, 'blake2b', 256),
'blake2b-384' => $this->blake($str, 'blake2b', 384),
'blake2b-512' => $this->blake($str, 'blake2b', 512),
};
}
protected function blake(string $str, string $algo, int $size): string
{
return sodium_bin2hex(sodium_crypto_generichash($str, '', $size / 8));
}
protected static function checkNotEmpty(mixed $num, string $name): void
{
if (!$num) {
throw new \UnexpectedValueException("Value: `$name` should not be empty.");
}
if ($num instanceof BigNumber && $num->isZero()) {
throw new \UnexpectedValueException("Value: `$name` should not be zero.");
}
}
public static function intToBytes(BigInteger $val): string
{
$hexStr = $val->toBase(16);
$hexStr = strlen($hexStr) % 2 ? '0' . $hexStr : $hexStr;
return pack('H*', $hexStr);
}
protected function pad(BigInteger $val): BigInteger
{
if (!$this->padEnabled) {
return $val;
}
$length = strlen(static::intToBytes($this->getPrime()));
return BigInteger::of(str_pad((string) $val, $length, '0'));
}
public function isPadEnabled(): bool
{
return $this->padEnabled;
}
public function enablePad(bool $padString): static
{
$this->padEnabled = $padString;
return $this;
}
}