blob: 7283066ecc982c193a1ac658b5f28ddc62473e13 [file] [log] [blame]
Richard Hartmanndb83aab2010-02-16 20:31:19 +08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Cryptographic API.
3 *
4 * CRC32C chksum
5 *
Herbert Xu69c35ef2008-11-07 15:11:47 +08006 *@Article{castagnoli-crc,
7 * author = { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
8 * title = {{Optimization of Cyclic Redundancy-Check Codes with 24
9 * and 32 Parity Bits}},
10 * journal = IEEE Transactions on Communication,
11 * year = {1993},
12 * volume = {41},
13 * number = {6},
14 * pages = {},
15 * month = {June},
16 *}
17 * Used by the iSCSI driver, possibly others, and derived from the
18 * the iscsi-crc.c module of the linux-iscsi driver at
19 * http://linux-iscsi.sourceforge.net.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 *
Herbert Xu69c35ef2008-11-07 15:11:47 +080021 * Following the example of lib/crc32, this function is intended to be
22 * flexible and useful for all users. Modules that currently have their
23 * own crc32c, but hopefully may be able to use this one are:
24 * net/sctp (please add all your doco to here if you change to
25 * use this one!)
26 * <endoflist>
27 *
28 * Copyright (c) 2004 Cisco Systems, Inc.
Herbert Xu5773a3e2008-07-08 20:54:28 +080029 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
30 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * This program is free software; you can redistribute it and/or modify it
32 * under the terms of the GNU General Public License as published by the Free
Richard Hartmanndb83aab2010-02-16 20:31:19 +080033 * Software Foundation; either version 2 of the License, or (at your option)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * any later version.
35 *
36 */
Herbert Xu5773a3e2008-07-08 20:54:28 +080037
Eric Biggers7bcfb132018-05-19 22:07:38 -070038#include <asm/unaligned.h>
Herbert Xu5773a3e2008-07-08 20:54:28 +080039#include <crypto/internal/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/init.h>
41#include <linux/module.h>
42#include <linux/string.h>
Herbert Xu25cdbcd2006-08-06 23:03:08 +100043#include <linux/kernel.h>
Darrick J. Wong6a0962b2012-03-23 15:02:25 -070044#include <linux/crc32.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Herbert Xu5773a3e2008-07-08 20:54:28 +080046#define CHKSUM_BLOCK_SIZE 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define CHKSUM_DIGEST_SIZE 4
48
49struct chksum_ctx {
Herbert Xu25cdbcd2006-08-06 23:03:08 +100050 u32 key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
Herbert Xufaccc4b2008-09-09 17:23:07 +100053struct chksum_desc_ctx {
54 u32 crc;
55};
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
Richard Hartmanndb83aab2010-02-16 20:31:19 +080058 * Steps through buffer one byte at at time, calculates reflected
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * crc using table.
60 */
61
Herbert Xufaccc4b2008-09-09 17:23:07 +100062static int chksum_init(struct shash_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Herbert Xufaccc4b2008-09-09 17:23:07 +100064 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
65 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Herbert Xufaccc4b2008-09-09 17:23:07 +100067 ctx->crc = mctx->key;
68
69 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72/*
73 * Setting the seed allows arbitrary accumulators and flexible XOR policy
74 * If your algorithm starts with ~0, then XOR with ~0 before you set
75 * the seed.
76 */
Herbert Xufaccc4b2008-09-09 17:23:07 +100077static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +100078 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Herbert Xufaccc4b2008-09-09 17:23:07 +100080 struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Herbert Xufaccc4b2008-09-09 17:23:07 +100082 if (keylen != sizeof(mctx->key)) {
83 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return -EINVAL;
85 }
Eric Biggers7bcfb132018-05-19 22:07:38 -070086 mctx->key = get_unaligned_le32(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
88}
89
Herbert Xufaccc4b2008-09-09 17:23:07 +100090static int chksum_update(struct shash_desc *desc, const u8 *data,
91 unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Herbert Xufaccc4b2008-09-09 17:23:07 +100093 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Darrick J. Wong6a0962b2012-03-23 15:02:25 -070095 ctx->crc = __crc32c_le(ctx->crc, data, length);
Herbert Xufaccc4b2008-09-09 17:23:07 +100096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Herbert Xufaccc4b2008-09-09 17:23:07 +100099static int chksum_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Herbert Xufaccc4b2008-09-09 17:23:07 +1000101 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
102
Eric Biggers7bcfb132018-05-19 22:07:38 -0700103 put_unaligned_le32(~ctx->crc, out);
Herbert Xufaccc4b2008-09-09 17:23:07 +1000104 return 0;
Herbert Xu25cdbcd2006-08-06 23:03:08 +1000105}
106
Herbert Xufaccc4b2008-09-09 17:23:07 +1000107static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
108{
Eric Biggers7bcfb132018-05-19 22:07:38 -0700109 put_unaligned_le32(~__crc32c_le(*crcp, data, len), out);
Herbert Xufaccc4b2008-09-09 17:23:07 +1000110 return 0;
111}
112
113static int chksum_finup(struct shash_desc *desc, const u8 *data,
114 unsigned int len, u8 *out)
115{
116 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
117
118 return __chksum_finup(&ctx->crc, data, len, out);
119}
120
121static int chksum_digest(struct shash_desc *desc, const u8 *data,
122 unsigned int length, u8 *out)
123{
124 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
125
126 return __chksum_finup(&mctx->key, data, length, out);
127}
128
129static int crc32c_cra_init(struct crypto_tfm *tfm)
Herbert Xu25cdbcd2006-08-06 23:03:08 +1000130{
131 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
132
133 mctx->key = ~0;
134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Herbert Xufaccc4b2008-09-09 17:23:07 +1000137static struct shash_alg alg = {
138 .digestsize = CHKSUM_DIGEST_SIZE,
139 .setkey = chksum_setkey,
Mati Vaitfae36642011-06-08 21:23:40 +0800140 .init = chksum_init,
141 .update = chksum_update,
142 .final = chksum_final,
143 .finup = chksum_finup,
144 .digest = chksum_digest,
Herbert Xufaccc4b2008-09-09 17:23:07 +1000145 .descsize = sizeof(struct chksum_desc_ctx),
146 .base = {
147 .cra_name = "crc32c",
148 .cra_driver_name = "crc32c-generic",
149 .cra_priority = 100,
Eric Biggersa208fa82018-01-03 11:16:26 -0800150 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
Herbert Xufaccc4b2008-09-09 17:23:07 +1000151 .cra_blocksize = CHKSUM_BLOCK_SIZE,
Herbert Xufaccc4b2008-09-09 17:23:07 +1000152 .cra_ctxsize = sizeof(struct chksum_ctx),
153 .cra_module = THIS_MODULE,
154 .cra_init = crc32c_cra_init,
Herbert Xu5773a3e2008-07-08 20:54:28 +0800155 }
156};
157
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800158static int __init crc32c_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Herbert Xufaccc4b2008-09-09 17:23:07 +1000160 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800163static void __exit crc32c_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Herbert Xufaccc4b2008-09-09 17:23:07 +1000165 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800168module_init(crc32c_mod_init);
169module_exit(crc32c_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
172MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
173MODULE_LICENSE("GPL");
Kees Cook5d26a102014-11-20 17:05:53 -0800174MODULE_ALIAS_CRYPTO("crc32c");
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100175MODULE_ALIAS_CRYPTO("crc32c-generic");