Skip to content

Commit a37e557

Browse files
ardbiesheuvelherbertx
authored andcommitted
crypto: crc32 - Provide crc32-arch driver for accelerated library code
crc32-generic is currently backed by the architecture's CRC-32 library code, which may offer a variety of implementations depending on the capabilities of the platform. These are not covered by the crypto subsystem's fuzz testing capabilities because crc32-generic is the reference driver that the fuzzing logic uses as a source of truth. Fix this by providing a crc32-arch implementation which is based on the arch library code if available, and modify crc32-generic so it is always based on the generic C implementation. If the arch has no CRC-32 library code, this change does nothing. Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent a1ba229 commit a37e557

File tree

3 files changed

+73
-24
lines changed

3 files changed

+73
-24
lines changed

crypto/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
155155
obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
156156
obj-$(CONFIG_CRYPTO_CRC32C) += crc32c_generic.o
157157
obj-$(CONFIG_CRYPTO_CRC32) += crc32_generic.o
158+
CFLAGS_crc32_generic.o += -DARCH=$(ARCH)
158159
obj-$(CONFIG_CRYPTO_CRCT10DIF) += crct10dif_common.o crct10dif_generic.o
159160
obj-$(CONFIG_CRYPTO_CRC64_ROCKSOFT) += crc64_rocksoft_generic.o
160161
obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o authencesn.o

crypto/crc32_generic.c

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,29 @@ static int crc32_update(struct shash_desc *desc, const u8 *data,
5959
{
6060
u32 *crcp = shash_desc_ctx(desc);
6161

62+
*crcp = crc32_le_base(*crcp, data, len);
63+
return 0;
64+
}
65+
66+
static int crc32_update_arch(struct shash_desc *desc, const u8 *data,
67+
unsigned int len)
68+
{
69+
u32 *crcp = shash_desc_ctx(desc);
70+
6271
*crcp = crc32_le(*crcp, data, len);
6372
return 0;
6473
}
6574

6675
/* No final XOR 0xFFFFFFFF, like crc32_le */
6776
static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
6877
u8 *out)
78+
{
79+
put_unaligned_le32(crc32_le_base(*crcp, data, len), out);
80+
return 0;
81+
}
82+
83+
static int __crc32_finup_arch(u32 *crcp, const u8 *data, unsigned int len,
84+
u8 *out)
6985
{
7086
put_unaligned_le32(crc32_le(*crcp, data, len), out);
7187
return 0;
@@ -77,6 +93,12 @@ static int crc32_finup(struct shash_desc *desc, const u8 *data,
7793
return __crc32_finup(shash_desc_ctx(desc), data, len, out);
7894
}
7995

96+
static int crc32_finup_arch(struct shash_desc *desc, const u8 *data,
97+
unsigned int len, u8 *out)
98+
{
99+
return __crc32_finup_arch(shash_desc_ctx(desc), data, len, out);
100+
}
101+
80102
static int crc32_final(struct shash_desc *desc, u8 *out)
81103
{
82104
u32 *crcp = shash_desc_ctx(desc);
@@ -88,38 +110,62 @@ static int crc32_final(struct shash_desc *desc, u8 *out)
88110
static int crc32_digest(struct shash_desc *desc, const u8 *data,
89111
unsigned int len, u8 *out)
90112
{
91-
return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
92-
out);
113+
return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len, out);
93114
}
94-
static struct shash_alg alg = {
95-
.setkey = crc32_setkey,
96-
.init = crc32_init,
97-
.update = crc32_update,
98-
.final = crc32_final,
99-
.finup = crc32_finup,
100-
.digest = crc32_digest,
101-
.descsize = sizeof(u32),
102-
.digestsize = CHKSUM_DIGEST_SIZE,
103-
.base = {
104-
.cra_name = "crc32",
105-
.cra_driver_name = "crc32-generic",
106-
.cra_priority = 100,
107-
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
108-
.cra_blocksize = CHKSUM_BLOCK_SIZE,
109-
.cra_ctxsize = sizeof(u32),
110-
.cra_module = THIS_MODULE,
111-
.cra_init = crc32_cra_init,
112-
}
113-
};
115+
116+
static int crc32_digest_arch(struct shash_desc *desc, const u8 *data,
117+
unsigned int len, u8 *out)
118+
{
119+
return __crc32_finup_arch(crypto_shash_ctx(desc->tfm), data, len, out);
120+
}
121+
122+
static struct shash_alg algs[] = {{
123+
.setkey = crc32_setkey,
124+
.init = crc32_init,
125+
.update = crc32_update,
126+
.final = crc32_final,
127+
.finup = crc32_finup,
128+
.digest = crc32_digest,
129+
.descsize = sizeof(u32),
130+
.digestsize = CHKSUM_DIGEST_SIZE,
131+
132+
.base.cra_name = "crc32",
133+
.base.cra_driver_name = "crc32-generic",
134+
.base.cra_priority = 100,
135+
.base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
136+
.base.cra_blocksize = CHKSUM_BLOCK_SIZE,
137+
.base.cra_ctxsize = sizeof(u32),
138+
.base.cra_module = THIS_MODULE,
139+
.base.cra_init = crc32_cra_init,
140+
}, {
141+
.setkey = crc32_setkey,
142+
.init = crc32_init,
143+
.update = crc32_update_arch,
144+
.final = crc32_final,
145+
.finup = crc32_finup_arch,
146+
.digest = crc32_digest_arch,
147+
.descsize = sizeof(u32),
148+
.digestsize = CHKSUM_DIGEST_SIZE,
149+
150+
.base.cra_name = "crc32",
151+
.base.cra_driver_name = "crc32-" __stringify(ARCH),
152+
.base.cra_priority = 150,
153+
.base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
154+
.base.cra_blocksize = CHKSUM_BLOCK_SIZE,
155+
.base.cra_ctxsize = sizeof(u32),
156+
.base.cra_module = THIS_MODULE,
157+
.base.cra_init = crc32_cra_init,
158+
}};
114159

115160
static int __init crc32_mod_init(void)
116161
{
117-
return crypto_register_shash(&alg);
162+
/* register the arch flavor only if it differs from the generic one */
163+
return crypto_register_shashes(algs, 1 + (&crc32_le != &crc32_le_base));
118164
}
119165

120166
static void __exit crc32_mod_fini(void)
121167
{
122-
crypto_unregister_shash(&alg);
168+
crypto_unregister_shashes(algs, 1 + (&crc32_le != &crc32_le_base));
123169
}
124170

125171
subsys_initcall(crc32_mod_init);

lib/crc32.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ EXPORT_SYMBOL(crc32_le);
205205
EXPORT_SYMBOL(__crc32c_le);
206206

207207
u32 __pure crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le);
208+
EXPORT_SYMBOL(crc32_le_base);
209+
208210
u32 __pure __crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
209211
u32 __pure crc32_be_base(u32, unsigned char const *, size_t) __alias(crc32_be);
210212

0 commit comments

Comments
 (0)