From 4cfc607ebbff840991d893a8c0fa3a004caeb416 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Sun, 26 Apr 2026 07:56:38 +0200 Subject: lib: Move CRC implementations to a subfolder Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- src/lib/CMakeLists.txt | 9 +- src/lib/crc/crc16.c | 64 +++++++ src/lib/crc/crc32.c | 84 +++++++++ src/lib/crc/crc64.c | 369 +++++++++++++++++++++++++++++++++++++++ src/lib/crc/crc8.c | 64 +++++++ src/lib/crc/tests/CMakeLists.txt | 21 +++ src/lib/crc/tests/crc16_test.c | 67 +++++++ src/lib/crc/tests/crc32_test.c | 72 ++++++++ src/lib/crc/tests/crc64_test.c | 126 +++++++++++++ src/lib/crc/tests/crc8_test.c | 67 +++++++ src/lib/crc16.c | 64 ------- src/lib/crc32.c | 84 --------- src/lib/crc64.c | 369 --------------------------------------- src/lib/crc8.c | 64 ------- src/lib/tests/CMakeLists.txt | 4 - src/lib/tests/crc16_test.c | 67 ------- src/lib/tests/crc32_test.c | 72 -------- src/lib/tests/crc64_test.c | 126 ------------- src/lib/tests/crc8_test.c | 67 ------- 19 files changed, 939 insertions(+), 921 deletions(-) create mode 100644 src/lib/crc/crc16.c create mode 100644 src/lib/crc/crc32.c create mode 100644 src/lib/crc/crc64.c create mode 100644 src/lib/crc/crc8.c create mode 100644 src/lib/crc/tests/CMakeLists.txt create mode 100644 src/lib/crc/tests/crc16_test.c create mode 100644 src/lib/crc/tests/crc32_test.c create mode 100644 src/lib/crc/tests/crc64_test.c create mode 100644 src/lib/crc/tests/crc8_test.c delete mode 100644 src/lib/crc16.c delete mode 100644 src/lib/crc32.c delete mode 100644 src/lib/crc64.c delete mode 100644 src/lib/crc8.c delete mode 100644 src/lib/tests/crc16_test.c delete mode 100644 src/lib/tests/crc32_test.c delete mode 100644 src/lib/tests/crc64_test.c delete mode 100644 src/lib/tests/crc8_test.c (limited to 'src') diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 74c3ac15..f68d3601 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -17,10 +17,10 @@ protobuf_generate_c(IPCP_PROTO_SRCS IPCP_PROTO_HDRS set(SOURCE_FILES_COMMON bitmap.c btree.c - crc8.c - crc16.c - crc32.c - crc64.c + crc/crc8.c + crc/crc16.c + crc/crc32.c + crc/crc64.c crypt.c hash.c lockfile.c @@ -158,5 +158,6 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/ssm/ssm.h.in" if(BUILD_TESTS) add_subdirectory(tests) + add_subdirectory(crc/tests) add_subdirectory(ssm/tests) endif() diff --git a/src/lib/crc/crc16.c b/src/lib/crc/crc16.c new file mode 100644 index 00000000..55af8647 --- /dev/null +++ b/src/lib/crc/crc16.c @@ -0,0 +1,64 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2026 + * + * 16-bit Cyclic Redundancy Check (CCITT-FALSE variant) + * + * Dimitri Staessens + * Sander Vrijders + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +/* + * CRC-16/CCITT-FALSE (reveng catalog, alias CRC-16/IBM-3740): + * poly = 0x1021 + * init = 0xffff + * refin = false + * refout = false + * xorout = 0x0000 + * check = crc16_ccitt_false("123456789") == 0x29b1 + */ + +#include "config.h" + +#include + +/* Bit-by-bit MSB-first CRC. Header-check use case rarely exceeds a + * few hundred bytes; a 256-entry uint16_t table costs 512 B of + * .rodata and is easy to add later if profiling demands it. + */ +void crc16_ccitt_false(uint16_t * crc, + const void * buf, + size_t len) +{ + const uint8_t * p; + uint16_t c; + size_t n; + int i; + + p = (const uint8_t *) buf; + c = *crc ^ 0xffff; + + for (n = 0; n < len; n++) { + c ^= ((uint16_t) p[n]) << 8; + for (i = 0; i < 8; i++) { + if (c & 0x8000) + c = (uint16_t) ((c << 1) ^ 0x1021); + else + c = (uint16_t) (c << 1); + } + } + + *crc = c; +} diff --git a/src/lib/crc/crc32.c b/src/lib/crc/crc32.c new file mode 100644 index 00000000..0fdb62b1 --- /dev/null +++ b/src/lib/crc/crc32.c @@ -0,0 +1,84 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2026 + * + * 32-bit Cyclic Redundancy Check + * + * Dimitri Staessens + * Sander Vrijders + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +#include + +static const uint32_t crc32_table[256] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d +}; + +void crc32(uint32_t * crc, + const void * buf, + size_t len) +{ + size_t n; + + *crc = *crc ^ 0xffffffff; + + for (n = 0; n < len; n++) + *crc = crc32_table[(*crc ^ ((uint8_t *) buf)[n]) & 0xff] + ^ (*crc >> 8); + + *crc = *crc ^ 0xffffffff; +} diff --git a/src/lib/crc/crc64.c b/src/lib/crc/crc64.c new file mode 100644 index 00000000..3ee5e798 --- /dev/null +++ b/src/lib/crc/crc64.c @@ -0,0 +1,369 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2026 + * + * 64-bit Cyclic Redundancy Check (NVMe variant) + * + * Dimitri Staessens + * Sander Vrijders + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +/* + * CRC-64/NVMe (reveng catalog): + * poly = 0xad93d23594c93659 + * init = 0xffffffffffffffff + * refin = true + * refout = true + * xorout = 0xffffffffffffffff + * check = crc64_nvme("123456789") == 0xae8b14860a799888 + */ + +#include "config.h" + +#include + +/* Reflected CRC-64/NVMe table. Polynomial in reflected form: + * 0x9a6c9329ac4bc9b5 (bitrev of 0xad93d23594c93659). + */ +static const uint64_t crc64_nvme_tab[256] = { + 0x0000000000000000ULL, 0x7f6ef0c830358979ULL, + 0xfedde190606b12f2ULL, 0x81b31158505e9b8bULL, + 0xc962e5739841b68fULL, 0xb60c15bba8743ff6ULL, + 0x37bf04e3f82aa47dULL, 0x48d1f42bc81f2d04ULL, + 0xa61cecb46814fe75ULL, 0xd9721c7c5821770cULL, + 0x58c10d24087fec87ULL, 0x27affdec384a65feULL, + 0x6f7e09c7f05548faULL, 0x1010f90fc060c183ULL, + 0x91a3e857903e5a08ULL, 0xeecd189fa00bd371ULL, + 0x78e0ff3b88be6f81ULL, 0x078e0ff3b88be6f8ULL, + 0x863d1eabe8d57d73ULL, 0xf953ee63d8e0f40aULL, + 0xb1821a4810ffd90eULL, 0xceecea8020ca5077ULL, + 0x4f5ffbd87094cbfcULL, 0x30310b1040a14285ULL, + 0xdefc138fe0aa91f4ULL, 0xa192e347d09f188dULL, + 0x2021f21f80c18306ULL, 0x5f4f02d7b0f40a7fULL, + 0x179ef6fc78eb277bULL, 0x68f0063448deae02ULL, + 0xe943176c18803589ULL, 0x962de7a428b5bcf0ULL, + 0xf1c1fe77117cdf02ULL, 0x8eaf0ebf2149567bULL, + 0x0f1c1fe77117cdf0ULL, 0x7072ef2f41224489ULL, + 0x38a31b04893d698dULL, 0x47cdebccb908e0f4ULL, + 0xc67efa94e9567b7fULL, 0xb9100a5cd963f206ULL, + 0x57dd12c379682177ULL, 0x28b3e20b495da80eULL, + 0xa900f35319033385ULL, 0xd66e039b2936bafcULL, + 0x9ebff7b0e12997f8ULL, 0xe1d10778d11c1e81ULL, + 0x606216208142850aULL, 0x1f0ce6e8b1770c73ULL, + 0x8921014c99c2b083ULL, 0xf64ff184a9f739faULL, + 0x77fce0dcf9a9a271ULL, 0x08921014c99c2b08ULL, + 0x4043e43f0183060cULL, 0x3f2d14f731b68f75ULL, + 0xbe9e05af61e814feULL, 0xc1f0f56751dd9d87ULL, + 0x2f3dedf8f1d64ef6ULL, 0x50531d30c1e3c78fULL, + 0xd1e00c6891bd5c04ULL, 0xae8efca0a188d57dULL, + 0xe65f088b6997f879ULL, 0x9931f84359a27100ULL, + 0x1882e91b09fcea8bULL, 0x67ec19d339c963f2ULL, + 0xd75adabd7a6e2d6fULL, 0xa8342a754a5ba416ULL, + 0x29873b2d1a053f9dULL, 0x56e9cbe52a30b6e4ULL, + 0x1e383fcee22f9be0ULL, 0x6156cf06d21a1299ULL, + 0xe0e5de5e82448912ULL, 0x9f8b2e96b271006bULL, + 0x71463609127ad31aULL, 0x0e28c6c1224f5a63ULL, + 0x8f9bd7997211c1e8ULL, 0xf0f5275142244891ULL, + 0xb824d37a8a3b6595ULL, 0xc74a23b2ba0eececULL, + 0x46f932eaea507767ULL, 0x3997c222da65fe1eULL, + 0xafba2586f2d042eeULL, 0xd0d4d54ec2e5cb97ULL, + 0x5167c41692bb501cULL, 0x2e0934dea28ed965ULL, + 0x66d8c0f56a91f461ULL, 0x19b6303d5aa47d18ULL, + 0x980521650afae693ULL, 0xe76bd1ad3acf6feaULL, + 0x09a6c9329ac4bc9bULL, 0x76c839faaaf135e2ULL, + 0xf77b28a2faafae69ULL, 0x8815d86aca9a2710ULL, + 0xc0c42c4102850a14ULL, 0xbfaadc8932b0836dULL, + 0x3e19cdd162ee18e6ULL, 0x41773d1952db919fULL, + 0x269b24ca6b12f26dULL, 0x59f5d4025b277b14ULL, + 0xd846c55a0b79e09fULL, 0xa72835923b4c69e6ULL, + 0xeff9c1b9f35344e2ULL, 0x90973171c366cd9bULL, + 0x1124202993385610ULL, 0x6e4ad0e1a30ddf69ULL, + 0x8087c87e03060c18ULL, 0xffe938b633338561ULL, + 0x7e5a29ee636d1eeaULL, 0x0134d92653589793ULL, + 0x49e52d0d9b47ba97ULL, 0x368bddc5ab7233eeULL, + 0xb738cc9dfb2ca865ULL, 0xc8563c55cb19211cULL, + 0x5e7bdbf1e3ac9decULL, 0x21152b39d3991495ULL, + 0xa0a63a6183c78f1eULL, 0xdfc8caa9b3f20667ULL, + 0x97193e827bed2b63ULL, 0xe877ce4a4bd8a21aULL, + 0x69c4df121b863991ULL, 0x16aa2fda2bb3b0e8ULL, + 0xf86737458bb86399ULL, 0x8709c78dbb8deae0ULL, + 0x06bad6d5ebd3716bULL, 0x79d4261ddbe6f812ULL, + 0x3105d23613f9d516ULL, 0x4e6b22fe23cc5c6fULL, + 0xcfd833a67392c7e4ULL, 0xb0b6c36e43a74e9dULL, + 0x9a6c9329ac4bc9b5ULL, 0xe50263e19c7e40ccULL, + 0x64b172b9cc20db47ULL, 0x1bdf8271fc15523eULL, + 0x530e765a340a7f3aULL, 0x2c608692043ff643ULL, + 0xadd397ca54616dc8ULL, 0xd2bd67026454e4b1ULL, + 0x3c707f9dc45f37c0ULL, 0x431e8f55f46abeb9ULL, + 0xc2ad9e0da4342532ULL, 0xbdc36ec59401ac4bULL, + 0xf5129aee5c1e814fULL, 0x8a7c6a266c2b0836ULL, + 0x0bcf7b7e3c7593bdULL, 0x74a18bb60c401ac4ULL, + 0xe28c6c1224f5a634ULL, 0x9de29cda14c02f4dULL, + 0x1c518d82449eb4c6ULL, 0x633f7d4a74ab3dbfULL, + 0x2bee8961bcb410bbULL, 0x548079a98c8199c2ULL, + 0xd53368f1dcdf0249ULL, 0xaa5d9839ecea8b30ULL, + 0x449080a64ce15841ULL, 0x3bfe706e7cd4d138ULL, + 0xba4d61362c8a4ab3ULL, 0xc52391fe1cbfc3caULL, + 0x8df265d5d4a0eeceULL, 0xf29c951de49567b7ULL, + 0x732f8445b4cbfc3cULL, 0x0c41748d84fe7545ULL, + 0x6bad6d5ebd3716b7ULL, 0x14c39d968d029fceULL, + 0x95708ccedd5c0445ULL, 0xea1e7c06ed698d3cULL, + 0xa2cf882d2576a038ULL, 0xdda178e515432941ULL, + 0x5c1269bd451db2caULL, 0x237c997575283bb3ULL, + 0xcdb181ead523e8c2ULL, 0xb2df7122e51661bbULL, + 0x336c607ab548fa30ULL, 0x4c0290b2857d7349ULL, + 0x04d364994d625e4dULL, 0x7bbd94517d57d734ULL, + 0xfa0e85092d094cbfULL, 0x856075c11d3cc5c6ULL, + 0x134d926535897936ULL, 0x6c2362ad05bcf04fULL, + 0xed9073f555e26bc4ULL, 0x92fe833d65d7e2bdULL, + 0xda2f7716adc8cfb9ULL, 0xa54187de9dfd46c0ULL, + 0x24f29686cda3dd4bULL, 0x5b9c664efd965432ULL, + 0xb5517ed15d9d8743ULL, 0xca3f8e196da80e3aULL, + 0x4b8c9f413df695b1ULL, 0x34e26f890dc31cc8ULL, + 0x7c339ba2c5dc31ccULL, 0x035d6b6af5e9b8b5ULL, + 0x82ee7a32a5b7233eULL, 0xfd808afa9582aa47ULL, + 0x4d364994d625e4daULL, 0x3258b95ce6106da3ULL, + 0xb3eba804b64ef628ULL, 0xcc8558cc867b7f51ULL, + 0x8454ace74e645255ULL, 0xfb3a5c2f7e51db2cULL, + 0x7a894d772e0f40a7ULL, 0x05e7bdbf1e3ac9deULL, + 0xeb2aa520be311aafULL, 0x944455e88e0493d6ULL, + 0x15f744b0de5a085dULL, 0x6a99b478ee6f8124ULL, + 0x224840532670ac20ULL, 0x5d26b09b16452559ULL, + 0xdc95a1c3461bbed2ULL, 0xa3fb510b762e37abULL, + 0x35d6b6af5e9b8b5bULL, 0x4ab846676eae0222ULL, + 0xcb0b573f3ef099a9ULL, 0xb465a7f70ec510d0ULL, + 0xfcb453dcc6da3dd4ULL, 0x83daa314f6efb4adULL, + 0x0269b24ca6b12f26ULL, 0x7d0742849684a65fULL, + 0x93ca5a1b368f752eULL, 0xeca4aad306bafc57ULL, + 0x6d17bb8b56e467dcULL, 0x12794b4366d1eea5ULL, + 0x5aa8bf68aecec3a1ULL, 0x25c64fa09efb4ad8ULL, + 0xa4755ef8cea5d153ULL, 0xdb1bae30fe90582aULL, + 0xbcf7b7e3c7593bd8ULL, 0xc399472bf76cb2a1ULL, + 0x422a5673a732292aULL, 0x3d44a6bb9707a053ULL, + 0x759552905f188d57ULL, 0x0afba2586f2d042eULL, + 0x8b48b3003f739fa5ULL, 0xf42643c80f4616dcULL, + 0x1aeb5b57af4dc5adULL, 0x6585ab9f9f784cd4ULL, + 0xe436bac7cf26d75fULL, 0x9b584a0fff135e26ULL, + 0xd389be24370c7322ULL, 0xace74eec0739fa5bULL, + 0x2d545fb4576761d0ULL, 0x523aaf7c6752e8a9ULL, + 0xc41748d84fe75459ULL, 0xbb79b8107fd2dd20ULL, + 0x3acaa9482f8c46abULL, 0x45a459801fb9cfd2ULL, + 0x0d75adabd7a6e2d6ULL, 0x721b5d63e7936bafULL, + 0xf3a84c3bb7cdf024ULL, 0x8cc6bcf387f8795dULL, + 0x620ba46c27f3aa2cULL, 0x1d6554a417c62355ULL, + 0x9cd645fc4798b8deULL, 0xe3b8b53477ad31a7ULL, + 0xab69411fbfb21ca3ULL, 0xd407b1d78f8795daULL, + 0x55b4a08fdfd90e51ULL, 0x2ada5047efec8728ULL +}; + +void crc64_nvme_table(uint64_t * crc, + const void * buf, + size_t len) +{ + size_t n; + + *crc = *crc ^ 0xffffffffffffffffULL; + + for (n = 0; n < len; n++) + *crc = crc64_nvme_tab[(*crc ^ ((uint8_t *) buf)[n]) + & 0xff] + ^ (*crc >> 8); + + *crc = *crc ^ 0xffffffffffffffffULL; +} + +#ifdef HAVE_PCLMUL + +#include +#include + +/* Fold-by-16 constants for reflected CRC-64/NVMe. Properties of the + * polynomial; identical between the PCLMUL and PMULL backends. + * k3 = bitrev64(x^(128+64) mod P) << 1 + * k4 = bitrev64(x^(128+0) mod P) << 1 + */ +static const uint64_t k3_clmul = 0xeadc41fd2ba3d420ULL; +static const uint64_t k4_clmul = 0x21e9761e252621acULL; + +__attribute__((target("pclmul,sse4.1"))) +static __m128i fold16(__m128i x, + __m128i k) +{ + __m128i lo; + __m128i hi; + + lo = _mm_clmulepi64_si128(x, k, 0x00); + hi = _mm_clmulepi64_si128(x, k, 0x11); + return _mm_xor_si128(lo, hi); +} + +/* Fold-by-16 over 16-byte chunks; the 128-bit folded state is then + * emitted as 16 little-endian bytes and run through the byte-table + * loop together with any tail (<=15 bytes). The 16-byte minimum on + * the bulk loop is why the short-input path uses the table directly. + */ +__attribute__((target("pclmul,sse4.1"))) +static void crc64_nvme_clmul(uint64_t * crc, + const void * buf, + size_t len) +{ + const uint8_t * p; + uint64_t seed; + uint64_t c; + size_t off; + size_t n; + __m128i x; + __m128i k; + uint8_t post[16]; + + p = (const uint8_t *) buf; + seed = *crc; + + if (len < 16) { + c = seed ^ 0xffffffffffffffffULL; + for (n = 0; n < len; n++) + c = crc64_nvme_tab[(c ^ p[n]) & 0xff] + ^ (c >> 8); + *crc = c ^ 0xffffffffffffffffULL; + return; + } + + x = _mm_loadu_si128((const __m128i *) p); + x = _mm_xor_si128(x, _mm_cvtsi64_si128((int64_t) + (seed ^ 0xffffffffffffffffULL))); + + k = _mm_set_epi64x((int64_t) k4_clmul, (int64_t) k3_clmul); + + off = 16; + while (off + 16 <= len) { + __m128i d; + + d = _mm_loadu_si128((const __m128i *) (p + off)); + x = _mm_xor_si128(fold16(x, k), d); + off += 16; + } + + _mm_storeu_si128((__m128i *) post, x); + + c = 0; + for (n = 0; n < 16; n++) + c = crc64_nvme_tab[(c ^ post[n]) & 0xff] ^ (c >> 8); + + for (n = off; n < len; n++) + c = crc64_nvme_tab[(c ^ p[n]) & 0xff] ^ (c >> 8); + + *crc = c ^ 0xffffffffffffffffULL; +} + +#endif /* HAVE_PCLMUL */ + +#ifdef HAVE_PMULL + +#include + +/* Same fold-by-16 constants as the PCLMUL path (poly properties). */ +static const uint64_t k3_pmull = 0xeadc41fd2ba3d420ULL; +static const uint64_t k4_pmull = 0x21e9761e252621acULL; + +__attribute__((target("+crypto"))) +static uint64x2_t fold16_pmull(uint64x2_t x, + uint64x2_t k) +{ + poly64x2_t xp; + poly64x2_t kp; + uint64x2_t lo; + uint64x2_t hi; + + xp = vreinterpretq_p64_u64(x); + kp = vreinterpretq_p64_u64(k); + lo = vreinterpretq_u64_p128( + vmull_p64((poly64_t) vgetq_lane_u64(x, 0), + (poly64_t) vgetq_lane_u64(k, 0))); + hi = vreinterpretq_u64_p128(vmull_high_p64(xp, kp)); + return veorq_u64(lo, hi); +} + +__attribute__((target("+crypto"))) +static void crc64_nvme_pmull(uint64_t * crc, + const void * buf, + size_t len) +{ + const uint8_t * p; + uint64_t seed; + uint64_t c; + size_t off; + size_t n; + uint64x2_t x; + uint64x2_t k; + uint64_t seed_lane[2]; + uint64_t k_lanes[2]; + uint8_t post[16]; + + p = (const uint8_t *) buf; + seed = *crc; + + if (len < 16) { + c = seed ^ 0xffffffffffffffffULL; + for (n = 0; n < len; n++) + c = crc64_nvme_tab[(c ^ p[n]) & 0xff] + ^ (c >> 8); + *crc = c ^ 0xffffffffffffffffULL; + return; + } + + x = vld1q_u64((const uint64_t *) p); + seed_lane[0] = seed ^ 0xffffffffffffffffULL; + seed_lane[1] = 0; + x = veorq_u64(x, vld1q_u64(seed_lane)); + + k_lanes[0] = k3_pmull; + k_lanes[1] = k4_pmull; + k = vld1q_u64(k_lanes); + + off = 16; + while (off + 16 <= len) { + uint64x2_t d; + + d = vld1q_u64((const uint64_t *) (p + off)); + x = veorq_u64(fold16_pmull(x, k), d); + off += 16; + } + + vst1q_u8(post, vreinterpretq_u8_u64(x)); + + c = 0; + for (n = 0; n < 16; n++) + c = crc64_nvme_tab[(c ^ post[n]) & 0xff] ^ (c >> 8); + + for (n = off; n < len; n++) + c = crc64_nvme_tab[(c ^ p[n]) & 0xff] ^ (c >> 8); + + *crc = c ^ 0xffffffffffffffffULL; +} + +#endif /* HAVE_PMULL */ + +void crc64_nvme(uint64_t * crc, + const void * buf, + size_t len) +{ +#ifdef HAVE_PCLMUL + crc64_nvme_clmul(crc, buf, len); +#elif defined(HAVE_PMULL) + crc64_nvme_pmull(crc, buf, len); +#else + crc64_nvme_table(crc, buf, len); +#endif +} diff --git a/src/lib/crc/crc8.c b/src/lib/crc/crc8.c new file mode 100644 index 00000000..e8b9685a --- /dev/null +++ b/src/lib/crc/crc8.c @@ -0,0 +1,64 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2026 + * + * 8-bit Cyclic Redundancy Check (AUTOSAR variant) + * + * Dimitri Staessens + * Sander Vrijders + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +/* + * CRC-8/AUTOSAR (reveng catalog): + * poly = 0x2f + * init = 0xff + * refin = false + * refout = false + * xorout = 0xff + * check = crc8_autosar("123456789") == 0xdf + */ + +#include "config.h" + +#include + +/* Bit-by-bit MSB-first CRC. The expected use case is header check + * sequences of a handful of bytes; a 256-byte lookup table would not + * pay for the extra .rodata footprint at typical input sizes. + */ +void crc8_autosar(uint8_t * crc, + const void * buf, + size_t len) +{ + const uint8_t * p; + uint8_t c; + size_t n; + int i; + + p = (const uint8_t *) buf; + c = *crc ^ 0xff; + + for (n = 0; n < len; n++) { + c ^= p[n]; + for (i = 0; i < 8; i++) { + if (c & 0x80) + c = (uint8_t) ((c << 1) ^ 0x2f); + else + c = (uint8_t) (c << 1); + } + } + + *crc = c ^ 0xff; +} diff --git a/src/lib/crc/tests/CMakeLists.txt b/src/lib/crc/tests/CMakeLists.txt new file mode 100644 index 00000000..11daca5a --- /dev/null +++ b/src/lib/crc/tests/CMakeLists.txt @@ -0,0 +1,21 @@ +get_filename_component(PARENT_PATH ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(PARENT_DIR ${PARENT_PATH} NAME) + +compute_test_prefix() + +create_test_sourcelist(${PARENT_DIR}_tests test_suite.c + # Add new tests here + crc8_test.c + crc16_test.c + crc32_test.c + crc64_test.c + ) + +add_executable(${PARENT_DIR}_test ${${PARENT_DIR}_tests}) + +disable_test_logging_for_target(${PARENT_DIR}_test) +target_link_libraries(${PARENT_DIR}_test ouroboros-common) + +add_dependencies(build_tests ${PARENT_DIR}_test) + +ouroboros_register_tests(TARGET ${PARENT_DIR}_test TESTS ${${PARENT_DIR}_tests}) diff --git a/src/lib/crc/tests/crc16_test.c b/src/lib/crc/tests/crc16_test.c new file mode 100644 index 00000000..03a5b504 --- /dev/null +++ b/src/lib/crc/tests/crc16_test.c @@ -0,0 +1,67 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2026 + * + * Test of the CRC-16/CCITT-FALSE function + * + * Dimitri Staessens + * Sander Vrijders + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +#include "config.h" + +#include + +#include + +#include +#include +#include + +/* reveng-catalog smoke vectors. */ +static int test_crc16_ccitt_false_basic(void) +{ + uint16_t crc; + + TEST_START(); + + crc = 0; + crc16_ccitt_false(&crc, "", 0); + if (crc != 0xffff) + goto fail; + + crc = 0; + crc16_ccitt_false(&crc, "123456789", 9); + if (crc != 0x29b1) + goto fail; + + TEST_SUCCESS(); + return TEST_RC_SUCCESS; + fail: + TEST_FAIL(); + return TEST_RC_FAIL; +} + +int crc16_test(int argc, + char ** argv) +{ + int ret = 0; + + (void) argc; + (void) argv; + + ret |= test_crc16_ccitt_false_basic(); + return ret; +} diff --git a/src/lib/crc/tests/crc32_test.c b/src/lib/crc/tests/crc32_test.c new file mode 100644 index 00000000..5a1ddd87 --- /dev/null +++ b/src/lib/crc/tests/crc32_test.c @@ -0,0 +1,72 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2026 + * + * Test of the CRC32 function + * + * Dimitri Staessens + * Sander Vrijders + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +#include + +#include +#include +#include +#include +#include + +/* + * Test vectors calculated at + * https://www.lammertbies.nl/comm/info/crc-calculation.html + */ + +int crc32_test(int argc, + char ** argv) +{ + uint32_t crc = 0; + int i = 0; + + (void) argc; + (void) argv; + + crc32(&crc, "0", 1); + if (crc != 0xF4DBDF21) + return -1; + + crc = 0; + + crc32(&crc, "123456789", 9); + if (crc != 0xCBF43926) + return -1; + + crc = 0; + + crc32(&crc, "987654321", 9); + if (crc != 0x015F0201) + return -1; + + crc32(&crc, "123456789", 9); + if (crc != 0x806B60E3) + return -1; + + crc = 0; + + crc32(&crc, &i , 1); + if (crc != 0xD202EF8D) + return -1; + + return 0; +} diff --git a/src/lib/crc/tests/crc64_test.c b/src/lib/crc/tests/crc64_test.c new file mode 100644 index 00000000..cf3f5ca3 --- /dev/null +++ b/src/lib/crc/tests/crc64_test.c @@ -0,0 +1,126 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2026 + * + * Test of the CRC-64/NVMe function + * + * Dimitri Staessens + * Sander Vrijders + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +#include "config.h" + +#include +#include + +#include + +#include +#include +#include + +/* Reference impl, internal to libouroboros-common. */ +extern void crc64_nvme_table(uint64_t * crc, + const void * buf, + size_t len); + +/* reveng-catalog smoke vectors plus a 16-byte fold-boundary check. */ +static int test_crc64_nvme_basic(void) +{ + uint64_t crc; + + TEST_START(); + + crc = 0; + crc64_nvme(&crc, "", 0); + if (crc != 0x0000000000000000ULL) + goto fail; + + crc = 0; + crc64_nvme(&crc, "123456789", 9); + if (crc != 0xae8b14860a799888ULL) + goto fail; + + crc = 0; + crc64_nvme(&crc, "0123456789abcdef", 16); + if (crc != 0x091485ca7018730eULL) + goto fail; + + TEST_SUCCESS(); + return TEST_RC_SUCCESS; + fail: + TEST_FAIL(); + return TEST_RC_FAIL; +} + +#if defined(HAVE_PCLMUL) || defined(HAVE_PMULL) +/* Cross-check the accelerated dispatcher path against the byte-table. */ +static int test_crc64_nvme_random(void) +{ + static const size_t lens[] = { + 0, 1, 7, 8, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, + 129, 255, 256, 257, 1023, 1024, 1025, 4096 + }; + uint8_t buf[4096]; + size_t i; + uint64_t ref; + uint64_t got; + + TEST_START(); + + if (random_buffer(buf, sizeof(buf)) < 0) { + printf("Failed to generate random data.\n"); + goto fail; + } + + for (i = 0; i < sizeof(lens) / sizeof(lens[0]); i++) { + ref = 0; + crc64_nvme_table(&ref, buf, lens[i]); + + got = 0; + crc64_nvme(&got, buf, lens[i]); + + if (ref == got) + continue; + + printf("Mismatch at len=%zu: table=0x%016lx disp=0x%016lx\n", + lens[i], + (unsigned long) ref, + (unsigned long) got); + goto fail; + } + + TEST_SUCCESS(); + return TEST_RC_SUCCESS; + fail: + TEST_FAIL(); + return TEST_RC_FAIL; +#endif +} + +int crc64_test(int argc, + char ** argv) +{ + int ret = 0; + + (void) argc; + (void) argv; + + ret |= test_crc64_nvme_basic(); +#if defined(HAVE_PCLMUL) || defined(HAVE_PMULL) + ret |= test_crc64_nvme_random(); +#endif + return ret; +} diff --git a/src/lib/crc/tests/crc8_test.c b/src/lib/crc/tests/crc8_test.c new file mode 100644 index 00000000..f7bb33b8 --- /dev/null +++ b/src/lib/crc/tests/crc8_test.c @@ -0,0 +1,67 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2026 + * + * Test of the CRC-8/AUTOSAR function + * + * Dimitri Staessens + * Sander Vrijders + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +#include "config.h" + +#include + +#include + +#include +#include +#include + +/* reveng-catalog smoke vectors. */ +static int test_crc8_autosar_basic(void) +{ + uint8_t crc; + + TEST_START(); + + crc = 0; + crc8_autosar(&crc, "", 0); + if (crc != 0x00) + goto fail; + + crc = 0; + crc8_autosar(&crc, "123456789", 9); + if (crc != 0xdf) + goto fail; + + TEST_SUCCESS(); + return TEST_RC_SUCCESS; + fail: + TEST_FAIL(); + return TEST_RC_FAIL; +} + +int crc8_test(int argc, + char ** argv) +{ + int ret = 0; + + (void) argc; + (void) argv; + + ret |= test_crc8_autosar_basic(); + return ret; +} diff --git a/src/lib/crc16.c b/src/lib/crc16.c deleted file mode 100644 index 55af8647..00000000 --- a/src/lib/crc16.c +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2026 - * - * 16-bit Cyclic Redundancy Check (CCITT-FALSE variant) - * - * Dimitri Staessens - * Sander Vrijders - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * version 2.1 as published by the Free Software Foundation. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., http://www.fsf.org/about/contact/. - */ - -/* - * CRC-16/CCITT-FALSE (reveng catalog, alias CRC-16/IBM-3740): - * poly = 0x1021 - * init = 0xffff - * refin = false - * refout = false - * xorout = 0x0000 - * check = crc16_ccitt_false("123456789") == 0x29b1 - */ - -#include "config.h" - -#include - -/* Bit-by-bit MSB-first CRC. Header-check use case rarely exceeds a - * few hundred bytes; a 256-entry uint16_t table costs 512 B of - * .rodata and is easy to add later if profiling demands it. - */ -void crc16_ccitt_false(uint16_t * crc, - const void * buf, - size_t len) -{ - const uint8_t * p; - uint16_t c; - size_t n; - int i; - - p = (const uint8_t *) buf; - c = *crc ^ 0xffff; - - for (n = 0; n < len; n++) { - c ^= ((uint16_t) p[n]) << 8; - for (i = 0; i < 8; i++) { - if (c & 0x8000) - c = (uint16_t) ((c << 1) ^ 0x1021); - else - c = (uint16_t) (c << 1); - } - } - - *crc = c; -} diff --git a/src/lib/crc32.c b/src/lib/crc32.c deleted file mode 100644 index 0fdb62b1..00000000 --- a/src/lib/crc32.c +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2026 - * - * 32-bit Cyclic Redundancy Check - * - * Dimitri Staessens - * Sander Vrijders - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * version 2.1 as published by the Free Software Foundation. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., http://www.fsf.org/about/contact/. - */ - -#include - -static const uint32_t crc32_table[256] = { - 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, - 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, - 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, - 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, - 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, - 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, - 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, - 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, - 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, - 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, - 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, - 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, - 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, - 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, - 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, - 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, - 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, - 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, - 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, - 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, - 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, - 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, - 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, - 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, - 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, - 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, - 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, - 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, - 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, - 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, - 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, - 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, - 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, - 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, - 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, - 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, - 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, - 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, - 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, - 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, - 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, - 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, - 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d -}; - -void crc32(uint32_t * crc, - const void * buf, - size_t len) -{ - size_t n; - - *crc = *crc ^ 0xffffffff; - - for (n = 0; n < len; n++) - *crc = crc32_table[(*crc ^ ((uint8_t *) buf)[n]) & 0xff] - ^ (*crc >> 8); - - *crc = *crc ^ 0xffffffff; -} diff --git a/src/lib/crc64.c b/src/lib/crc64.c deleted file mode 100644 index 3ee5e798..00000000 --- a/src/lib/crc64.c +++ /dev/null @@ -1,369 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2026 - * - * 64-bit Cyclic Redundancy Check (NVMe variant) - * - * Dimitri Staessens - * Sander Vrijders - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * version 2.1 as published by the Free Software Foundation. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., http://www.fsf.org/about/contact/. - */ - -/* - * CRC-64/NVMe (reveng catalog): - * poly = 0xad93d23594c93659 - * init = 0xffffffffffffffff - * refin = true - * refout = true - * xorout = 0xffffffffffffffff - * check = crc64_nvme("123456789") == 0xae8b14860a799888 - */ - -#include "config.h" - -#include - -/* Reflected CRC-64/NVMe table. Polynomial in reflected form: - * 0x9a6c9329ac4bc9b5 (bitrev of 0xad93d23594c93659). - */ -static const uint64_t crc64_nvme_tab[256] = { - 0x0000000000000000ULL, 0x7f6ef0c830358979ULL, - 0xfedde190606b12f2ULL, 0x81b31158505e9b8bULL, - 0xc962e5739841b68fULL, 0xb60c15bba8743ff6ULL, - 0x37bf04e3f82aa47dULL, 0x48d1f42bc81f2d04ULL, - 0xa61cecb46814fe75ULL, 0xd9721c7c5821770cULL, - 0x58c10d24087fec87ULL, 0x27affdec384a65feULL, - 0x6f7e09c7f05548faULL, 0x1010f90fc060c183ULL, - 0x91a3e857903e5a08ULL, 0xeecd189fa00bd371ULL, - 0x78e0ff3b88be6f81ULL, 0x078e0ff3b88be6f8ULL, - 0x863d1eabe8d57d73ULL, 0xf953ee63d8e0f40aULL, - 0xb1821a4810ffd90eULL, 0xceecea8020ca5077ULL, - 0x4f5ffbd87094cbfcULL, 0x30310b1040a14285ULL, - 0xdefc138fe0aa91f4ULL, 0xa192e347d09f188dULL, - 0x2021f21f80c18306ULL, 0x5f4f02d7b0f40a7fULL, - 0x179ef6fc78eb277bULL, 0x68f0063448deae02ULL, - 0xe943176c18803589ULL, 0x962de7a428b5bcf0ULL, - 0xf1c1fe77117cdf02ULL, 0x8eaf0ebf2149567bULL, - 0x0f1c1fe77117cdf0ULL, 0x7072ef2f41224489ULL, - 0x38a31b04893d698dULL, 0x47cdebccb908e0f4ULL, - 0xc67efa94e9567b7fULL, 0xb9100a5cd963f206ULL, - 0x57dd12c379682177ULL, 0x28b3e20b495da80eULL, - 0xa900f35319033385ULL, 0xd66e039b2936bafcULL, - 0x9ebff7b0e12997f8ULL, 0xe1d10778d11c1e81ULL, - 0x606216208142850aULL, 0x1f0ce6e8b1770c73ULL, - 0x8921014c99c2b083ULL, 0xf64ff184a9f739faULL, - 0x77fce0dcf9a9a271ULL, 0x08921014c99c2b08ULL, - 0x4043e43f0183060cULL, 0x3f2d14f731b68f75ULL, - 0xbe9e05af61e814feULL, 0xc1f0f56751dd9d87ULL, - 0x2f3dedf8f1d64ef6ULL, 0x50531d30c1e3c78fULL, - 0xd1e00c6891bd5c04ULL, 0xae8efca0a188d57dULL, - 0xe65f088b6997f879ULL, 0x9931f84359a27100ULL, - 0x1882e91b09fcea8bULL, 0x67ec19d339c963f2ULL, - 0xd75adabd7a6e2d6fULL, 0xa8342a754a5ba416ULL, - 0x29873b2d1a053f9dULL, 0x56e9cbe52a30b6e4ULL, - 0x1e383fcee22f9be0ULL, 0x6156cf06d21a1299ULL, - 0xe0e5de5e82448912ULL, 0x9f8b2e96b271006bULL, - 0x71463609127ad31aULL, 0x0e28c6c1224f5a63ULL, - 0x8f9bd7997211c1e8ULL, 0xf0f5275142244891ULL, - 0xb824d37a8a3b6595ULL, 0xc74a23b2ba0eececULL, - 0x46f932eaea507767ULL, 0x3997c222da65fe1eULL, - 0xafba2586f2d042eeULL, 0xd0d4d54ec2e5cb97ULL, - 0x5167c41692bb501cULL, 0x2e0934dea28ed965ULL, - 0x66d8c0f56a91f461ULL, 0x19b6303d5aa47d18ULL, - 0x980521650afae693ULL, 0xe76bd1ad3acf6feaULL, - 0x09a6c9329ac4bc9bULL, 0x76c839faaaf135e2ULL, - 0xf77b28a2faafae69ULL, 0x8815d86aca9a2710ULL, - 0xc0c42c4102850a14ULL, 0xbfaadc8932b0836dULL, - 0x3e19cdd162ee18e6ULL, 0x41773d1952db919fULL, - 0x269b24ca6b12f26dULL, 0x59f5d4025b277b14ULL, - 0xd846c55a0b79e09fULL, 0xa72835923b4c69e6ULL, - 0xeff9c1b9f35344e2ULL, 0x90973171c366cd9bULL, - 0x1124202993385610ULL, 0x6e4ad0e1a30ddf69ULL, - 0x8087c87e03060c18ULL, 0xffe938b633338561ULL, - 0x7e5a29ee636d1eeaULL, 0x0134d92653589793ULL, - 0x49e52d0d9b47ba97ULL, 0x368bddc5ab7233eeULL, - 0xb738cc9dfb2ca865ULL, 0xc8563c55cb19211cULL, - 0x5e7bdbf1e3ac9decULL, 0x21152b39d3991495ULL, - 0xa0a63a6183c78f1eULL, 0xdfc8caa9b3f20667ULL, - 0x97193e827bed2b63ULL, 0xe877ce4a4bd8a21aULL, - 0x69c4df121b863991ULL, 0x16aa2fda2bb3b0e8ULL, - 0xf86737458bb86399ULL, 0x8709c78dbb8deae0ULL, - 0x06bad6d5ebd3716bULL, 0x79d4261ddbe6f812ULL, - 0x3105d23613f9d516ULL, 0x4e6b22fe23cc5c6fULL, - 0xcfd833a67392c7e4ULL, 0xb0b6c36e43a74e9dULL, - 0x9a6c9329ac4bc9b5ULL, 0xe50263e19c7e40ccULL, - 0x64b172b9cc20db47ULL, 0x1bdf8271fc15523eULL, - 0x530e765a340a7f3aULL, 0x2c608692043ff643ULL, - 0xadd397ca54616dc8ULL, 0xd2bd67026454e4b1ULL, - 0x3c707f9dc45f37c0ULL, 0x431e8f55f46abeb9ULL, - 0xc2ad9e0da4342532ULL, 0xbdc36ec59401ac4bULL, - 0xf5129aee5c1e814fULL, 0x8a7c6a266c2b0836ULL, - 0x0bcf7b7e3c7593bdULL, 0x74a18bb60c401ac4ULL, - 0xe28c6c1224f5a634ULL, 0x9de29cda14c02f4dULL, - 0x1c518d82449eb4c6ULL, 0x633f7d4a74ab3dbfULL, - 0x2bee8961bcb410bbULL, 0x548079a98c8199c2ULL, - 0xd53368f1dcdf0249ULL, 0xaa5d9839ecea8b30ULL, - 0x449080a64ce15841ULL, 0x3bfe706e7cd4d138ULL, - 0xba4d61362c8a4ab3ULL, 0xc52391fe1cbfc3caULL, - 0x8df265d5d4a0eeceULL, 0xf29c951de49567b7ULL, - 0x732f8445b4cbfc3cULL, 0x0c41748d84fe7545ULL, - 0x6bad6d5ebd3716b7ULL, 0x14c39d968d029fceULL, - 0x95708ccedd5c0445ULL, 0xea1e7c06ed698d3cULL, - 0xa2cf882d2576a038ULL, 0xdda178e515432941ULL, - 0x5c1269bd451db2caULL, 0x237c997575283bb3ULL, - 0xcdb181ead523e8c2ULL, 0xb2df7122e51661bbULL, - 0x336c607ab548fa30ULL, 0x4c0290b2857d7349ULL, - 0x04d364994d625e4dULL, 0x7bbd94517d57d734ULL, - 0xfa0e85092d094cbfULL, 0x856075c11d3cc5c6ULL, - 0x134d926535897936ULL, 0x6c2362ad05bcf04fULL, - 0xed9073f555e26bc4ULL, 0x92fe833d65d7e2bdULL, - 0xda2f7716adc8cfb9ULL, 0xa54187de9dfd46c0ULL, - 0x24f29686cda3dd4bULL, 0x5b9c664efd965432ULL, - 0xb5517ed15d9d8743ULL, 0xca3f8e196da80e3aULL, - 0x4b8c9f413df695b1ULL, 0x34e26f890dc31cc8ULL, - 0x7c339ba2c5dc31ccULL, 0x035d6b6af5e9b8b5ULL, - 0x82ee7a32a5b7233eULL, 0xfd808afa9582aa47ULL, - 0x4d364994d625e4daULL, 0x3258b95ce6106da3ULL, - 0xb3eba804b64ef628ULL, 0xcc8558cc867b7f51ULL, - 0x8454ace74e645255ULL, 0xfb3a5c2f7e51db2cULL, - 0x7a894d772e0f40a7ULL, 0x05e7bdbf1e3ac9deULL, - 0xeb2aa520be311aafULL, 0x944455e88e0493d6ULL, - 0x15f744b0de5a085dULL, 0x6a99b478ee6f8124ULL, - 0x224840532670ac20ULL, 0x5d26b09b16452559ULL, - 0xdc95a1c3461bbed2ULL, 0xa3fb510b762e37abULL, - 0x35d6b6af5e9b8b5bULL, 0x4ab846676eae0222ULL, - 0xcb0b573f3ef099a9ULL, 0xb465a7f70ec510d0ULL, - 0xfcb453dcc6da3dd4ULL, 0x83daa314f6efb4adULL, - 0x0269b24ca6b12f26ULL, 0x7d0742849684a65fULL, - 0x93ca5a1b368f752eULL, 0xeca4aad306bafc57ULL, - 0x6d17bb8b56e467dcULL, 0x12794b4366d1eea5ULL, - 0x5aa8bf68aecec3a1ULL, 0x25c64fa09efb4ad8ULL, - 0xa4755ef8cea5d153ULL, 0xdb1bae30fe90582aULL, - 0xbcf7b7e3c7593bd8ULL, 0xc399472bf76cb2a1ULL, - 0x422a5673a732292aULL, 0x3d44a6bb9707a053ULL, - 0x759552905f188d57ULL, 0x0afba2586f2d042eULL, - 0x8b48b3003f739fa5ULL, 0xf42643c80f4616dcULL, - 0x1aeb5b57af4dc5adULL, 0x6585ab9f9f784cd4ULL, - 0xe436bac7cf26d75fULL, 0x9b584a0fff135e26ULL, - 0xd389be24370c7322ULL, 0xace74eec0739fa5bULL, - 0x2d545fb4576761d0ULL, 0x523aaf7c6752e8a9ULL, - 0xc41748d84fe75459ULL, 0xbb79b8107fd2dd20ULL, - 0x3acaa9482f8c46abULL, 0x45a459801fb9cfd2ULL, - 0x0d75adabd7a6e2d6ULL, 0x721b5d63e7936bafULL, - 0xf3a84c3bb7cdf024ULL, 0x8cc6bcf387f8795dULL, - 0x620ba46c27f3aa2cULL, 0x1d6554a417c62355ULL, - 0x9cd645fc4798b8deULL, 0xe3b8b53477ad31a7ULL, - 0xab69411fbfb21ca3ULL, 0xd407b1d78f8795daULL, - 0x55b4a08fdfd90e51ULL, 0x2ada5047efec8728ULL -}; - -void crc64_nvme_table(uint64_t * crc, - const void * buf, - size_t len) -{ - size_t n; - - *crc = *crc ^ 0xffffffffffffffffULL; - - for (n = 0; n < len; n++) - *crc = crc64_nvme_tab[(*crc ^ ((uint8_t *) buf)[n]) - & 0xff] - ^ (*crc >> 8); - - *crc = *crc ^ 0xffffffffffffffffULL; -} - -#ifdef HAVE_PCLMUL - -#include -#include - -/* Fold-by-16 constants for reflected CRC-64/NVMe. Properties of the - * polynomial; identical between the PCLMUL and PMULL backends. - * k3 = bitrev64(x^(128+64) mod P) << 1 - * k4 = bitrev64(x^(128+0) mod P) << 1 - */ -static const uint64_t k3_clmul = 0xeadc41fd2ba3d420ULL; -static const uint64_t k4_clmul = 0x21e9761e252621acULL; - -__attribute__((target("pclmul,sse4.1"))) -static __m128i fold16(__m128i x, - __m128i k) -{ - __m128i lo; - __m128i hi; - - lo = _mm_clmulepi64_si128(x, k, 0x00); - hi = _mm_clmulepi64_si128(x, k, 0x11); - return _mm_xor_si128(lo, hi); -} - -/* Fold-by-16 over 16-byte chunks; the 128-bit folded state is then - * emitted as 16 little-endian bytes and run through the byte-table - * loop together with any tail (<=15 bytes). The 16-byte minimum on - * the bulk loop is why the short-input path uses the table directly. - */ -__attribute__((target("pclmul,sse4.1"))) -static void crc64_nvme_clmul(uint64_t * crc, - const void * buf, - size_t len) -{ - const uint8_t * p; - uint64_t seed; - uint64_t c; - size_t off; - size_t n; - __m128i x; - __m128i k; - uint8_t post[16]; - - p = (const uint8_t *) buf; - seed = *crc; - - if (len < 16) { - c = seed ^ 0xffffffffffffffffULL; - for (n = 0; n < len; n++) - c = crc64_nvme_tab[(c ^ p[n]) & 0xff] - ^ (c >> 8); - *crc = c ^ 0xffffffffffffffffULL; - return; - } - - x = _mm_loadu_si128((const __m128i *) p); - x = _mm_xor_si128(x, _mm_cvtsi64_si128((int64_t) - (seed ^ 0xffffffffffffffffULL))); - - k = _mm_set_epi64x((int64_t) k4_clmul, (int64_t) k3_clmul); - - off = 16; - while (off + 16 <= len) { - __m128i d; - - d = _mm_loadu_si128((const __m128i *) (p + off)); - x = _mm_xor_si128(fold16(x, k), d); - off += 16; - } - - _mm_storeu_si128((__m128i *) post, x); - - c = 0; - for (n = 0; n < 16; n++) - c = crc64_nvme_tab[(c ^ post[n]) & 0xff] ^ (c >> 8); - - for (n = off; n < len; n++) - c = crc64_nvme_tab[(c ^ p[n]) & 0xff] ^ (c >> 8); - - *crc = c ^ 0xffffffffffffffffULL; -} - -#endif /* HAVE_PCLMUL */ - -#ifdef HAVE_PMULL - -#include - -/* Same fold-by-16 constants as the PCLMUL path (poly properties). */ -static const uint64_t k3_pmull = 0xeadc41fd2ba3d420ULL; -static const uint64_t k4_pmull = 0x21e9761e252621acULL; - -__attribute__((target("+crypto"))) -static uint64x2_t fold16_pmull(uint64x2_t x, - uint64x2_t k) -{ - poly64x2_t xp; - poly64x2_t kp; - uint64x2_t lo; - uint64x2_t hi; - - xp = vreinterpretq_p64_u64(x); - kp = vreinterpretq_p64_u64(k); - lo = vreinterpretq_u64_p128( - vmull_p64((poly64_t) vgetq_lane_u64(x, 0), - (poly64_t) vgetq_lane_u64(k, 0))); - hi = vreinterpretq_u64_p128(vmull_high_p64(xp, kp)); - return veorq_u64(lo, hi); -} - -__attribute__((target("+crypto"))) -static void crc64_nvme_pmull(uint64_t * crc, - const void * buf, - size_t len) -{ - const uint8_t * p; - uint64_t seed; - uint64_t c; - size_t off; - size_t n; - uint64x2_t x; - uint64x2_t k; - uint64_t seed_lane[2]; - uint64_t k_lanes[2]; - uint8_t post[16]; - - p = (const uint8_t *) buf; - seed = *crc; - - if (len < 16) { - c = seed ^ 0xffffffffffffffffULL; - for (n = 0; n < len; n++) - c = crc64_nvme_tab[(c ^ p[n]) & 0xff] - ^ (c >> 8); - *crc = c ^ 0xffffffffffffffffULL; - return; - } - - x = vld1q_u64((const uint64_t *) p); - seed_lane[0] = seed ^ 0xffffffffffffffffULL; - seed_lane[1] = 0; - x = veorq_u64(x, vld1q_u64(seed_lane)); - - k_lanes[0] = k3_pmull; - k_lanes[1] = k4_pmull; - k = vld1q_u64(k_lanes); - - off = 16; - while (off + 16 <= len) { - uint64x2_t d; - - d = vld1q_u64((const uint64_t *) (p + off)); - x = veorq_u64(fold16_pmull(x, k), d); - off += 16; - } - - vst1q_u8(post, vreinterpretq_u8_u64(x)); - - c = 0; - for (n = 0; n < 16; n++) - c = crc64_nvme_tab[(c ^ post[n]) & 0xff] ^ (c >> 8); - - for (n = off; n < len; n++) - c = crc64_nvme_tab[(c ^ p[n]) & 0xff] ^ (c >> 8); - - *crc = c ^ 0xffffffffffffffffULL; -} - -#endif /* HAVE_PMULL */ - -void crc64_nvme(uint64_t * crc, - const void * buf, - size_t len) -{ -#ifdef HAVE_PCLMUL - crc64_nvme_clmul(crc, buf, len); -#elif defined(HAVE_PMULL) - crc64_nvme_pmull(crc, buf, len); -#else - crc64_nvme_table(crc, buf, len); -#endif -} diff --git a/src/lib/crc8.c b/src/lib/crc8.c deleted file mode 100644 index e8b9685a..00000000 --- a/src/lib/crc8.c +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2026 - * - * 8-bit Cyclic Redundancy Check (AUTOSAR variant) - * - * Dimitri Staessens - * Sander Vrijders - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * version 2.1 as published by the Free Software Foundation. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., http://www.fsf.org/about/contact/. - */ - -/* - * CRC-8/AUTOSAR (reveng catalog): - * poly = 0x2f - * init = 0xff - * refin = false - * refout = false - * xorout = 0xff - * check = crc8_autosar("123456789") == 0xdf - */ - -#include "config.h" - -#include - -/* Bit-by-bit MSB-first CRC. The expected use case is header check - * sequences of a handful of bytes; a 256-byte lookup table would not - * pay for the extra .rodata footprint at typical input sizes. - */ -void crc8_autosar(uint8_t * crc, - const void * buf, - size_t len) -{ - const uint8_t * p; - uint8_t c; - size_t n; - int i; - - p = (const uint8_t *) buf; - c = *crc ^ 0xff; - - for (n = 0; n < len; n++) { - c ^= p[n]; - for (i = 0; i < 8; i++) { - if (c & 0x80) - c = (uint8_t) ((c << 1) ^ 0x2f); - else - c = (uint8_t) (c << 1); - } - } - - *crc = c ^ 0xff; -} diff --git a/src/lib/tests/CMakeLists.txt b/src/lib/tests/CMakeLists.txt index afe64884..337d85a6 100644 --- a/src/lib/tests/CMakeLists.txt +++ b/src/lib/tests/CMakeLists.txt @@ -10,10 +10,6 @@ create_test_sourcelist(${PARENT_DIR}_tests test_suite.c auth_test_slh_dsa.c bitmap_test.c btree_test.c - crc8_test.c - crc16_test.c - crc32_test.c - crc64_test.c crypt_test.c hash_test.c kex_test.c diff --git a/src/lib/tests/crc16_test.c b/src/lib/tests/crc16_test.c deleted file mode 100644 index 03a5b504..00000000 --- a/src/lib/tests/crc16_test.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2026 - * - * Test of the CRC-16/CCITT-FALSE function - * - * Dimitri Staessens - * Sander Vrijders - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., http://www.fsf.org/about/contact/. - */ - -#include "config.h" - -#include - -#include - -#include -#include -#include - -/* reveng-catalog smoke vectors. */ -static int test_crc16_ccitt_false_basic(void) -{ - uint16_t crc; - - TEST_START(); - - crc = 0; - crc16_ccitt_false(&crc, "", 0); - if (crc != 0xffff) - goto fail; - - crc = 0; - crc16_ccitt_false(&crc, "123456789", 9); - if (crc != 0x29b1) - goto fail; - - TEST_SUCCESS(); - return TEST_RC_SUCCESS; - fail: - TEST_FAIL(); - return TEST_RC_FAIL; -} - -int crc16_test(int argc, - char ** argv) -{ - int ret = 0; - - (void) argc; - (void) argv; - - ret |= test_crc16_ccitt_false_basic(); - return ret; -} diff --git a/src/lib/tests/crc32_test.c b/src/lib/tests/crc32_test.c deleted file mode 100644 index 5a1ddd87..00000000 --- a/src/lib/tests/crc32_test.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2026 - * - * Test of the CRC32 function - * - * Dimitri Staessens - * Sander Vrijders - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., http://www.fsf.org/about/contact/. - */ - -#include - -#include -#include -#include -#include -#include - -/* - * Test vectors calculated at - * https://www.lammertbies.nl/comm/info/crc-calculation.html - */ - -int crc32_test(int argc, - char ** argv) -{ - uint32_t crc = 0; - int i = 0; - - (void) argc; - (void) argv; - - crc32(&crc, "0", 1); - if (crc != 0xF4DBDF21) - return -1; - - crc = 0; - - crc32(&crc, "123456789", 9); - if (crc != 0xCBF43926) - return -1; - - crc = 0; - - crc32(&crc, "987654321", 9); - if (crc != 0x015F0201) - return -1; - - crc32(&crc, "123456789", 9); - if (crc != 0x806B60E3) - return -1; - - crc = 0; - - crc32(&crc, &i , 1); - if (crc != 0xD202EF8D) - return -1; - - return 0; -} diff --git a/src/lib/tests/crc64_test.c b/src/lib/tests/crc64_test.c deleted file mode 100644 index cf3f5ca3..00000000 --- a/src/lib/tests/crc64_test.c +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2026 - * - * Test of the CRC-64/NVMe function - * - * Dimitri Staessens - * Sander Vrijders - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., http://www.fsf.org/about/contact/. - */ - -#include "config.h" - -#include -#include - -#include - -#include -#include -#include - -/* Reference impl, internal to libouroboros-common. */ -extern void crc64_nvme_table(uint64_t * crc, - const void * buf, - size_t len); - -/* reveng-catalog smoke vectors plus a 16-byte fold-boundary check. */ -static int test_crc64_nvme_basic(void) -{ - uint64_t crc; - - TEST_START(); - - crc = 0; - crc64_nvme(&crc, "", 0); - if (crc != 0x0000000000000000ULL) - goto fail; - - crc = 0; - crc64_nvme(&crc, "123456789", 9); - if (crc != 0xae8b14860a799888ULL) - goto fail; - - crc = 0; - crc64_nvme(&crc, "0123456789abcdef", 16); - if (crc != 0x091485ca7018730eULL) - goto fail; - - TEST_SUCCESS(); - return TEST_RC_SUCCESS; - fail: - TEST_FAIL(); - return TEST_RC_FAIL; -} - -#if defined(HAVE_PCLMUL) || defined(HAVE_PMULL) -/* Cross-check the accelerated dispatcher path against the byte-table. */ -static int test_crc64_nvme_random(void) -{ - static const size_t lens[] = { - 0, 1, 7, 8, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, - 129, 255, 256, 257, 1023, 1024, 1025, 4096 - }; - uint8_t buf[4096]; - size_t i; - uint64_t ref; - uint64_t got; - - TEST_START(); - - if (random_buffer(buf, sizeof(buf)) < 0) { - printf("Failed to generate random data.\n"); - goto fail; - } - - for (i = 0; i < sizeof(lens) / sizeof(lens[0]); i++) { - ref = 0; - crc64_nvme_table(&ref, buf, lens[i]); - - got = 0; - crc64_nvme(&got, buf, lens[i]); - - if (ref == got) - continue; - - printf("Mismatch at len=%zu: table=0x%016lx disp=0x%016lx\n", - lens[i], - (unsigned long) ref, - (unsigned long) got); - goto fail; - } - - TEST_SUCCESS(); - return TEST_RC_SUCCESS; - fail: - TEST_FAIL(); - return TEST_RC_FAIL; -#endif -} - -int crc64_test(int argc, - char ** argv) -{ - int ret = 0; - - (void) argc; - (void) argv; - - ret |= test_crc64_nvme_basic(); -#if defined(HAVE_PCLMUL) || defined(HAVE_PMULL) - ret |= test_crc64_nvme_random(); -#endif - return ret; -} diff --git a/src/lib/tests/crc8_test.c b/src/lib/tests/crc8_test.c deleted file mode 100644 index f7bb33b8..00000000 --- a/src/lib/tests/crc8_test.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2026 - * - * Test of the CRC-8/AUTOSAR function - * - * Dimitri Staessens - * Sander Vrijders - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., http://www.fsf.org/about/contact/. - */ - -#include "config.h" - -#include - -#include - -#include -#include -#include - -/* reveng-catalog smoke vectors. */ -static int test_crc8_autosar_basic(void) -{ - uint8_t crc; - - TEST_START(); - - crc = 0; - crc8_autosar(&crc, "", 0); - if (crc != 0x00) - goto fail; - - crc = 0; - crc8_autosar(&crc, "123456789", 9); - if (crc != 0xdf) - goto fail; - - TEST_SUCCESS(); - return TEST_RC_SUCCESS; - fail: - TEST_FAIL(); - return TEST_RC_FAIL; -} - -int crc8_test(int argc, - char ** argv) -{ - int ret = 0; - - (void) argc; - (void) argv; - - ret |= test_crc8_autosar_basic(); - return ret; -} -- cgit v1.2.3