File Index
Files and directories
Sign in
FILE

crc32.c

File preview with syntax highlighting.

linux-6.19.14/crypto/crc32.c C
  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 <linux/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 */
 22static 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 */
 36static 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
 47static 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
 57static 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 */
 67static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
 68{
 69	put_unaligned_le32(crc32_le(*crcp, data, len), out);
 70	return 0;
 71}
 72
 73static int crc32_finup(struct shash_desc *desc, const u8 *data,
 74		       unsigned int len, u8 *out)
 75{
 76	return __crc32_finup(shash_desc_ctx(desc), data, len, out);
 77}
 78
 79static int crc32_final(struct shash_desc *desc, u8 *out)
 80{
 81	u32 *crcp = shash_desc_ctx(desc);
 82
 83	put_unaligned_le32(*crcp, out);
 84	return 0;
 85}
 86
 87static int crc32_digest(struct shash_desc *desc, const u8 *data,
 88			unsigned int len, u8 *out)
 89{
 90	return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len, out);
 91}
 92
 93static struct shash_alg alg = {
 94	.setkey			= crc32_setkey,
 95	.init			= crc32_init,
 96	.update			= crc32_update,
 97	.final			= crc32_final,
 98	.finup			= crc32_finup,
 99	.digest			= crc32_digest,
100	.descsize		= sizeof(u32),
101	.digestsize		= CHKSUM_DIGEST_SIZE,
102
103	.base.cra_name		= "crc32",
104	.base.cra_driver_name	= "crc32-lib",
105	.base.cra_priority	= 100,
106	.base.cra_flags		= CRYPTO_ALG_OPTIONAL_KEY,
107	.base.cra_blocksize	= CHKSUM_BLOCK_SIZE,
108	.base.cra_ctxsize	= sizeof(u32),
109	.base.cra_module	= THIS_MODULE,
110	.base.cra_init		= crc32_cra_init,
111};
112
113static int __init crc32_mod_init(void)
114{
115	return crypto_register_shash(&alg);
116}
117
118static void __exit crc32_mod_fini(void)
119{
120	crypto_unregister_shash(&alg);
121}
122
123module_init(crc32_mod_init);
124module_exit(crc32_mod_fini);
125
126MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
127MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
128MODULE_LICENSE("GPL");
129MODULE_ALIAS_CRYPTO("crc32");
~/ Read-only file preview.