blob: 0f45fbff34cb970b68dd4642eb30b67d3857a31e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
3 * Nicer crc32 functions/docs submitted by linux@horizon.com. Thanks!
4 * Code was from the public domain, copyright abandoned. Code was
5 * subsequently included in the kernel, thus was re-licensed under the
6 * GNU GPL v2.
7 *
8 * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
9 * Same crc32 function was used in 5 other places in the kernel.
10 * I made one version, and deleted the others.
11 * There are various incantations of crc32(). Some use a seed of 0 or ~0.
12 * Some xor at the end with ~0. The generic crc32() function takes
13 * seed as an argument, and doesn't xor at the end. Then individual
14 * users can do whatever they need.
15 * drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
16 * fs/jffs2 uses seed 0, doesn't xor with ~0.
17 * fs/partitions/efi.c uses seed ~0, xor's with ~0.
18 *
19 * This source code is licensed under the GNU General Public License,
20 * Version 2. See the file COPYING for more details.
21 */
22
23#include <linux/crc32.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/compiler.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/init.h>
30#include <asm/atomic.h>
31#include "crc32defs.h"
32#if CRC_LE_BITS == 8
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080033# define tole(x) __constant_cpu_to_le32(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#else
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080035# define tole(x) (x)
36#endif
37
38#if CRC_BE_BITS == 8
39# define tobe(x) __constant_cpu_to_be32(x)
40#else
41# define tobe(x) (x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#endif
43#include "crc32table.h"
44
45MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
46MODULE_DESCRIPTION("Ethernet CRC32 calculations");
47MODULE_LICENSE("GPL");
48
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080049#if CRC_LE_BITS == 8 || CRC_BE_BITS == 8
50
51static inline u32
52crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 *tab)
53{
54# ifdef __LITTLE_ENDIAN
55# define DO_CRC(x) crc = tab[(crc ^ (x)) & 255 ] ^ (crc >> 8)
56# else
57# define DO_CRC(x) crc = tab[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
58# endif
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080059 const u32 *b;
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080060 size_t rem_len;
61
62 /* Align it */
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080063 if (unlikely((long)buf & 3 && len)) {
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080064 do {
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080065 DO_CRC(*buf++);
66 } while ((--len) && ((long)buf)&3);
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080067 }
68 rem_len = len & 3;
69 /* load data 32 bits wide, xor data 32 bits wide. */
70 len = len >> 2;
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080071 b = (const u32 *)buf;
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080072 for (--b; len; --len) {
73 crc ^= *++b; /* use pre increment for speed */
74 DO_CRC(0);
75 DO_CRC(0);
76 DO_CRC(0);
77 DO_CRC(0);
78 }
79 len = rem_len;
80 /* And the last few bytes */
81 if (len) {
82 u8 *p = (u8 *)(b + 1) - 1;
83 do {
84 DO_CRC(*++p); /* use pre increment for speed */
85 } while (--len);
86 }
87 return crc;
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080088#undef DO_CRC
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080089}
90#endif
Randy Dunlap2f721002006-06-25 05:48:59 -070091/**
92 * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
93 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
94 * other uses, or the previous crc32 value if computing incrementally.
95 * @p: pointer to buffer over which CRC is run
96 * @len: length of buffer @p
97 */
Ralf Baechlee8c44312007-10-18 03:07:07 -070098u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len);
Randy Dunlap2f721002006-06-25 05:48:59 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#if CRC_LE_BITS == 1
101/*
102 * In fact, the table-based code will work in this case, but it can be
103 * simplified by inlining the table in ?: form.
104 */
105
Ralf Baechlee8c44312007-10-18 03:07:07 -0700106u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 int i;
109 while (len--) {
110 crc ^= *p++;
111 for (i = 0; i < 8; i++)
112 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
113 }
114 return crc;
115}
116#else /* Table-based approach */
117
Ralf Baechlee8c44312007-10-18 03:07:07 -0700118u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120# if CRC_LE_BITS == 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 const u32 *tab = crc32table_le;
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 crc = __cpu_to_le32(crc);
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800124 crc = crc32_body(crc, p, len, tab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return __le32_to_cpu(crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126# elif CRC_LE_BITS == 4
127 while (len--) {
128 crc ^= *p++;
129 crc = (crc >> 4) ^ crc32table_le[crc & 15];
130 crc = (crc >> 4) ^ crc32table_le[crc & 15];
131 }
132 return crc;
133# elif CRC_LE_BITS == 2
134 while (len--) {
135 crc ^= *p++;
136 crc = (crc >> 2) ^ crc32table_le[crc & 3];
137 crc = (crc >> 2) ^ crc32table_le[crc & 3];
138 crc = (crc >> 2) ^ crc32table_le[crc & 3];
139 crc = (crc >> 2) ^ crc32table_le[crc & 3];
140 }
141 return crc;
142# endif
143}
144#endif
145
Randy Dunlap2f721002006-06-25 05:48:59 -0700146/**
147 * crc32_be() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
148 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
149 * other uses, or the previous crc32 value if computing incrementally.
150 * @p: pointer to buffer over which CRC is run
151 * @len: length of buffer @p
152 */
Ralf Baechlee8c44312007-10-18 03:07:07 -0700153u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len);
Randy Dunlap2f721002006-06-25 05:48:59 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#if CRC_BE_BITS == 1
156/*
157 * In fact, the table-based code will work in this case, but it can be
158 * simplified by inlining the table in ?: form.
159 */
160
Ralf Baechlee8c44312007-10-18 03:07:07 -0700161u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 int i;
164 while (len--) {
165 crc ^= *p++ << 24;
166 for (i = 0; i < 8; i++)
167 crc =
168 (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE :
169 0);
170 }
171 return crc;
172}
173
174#else /* Table-based approach */
Ralf Baechlee8c44312007-10-18 03:07:07 -0700175u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177# if CRC_BE_BITS == 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 const u32 *tab = crc32table_be;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 crc = __cpu_to_be32(crc);
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800181 crc = crc32_body(crc, p, len, tab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return __be32_to_cpu(crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183# elif CRC_BE_BITS == 4
184 while (len--) {
185 crc ^= *p++ << 24;
186 crc = (crc << 4) ^ crc32table_be[crc >> 28];
187 crc = (crc << 4) ^ crc32table_be[crc >> 28];
188 }
189 return crc;
190# elif CRC_BE_BITS == 2
191 while (len--) {
192 crc ^= *p++ << 24;
193 crc = (crc << 2) ^ crc32table_be[crc >> 30];
194 crc = (crc << 2) ^ crc32table_be[crc >> 30];
195 crc = (crc << 2) ^ crc32table_be[crc >> 30];
196 crc = (crc << 2) ^ crc32table_be[crc >> 30];
197 }
198 return crc;
199# endif
200}
201#endif
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203EXPORT_SYMBOL(crc32_le);
204EXPORT_SYMBOL(crc32_be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206/*
207 * A brief CRC tutorial.
208 *
209 * A CRC is a long-division remainder. You add the CRC to the message,
210 * and the whole thing (message+CRC) is a multiple of the given
211 * CRC polynomial. To check the CRC, you can either check that the
212 * CRC matches the recomputed value, *or* you can check that the
213 * remainder computed on the message+CRC is 0. This latter approach
214 * is used by a lot of hardware implementations, and is why so many
215 * protocols put the end-of-frame flag after the CRC.
216 *
217 * It's actually the same long division you learned in school, except that
218 * - We're working in binary, so the digits are only 0 and 1, and
219 * - When dividing polynomials, there are no carries. Rather than add and
220 * subtract, we just xor. Thus, we tend to get a bit sloppy about
221 * the difference between adding and subtracting.
222 *
223 * A 32-bit CRC polynomial is actually 33 bits long. But since it's
224 * 33 bits long, bit 32 is always going to be set, so usually the CRC
225 * is written in hex with the most significant bit omitted. (If you're
226 * familiar with the IEEE 754 floating-point format, it's the same idea.)
227 *
228 * Note that a CRC is computed over a string of *bits*, so you have
229 * to decide on the endianness of the bits within each byte. To get
230 * the best error-detecting properties, this should correspond to the
231 * order they're actually sent. For example, standard RS-232 serial is
232 * little-endian; the most significant bit (sometimes used for parity)
233 * is sent last. And when appending a CRC word to a message, you should
234 * do it in the right order, matching the endianness.
235 *
236 * Just like with ordinary division, the remainder is always smaller than
237 * the divisor (the CRC polynomial) you're dividing by. Each step of the
238 * division, you take one more digit (bit) of the dividend and append it
239 * to the current remainder. Then you figure out the appropriate multiple
240 * of the divisor to subtract to being the remainder back into range.
241 * In binary, it's easy - it has to be either 0 or 1, and to make the
242 * XOR cancel, it's just a copy of bit 32 of the remainder.
243 *
244 * When computing a CRC, we don't care about the quotient, so we can
245 * throw the quotient bit away, but subtract the appropriate multiple of
246 * the polynomial from the remainder and we're back to where we started,
247 * ready to process the next bit.
248 *
249 * A big-endian CRC written this way would be coded like:
250 * for (i = 0; i < input_bits; i++) {
251 * multiple = remainder & 0x80000000 ? CRCPOLY : 0;
252 * remainder = (remainder << 1 | next_input_bit()) ^ multiple;
253 * }
254 * Notice how, to get at bit 32 of the shifted remainder, we look
255 * at bit 31 of the remainder *before* shifting it.
256 *
257 * But also notice how the next_input_bit() bits we're shifting into
258 * the remainder don't actually affect any decision-making until
259 * 32 bits later. Thus, the first 32 cycles of this are pretty boring.
260 * Also, to add the CRC to a message, we need a 32-bit-long hole for it at
261 * the end, so we have to add 32 extra cycles shifting in zeros at the
262 * end of every message,
263 *
264 * So the standard trick is to rearrage merging in the next_input_bit()
265 * until the moment it's needed. Then the first 32 cycles can be precomputed,
266 * and merging in the final 32 zero bits to make room for the CRC can be
267 * skipped entirely.
268 * This changes the code to:
269 * for (i = 0; i < input_bits; i++) {
270 * remainder ^= next_input_bit() << 31;
271 * multiple = (remainder & 0x80000000) ? CRCPOLY : 0;
272 * remainder = (remainder << 1) ^ multiple;
273 * }
274 * With this optimization, the little-endian code is simpler:
275 * for (i = 0; i < input_bits; i++) {
276 * remainder ^= next_input_bit();
277 * multiple = (remainder & 1) ? CRCPOLY : 0;
278 * remainder = (remainder >> 1) ^ multiple;
279 * }
280 *
281 * Note that the other details of endianness have been hidden in CRCPOLY
282 * (which must be bit-reversed) and next_input_bit().
283 *
284 * However, as long as next_input_bit is returning the bits in a sensible
285 * order, we can actually do the merging 8 or more bits at a time rather
286 * than one bit at a time:
287 * for (i = 0; i < input_bytes; i++) {
288 * remainder ^= next_input_byte() << 24;
289 * for (j = 0; j < 8; j++) {
290 * multiple = (remainder & 0x80000000) ? CRCPOLY : 0;
291 * remainder = (remainder << 1) ^ multiple;
292 * }
293 * }
294 * Or in little-endian:
295 * for (i = 0; i < input_bytes; i++) {
296 * remainder ^= next_input_byte();
297 * for (j = 0; j < 8; j++) {
298 * multiple = (remainder & 1) ? CRCPOLY : 0;
299 * remainder = (remainder << 1) ^ multiple;
300 * }
301 * }
302 * If the input is a multiple of 32 bits, you can even XOR in a 32-bit
303 * word at a time and increase the inner loop count to 32.
304 *
305 * You can also mix and match the two loop styles, for example doing the
306 * bulk of a message byte-at-a-time and adding bit-at-a-time processing
307 * for any fractional bytes at the end.
308 *
309 * The only remaining optimization is to the byte-at-a-time table method.
310 * Here, rather than just shifting one bit of the remainder to decide
311 * in the correct multiple to subtract, we can shift a byte at a time.
312 * This produces a 40-bit (rather than a 33-bit) intermediate remainder,
313 * but again the multiple of the polynomial to subtract depends only on
314 * the high bits, the high 8 bits in this case.
315 *
Joe Perches643d1f72008-02-03 17:48:52 +0200316 * The multiple we need in that case is the low 32 bits of a 40-bit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 * value whose high 8 bits are given, and which is a multiple of the
318 * generator polynomial. This is simply the CRC-32 of the given
319 * one-byte message.
320 *
321 * Two more details: normally, appending zero bits to a message which
322 * is already a multiple of a polynomial produces a larger multiple of that
323 * polynomial. To enable a CRC to detect this condition, it's common to
324 * invert the CRC before appending it. This makes the remainder of the
325 * message+crc come out not as zero, but some fixed non-zero value.
326 *
327 * The same problem applies to zero bits prepended to the message, and
328 * a similar solution is used. Instead of starting with a remainder of
329 * 0, an initial remainder of all ones is used. As long as you start
330 * the same way on decoding, it doesn't make a difference.
331 */
332
333#ifdef UNITTEST
334
335#include <stdlib.h>
336#include <stdio.h>
337
338#if 0 /*Not used at present */
339static void
340buf_dump(char const *prefix, unsigned char const *buf, size_t len)
341{
342 fputs(prefix, stdout);
343 while (len--)
344 printf(" %02x", *buf++);
345 putchar('\n');
346
347}
348#endif
349
350static void bytereverse(unsigned char *buf, size_t len)
351{
352 while (len--) {
Akinobu Mita906d66d2006-12-08 02:36:25 -0800353 unsigned char x = bitrev8(*buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 *buf++ = x;
355 }
356}
357
358static void random_garbage(unsigned char *buf, size_t len)
359{
360 while (len--)
361 *buf++ = (unsigned char) random();
362}
363
364#if 0 /* Not used at present */
365static void store_le(u32 x, unsigned char *buf)
366{
367 buf[0] = (unsigned char) x;
368 buf[1] = (unsigned char) (x >> 8);
369 buf[2] = (unsigned char) (x >> 16);
370 buf[3] = (unsigned char) (x >> 24);
371}
372#endif
373
374static void store_be(u32 x, unsigned char *buf)
375{
376 buf[0] = (unsigned char) (x >> 24);
377 buf[1] = (unsigned char) (x >> 16);
378 buf[2] = (unsigned char) (x >> 8);
379 buf[3] = (unsigned char) x;
380}
381
382/*
383 * This checks that CRC(buf + CRC(buf)) = 0, and that
384 * CRC commutes with bit-reversal. This has the side effect
385 * of bytewise bit-reversing the input buffer, and returns
386 * the CRC of the reversed buffer.
387 */
388static u32 test_step(u32 init, unsigned char *buf, size_t len)
389{
390 u32 crc1, crc2;
391 size_t i;
392
393 crc1 = crc32_be(init, buf, len);
394 store_be(crc1, buf + len);
395 crc2 = crc32_be(init, buf, len + 4);
396 if (crc2)
397 printf("\nCRC cancellation fail: 0x%08x should be 0\n",
398 crc2);
399
400 for (i = 0; i <= len + 4; i++) {
401 crc2 = crc32_be(init, buf, i);
402 crc2 = crc32_be(crc2, buf + i, len + 4 - i);
403 if (crc2)
404 printf("\nCRC split fail: 0x%08x\n", crc2);
405 }
406
407 /* Now swap it around for the other test */
408
409 bytereverse(buf, len + 4);
Akinobu Mita906d66d2006-12-08 02:36:25 -0800410 init = bitrev32(init);
411 crc2 = bitrev32(crc1);
412 if (crc1 != bitrev32(crc2))
Dominik Hacklcfc646f2005-08-07 09:42:53 -0700413 printf("\nBit reversal fail: 0x%08x -> 0x%08x -> 0x%08x\n",
Akinobu Mita906d66d2006-12-08 02:36:25 -0800414 crc1, crc2, bitrev32(crc2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 crc1 = crc32_le(init, buf, len);
416 if (crc1 != crc2)
417 printf("\nCRC endianness fail: 0x%08x != 0x%08x\n", crc1,
418 crc2);
419 crc2 = crc32_le(init, buf, len + 4);
420 if (crc2)
421 printf("\nCRC cancellation fail: 0x%08x should be 0\n",
422 crc2);
423
424 for (i = 0; i <= len + 4; i++) {
425 crc2 = crc32_le(init, buf, i);
426 crc2 = crc32_le(crc2, buf + i, len + 4 - i);
427 if (crc2)
428 printf("\nCRC split fail: 0x%08x\n", crc2);
429 }
430
431 return crc1;
432}
433
434#define SIZE 64
435#define INIT1 0
436#define INIT2 0
437
438int main(void)
439{
440 unsigned char buf1[SIZE + 4];
441 unsigned char buf2[SIZE + 4];
442 unsigned char buf3[SIZE + 4];
443 int i, j;
444 u32 crc1, crc2, crc3;
445
446 for (i = 0; i <= SIZE; i++) {
447 printf("\rTesting length %d...", i);
448 fflush(stdout);
449 random_garbage(buf1, i);
450 random_garbage(buf2, i);
451 for (j = 0; j < i; j++)
452 buf3[j] = buf1[j] ^ buf2[j];
453
454 crc1 = test_step(INIT1, buf1, i);
455 crc2 = test_step(INIT2, buf2, i);
456 /* Now check that CRC(buf1 ^ buf2) = CRC(buf1) ^ CRC(buf2) */
457 crc3 = test_step(INIT1 ^ INIT2, buf3, i);
458 if (crc3 != (crc1 ^ crc2))
459 printf("CRC XOR fail: 0x%08x != 0x%08x ^ 0x%08x\n",
460 crc3, crc1, crc2);
461 }
462 printf("\nAll test complete. No failures expected.\n");
463 return 0;
464}
465
466#endif /* UNITTEST */