1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/crypto_tests/t_crc.c */
4 * Copyright 2002,2005 by the Massachusetts Institute of Technology.
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.
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.
28 * Sanity checks for CRC32.
30 #include <sys/times.h>
35 #include "crypto_int.h"
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}
107 #define NTRIALS (sizeof(trials) / sizeof(trials[0]))
111 timetest(unsigned int nblk, unsigned int blksiz)
115 struct tms before, after;
118 block = malloc(blksiz * nblk);
121 for (i = 0; i < blksiz * nblk; i++)
124 for (i = 0; i < nblk; i++) {
126 mit_crc32(block + i * blksiz, blksiz, &cksum);
130 printf("shift-8 implementation, %d blocks of %d bytes:\n",
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));
142 static void gethexstr(char *data, size_t *outlen, unsigned char *outbuf,
149 inlen = strlen(data);
151 for (cp = data; (size_t) (cp - data) < inlen; cp += 2) {
154 n = strtol(buf, NULL, 16);
155 outbuf[(*outlen)++] = n;
156 if (*outlen > buflen)
165 struct crc_trial trial;
166 unsigned char buf[4];
171 for (i = 0; i < NTRIALS; i++) {
173 switch (trial.type) {
175 len = strlen(trial.data);
178 mit_crc32(trial.data, len, &cksum);
182 gethexstr(trial.data, &len, buf, 4);
184 mit_crc32(buf, len, &cksum);
188 fprintf(stderr, "bad trial type %d\n", trial.type);
191 printf("%s: %s \"%s\" = 0x%08lx\n",
192 (trial.sum == cksum) ? "OK" : "***BAD***",
193 typestr, trial.data, cksum);
201 timetest(64*1024, 1024);