11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds * crc32.h
31da177e4SLinus Torvalds * See linux/lib/crc32.c for license and changes
41da177e4SLinus Torvalds */
51da177e4SLinus Torvalds #ifndef _LINUX_CRC32_H
61da177e4SLinus Torvalds #define _LINUX_CRC32_H
71da177e4SLinus Torvalds
81da177e4SLinus Torvalds #include <linux/types.h>
9906d66dfSAkinobu Mita #include <linux/bitrev.h>
101da177e4SLinus Torvalds
11bc2736feSEric Biggers u32 crc32_le_arch(u32 crc, const u8 *p, size_t len);
12bc2736feSEric Biggers u32 crc32_le_base(u32 crc, const u8 *p, size_t len);
13bc2736feSEric Biggers u32 crc32_be_arch(u32 crc, const u8 *p, size_t len);
14bc2736feSEric Biggers u32 crc32_be_base(u32 crc, const u8 *p, size_t len);
15*68ea3c2aSEric Biggers u32 crc32c_arch(u32 crc, const u8 *p, size_t len);
16*68ea3c2aSEric Biggers u32 crc32c_base(u32 crc, const u8 *p, size_t len);
17d36cebe0SEric Biggers
crc32_le(u32 crc,const void * p,size_t len)18bc2736feSEric Biggers static inline u32 crc32_le(u32 crc, const void *p, size_t len)
19d36cebe0SEric Biggers {
20d36cebe0SEric Biggers if (IS_ENABLED(CONFIG_CRC32_ARCH))
21d36cebe0SEric Biggers return crc32_le_arch(crc, p, len);
22d36cebe0SEric Biggers return crc32_le_base(crc, p, len);
23d36cebe0SEric Biggers }
24d36cebe0SEric Biggers
crc32_be(u32 crc,const void * p,size_t len)25bc2736feSEric Biggers static inline u32 crc32_be(u32 crc, const void *p, size_t len)
26d36cebe0SEric Biggers {
27d36cebe0SEric Biggers if (IS_ENABLED(CONFIG_CRC32_ARCH))
28d36cebe0SEric Biggers return crc32_be_arch(crc, p, len);
29d36cebe0SEric Biggers return crc32_be_base(crc, p, len);
30d36cebe0SEric Biggers }
31d36cebe0SEric Biggers
crc32c(u32 crc,const void * p,size_t len)328df36829SEric Biggers static inline u32 crc32c(u32 crc, const void *p, size_t len)
33d36cebe0SEric Biggers {
34d36cebe0SEric Biggers if (IS_ENABLED(CONFIG_CRC32_ARCH))
35*68ea3c2aSEric Biggers return crc32c_arch(crc, p, len);
36*68ea3c2aSEric Biggers return crc32c_base(crc, p, len);
37d36cebe0SEric Biggers }
381da177e4SLinus Torvalds
39b5ae12e0SEric Biggers /*
40b5ae12e0SEric Biggers * crc32_optimizations() returns flags that indicate which CRC32 library
41b5ae12e0SEric Biggers * functions are using architecture-specific optimizations. Unlike
42b5ae12e0SEric Biggers * IS_ENABLED(CONFIG_CRC32_ARCH) it takes into account the different CRC32
43b5ae12e0SEric Biggers * variants and also whether any needed CPU features are available at runtime.
44b5ae12e0SEric Biggers */
45b5ae12e0SEric Biggers #define CRC32_LE_OPTIMIZATION BIT(0) /* crc32_le() is optimized */
46b5ae12e0SEric Biggers #define CRC32_BE_OPTIMIZATION BIT(1) /* crc32_be() is optimized */
478df36829SEric Biggers #define CRC32C_OPTIMIZATION BIT(2) /* crc32c() is optimized */
48b5ae12e0SEric Biggers #if IS_ENABLED(CONFIG_CRC32_ARCH)
49b5ae12e0SEric Biggers u32 crc32_optimizations(void);
50b5ae12e0SEric Biggers #else
crc32_optimizations(void)51b5ae12e0SEric Biggers static inline u32 crc32_optimizations(void) { return 0; }
52b5ae12e0SEric Biggers #endif
53b5ae12e0SEric Biggers
546e95fcaaSDaniel Borkmann /**
556e95fcaaSDaniel Borkmann * crc32_le_combine - Combine two crc32 check values into one. For two
566e95fcaaSDaniel Borkmann * sequences of bytes, seq1 and seq2 with lengths len1
576e95fcaaSDaniel Borkmann * and len2, crc32_le() check values were calculated
586e95fcaaSDaniel Borkmann * for each, crc1 and crc2.
596e95fcaaSDaniel Borkmann *
606e95fcaaSDaniel Borkmann * @crc1: crc32 of the first block
616e95fcaaSDaniel Borkmann * @crc2: crc32 of the second block
626e95fcaaSDaniel Borkmann * @len2: length of the second block
636e95fcaaSDaniel Borkmann *
646e95fcaaSDaniel Borkmann * Return: The crc32_le() check value of seq1 and seq2 concatenated,
656e95fcaaSDaniel Borkmann * requiring only crc1, crc2, and len2. Note: If seq_full denotes
666e95fcaaSDaniel Borkmann * the concatenated memory area of seq1 with seq2, and crc_full
676e95fcaaSDaniel Borkmann * the crc32_le() value of seq_full, then crc_full ==
686e95fcaaSDaniel Borkmann * crc32_le_combine(crc1, crc2, len2) when crc_full was seeded
696e95fcaaSDaniel Borkmann * with the same initializer as crc1, and crc2 seed was 0. See
706e95fcaaSDaniel Borkmann * also crc32_combine_test().
716e95fcaaSDaniel Borkmann */
72bc2736feSEric Biggers u32 crc32_le_shift(u32 crc, size_t len);
736d514b4eSGeorge Spelvin
crc32_le_combine(u32 crc1,u32 crc2,size_t len2)746d514b4eSGeorge Spelvin static inline u32 crc32_le_combine(u32 crc1, u32 crc2, size_t len2)
756d514b4eSGeorge Spelvin {
766d514b4eSGeorge Spelvin return crc32_le_shift(crc1, len2) ^ crc2;
776d514b4eSGeorge Spelvin }
786e95fcaaSDaniel Borkmann
79c64e6570SEric Biggers u32 crc32c_shift(u32 crc, size_t len);
80c64e6570SEric Biggers
816e95fcaaSDaniel Borkmann /**
82c64e6570SEric Biggers * crc32c_combine - Combine two crc32c check values into one. For two sequences
83c64e6570SEric Biggers * of bytes, seq1 and seq2 with lengths len1 and len2, crc32c()
84c64e6570SEric Biggers * check values were calculated for each, crc1 and crc2.
856e95fcaaSDaniel Borkmann *
866e95fcaaSDaniel Borkmann * @crc1: crc32c of the first block
876e95fcaaSDaniel Borkmann * @crc2: crc32c of the second block
886e95fcaaSDaniel Borkmann * @len2: length of the second block
896e95fcaaSDaniel Borkmann *
90c64e6570SEric Biggers * Return: The crc32c() check value of seq1 and seq2 concatenated, requiring
91c64e6570SEric Biggers * only crc1, crc2, and len2. Note: If seq_full denotes the concatenated
92c64e6570SEric Biggers * memory area of seq1 with seq2, and crc_full the crc32c() value of
93c64e6570SEric Biggers * seq_full, then crc_full == crc32c_combine(crc1, crc2, len2) when
94c64e6570SEric Biggers * crc_full was seeded with the same initializer as crc1, and crc2 seed
95c64e6570SEric Biggers * was 0. See also crc_combine_test().
966e95fcaaSDaniel Borkmann */
crc32c_combine(u32 crc1,u32 crc2,size_t len2)97c64e6570SEric Biggers static inline u32 crc32c_combine(u32 crc1, u32 crc2, size_t len2)
986d514b4eSGeorge Spelvin {
99c64e6570SEric Biggers return crc32c_shift(crc1, len2) ^ crc2;
1006d514b4eSGeorge Spelvin }
1016e95fcaaSDaniel Borkmann
102d03e1617SKonstantin Khlebnikov #define crc32(seed, data, length) crc32_le(seed, (unsigned char const *)(data), length)
1031da177e4SLinus Torvalds
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds * Helpers for hash table generation of ethernet nics:
1061da177e4SLinus Torvalds *
1071da177e4SLinus Torvalds * Ethernet sends the least significant bit of a byte first, thus crc32_le
1081da177e4SLinus Torvalds * is used. The output of crc32_le is bit reversed [most significant bit
1091da177e4SLinus Torvalds * is in bit nr 0], thus it must be reversed before use. Except for
1101da177e4SLinus Torvalds * nics that bit swap the result internally...
1111da177e4SLinus Torvalds */
112906d66dfSAkinobu Mita #define ether_crc(length, data) bitrev32(crc32_le(~0, data, length))
1131da177e4SLinus Torvalds #define ether_crc_le(length, data) crc32_le(~0, data, length)
1141da177e4SLinus Torvalds
1151da177e4SLinus Torvalds #endif /* _LINUX_CRC32_H */
116