|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2020 UMI |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
14
|
|
|
* copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
22
|
|
|
* SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
declare(strict_types=1); |
|
26
|
|
|
|
|
27
|
|
|
namespace UmiTop\UmiCore\Util\Ed25519; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class AbstractBase |
|
31
|
|
|
* Implementation derived from TweetNaCl version 20140427. |
|
32
|
|
|
* @see http://tweetnacl.cr.yp.to/ |
|
33
|
|
|
* @package UmiTop\UmiCore\Util\Ed25519 |
|
34
|
|
|
* @SuppressWarnings(PHPMD.ShortMethodName) |
|
35
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
|
36
|
|
|
*/ |
|
37
|
|
|
abstract class AbstractBase |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var int */ |
|
40
|
|
|
public const PUBLIC_KEY_BYTES = 32; |
|
41
|
|
|
|
|
42
|
|
|
/** @var int */ |
|
43
|
|
|
public const SECRET_KEY_BYTES = 64; |
|
44
|
|
|
|
|
45
|
|
|
/** @var int */ |
|
46
|
|
|
public const SEED_BYTES = 32; |
|
47
|
|
|
|
|
48
|
|
|
/** @var array<int, int> */ |
|
49
|
|
|
protected $D2; |
|
50
|
|
|
|
|
51
|
|
|
/** @var array<int, int> */ |
|
52
|
|
|
protected $D; |
|
53
|
|
|
|
|
54
|
|
|
/** @var array<int, int> */ |
|
55
|
|
|
protected $gf0; |
|
56
|
|
|
|
|
57
|
|
|
/** @var array<int, int> */ |
|
58
|
|
|
protected $gf1; |
|
59
|
|
|
|
|
60
|
|
|
/** @var array<int, int> */ |
|
61
|
|
|
protected $I; |
|
62
|
|
|
|
|
63
|
|
|
/** @var array<int, int> */ |
|
64
|
|
|
protected $L; |
|
65
|
|
|
|
|
66
|
|
|
/** @var array<int, int> */ |
|
67
|
|
|
protected $X; |
|
68
|
|
|
|
|
69
|
|
|
/** @var array<int, int> */ |
|
70
|
|
|
protected $Y; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Ed25519 constructor. |
|
74
|
|
|
*/ |
|
75
|
12 |
|
public function __construct() |
|
76
|
|
|
{ |
|
77
|
12 |
|
$this->D2 = [ |
|
78
|
|
|
0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, |
|
79
|
|
|
0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406 |
|
80
|
|
|
]; |
|
81
|
12 |
|
$this->D = [ |
|
82
|
|
|
0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, |
|
83
|
|
|
0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203 |
|
84
|
|
|
]; |
|
85
|
12 |
|
$this->gf0 = array_fill(0, 16, 0); |
|
86
|
12 |
|
$this->gf1 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; |
|
87
|
12 |
|
$this->I = [ |
|
88
|
|
|
0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, |
|
89
|
|
|
0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83 |
|
90
|
|
|
]; |
|
91
|
12 |
|
$this->L = [ |
|
92
|
|
|
0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, |
|
93
|
|
|
0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10 |
|
94
|
|
|
]; |
|
95
|
12 |
|
$this->X = [ |
|
96
|
|
|
0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, |
|
97
|
|
|
0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169 |
|
98
|
|
|
]; |
|
99
|
12 |
|
$this->Y = [ |
|
100
|
|
|
0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, |
|
101
|
|
|
0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666 |
|
102
|
|
|
]; |
|
103
|
12 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param array<int, int> $o |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
12 |
|
protected function car25519(array &$o): void |
|
110
|
|
|
{ |
|
111
|
12 |
|
for ($i = 0; $i < 16; $i++) { |
|
112
|
12 |
|
$o[$i] += (1 << 16); |
|
113
|
12 |
|
$c = $o[$i] >> 16; |
|
114
|
12 |
|
$o[($i + 1) * (int)($i < 15)] += $c - 1 + 37 * ($c - 1) * (int)($i === 15); |
|
115
|
12 |
|
$o[$i] -= $c << 16; |
|
116
|
|
|
} |
|
117
|
12 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $x |
|
121
|
|
|
* @param string $y |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
6 |
|
protected function cryptoVerify32(string $x, string $y): bool |
|
125
|
|
|
{ |
|
126
|
6 |
|
$d = 0; |
|
127
|
6 |
|
for ($i = 0; $i < 32; $i++) { |
|
128
|
6 |
|
$d |= ord($x[$i]) ^ ord($y[$i]); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
6 |
|
return (1 & (($d - 1) >> 8)) === 1; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param array<int, int> $o |
|
136
|
|
|
* @param array<int, int> $a |
|
137
|
|
|
* @param array<int, int> $b |
|
138
|
|
|
* @return void |
|
139
|
|
|
*/ |
|
140
|
12 |
|
protected function fnA(array &$o, array $a, array $b): void |
|
141
|
|
|
{ |
|
142
|
12 |
|
for ($i = 0; $i < 16; $i++) { |
|
143
|
12 |
|
$o[$i] = $a[$i] + $b[$i]; |
|
144
|
|
|
} |
|
145
|
12 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param array<int, int> $o |
|
149
|
|
|
* @param array<int, int> $a |
|
150
|
|
|
* @param array<int, int> $b |
|
151
|
|
|
* @return void |
|
152
|
|
|
*/ |
|
153
|
12 |
|
protected function fnM(array &$o, array $a, array $b): void |
|
154
|
|
|
{ |
|
155
|
12 |
|
$t = array_fill(0, 31, 0); |
|
156
|
|
|
|
|
157
|
12 |
|
for ($i = 0; $i < 16; $i++) { |
|
158
|
12 |
|
for ($j = 0; $j < 16; $j++) { |
|
159
|
12 |
|
$t[$i + $j] += $a[$i] * $b[$j]; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
12 |
|
for ($i = 0; $i < 15; $i++) { |
|
163
|
12 |
|
$t[$i] += 38 * $t[$i + 16]; |
|
164
|
|
|
} |
|
165
|
12 |
|
for ($i = 0; $i < 16; $i++) { |
|
166
|
12 |
|
$o[$i] = $t[$i]; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
12 |
|
$this->car25519($o); |
|
170
|
12 |
|
$this->car25519($o); |
|
171
|
12 |
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param array<int, int> $o |
|
175
|
|
|
* @param array<int, int> $a |
|
176
|
|
|
* @param array<int, int> $b |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
12 |
|
protected function fnZ(array &$o, array $a, array $b): void |
|
180
|
|
|
{ |
|
181
|
12 |
|
for ($i = 0; $i < 16; $i++) { |
|
182
|
12 |
|
$o[$i] = $a[$i] - $b[$i]; |
|
183
|
|
|
} |
|
184
|
12 |
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|