^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Cryptographic API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * CRC32C chksum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *@Article{castagnoli-crc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * author = { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * title = {{Optimization of Cyclic Redundancy-Check Codes with 24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * and 32 Parity Bits}},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * journal = IEEE Transactions on Communication,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * year = {1993},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * volume = {41},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * number = {6},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * pages = {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * month = {June},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Used by the iSCSI driver, possibly others, and derived from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * the iscsi-crc.c module of the linux-iscsi driver at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * http://linux-iscsi.sourceforge.net.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Following the example of lib/crc32, this function is intended to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * flexible and useful for all users. Modules that currently have their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * own crc32c, but hopefully may be able to use this one are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * net/sctp (please add all your doco to here if you change to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * use this one!)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * <endoflist>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * Copyright (c) 2004 Cisco Systems, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define CHKSUM_BLOCK_SIZE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define CHKSUM_DIGEST_SIZE 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct chksum_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) u32 key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct chksum_desc_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u32 crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Steps through buffer one byte at a time, calculates reflected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * crc using table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static int chksum_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ctx->crc = mctx->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * Setting the seed allows arbitrary accumulators and flexible XOR policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * If your algorithm starts with ~0, then XOR with ~0 before you set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * the seed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (keylen != sizeof(mctx->key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) mctx->key = get_unaligned_le32(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static int chksum_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) ctx->crc = __crc32c_le(ctx->crc, data, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static int chksum_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) put_unaligned_le32(~ctx->crc, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) put_unaligned_le32(~__crc32c_le(*crcp, data, len), out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int chksum_finup(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return __chksum_finup(&ctx->crc, data, len, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int chksum_digest(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned int length, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return __chksum_finup(&mctx->key, data, length, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int crc32c_cra_init(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) mctx->key = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static struct shash_alg alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .digestsize = CHKSUM_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .setkey = chksum_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .init = chksum_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .update = chksum_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .final = chksum_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .finup = chksum_finup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) .digest = chksum_digest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .descsize = sizeof(struct chksum_desc_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) .cra_name = "crc32c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) .cra_driver_name = "crc32c-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .cra_priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .cra_blocksize = CHKSUM_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .cra_ctxsize = sizeof(struct chksum_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .cra_init = crc32c_cra_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int __init crc32c_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return crypto_register_shash(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static void __exit crc32c_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) crypto_unregister_shash(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) subsys_initcall(crc32c_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) module_exit(crc32c_mod_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) MODULE_ALIAS_CRYPTO("crc32c");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) MODULE_ALIAS_CRYPTO("crc32c-generic");