summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkleines Filmröllchen <filmroellchen@serenityos.org>2023-05-18 15:57:19 +0200
committerJelle Raaijmakers <jelle@gmta.nl>2023-05-18 22:23:15 +0200
commitdaf50ed88521dc6e61cd80e57dc00f6a3d3d13bf (patch)
tree42fdbe46b37b7fc854a4c40b213b3fefe4a2543e
parent4f9c91e34d22174b0a587fe598cde78cb1185b7a (diff)
downloadserenity-daf50ed88521dc6e61cd80e57dc00f6a3d3d13bf.zip
LibCrypto: Add generic 8-bit CRC
The implementation of this is naive enough so it can handle all 8-bit CRC polynomials, of which there are quite a few. The table generation and update procedure is MSB first, which is backwards from the LSB first method of CRC32.
-rw-r--r--Userland/Libraries/LibCrypto/Checksum/CRC8.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCrypto/Checksum/CRC8.h b/Userland/Libraries/LibCrypto/Checksum/CRC8.h
new file mode 100644
index 0000000000..8850162c97
--- /dev/null
+++ b/Userland/Libraries/LibCrypto/Checksum/CRC8.h
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Types.h>
+#include <LibCrypto/Checksum/ChecksumFunction.h>
+
+namespace Crypto::Checksum {
+
+// A generic 8-bit Cyclic Redundancy Check.
+// Note that as opposed to CRC32, this class operates with MSB first, so the polynomial must not be reversed.
+// For example, the polynomial x⁸ + x² + x + 1 is represented as 0x07 and not 0xE0.
+template<u8 polynomial>
+class CRC8 : public ChecksumFunction<u8> {
+public:
+ // This is a big endian table, while CRC-32 uses a little endian table.
+ static constexpr auto generate_table()
+ {
+ Array<u8, 256> data {};
+ u8 value = 0x80;
+ auto i = 1u;
+ do {
+ if ((value & 0x80) != 0) {
+ value = polynomial ^ (value << 1);
+ } else {
+ value = value << 1;
+ }
+
+ for (auto j = 0u; j < i; ++j) {
+ data[i + j] = value ^ data[j];
+ }
+ i <<= 1;
+ } while (i < 256);
+ return data;
+ }
+
+ static constexpr auto table = generate_table();
+
+ virtual ~CRC8() = default;
+
+ CRC8() = default;
+ CRC8(ReadonlyBytes data)
+ {
+ update(data);
+ }
+
+ CRC8(u8 initial_state, ReadonlyBytes data)
+ : m_state(initial_state)
+ {
+ update(data);
+ }
+
+ // FIXME: This implementation is naive and slow.
+ // Figure out how to adopt the slicing-by-8 algorithm (see CRC32) for 8 bit polynomials.
+ virtual void update(ReadonlyBytes data) override
+ {
+ for (size_t i = 0; i < data.size(); i++) {
+ size_t table_index = (m_state ^ data.at(i)) & 0xFF;
+ m_state = (table[table_index] ^ (static_cast<u32>(m_state) << 8)) & 0xFF;
+ }
+ }
+
+ virtual u8 digest() override
+ {
+ return m_state;
+ }
+
+private:
+ u8 m_state { 0 };
+};
+
+}