Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * CRC32C chksum |
| 5 | * |
| 6 | * This module file is a wrapper to invoke the lib/crc32c routines. |
| 7 | * |
Herbert Xu | 5773a3e | 2008-07-08 20:54:28 +0800 | [diff] [blame] | 8 | * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au> |
| 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
| 12 | * Software Foundation; either version 2 of the License, or (at your option) |
| 13 | * any later version. |
| 14 | * |
| 15 | */ |
Herbert Xu | 5773a3e | 2008-07-08 20:54:28 +0800 | [diff] [blame] | 16 | |
| 17 | #include <crypto/internal/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/crc32c.h> |
Herbert Xu | 25cdbcd | 2006-08-06 23:03:08 +1000 | [diff] [blame] | 22 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Herbert Xu | 5773a3e | 2008-07-08 20:54:28 +0800 | [diff] [blame] | 24 | #define CHKSUM_BLOCK_SIZE 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #define CHKSUM_DIGEST_SIZE 4 |
| 26 | |
| 27 | struct chksum_ctx { |
Herbert Xu | 25cdbcd | 2006-08-06 23:03:08 +1000 | [diff] [blame] | 28 | u32 key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 31 | struct chksum_desc_ctx { |
| 32 | u32 crc; |
| 33 | }; |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | /* |
| 36 | * Steps through buffer one byte at at time, calculates reflected |
| 37 | * crc using table. |
| 38 | */ |
| 39 | |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 40 | static int chksum_init(struct shash_desc *desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 42 | struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm); |
| 43 | struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 45 | ctx->crc = mctx->key; |
| 46 | |
| 47 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Setting the seed allows arbitrary accumulators and flexible XOR policy |
| 52 | * If your algorithm starts with ~0, then XOR with ~0 before you set |
| 53 | * the seed. |
| 54 | */ |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 55 | static int chksum_setkey(struct crypto_shash *tfm, const u8 *key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 56 | unsigned int keylen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 58 | struct chksum_ctx *mctx = crypto_shash_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 60 | if (keylen != sizeof(mctx->key)) { |
| 61 | crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | return -EINVAL; |
| 63 | } |
Herbert Xu | 25cdbcd | 2006-08-06 23:03:08 +1000 | [diff] [blame] | 64 | mctx->key = le32_to_cpu(*(__le32 *)key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 68 | static int chksum_update(struct shash_desc *desc, const u8 *data, |
| 69 | unsigned int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 71 | struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 73 | ctx->crc = crc32c(ctx->crc, data, length); |
| 74 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 77 | static int chksum_final(struct shash_desc *desc, u8 *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 79 | struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); |
| 80 | |
| 81 | *(__le32 *)out = ~cpu_to_le32p(&ctx->crc); |
| 82 | return 0; |
Herbert Xu | 25cdbcd | 2006-08-06 23:03:08 +1000 | [diff] [blame] | 83 | } |
| 84 | |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 85 | static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out) |
| 86 | { |
| 87 | *(__le32 *)out = ~cpu_to_le32(crc32c(*crcp, data, len)); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int chksum_finup(struct shash_desc *desc, const u8 *data, |
| 92 | unsigned int len, u8 *out) |
| 93 | { |
| 94 | struct chksum_desc_ctx *ctx = shash_desc_ctx(desc); |
| 95 | |
| 96 | return __chksum_finup(&ctx->crc, data, len, out); |
| 97 | } |
| 98 | |
| 99 | static int chksum_digest(struct shash_desc *desc, const u8 *data, |
| 100 | unsigned int length, u8 *out) |
| 101 | { |
| 102 | struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm); |
| 103 | |
| 104 | return __chksum_finup(&mctx->key, data, length, out); |
| 105 | } |
| 106 | |
| 107 | static int crc32c_cra_init(struct crypto_tfm *tfm) |
Herbert Xu | 25cdbcd | 2006-08-06 23:03:08 +1000 | [diff] [blame] | 108 | { |
| 109 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); |
| 110 | |
| 111 | mctx->key = ~0; |
| 112 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 115 | static struct shash_alg alg = { |
| 116 | .digestsize = CHKSUM_DIGEST_SIZE, |
| 117 | .setkey = chksum_setkey, |
| 118 | .init = chksum_init, |
| 119 | .update = chksum_update, |
| 120 | .final = chksum_final, |
| 121 | .finup = chksum_finup, |
| 122 | .digest = chksum_digest, |
| 123 | .descsize = sizeof(struct chksum_desc_ctx), |
| 124 | .base = { |
| 125 | .cra_name = "crc32c", |
| 126 | .cra_driver_name = "crc32c-generic", |
| 127 | .cra_priority = 100, |
| 128 | .cra_blocksize = CHKSUM_BLOCK_SIZE, |
| 129 | .cra_alignmask = 3, |
| 130 | .cra_ctxsize = sizeof(struct chksum_ctx), |
| 131 | .cra_module = THIS_MODULE, |
| 132 | .cra_init = crc32c_cra_init, |
Herbert Xu | 5773a3e | 2008-07-08 20:54:28 +0800 | [diff] [blame] | 133 | } |
| 134 | }; |
| 135 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 136 | static int __init crc32c_mod_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 138 | return crypto_register_shash(&alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 141 | static void __exit crc32c_mod_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Herbert Xu | faccc4b | 2008-09-09 17:23:07 +1000 | [diff] [blame^] | 143 | crypto_unregister_shash(&alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 146 | module_init(crc32c_mod_init); |
| 147 | module_exit(crc32c_mod_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>"); |
| 150 | MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c"); |
| 151 | MODULE_LICENSE("GPL"); |