6 * This module file is a wrapper to invoke the lib/crc32c routines.
8 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
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)
17 #include <crypto/internal/hash.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/string.h>
21 #include <linux/crc32c.h>
22 #include <linux/kernel.h>
24 #define CHKSUM_BLOCK_SIZE 1
25 #define CHKSUM_DIGEST_SIZE 4
31 struct chksum_desc_ctx {
36 * Steps through buffer one byte at at time, calculates reflected
40 static int chksum_init(struct shash_desc *desc)
42 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
43 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
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
55 static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
58 struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
60 if (keylen != sizeof(mctx->key)) {
61 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
64 mctx->key = le32_to_cpu(*(__le32 *)key);
68 static int chksum_update(struct shash_desc *desc, const u8 *data,
71 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
73 ctx->crc = crc32c(ctx->crc, data, length);
77 static int chksum_final(struct shash_desc *desc, u8 *out)
79 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
81 *(__le32 *)out = ~cpu_to_le32p(&ctx->crc);
85 static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
87 *(__le32 *)out = ~cpu_to_le32(crc32c(*crcp, data, len));
91 static int chksum_finup(struct shash_desc *desc, const u8 *data,
92 unsigned int len, u8 *out)
94 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
96 return __chksum_finup(&ctx->crc, data, len, out);
99 static int chksum_digest(struct shash_desc *desc, const u8 *data,
100 unsigned int length, u8 *out)
102 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
104 return __chksum_finup(&mctx->key, data, length, out);
107 static int crc32c_cra_init(struct crypto_tfm *tfm)
109 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
115 static struct shash_alg alg = {
116 .digestsize = CHKSUM_DIGEST_SIZE,
117 .setkey = chksum_setkey,
119 .update = chksum_update,
120 .final = chksum_final,
121 .finup = chksum_finup,
122 .digest = chksum_digest,
123 .descsize = sizeof(struct chksum_desc_ctx),
125 .cra_name = "crc32c",
126 .cra_driver_name = "crc32c-generic",
128 .cra_blocksize = CHKSUM_BLOCK_SIZE,
130 .cra_ctxsize = sizeof(struct chksum_ctx),
131 .cra_module = THIS_MODULE,
132 .cra_init = crc32c_cra_init,
136 static int __init crc32c_mod_init(void)
138 return crypto_register_shash(&alg);
141 static void __exit crc32c_mod_fini(void)
143 crypto_unregister_shash(&alg);
146 module_init(crc32c_mod_init);
147 module_exit(crc32c_mod_fini);
149 MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
150 MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
151 MODULE_LICENSE("GPL");