blob: 7b25fe82072c054362ef2d4b8aeebe8eaddb42b2 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Richard Hartmanndb83aab2010-02-16 20:31:19 +08002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Cryptographic API.
4 *
5 * CRC32C chksum
6 *
Herbert Xu69c35ef2008-11-07 15:11:47 +08007 *@Article{castagnoli-crc,
8 * author = { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
9 * title = {{Optimization of Cyclic Redundancy-Check Codes with 24
10 * and 32 Parity Bits}},
11 * journal = IEEE Transactions on Communication,
12 * year = {1993},
13 * volume = {41},
14 * number = {6},
15 * pages = {},
16 * month = {June},
17 *}
18 * Used by the iSCSI driver, possibly others, and derived from the
19 * the iscsi-crc.c module of the linux-iscsi driver at
20 * http://linux-iscsi.sourceforge.net.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
Herbert Xu69c35ef2008-11-07 15:11:47 +080022 * Following the example of lib/crc32, this function is intended to be
23 * flexible and useful for all users. Modules that currently have their
24 * own crc32c, but hopefully may be able to use this one are:
25 * net/sctp (please add all your doco to here if you change to
26 * use this one!)
27 * <endoflist>
28 *
29 * Copyright (c) 2004 Cisco Systems, Inc.
Herbert Xu5773a3e2008-07-08 20:54:28 +080030 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
Herbert Xu5773a3e2008-07-08 20:54:28 +080032
Eric Biggers7bcfb132018-05-19 22:07:38 -070033#include <asm/unaligned.h>
Herbert Xu5773a3e2008-07-08 20:54:28 +080034#include <crypto/internal/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/string.h>
Herbert Xu25cdbcd2006-08-06 23:03:08 +100038#include <linux/kernel.h>
Darrick J. Wong6a0962b2012-03-23 15:02:25 -070039#include <linux/crc32.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Herbert Xu5773a3e2008-07-08 20:54:28 +080041#define CHKSUM_BLOCK_SIZE 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define CHKSUM_DIGEST_SIZE 4
43
44struct chksum_ctx {
Herbert Xu25cdbcd2006-08-06 23:03:08 +100045 u32 key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Herbert Xufaccc4b2008-09-09 17:23:07 +100048struct chksum_desc_ctx {
49 u32 crc;
50};
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
Richard Hartmanndb83aab2010-02-16 20:31:19 +080053 * Steps through buffer one byte at at time, calculates reflected
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * crc using table.
55 */
56
Herbert Xufaccc4b2008-09-09 17:23:07 +100057static int chksum_init(struct shash_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Herbert Xufaccc4b2008-09-09 17:23:07 +100059 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
60 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Herbert Xufaccc4b2008-09-09 17:23:07 +100062 ctx->crc = mctx->key;
63
64 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67/*
68 * Setting the seed allows arbitrary accumulators and flexible XOR policy
69 * If your algorithm starts with ~0, then XOR with ~0 before you set
70 * the seed.
71 */
Herbert Xufaccc4b2008-09-09 17:23:07 +100072static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +100073 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Herbert Xufaccc4b2008-09-09 17:23:07 +100075 struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Herbert Xufaccc4b2008-09-09 17:23:07 +100077 if (keylen != sizeof(mctx->key)) {
78 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return -EINVAL;
80 }
Eric Biggers7bcfb132018-05-19 22:07:38 -070081 mctx->key = get_unaligned_le32(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 0;
83}
84
Herbert Xufaccc4b2008-09-09 17:23:07 +100085static int chksum_update(struct shash_desc *desc, const u8 *data,
86 unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Herbert Xufaccc4b2008-09-09 17:23:07 +100088 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Darrick J. Wong6a0962b2012-03-23 15:02:25 -070090 ctx->crc = __crc32c_le(ctx->crc, data, length);
Herbert Xufaccc4b2008-09-09 17:23:07 +100091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Herbert Xufaccc4b2008-09-09 17:23:07 +100094static int chksum_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Herbert Xufaccc4b2008-09-09 17:23:07 +100096 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
97
Eric Biggers7bcfb132018-05-19 22:07:38 -070098 put_unaligned_le32(~ctx->crc, out);
Herbert Xufaccc4b2008-09-09 17:23:07 +100099 return 0;
Herbert Xu25cdbcd2006-08-06 23:03:08 +1000100}
101
Herbert Xufaccc4b2008-09-09 17:23:07 +1000102static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
103{
Eric Biggers7bcfb132018-05-19 22:07:38 -0700104 put_unaligned_le32(~__crc32c_le(*crcp, data, len), out);
Herbert Xufaccc4b2008-09-09 17:23:07 +1000105 return 0;
106}
107
108static int chksum_finup(struct shash_desc *desc, const u8 *data,
109 unsigned int len, u8 *out)
110{
111 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
112
113 return __chksum_finup(&ctx->crc, data, len, out);
114}
115
116static int chksum_digest(struct shash_desc *desc, const u8 *data,
117 unsigned int length, u8 *out)
118{
119 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
120
121 return __chksum_finup(&mctx->key, data, length, out);
122}
123
124static int crc32c_cra_init(struct crypto_tfm *tfm)
Herbert Xu25cdbcd2006-08-06 23:03:08 +1000125{
126 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
127
128 mctx->key = ~0;
129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Herbert Xufaccc4b2008-09-09 17:23:07 +1000132static struct shash_alg alg = {
133 .digestsize = CHKSUM_DIGEST_SIZE,
134 .setkey = chksum_setkey,
Mati Vaitfae36642011-06-08 21:23:40 +0800135 .init = chksum_init,
136 .update = chksum_update,
137 .final = chksum_final,
138 .finup = chksum_finup,
139 .digest = chksum_digest,
Herbert Xufaccc4b2008-09-09 17:23:07 +1000140 .descsize = sizeof(struct chksum_desc_ctx),
141 .base = {
142 .cra_name = "crc32c",
143 .cra_driver_name = "crc32c-generic",
144 .cra_priority = 100,
Eric Biggersa208fa82018-01-03 11:16:26 -0800145 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
Herbert Xufaccc4b2008-09-09 17:23:07 +1000146 .cra_blocksize = CHKSUM_BLOCK_SIZE,
Herbert Xufaccc4b2008-09-09 17:23:07 +1000147 .cra_ctxsize = sizeof(struct chksum_ctx),
148 .cra_module = THIS_MODULE,
149 .cra_init = crc32c_cra_init,
Herbert Xu5773a3e2008-07-08 20:54:28 +0800150 }
151};
152
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800153static int __init crc32c_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Herbert Xufaccc4b2008-09-09 17:23:07 +1000155 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800158static void __exit crc32c_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Herbert Xufaccc4b2008-09-09 17:23:07 +1000160 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Eric Biggersc4741b22019-04-11 21:57:42 -0700163subsys_initcall(crc32c_mod_init);
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800164module_exit(crc32c_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
167MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
168MODULE_LICENSE("GPL");
Kees Cook5d26a102014-11-20 17:05:53 -0800169MODULE_ALIAS_CRYPTO("crc32c");
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100170MODULE_ALIAS_CRYPTO("crc32c-generic");