feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 1) // SPDX-License-Identifier: GPL-2.0
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 2) /*
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 3) * Normal 64-bit CRC calculation.
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 4) *
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 5) * This is a basic crc64 implementation following ECMA-182 specification,
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 6) * which can be found from,
d89775fc929c5 (Alexander A. Klimov 2020-08-11 18:34:50 -0700 7) * https://www.ecma-international.org/publications/standards/Ecma-182.htm
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 8) *
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 9) * Dr. Ross N. Williams has a great document to introduce the idea of CRC
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 10) * algorithm, here the CRC64 code is also inspired by the table-driven
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 11) * algorithm and detail example from this paper. This paper can be found
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 12) * from,
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 13) * http://www.ross.net/crc/download/crc_v3.txt
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 14) *
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 15) * crc64table[256] is the lookup table of a table-driven 64-bit CRC
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 16) * calculation, which is generated by gen_crc64table.c in kernel build
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 17) * time. The polynomial of crc64 arithmetic is from ECMA-182 specification
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 18) * as well, which is defined as,
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 19) *
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 20) * x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 +
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 21) * x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 +
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 22) * x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 +
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 23) * x^7 + x^4 + x + 1
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 24) *
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 25) * Copyright 2018 SUSE Linux.
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 26) * Author: Coly Li <colyli@suse.de>
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 27) */
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 28)
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 29) #include <linux/module.h>
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 30) #include <linux/types.h>
0e0c12316d8a6 (Ben Dooks (Codethink) 2020-01-24 01:01:35 +0800 31) #include <linux/crc64.h>
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 32) #include "crc64table.h"
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 33)
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 34) MODULE_DESCRIPTION("CRC64 calculations");
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 35) MODULE_LICENSE("GPL v2");
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 36)
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 37) /**
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 38) * crc64_be - Calculate bitwise big-endian ECMA-182 CRC64
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 39) * @crc: seed value for computation. 0 or (u64)~0 for a new CRC calculation,
415f0c835ba79 (YueHaibing 2021-06-04 20:01:39 -0700 40) * or the previous crc64 value if computing incrementally.
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 41) * @p: pointer to buffer over which CRC64 is run
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 42) * @len: length of buffer @p
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 43) */
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 44) u64 __pure crc64_be(u64 crc, const void *p, size_t len)
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 45) {
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 46) size_t i, t;
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 47)
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 48) const unsigned char *_p = p;
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 49)
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 50) for (i = 0; i < len; i++) {
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 51) t = ((crc >> 56) ^ (*_p++)) & 0xFF;
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 52) crc = crc64table[t] ^ (crc << 8);
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 53) }
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 54)
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 55) return crc;
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 56) }
feba04fd2cf8f (Coly Li 2018-08-21 21:57:11 -0700 57) EXPORT_SYMBOL_GPL(crc64_be);