18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Normal 64-bit CRC calculation.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This is a basic crc64 implementation following ECMA-182 specification,
68c2ecf20Sopenharmony_ci * which can be found from,
78c2ecf20Sopenharmony_ci * https://www.ecma-international.org/publications/standards/Ecma-182.htm
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Dr. Ross N. Williams has a great document to introduce the idea of CRC
108c2ecf20Sopenharmony_ci * algorithm, here the CRC64 code is also inspired by the table-driven
118c2ecf20Sopenharmony_ci * algorithm and detail example from this paper. This paper can be found
128c2ecf20Sopenharmony_ci * from,
138c2ecf20Sopenharmony_ci * http://www.ross.net/crc/download/crc_v3.txt
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * crc64table[256] is the lookup table of a table-driven 64-bit CRC
168c2ecf20Sopenharmony_ci * calculation, which is generated by gen_crc64table.c in kernel build
178c2ecf20Sopenharmony_ci * time. The polynomial of crc64 arithmetic is from ECMA-182 specification
188c2ecf20Sopenharmony_ci * as well, which is defined as,
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 +
218c2ecf20Sopenharmony_ci * x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 +
228c2ecf20Sopenharmony_ci * x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 +
238c2ecf20Sopenharmony_ci * x^7 + x^4 + x + 1
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * Copyright 2018 SUSE Linux.
268c2ecf20Sopenharmony_ci *   Author: Coly Li <colyli@suse.de>
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include <linux/module.h>
308c2ecf20Sopenharmony_ci#include <linux/types.h>
318c2ecf20Sopenharmony_ci#include <linux/crc64.h>
328c2ecf20Sopenharmony_ci#include "crc64table.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("CRC64 calculations");
358c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/**
388c2ecf20Sopenharmony_ci * crc64_be - Calculate bitwise big-endian ECMA-182 CRC64
398c2ecf20Sopenharmony_ci * @crc: seed value for computation. 0 or (u64)~0 for a new CRC calculation,
408c2ecf20Sopenharmony_ci	or the previous crc64 value if computing incrementally.
418c2ecf20Sopenharmony_ci * @p: pointer to buffer over which CRC64 is run
428c2ecf20Sopenharmony_ci * @len: length of buffer @p
438c2ecf20Sopenharmony_ci */
448c2ecf20Sopenharmony_ciu64 __pure crc64_be(u64 crc, const void *p, size_t len)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	size_t i, t;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	const unsigned char *_p = p;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
518c2ecf20Sopenharmony_ci		t = ((crc >> 56) ^ (*_p++)) & 0xFF;
528c2ecf20Sopenharmony_ci		crc = crc64table[t] ^ (crc << 8);
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	return crc;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crc64_be);
58