This repository was archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathPrivateKey.php
372 lines (299 loc) · 10 KB
/
PrivateKey.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* @license Copyright 2011-2014 BitPay Inc., MIT License
* see https://github.com/bitpay/php-bitpay-client/blob/master/LICENSE
*/
namespace Bitpay;
use Bitpay\Util\Secp256k1;
use Bitpay\Util\Util;
use Bitpay\Util\SecureRandom;
use Bitpay\Math\Math;
/**
* @package Bitcore
* @see https://en.bitcoin.it/wiki/List_of_address_prefixes
*/
class PrivateKey extends Key
{
/**
* @var PublicKey
*/
protected $publicKey;
/**
* @var string
*/
public $pemEncoded = '';
/**
* @var array
*/
public $pemDecoded = array();
/**
* @return string
*/
public function __toString()
{
return (string) $this->hex;
}
/*
* Use this method if you have a hex-encoded private key
* and you want to initialize your private key.
* If you have a private key, you can derive your public key
* and also your sin.
* @param string
*/
public function setHex($hex)
{
$this->hex = $hex;
$this->dec = Util::decodeHex($this->hex);
}
/**
* @return PublicKey
*/
public function getPublicKey()
{
if (null === $this->publicKey) {
$this->publicKey = new PublicKey();
$this->publicKey->setPrivateKey($this);
$this->publicKey->generate();
}
return $this->publicKey;
}
/**
* Generates an EC private key
*
* @return \Bitpay\PrivateKey
*/
public function generate()
{
if (!empty($this->hex)) {
return $this;
}
do {
$privateKey = \Bitpay\Util\SecureRandom::generateRandom(32);
$this->hex = strtolower(bin2hex($privateKey));
} while (Math::cmp('0x'.$this->hex, '1') <= 0 || Math::cmp('0x'.$this->hex, '0x'.Secp256k1::N) >= 0);
$this->dec = Util::decodeHex($this->hex);
return $this;
}
/**
* Checks to see if the private key value is not empty and
* the hex form only contains hexits and the decimal form
* only contains devimal digits.
*
* @return boolean
*/
public function isValid()
{
return ($this->hasValidDec() && $this->hasValidHex());
}
/**
* @return boolean
*/
public function hasValidHex()
{
return (!empty($this->hex) || ctype_xdigit($this->hex));
}
/**
* @return boolean
*/
public function hasValidDec()
{
return (!empty($this->dec) || ctype_digit($this->dec));
}
/**
* Creates an ECDSA signature of $message
*
* @return string
*/
public function sign($data)
{
if (!ctype_xdigit($this->hex)) {
throw new \Exception('The private key must be in hex format.');
}
if (empty($data)) {
throw new \Exception('You did not provide any data to sign.');
}
$e = Util::decodeHex(hash('sha256', $data));
do {
if (substr(strtolower($this->hex), 0, 2) != '0x') {
$d = '0x'.$this->hex;
} else {
$d = $this->hex;
}
$k = SecureRandom::generateRandom(32);
$k_hex = '0x'.strtolower(bin2hex($k));
$n_hex = '0x'.Secp256k1::N;
$Gx = '0x'.substr(Secp256k1::G, 2, 64);
$Gy = '0x'.substr(Secp256k1::G, 66, 64);
$P = new Point($Gx, $Gy);
// Calculate a new curve point from Q=k*G (x1,y1)
$R = Util::doubleAndAdd($k_hex, $P);
$Rx_hex = Util::encodeHex($R->getX());
$Rx_hex = str_pad($Rx_hex, 64, '0', STR_PAD_LEFT);
// r = x1 mod n
$r = Math::mod('0x'.$Rx_hex, $n_hex);
// s = k^-1 * (e+d*r) mod n
$edr = Math::add($e, Math::mul($d, $r));
$invk = Math::invertm($k_hex, $n_hex);
$kedr = Math::mul($invk, $edr);
$s = Math::mod($kedr, $n_hex);
// The signature is the pair (r,s)
$signature = array(
'r' => Util::encodeHex($r),
's' => Util::encodeHex($s),
);
$signature['r'] = str_pad($signature['r'], 64, '0', STR_PAD_LEFT);
$signature['s'] = str_pad($signature['s'], 64, '0', STR_PAD_LEFT);
} while (Math::cmp($r, '0') <= 0 || Math::cmp($s, '0') <= 0);
$sig = array(
'sig_rs' => $signature,
'sig_hex' => self::serializeSig($signature['r'], $signature['s']),
);
return $sig['sig_hex']['seq'];
}
/**
* ASN.1 DER encodes the signature based on the form:
* 0x30 + size(all) + 0x02 + size(r) + r + 0x02 + size(s) + s
* http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
*
* @param string
* @param string
* @return string
*/
public static function serializeSig($r, $s)
{
$dec = '';
$byte = '';
$seq = '';
$digits = array();
$retval = array();
for ($x = 0; $x < 256; $x++) {
$digits[$x] = chr($x);
}
$dec = Util::decodeHex($r);
while (Math::cmp($dec, '0') > 0) {
$dv = Math::div($dec, '256');
$rem = Math::mod($dec, '256');
$dec = $dv;
$byte = $byte.$digits[$rem];
}
$byte = strrev($byte);
// msb check
if (Math::cmp('0x'.bin2hex($byte[0]), '0'.'x80') >= 0) {
$byte = chr(0x00).$byte;
}
$retval['bin_r'] = bin2hex($byte);
$seq = chr(0x02).chr(strlen($byte)).$byte;
$dec = Util::decodeHex($s);
$byte = '';
while (Math::cmp($dec, '0') > 0) {
$dv = Math::div($dec, '256');
$rem = Math::mod($dec, '256');
$dec = $dv;
$byte = $byte.$digits[$rem];
}
$byte = strrev($byte);
// msb check
if (Math::cmp('0x'.bin2hex($byte[0]), '0'.'x80') >= 0) {
$byte = chr(0x00).$byte;
}
$retval['bin_s'] = bin2hex($byte);
$seq = $seq.chr(0x02).chr(strlen($byte)).$byte;
$seq = chr(0x30).chr(strlen($seq)).$seq;
$retval['seq'] = bin2hex($seq);
return $retval;
}
/**
* Decodes PEM data to retrieve the keypair.
*
* @param string $pem_data The data to decode.
* @return array The keypair info.
*/
public function pemDecode($pem_data)
{
$beg_ec_text = '-----BEGIN EC PRIVATE KEY-----';
$end_ec_text = '-----END EC PRIVATE KEY-----';
$decoded = '';
$ecpemstruct = array();
$pem_data = str_ireplace($beg_ec_text, '', $pem_data);
$pem_data = str_ireplace($end_ec_text, '', $pem_data);
$pem_data = str_ireplace("\r", '', trim($pem_data));
$pem_data = str_ireplace("\n", '', trim($pem_data));
$pem_data = str_ireplace(' ', '', trim($pem_data));
$decoded = bin2hex(base64_decode($pem_data));
if (strlen($decoded) < 230) {
throw new \Exception('Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data.');
}
$ecpemstruct = array(
'oct_sec_val' => substr($decoded, 14, 64),
'obj_id_val' => substr($decoded, 86, 10),
'bit_str_val' => substr($decoded, 106),
);
if ($ecpemstruct['obj_id_val'] != '2b8104000a') {
throw new \Exception('Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data.');
}
$private_key = $ecpemstruct['oct_sec_val'];
$public_key = $ecpemstruct['bit_str_val'];
if (strlen($private_key) < 64 || strlen($public_key) < 128) {
throw new \Exception('Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data.');
}
$this->pemDecoded = array('private_key' => $private_key, 'public_key' => $public_key);
return $this->pemDecoded;
}
/**
* Encodes keypair data to PEM format.
*
* @param array $keypair The keypair info.
* @return string The data to decode.
*/
public function pemEncode($keypair)
{
if (is_array($keypair) && (strlen($keypair[0]) < 64 || strlen($keypair[1]) < 128)) {
throw new \Exception('Invalid or corrupt secp256k1 keypair provided. Cannot decode the supplied PEM data.');
}
$dec = '';
$byte = '';
$beg_ec_text = '';
$end_ec_text = '';
$ecpemstruct = array();
$digits = array();
for ($x = 0; $x < 256; $x++) {
$digits[$x] = chr($x);
}
$ecpemstruct = array(
'sequence_beg' => '30',
'total_len' => '74',
'int_sec_beg' => '02',
'int_sec_len' => '01',
'int_sec_val' => '01',
'oct_sec_beg' => '04',
'oct_sec_len' => '20',
'oct_sec_val' => $keypair[0],
'a0_ele_beg' => 'a0',
'a0_ele_len' => '07',
'obj_id_beg' => '06',
'obj_id_len' => '05',
'obj_id_val' => '2b8104000a',
'a1_ele_beg' => 'a1',
'a1_ele_len' => '44',
'bit_str_beg' => '03',
'bit_str_len' => '42',
'bit_str_val' => '00'.$keypair[1],
);
$beg_ec_text = '-----BEGIN EC PRIVATE KEY-----';
$end_ec_text = '-----END EC PRIVATE KEY-----';
$dec = trim(implode($ecpemstruct));
if (strlen($dec) < 230) {
throw new \Exception('Invalid or corrupt secp256k1 keypair provided. Cannot encode the supplied data.');
}
$dec = Util::decodeHex('0x'.$dec);
while (Math::cmp($dec, '0') > 0) {
$dv = Math::div($dec, '256');
$rem = Math::mod($dec, '256');
$dec = $dv;
$byte = $byte.$digits[$rem];
}
$byte = $beg_ec_text."\r\n".chunk_split(base64_encode(strrev($byte)), 64).$end_ec_text;
$this->pemEncoded = $byte;
return $byte;
}
}