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