-
Notifications
You must be signed in to change notification settings - Fork 38
/
keygen.c
291 lines (245 loc) · 8.17 KB
/
keygen.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
/*
develop by Luis Alberto
Twitter: @albertobsd
email: alberto.bsd@gmail.com
Install dependencies:
apt install libssl-dev
apt install libgcrypt20-dev
apt install libgmp-dev
Compilation:
gcc -o keygen keygen.c gmpecc.o util.o sha256.o base58.o rmd160.o -lgmp -lcrypto `libgcrypt-config --cflags --libs`
Usage Examples:
./keygen -s openssl -b 250
./keygen -s urandom -b 256
./keygen -s random -b 64
./keygen -s getrandom
./keygen -s gcrypt -b 128
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <gmp.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <openssl/rand.h>
#include <sys/random.h>
#include <gcrypt.h>
#include "util.h"
#include "gmpecc.h"
#include "base58/libbase58.h"
#include "rmd160/rmd160.h"
#include "sha256/sha256.h"
struct Elliptic_Curve EC;
struct Point G;
struct Point DoublingG[256];
const char *EC_constant_N = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141";
const char *EC_constant_P = "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f";
const char *EC_constant_Gx = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798";
const char *EC_constant_Gy = "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8";
void generate_publickey_address_rmd160(struct Point *publickey,bool compress,char *dst_publickey,char *dst_address,char *dst_rmd160);
void generate_publickey_and_address(struct Point *publickey,bool compress,char *dst_publickey,char *dst_address);
const char *sources[5] = {"urandom","random","openssl","getrandom","gcrypt"};
gmp_randstate_t state;
/*
for some reason the GMP function mp_set_memory_functions needs a extra parameter in the function call of realloc and free warppers
*/
void *wrapper_gcry_alloc(size_t size);
void *wrapper_gcry_realloc(void *ptr, size_t old_size, size_t new_size);
void wrapper_gcry_free(void *ptr, size_t cur_size);
int main(int argc, char **argv) {
unsigned long err;
int FLAG_SOURCE = 0,INDEX_SOURCE = 0,FLAG_BITS = 0;
int bits = 256,maxbits;
int bytes = 32,index,i,rc;
char c,*buffer_key;
FILE *fd;
mpz_t key;
struct Point publickey;
char str_publickey[131];
char str_address[50];
char str_rmd[50];
char *hextemp,*aux,*public_address;
mp_set_memory_functions(wrapper_gcry_alloc,wrapper_gcry_realloc,wrapper_gcry_free); //Using secure memory storage from lib gcrypt
buffer_key = (char*)gcry_malloc_secure(32); //Secure buffer for the KEY
/* Init Constant Values in mpz numbers */
mpz_init_set_str(EC.p, EC_constant_P, 16);
mpz_init_set_str(EC.n, EC_constant_N, 16);
mpz_init_set_str(G.x , EC_constant_Gx, 16);
mpz_init_set_str(G.y , EC_constant_Gy, 16);
init_doublingG(&G);
/* Init empty values */
mpz_init(publickey.x);
mpz_init(publickey.y);
mpz_init(key);
gmp_randinit_mt(state);
gmp_randseed_ui(state, ((int)clock()) + ((int)time(NULL)) );
while ((c = getopt(argc, argv, "b:s:")) != -1) {
switch(c) {
case 's':
index = indexOf(optarg,sources,5);
FLAG_SOURCE = 1;
INDEX_SOURCE = index;
break;
case 'b':
bits = strtol(optarg,NULL,10);
if(bits >256 || bits < 1) {
bits = 256;
}
else {
bytes = bits / 8;
if(bits % 8 != 0) {
bytes++;
}
FLAG_BITS = 1;
}
break;
}
}
switch(INDEX_SOURCE) {
case -1:
fprintf(stderr,"Invalid option: -s %s\n",optarg);
exit(0);
break;
case 0:
fd = fopen("/dev/urandom","rb");
if(fd == NULL) {
fprintf(stderr,"Can't open /dev/urandom\n");
exit(0);
}
fread(buffer_key,1,bytes,fd);
fclose(fd);
mpz_import(key,bytes,1,1,0,0,buffer_key);
break;
case 1:
fd = fopen("/dev/random","rb");
if(fd == NULL) {
fprintf(stderr,"Can't open /dev/random\n");
exit(0);
}
fread(buffer_key,1,bytes,fd);
fclose(fd);
mpz_import(key,bytes,1,1,0,0,buffer_key);
break;
case 2:
rc = RAND_bytes(buffer_key, bytes);
if(rc != 1) {
fprintf(stderr,"OpenSSL error: %l\n",err);
exit(0);
}
mpz_import(key,bytes,1,1,0,0,buffer_key);
break;
case 3:
getrandom(buffer_key,bytes,GRND_NONBLOCK);
mpz_import(key,bytes,1,1,0,0,buffer_key);
break;
case 4:
gcry_randomize(buffer_key,bytes,GCRY_VERY_STRONG_RANDOM);
mpz_import(key,bytes,1,1,0,0,buffer_key);
break;
}
if(FLAG_BITS) {
maxbits = mpz_sizeinbase(key,2);
if(maxbits > bits) { // If the number of maxbis is great that the bit size requested we need to clear those upper bits
for(i = maxbits ; i > bits; i--) {
mpz_clrbit(key,i-1);
}
}
mpz_setbit(key,bits-1); // In any case we need to set the requested bit in 1 to fit the Key to the specific subrange.
}
Scalar_Multiplication(G,&publickey,key);
gmp_printf("KEY (Secret: DON'T SHARE THIS VALUE): %Zx\n",key);
//void generate_publickey_and_address(struct Point *publickey,bool compress,char *dst_publickey,char *dst_address)
generate_publickey_and_address(&publickey,false,str_publickey,str_address);
printf("publickey uncompressed: %s\n",str_publickey);
printf("address uncompressed %s\n",str_address);
generate_publickey_and_address(&publickey,true,str_publickey,str_address);
printf("publickey compressed: %s\n",str_publickey);
printf("address compressed %s\n",str_address);
//We overwrite the random buffer and the key mpz
for(i = 0; i <256;i++){
mpz_urandomb(key,state,256);
memset(buffer_key,i,32);
}
mpz_clear(key);
gcry_free(buffer_key);
return 0;
}
void generate_publickey_address_rmd160(struct Point *publickey,bool compress,char *dst_publickey,char *dst_address,char *dst_rmd160) {
char bin_publickey[65];
char bin_sha256[32];
char bin_digest[60];
size_t pubaddress_size = 50;
memset(dst_address,0,50);
memset(dst_publickey,0,131);
if(compress) {
if(mpz_tstbit(publickey->y, 0) == 0) { // Even
gmp_snprintf (dst_publickey,67,"02%0.64Zx",publickey->x);
}
else {
gmp_snprintf(dst_publickey,67,"03%0.64Zx",publickey->x);
}
hexs2bin(dst_publickey,bin_publickey);
sha256(bin_publickey, 33, bin_sha256);
}
else {
gmp_snprintf(dst_publickey,131,"04%0.64Zx%0.64Zx",publickey->x,publickey->y);
hexs2bin(dst_publickey,bin_publickey);
sha256(bin_publickey, 65, bin_sha256);
}
RMD160Data((const unsigned char*)bin_sha256,32, bin_digest+1);
tohex_dst(bin_digest+1,20,dst_rmd160);
/* Firts byte 0, this is for the Address begining with 1.... */
bin_digest[0] = 0;
/* Double sha256 checksum */
sha256(bin_digest, 21, bin_digest+21);
sha256(bin_digest+21, 32, bin_digest+21);
/* Get the address */
if(!b58enc(dst_address,&pubaddress_size,bin_digest,25)){
fprintf(stderr,"error b58enc\n");
}
}
void generate_publickey_and_address(struct Point *publickey,bool compress,char *dst_publickey,char *dst_address) {
char bin_publickey[65];
char bin_sha256[32];
char bin_digest[60];
size_t pubaddress_size = 50;
memset(dst_address,0,50);
memset(dst_publickey,0,131);
if(compress) {
if(mpz_tstbit(publickey->y, 0) == 0) { // Even
gmp_snprintf (dst_publickey,67,"02%0.64Zx",publickey->x);
}
else {
gmp_snprintf(dst_publickey,67,"03%0.64Zx",publickey->x);
}
hexs2bin(dst_publickey,bin_publickey);
sha256(bin_publickey, 33, bin_sha256);
}
else {
gmp_snprintf(dst_publickey,131,"04%0.64Zx%0.64Zx",publickey->x,publickey->y);
hexs2bin(dst_publickey,bin_publickey);
sha256(bin_publickey, 65, bin_sha256);
}
RMD160Data((const unsigned char*)bin_sha256,32, bin_digest+1);
/* Firts byte 0, this is for the Address begining with 1.... */
bin_digest[0] = 0;
/* Double sha256 checksum */
sha256(bin_digest, 21, bin_digest+21);
sha256(bin_digest+21, 32, bin_digest+21);
/* Get the address */
if(!b58enc(dst_address,&pubaddress_size,bin_digest,25)){
fprintf(stderr,"error b58enc\n");
}
}
void *wrapper_gcry_alloc(size_t size) { //To use calloc instead of malloc
return gcry_calloc(size,1);
}
void *wrapper_gcry_realloc(void *ptr, size_t old_size, size_t new_size) {
return gcry_realloc(ptr,new_size);
}
void wrapper_gcry_free(void *ptr, size_t cur_size) {
gcry_free(ptr);
}