~ubuntu-branches/ubuntu/quantal/genometools/quantal-backports

« back to all changes in this revision

Viewing changes to src/external/md5-1.1.2/tests/DES56/ftest.c

  • Committer: Package Import Robot
  • Author(s): Sascha Steinbiss
  • Date: 2012-07-09 14:10:23 UTC
  • Revision ID: package-import@ubuntu.com-20120709141023-juuu4spm6chqsf9o
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * Fast DES evaluation & benchmarking for UNIX
 
4
 * Compares output of fencrypt()/fsetkey() with UNIX crypt()/encrypt()/setkey()
 
5
 * and measures speed using times().
 
6
 */
 
7
 
 
8
#include <stdio.h>
 
9
#include <stdlib.h>
 
10
#if USG
 
11
#include <sys/types.h>
 
12
#endif /*USG*/
 
13
#include <sys/param.h>  /* for HZ */
 
14
#include <sys/times.h>
 
15
 
 
16
#include <crypt.h>
 
17
 
 
18
#include "des56.h"
 
19
 
 
20
typedef struct {
 
21
        char b[8];
 
22
} chunk;
 
23
 
 
24
#ifdef CRAY
 
25
#  define  USG  1
 
26
#endif /*CRAY*/
 
27
 
 
28
#ifndef USG
 
29
#  define  USG  0
 
30
#endif  /*!USG*/
 
31
 
 
32
#if BSD >= 43 && defined(vax) && !defined(HZ)
 
33
#  define  HZ 100
 
34
#endif
 
35
 
 
36
 
 
37
char *
 
38
bprint(char b[64])
 
39
{
 
40
        static char s[17];
 
41
        register int i;
 
42
 
 
43
        for(i = 0; i < 64; i += 4)
 
44
            sprintf(&s[i/4], "%1x", b[i]<<3 | b[i+1]<<2 | b[i+2]<<1 | b[i+3]);
 
45
        return(s);
 
46
}
 
47
 
 
48
char *
 
49
wprint(chunk *v)
 
50
{
 
51
        static char s[17];
 
52
        register int i;
 
53
 
 
54
        for(i = 0; i < 8; i++)
 
55
                sprintf(&s[2*i], "%02x", v->b[i] & 0xff);
 
56
        return(s);
 
57
}
 
58
 
 
59
void getv(char *s, chunk *v)
 
60
{
 
61
        register int i, t;
 
62
 
 
63
        if(s[0] == '0' && s[1] == 'x')
 
64
                s += 2;
 
65
        for(i = 0; i < 8; i++) {
 
66
                t = 0;
 
67
                if(*s >= '0' && *s <= '9') t = *s++ - '0';
 
68
                else if(*s >= 'a' && *s <= 'f') t = *s++ - 'a' + 10;
 
69
                else if(*s >= 'A' && *s <= 'F') t = *s++ - 'A' + 10;
 
70
                t <<= 4;
 
71
                if(*s >= '0' && *s <= '9') t |= *s++ - '0';
 
72
                else if(*s >= 'a' && *s <= 'f') t |= *s++ - 'a' + 10;
 
73
                else if(*s >= 'A' && *s <= 'F') t |= *s++ - 'A' + 10;
 
74
                v->b[i] = t;
 
75
                if(*s == '.') {
 
76
                        s++;
 
77
                        i = 4-1;
 
78
                }
 
79
        }
 
80
}
 
81
 
 
82
void expand(chunk *v, char bits[64])
 
83
{
 
84
        register unsigned int i;
 
85
 
 
86
        for(i = 0; i < 64; i++)
 
87
                bits[i] = (v->b[i/8] >> (7 - i%8)) & 1;
 
88
}
 
89
 
 
90
 
 
91
int
 
92
main(int argc, char *argv[])
 
