Skip to content

Commit 16739ef

Browse files
ardbiesheuvelherbertx
authored andcommitted
crypto: crc32c - Provide crc32c-arch driver for accelerated library code
crc32c-generic is currently backed by the architecture's CRC-32c 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 crc32c-generic is the reference driver that the fuzzing logic uses as a source of truth. Fix this by providing a crc32c-arch implementation which is based on the arch library code if available, and modify crc32c-generic so it is always based on the generic C implementation. If the arch has no CRC-32c library code, this change does nothing. Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent a37e557 commit 16739ef

File tree

3 files changed

+75
-22
lines changed

3 files changed

+75
-22
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_crc32c_generic.o += -DARCH=$(ARCH)
158159
CFLAGS_crc32_generic.o += -DARCH=$(ARCH)
159160
obj-$(CONFIG_CRYPTO_CRCT10DIF) += crct10dif_common.o crct10dif_generic.o
160161
obj-$(CONFIG_CRYPTO_CRC64_ROCKSOFT) += crc64_rocksoft_generic.o

crypto/crc32c_generic.c

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ static int chksum_update(struct shash_desc *desc, const u8 *data,
8585
{
8686
struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
8787

88+
ctx->crc = __crc32c_le_base(ctx->crc, data, length);
89+
return 0;
90+
}
91+
92+
static int chksum_update_arch(struct shash_desc *desc, const u8 *data,
93+
unsigned int length)
94+
{
95+
struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
96+
8897
ctx->crc = __crc32c_le(ctx->crc, data, length);
8998
return 0;
9099
}
@@ -98,6 +107,13 @@ static int chksum_final(struct shash_desc *desc, u8 *out)
98107
}
99108

100109
static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
110+
{
111+
put_unaligned_le32(~__crc32c_le_base(*crcp, data, len), out);
112+
return 0;
113+
}
114+
115+
static int __chksum_finup_arch(u32 *crcp, const u8 *data, unsigned int len,
116+
u8 *out)
101117
{
102118
put_unaligned_le32(~__crc32c_le(*crcp, data, len), out);
103119
return 0;
@@ -111,6 +127,14 @@ static int chksum_finup(struct shash_desc *desc, const u8 *data,
111127
return __chksum_finup(&ctx->crc, data, len, out);
112128
}
113129

130+
static int chksum_finup_arch(struct shash_desc *desc, const u8 *data,
131+
unsigned int len, u8 *out)
132+
{
133+
struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
134+
135+
return __chksum_finup_arch(&ctx->crc, data, len, out);
136+
}
137+
114138
static int chksum_digest(struct shash_desc *desc, const u8 *data,
115139
unsigned int length, u8 *out)
116140
{
@@ -119,6 +143,14 @@ static int chksum_digest(struct shash_desc *desc, const u8 *data,
119143
return __chksum_finup(&mctx->key, data, length, out);
120144
}
121145

146+
static int chksum_digest_arch(struct shash_desc *desc, const u8 *data,
147+
unsigned int length, u8 *out)
148+
{
149+
struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
150+
151+
return __chksum_finup_arch(&mctx->key, data, length, out);
152+
}
153+
122154
static int crc32c_cra_init(struct crypto_tfm *tfm)
123155
{
124156
struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
@@ -127,35 +159,53 @@ static int crc32c_cra_init(struct crypto_tfm *tfm)
127159
return 0;
128160
}
129161

130-
static struct shash_alg alg = {
131-
.digestsize = CHKSUM_DIGEST_SIZE,
132-
.setkey = chksum_setkey,
133-
.init = chksum_init,
134-
.update = chksum_update,
135-
.final = chksum_final,
136-
.finup = chksum_finup,
137-
.digest = chksum_digest,
138-
.descsize = sizeof(struct chksum_desc_ctx),
139-
.base = {
140-
.cra_name = "crc32c",
141-
.cra_driver_name = "crc32c-generic",
142-
.cra_priority = 100,
143-
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
144-
.cra_blocksize = CHKSUM_BLOCK_SIZE,
145-
.cra_ctxsize = sizeof(struct chksum_ctx),
146-
.cra_module = THIS_MODULE,
147-
.cra_init = crc32c_cra_init,
148-
}
149-
};
162+
static struct shash_alg algs[] = {{
163+
.digestsize = CHKSUM_DIGEST_SIZE,
164+
.setkey = chksum_setkey,
165+
.init = chksum_init,
166+
.update = chksum_update,
167+
.final = chksum_final,
168+
.finup = chksum_finup,
169+
.digest = chksum_digest,
170+
.descsize = sizeof(struct chksum_desc_ctx),
171+
172+
.base.cra_name = "crc32c",
173+
.base.cra_driver_name = "crc32c-generic",
174+
.base.cra_priority = 100,
175+
.base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
176+
.base.cra_blocksize = CHKSUM_BLOCK_SIZE,
177+
.base.cra_ctxsize = sizeof(struct chksum_ctx),
178+
.base.cra_module = THIS_MODULE,
179+
.base.cra_init = crc32c_cra_init,
180+
}, {
181+
.digestsize = CHKSUM_DIGEST_SIZE,
182+
.setkey = chksum_setkey,
183+
.init = chksum_init,
184+
.update = chksum_update_arch,
185+
.final = chksum_final,
186+
.finup = chksum_finup_arch,
187+
.digest = chksum_digest_arch,
188+
.descsize = sizeof(struct chksum_desc_ctx),
189+
190+
.base.cra_name = "crc32c",
191+
.base.cra_driver_name = "crc32c-" __stringify(ARCH),
192+
.base.cra_priority = 150,
193+
.base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
194+
.base.cra_blocksize = CHKSUM_BLOCK_SIZE,
195+
.base.cra_ctxsize = sizeof(struct chksum_ctx),
196+
.base.cra_module = THIS_MODULE,
197+
.base.cra_init = crc32c_cra_init,
198+
}};
150199

151200
static int __init crc32c_mod_init(void)
152201
{
153-
return crypto_register_shash(&alg);
202+
/* register the arch flavor only if it differs from the generic one */
203+
return crypto_register_shashes(algs, 1 + (&__crc32c_le != &__crc32c_le_base));
154204
}
155205

156206
static void __exit crc32c_mod_fini(void)
157207
{
158-
crypto_unregister_shash(&alg);
208+
crypto_unregister_shashes(algs, 1 + (&__crc32c_le != &__crc32c_le_base));
159209
}
160210

161211
subsys_initcall(crc32c_mod_init);

lib/crc32.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ u32 __pure crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le);
208208
EXPORT_SYMBOL(crc32_le_base);
209209

210210
u32 __pure __crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
211+
EXPORT_SYMBOL(__crc32c_le_base);
212+
211213
u32 __pure crc32_be_base(u32, unsigned char const *, size_t) __alias(crc32_be);
212214

213215
/*

0 commit comments

Comments
 (0)