~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/crypto/crc32_generic.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * Copyright 2012 Xyratex Technology Limited
  4  */
  5 
  6 /*
  7  * This is crypto api shash wrappers to crc32_le.
  8  */
  9 
 10 #include <asm/unaligned.h>
 11 #include <linux/crc32.h>
 12 #include <crypto/internal/hash.h>
 13 #include <linux/init.h>
 14 #include <linux/module.h>
 15 #include <linux/string.h>
 16 #include <linux/kernel.h>
 17 
 18 #define CHKSUM_BLOCK_SIZE       1
 19 #define CHKSUM_DIGEST_SIZE      4
 20 
 21 /** No default init with ~0 */
 22 static int crc32_cra_init(struct crypto_tfm *tfm)
 23 {
 24         u32 *key = crypto_tfm_ctx(tfm);
 25 
 26         *key = 0;
 27 
 28         return 0;
 29 }
 30 
 31 /*
 32  * Setting the seed allows arbitrary accumulators and flexible XOR policy
 33  * If your algorithm starts with ~0, then XOR with ~0 before you set
 34  * the seed.
 35  */
 36 static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
 37                         unsigned int keylen)
 38 {
 39         u32 *mctx = crypto_shash_ctx(hash);
 40 
 41         if (keylen != sizeof(u32))
 42                 return -EINVAL;
 43         *mctx = get_unaligned_le32(key);
 44         return 0;
 45 }
 46 
 47 static int crc32_init(struct shash_desc *desc)
 48 {
 49         u32 *mctx = crypto_shash_ctx(desc->tfm);
 50         u32 *crcp = shash_desc_ctx(desc);
 51 
 52         *crcp = *mctx;
 53 
 54         return 0;
 55 }
 56 
 57 static int crc32_update(struct shash_desc *desc, const u8 *data,
 58                         unsigned int len)
 59 {
 60         u32 *crcp = shash_desc_ctx(desc);
 61 
 62         *crcp = crc32_le(*crcp, data, len);
 63         return 0;
 64 }
 65 
 66 /* No final XOR 0xFFFFFFFF, like crc32_le */
 67 static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
 68                          u8 *out)
 69 {
 70         put_unaligned_le32(crc32_le(*crcp, data, len), out);
 71         return 0;
 72 }
 73 
 74 static int crc32_finup(struct shash_desc *desc, const u8 *data,
 75                        unsigned int len, u8 *out)
 76 {
 77         return __crc32_finup(shash_desc_ctx(desc), data, len, out);
 78 }
 79 
 80 static int crc32_final(struct shash_desc *desc, u8 *out)
 81 {
 82         u32 *crcp = shash_desc_ctx(desc);
 83 
 84         put_unaligned_le32(*crcp, out);
 85         return 0;
 86 }
 87 
 88 static int crc32_digest(struct shash_desc *desc, const u8 *data,
 89                         unsigned int len, u8 *out)
 90 {
 91         return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
 92                              out);
 93 }
 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 };
114 
115 static int __init crc32_mod_init(void)
116 {
117         return crypto_register_shash(&alg);
118 }
119 
120 static void __exit crc32_mod_fini(void)
121 {
122         crypto_unregister_shash(&alg);
123 }
124 
125 subsys_initcall(crc32_mod_init);
126 module_exit(crc32_mod_fini);
127 
128 MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
129 MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
130 MODULE_LICENSE("GPL");
131 MODULE_ALIAS_CRYPTO("crc32");
132 MODULE_ALIAS_CRYPTO("crc32-generic");
133 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php