[PATCH] Fix swsusp with PNP BIOS
[linux-2.6/linux-mips.git] / crypto / crc32c.c
blobf2660123aeb41b9626d11e8518bdc47551733f15
1 /*
2 * Cryptographic API.
4 * CRC32C chksum
6 * This module file is a wrapper to invoke the lib/crc32c routines.
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.
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>
19 #include <linux/types.h>
20 #include <asm/byteorder.h>
22 #define CHKSUM_BLOCK_SIZE 32
23 #define CHKSUM_DIGEST_SIZE 4
25 struct chksum_ctx {
26 u32 crc;
30 * Steps through buffer one byte at at time, calculates reflected
31 * crc using table.
34 static void chksum_init(struct crypto_tfm *tfm)
36 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
38 mctx->crc = ~(u32)0; /* common usage */
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.
46 static int chksum_setkey(struct crypto_tfm *tfm, const u8 *key,
47 unsigned int keylen, u32 *flags)
49 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
51 if (keylen != sizeof(mctx->crc)) {
52 if (flags)
53 *flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
54 return -EINVAL;
56 mctx->crc = __cpu_to_le32(*(u32 *)key);
57 return 0;
60 static void chksum_update(struct crypto_tfm *tfm, const u8 *data,
61 unsigned int length)
63 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
64 u32 mcrc;
66 mcrc = crc32c(mctx->crc, data, (size_t)length);
68 mctx->crc = mcrc;
71 static void chksum_final(struct crypto_tfm *tfm, u8 *out)
73 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
74 u32 mcrc = (mctx->crc ^ ~(u32)0);
76 *(u32 *)out = __le32_to_cpu(mcrc);
79 static struct crypto_alg alg = {
80 .cra_name = "crc32c",
81 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
82 .cra_blocksize = CHKSUM_BLOCK_SIZE,
83 .cra_ctxsize = sizeof(struct chksum_ctx),
84 .cra_module = THIS_MODULE,
85 .cra_list = LIST_HEAD_INIT(alg.cra_list),
86 .cra_u = {
87 .digest = {
88 .dia_digestsize= CHKSUM_DIGEST_SIZE,
89 .dia_setkey = chksum_setkey,
90 .dia_init = chksum_init,
91 .dia_update = chksum_update,
92 .dia_final = chksum_final
97 static int __init init(void)
99 return crypto_register_alg(&alg);
102 static void __exit fini(void)
104 crypto_unregister_alg(&alg);
107 module_init(init);
108 module_exit(fini);
110 MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
111 MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
112 MODULE_LICENSE("GPL");