blob: a989cb44fd16081c01966071ac8203108f259236 [file] [log] [blame]
Christophe JAILLET0f049f72021-04-10 22:30:16 +02001// SPDX-License-Identifier: GPL-2.0-only
Alexander Boyko78c37d12013-01-10 18:54:59 +04002/*
3 * Copyright 2012 Xyratex Technology Limited
4 */
5
6/*
7 * This is crypto api shash wrappers to crc32_le.
8 */
9
Eric Biggersfffe7d92018-05-19 22:07:37 -070010#include <asm/unaligned.h>
Alexander Boyko78c37d12013-01-10 18:54:59 +040011#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
Alexander Boyko78c37d12013-01-10 18:54:59 +040021/** 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
Alexander Boyko78c37d12013-01-10 18:54:59 +040031/*
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
Eric Biggers674f3682019-12-30 21:19:36 -060041 if (keylen != sizeof(u32))
Alexander Boyko78c37d12013-01-10 18:54:59 +040042 return -EINVAL;
Eric Biggersfffe7d92018-05-19 22:07:37 -070043 *mctx = get_unaligned_le32(key);
Alexander Boyko78c37d12013-01-10 18:54:59 +040044 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
Eric Biggers69435462018-05-19 22:07:39 -070062 *crcp = crc32_le(*crcp, data, len);
Alexander Boyko78c37d12013-01-10 18:54:59 +040063 return 0;
64}
65
66/* No final XOR 0xFFFFFFFF, like crc32_le */
67static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
68 u8 *out)
69{
Eric Biggers69435462018-05-19 22:07:39 -070070 put_unaligned_le32(crc32_le(*crcp, data, len), out);
Alexander Boyko78c37d12013-01-10 18:54:59 +040071 return 0;
72}
73
74static 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
80static int crc32_final(struct shash_desc *desc, u8 *out)
81{
82 u32 *crcp = shash_desc_ctx(desc);
83
Eric Biggersfffe7d92018-05-19 22:07:37 -070084 put_unaligned_le32(*crcp, out);
Alexander Boyko78c37d12013-01-10 18:54:59 +040085 return 0;
86}
87
88static 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}
94static 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",
Herbert Xua7c58ac2016-01-29 18:20:17 +0800105 .cra_driver_name = "crc32-generic",
Alexander Boyko78c37d12013-01-10 18:54:59 +0400106 .cra_priority = 100,
Eric Biggersa208fa82018-01-03 11:16:26 -0800107 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
Alexander Boyko78c37d12013-01-10 18:54:59 +0400108 .cra_blocksize = CHKSUM_BLOCK_SIZE,
109 .cra_ctxsize = sizeof(u32),
110 .cra_module = THIS_MODULE,
111 .cra_init = crc32_cra_init,
112 }
113};
114
115static int __init crc32_mod_init(void)
116{
117 return crypto_register_shash(&alg);
118}
119
120static void __exit crc32_mod_fini(void)
121{
122 crypto_unregister_shash(&alg);
123}
124
Eric Biggersc4741b22019-04-11 21:57:42 -0700125subsys_initcall(crc32_mod_init);
Alexander Boyko78c37d12013-01-10 18:54:59 +0400126module_exit(crc32_mod_fini);
127
128MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
129MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
130MODULE_LICENSE("GPL");
Kees Cook5d26a102014-11-20 17:05:53 -0800131MODULE_ALIAS_CRYPTO("crc32");
Herbert Xua7c58ac2016-01-29 18:20:17 +0800132MODULE_ALIAS_CRYPTO("crc32-generic");