0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <asm/unaligned.h>
0011 #include <linux/crc32.h>
0012 #include <crypto/internal/hash.h>
0013 #include <linux/init.h>
0014 #include <linux/module.h>
0015 #include <linux/string.h>
0016 #include <linux/kernel.h>
0017
0018 #define CHKSUM_BLOCK_SIZE 1
0019 #define CHKSUM_DIGEST_SIZE 4
0020
0021
0022 static int crc32_cra_init(struct crypto_tfm *tfm)
0023 {
0024 u32 *key = crypto_tfm_ctx(tfm);
0025
0026 *key = 0;
0027
0028 return 0;
0029 }
0030
0031
0032
0033
0034
0035
0036 static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
0037 unsigned int keylen)
0038 {
0039 u32 *mctx = crypto_shash_ctx(hash);
0040
0041 if (keylen != sizeof(u32))
0042 return -EINVAL;
0043 *mctx = get_unaligned_le32(key);
0044 return 0;
0045 }
0046
0047 static int crc32_init(struct shash_desc *desc)
0048 {
0049 u32 *mctx = crypto_shash_ctx(desc->tfm);
0050 u32 *crcp = shash_desc_ctx(desc);
0051
0052 *crcp = *mctx;
0053
0054 return 0;
0055 }
0056
0057 static int crc32_update(struct shash_desc *desc, const u8 *data,
0058 unsigned int len)
0059 {
0060 u32 *crcp = shash_desc_ctx(desc);
0061
0062 *crcp = crc32_le(*crcp, data, len);
0063 return 0;
0064 }
0065
0066
0067 static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
0068 u8 *out)
0069 {
0070 put_unaligned_le32(crc32_le(*crcp, data, len), out);
0071 return 0;
0072 }
0073
0074 static int crc32_finup(struct shash_desc *desc, const u8 *data,
0075 unsigned int len, u8 *out)
0076 {
0077 return __crc32_finup(shash_desc_ctx(desc), data, len, out);
0078 }
0079
0080 static int crc32_final(struct shash_desc *desc, u8 *out)
0081 {
0082 u32 *crcp = shash_desc_ctx(desc);
0083
0084 put_unaligned_le32(*crcp, out);
0085 return 0;
0086 }
0087
0088 static int crc32_digest(struct shash_desc *desc, const u8 *data,
0089 unsigned int len, u8 *out)
0090 {
0091 return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
0092 out);
0093 }
0094 static struct shash_alg alg = {
0095 .setkey = crc32_setkey,
0096 .init = crc32_init,
0097 .update = crc32_update,
0098 .final = crc32_final,
0099 .finup = crc32_finup,
0100 .digest = crc32_digest,
0101 .descsize = sizeof(u32),
0102 .digestsize = CHKSUM_DIGEST_SIZE,
0103 .base = {
0104 .cra_name = "crc32",
0105 .cra_driver_name = "crc32-generic",
0106 .cra_priority = 100,
0107 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
0108 .cra_blocksize = CHKSUM_BLOCK_SIZE,
0109 .cra_ctxsize = sizeof(u32),
0110 .cra_module = THIS_MODULE,
0111 .cra_init = crc32_cra_init,
0112 }
0113 };
0114
0115 static int __init crc32_mod_init(void)
0116 {
0117 return crypto_register_shash(&alg);
0118 }
0119
0120 static void __exit crc32_mod_fini(void)
0121 {
0122 crypto_unregister_shash(&alg);
0123 }
0124
0125 subsys_initcall(crc32_mod_init);
0126 module_exit(crc32_mod_fini);
0127
0128 MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
0129 MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
0130 MODULE_LICENSE("GPL");
0131 MODULE_ALIAS_CRYPTO("crc32");
0132 MODULE_ALIAS_CRYPTO("crc32-generic");