blob: 7fa9b0788685b5f46610eb9a2387b3ab0b32ebca [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
Eric Biggers674f3682019-12-30 21:19:36 -060077 if (keylen != sizeof(mctx->key))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -EINVAL;
Eric Biggers7bcfb132018-05-19 22:07:38 -070079 mctx->key = get_unaligned_le32(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return 0;
81}
82
Herbert Xufaccc4b2008-09-09 17:23:07 +100083static int chksum_update(struct shash_desc *desc, const u8 *data,
84 unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Herbert Xufaccc4b2008-09-09 17:23:07 +100086 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Darrick J. Wong6a0962b2012-03-23 15:02:25 -070088 ctx->crc = __crc32c_le(ctx->crc, data, length);
Herbert Xufaccc4b2008-09-09 17:23:07 +100089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Herbert Xufaccc4b2008-09-09 17:23:07 +100092static int chksum_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Herbert Xufaccc4b2008-09-09 17:23:07 +100094 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
95
Eric Biggers7bcfb132018-05-19 22:07:38 -070096 put_unaligned_le32(~ctx->crc, out);
Herbert Xufaccc4b2008-09-09 17:23:07 +100097 return 0;
Herbert Xu25cdbcd2006-08-06 23:03:08 +100098}
99
Herbert Xufaccc4b2008-09-09 17:23:07 +1000100static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
101{
Eric Biggers7bcfb132018-05-19 22:07:38 -0700102 put_unaligned_le32(~__crc32c_le(*crcp, data, len), out);
Herbert Xufaccc4b2008-09-09 17:23:07 +1000103 return 0;
104}
105
106static int chksum_finup(struct shash_desc *desc, const u8 *data,
107 unsigned int len, u8 *out)
108{
109 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
110
111 return __chksum_finup(&ctx->crc, data, len, out);
112}
113
114static int chksum_digest(struct shash_desc *desc, const u8 *data,
115 unsigned int length, u8 *out)
116{
117 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
118
119 return __chksum_finup(&mctx->key, data, length, out);
120}
121
122static int crc32c_cra_init(struct crypto_tfm *tfm)
Herbert Xu25cdbcd2006-08-06 23:03:08 +1000123{
124 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
125
126 mctx->key = ~0;
127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Herbert Xufaccc4b2008-09-09 17:23:07 +1000130static struct shash_alg alg = {
131 .digestsize = CHKSUM_DIGEST_SIZE,
132 .setkey = chksum_setkey,
Mati Vaitfae36642011-06-08 21:23:40 +0800133 .init = chksum_init,
134 .update = chksum_update,
135 .final = chksum_final,
136 .finup = chksum_finup,
137 .digest = chksum_digest,
Herbert Xufaccc4b2008-09-09 17:23:07 +1000138 .descsize = sizeof(struct chksum_desc_ctx),
139 .base = {
140 .cra_name = "crc32c",
141 .cra_driver_name = "crc32c-generic",
142 .cra_priority = 100,
Eric Biggersa208fa82018-01-03 11:16:26 -0800143 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
Herbert Xufaccc4b2008-09-09 17:23:07 +1000144 .cra_blocksize = CHKSUM_BLOCK_SIZE,
Herbert Xufaccc4b2008-09-09 17:23:07 +1000145 .cra_ctxsize = sizeof(struct chksum_ctx),
146 .cra_module = THIS_MODULE,
147 .cra_init = crc32c_cra_init,
Herbert Xu5773a3e2008-07-08 20:54:28 +0800148 }
149};
150
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800151static int __init crc32c_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Herbert Xufaccc4b2008-09-09 17:23:07 +1000153 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800156static void __exit crc32c_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Herbert Xufaccc4b2008-09-09 17:23:07 +1000158 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Eric Biggersc4741b22019-04-11 21:57:42 -0700161subsys_initcall(crc32c_mod_init);
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800162module_exit(crc32c_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
165MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
166MODULE_LICENSE("GPL");
Kees Cook5d26a102014-11-20 17:05:53 -0800167MODULE_ALIAS_CRYPTO("crc32c");
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100168MODULE_ALIAS_CRYPTO("crc32c-generic");