93
{
 
94
        register int i;
 
95
        chunk key, olddata, newdata;
 
96
        char bkey[64], bdata[64];
 
97
        int decrypt = 0;
 
98
        keysched KS;
 
99
 
 
100
        if(argc < 2 || argv[1][0] == '-') {
 
101
 
 
102
    usage:
 
103
                printf("\
 
104
Usage: %s  key  [ -{ckCK} count ] [ data ... ]\n\
 
105
Demonstrate and/or time fast DES routines.\n\
 
106
``key'' and ``data'' are left-justified, 0-padded hex values <= 16 digits\n\
 
107
By default, encrypts & decrypts each 'data' block with both fastdes and\n\
 
108
crypt() library DES routines to show equality.\n\
 
109
-c N    encrypt N times using fast DES\n\
 
110
-k N    set-key N times using fast DES\n\
 
111
-C N    encrypt N times using library DES\n\
 
112
%s",
 
113
                        argv[0],
 
114
                        USG ? "" : "\
 
115
-K N    set-key N times using library DES\n");
 
116
                exit(1);
 
117
        }
 
118
 
 
119
        getv(argv[1], &key);
 
120
        fsetkey(key.b, &KS);
 
121
        expand(&key, bkey);
 
122
#if USG
 
123
        /* System V systems don't seem to have setkey, just crypt
 
124
         * so we use that to set the key.
 
125
         */
 
126
        for(i = 0; i < 8; i++)
 
127
                bdata[i] = (key.b[i] >> 1) | 0x80;
 
128
        bdata[8] = '\0';
 
129
        crypt((char *)bdata, "..");     /* Key, no salt */
 
130
#else /*!USG*/
 
131
        setkey(bkey);
 
132
#endif /*!USG*/
 
133
        printf("key\t%s\n", bprint(bkey));
 
134
 
 
135
        for(i = 2; i < argc; i++) {
 
136
            if(argv[i][0] == '-') {
 
137
                int count, n;
 
138
                char c, *op;
 
139
                struct tms now, then;
 
140
 
 
141
                c = argv[i][1];
 
142
                op = &argv[i][2];
 
143
                if(*op == '\0')
 
144
                        if((op = argv[++i]) == NULL)
 
145
                                goto usage;
 
146
                count = atoi(op);
 
147
                if(count <= 0)
 
148
                    count = 1;
 
149
                n = count;
 
150
                expand(&olddata, bdata);
 
151
                times(&now);
 
152
                switch(c) {
 
153
                case 'c':
 
154
                    op = "fencrypt";
 
155
                    do fencrypt(newdata.b, 0, &KS); while(--n > 0); break;
 
156
                case 'k':
 
157
                    op = "fsetkey";
 
158
                    do fsetkey(key.b, &KS); while(--n > 0); break;
 
159
 
 
160
                case 'C':
 
161
                    op = "library encrypt";
 
162
                    do encrypt(bdata, decrypt); while(--n > 0); break;
 
163
 
 
164
                case 'K':
 
165
#if USG
 
166
                    printf("UNIX library has no setkey() function on this system\n");
 
167
                    continue;
 
168
#else /*!USG*/
 
169
                    op = "library setkey";
 
170
                    do setkey(bkey); while(--n > 0); break;
 
171
#endif /*!USG*/
 
172
 
 
173
                default:
 
174
                    printf("Unknown option -%c\n", c);
 
175
                    goto usage;
 
176
                }
 
177
                times(&then);
 
178
                n = then.tms_utime - now.tms_utime;
 
179
                printf("%d %s's in %0.2f seconds (%.2f us apiece)\n",
 
180
                    count, op, (float) n / HZ,
 
181
                    (1.0e6 * n / (HZ * count)));
 
182
            } else {
 
183
                /* Demonstrate that it works for a particular data block.
 
184
                 * To compare with UNIX encrypt(), we must play its game.
 
185
                 * On BSD systems, encrypt(block, 1) is the inverse of (..., 0)
 
186
                 * but on USG systems the second parameter is ignored and
 
187
                 * encrypt(block, x) always encrypts.
 
188
                 */
 
189
                getv(argv[i], &olddata);
 
190
                newdata = olddata;
 
191
 
 
192
                printf("\tOriginal data\t\tEncrypted\t\t%s\n",
 
193
                        USG ? "Encrypted again" : "Decrypted");
 
194
 
 
195
                printf("fastdes\t%s", wprint(&olddata));
 
196
                fencrypt(newdata.b, 0, &KS);
 
197
                printf("\t%s", wprint(&newdata));
 
198
                fencrypt(newdata.b, USG ? 0 : 1, &KS);
 
199
                printf("\t%s\n", wprint(&newdata));
 
200
 
 
201
                expand(&olddata, bdata);
 
202
                printf("UNIXdes\t%s", bprint(bdata));
 
203
                encrypt(bdata, 0);
 
204
                printf("\t%s", bprint(bdata));
 
205
                encrypt(bdata, USG ? 0 : 1);
 
206
                printf("\t%s\n", bprint(bdata));
 
207
            }
 
208
        }
 
209
        exit(0);
 
210
}
 
211