-
Notifications
You must be signed in to change notification settings - Fork 5
/
genkey.c
283 lines (261 loc) · 7.33 KB
/
genkey.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
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <openssl/ecdsa.h>
#include <openssl/obj_mac.h>
#include <openssl/ripemd.h>
#include <pthread.h>
// bitcoin
int pubKeyVersion = 0;
int privKeyVersion = 128;
BIGNUM* B58;
EC_GROUP* group;
#define SCRATCH_SIZE 1024 * 100
static const char* Base58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
char* base58check ( const uint8_t* data, int len, // what I need here
// allocated earlier for efficency
BN_CTX* ctx,
BIGNUM* num, BIGNUM* dv, BIGNUM* rem, uint8_t* out )
{
// append 4 byte check
memcpy ( out, data, len );
uint8_t sout[32];
SHA256 ( data, len, sout );
SHA256 ( sout, 32, sout );
memcpy ( out + len, sout, 4 );
// convert to base58
if ( BN_bin2bn ( out, len + 4, num ) == NULL ) {
ERR_print_errors ( stderr );
exit(5);
}
if ( len + 3 > BN_num_bytes ( num ) ) { // allow for 1 leading zero squash (public address)
// printf ( "error: possible leading zero sqashing\n" );
// exit ( 3 );
}
char* p = out + SCRATCH_SIZE - 1;
*p = '\0';
p--;
while ( !BN_is_zero(num) ) {
if ( BN_div ( dv, rem, num, B58, ctx ) == 0 ) {
unsigned long en = ERR_get_error ();
printf ( "%s\n", ERR_error_string ( en, NULL ) );
exit(1);
}
BN_copy ( num, dv );
uint8_t n;
BN_bn2bin ( rem, &n );
*p = Base58[n];
p--;
n = 0;
}
uint8_t* q = data;
while ( *q == 0 ) { // add leading zeros back in
*p = Base58[0];
p--;
q++;
}
p++;
return p; // p points int a scratch buffer allocated by thread
}
uint8_t* PublicAddress ( uint8_t* scratch, BN_CTX* ctx, BIGNUM* num, BIGNUM* dv, BIGNUM* rem ) {
uint8_t a[65];
a[0] = 4;
memcpy ( a + 1, scratch, 64 );
uint8_t sout[32];
SHA256 ( a, 65, sout );
uint8_t rout[21];
rout[0] = pubKeyVersion;
RIPEMD160 ( sout, 32, rout + 1 );
return base58check ( rout, sizeof rout, ctx, num, dv, rem, scratch );
}
uint8_t* calcECPubkey ( const EC_GROUP* group, BIGNUM* x, BIGNUM* y, const BIGNUM* privkey, uint8_t* scratch ) {
EC_POINT* pubpoint = EC_POINT_new(group);
if ( EC_POINT_mul(group, pubpoint, privkey, NULL, NULL, NULL) == 0 ) {
printf ( "error %d\n", __LINE__ );
exit(1);
}
int r = EC_POINT_get_affine_coordinates_GFp(group, pubpoint, x, y, NULL);
if ( r == 0 ) {
printf ( "error %d\n", __LINE__ );
exit(1);
}
memset ( scratch, 0, 64 );
if ( !BN_bn2bin ( x, scratch + 32 - BN_num_bytes(x) ) ) {
printf ( "error: converting x\n" );
exit ( 2 );
}
if ( !BN_bn2bin ( y, scratch + 32 + 32 - BN_num_bytes(y) ) ) {
printf ( "error: converting x\n" );
exit ( 2 );
}
return scratch;
}
uint8_t* WIFPrivateKey ( BIGNUM* privkey, BN_CTX* ctx, BIGNUM* num, BIGNUM* dv, BIGNUM* rem, uint8_t* scratch ) {
uint8_t pk[33];
BN_bn2bin ( privkey, pk + 1 );
pk[0] = privKeyVersion;
return base58check ( pk, sizeof pk, ctx, num, dv, rem, scratch );
}
struct threadParams {
char* str;
int anywhere;
int offset;
uint8_t randStart[32];
uint64_t cnt;
int go;
};
void* run (void* param) {
struct threadParams* tp = (struct threadParams*)param;
// allocations per thread, passed to subroutines for efficency
BIGNUM* x = BN_new();
BIGNUM* y = BN_new();
BN_CTX* ctx = BN_CTX_new();
BIGNUM* num = BN_new();
BIGNUM* dv = BN_new();
BIGNUM* rem = BN_new();
uint8_t scratch[SCRATCH_SIZE];
BIGNUM* privkey = BN_new();
uint8_t* privdata = tp->randStart;
uint64_t* p = privdata + tp->offset;
if ( *p == 0 ) {
*p = 1;
}
char* str = tp->str;
tp->cnt = 0;
if ( str == NULL ) { // non vanity
BN_bin2bn ( privdata, 32, privkey );
char* pub = PublicAddress ( calcECPubkey ( group, x, y, privkey, scratch ), ctx, num, dv, rem );
char* addr = strdup ( pub ); // pub is in scratch, which is about to be reused
char* k = WIFPrivateKey ( privkey, ctx, num, dv, rem, scratch );
BN_print_fp ( stdout, privkey ); printf ( "\n" );
printf ( "%s\n%s\n", k, addr );
return 0;
}
while ( tp->go && *p ) {
BN_bin2bn ( privdata, 32, privkey );
char* pub = PublicAddress ( calcECPubkey ( group, x, y, privkey, scratch ), ctx, num, dv, rem );
if ( strstr ( pub + 1, str ) != NULL ) {
if ( tp->anywhere == 0 ) { // must be at the begining
if ( strstr ( pub + 1, str ) != pub + 1 ) {
continue;
}
}
char* addr = strdup ( pub ); // pub is in scratch, which is about to be reused
char* k = WIFPrivateKey ( privkey, ctx, num, dv, rem, scratch );
BN_print_fp ( stdout, privkey ); printf ( "\n" );
printf ( "%s\n%s\n", k, addr );
return 0;
}
(*p)++;
tp->cnt++;
}
}
void usage () {
printf ( "-s string: generate a public address with the given string\n"
"-t number: number of threads to use\n"
"-a: allow the string to be anywhere in the public address, not just at the begining\n"
"-c: the string should be case sensitive\n"
"-l: generate a litecoin key/address pair\n"
"-v verbose\n"
"examples:\n"
"\tgenerate a private key and its public address\n\t\t ./genkey\n"
"\tgenerate a private key where the public address contains the string \'BiT\'\n"
"\t\t./genkey -s BiT -c -t 8\n"
);
exit(0);
}
void checkString58 ( char* in ) {
char* p = in;
char* invalid = strdup ( in );
char* q = invalid;
int invalidFound = 0;
while ( *p ) {
if ( strchr ( Base58, *p ) != NULL ) {
*q = '.';
} else {
invalidFound++;
}
p++; q++;
}
if ( invalidFound ) {
printf ( "Error: %d invalid base85 characters found: %s\n", invalidFound, invalid );
exit ( 1 );
}
}
int main( int argc, char* argv[] ) {
int numthreads = 1;
char* str = NULL;
int anywhere = 0;
int casesensitive = 0;
int verbose = 0;
int ch;
while ((ch = getopt(argc, argv, "t:acs:vlh")) != -1) {
switch (ch) {
case 't':
numthreads = atoi ( optarg );
break;
case 'a':
anywhere = 1;
break;
case 'c':
casesensitive = 1;
break;
case 's':
str = strdup ( optarg );
checkString58 ( str );
break;
case 'v':
verbose = 1;
break;
case 'h':
usage();
break;
case 'l':
pubKeyVersion = 48;
privKeyVersion = 176;
break;
}
}
if ( numthreads == 0 ) {
numthreads = 1;
}
uint8_t rand[32];
int fd = open ( "/dev/urandom", O_RDONLY );
read ( fd, rand, sizeof rand );
close ( fd );
// some global stuff
group = EC_GROUP_new_by_curve_name ( NID_secp256k1 );
if ( group == NULL ) {
printf ( "error %d\n", __LINE__ );
exit(1);
}
B58 = BN_new();
BN_dec2bn ( &B58, "58" );
pthread_t t;
int i;
struct threadParams** tplist = malloc ( sizeof ( struct threadParams* ) * numthreads );
for ( i = 0; i < numthreads; i++ ) {
tplist[i] = malloc ( sizeof ( struct threadParams ) );
tplist[i]->str = str;
tplist[i]->anywhere = anywhere;
tplist[i]->offset = 1 + i;
tplist[i]->cnt = 0;
tplist[i]->go = 1;
memcpy ( tplist[i]->randStart, rand, sizeof rand );
pthread_create ( &t, NULL, run, (void*) tplist[i] );
}
// kinda getto here, if the last thread finishes just bail
int status;
pthread_join ( t, &status );
uint64_t cnt = 0;
for ( i = 0; i < numthreads; i++ ) {
cnt += tplist[i]->cnt;
tplist[i]->go = 0;
}
if ( verbose ) {
printf ( "tried %u\n", cnt );
}
return 0;
}