Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / crypto / crc32_generic.c
blob6a55d206fab31714e778adca6fb482c68c76349c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2012 Xyratex Technology Limited
4 */
6 /*
7 * This is crypto api shash wrappers to crc32_le.
8 */
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>
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);
26 *key = 0;
28 return 0;
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.
36 static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
37 unsigned int keylen)
39 u32 *mctx = crypto_shash_ctx(hash);
41 if (keylen != sizeof(u32))
42 return -EINVAL;
43 *mctx = get_unaligned_le32(key);
44 return 0;
47 static int crc32_init(struct shash_desc *desc)
49 u32 *mctx = crypto_shash_ctx(desc->tfm);
50 u32 *crcp = shash_desc_ctx(desc);
52 *crcp = *mctx;
54 return 0;
57 static int crc32_update(struct shash_desc *desc, const u8 *data,
58 unsigned int len)
60 u32 *crcp = shash_desc_ctx(desc);
62 *crcp = crc32_le_base(*crcp, data, len);
63 return 0;
66 static int crc32_update_arch(struct shash_desc *desc, const u8 *data,
67 unsigned int len)
69 u32 *crcp = shash_desc_ctx(desc);
71 *crcp = crc32_le(*crcp, data, len);
72 return 0;
75 /* No final XOR 0xFFFFFFFF, like crc32_le */
76 static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
77 u8 *out)
79 put_unaligned_le32(crc32_le_base(*crcp, data, len), out);
80 return 0;
83 static int __crc32_finup_arch(u32 *crcp, const u8 *data, unsigned int len,
84 u8 *out)
86 put_unaligned_le32(crc32_le(*crcp, data, len), out);
87 return 0;
90 static int crc32_finup(struct shash_desc *desc, const u8 *data,
91 unsigned int len, u8 *out)
93 return __crc32_finup(shash_desc_ctx(desc), data, len, out);
96 static int crc32_finup_arch(struct shash_desc *desc, const u8 *data,
97 unsigned int len, u8 *out)
99 return __crc32_finup_arch(shash_desc_ctx(desc), data, len, out);
102 static int crc32_final(struct shash_desc *desc, u8 *out)
104 u32 *crcp = shash_desc_ctx(desc);
106 put_unaligned_le32(*crcp, out);
107 return 0;
110 static int crc32_digest(struct shash_desc *desc, const u8 *data,
111 unsigned int len, u8 *out)
113 return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len, out);
116 static int crc32_digest_arch(struct shash_desc *desc, const u8 *data,
117 unsigned int len, u8 *out)
119 return __crc32_finup_arch(crypto_shash_ctx(desc->tfm), data, len, out);
122 static struct shash_alg algs[] = {{
123 .setkey = crc32_setkey,
124 .init = crc32_init,
125 .update = crc32_update,
126 .final = crc32_final,
127 .finup = crc32_finup,
128 .digest = crc32_digest,
129 .descsize = sizeof(u32),
130 .digestsize = CHKSUM_DIGEST_SIZE,
132 .base.cra_name = "crc32",
133 .base.cra_driver_name = "crc32-generic",
134 .base.cra_priority = 100,
135 .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
136 .base.cra_blocksize = CHKSUM_BLOCK_SIZE,
137 .base.cra_ctxsize = sizeof(u32),
138 .base.cra_module = THIS_MODULE,
139 .base.cra_init = crc32_cra_init,
140 }, {
141 .setkey = crc32_setkey,
142 .init = crc32_init,
143 .update = crc32_update_arch,
144 .final = crc32_final,
145 .finup = crc32_finup_arch,
146 .digest = crc32_digest_arch,
147 .descsize = sizeof(u32),
148 .digestsize = CHKSUM_DIGEST_SIZE,
150 .base.cra_name = "crc32",
151 .base.cra_driver_name = "crc32-" __stringify(ARCH),
152 .base.cra_priority = 150,
153 .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
154 .base.cra_blocksize = CHKSUM_BLOCK_SIZE,
155 .base.cra_ctxsize = sizeof(u32),
156 .base.cra_module = THIS_MODULE,
157 .base.cra_init = crc32_cra_init,
160 static int __init crc32_mod_init(void)
162 /* register the arch flavor only if it differs from the generic one */
163 return crypto_register_shashes(algs, 1 + (&crc32_le != &crc32_le_base));
166 static void __exit crc32_mod_fini(void)
168 crypto_unregister_shashes(algs, 1 + (&crc32_le != &crc32_le_base));
171 subsys_initcall(crc32_mod_init);
172 module_exit(crc32_mod_fini);
174 MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
175 MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
176 MODULE_LICENSE("GPL");
177 MODULE_ALIAS_CRYPTO("crc32");
178 MODULE_ALIAS_CRYPTO("crc32-generic");