2 * July 20, 2011 Bob Pearson <rpearson at systemfabricworks.com>
3 * added slice by 8 algorithm to the existing conventional and
4 * slice by 4 algorithms.
6 * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
7 * Nicer crc32 functions/docs submitted by linux@horizon.com. Thanks!
8 * Code was from the public domain, copyright abandoned. Code was
9 * subsequently included in the kernel, thus was re-licensed under the
12 * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
13 * Same crc32 function was used in 5 other places in the kernel.
14 * I made one version, and deleted the others.
15 * There are various incantations of crc32(). Some use a seed of 0 or ~0.
16 * Some xor at the end with ~0. The generic crc32() function takes
17 * seed as an argument, and doesn't xor at the end. Then individual
18 * users can do whatever they need.
19 * drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
20 * fs/jffs2 uses seed 0, doesn't xor with ~0.
21 * fs/partitions/efi.c uses seed ~0, xor's with ~0.
23 * This source code is licensed under the GNU General Public License,
24 * Version 2. See the file COPYING for more details.
26 #include <linux/crc32.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/compiler.h>
30 #include <linux/types.h>
31 #include <linux/init.h>
32 #include <linux/atomic.h>
33 #include "crc32defs.h"
36 # define tole(x) (__force u32) __constant_cpu_to_le32(x)
42 # define tobe(x) (__force u32) __constant_cpu_to_be32(x)
46 #include "crc32table.h"
48 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
49 MODULE_DESCRIPTION("Ethernet CRC32 calculations");
50 MODULE_LICENSE("GPL");
53 static inline u32
crc32_le_body(u32 crc
, u8
const *buf
, size_t len
)
57 int init_bytes
, end_bytes
;
63 crc
= (__force u32
) __cpu_to_le32(crc
);
67 p32
= (u32
*)(((uintptr_t)p8
+ 7) & ~7);
69 init_bytes
= (uintptr_t)p32
- (uintptr_t)p8
;
72 words
= (len
- init_bytes
) >> 3;
73 end_bytes
= (len
- init_bytes
) & 7;
76 p32
= (u32
*)(((uintptr_t)p8
+ 3) & ~3);
78 init_bytes
= (uintptr_t)p32
- (uintptr_t)p8
;
81 words
= (len
- init_bytes
) >> 2;
82 end_bytes
= (len
- init_bytes
) & 3;
85 for (i
= 0; i
< init_bytes
; i
++) {
86 #ifdef __LITTLE_ENDIAN
88 crc
= t0_le
[i0
] ^ (crc
>> 8);
90 i0
= *p8
++ ^ (crc
>> 24);
91 crc
= t0_le
[i0
] ^ (crc
<< 8);
95 for (i
= 0; i
< words
; i
++) {
96 #ifdef __LITTLE_ENDIAN
97 # if CRC_LE_BITS == 64
98 /* slice by 8 algorithm */
104 crc
= t7_le
[i3
] ^ t6_le
[i2
] ^ t5_le
[i1
] ^ t4_le
[i0
];
111 crc
^= t3_le
[i3
] ^ t2_le
[i2
] ^ t1_le
[i1
] ^ t0_le
[i0
];
113 /* slice by 4 algorithm */
119 crc
= t3_le
[i3
] ^ t2_le
[i2
] ^ t1_le
[i1
] ^ t0_le
[i0
];
122 # if CRC_LE_BITS == 64
128 crc
= t7_le
[i3
] ^ t6_le
[i2
] ^ t5_le
[i1
] ^ t4_le
[i0
];
135 crc
^= t3_le
[i3
] ^ t2_le
[i2
] ^ t1_le
[i1
] ^ t0_le
[i0
];
142 crc
= t3_le
[i3
] ^ t2_le
[i2
] ^ t1_le
[i1
] ^ t0_le
[i0
];
149 for (i
= 0; i
< end_bytes
; i
++) {
150 #ifdef __LITTLE_ENDIAN
152 crc
= t0_le
[i0
] ^ (crc
>> 8);
154 i0
= *p8
++ ^ (crc
>> 24);
155 crc
= t0_le
[i0
] ^ (crc
<< 8);
159 return __le32_to_cpu((__force __le32
)crc
);
164 static inline u32
crc32_be_body(u32 crc
, u8
const *buf
, size_t len
)
168 int init_bytes
, end_bytes
;
174 crc
= (__force u32
) __cpu_to_be32(crc
);
176 #if CRC_LE_BITS == 64
178 p32
= (u32
*)(((uintptr_t)p8
+ 7) & ~7);
180 init_bytes
= (uintptr_t)p32
- (uintptr_t)p8
;
181 if (init_bytes
> len
)
183 words
= (len
- init_bytes
) >> 3;
184 end_bytes
= (len
- init_bytes
) & 7;
187 p32
= (u32
*)(((uintptr_t)p8
+ 3) & ~3);
189 init_bytes
= (uintptr_t)p32
- (uintptr_t)p8
;
190 if (init_bytes
> len
)
192 words
= (len
- init_bytes
) >> 2;
193 end_bytes
= (len
- init_bytes
) & 3;
196 for (i
= 0; i
< init_bytes
; i
++) {
197 #ifdef __LITTLE_ENDIAN
199 crc
= t0_be
[i0
] ^ (crc
>> 8);
201 i0
= *p8
++ ^ (crc
>> 24);
202 crc
= t0_be
[i0
] ^ (crc
<< 8);
206 for (i
= 0; i
< words
; i
++) {
207 #ifdef __LITTLE_ENDIAN
208 # if CRC_LE_BITS == 64
209 /* slice by 8 algorithm */
215 crc
= t7_be
[i3
] ^ t6_be
[i2
] ^ t5_be
[i1
] ^ t4_be
[i0
];
222 crc
^= t3_be
[i3
] ^ t2_be
[i2
] ^ t1_be
[i1
] ^ t0_be
[i0
];
224 /* slice by 4 algorithm */
230 crc
= t3_be
[i3
] ^ t2_be
[i2
] ^ t1_be
[i1
] ^ t0_be
[i0
];
233 # if CRC_LE_BITS == 64
239 crc
= t7_be
[i3
] ^ t6_be
[i2
] ^ t5_be
[i1
] ^ t4_be
[i0
];
246 crc
^= t3_be
[i3
] ^ t2_be
[i2
] ^ t1_be
[i1
] ^ t0_be
[i0
];
253 crc
= t3_be
[i3
] ^ t2_be
[i2
] ^ t1_be
[i1
] ^ t0_be
[i0
];
260 for (i
= 0; i
< end_bytes
; i
++) {
261 #ifdef __LITTLE_ENDIAN
263 crc
= t0_be
[i0
] ^ (crc
>> 8);
265 i0
= *p8
++ ^ (crc
>> 24);
266 crc
= t0_be
[i0
] ^ (crc
<< 8);
270 return __be32_to_cpu((__force __be32
)crc
);
275 * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
276 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
277 * other uses, or the previous crc32 value if computing incrementally.
278 * @p: pointer to buffer over which CRC is run
279 * @len: length of buffer @p
281 u32 __pure
crc32_le(u32 crc
, unsigned char const *p
, size_t len
)
287 for (i
= 0; i
< 8; i
++)
288 crc
= (crc
>> 1) ^ ((crc
& 1) ? CRCPOLY_LE
: 0);
290 # elif CRC_LE_BITS == 2
293 crc
= (crc
>> 2) ^ t0_le
[crc
& 0x03];
294 crc
= (crc
>> 2) ^ t0_le
[crc
& 0x03];
295 crc
= (crc
>> 2) ^ t0_le
[crc
& 0x03];
296 crc
= (crc
>> 2) ^ t0_le
[crc
& 0x03];
298 # elif CRC_LE_BITS == 4
301 crc
= (crc
>> 4) ^ t0_le
[crc
& 0x0f];
302 crc
= (crc
>> 4) ^ t0_le
[crc
& 0x0f];
304 # elif CRC_LE_BITS == 8
307 crc
= (crc
>> 8) ^ t0_le
[crc
& 0xff];
310 crc
= crc32_le_body(crc
, p
, len
);
314 EXPORT_SYMBOL(crc32_le
);
317 * crc32_be() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
318 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
319 * other uses, or the previous crc32 value if computing incrementally.
320 * @p: pointer to buffer over which CRC is run
321 * @len: length of buffer @p
323 u32 __pure
crc32_be(u32 crc
, unsigned char const *p
, size_t len
)
329 for (i
= 0; i
< 8; i
++)
331 ((crc
& 0x80000000) ? CRCPOLY_BE
: 0);
333 # elif CRC_BE_BITS == 2
336 crc
= (crc
<< 2) ^ t0_be
[crc
>> 30];
337 crc
= (crc
<< 2) ^ t0_be
[crc
>> 30];
338 crc
= (crc
<< 2) ^ t0_be
[crc
>> 30];
339 crc
= (crc
<< 2) ^ t0_be
[crc
>> 30];
341 # elif CRC_BE_BITS == 4
344 crc
= (crc
<< 4) ^ t0_be
[crc
>> 28];
345 crc
= (crc
<< 4) ^ t0_be
[crc
>> 28];
347 # elif CRC_BE_BITS == 8
350 crc
= (crc
<< 8) ^ t0_be
[crc
>> 24];
353 crc
= crc32_be_body(crc
, p
, len
);
357 EXPORT_SYMBOL(crc32_be
);
360 * A brief CRC tutorial.
362 * A CRC is a long-division remainder. You add the CRC to the message,
363 * and the whole thing (message+CRC) is a multiple of the given
364 * CRC polynomial. To check the CRC, you can either check that the
365 * CRC matches the recomputed value, *or* you can check that the
366 * remainder computed on the message+CRC is 0. This latter approach
367 * is used by a lot of hardware implementations, and is why so many
368 * protocols put the end-of-frame flag after the CRC.
370 * It's actually the same long division you learned in school, except that
371 * - We're working in binary, so the digits are only 0 and 1, and
372 * - When dividing polynomials, there are no carries. Rather than add and
373 * subtract, we just xor. Thus, we tend to get a bit sloppy about
374 * the difference between adding and subtracting.
376 * A 32-bit CRC polynomial is actually 33 bits long. But since it's
377 * 33 bits long, bit 32 is always going to be set, so usually the CRC
378 * is written in hex with the most significant bit omitted. (If you're
379 * familiar with the IEEE 754 floating-point format, it's the same idea.)
381 * Note that a CRC is computed over a string of *bits*, so you have
382 * to decide on the endianness of the bits within each byte. To get
383 * the best error-detecting properties, this should correspond to the
384 * order they're actually sent. For example, standard RS-232 serial is
385 * little-endian; the most significant bit (sometimes used for parity)
386 * is sent last. And when appending a CRC word to a message, you should
387 * do it in the right order, matching the endianness.
389 * Just like with ordinary division, the remainder is always smaller than
390 * the divisor (the CRC polynomial) you're dividing by. Each step of the
391 * division, you take one more digit (bit) of the dividend and append it
392 * to the current remainder. Then you figure out the appropriate multiple
393 * of the divisor to subtract to being the remainder back into range.
394 * In binary, it's easy - it has to be either 0 or 1, and to make the
395 * XOR cancel, it's just a copy of bit 32 of the remainder.
397 * When computing a CRC, we don't care about the quotient, so we can
398 * throw the quotient bit away, but subtract the appropriate multiple of
399 * the polynomial from the remainder and we're back to where we started,
400 * ready to process the next bit.
402 * A big-endian CRC written this way would be coded like:
403 * for (i = 0; i < input_bits; i++) {
404 * multiple = remainder & 0x80000000 ? CRCPOLY : 0;
405 * remainder = (remainder << 1 | next_input_bit()) ^ multiple;
407 * Notice how, to get at bit 32 of the shifted remainder, we look
408 * at bit 31 of the remainder *before* shifting it.
410 * But also notice how the next_input_bit() bits we're shifting into
411 * the remainder don't actually affect any decision-making until
412 * 32 bits later. Thus, the first 32 cycles of this are pretty boring.
413 * Also, to add the CRC to a message, we need a 32-bit-long hole for it at
414 * the end, so we have to add 32 extra cycles shifting in zeros at the
415 * end of every message,
417 * So the standard trick is to rearrage merging in the next_input_bit()
418 * until the moment it's needed. Then the first 32 cycles can be precomputed,
419 * and merging in the final 32 zero bits to make room for the CRC can be
421 * This changes the code to:
422 * for (i = 0; i < input_bits; i++) {
423 * remainder ^= next_input_bit() << 31;
424 * multiple = (remainder & 0x80000000) ? CRCPOLY : 0;
425 * remainder = (remainder << 1) ^ multiple;
427 * With this optimization, the little-endian code is simpler:
428 * for (i = 0; i < input_bits; i++) {
429 * remainder ^= next_input_bit();
430 * multiple = (remainder & 1) ? CRCPOLY : 0;
431 * remainder = (remainder >> 1) ^ multiple;
434 * Note that the other details of endianness have been hidden in CRCPOLY
435 * (which must be bit-reversed) and next_input_bit().
437 * However, as long as next_input_bit is returning the bits in a sensible
438 * order, we can actually do the merging 8 or more bits at a time rather
439 * than one bit at a time:
440 * for (i = 0; i < input_bytes; i++) {
441 * remainder ^= next_input_byte() << 24;
442 * for (j = 0; j < 8; j++) {
443 * multiple = (remainder & 0x80000000) ? CRCPOLY : 0;
444 * remainder = (remainder << 1) ^ multiple;
447 * Or in little-endian:
448 * for (i = 0; i < input_bytes; i++) {
449 * remainder ^= next_input_byte();
450 * for (j = 0; j < 8; j++) {
451 * multiple = (remainder & 1) ? CRCPOLY : 0;
452 * remainder = (remainder << 1) ^ multiple;
455 * If the input is a multiple of 32 bits, you can even XOR in a 32-bit
456 * word at a time and increase the inner loop count to 32.
458 * You can also mix and match the two loop styles, for example doing the
459 * bulk of a message byte-at-a-time and adding bit-at-a-time processing
460 * for any fractional bytes at the end.
462 * The only remaining optimization is to the byte-at-a-time table method.
463 * Here, rather than just shifting one bit of the remainder to decide
464 * in the correct multiple to subtract, we can shift a byte at a time.
465 * This produces a 40-bit (rather than a 33-bit) intermediate remainder,
466 * but again the multiple of the polynomial to subtract depends only on
467 * the high bits, the high 8 bits in this case.
469 * The multiple we need in that case is the low 32 bits of a 40-bit
470 * value whose high 8 bits are given, and which is a multiple of the
471 * generator polynomial. This is simply the CRC-32 of the given
474 * Two more details: normally, appending zero bits to a message which
475 * is already a multiple of a polynomial produces a larger multiple of that
476 * polynomial. To enable a CRC to detect this condition, it's common to
477 * invert the CRC before appending it. This makes the remainder of the
478 * message+crc come out not as zero, but some fixed non-zero value.
480 * The same problem applies to zero bits prepended to the message, and
481 * a similar solution is used. Instead of starting with a remainder of
482 * 0, an initial remainder of all ones is used. As long as you start
483 * the same way on decoding, it doesn't make a difference.
491 #if 0 /*Not used at present */
493 buf_dump(char const *prefix
, unsigned char const *buf
, size_t len
)
495 fputs(prefix
, stdout
);
497 printf(" %02x", *buf
++);
503 static void bytereverse(unsigned char *buf
, size_t len
)
506 unsigned char x
= bitrev8(*buf
);
511 static void random_garbage(unsigned char *buf
, size_t len
)
514 *buf
++ = (unsigned char) random();
517 #if 0 /* Not used at present */
518 static void store_le(u32 x
, unsigned char *buf
)
520 buf
[0] = (unsigned char) x
;
521 buf
[1] = (unsigned char) (x
>> 8);
522 buf
[2] = (unsigned char) (x
>> 16);
523 buf
[3] = (unsigned char) (x
>> 24);
527 static void store_be(u32 x
, unsigned char *buf
)
529 buf
[0] = (unsigned char) (x
>> 24);
530 buf
[1] = (unsigned char) (x
>> 16);
531 buf
[2] = (unsigned char) (x
>> 8);
532 buf
[3] = (unsigned char) x
;
536 * This checks that CRC(buf + CRC(buf)) = 0, and that
537 * CRC commutes with bit-reversal. This has the side effect
538 * of bytewise bit-reversing the input buffer, and returns
539 * the CRC of the reversed buffer.
541 static u32
test_step(u32 init
, unsigned char *buf
, size_t len
)
546 crc1
= crc32_be(init
, buf
, len
);
547 store_be(crc1
, buf
+ len
);
548 crc2
= crc32_be(init
, buf
, len
+ 4);
550 printf("\nCRC cancellation fail: 0x%08x should be 0\n",
553 for (i
= 0; i
<= len
+ 4; i
++) {
554 crc2
= crc32_be(init
, buf
, i
);
555 crc2
= crc32_be(crc2
, buf
+ i
, len
+ 4 - i
);
557 printf("\nCRC split fail: 0x%08x\n", crc2
);
560 /* Now swap it around for the other test */
562 bytereverse(buf
, len
+ 4);
563 init
= bitrev32(init
);
564 crc2
= bitrev32(crc1
);
565 if (crc1
!= bitrev32(crc2
))
566 printf("\nBit reversal fail: 0x%08x -> 0x%08x -> 0x%08x\n",
567 crc1
, crc2
, bitrev32(crc2
));
568 crc1
= crc32_le(init
, buf
, len
);
570 printf("\nCRC endianness fail: 0x%08x != 0x%08x\n", crc1
,
572 crc2
= crc32_le(init
, buf
, len
+ 4);
574 printf("\nCRC cancellation fail: 0x%08x should be 0\n",
577 for (i
= 0; i
<= len
+ 4; i
++) {
578 crc2
= crc32_le(init
, buf
, i
);
579 crc2
= crc32_le(crc2
, buf
+ i
, len
+ 4 - i
);
581 printf("\nCRC split fail: 0x%08x\n", crc2
);
593 unsigned char buf1
[SIZE
+ 4];
594 unsigned char buf2
[SIZE
+ 4];
595 unsigned char buf3
[SIZE
+ 4];
597 u32 crc1
, crc2
, crc3
;
599 for (i
= 0; i
<= SIZE
; i
++) {
600 printf("\rTesting length %d...", i
);
602 random_garbage(buf1
, i
);
603 random_garbage(buf2
, i
);
604 for (j
= 0; j
< i
; j
++)
605 buf3
[j
] = buf1
[j
] ^ buf2
[j
];
607 crc1
= test_step(INIT1
, buf1
, i
);
608 crc2
= test_step(INIT2
, buf2
, i
);
609 /* Now check that CRC(buf1 ^ buf2) = CRC(buf1) ^ CRC(buf2) */
610 crc3
= test_step(INIT1
^ INIT2
, buf3
, i
);
611 if (crc3
!= (crc1
^ crc2
))
612 printf("CRC XOR fail: 0x%08x != 0x%08x ^ 0x%08x\n",
615 printf("\nAll test complete. No failures expected.\n");
619 #endif /* UNITTEST */