projects/linux·blob master · refs

149 lines · 3.5 KiB

1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * crypto_shash support for CRC-32C
4 *
5 *@Article{castagnoli-crc,
6 * author = { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
7 * title = {{Optimization of Cyclic Redundancy-Check Codes with 24
8 * and 32 Parity Bits}},
9 * journal = IEEE Transactions on Communication,
10 * year = {1993},
11 * volume = {41},
12 * number = {6},
13 * pages = {},
14 * month = {June},
15 *}
16 *
17 * Copyright (c) 2004 Cisco Systems, Inc.
18 * Copyright (c) 2008 Herbert Xu <[email protected]>
19 */
20
21#include <linux/unaligned.h>
22#include <crypto/internal/hash.h>
23#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/string.h>
26#include <linux/kernel.h>
27#include <linux/crc32.h>
28
29#define CHKSUM_BLOCK_SIZE 1
30#define CHKSUM_DIGEST_SIZE 4
31
32struct chksum_ctx {
33 u32 key;
34};
35
36struct chksum_desc_ctx {
37 u32 crc;
38};
39
40static int chksum_init(struct shash_desc *desc)
41{
42 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
43 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
44
45 ctx->crc = mctx->key;
46
47 return 0;
48}
49
50/*
51 * Setting the seed allows arbitrary accumulators and flexible XOR policy
52 * If your algorithm starts with ~0, then XOR with ~0 before you set
53 * the seed.
54 */
55static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
56 unsigned int keylen)
57{
58 struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
59
60 if (keylen != sizeof(mctx->key))
61 return -EINVAL;
62 mctx->key = get_unaligned_le32(key);
63 return 0;
64}
65
66static int chksum_update(struct shash_desc *desc, const u8 *data,
67 unsigned int length)
68{
69 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
70
71 ctx->crc = crc32c(ctx->crc, data, length);
72 return 0;
73}
74
75static int chksum_final(struct shash_desc *desc, u8 *out)
76{
77 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
78
79 put_unaligned_le32(~ctx->crc, out);
80 return 0;
81}
82
83static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
84{
85 put_unaligned_le32(~crc32c(*crcp, data, len), out);
86 return 0;
87}
88
89static int chksum_finup(struct shash_desc *desc, const u8 *data,
90 unsigned int len, u8 *out)
91{
92 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
93
94 return __chksum_finup(&ctx->crc, data, len, out);
95}
96
97static int chksum_digest(struct shash_desc *desc, const u8 *data,
98 unsigned int length, u8 *out)
99{
100 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
101
102 return __chksum_finup(&mctx->key, data, length, out);
103}
104
105static int crc32c_cra_init(struct crypto_tfm *tfm)
106{
107 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
108
109 mctx->key = ~0;
110 return 0;
111}
112
113static struct shash_alg alg = {
114 .digestsize = CHKSUM_DIGEST_SIZE,
115 .setkey = chksum_setkey,
116 .init = chksum_init,
117 .update = chksum_update,
118 .final = chksum_final,
119 .finup = chksum_finup,
120 .digest = chksum_digest,
121 .descsize = sizeof(struct chksum_desc_ctx),
122
123 .base.cra_name = "crc32c",
124 .base.cra_driver_name = "crc32c-lib",
125 .base.cra_priority = 100,
126 .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
127 .base.cra_blocksize = CHKSUM_BLOCK_SIZE,
128 .base.cra_ctxsize = sizeof(struct chksum_ctx),
129 .base.cra_module = THIS_MODULE,
130 .base.cra_init = crc32c_cra_init,
131};
132
133static int __init crc32c_mod_init(void)
134{
135 return crypto_register_shash(&alg);
136}
137
138static void __exit crc32c_mod_fini(void)
139{
140 crypto_unregister_shash(&alg);
141}
142
143module_init(crc32c_mod_init);
144module_exit(crc32c_mod_fini);
145
146MODULE_AUTHOR("Clay Haapala <[email protected]>");
147MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
148MODULE_LICENSE("GPL");
149MODULE_ALIAS_CRYPTO("crc32c");