-
Notifications
You must be signed in to change notification settings - Fork 48
/
keyconv.c
432 lines (393 loc) · 11.8 KB
/
keyconv.c
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <openssl/evp.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/obj_mac.h>
#if !defined(_WIN32)
#include <unistd.h>
#else
#include "winglue.h"
#endif
#include "pattern.h"
#include "util.h"
#include "json.h"
#include "segwit_addr.h"
const char *version = VANITYGEN_VERSION;
static void
usage(const char *progname)
{
fprintf(stderr,
"Vanitygen keyconv %s\n"
"Usage: %s [-8] [-e|-E <password>] [-c <key>] [<key>]\n"
"-G Generate a key pair and output the full public key\n"
"-8 Output key in PKCS#8 form\n"
"-C Generate Compressed key\n"
"-e Encrypt output key, prompt for password\n"
"-X <version> Generate address with the given version\n"
"-p <privtyp> The priv-type belonging to the version, default <version>+128\n"
" default is used when ommitted, can only be used combined with -X\n"
"-E <password> Encrypt output key with <password> (UNSAFE)\n"
"-c <key> Combine private key parts to make complete private key\n"
"-M <sig> create a multi-sign P2SH address requiring <sig> signatures\n"
"-j <JSON> JSON array containing the additional keys for the multi-signing\n"
"-v Verbose output\n",
version, progname);
}
int
main(int argc, char **argv)
{
char pwbuf[128];
char ecprot[128];
char pbuf[1024];
char cbuf[1024];
char p2shbuf[1024];
const char *key_in;
const char *pass_in = NULL;
const char *key2_in = NULL;
EC_KEY *pkey;
int parameter_group = -1;
int compressedkey = 0;
int privtype, addrtype, newprivtype=0;
int pkcs8 = 0;
int pass_prompt = 0;
int verbose = 0;
int generate = 0;
int multisign = 0;
int versionoverride=0, privtypeoverride=0;
int opt;
int res;
// int len;
int nrkeys=0;
int multisignhashlen=1; // keep a place open for the nr signatures
int hashlen=0;
json_value *JSon_input=NULL;
while ((opt = getopt(argc, argv, "8E:ec:vGX:p:CM:j:")) != -1) {
switch (opt) {
case 'C':
compressedkey = 1;
break;
case '8':
pkcs8 = 1;
break;
case 'E':
if (pass_prompt) {
usage(argv[0]);
return 1;
}
pass_in = optarg;
if (!vg_check_password_complexity(pass_in, 1))
fprintf(stderr,
"WARNING: Using weak password\n");
break;
case 'e':
if (pass_in) {
usage(argv[0]);
return 1;
}
pass_prompt = 1;
break;
case 'c':
key2_in = optarg;
break;
case 'M':
multisign = atoi(optarg);
break;
case 'j':
JSon_input = json_parse(optarg, strlen(optarg));
if (!JSon_input){
fprintf(stderr,
"WARNING: Invalid Json input\n");
}
break;
case 'v':
verbose = 1;
break;
case 'G':
generate = 1;
break;
case 'X':
addrtype = atoi(optarg);
privtype = 128 + addrtype;
versionoverride = 1;
break;
case 'p':
newprivtype = atoi(optarg);
privtypeoverride = 1;
break;
default:
usage(argv[0]);
return 1;
}
}
if (JSon_input){
unsigned char *p2sh = (unsigned char *) p2shbuf;
fprintf(stderr, "-------------------Multi sign----------------------------\n");
if (JSon_input){
EC_KEY *pkeyx = EC_KEY_new_by_curve_name(NID_secp256k1);
EC_POINT *pubkey_base = NULL;
for (nrkeys=0; nrkeys< JSon_input->u.array.length; nrkeys++){
pubkey_base = EC_POINT_hex2point(EC_KEY_get0_group(pkeyx),
json_string_value(JSon_input->u.array.values[nrkeys]),
NULL, NULL);
if (pubkey_base){
res = strlen(json_string_value(JSon_input->u.array.values[nrkeys]));
if (res==33*2){
res = EC_POINT_point2oct(EC_KEY_get0_group(pkeyx), pubkey_base,
POINT_CONVERSION_COMPRESSED,
p2sh+multisignhashlen+1, 33, NULL);
}else{
res = EC_POINT_point2oct(EC_KEY_get0_group(pkeyx), pubkey_base,
POINT_CONVERSION_UNCOMPRESSED,
p2sh+multisignhashlen+1, 65, NULL);
}
p2sh[multisignhashlen++] = res;
multisignhashlen = multisignhashlen + res;
}else{
fprintf(stderr, "#%d pub key is invalid\n\t", nrkeys);
multisignhashlen = 0;
nrkeys = JSon_input->u.array.length; //exit for
}
}
EC_POINT_free(pubkey_base);
EC_KEY_free(pkeyx);
}
json_value_free(JSon_input);
if (!multisignhashlen)
return 1;
}
OpenSSL_add_all_algorithms();
if (versionoverride==0){
addrtype = 0;
privtype = 128;
}
pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
if (!(versionoverride)){
switch (privtype) {
case 128: addrtype = 0; break;
case 239: addrtype = 111; break;
default: addrtype = 0; break;
}
}
if (privtypeoverride && versionoverride){
privtype = newprivtype;
}
if (generate) {
unsigned char *pend = (unsigned char *) pbuf;
unsigned char *cpub = (unsigned char *) cbuf;
EC_KEY_generate_key(pkey);
res = i2o_ECPublicKey(pkey, &pend);
fprintf(stderr, "---------------------------------------------------------\n");
fprintf(stderr, "Results:\n");
fprintf(stderr, "Pubkey (hex):\n\t");
fdumphex(stderr,(unsigned char *)pbuf, res);
fprintf(stderr, "Privkey (hex):\n\t");
fdumpbn(stderr, EC_KEY_get0_private_key(pkey));
vg_encode_address(EC_KEY_get0_public_key(pkey),
EC_KEY_get0_group(pkey),
addrtype, ecprot);
fprintf(stderr,"Address: %s\n", ecprot);
vg_encode_privkey(pkey, privtype, ecprot);
fprintf(stderr,"WiF key: %s\n", ecprot);
if (compressedkey == 1){
fprintf(stderr,"Compressed Results:\n");
res = EC_POINT_point2oct(EC_KEY_get0_group(pkey), EC_KEY_get0_public_key(pkey),
POINT_CONVERSION_COMPRESSED,
cpub, 33, NULL);
fprintf(stderr,"Pubkey Compressed (hex):\n\t");
fdumphex(stderr,(unsigned char *)cpub, res);
vg_encode_address_compressed(EC_KEY_get0_public_key(pkey),
EC_KEY_get0_group(pkey),
addrtype, ecprot);
fprintf(stderr,"Address Compressed:\n\t%s\n", ecprot);
vg_encode_privkey_compressed(pkey, privtype, ecprot);
fprintf(stderr,"Wif key Compressed:\n\t%s\n", ecprot);
fprintf(stderr, "---------------------------------------------------------\n");
}
return 0;
}
if (optind >= argc) {
// res = fread(pbuf, 1, sizeof(pbuf) - 1, stdin);
// pbuf[res] = '\0';
// key_in = pbuf;
key_in = NULL;
EC_KEY_free(pkey);
pkey = NULL;
} else {
key_in = argv[optind];
}
if (key_in){
res = vg_decode_privkey_any(pkey, &privtype, key_in, NULL);
if (res < 0) {
if (EVP_read_pw_string(pwbuf, sizeof(pwbuf),
"Enter import password:", 0) ||
!vg_decode_privkey_any(pkey, &privtype, key_in, pwbuf))
return 1;
}
if (!res) {
fprintf(stderr, "\nERROR: Unrecognized key format\n");
return 1;
}
}
if (key2_in) {
BN_CTX *bnctx;
BIGNUM *bntmp, *bntmp2;
EC_KEY *pkey2;
pkey2 = EC_KEY_new_by_curve_name(NID_secp256k1);
res = vg_decode_privkey_any(pkey2, &privtype, key2_in, NULL);
if (res < 0) {
if (EVP_read_pw_string(pwbuf, sizeof(pwbuf),
"Enter import password:", 0) ||
!vg_decode_privkey_any(pkey2, &privtype,
key2_in, pwbuf))
return 1;
}
if (!res) {
fprintf(stderr, "ERROR: Unrecognized key format\n");
return 1;
}
bntmp = BN_new();
bntmp2 = BN_new();
bnctx = BN_CTX_new();
EC_GROUP_get_order(EC_KEY_get0_group(pkey), bntmp2, NULL);
BN_mod_add(bntmp,
EC_KEY_get0_private_key(pkey),
EC_KEY_get0_private_key(pkey2),
bntmp2,
bnctx);
vg_set_privkey(bntmp, pkey);
EC_KEY_free(pkey2);
BN_clear_free(bntmp);
BN_clear_free(bntmp2);
BN_CTX_free(bnctx);
}
if (multisign){
unsigned char *p2sh = (unsigned char *) p2shbuf;
unsigned char *cpub = (unsigned char *) cbuf;
if (pkey) {
nrkeys = nrkeys + 1;
}
if (!((multisign>0) && (multisign<=nrkeys))){
fprintf(stderr, "\nInvalid: nr signatures %d and/or nr keys %d\n", multisign, nrkeys+1);
return 1;
}
if (!pkey) {
p2sh[0] = 0x50+multisign;
hashlen = multisignhashlen;
p2sh[hashlen++] = 0x50 + nrkeys;
p2sh[hashlen++] = 0xae; // OP_CHECKMULTISIG
fprintf(stderr, "\nP2SH (hex):\n\t");
fdumphex(stderr, p2sh, hashlen);
vg_encode_p2sh(p2sh,hashlen,addrtype,ecprot);
fprintf(stderr, "\nP2SH address:\n\t%s\n", ecprot);
fprintf(stderr, "\n---------------------------------------------------------\n");
return 1;
}else{
fprintf(stderr, "\n------------Adding Private key to P2SH script------------\n");
if (compressedkey){
res = EC_POINT_point2oct(EC_KEY_get0_group(pkey), EC_KEY_get0_public_key(pkey),
POINT_CONVERSION_COMPRESSED,
cpub, 33, NULL);
fprintf(stderr, "Pubkey Compressed (hex):\n\t");
fdumphex(stderr,(unsigned char *)cpub, res);
}else{
res = EC_POINT_point2oct(EC_KEY_get0_group(pkey), EC_KEY_get0_public_key(pkey),
POINT_CONVERSION_UNCOMPRESSED,
cpub, 65, NULL);
fprintf(stderr, "Pubkey UnCompressed (hex):\n\t");
fdumphex(stderr,(unsigned char *)cpub, res);
}
p2sh[0] = 0x50+multisign;
hashlen = multisignhashlen;
p2sh[hashlen++] = res;
memcpy(p2sh+hashlen,cbuf,res);
hashlen = hashlen + res;
p2sh[hashlen++] = 0x50 + nrkeys;
p2sh[hashlen++] = 0xae; // OP_CHECKMULTISIG
fprintf(stderr, "\nP2SH (hex):\n\t");
fdumphex(stderr, p2sh, hashlen);
vg_encode_p2sh(p2sh,hashlen,addrtype,ecprot);
fprintf(stderr, "\nP2SH address:\n\t%s\n", ecprot);
fprintf(stderr, "\n---------------------------------------------------------\n");
}
}
if (pass_prompt) {
res = EVP_read_pw_string(pwbuf, sizeof(pwbuf),
"Enter password:", 1);
if (res)
return 1;
pass_in = pwbuf;
if (!vg_check_password_complexity(pwbuf, 1))
fprintf(stderr, "WARNING: Using weak password\n");
}
if (verbose) {
res = vg_decode_privkey_any(pkey, &privtype, key_in, NULL);
unsigned char *pend = (unsigned char *) pbuf;
res = i2o_ECPublicKey(pkey, &pend);
fprintf(stderr, "Pubkey (hex): ");
fdumphex(stderr,(unsigned char *)pbuf, res);
fprintf(stderr, "Privkey (hex): ");
fdumpbn(stderr,EC_KEY_get0_private_key(pkey));
}
if (pkcs8) {
res = vg_pkcs8_encode_privkey(pbuf, sizeof(pbuf),
pkey, pass_in);
if (!res) {
fprintf(stderr,
"ERROR: Could not encode private key\n");
return 1;
}
printf("%s", pbuf);
}
else if (pass_in) {
res = vg_protect_encode_privkey(ecprot, pkey, privtype,
parameter_group, pass_in);
if (!res) {
fprintf(stderr, "ERROR: could not password-protect "
"private key\n");
return 1;
}
vg_encode_address(EC_KEY_get0_public_key(pkey),
EC_KEY_get0_group(pkey),
addrtype, pwbuf);
printf("Address: %s\n", pwbuf);
printf("Protkey: %s\n", ecprot);
}
if (pkey){
unsigned char *cpub = (unsigned char *) cbuf;
unsigned char *pend = (unsigned char *) pbuf;
res = i2o_ECPublicKey(pkey, &pend);
fprintf(stderr, "\n---------------------------------------------------------\n");
fprintf(stderr, "Results:\n");
fprintf(stderr, "Pubkey (hex):\n\t");
fdumphex(stderr,(unsigned char *)pbuf, res);
fprintf(stderr, "\nPrivkey (hex):\n\t");
fdumpbn(stderr, EC_KEY_get0_private_key(pkey));
vg_encode_address(EC_KEY_get0_public_key(pkey),
EC_KEY_get0_group(pkey),
addrtype, ecprot);
fprintf(stderr, "\nAddress: %s\n", ecprot);
vg_encode_privkey(pkey, privtype, ecprot);
fprintf(stderr, "Wif key: %s\n", ecprot);
if (compressedkey == 1){
fprintf(stderr, "Compressed Results:\n");
res = EC_POINT_point2oct(EC_KEY_get0_group(pkey), EC_KEY_get0_public_key(pkey),
POINT_CONVERSION_COMPRESSED,
cpub, 33, NULL);
fprintf(stderr, "Pubkey Compressed (hex):\n\t");
fdumphex(stderr,(unsigned char *)cpub, res);
vg_encode_address_compressed(EC_KEY_get0_public_key(pkey),
EC_KEY_get0_group(pkey),
addrtype, ecprot);
fprintf(stderr,"\nAddress Compressed:\n\t%s\n", ecprot);
vg_encode_privkey_compressed(pkey, privtype, ecprot);
fprintf(stderr, "Wif key Compressed:\n\t%s\n", ecprot);
fprintf(stderr, "---------------------------------------------------------\n");
}
}
OPENSSL_cleanse(pwbuf, sizeof(pwbuf));
EC_KEY_free(pkey);
return 0;
}