]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
crypto: crc32 - Provide crc32-arch driver for accelerated library code
authorArd Biesheuvel <ardb@kernel.org>
Wed, 16 Oct 2024 18:57:24 +0000 (20:57 +0200)
committerHerbert Xu <herbert@gondor.apana.org.au>
Mon, 28 Oct 2024 10:33:10 +0000 (18:33 +0800)
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 <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/Makefile
crypto/crc32_generic.c
lib/crc32.c

index 81be78d39c2d72f6cffead9860de2c14615c9b77..3e49a820f148deccc8869c0b6492ed6149a2bf26 100644 (file)
@@ -155,6 +155,7 @@ obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
 obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
 obj-$(CONFIG_CRYPTO_CRC32C) += crc32c_generic.o
 obj-$(CONFIG_CRYPTO_CRC32) += crc32_generic.o
+CFLAGS_crc32_generic.o += -DARCH=$(ARCH)
 obj-$(CONFIG_CRYPTO_CRCT10DIF) += crct10dif_common.o crct10dif_generic.o
 obj-$(CONFIG_CRYPTO_CRC64_ROCKSOFT) += crc64_rocksoft_generic.o
 obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o authencesn.o
index a989cb44fd16081c01966071ac8203108f259236..8d6e1baec509e6e08c4a53a3f29e8508e1661967 100644 (file)
@@ -59,6 +59,15 @@ static int crc32_update(struct shash_desc *desc, const u8 *data,
 {
        u32 *crcp = shash_desc_ctx(desc);
 
+       *crcp = crc32_le_base(*crcp, data, len);
+       return 0;
+}
+
+static int crc32_update_arch(struct shash_desc *desc, const u8 *data,
+                            unsigned int len)
+{
+       u32 *crcp = shash_desc_ctx(desc);
+
        *crcp = crc32_le(*crcp, data, len);
        return 0;
 }
@@ -66,6 +75,13 @@ static int crc32_update(struct shash_desc *desc, const u8 *data,
 /* No final XOR 0xFFFFFFFF, like crc32_le */
 static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
                         u8 *out)
+{
+       put_unaligned_le32(crc32_le_base(*crcp, data, len), out);
+       return 0;
+}
+
+static int __crc32_finup_arch(u32 *crcp, const u8 *data, unsigned int len,
+                             u8 *out)
 {
        put_unaligned_le32(crc32_le(*crcp, data, len), out);
        return 0;
@@ -77,6 +93,12 @@ static int crc32_finup(struct shash_desc *desc, const u8 *data,
        return __crc32_finup(shash_desc_ctx(desc), data, len, out);
 }
 
+static int crc32_finup_arch(struct shash_desc *desc, const u8 *data,
+                      unsigned int len, u8 *out)
+{
+       return __crc32_finup_arch(shash_desc_ctx(desc), data, len, out);
+}
+
 static int crc32_final(struct shash_desc *desc, u8 *out)
 {
        u32 *crcp = shash_desc_ctx(desc);
@@ -88,38 +110,62 @@ static int crc32_final(struct shash_desc *desc, u8 *out)
 static int crc32_digest(struct shash_desc *desc, const u8 *data,
                        unsigned int len, u8 *out)
 {
-       return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
-                            out);
+       return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len, out);
 }
-static struct shash_alg alg = {
-       .setkey         = crc32_setkey,
-       .init           = crc32_init,
-       .update         = crc32_update,
-       .final          = crc32_final,
-       .finup          = crc32_finup,
-       .digest         = crc32_digest,
-       .descsize       = sizeof(u32),
-       .digestsize     = CHKSUM_DIGEST_SIZE,
-       .base           = {
-               .cra_name               = "crc32",
-               .cra_driver_name        = "crc32-generic",
-               .cra_priority           = 100,
-               .cra_flags              = CRYPTO_ALG_OPTIONAL_KEY,
-               .cra_blocksize          = CHKSUM_BLOCK_SIZE,
-               .cra_ctxsize            = sizeof(u32),
-               .cra_module             = THIS_MODULE,
-               .cra_init               = crc32_cra_init,
-       }
-};
+
+static int crc32_digest_arch(struct shash_desc *desc, const u8 *data,
+                            unsigned int len, u8 *out)
+{
+       return __crc32_finup_arch(crypto_shash_ctx(desc->tfm), data, len, out);
+}
+
+static struct shash_alg algs[] = {{
+       .setkey                 = crc32_setkey,
+       .init                   = crc32_init,
+       .update                 = crc32_update,
+       .final                  = crc32_final,
+       .finup                  = crc32_finup,
+       .digest                 = crc32_digest,
+       .descsize               = sizeof(u32),
+       .digestsize             = CHKSUM_DIGEST_SIZE,
+
+       .base.cra_name          = "crc32",
+       .base.cra_driver_name   = "crc32-generic",
+       .base.cra_priority      = 100,
+       .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
+       .base.cra_blocksize     = CHKSUM_BLOCK_SIZE,
+       .base.cra_ctxsize       = sizeof(u32),
+       .base.cra_module        = THIS_MODULE,
+       .base.cra_init          = crc32_cra_init,
+}, {
+       .setkey                 = crc32_setkey,
+       .init                   = crc32_init,
+       .update                 = crc32_update_arch,
+       .final                  = crc32_final,
+       .finup                  = crc32_finup_arch,
+       .digest                 = crc32_digest_arch,
+       .descsize               = sizeof(u32),
+       .digestsize             = CHKSUM_DIGEST_SIZE,
+
+       .base.cra_name          = "crc32",
+       .base.cra_driver_name   = "crc32-" __stringify(ARCH),
+       .base.cra_priority      = 150,
+       .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
+       .base.cra_blocksize     = CHKSUM_BLOCK_SIZE,
+       .base.cra_ctxsize       = sizeof(u32),
+       .base.cra_module        = THIS_MODULE,
+       .base.cra_init          = crc32_cra_init,
+}};
 
 static int __init crc32_mod_init(void)
 {
-       return crypto_register_shash(&alg);
+       /* register the arch flavor only if it differs from the generic one */
+       return crypto_register_shashes(algs, 1 + (&crc32_le != &crc32_le_base));
 }
 
 static void __exit crc32_mod_fini(void)
 {
-       crypto_unregister_shash(&alg);
+       crypto_unregister_shashes(algs, 1 + (&crc32_le != &crc32_le_base));
 }
 
 subsys_initcall(crc32_mod_init);
index 5649847d0a8d0bfc61481b32c7edfa196b5edb48..a54ba87b70730bd1856e8a99ab55695777bdfad3 100644 (file)
@@ -205,6 +205,8 @@ EXPORT_SYMBOL(crc32_le);
 EXPORT_SYMBOL(__crc32c_le);
 
 u32 __pure crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le);
+EXPORT_SYMBOL(crc32_le_base);
+
 u32 __pure __crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
 u32 __pure crc32_be_base(u32, unsigned char const *, size_t) __alias(crc32_be);