162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Normal 64-bit CRC calculation. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * This is a basic crc64 implementation following ECMA-182 specification, 662306a36Sopenharmony_ci * which can be found from, 762306a36Sopenharmony_ci * https://www.ecma-international.org/publications/standards/Ecma-182.htm 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * Dr. Ross N. Williams has a great document to introduce the idea of CRC 1062306a36Sopenharmony_ci * algorithm, here the CRC64 code is also inspired by the table-driven 1162306a36Sopenharmony_ci * algorithm and detail example from this paper. This paper can be found 1262306a36Sopenharmony_ci * from, 1362306a36Sopenharmony_ci * http://www.ross.net/crc/download/crc_v3.txt 1462306a36Sopenharmony_ci * 1562306a36Sopenharmony_ci * crc64table[256] is the lookup table of a table-driven 64-bit CRC 1662306a36Sopenharmony_ci * calculation, which is generated by gen_crc64table.c in kernel build 1762306a36Sopenharmony_ci * time. The polynomial of crc64 arithmetic is from ECMA-182 specification 1862306a36Sopenharmony_ci * as well, which is defined as, 1962306a36Sopenharmony_ci * 2062306a36Sopenharmony_ci * x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 + 2162306a36Sopenharmony_ci * x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 + 2262306a36Sopenharmony_ci * x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 + 2362306a36Sopenharmony_ci * x^7 + x^4 + x + 1 2462306a36Sopenharmony_ci * 2562306a36Sopenharmony_ci * crc64rocksoft[256] table is from the Rocksoft specification polynomial 2662306a36Sopenharmony_ci * defined as, 2762306a36Sopenharmony_ci * 2862306a36Sopenharmony_ci * x^64 + x^63 + x^61 + x^59 + x^58 + x^56 + x^55 + x^52 + x^49 + x^48 + x^47 + 2962306a36Sopenharmony_ci * x^46 + x^44 + x^41 + x^37 + x^36 + x^34 + x^32 + x^31 + x^28 + x^26 + x^23 + 3062306a36Sopenharmony_ci * x^22 + x^19 + x^16 + x^13 + x^12 + x^10 + x^9 + x^6 + x^4 + x^3 + 1 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * Copyright 2018 SUSE Linux. 3362306a36Sopenharmony_ci * Author: Coly Li <colyli@suse.de> 3462306a36Sopenharmony_ci */ 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci#include <linux/module.h> 3762306a36Sopenharmony_ci#include <linux/types.h> 3862306a36Sopenharmony_ci#include <linux/crc64.h> 3962306a36Sopenharmony_ci#include "crc64table.h" 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ciMODULE_DESCRIPTION("CRC64 calculations"); 4262306a36Sopenharmony_ciMODULE_LICENSE("GPL v2"); 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci/** 4562306a36Sopenharmony_ci * crc64_be - Calculate bitwise big-endian ECMA-182 CRC64 4662306a36Sopenharmony_ci * @crc: seed value for computation. 0 or (u64)~0 for a new CRC calculation, 4762306a36Sopenharmony_ci * or the previous crc64 value if computing incrementally. 4862306a36Sopenharmony_ci * @p: pointer to buffer over which CRC64 is run 4962306a36Sopenharmony_ci * @len: length of buffer @p 5062306a36Sopenharmony_ci */ 5162306a36Sopenharmony_ciu64 __pure crc64_be(u64 crc, const void *p, size_t len) 5262306a36Sopenharmony_ci{ 5362306a36Sopenharmony_ci size_t i, t; 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci const unsigned char *_p = p; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci for (i = 0; i < len; i++) { 5862306a36Sopenharmony_ci t = ((crc >> 56) ^ (*_p++)) & 0xFF; 5962306a36Sopenharmony_ci crc = crc64table[t] ^ (crc << 8); 6062306a36Sopenharmony_ci } 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci return crc; 6362306a36Sopenharmony_ci} 6462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(crc64_be); 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci/** 6762306a36Sopenharmony_ci * crc64_rocksoft_generic - Calculate bitwise Rocksoft CRC64 6862306a36Sopenharmony_ci * @crc: seed value for computation. 0 for a new CRC calculation, or the 6962306a36Sopenharmony_ci * previous crc64 value if computing incrementally. 7062306a36Sopenharmony_ci * @p: pointer to buffer over which CRC64 is run 7162306a36Sopenharmony_ci * @len: length of buffer @p 7262306a36Sopenharmony_ci */ 7362306a36Sopenharmony_ciu64 __pure crc64_rocksoft_generic(u64 crc, const void *p, size_t len) 7462306a36Sopenharmony_ci{ 7562306a36Sopenharmony_ci const unsigned char *_p = p; 7662306a36Sopenharmony_ci size_t i; 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci crc = ~crc; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci for (i = 0; i < len; i++) 8162306a36Sopenharmony_ci crc = (crc >> 8) ^ crc64rocksofttable[(crc & 0xff) ^ *_p++]; 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci return ~crc; 8462306a36Sopenharmony_ci} 8562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(crc64_rocksoft_generic); 86