summaryrefslogtreecommitdiffstats
path: root/lib/libcrypt/test/speedcrypt.c
blob: 13c8f825346c03a9cbddf6c400fe2ac3bba966fc (plain)
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
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <signal.h>
#include <stdio.h>

int		keep_going, count, seconds;
struct rusage	prior, now;

void
finish()
{
	keep_going = 0;
}


main(int argc, char *argv[])
{
	struct itimerval itv;
	u_long		msecs, key1[8], key2[8];
	char		*k1, *k2;
        char            *token, deftok[] = "eek";

	if (argc < 2 || sscanf(argv[1], "%d", &seconds) != 1)
		seconds = 20;

	if (argc < 3) {
             token = &deftok[0];
        } else {
             token = argv[2];
        }

	printf ("Running crypt for %d seconds of vtime\n", seconds);

	bzero(&itv, sizeof (itv));
	signal (SIGVTALRM, finish);
	itv.it_value.tv_sec = seconds;
	itv.it_value.tv_usec = 0;
	setitimer(ITIMER_VIRTUAL, &itv, NULL);

	keep_going = 1;

	k1 = (char *) key1;
	k2 = (char *) key2;
	strcpy(k1, "fredfredfredfredfred");
	strcpy(k2, "joejoejoejoejoejoejo");

        printf("Dry run (to verify seed token):\n\tcrypt(\"test\", \"%s\")\n",
               token);
        printf("\t=> \"%s\"\nTesting...", crypt("test", token));
        fflush(stdout);

	if (getrusage(0, &prior) < 0) {
		perror("getrusage");
		exit(1);
	}

	for (count = 0; keep_going; count++) {
		crypt((count & 1) ? k1 : k2, token);
	}

	if (getrusage(0, &now) < 0) {
		perror("getrusage");
		exit(1);
	}
	msecs = (now.ru_utime.tv_sec - prior.ru_utime.tv_sec) * 1000
	      + (now.ru_utime.tv_usec - prior.ru_utime.tv_usec) / 1000;
	printf ("\tDid %d crypt()s per second.\n", 1000 * count / msecs);
	exit(0);
}
OpenPOWER on IntegriCloud