summaryrefslogtreecommitdiff
path: root/src/lib/hash.c
diff options
context:
space:
mode:
authorDimitri Staessens <dimitri@ouroboros.rocks>2026-04-26 07:38:43 +0200
committerSander Vrijders <sander@ouroboros.rocks>2026-05-06 09:04:48 +0200
commit964c26eb326dc26c032a3ccba10cb6d376bb3cf4 (patch)
tree1bf4a80f81f5d69afc17a4950248771d60aeb030 /src/lib/hash.c
parent20d4a472800cbc9338f0c6c9c3dfce8eb13663c7 (diff)
downloadouroboros-964c26eb326dc26c032a3ccba10cb6d376bb3cf4.tar.gz
ouroboros-964c26eb326dc26c032a3ccba10cb6d376bb3cf4.zip
lib: Add CRC-8 and CRC-16 checksums
These checksum will be handy for header checksums. Signed-off-by: Dimitri Staessens <dimitri@ouroboros.rocks> Signed-off-by: Sander Vrijders <sander@ouroboros.rocks>
Diffstat (limited to 'src/lib/hash.c')
-rw-r--r--src/lib/hash.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/lib/hash.c b/src/lib/hash.c
index f32001d9..7ffa5bc1 100644
--- a/src/lib/hash.c
+++ b/src/lib/hash.c
@@ -39,6 +39,8 @@
#include <ouroboros/md5.h>
#include <ouroboros/sha3.h>
#endif
+#include <ouroboros/crc8.h>
+#include <ouroboros/crc16.h>
#include <ouroboros/crc64.h>
#include <string.h>
#include <assert.h>
@@ -70,6 +72,10 @@ int hash_len_tbl [] = {
uint16_t hash_len(enum hash_algo algo)
{
+ if (algo == HASH_CRC8)
+ return CRC8_HASH_LEN;
+ if (algo == HASH_CRC16)
+ return CRC16_HASH_LEN;
if (algo == HASH_CRC64)
return CRC64_HASH_LEN;
#ifdef HAVE_LIBGCRYPT
@@ -84,6 +90,20 @@ void mem_hash(enum hash_algo algo,
const uint8_t * buf,
size_t len)
{
+ if (algo == HASH_CRC8) {
+ uint8_t crc = 0;
+
+ crc8_autosar(&crc, buf, len);
+ *(uint8_t *) dst = crc;
+ return;
+ }
+ if (algo == HASH_CRC16) {
+ uint16_t crc = 0;
+
+ crc16_ccitt_false(&crc, buf, len);
+ *(uint16_t *) dst = htobe16(crc);
+ return;
+ }
if (algo == HASH_CRC64) {
uint64_t crc = 0;