xref: /linux/lib/crc64.c (revision cbc0a40e)
1feba04fdSColy Li // SPDX-License-Identifier: GPL-2.0
2feba04fdSColy Li /*
3feba04fdSColy Li  * Normal 64-bit CRC calculation.
4feba04fdSColy Li  *
5feba04fdSColy Li  * This is a basic crc64 implementation following ECMA-182 specification,
6feba04fdSColy Li  * which can be found from,
7d89775fcSAlexander A. Klimov  * https://www.ecma-international.org/publications/standards/Ecma-182.htm
8feba04fdSColy Li  *
9feba04fdSColy Li  * Dr. Ross N. Williams has a great document to introduce the idea of CRC
10feba04fdSColy Li  * algorithm, here the CRC64 code is also inspired by the table-driven
11feba04fdSColy Li  * algorithm and detail example from this paper. This paper can be found
12feba04fdSColy Li  * from,
13feba04fdSColy Li  * http://www.ross.net/crc/download/crc_v3.txt
14feba04fdSColy Li  *
15feba04fdSColy Li  * crc64table[256] is the lookup table of a table-driven 64-bit CRC
16feba04fdSColy Li  * calculation, which is generated by gen_crc64table.c in kernel build
17feba04fdSColy Li  * time. The polynomial of crc64 arithmetic is from ECMA-182 specification
18feba04fdSColy Li  * as well, which is defined as,
19feba04fdSColy Li  *
20feba04fdSColy Li  * x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 +
21feba04fdSColy Li  * x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 +
22feba04fdSColy Li  * x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 +
23feba04fdSColy Li  * x^7 + x^4 + x + 1
24feba04fdSColy Li  *
25cbc0a40eSKeith Busch  * crc64rocksoft[256] table is from the Rocksoft specification polynomial
26cbc0a40eSKeith Busch  * defined as,
27cbc0a40eSKeith Busch  *
28cbc0a40eSKeith Busch  * x^64 + x^63 + x^61 + x^59 + x^58 + x^56 + x^55 + x^52 + x^49 + x^48 + x^47 +
29cbc0a40eSKeith Busch  * x^46 + x^44 + x^41 + x^37 + x^36 + x^34 + x^32 + x^31 + x^28 + x^26 + x^23 +
30cbc0a40eSKeith Busch  * x^22 + x^19 + x^16 + x^13 + x^12 + x^10 + x^9 + x^6 + x^4 + x^3 + 1
31cbc0a40eSKeith Busch  *
32feba04fdSColy Li  * Copyright 2018 SUSE Linux.
33feba04fdSColy Li  *   Author: Coly Li <colyli@suse.de>
34feba04fdSColy Li  */
35feba04fdSColy Li 
36feba04fdSColy Li #include <linux/module.h>
37feba04fdSColy Li #include <linux/types.h>
380e0c1231SBen Dooks (Codethink) #include <linux/crc64.h>
39feba04fdSColy Li #include "crc64table.h"
40feba04fdSColy Li 
41feba04fdSColy Li MODULE_DESCRIPTION("CRC64 calculations");
42feba04fdSColy Li MODULE_LICENSE("GPL v2");
43feba04fdSColy Li 
44feba04fdSColy Li /**
45feba04fdSColy Li  * crc64_be - Calculate bitwise big-endian ECMA-182 CRC64
46feba04fdSColy Li  * @crc: seed value for computation. 0 or (u64)~0 for a new CRC calculation,
47415f0c83SYueHaibing  *       or the previous crc64 value if computing incrementally.
48feba04fdSColy Li  * @p: pointer to buffer over which CRC64 is run
49feba04fdSColy Li  * @len: length of buffer @p
50feba04fdSColy Li  */
crc64_be(u64 crc,const void * p,size_t len)51feba04fdSColy Li u64 __pure crc64_be(u64 crc, const void *p, size_t len)
52feba04fdSColy Li {
53feba04fdSColy Li 	size_t i, t;
54feba04fdSColy Li 
55feba04fdSColy Li 	const unsigned char *_p = p;
56feba04fdSColy Li 
57feba04fdSColy Li 	for (i = 0; i < len; i++) {
58feba04fdSColy Li 		t = ((crc >> 56) ^ (*_p++)) & 0xFF;
59feba04fdSColy Li 		crc = crc64table[t] ^ (crc << 8);
60feba04fdSColy Li 	}
61feba04fdSColy Li 
62feba04fdSColy Li 	return crc;
63feba04fdSColy Li }
64feba04fdSColy Li EXPORT_SYMBOL_GPL(crc64_be);
65cbc0a40eSKeith Busch 
66cbc0a40eSKeith Busch /**
67cbc0a40eSKeith Busch  * crc64_rocksoft_generic - Calculate bitwise Rocksoft CRC64
68cbc0a40eSKeith Busch  * @crc: seed value for computation. 0 for a new CRC calculation, or the
69cbc0a40eSKeith Busch  * 	 previous crc64 value if computing incrementally.
70cbc0a40eSKeith Busch  * @p: pointer to buffer over which CRC64 is run
71cbc0a40eSKeith Busch  * @len: length of buffer @p
72cbc0a40eSKeith Busch  */
crc64_rocksoft_generic(u64 crc,const void * p,size_t len)73cbc0a40eSKeith Busch u64 __pure crc64_rocksoft_generic(u64 crc, const void *p, size_t len)
74cbc0a40eSKeith Busch {
75cbc0a40eSKeith Busch 	const unsigned char *_p = p;
76cbc0a40eSKeith Busch 	size_t i;
77cbc0a40eSKeith Busch 
78cbc0a40eSKeith Busch 	crc = ~crc;
79cbc0a40eSKeith Busch 
80cbc0a40eSKeith Busch 	for (i = 0; i < len; i++)
81cbc0a40eSKeith Busch 		crc = (crc >> 8) ^ crc64rocksofttable[(crc & 0xff) ^ *_p++];
82cbc0a40eSKeith Busch 
83cbc0a40eSKeith Busch 	return ~crc;
84cbc0a40eSKeith Busch }
85cbc0a40eSKeith Busch EXPORT_SYMBOL_GPL(crc64_rocksoft_generic);
86