summaryrefslogtreecommitdiffstats
path: root/src/lib/crypto/crc32/crc.c
blob: f80e553512505d620d7a4ea0d7d151118d191e68 (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
/*
 * $Source$
 * $Author$
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <krb5/copyright.h>.
 *
 * CRC-32 routines
 */

#if !defined(lint) && !defined(SABER)
static char rcsid_crc_c[] =
"$Id$";
#endif	/* !lint & !SABER */

#include <krb5/copyright.h>
#include <krb5/krb5.h>
#include <krb5/crc-32.h>

static u_long const crc_table[256] = {
    0x00000000, 0x01080082, 0x02100104, 0x03180186,
    0x04200208, 0x0528028a, 0x0630030c, 0x0738038e,
    0x08400410, 0x09480492, 0x0a500514, 0x0b580596,
    0x0c600618, 0x0d68069a, 0x0e70071c, 0x0f78079e,
    0x10800820, 0x118808a2, 0x12900924, 0x139809a6,
    0x14a00a28, 0x15a80aaa, 0x16b00b2c, 0x17b80bae,
    0x18c00c30, 0x19c80cb2, 0x1ad00d34, 0x1bd80db6,
    0x1ce00e38, 0x1de80eba, 0x1ef00f3c, 0x1ff80fbe,
    0x21001040, 0x200810c2, 0x23101144, 0x221811c6,
    0x25201248, 0x242812ca, 0x2730134c, 0x263813ce,
    0x29401450, 0x284814d2, 0x2b501554, 0x2a5815d6,
    0x2d601658, 0x2c6816da, 0x2f70175c, 0x2e7817de,
    0x31801860, 0x308818e2, 0x33901964, 0x329819e6,
    0x35a01a68, 0x34a81aea, 0x37b01b6c, 0x36b81bee,
    0x39c01c70, 0x38c81cf2, 0x3bd01d74, 0x3ad81df6,
    0x3de01e78, 0x3ce81efa, 0x3ff01f7c, 0x3ef81ffe,
    0x42002080, 0x43082002, 0x40102184, 0x41182106,
    0x46202288, 0x4728220a, 0x4430238c, 0x4538230e,
    0x4a402490, 0x4b482412, 0x48502594, 0x49582516,
    0x4e602698, 0x4f68261a, 0x4c70279c, 0x4d78271e,
    0x528028a0, 0x53882822, 0x509029a4, 0x51982926,
    0x56a02aa8, 0x57a82a2a, 0x54b02bac, 0x55b82b2e,
    0x5ac02cb0, 0x5bc82c32, 0x58d02db4, 0x59d82d36,
    0x5ee02eb8, 0x5fe82e3a, 0x5cf02fbc, 0x5df82f3e,
    0x630030c0, 0x62083042, 0x611031c4, 0x60183146,
    0x672032c8, 0x6628324a, 0x653033cc, 0x6438334e,
    0x6b4034d0, 0x6a483452, 0x695035d4, 0x68583556,
    0x6f6036d8, 0x6e68365a, 0x6d7037dc, 0x6c78375e,
    0x738038e0, 0x72883862, 0x719039e4, 0x70983966,
    0x77a03ae8, 0x76a83a6a, 0x75b03bec, 0x74b83b6e,
    0x7bc03cf0, 0x7ac83c72, 0x79d03df4, 0x78d83d76,
    0x7fe03ef8, 0x7ee83e7a, 0x7df03ffc, 0x7cf83f7e,
    0x84004100, 0x85084182, 0x86104004, 0x87184086,
    0x80204308, 0x8128438a, 0x8230420c, 0x8338428e,
    0x8c404510, 0x8d484592, 0x8e504414, 0x8f584496,
    0x88604718, 0x8968479a, 0x8a70461c, 0x8b78469e,
    0x94804920, 0x958849a2, 0x96904824, 0x979848a6,
    0x90a04b28, 0x91a84baa, 0x92b04a2c, 0x93b84aae,
    0x9cc04d30, 0x9dc84db2, 0x9ed04c34, 0x9fd84cb6,
    0x98e04f38, 0x99e84fba, 0x9af04e3c, 0x9bf84ebe,
    0xa5005140, 0xa40851c2, 0xa7105044, 0xa61850c6,
    0xa1205348, 0xa02853ca, 0xa330524c, 0xa23852ce,
    0xad405550, 0xac4855d2, 0xaf505454, 0xae5854d6,
    0xa9605758, 0xa86857da, 0xab70565c, 0xaa7856de,
    0xb5805960, 0xb48859e2, 0xb7905864, 0xb69858e6,
    0xb1a05b68, 0xb0a85bea, 0xb3b05a6c, 0xb2b85aee,
    0xbdc05d70, 0xbcc85df2, 0xbfd05c74, 0xbed85cf6,
    0xb9e05f78, 0xb8e85ffa, 0xbbf05e7c, 0xbaf85efe,
    0xc6006180, 0xc7086102, 0xc4106084, 0xc5186006,
    0xc2206388, 0xc328630a, 0xc030628c, 0xc138620e,
    0xce406590, 0xcf486512, 0xcc506494, 0xcd586416,
    0xca606798, 0xcb68671a, 0xc870669c, 0xc978661e,
    0xd68069a0, 0xd7886922, 0xd49068a4, 0xd5986826,
    0xd2a06ba8, 0xd3a86b2a, 0xd0b06aac, 0xd1b86a2e,
    0xdec06db0, 0xdfc86d32, 0xdcd06cb4, 0xddd86c36,
    0xdae06fb8, 0xdbe86f3a, 0xd8f06ebc, 0xd9f86e3e,
    0xe70071c0, 0xe6087142, 0xe51070c4, 0xe4187046,
    0xe32073c8, 0xe228734a, 0xe13072cc, 0xe038724e,
    0xef4075d0, 0xee487552, 0xed5074d4, 0xec587456,
    0xeb6077d8, 0xea68775a, 0xe97076dc, 0xe878765e,
    0xf78079e0, 0xf6887962, 0xf59078e4, 0xf4987866,
    0xf3a07be8, 0xf2a87b6a, 0xf1b07aec, 0xf0b87a6e,
    0xffc07df0, 0xfec87d72, 0xfdd07cf4, 0xfcd87c76,
    0xfbe07ff8, 0xfae87f7a, 0xf9f07efc, 0xf8f87e7e
    };

static krb5_error_code
crc32_sum_func(in, out, seed, in_length, seed_length, outcksum)
krb5_pointer in;
krb5_pointer out;
krb5_pointer seed;
size_t in_length;
size_t seed_length;
krb5_checksum *outcksum;
{
    register u_char *data = (u_char *)in;
    register u_long c = 0;
    register int idx;
    int i;

    for (i=0; i<in_length;i++) {
	idx = (data[i] ^ c);
	idx &= 0xff;
	c >>= 8;
	c ^= crc_table[idx];
    }
    /* c now holds the result */
    outcksum->checksum_type = CKSUMTYPE_CRC32;
    outcksum->length = 4;
    outcksum->contents[0] = c & 0xff;
    outcksum->contents[1] = (c >> 8) & 0xff;
    outcksum->contents[2] = (c >> 16) & 0xff;
    outcksum->contents[3] = (c >> 24) & 0xff;
    return 0;
}


krb5_checksum_entry crc32_cksumtable_entry = {
    &crc32_sum_func,
    4,					/* CRC-32 is 4 octets */
};