forked from zedshaw/srpmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
srp6bench.c
278 lines (242 loc) · 6.82 KB
/
srp6bench.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
/*
* Copyright (c) 1997-2007 The Stanford SRP Authentication Project
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
* THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Redistributions in source or binary form must retain an intact copy
* of this copyright notice.
*/
#include <stdio.h>
#ifdef WIN32
#include <sys/timeb.h>
#else
#include <sys/time.h>
#endif
#include <stdlib.h>
#include <assert.h>
#include "t_pwd.h"
#include "srp.h"
int
do_srp6(struct t_confent * pce, struct t_pwent * ppwe,
const char * user, const char * pass)
{
SRP_RESULT rc;
SRP * srps;
SRP * srpc;
cstr * client_pub;
cstr * server_pub;
cstr * ks;
cstr * kc;
client_pub = NULL;
srps = SRP_new(SRP6a_server_method());
if(!SRP_OK(SRP_set_username(srps, user))) {
printf("SRP_set_username failed\n");
return -1;
}
if(!SRP_OK(SRP_set_params(srps, pce->modulus.data, pce->modulus.len,
pce->generator.data, pce->generator.len,
ppwe->salt.data, ppwe->salt.len))) {
printf("SRP_set_params failed\n");
return -1;
}
if(!SRP_OK(SRP_set_authenticator(srps, ppwe->password.data,
ppwe->password.len))) {
printf("SRP_set_authenticator failed\n");
return -1;
}
srpc = SRP_new(SRP6a_client_method());
if(!SRP_OK(SRP_set_username(srpc, user))) {
printf("SRP_set_username failed\n");
return -1;
}
if(!SRP_OK(SRP_set_params(srpc, pce->modulus.data, pce->modulus.len,
pce->generator.data, pce->generator.len,
ppwe->salt.data, ppwe->salt.len))) {
printf("SRP_set_params failed\n");
return -1;
}
if(!SRP_OK(SRP_gen_pub(srpc, &client_pub))) {
printf("SRP_gen_pub failed\n");
return -1;
}
server_pub = NULL;
if(!SRP_OK(SRP_gen_pub(srps, &server_pub))) {
printf("SRP_gen_pub failed\n");
return -1;
}
ks = NULL;
if(!SRP_OK(SRP_compute_key(srps, &ks, client_pub->data, client_pub->length))) {
printf("SRP_compute_key failed\n");
return -1;
}
if(!SRP_OK(SRP_set_auth_password(srpc, pass))) {
printf("SRP_set_authenticator failed\n");
return -1;
}
kc = NULL;
if(!SRP_OK(SRP_compute_key(srpc, &kc, server_pub->data, server_pub->length))) {
printf("SRP_compute_key failed\n");
return -1;
}
if(!SRP_OK(SRP_respond(srpc, &kc))) {
printf("SRP_respond failed\n");
return -1;
}
rc = SRP_verify(srps, kc->data, kc->length);
if(SRP_OK(rc)) {
if(!SRP_OK(SRP_respond(srps, &ks))) {
printf("SRP_respond failed\n");
return -1;
}
rc = SRP_verify(srpc, ks->data, ks->length);
if(!SRP_OK(rc)) {
printf("[client] verify status = %d (failure)\n", rc);
return -1;
}
}
else {
printf("[server] verify status = %d (failure)\n", rc);
return -1;
}
SRP_free(srpc);
SRP_free(srps);
cstr_free(client_pub);
cstr_free(server_pub);
cstr_free(kc);
cstr_free(ks);
return 0;
}
int
do_srp6param(struct t_confent * pce, int iterations)
{
const char * user = "user";
const char * pass = "password";
struct t_pw * tpw;
struct t_pwent * ppwe;
double elapsedsecs;
int i;
#ifdef WIN32
struct timeb before, after;
unsigned long elapsedms;
#else
struct timeval before, after;
unsigned long elapsedus;
#endif
tpw = t_newpw();
ppwe = t_makepwent(tpw, user, pass, NULL, pce);
#ifdef WIN32
ftime(&before);
#else
gettimeofday(&before, NULL);
#endif
for(i = 0; i < iterations; ++i)
do_srp6(pce, ppwe, user, pass);
#ifdef WIN32
ftime(&after);
elapsedms = 1000 * (after.time - before.time) +
after.millitm - before.millitm;
elapsedsecs = (double) elapsedms / 1000;
#else
gettimeofday(&after, NULL);
elapsedus = 1000000 * (after.tv_sec - before.tv_sec) +
after.tv_usec - before.tv_usec;
elapsedsecs = (double) elapsedus / 1000000;
#endif
printf("%d %d-bit operations in %g seconds (%g ops/s)\n", 2 * iterations,
8 * pce->modulus.len, elapsedsecs, 2.0 * iterations / elapsedsecs);
t_closepw(tpw);
return 0;
}
int
do_srp6preparam(int pindex, int iterations)
{
struct t_confent ce;
struct t_preconf * pc;
pc = t_getpreparam(pindex);
if(pc == NULL) {
fprintf(stderr, "Unable to load parameter set %d\n", pindex);
exit(2);
}
ce.modulus.len = pc->modulus.len;
ce.modulus.data = pc->modulus.data;
ce.generator.len = pc->generator.len;
ce.generator.data = pc->generator.data;
ce.index = pindex - 1;
return do_srp6param(&ce, iterations);
}
void
usage()
{
fprintf(stderr, "Usage: srp6bench [-engine e] [-index]\n");
fprintf(stderr, " where 'index' is a precompiled parameter index\n");
exit(1);
}
/* These should correspond to the preparam numbers of the
* appropriate bit sizes, preferably the IETF numbers. */
#define PREPARAM_1024 4
#define PREPARAM_2048 8
#define PREPARAM_4096 10
/* Number of iterations to run for each bit length.
* Should be balanced so that each one takes equally long. */
#define ITERATIONS_1024 360 * 2
#define ITERATIONS_2048 120 * 2
#define ITERATIONS_4096 40 * 2
#define ITERATIONS_GENERIC 50
int
main(argc, argv)
int argc;
char **argv;
{
int pindex = -1;
SRP_initialize_library();
while(--argc > 0 && *++argv != NULL) {
if(strcmp(*argv, "-engine") == 0) {
if(--argc > 0 && *++argv != NULL) {
if(!SRP_OK(SRP_use_engine(*argv))) {
fprintf(stderr, "Unable to use engine '%s'\n", *argv);
exit(2);
}
}
else
usage();
}
else if((*argv)[1] >= '0' && (*argv)[1] <= '9')
pindex = atoi(*argv + 1);
else
usage();
}
if(pindex < 0) {
if(do_srp6preparam(PREPARAM_1024, ITERATIONS_1024) < 0)
return 1;
if(do_srp6preparam(PREPARAM_2048, ITERATIONS_2048) < 0)
return 1;
if(do_srp6preparam(PREPARAM_4096, ITERATIONS_4096) < 0)
return 1;
}
else {
if(do_srp6preparam(pindex, ITERATIONS_GENERIC) < 0)
return 1;
}
SRP_finalize_library();
return 0;
}