]>
git.ipfire.org Git - thirdparty/linux.git/blob - crypto/crc32_generic.c
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2012 Xyratex Technology Limited
7 * This is crypto api shash wrappers to crc32_le.
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>
18 #define CHKSUM_BLOCK_SIZE 1
19 #define CHKSUM_DIGEST_SIZE 4
21 /** No default init with ~0 */
22 static int crc32_cra_init(struct crypto_tfm
*tfm
)
24 u32
*key
= crypto_tfm_ctx(tfm
);
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
36 static int crc32_setkey(struct crypto_shash
*hash
, const u8
*key
,
39 u32
*mctx
= crypto_shash_ctx(hash
);
41 if (keylen
!= sizeof(u32
))
43 *mctx
= get_unaligned_le32(key
);
47 static int crc32_init(struct shash_desc
*desc
)
49 u32
*mctx
= crypto_shash_ctx(desc
->tfm
);
50 u32
*crcp
= shash_desc_ctx(desc
);
57 static int crc32_update(struct shash_desc
*desc
, const u8
*data
,
60 u32
*crcp
= shash_desc_ctx(desc
);
62 *crcp
= crc32_le(*crcp
, data
, len
);
66 /* No final XOR 0xFFFFFFFF, like crc32_le */
67 static int __crc32_finup(u32
*crcp
, const u8
*data
, unsigned int len
,
70 put_unaligned_le32(crc32_le(*crcp
, data
, len
), out
);
74 static int crc32_finup(struct shash_desc
*desc
, const u8
*data
,
75 unsigned int len
, u8
*out
)
77 return __crc32_finup(shash_desc_ctx(desc
), data
, len
, out
);
80 static int crc32_final(struct shash_desc
*desc
, u8
*out
)
82 u32
*crcp
= shash_desc_ctx(desc
);
84 put_unaligned_le32(*crcp
, out
);
88 static int crc32_digest(struct shash_desc
*desc
, const u8
*data
,
89 unsigned int len
, u8
*out
)
91 return __crc32_finup(crypto_shash_ctx(desc
->tfm
), data
, len
,
94 static struct shash_alg alg
= {
95 .setkey
= crc32_setkey
,
97 .update
= crc32_update
,
100 .digest
= crc32_digest
,
101 .descsize
= sizeof(u32
),
102 .digestsize
= CHKSUM_DIGEST_SIZE
,
105 .cra_driver_name
= "crc32-generic",
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
,
115 static int __init
crc32_mod_init(void)
117 return crypto_register_shash(&alg
);
120 static void __exit
crc32_mod_fini(void)
122 crypto_unregister_shash(&alg
);
125 subsys_initcall(crc32_mod_init
);
126 module_exit(crc32_mod_fini
);
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");