Skip to content

Commit 6e95fca

Browse files
Daniel Borkmanndavem330
authored andcommitted
lib: crc32: add functionality to combine two crc32{, c}s in GF(2)
This patch adds a combinator to merge two or more crc32{,c}s into a new one. This is useful for checksum computations of fragmented skbs that use crc32/crc32c as checksums. The arithmetics for combining both in the GF(2) was taken and slightly modified from zlib. Only passing two crcs is insufficient as two crcs and the length of the second piece is needed for merging. The code is made generic, so that only polynomials need to be passed for crc32_le resp. crc32c_le. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Cc: linux-kernel@vger.kernel.org Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d921e04 commit 6e95fca

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

include/linux/crc32.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,48 @@
1111
extern u32 crc32_le(u32 crc, unsigned char const *p, size_t len);
1212
extern u32 crc32_be(u32 crc, unsigned char const *p, size_t len);
1313

14+
/**
15+
* crc32_le_combine - Combine two crc32 check values into one. For two
16+
* sequences of bytes, seq1 and seq2 with lengths len1
17+
* and len2, crc32_le() check values were calculated
18+
* for each, crc1 and crc2.
19+
*
20+
* @crc1: crc32 of the first block
21+
* @crc2: crc32 of the second block
22+
* @len2: length of the second block
23+
*
24+
* Return: The crc32_le() check value of seq1 and seq2 concatenated,
25+
* requiring only crc1, crc2, and len2. Note: If seq_full denotes
26+
* the concatenated memory area of seq1 with seq2, and crc_full
27+
* the crc32_le() value of seq_full, then crc_full ==
28+
* crc32_le_combine(crc1, crc2, len2) when crc_full was seeded
29+
* with the same initializer as crc1, and crc2 seed was 0. See
30+
* also crc32_combine_test().
31+
*/
32+
extern u32 crc32_le_combine(u32 crc1, u32 crc2, size_t len2);
33+
1434
extern u32 __crc32c_le(u32 crc, unsigned char const *p, size_t len);
1535

36+
/**
37+
* __crc32c_le_combine - Combine two crc32c check values into one. For two
38+
* sequences of bytes, seq1 and seq2 with lengths len1
39+
* and len2, __crc32c_le() check values were calculated
40+
* for each, crc1 and crc2.
41+
*
42+
* @crc1: crc32c of the first block
43+
* @crc2: crc32c of the second block
44+
* @len2: length of the second block
45+
*
46+
* Return: The __crc32c_le() check value of seq1 and seq2 concatenated,
47+
* requiring only crc1, crc2, and len2. Note: If seq_full denotes
48+
* the concatenated memory area of seq1 with seq2, and crc_full
49+
* the __crc32c_le() value of seq_full, then crc_full ==
50+
* __crc32c_le_combine(crc1, crc2, len2) when crc_full was
51+
* seeded with the same initializer as crc1, and crc2 seed
52+
* was 0. See also crc32c_combine_test().
53+
*/
54+
extern u32 __crc32c_le_combine(u32 crc1, u32 crc2, size_t len2);
55+
1656
#define crc32(seed, data, length) crc32_le(seed, (unsigned char const *)(data), length)
1757

1858
/*

lib/crc32.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
4949
MODULE_DESCRIPTION("Various CRC32 calculations");
5050
MODULE_LICENSE("GPL");
5151

52+
#define GF2_DIM 32
53+
54+
static u32 gf2_matrix_times(u32 *mat, u32 vec)
55+
{
56+
u32 sum = 0;
57+
58+
while (vec) {
59+
if (vec & 1)
60+
sum ^= *mat;
61+
vec >>= 1;
62+
mat++;
63+
}
64+
65+
return sum;
66+
}
67+
68+
static void gf2_matrix_square(u32 *square, u32 *mat)
69+
{
70+
int i;
71+
72+
for (i = 0; i < GF2_DIM; i++)
73+
square[i] = gf2_matrix_times(mat, mat[i]);
74+
}
75+
5276
#if CRC_LE_BITS > 8 || CRC_BE_BITS > 8
5377

5478
/* implements slicing-by-4 or slicing-by-8 algorithm */
@@ -130,6 +154,52 @@ crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
130154
}
131155
#endif
132156

157+
/* For conditions of distribution and use, see copyright notice in zlib.h */
158+
static u32 crc32_generic_combine(u32 crc1, u32 crc2, size_t len2,
159+
u32 polynomial)
160+
{
161+
u32 even[GF2_DIM]; /* Even-power-of-two zeros operator */
162+
u32 odd[GF2_DIM]; /* Odd-power-of-two zeros operator */
163+
u32 row;
164+
int i;
165+
166+
if (len2 <= 0)
167+
return crc1;
168+
169+
/* Put operator for one zero bit in odd */
170+
odd[0] = polynomial;
171+
row = 1;
172+
for (i = 1; i < GF2_DIM; i++) {
173+
odd[i] = row;
174+
row <<= 1;
175+
}
176+
177+
gf2_matrix_square(even, odd); /* Put operator for two zero bits in even */
178+
gf2_matrix_square(odd, even); /* Put operator for four zero bits in odd */
179+
180+
/* Apply len2 zeros to crc1 (first square will put the operator for one
181+
* zero byte, eight zero bits, in even).
182+
*/
183+
do {
184+
/* Apply zeros operator for this bit of len2 */
185+
gf2_matrix_square(even, odd);
186+
if (len2 & 1)
187+
crc1 = gf2_matrix_times(even, crc1);
188+
len2 >>= 1;
189+
/* If no more bits set, then done */
190+
if (len2 == 0)
191+
break;
192+
/* Another iteration of the loop with odd and even swapped */
193+
gf2_matrix_square(odd, even);
194+
if (len2 & 1)
195+
crc1 = gf2_matrix_times(odd, crc1);
196+
len2 >>= 1;
197+
} while (len2 != 0);
198+
199+
crc1 ^= crc2;
200+
return crc1;
201+
}
202+
133203
/**
134204
* crc32_le_generic() - Calculate bitwise little-endian Ethernet AUTODIN II
135205
* CRC32/CRC32C
@@ -200,8 +270,19 @@ u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len)
200270
(const u32 (*)[256])crc32ctable_le, CRC32C_POLY_LE);
201271
}
202272
#endif
273+
u32 __pure crc32_le_combine(u32 crc1, u32 crc2, size_t len2)
274+
{
275+
return crc32_generic_combine(crc1, crc2, len2, CRCPOLY_LE);
276+
}
277+
278+
u32 __pure __crc32c_le_combine(u32 crc1, u32 crc2, size_t len2)
279+
{
280+
return crc32_generic_combine(crc1, crc2, len2, CRC32C_POLY_LE);
281+
}
203282
EXPORT_SYMBOL(crc32_le);
283+
EXPORT_SYMBOL(crc32_le_combine);
204284
EXPORT_SYMBOL(__crc32c_le);
285+
EXPORT_SYMBOL(__crc32c_le_combine);
205286

206287
/**
207288
* crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32

0 commit comments

Comments
 (0)