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> !! 38 #include <asm/unaligned.h> 34 #include <crypto/internal/hash.h> 39 #include <crypto/internal/hash.h> 35 #include <linux/init.h> 40 #include <linux/init.h> 36 #include <linux/module.h> 41 #include <linux/module.h> 37 #include <linux/string.h> 42 #include <linux/string.h> 38 #include <linux/kernel.h> 43 #include <linux/kernel.h> 39 #include <linux/crc32.h> 44 #include <linux/crc32.h> 40 45 41 #define CHKSUM_BLOCK_SIZE 1 46 #define CHKSUM_BLOCK_SIZE 1 42 #define CHKSUM_DIGEST_SIZE 4 47 #define CHKSUM_DIGEST_SIZE 4 43 48 44 struct chksum_ctx { 49 struct chksum_ctx { 45 u32 key; 50 u32 key; 46 }; 51 }; 47 52 48 struct chksum_desc_ctx { 53 struct chksum_desc_ctx { 49 u32 crc; 54 u32 crc; 50 }; 55 }; 51 56 52 /* 57 /* 53 * Steps through buffer one byte at a time, ca !! 58 * Steps through buffer one byte at at time, calculates reflected 54 * crc using table. 59 * crc using table. 55 */ 60 */ 56 61 57 static int chksum_init(struct shash_desc *desc 62 static int chksum_init(struct shash_desc *desc) 58 { 63 { 59 struct chksum_ctx *mctx = crypto_shash 64 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm); 60 struct chksum_desc_ctx *ctx = shash_de 65 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); 61 66 62 ctx->crc = mctx->key; 67 ctx->crc = mctx->key; 63 68 64 return 0; 69 return 0; 65 } 70 } 66 71 67 /* 72 /* 68 * Setting the seed allows arbitrary accumulat 73 * Setting the seed allows arbitrary accumulators and flexible XOR policy 69 * If your algorithm starts with ~0, then XOR 74 * If your algorithm starts with ~0, then XOR with ~0 before you set 70 * the seed. 75 * the seed. 71 */ 76 */ 72 static int chksum_setkey(struct crypto_shash * 77 static int chksum_setkey(struct crypto_shash *tfm, const u8 *key, 73 unsigned int keylen) 78 unsigned int keylen) 74 { 79 { 75 struct chksum_ctx *mctx = crypto_shash 80 struct chksum_ctx *mctx = crypto_shash_ctx(tfm); 76 81 77 if (keylen != sizeof(mctx->key)) !! 82 if (keylen != sizeof(mctx->key)) { >> 83 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); 78 return -EINVAL; 84 return -EINVAL; >> 85 } 79 mctx->key = get_unaligned_le32(key); 86 mctx->key = get_unaligned_le32(key); 80 return 0; 87 return 0; 81 } 88 } 82 89 83 static int chksum_update(struct shash_desc *de 90 static int chksum_update(struct shash_desc *desc, const u8 *data, 84 unsigned int length) 91 unsigned int length) 85 { 92 { 86 struct chksum_desc_ctx *ctx = shash_de 93 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); 87 94 88 ctx->crc = __crc32c_le(ctx->crc, data, 95 ctx->crc = __crc32c_le(ctx->crc, data, length); 89 return 0; 96 return 0; 90 } 97 } 91 98 92 static int chksum_final(struct shash_desc *des 99 static int chksum_final(struct shash_desc *desc, u8 *out) 93 { 100 { 94 struct chksum_desc_ctx *ctx = shash_de 101 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); 95 102 96 put_unaligned_le32(~ctx->crc, out); 103 put_unaligned_le32(~ctx->crc, out); 97 return 0; 104 return 0; 98 } 105 } 99 106 100 static int __chksum_finup(u32 *crcp, const u8 107 static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out) 101 { 108 { 102 put_unaligned_le32(~__crc32c_le(*crcp, 109 put_unaligned_le32(~__crc32c_le(*crcp, data, len), out); 103 return 0; 110 return 0; 104 } 111 } 105 112 106 static int chksum_finup(struct shash_desc *des 113 static int chksum_finup(struct shash_desc *desc, const u8 *data, 107 unsigned int len, u8 * 114 unsigned int len, u8 *out) 108 { 115 { 109 struct chksum_desc_ctx *ctx = shash_de 116 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); 110 117 111 return __chksum_finup(&ctx->crc, data, 118 return __chksum_finup(&ctx->crc, data, len, out); 112 } 119 } 113 120 114 static int chksum_digest(struct shash_desc *de 121 static int chksum_digest(struct shash_desc *desc, const u8 *data, 115 unsigned int length, 122 unsigned int length, u8 *out) 116 { 123 { 117 struct chksum_ctx *mctx = crypto_shash 124 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm); 118 125 119 return __chksum_finup(&mctx->key, data 126 return __chksum_finup(&mctx->key, data, length, out); 120 } 127 } 121 128 122 static int crc32c_cra_init(struct crypto_tfm * 129 static int crc32c_cra_init(struct crypto_tfm *tfm) 123 { 130 { 124 struct chksum_ctx *mctx = crypto_tfm_c 131 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); 125 132 126 mctx->key = ~0; 133 mctx->key = ~0; 127 return 0; 134 return 0; 128 } 135 } 129 136 130 static struct shash_alg alg = { 137 static struct shash_alg alg = { 131 .digestsize = CHKSUM 138 .digestsize = CHKSUM_DIGEST_SIZE, 132 .setkey = chksum 139 .setkey = chksum_setkey, 133 .init = chksum_init, 140 .init = chksum_init, 134 .update = chksum_update, 141 .update = chksum_update, 135 .final = chksum_final, 142 .final = chksum_final, 136 .finup = chksum_finup, 143 .finup = chksum_finup, 137 .digest = chksum_digest, 144 .digest = chksum_digest, 138 .descsize = sizeof 145 .descsize = sizeof(struct chksum_desc_ctx), 139 .base = { 146 .base = { 140 .cra_name = 147 .cra_name = "crc32c", 141 .cra_driver_name = 148 .cra_driver_name = "crc32c-generic", 142 .cra_priority = 149 .cra_priority = 100, 143 .cra_flags = 150 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY, 144 .cra_blocksize = 151 .cra_blocksize = CHKSUM_BLOCK_SIZE, 145 .cra_ctxsize = 152 .cra_ctxsize = sizeof(struct chksum_ctx), 146 .cra_module = 153 .cra_module = THIS_MODULE, 147 .cra_init = 154 .cra_init = crc32c_cra_init, 148 } 155 } 149 }; 156 }; 150 157 151 static int __init crc32c_mod_init(void) 158 static int __init crc32c_mod_init(void) 152 { 159 { 153 return crypto_register_shash(&alg); 160 return crypto_register_shash(&alg); 154 } 161 } 155 162 156 static void __exit crc32c_mod_fini(void) 163 static void __exit crc32c_mod_fini(void) 157 { 164 { 158 crypto_unregister_shash(&alg); 165 crypto_unregister_shash(&alg); 159 } 166 } 160 167 161 subsys_initcall(crc32c_mod_init); !! 168 module_init(crc32c_mod_init); 162 module_exit(crc32c_mod_fini); 169 module_exit(crc32c_mod_fini); 163 170 164 MODULE_AUTHOR("Clay Haapala <chaapala@cisco.co 171 MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>"); 165 MODULE_DESCRIPTION("CRC32c (Castagnoli) calcul 172 MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c"); 166 MODULE_LICENSE("GPL"); 173 MODULE_LICENSE("GPL"); 167 MODULE_ALIAS_CRYPTO("crc32c"); 174 MODULE_ALIAS_CRYPTO("crc32c"); 168 MODULE_ALIAS_CRYPTO("crc32c-generic"); 175 MODULE_ALIAS_CRYPTO("crc32c-generic"); 169 176
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.