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