18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Cryptographic API. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * CRC32C chksum 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci *@Article{castagnoli-crc, 88c2ecf20Sopenharmony_ci * author = { Guy Castagnoli and Stefan Braeuer and Martin Herrman}, 98c2ecf20Sopenharmony_ci * title = {{Optimization of Cyclic Redundancy-Check Codes with 24 108c2ecf20Sopenharmony_ci * and 32 Parity Bits}}, 118c2ecf20Sopenharmony_ci * journal = IEEE Transactions on Communication, 128c2ecf20Sopenharmony_ci * year = {1993}, 138c2ecf20Sopenharmony_ci * volume = {41}, 148c2ecf20Sopenharmony_ci * number = {6}, 158c2ecf20Sopenharmony_ci * pages = {}, 168c2ecf20Sopenharmony_ci * month = {June}, 178c2ecf20Sopenharmony_ci *} 188c2ecf20Sopenharmony_ci * Used by the iSCSI driver, possibly others, and derived from 198c2ecf20Sopenharmony_ci * the iscsi-crc.c module of the linux-iscsi driver at 208c2ecf20Sopenharmony_ci * http://linux-iscsi.sourceforge.net. 218c2ecf20Sopenharmony_ci * 228c2ecf20Sopenharmony_ci * Following the example of lib/crc32, this function is intended to be 238c2ecf20Sopenharmony_ci * flexible and useful for all users. Modules that currently have their 248c2ecf20Sopenharmony_ci * own crc32c, but hopefully may be able to use this one are: 258c2ecf20Sopenharmony_ci * net/sctp (please add all your doco to here if you change to 268c2ecf20Sopenharmony_ci * use this one!) 278c2ecf20Sopenharmony_ci * <endoflist> 288c2ecf20Sopenharmony_ci * 298c2ecf20Sopenharmony_ci * Copyright (c) 2004 Cisco Systems, Inc. 308c2ecf20Sopenharmony_ci * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au> 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 348c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h> 358c2ecf20Sopenharmony_ci#include <linux/init.h> 368c2ecf20Sopenharmony_ci#include <linux/module.h> 378c2ecf20Sopenharmony_ci#include <linux/string.h> 388c2ecf20Sopenharmony_ci#include <linux/kernel.h> 398c2ecf20Sopenharmony_ci#include <linux/crc32.h> 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#define CHKSUM_BLOCK_SIZE 1 428c2ecf20Sopenharmony_ci#define CHKSUM_DIGEST_SIZE 4 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistruct chksum_ctx { 458c2ecf20Sopenharmony_ci u32 key; 468c2ecf20Sopenharmony_ci}; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistruct chksum_desc_ctx { 498c2ecf20Sopenharmony_ci u32 crc; 508c2ecf20Sopenharmony_ci}; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci/* 538c2ecf20Sopenharmony_ci * Steps through buffer one byte at a time, calculates reflected 548c2ecf20Sopenharmony_ci * crc using table. 558c2ecf20Sopenharmony_ci */ 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistatic int chksum_init(struct shash_desc *desc) 588c2ecf20Sopenharmony_ci{ 598c2ecf20Sopenharmony_ci struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm); 608c2ecf20Sopenharmony_ci struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci ctx->crc = mctx->key; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci return 0; 658c2ecf20Sopenharmony_ci} 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci/* 688c2ecf20Sopenharmony_ci * Setting the seed allows arbitrary accumulators and flexible XOR policy 698c2ecf20Sopenharmony_ci * If your algorithm starts with ~0, then XOR with ~0 before you set 708c2ecf20Sopenharmony_ci * the seed. 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_cistatic int chksum_setkey(struct crypto_shash *tfm, const u8 *key, 738c2ecf20Sopenharmony_ci unsigned int keylen) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci struct chksum_ctx *mctx = crypto_shash_ctx(tfm); 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci if (keylen != sizeof(mctx->key)) 788c2ecf20Sopenharmony_ci return -EINVAL; 798c2ecf20Sopenharmony_ci mctx->key = get_unaligned_le32(key); 808c2ecf20Sopenharmony_ci return 0; 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistatic int chksum_update(struct shash_desc *desc, const u8 *data, 848c2ecf20Sopenharmony_ci unsigned int length) 858c2ecf20Sopenharmony_ci{ 868c2ecf20Sopenharmony_ci struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci ctx->crc = __crc32c_le(ctx->crc, data, length); 898c2ecf20Sopenharmony_ci return 0; 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic int chksum_final(struct shash_desc *desc, u8 *out) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci put_unaligned_le32(~ctx->crc, out); 978c2ecf20Sopenharmony_ci return 0; 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci put_unaligned_le32(~__crc32c_le(*crcp, data, len), out); 1038c2ecf20Sopenharmony_ci return 0; 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistatic int chksum_finup(struct shash_desc *desc, const u8 *data, 1078c2ecf20Sopenharmony_ci unsigned int len, u8 *out) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci return __chksum_finup(&ctx->crc, data, len, out); 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic int chksum_digest(struct shash_desc *desc, const u8 *data, 1158c2ecf20Sopenharmony_ci unsigned int length, u8 *out) 1168c2ecf20Sopenharmony_ci{ 1178c2ecf20Sopenharmony_ci struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci return __chksum_finup(&mctx->key, data, length, out); 1208c2ecf20Sopenharmony_ci} 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cistatic int crc32c_cra_init(struct crypto_tfm *tfm) 1238c2ecf20Sopenharmony_ci{ 1248c2ecf20Sopenharmony_ci struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci mctx->key = ~0; 1278c2ecf20Sopenharmony_ci return 0; 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic struct shash_alg alg = { 1318c2ecf20Sopenharmony_ci .digestsize = CHKSUM_DIGEST_SIZE, 1328c2ecf20Sopenharmony_ci .setkey = chksum_setkey, 1338c2ecf20Sopenharmony_ci .init = chksum_init, 1348c2ecf20Sopenharmony_ci .update = chksum_update, 1358c2ecf20Sopenharmony_ci .final = chksum_final, 1368c2ecf20Sopenharmony_ci .finup = chksum_finup, 1378c2ecf20Sopenharmony_ci .digest = chksum_digest, 1388c2ecf20Sopenharmony_ci .descsize = sizeof(struct chksum_desc_ctx), 1398c2ecf20Sopenharmony_ci .base = { 1408c2ecf20Sopenharmony_ci .cra_name = "crc32c", 1418c2ecf20Sopenharmony_ci .cra_driver_name = "crc32c-generic", 1428c2ecf20Sopenharmony_ci .cra_priority = 100, 1438c2ecf20Sopenharmony_ci .cra_flags = CRYPTO_ALG_OPTIONAL_KEY, 1448c2ecf20Sopenharmony_ci .cra_blocksize = CHKSUM_BLOCK_SIZE, 1458c2ecf20Sopenharmony_ci .cra_ctxsize = sizeof(struct chksum_ctx), 1468c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 1478c2ecf20Sopenharmony_ci .cra_init = crc32c_cra_init, 1488c2ecf20Sopenharmony_ci } 1498c2ecf20Sopenharmony_ci}; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic int __init crc32c_mod_init(void) 1528c2ecf20Sopenharmony_ci{ 1538c2ecf20Sopenharmony_ci return crypto_register_shash(&alg); 1548c2ecf20Sopenharmony_ci} 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cistatic void __exit crc32c_mod_fini(void) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci crypto_unregister_shash(&alg); 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cisubsys_initcall(crc32c_mod_init); 1628c2ecf20Sopenharmony_cimodule_exit(crc32c_mod_fini); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ciMODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>"); 1658c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c"); 1668c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1678c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("crc32c"); 1688c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("crc32c-generic"); 169