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 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | */ |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/crypto.h> |
| 18 | #include <linux/crc32c.h> |
Herbert Xu | 25cdbcd | 2006-08-06 23:03:08 +1000 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #define CHKSUM_BLOCK_SIZE 32 |
| 22 | #define CHKSUM_DIGEST_SIZE 4 |
| 23 | |
| 24 | struct chksum_ctx { |
| 25 | u32 crc; |
Herbert Xu | 25cdbcd | 2006-08-06 23:03:08 +1000 | [diff] [blame] | 26 | u32 key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | /* |
| 30 | * Steps through buffer one byte at at time, calculates reflected |
| 31 | * crc using table. |
| 32 | */ |
| 33 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 34 | static void chksum_init(struct crypto_tfm *tfm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 36 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Herbert Xu | 25cdbcd | 2006-08-06 23:03:08 +1000 | [diff] [blame] | 38 | mctx->crc = mctx->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /* |
| 42 | * Setting the seed allows arbitrary accumulators and flexible XOR policy |
| 43 | * If your algorithm starts with ~0, then XOR with ~0 before you set |
| 44 | * the seed. |
| 45 | */ |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 46 | static int chksum_setkey(struct crypto_tfm *tfm, const u8 *key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 47 | unsigned int keylen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 49 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | if (keylen != sizeof(mctx->crc)) { |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 52 | tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | return -EINVAL; |
| 54 | } |
Herbert Xu | 25cdbcd | 2006-08-06 23:03:08 +1000 | [diff] [blame] | 55 | mctx->key = le32_to_cpu(*(__le32 *)key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 59 | static void chksum_update(struct crypto_tfm *tfm, const u8 *data, |
| 60 | unsigned int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 62 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Herbert Xu | 25cdbcd | 2006-08-06 23:03:08 +1000 | [diff] [blame] | 64 | mctx->crc = crc32c(mctx->crc, data, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 67 | static void chksum_final(struct crypto_tfm *tfm, u8 *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 69 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Herbert Xu | 25cdbcd | 2006-08-06 23:03:08 +1000 | [diff] [blame] | 71 | *(__le32 *)out = ~cpu_to_le32(mctx->crc); |
| 72 | } |
| 73 | |
| 74 | static int crc32c_cra_init(struct crypto_tfm *tfm) |
| 75 | { |
| 76 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); |
| 77 | |
| 78 | mctx->key = ~0; |
| 79 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static struct crypto_alg alg = { |
| 83 | .cra_name = "crc32c", |
| 84 | .cra_flags = CRYPTO_ALG_TYPE_DIGEST, |
| 85 | .cra_blocksize = CHKSUM_BLOCK_SIZE, |
| 86 | .cra_ctxsize = sizeof(struct chksum_ctx), |
| 87 | .cra_module = THIS_MODULE, |
| 88 | .cra_list = LIST_HEAD_INIT(alg.cra_list), |
Herbert Xu | 25cdbcd | 2006-08-06 23:03:08 +1000 | [diff] [blame] | 89 | .cra_init = crc32c_cra_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | .cra_u = { |
| 91 | .digest = { |
| 92 | .dia_digestsize= CHKSUM_DIGEST_SIZE, |
| 93 | .dia_setkey = chksum_setkey, |
| 94 | .dia_init = chksum_init, |
| 95 | .dia_update = chksum_update, |
| 96 | .dia_final = chksum_final |
| 97 | } |
| 98 | } |
| 99 | }; |
| 100 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame^] | 101 | static int __init crc32c_mod_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
| 103 | return crypto_register_alg(&alg); |
| 104 | } |
| 105 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame^] | 106 | static void __exit crc32c_mod_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | crypto_unregister_alg(&alg); |
| 109 | } |
| 110 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame^] | 111 | module_init(crc32c_mod_init); |
| 112 | module_exit(crc32c_mod_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>"); |
| 115 | MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c"); |
| 116 | MODULE_LICENSE("GPL"); |