2010-02-16 19:31:19 +07:00
|
|
|
/*
|
2005-04-17 05:20:36 +07:00
|
|
|
* Cryptographic API.
|
|
|
|
*
|
|
|
|
* CRC32C chksum
|
|
|
|
*
|
2008-11-07 14:11:47 +07:00
|
|
|
*@Article{castagnoli-crc,
|
|
|
|
* author = { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
|
|
|
|
* title = {{Optimization of Cyclic Redundancy-Check Codes with 24
|
|
|
|
* and 32 Parity Bits}},
|
|
|
|
* journal = IEEE Transactions on Communication,
|
|
|
|
* year = {1993},
|
|
|
|
* volume = {41},
|
|
|
|
* number = {6},
|
|
|
|
* pages = {},
|
|
|
|
* month = {June},
|
|
|
|
*}
|
|
|
|
* Used by the iSCSI driver, possibly others, and derived from the
|
|
|
|
* the iscsi-crc.c module of the linux-iscsi driver at
|
|
|
|
* http://linux-iscsi.sourceforge.net.
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2008-11-07 14:11:47 +07:00
|
|
|
* Following the example of lib/crc32, this function is intended to be
|
|
|
|
* flexible and useful for all users. Modules that currently have their
|
|
|
|
* own crc32c, but hopefully may be able to use this one are:
|
|
|
|
* net/sctp (please add all your doco to here if you change to
|
|
|
|
* use this one!)
|
|
|
|
* <endoflist>
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004 Cisco Systems, Inc.
|
2008-07-08 19:54:28 +07:00
|
|
|
* Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
|
|
|
|
*
|
2005-04-17 05:20:36 +07:00
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
2010-02-16 19:31:19 +07:00
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
2005-04-17 05:20:36 +07:00
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
*/
|
2008-07-08 19:54:28 +07:00
|
|
|
|
|
|
|
#include <crypto/internal/hash.h>
|
2005-04-17 05:20:36 +07:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/string.h>
|
2006-08-06 20:03:08 +07:00
|
|
|
#include <linux/kernel.h>
|
2012-03-24 05:02:25 +07:00
|
|
|
#include <linux/crc32.h>
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2008-07-08 19:54:28 +07:00
|
|
|
#define CHKSUM_BLOCK_SIZE 1
|
2005-04-17 05:20:36 +07:00
|
|
|
#define CHKSUM_DIGEST_SIZE 4
|
|
|
|
|
|
|
|
struct chksum_ctx {
|
2006-08-06 20:03:08 +07:00
|
|
|
u32 key;
|
2005-04-17 05:20:36 +07:00
|
|
|
};
|
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
struct chksum_desc_ctx {
|
|
|
|
u32 crc;
|
|
|
|
};
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
/*
|
2010-02-16 19:31:19 +07:00
|
|
|
* Steps through buffer one byte at at time, calculates reflected
|
2005-04-17 05:20:36 +07:00
|
|
|
* crc using table.
|
|
|
|
*/
|
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
static int chksum_init(struct shash_desc *desc)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-09-09 14:23:07 +07:00
|
|
|
struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
|
|
|
|
struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
|
|
|
|
|
|
|
|
ctx->crc = mctx->key;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
return 0;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setting the seed allows arbitrary accumulators and flexible XOR policy
|
|
|
|
* If your algorithm starts with ~0, then XOR with ~0 before you set
|
|
|
|
* the seed.
|
|
|
|
*/
|
2008-09-09 14:23:07 +07:00
|
|
|
static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
|
2006-08-13 11:16:39 +07:00
|
|
|
unsigned int keylen)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-09-09 14:23:07 +07:00
|
|
|
struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
if (keylen != sizeof(mctx->key)) {
|
|
|
|
crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
|
2005-04-17 05:20:36 +07:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2006-08-06 20:03:08 +07:00
|
|
|
mctx->key = le32_to_cpu(*(__le32 *)key);
|
2005-04-17 05:20:36 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
static int chksum_update(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int length)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-09-09 14:23:07 +07:00
|
|
|
struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2012-03-24 05:02:25 +07:00
|
|
|
ctx->crc = __crc32c_le(ctx->crc, data, length);
|
2006-08-06 20:03:08 +07:00
|
|
|
return 0;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
static int chksum_final(struct shash_desc *desc, u8 *out)
|
2008-07-08 19:54:28 +07:00
|
|
|
{
|
2008-09-09 14:23:07 +07:00
|
|
|
struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
|
2008-07-08 19:54:28 +07:00
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
*(__le32 *)out = ~cpu_to_le32p(&ctx->crc);
|
2008-07-08 19:54:28 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
|
2008-07-08 19:54:28 +07:00
|
|
|
{
|
2012-03-24 05:02:25 +07:00
|
|
|
*(__le32 *)out = ~cpu_to_le32(__crc32c_le(*crcp, data, len));
|
2008-07-08 19:54:28 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
static int chksum_finup(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int len, u8 *out)
|
2008-07-08 19:54:28 +07:00
|
|
|
{
|
2008-09-09 14:23:07 +07:00
|
|
|
struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
|
2008-07-08 19:54:28 +07:00
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
return __chksum_finup(&ctx->crc, data, len, out);
|
2008-07-08 19:54:28 +07:00
|
|
|
}
|
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
static int chksum_digest(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int length, u8 *out)
|
2008-07-08 19:54:28 +07:00
|
|
|
{
|
2008-09-09 14:23:07 +07:00
|
|
|
struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
|
2008-07-08 19:54:28 +07:00
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
return __chksum_finup(&mctx->key, data, length, out);
|
2008-07-08 19:54:28 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int crc32c_cra_init(struct crypto_tfm *tfm)
|
|
|
|
{
|
2008-09-09 14:23:07 +07:00
|
|
|
struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
|
2008-07-08 19:54:28 +07:00
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
mctx->key = ~0;
|
2008-07-08 19:54:28 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-09 14:23:07 +07:00
|
|
|
static struct shash_alg alg = {
|
|
|
|
.digestsize = CHKSUM_DIGEST_SIZE,
|
|
|
|
.setkey = chksum_setkey,
|
2011-06-08 20:23:40 +07:00
|
|
|
.init = chksum_init,
|
|
|
|
.update = chksum_update,
|
|
|
|
.final = chksum_final,
|
|
|
|
.finup = chksum_finup,
|
|
|
|
.digest = chksum_digest,
|
2008-09-09 14:23:07 +07:00
|
|
|
.descsize = sizeof(struct chksum_desc_ctx),
|
|
|
|
.base = {
|
|
|
|
.cra_name = "crc32c",
|
|
|
|
.cra_driver_name = "crc32c-generic",
|
|
|
|
.cra_priority = 100,
|
|
|
|
.cra_blocksize = CHKSUM_BLOCK_SIZE,
|
|
|
|
.cra_alignmask = 3,
|
|
|
|
.cra_ctxsize = sizeof(struct chksum_ctx),
|
|
|
|
.cra_module = THIS_MODULE,
|
|
|
|
.cra_init = crc32c_cra_init,
|
2008-07-08 19:54:28 +07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-04-05 20:00:57 +07:00
|
|
|
static int __init crc32c_mod_init(void)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-09-09 14:23:07 +07:00
|
|
|
return crypto_register_shash(&alg);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2008-04-05 20:00:57 +07:00
|
|
|
static void __exit crc32c_mod_fini(void)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-09-09 14:23:07 +07:00
|
|
|
crypto_unregister_shash(&alg);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2008-04-05 20:00:57 +07:00
|
|
|
module_init(crc32c_mod_init);
|
|
|
|
module_exit(crc32c_mod_fini);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
|
|
|
|
MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
|
|
|
|
MODULE_LICENSE("GPL");
|
2014-11-21 08:05:53 +07:00
|
|
|
MODULE_ALIAS_CRYPTO("crc32c");
|
2014-01-23 18:25:47 +07:00
|
|
|
MODULE_SOFTDEP("pre: crc32c");
|