aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/xntpd/authstuff/authspeed.c
blob: 05af1326d8edf846542a4d56771a58f9397a68a0 (plain) (blame)
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
/* authspeed.c,v 3.1 1993/07/06 01:04:54 jbj Exp
 * authspeed - figure out how LONG it takes to do an NTP encryption
 */

#if defined(SYS_HPUX) || defined(SYS_AUX3) || defined(SYS_AUX2) || defined(SOLARIS) || defined(SYS_SVR4) || defined(SYS_PTX)
#define FAKE_RUSAGE
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#ifdef FAKE_RUSAGE
#include <sys/param.h>
#include <sys/times.h>
#endif

#include "ntp_fp.h"
#include "ntp_stdlib.h"

#define	STREQ(a, b)	(*(a) == *(b) && strcmp((a), (b)) == 0)

#define DEFLOOPS	-1

#define	DEFDELAYLOOPS	20000
#define	DEFCOSTLOOPS	2000

char *progname;
int debug;

struct timeval tstart, tend;
#ifdef FAKE_RUSAGE
struct tms rstart, rend;
#define	getrusage(foo, t)	times(t)
#define	RUSAGE_SELF	0
#else
struct rusage rstart, rend;
#endif

l_fp dummy1, dummy2;
U_LONG dummy3;

U_LONG pkt[15];

int totalcost = 0;
double rtime;
double vtime;

int domd5 = 0;

static	void	dodelay	P((int));
static	void	docheap	P((int));
static	void	docost	P((int));
static	void	subtime	P((struct timeval *, struct timeval *, double *));

/*
 * main - parse arguments and handle options
 */
void
main(argc, argv)
int argc;
char *argv[];
{
	int c;
	int loops;
	int i;
	int errflg = 0;
	extern int optind;
	extern char *optarg;

	progname = argv[0];
	loops = DEFLOOPS;
	while ((c = getopt_l(argc, argv, "cdmn:")) != EOF)
		switch (c) {
		case 'c':
			totalcost++;
			break;
		case 'd':
			++debug;
			break;
		case 'm':
			domd5 = 16;	/* offset into list of keys */
			break;
		case 'n':
			loops = atoi(optarg);
			if (loops <= 0) {
				(void) fprintf(stderr, 
			"%s: %s is unlikely to be a useful number of loops\n",
				    progname, optarg);
				errflg++;
			}
			break;
		default:
			errflg++;
			break;
		}
	if (errflg || optind == argc) {
		(void) fprintf(stderr,
		    "usage: %s [-d] [-n loops] [ -c ] auth.samplekeys\n",
		    progname);
		exit(2);
	}
	printf("Compute timing for ");
	if (domd5)
	    printf("MD5");
	else
	    printf("DES");
	printf(" based authentication.\n");

	init_auth();
	authreadkeys(argv[optind]);
	for (i = 0; i < 16; i++) {
		if (!auth_havekey(i + domd5)) {
			errflg++;
			(void) fprintf(stderr, "%s: key %d missing\n",
			    progname, i + domd5);
		}
	}

	if (errflg) {
		(void) fprintf(stderr,
	"%s: check syslog for errors, or use file with complete set of keys\n",
		    progname);
		exit(1);
	}

	if (loops == DEFLOOPS) {
		if (totalcost)
			loops = DEFCOSTLOOPS;
		else
			loops = DEFDELAYLOOPS;
	}

	dummy1.l_ui = 0x80808080;
	dummy1.l_uf = 0xffffff00;
	dummy3 = 0x0aaaaaaa;

	for (i = 0; i < 12; i++)
		pkt[i] = i * 0x22222;

	if (totalcost) {
		if (totalcost > 1)
			docheap(loops);
		else
			docost(loops);
	} else {
		dodelay(loops);
	}

	printf("total real time: %.3f\n", rtime);
	printf("total CPU time: %.3f\n", vtime);
	if (totalcost) {
		printf("real cost (in seconds): %.6f\n",
		    rtime/(double)loops);
		printf("CPU cost (in seconds): %.6f\n",
		    vtime/(double)loops);
		printf("\nThis includes the cost of a decryption plus the\n");
		printf("the cost of an encryption, i.e. the cost to process\n");
		printf("a single authenticated packet.\n");
	} else {
		printf("authdelay in the configuration file\n");
		printf("real authentication delay: %.6f\n",
		    rtime/(double)loops);
		printf("authentication delay in CPU time: %.6f\n",
		    vtime/(double)loops);
		printf("\nThe CPU delay is probably the best bet for\n");
		printf("authdelay in the configuration file\n");
	}
	exit(0);
}


