190773252ded7593c47a8a95b9747e25da8b69a0
[platform/upstream/krb5.git] / src / lib / crypto / crypto_tests / t_crc.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/crypto_tests/t_crc.c */
3 /*
4  * Copyright 2002,2005 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26
27 /*
28  * Sanity checks for CRC32.
29  */
30 #include <sys/times.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "crypto_int.h"
36
37 #define HEX 1
38 #define STR 2
39 struct crc_trial {
40     int         type;
41     char        *data;
42     unsigned long       sum;
43 };
44
45 struct crc_trial trials[] = {
46     {HEX, "01", 0x77073096},
47     {HEX, "02", 0xee0e612c},
48     {HEX, "04", 0x076dc419},
49     {HEX, "08", 0x0edb8832},
50     {HEX, "10", 0x1db71064},
51     {HEX, "20", 0x3b6e20c8},
52     {HEX, "40", 0x76dc4190},
53     {HEX, "80", 0xedb88320},
54     {HEX, "0100", 0x191b3141},
55     {HEX, "0200", 0x32366282},
56     {HEX, "0400", 0x646cc504},
57     {HEX, "0800", 0xc8d98a08},
58     {HEX, "1000", 0x4ac21251},
59     {HEX, "2000", 0x958424a2},
60     {HEX, "4000", 0xf0794f05},
61     {HEX, "8000", 0x3b83984b},
62     {HEX, "0001", 0x77073096},
63     {HEX, "0002", 0xee0e612c},
64     {HEX, "0004", 0x076dc419},
65     {HEX, "0008", 0x0edb8832},
66     {HEX, "0010", 0x1db71064},
67     {HEX, "0020", 0x3b6e20c8},
68     {HEX, "0040", 0x76dc4190},
69     {HEX, "0080", 0xedb88320},
70     {HEX, "01000000", 0xb8bc6765},
71     {HEX, "02000000", 0xaa09c88b},
72     {HEX, "04000000", 0x8f629757},
73     {HEX, "08000000", 0xc5b428ef},
74     {HEX, "10000000", 0x5019579f},
75     {HEX, "20000000", 0xa032af3e},
76     {HEX, "40000000", 0x9b14583d},
77     {HEX, "80000000", 0xed59b63b},
78     {HEX, "00010000", 0x01c26a37},
79     {HEX, "00020000", 0x0384d46e},
80     {HEX, "00040000", 0x0709a8dc},
81     {HEX, "00080000", 0x0e1351b8},
82     {HEX, "00100000", 0x1c26a370},
83     {HEX, "00200000", 0x384d46e0},
84     {HEX, "00400000", 0x709a8dc0},
85     {HEX, "00800000", 0xe1351b80},
86     {HEX, "00000100", 0x191b3141},
87     {HEX, "00000200", 0x32366282},
88     {HEX, "00000400", 0x646cc504},
89     {HEX, "00000800", 0xc8d98a08},
90     {HEX, "00001000", 0x4ac21251},
91     {HEX, "00002000", 0x958424a2},
92     {HEX, "00004000", 0xf0794f05},
93     {HEX, "00008000", 0x3b83984b},
94     {HEX, "00000001", 0x77073096},
95     {HEX, "00000002", 0xee0e612c},
96     {HEX, "00000004", 0x076dc419},
97     {HEX, "00000008", 0x0edb8832},
98     {HEX, "00000010", 0x1db71064},
99     {HEX, "00000020", 0x3b6e20c8},
100     {HEX, "00000040", 0x76dc4190},
101     {HEX, "00000080", 0xedb88320},
102     {STR, "foo", 0x7332bc33},
103     {STR, "test0123456789", 0xb83e88d6},
104     {STR, "MASSACHVSETTS INSTITVTE OF TECHNOLOGY", 0xe34180f7}
105 };
106
107 #define NTRIALS (sizeof(trials) / sizeof(trials[0]))
108
109 #if 0
110 static void
111 timetest(unsigned int nblk, unsigned int blksiz)
112 {
113     char *block;
114     unsigned int i;
115     struct tms before, after;
116     unsigned long cksum;
117
118     block = malloc(blksiz * nblk);
119     if (block == NULL)
120         exit(1);
121     for (i = 0; i < blksiz * nblk; i++)
122         block[i] = i % 256;
123     times(&before);
124     for (i = 0; i < nblk; i++) {
125         cksum = 0;
126         mit_crc32(block + i * blksiz, blksiz, &cksum);
127     }
128
129     times(&after);
130     printf("shift-8 implementation, %d blocks of %d bytes:\n",
131            nblk, blksiz);
132     printf("\tu=%ld s=%ld cu=%ld cs=%ld\n",
133            (long)(after.tms_utime - before.tms_utime),
134            (long)(after.tms_stime - before.tms_stime),
135            (long)(after.tms_cutime - before.tms_cutime),
136            (long)(after.tms_cstime - before.tms_cstime));
137
138     free(block);
139 }
140 #endif
141
142 static void gethexstr(char *data, size_t *outlen, unsigned char *outbuf,
143                       size_t buflen)
144 {
145     size_t inlen;
146     char *cp, buf[3];
147     long n;
148
149     inlen = strlen(data);
150     *outlen = 0;
151     for (cp = data; (size_t) (cp - data) < inlen; cp += 2) {
152         strncpy(buf, cp, 2);
153         buf[2] = '\0';
154         n = strtol(buf, NULL, 16);
155         outbuf[(*outlen)++] = n;
156         if (*outlen > buflen)
157             break;
158     }
159 }
160
161 static void
162 verify(void)
163 {
164     unsigned int i;
165     struct crc_trial trial;
166     unsigned char buf[4];
167     size_t len;
168     unsigned long cksum;
169     char *typestr;
170
171     for (i = 0; i < NTRIALS; i++) {
172         trial = trials[i];
173         switch (trial.type) {
174         case STR:
175             len = strlen(trial.data);
176             typestr = "STR";
177             cksum = 0;
178             mit_crc32(trial.data, len, &cksum);
179             break;
180         case HEX:
181             typestr = "HEX";
182             gethexstr(trial.data, &len, buf, 4);
183             cksum = 0;
184             mit_crc32(buf, len, &cksum);
185             break;
186         default:
187             typestr = "BOGUS";
188             fprintf(stderr, "bad trial type %d\n", trial.type);
189             exit(1);
190         }
191         printf("%s: %s \"%s\" = 0x%08lx\n",
192                (trial.sum == cksum) ? "OK" : "***BAD***",
193                typestr, trial.data, cksum);
194     }
195 }
196
197 int
198 main(void)
199 {
200 #if 0
201     timetest(64*1024, 1024);
202 #endif
203     verify();
204     exit(0);
205 }