blob: 06f7018c9d952f632a3541ea236ee0721b8ebdb1 [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
38#include <crypto/internal/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
40#include <linux/module.h>
41#include <linux/string.h>
Herbert Xu25cdbcd2006-08-06 23:03:08 +100042#include <linux/kernel.h>
Darrick J. Wong6a0962b2012-03-23 15:02:25 -070043#include <linux/crc32.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Herbert Xu5773a3e2008-07-08 20:54:28 +080045#define CHKSUM_BLOCK_SIZE 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define CHKSUM_DIGEST_SIZE 4
47
48struct chksum_ctx {
Herbert Xu25cdbcd2006-08-06 23:03:08 +100049 u32 key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Herbert Xufaccc4b2008-09-09 17:23:07 +100052struct chksum_desc_ctx {
53 u32 crc;
54};
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
Richard Hartmanndb83aab2010-02-16 20:31:19 +080057 * Steps through buffer one byte at at time, calculates reflected
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * crc using table.
59 */
60
Herbert Xufaccc4b2008-09-09 17:23:07 +100061static int chksum_init(struct shash_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Herbert Xufaccc4b2008-09-09 17:23:07 +100063 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
64 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Herbert Xufaccc4b2008-09-09 17:23:07 +100066 ctx->crc = mctx->key;
67
68 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71/*
72 * Setting the seed allows arbitrary accumulators and flexible XOR policy
73 * If your algorithm starts with ~0, then XOR with ~0 before you set
74 * the seed.
75 */
Herbert Xufaccc4b2008-09-09 17:23:07 +100076static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +100077 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Herbert Xufaccc4b2008-09-09 17:23:07 +100079 struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Herbert Xufaccc4b2008-09-09 17:23:07 +100081 if (keylen != sizeof(mctx->key)) {
82 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return -EINVAL;
84 }
Herbert Xu25cdbcd2006-08-06 23:03:08 +100085 mctx->key = le32_to_cpu(*(__le32 *)key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 0;
87}
88
Herbert Xufaccc4b2008-09-09 17:23:07 +100089static int chksum_update(struct shash_desc *desc, const u8 *data,
90 unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Herbert Xufaccc4b2008-09-09 17:23:07 +100092 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Darrick J. Wong6a0962b2012-03-23 15:02:25 -070094 ctx->crc = __crc32c_le(ctx->crc, data, length);
Herbert Xufaccc4b2008-09-09 17:23:07 +100095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Herbert Xufaccc4b2008-09-09 17:23:07 +100098static int chksum_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Herbert Xufaccc4b2008-09-09 17:23:07 +1000100 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
101
102 *(__le32 *)out = ~cpu_to_le32p(&ctx->crc);
103 return 0;
Herbert Xu25cdbcd2006-08-06 23:03:08 +1000104}
105
Herbert Xufaccc4b2008-09-09 17:23:07 +1000106static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
107{
Darrick J. Wong6a0962b2012-03-23 15:02:25 -0700108 *(__le32 *)out = ~cpu_to_le32(__crc32c_le(*crcp, data, len));
Herbert Xufaccc4b2008-09-09 17:23:07 +1000109 return 0;
110}
111
112static int chksum_finup(struct shash_desc *desc, const u8 *data,
113 unsigned int len, u8 *out)
114{
115 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
116
117 return __chksum_finup(&ctx->crc, data, len, out);
118}
119
120static int chksum_digest(struct shash_desc *desc, const u8 *data,
121 unsigned int length, u8 *out)
122{
123 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
124
125 return __chksum_finup(&mctx->key, data, length, out);
126}
127
128static int crc32c_cra_init(struct crypto_tfm *tfm)
Herbert Xu25cdbcd2006-08-06 23:03:08 +1000129{
130 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
131
132 mctx->key = ~0;
133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Herbert Xufaccc4b2008-09-09 17:23:07 +1000136static struct shash_alg alg = {
137 .digestsize = CHKSUM_DIGEST_SIZE,
138 .setkey = chksum_setkey,
Mati Vaitfae36642011-06-08 21:23:40 +0800139 .init = chksum_init,
140 .update = chksum_update,
141 .final = chksum_final,
142 .finup = chksum_finup,
143 .digest = chksum_digest,
Herbert Xufaccc4b2008-09-09 17:23:07 +1000144 .descsize = sizeof(struct chksum_desc_ctx),
145 .base = {
146 .cra_name = "crc32c",
147 .cra_driver_name = "crc32c-generic",
148 .cra_priority = 100,
149 .cra_blocksize = CHKSUM_BLOCK_SIZE,
150 .cra_alignmask = 3,
151 .cra_ctxsize = sizeof(struct chksum_ctx),
152 .cra_module = THIS_MODULE,
153 .cra_init = crc32c_cra_init,
Herbert Xu5773a3e2008-07-08 20:54:28 +0800154 }
155};
156
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800157static int __init crc32c_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Herbert Xufaccc4b2008-09-09 17:23:07 +1000159 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800162static void __exit crc32c_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Herbert Xufaccc4b2008-09-09 17:23:07 +1000164 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800167module_init(crc32c_mod_init);
168module_exit(crc32c_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
171MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
172MODULE_LICENSE("GPL");