/*
 * dodelay - do the delay measurement
 */
static void
dodelay(loops)
	int loops;
{
	double vtime1, rtime1, vtime2, rtime2;
	register int loopcount;
	/*
	 *  If we're attempting to compute the cost of an auth2crypt()
	 *  for first compute the total cost, then compute the
	 *  cost of only doing the first step, auth1crypt().  What 
	 *  remains is the cost of auth2crypt.
	 */
	loopcount = loops;
	(void) gettimeofday(&tstart, (struct timezone *)0);
	(void) getrusage(RUSAGE_SELF, &rstart);
	
	while (loopcount-- > 0) {
		auth1crypt((loops & 0xf) + domd5, pkt, 48);
		L_ADDUF(&dummy1, dummy3);
		auth2crypt((loops & 0xf) + domd5, pkt, 48);
	}
	
	(void) getrusage(RUSAGE_SELF, &rend);
	(void) gettimeofday(&tend, (struct timezone *)0);
	
	subtime(&tstart, &tend, &rtime1);
#ifdef FAKE_RUSAGE
	vtime1 = (rend.tms_utime - rstart.tms_utime) * 1.0 / HZ;
#else
	subtime(&rstart.ru_utime, &rend.ru_utime, &vtime1);
#endif
printf("Time for full encryptions is %f rusage %f real\n", vtime1, rtime1);
	loopcount = loops;
	(void) gettimeofday(&tstart, (struct timezone *)0);
	(void) getrusage(RUSAGE_SELF, &rstart);

	while (loopcount-- > 0) {
		auth1crypt((loops & 0xf) + domd5, pkt, 48);
	}

	(void) getrusage(RUSAGE_SELF, &rend);
	(void) gettimeofday(&tend, (struct timezone *)0);

	subtime(&tstart, &tend, &rtime2);
#ifdef FAKE_RUSAGE
	vtime2 = (rend.tms_utime - rstart.tms_utime) * 1.0 / HZ;
#else
	subtime(&rstart.ru_utime, &rend.ru_utime, &vtime2);
#endif

printf("Time for auth1crypt is %f rusage %f real\n", vtime2, rtime2);
	vtime = vtime1 - vtime2;
	rtime = rtime1 - rtime2;
}


/*
 * docheap - do the cost measurement the cheap way
 */
static void
docheap(loops)
	register int loops;
{

	(void) authhavekey(3 + domd5);

	(void) gettimeofday(&tstart, (struct timezone *)0);
	(void) getrusage(RUSAGE_SELF, &rstart);

	while (loops-- > 0) {
		auth1crypt(3 + domd5, pkt, 48);
		L_ADDUF(&dummy1, dummy3);
		auth2crypt(3 + domd5, pkt, 48);
		(void) authdecrypt(3 + domd5, pkt, 48);
	}

	(void) getrusage(RUSAGE_SELF, &rend);
	(void) gettimeofday(&tend, (struct timezone *)0);

	subtime(&tstart, &tend, &rtime);
#ifdef FAKE_RUSAGE
	vtime = (rend.tms_utime - rstart.tms_utime) * 1.0 / HZ;
#else
	subtime(&rstart.ru_utime, &rend.ru_utime, &vtime);
#endif
}


/*
 * docost - do the cost measurement
 */
static void
docost(loops)
	register int loops;
{

	(void) gettimeofday(&tstart, (struct timezone *)0);
	(void) getrusage(RUSAGE_SELF, &rstart);

	while (loops-- > 0) {
		auth1crypt((loops & 0xf) + domd5, pkt, 48);
		L_ADDUF(&dummy1, dummy3);
		auth2crypt((loops & 0xf) + domd5, pkt, 48);
		(void) authdecrypt(((loops+1) & 0xf) + domd5, pkt, 48);
	}

	(void) getrusage(RUSAGE_SELF, &rend);
	(void) gettimeofday(&tend, (struct timezone *)0);

	subtime(&tstart, &tend, &rtime);
#ifdef FAKE_RUSAGE
	vtime = (rend.tms_utime - rstart.tms_utime) * 1.0 / HZ;
#else
	subtime(&rstart.ru_utime, &rend.ru_utime, &vtime);
#endif
}


/*
 * subtime - subtract two struct timevals, return double result
 */
static void
subtime(tvs, tve, res)
	struct timeval *tvs, *tve;
	double *res;
{
	LONG sec;
	LONG usec;

	sec = tve->tv_sec - tvs->tv_sec;
	usec = tve->tv_usec - tvs->tv_usec;

	if (usec < 0) {
		usec += 1000000;
		sec--;
	}

	*res = (double)sec + (double)usec/1000000.;
	return;
}