forked from samr7/vanitygen
-
Notifications
You must be signed in to change notification settings - Fork 8
/
keyconv.c
153 lines (133 loc) · 3.43 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
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <string.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"
const char *version = VANITYGEN_VERSION;
static void usage(const char *progname) {
fprintf(stderr,
"Vanitygen keyconv %s\n"
"Usage: %s [-8] [-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 <key> Combine private key parts to make complete "
"private key\n"
"-v Verbose output\n",
version, progname);
}
int main(int argc, char **argv) {
if (argc == 1) {
usage(argv[0]);
return 0;
}
char ecprot[128];
char pbuf[1024];
const char *key_in;
const char *pass_in = NULL;
const char *key2_in = NULL;
EC_KEY *pkey;
int privtype;
int pkcs8 = 0;
int verbose = 0;
int generate = 0;
int opt;
int res;
while ((opt = getopt(argc, argv, "8c:vG")) != -1) {
switch (opt) {
case '8': pkcs8 = 1; break;
case 'c': key2_in = optarg; break;
case 'v': verbose = 1; break;
case 'G': generate = 1; break;
default: usage(argv[0]); return 1;
}
}
OpenSSL_add_all_algorithms();
pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
if (generate) {
unsigned char *pend = (unsigned char *) pbuf;
privtype = 128;
EC_KEY_generate_key(pkey);
res = i2o_ECPublicKey(pkey, &pend);
fprintf(stderr, "Pubkey (hex): ");
dumphex((unsigned char *) pbuf, res);
fprintf(stderr, "Privkey (hex): ");
dumpbn(EC_KEY_get0_private_key(pkey));
vg_encode_address(EC_KEY_get0_public_key(pkey),
EC_KEY_get0_group(pkey), 0, ecprot);
printf("Address: %s\n", ecprot);
vg_encode_privkey(pkey, privtype, ecprot);
printf("Privkey: %s\n", ecprot);
return 0;
}
if (optind >= argc) {
res = fread(pbuf, 1, sizeof(pbuf) - 1, stdin);
pbuf[res] = '\0';
key_in = pbuf;
} else {
key_in = argv[optind];
}
res = vg_decode_privkey_any(pkey, &privtype, key_in, NULL);
if (!res) {
fprintf(stderr, "ERROR: 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) {
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 (verbose) {
unsigned char *pend = (unsigned char *) pbuf;
res = i2o_ECPublicKey(pkey, &pend);
fprintf(stderr, "Pubkey (hex): ");
dumphex((unsigned char *) pbuf, res);
fprintf(stderr, "Privkey (hex): ");
dumpbn(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 {
vg_encode_address(EC_KEY_get0_public_key(pkey),
EC_KEY_get0_group(pkey), 0, ecprot);
printf("Address: %s\n", ecprot);
vg_encode_privkey(pkey, privtype, ecprot);
printf("Privkey: %s\n", ecprot);
}
EC_KEY_free(pkey);
return 0;
}