aboutsummaryrefslogtreecommitdiff
path: root/crypt/speeds.c
blob: 0d55ae83be249dd00b5d862259760621a2ba90f7 (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
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
/*
 * This fcrypt/crypt speed testing program
 * is derived from one floating around in
 * the net. It's distributed along with
 * UFC-crypt but is not covered by any
 * licence.
 *
 * @(#)speeds.c	1.11 20 Aug 1996
 */

#include <signal.h>
#include <stdio.h>

#ifndef SIGVTALRM
/*
 * patch from chip@chinacat.unicom.com (Chip Rosenthal):
 * you may enable it if your system does not include
 * a setitimer() function. You'll have to ensure the
 * existence a environment variable: HZ giving how many
 * ticks goes per second.
 * If not existing in your default environment 50, 60
 * or even 100 may be the right value. Perhaps you should
 * then use 'time ./ufc 10000' instead of guessing.
 */
#define NO_ITIMER
#endif

#ifdef NO_ITIMER
#include <sys/types.h>
#include <sys/times.h>
#else
#include <sys/time.h>
#endif

static int cnt;
#ifdef NO_ITIMER
char *hz;
struct tms tstart, tfinish;
#endif
#define ITIME	10		/* Number of seconds to run test. */

char *crypt(), *fcrypt();

void
Stop (void)
{
    double elapsed;
#ifdef NO_ITIMER
    (void) times(&tfinish);
    elapsed = ((tfinish.tms_utime + tfinish.tms_stime) -
	(tstart.tms_utime + tstart.tms_stime)) / atoi(hz);
    printf("elapsed time = %d sec,  CPU time = %f sec\n", ITIME, elapsed);
#else
    elapsed = ITIME;
#endif
    printf ("Did %f %s()s per second.\n", ((float) cnt) / elapsed,
#if defined(FCRYPT)
	    "fcrypt"
#else
	    "crypt"
#endif
    );
    exit (0);
}

/*
 * Silly rewrite of 'bzero'. I do so
 * because some machines don't have
 * bzero and some don't have memset.
 */

static void clearmem(start, cnt)
  char *start;
  int cnt;
  { while(cnt--)
      *start++ = '\0';
  }

main (void)
{
   char *s;
#ifdef NO_ITIMER
    extern char *getenv();
#else
    struct itimerval itv;
#endif

#ifdef NO_ITIMER
    if ((hz = getenv("HZ")) == NULL) {
	fprintf(stderr, "HZ environment parameter undefined\n");
	exit(1);
    }
#endif

#ifdef FCRYPT
    printf("\n");
    printf("Warning: this version of the speed program may run slower when\n");
    printf("benchmarking UFC-crypt than previous versions. This is because it\n");
    printf("stresses the CPU hardware cache in order to get benchmark figures\n");
    printf("that corresponds closer to the performance that can be expected in\n");
    printf("a password cracker.\n\n");
#endif

    printf ("Running %s for %d seconds of virtual time ...\n",
#ifdef FCRYPT
    "UFC-crypt",
#else
    "crypt(libc)",
#endif
	    ITIME);

#ifdef FCRYPT
    init_des ();
#endif

#ifdef NO_ITIMER
    signal(SIGALRM, Stop);
    switch (fork()) {
    case -1:
	perror("fork failed");
	exit(1);
    case 0:
	sleep(10);
	kill(getppid(), SIGALRM);
	exit(0);
    default:
	(void) times(&tstart);
    }
#else
    clearmem ((char*)&itv, (int)sizeof (itv));
    signal (SIGVTALRM, Stop);
    itv.it_value.tv_sec = ITIME;
    itv.it_value.tv_usec = 0;
    setitimer (ITIMER_VIRTUAL, &itv, NULL);
#endif


    s = "fredred";
    for (cnt = 0;; cnt++)
    {
#ifdef FCRYPT
	s = fcrypt (s, "eek");
#else
	s = crypt (s, "eek");
#endif
    }
}