-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeygen.cpp
More file actions
340 lines (299 loc) · 10.4 KB
/
Copy pathkeygen.cpp
File metadata and controls
340 lines (299 loc) · 10.4 KB
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
/*
WASTE - keygen.cpp (Keypair generation UI)
Copyright (C) 2003 Nullsoft, Inc.
WASTE is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
WASTE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WASTE; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <windows.h>
#include <time.h>
#include <stdio.h>
#include "resource.h"
#include "main.h"
extern "C" {
#include "rsa/r_random.h"
};
static char *kg_privout;
static char kg_passbf[SHA_OUTSIZE];
static int kg_keysize;
static R_RANDOM_STRUCT kg_random;
static int kg_going;
static time_t kg_start_time;
static unsigned int kg_movebuf[7];
static int kg_movebuf_cnt;
static WNDPROC kg_oldWndProc;
static HANDLE kg_thread;
static R_RSA_PRIVATE_KEY kg_privKey;
static HWND kg_hwndDlg;
static void writeBFdata(FILE *out, BLOWFISH_CTX *ctx, void *data, unsigned int len, unsigned long *cbcl, unsigned long *cbcr, int *lc)
{
unsigned int x;
unsigned long *p=(unsigned long *)data;
for (x = 0; x < len; x += 8)
{
unsigned long pp[2];
pp[0]=p[0];
pp[1]=p[1];
pp[0]^=*cbcl;
pp[1]^=*cbcr;
Blowfish_Encrypt(ctx,pp,pp+1);
*cbcl=pp[0];
*cbcr=pp[1];
int c;
for (c = 0; c < 8; c ++)
{
fprintf(out,"%02X",((unsigned char *)pp)[c]);
if (++*lc % 30 == 0) fprintf(out,"\n");
}
p+=2;
}
}
int kg_writePrivateKey(char *fn, R_RSA_PRIVATE_KEY *key, R_RANDOM_STRUCT *rnd, char *passhash)
{
FILE *fp;
int ks=(key->bits+7)/8;
int x;
int lc=8;
fp=fopen(fn,"wt");
if (!fp) return 1;
fprintf(fp,"WASTE_PRIVATE_KEY 10 %d\n",key->bits);
unsigned long tl[2];
R_GenerateBytes((unsigned char *)&tl,8,rnd);
for (x = 0; x < 8; x ++)
{
fprintf(fp,"%02X",(tl[x/4]>>((x&3)*8))&0xff);
}
BLOWFISH_CTX c;
Blowfish_Init(&c,(unsigned char *)passhash,SHA_OUTSIZE);
char buf[9]="PASSWORD";
writeBFdata(fp,&c,buf,8,tl,tl+1,&lc);
#define WPK(x) writeBFdata(fp,&c,key->x,sizeof(key->x),tl,tl+1,&lc);
WPK(modulus)
WPK(publicExponent)
WPK(exponent)
WPK(prime)
WPK(primeExponent)
WPK(coefficient)
#undef WPK
if (lc % 30) fprintf(fp,"\n");
fprintf(fp,"WASTE_PRIVATE_KEY_END\n");
fclose(fp);
memset(&c,0,sizeof(c));//safety
return 0;
}
static DWORD WINAPI keyThread(LPVOID p)
{
kg_start_time=time(NULL);
R_RSA_PROTO_KEY protoKey;
protoKey.bits=kg_keysize;
protoKey.useFermat4=1;
R_RSA_PUBLIC_KEY kg_pubKey;
memset(&kg_pubKey,0,sizeof(kg_pubKey));
if (R_GeneratePEMKeys(&kg_pubKey,&kg_privKey,&protoKey,&kg_random) ||
kg_writePrivateKey(kg_privout,&kg_privKey,&kg_random,kg_passbf))
{
SetDlgItemText(kg_hwndDlg,IDC_LABEL1,"Error creating keys");
SetDlgItemText(kg_hwndDlg,IDC_LABEL5,"");
MessageBox(kg_hwndDlg,"Error calling GeneratePEMKeys()",APP_NAME " Key Generation Error",MB_OK|MB_ICONSTOP);
}
else
{
kg_start_time=time(NULL)-kg_start_time;
PostMessage(kg_hwndDlg,WM_USER+0x131,0,1);
}
memset(kg_passbf,0,SHA_OUTSIZE);
memset(&kg_pubKey,0,sizeof(kg_pubKey));
memset(&kg_random,0,sizeof(kg_random));
memcpy(&g_key,&kg_privKey,sizeof(R_RSA_PRIVATE_KEY));
SHAify m;
m.add((unsigned char *)g_key.modulus,MAX_RSA_MODULUS_LEN);
m.add((unsigned char *)g_key.publicExponent,MAX_RSA_MODULUS_LEN);
m.final(g_pubkeyhash);
memset(&kg_privKey,0,sizeof(R_RSA_PRIVATE_KEY));
KillTimer(kg_hwndDlg,1);
return 0;
}
static BOOL CALLBACK kg_newWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
{
if (uMsg == WM_MOUSEMOVE && kg_going==1)
{
kg_movebuf[kg_movebuf_cnt%7]+=lParam;
kg_movebuf[(kg_movebuf_cnt+1)%7]+=GetTickCount();
kg_movebuf[(kg_movebuf_cnt+2)%7]+=GetMessageTime()+GetMessagePos();
if (++kg_movebuf_cnt >= 29)
{
kg_movebuf_cnt=0;
R_RandomUpdate(&kg_random,(unsigned char *)kg_movebuf,sizeof(kg_movebuf));
unsigned int bytesNeeded;
R_GetRandomBytesNeeded(&bytesNeeded, &kg_random);
SendDlgItemMessage(hwndDlg,IDC_PROGRESS1,PBM_SETPOS,(WPARAM)(64-bytesNeeded/4),0);
if (!bytesNeeded)
{
kg_going=2;
SendDlgItemMessage(hwndDlg,IDC_PROGRESS1,PBM_SETPOS,0,0);
SetDlgItemText(hwndDlg,IDC_LABEL1,"Generating key pair... please wait");
char buf[128];
wsprintf(buf,"(this may take as long as %d minutes)",kg_keysize<=1024?1:kg_keysize<=2048?3:kg_keysize<=3072?10:20);
SetDlgItemText(hwndDlg,IDC_LABEL5,buf);
SetTimer(hwndDlg,1,250,NULL);
DWORD id;
kg_thread=CreateThread(NULL,0,keyThread,NULL,0,&id);
}
}
}
return CallWindowProc(kg_oldWndProc,hwndDlg,uMsg,wParam,lParam);
}
static BOOL WINAPI dlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_USER+0x131: // success, finish writing out
SendDlgItemMessage(hwndDlg,IDC_PROGRESS1,PBM_SETPOS,(WPARAM)64,0);
{
char buf[123];
wsprintf(buf,"Completed in %d minutes %d seconds",kg_start_time/60,kg_start_time%60);
SetDlgItemText(hwndDlg,IDC_LABEL1,buf);
SetDlgItemText(hwndDlg,IDC_LABEL5,"");
MessageBox(hwndDlg,buf,APP_NAME " Key Generator - Completed",MB_OK|MB_ICONINFORMATION);
}
SetDlgItemText(hwndDlg,IDOK,"Close");
EnableWindow(GetDlgItem(hwndDlg,IDOK),1);
SetActiveWindow(GetDlgItem(hwndDlg,IDOK));
EnableWindow(GetDlgItem(hwndDlg,IDCANCEL),0);
if (kg_thread)
{
CloseHandle(kg_thread);
kg_thread=0;
}
EndDialog(hwndDlg,1); // yay
break;
case WM_INITDIALOG:
kg_hwndDlg=hwndDlg;
SetWindowText(hwndDlg,APP_NAME " Key Generator");
SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_ADDSTRING,0,(LPARAM)"1024 bits (weak, not recommended)");
SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_ADDSTRING,0,(LPARAM)"1536 bits (recommended)");
SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_ADDSTRING,0,(LPARAM)"2048 bits (slower, recommended)");
SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_ADDSTRING,0,(LPARAM)"3072 bits (slow, not recommended)");
SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_ADDSTRING,0,(LPARAM)"4096 bits (very slow, not recommended)");
SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_SETCURSEL,1,0);
break;
case WM_CLOSE: EndDialog(hwndDlg,0); break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDCANCEL:
if (kg_thread)
{
if (WaitForSingleObject(kg_thread,0) == WAIT_TIMEOUT) TerminateThread(kg_thread,0);
CloseHandle(kg_thread);
kg_thread=0;
}
EndDialog(hwndDlg,0);
break;
case IDOK:
if (kg_going == 2)
{
EndDialog(hwndDlg,1);
}
if (!kg_going)
{
int wndhide[]={
IDC_EDIT1,IDC_EDIT2,
IDC_LABEL3,IDC_LABEL4,IDC_LABEL6,IDC_COMBO1
};
switch (SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_GETCURSEL,0,0))
{
case 0:
kg_keysize=1024;
break;
case 1:
kg_keysize=1536;
break;
case 2:
kg_keysize=2048;
break;
case 3:
kg_keysize=3072;
break;
case 4:
kg_keysize=4096;
break;
default:
kg_keysize=2048;
}
char pass1[1024],pass2[1024];
GetDlgItemText(hwndDlg,IDC_EDIT1,pass1,sizeof(pass1));
GetDlgItemText(hwndDlg,IDC_EDIT2,pass2,sizeof(pass2));
if (strcmp(pass1,pass2))
{
MessageBox(hwndDlg,"Passphrase mistyped",APP_NAME " Key Generator Error",MB_OK|MB_ICONSTOP);
break;
}
SHAify c;
c.add((unsigned char *)pass1,strlen(pass1));
c.final((unsigned char *)kg_passbf);
memset(pass1,0,sizeof(pass1));
memset(pass2,0,sizeof(pass2));
int x;
for (x = 0; x < sizeof(wndhide)/sizeof(wndhide[0]); x ++)
ShowWindow(GetDlgItem(hwndDlg,wndhide[x]),SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg,IDC_LABEL5),SW_SHOWNA);
SetDlgItemText(hwndDlg,IDC_LABEL1,"Giving random number generator more randomness, please move");
EnableWindow(GetDlgItem(hwndDlg,IDOK),0);
ShowWindow(GetDlgItem(hwndDlg,IDC_PROGRESS1),SW_SHOWNA);
SendDlgItemMessage(hwndDlg,IDC_PROGRESS1,PBM_SETRANGE,0,MAKELPARAM(0,64));
kg_oldWndProc=(WNDPROC) SetWindowLong(hwndDlg,GWL_WNDPROC,(LONG)kg_newWndProc);
kg_going=1;
R_RandomInit(&kg_random);
unsigned char buf[16];
R_GenerateBytes(buf,sizeof(buf),&g_random);
R_RandomUpdate(&kg_random,buf,sizeof(buf));
memset(buf,0,sizeof(buf));
}
break;
}
return 0;
case WM_DESTROY:
if (kg_oldWndProc)
SetWindowLong(hwndDlg,GWL_WNDPROC,(LONG)kg_oldWndProc);
kg_oldWndProc=0;
return 0;
case WM_TIMER:
if (wParam == 1)
{
static int pos;
if (++pos == 64) pos=0;
SendDlgItemMessage(hwndDlg,IDC_PROGRESS1,PBM_SETPOS,(WPARAM)pos,0);
}
break;
}
return 0;
}
static int running=0;
void RunKeyGen(HWND hwndParent, char *keyout)
{
if (!running)
{
running=1;
memset(&kg_privKey,0,sizeof(kg_privKey));
kg_thread=0;
kg_start_time=0;
memset(kg_movebuf,0,sizeof(kg_movebuf));
kg_movebuf_cnt=0;
kg_oldWndProc=0;
kg_going=0;
kg_keysize=0;
memset(&kg_random,0,sizeof(kg_random));
kg_privout=keyout;
DialogBox(g_hInst,MAKEINTRESOURCE(IDD_KEYGEN),hwndParent,dlgProc);
running=0;
}
}