[CRYPTO] crc32c: Fix unconventional setkey usage
[linux-2.6-block.git] / crypto / crc32c.c
CommitLineData
1da177e4
LT
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>
25cdbcd9 19#include <linux/kernel.h>
1da177e4
LT
20
21#define CHKSUM_BLOCK_SIZE 32
22#define CHKSUM_DIGEST_SIZE 4
23
24struct chksum_ctx {
25 u32 crc;
25cdbcd9 26 u32 key;
1da177e4
LT
27};
28
29/*
30 * Steps through buffer one byte at at time, calculates reflected
31 * crc using table.
32 */
33
6c2bb98b 34static void chksum_init(struct crypto_tfm *tfm)
1da177e4 35{
6c2bb98b 36 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
1da177e4 37
25cdbcd9 38 mctx->crc = mctx->key;
1da177e4
LT
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 */
6c2bb98b
HX
46static int chksum_setkey(struct crypto_tfm *tfm, const u8 *key,
47 unsigned int keylen, u32 *flags)
1da177e4 48{
6c2bb98b 49 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
1da177e4
LT
50
51 if (keylen != sizeof(mctx->crc)) {
52 if (flags)
53 *flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
54 return -EINVAL;
55 }
25cdbcd9 56 mctx->key = le32_to_cpu(*(__le32 *)key);
1da177e4
LT
57 return 0;
58}
59
6c2bb98b
HX
60static void chksum_update(struct crypto_tfm *tfm, const u8 *data,
61 unsigned int length)
1da177e4 62{
6c2bb98b 63 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
1da177e4 64
25cdbcd9 65 mctx->crc = crc32c(mctx->crc, data, length);
1da177e4
LT
66}
67
6c2bb98b 68static void chksum_final(struct crypto_tfm *tfm, u8 *out)
1da177e4 69{
6c2bb98b 70 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
1da177e4 71
25cdbcd9
HX
72 *(__le32 *)out = ~cpu_to_le32(mctx->crc);
73}
74
75static int crc32c_cra_init(struct crypto_tfm *tfm)
76{
77 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
78
79 mctx->key = ~0;
80 return 0;
1da177e4
LT
81}
82
83static struct crypto_alg alg = {
84 .cra_name = "crc32c",
85 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
86 .cra_blocksize = CHKSUM_BLOCK_SIZE,
87 .cra_ctxsize = sizeof(struct chksum_ctx),
88 .cra_module = THIS_MODULE,
89 .cra_list = LIST_HEAD_INIT(alg.cra_list),
25cdbcd9 90 .cra_init = crc32c_cra_init,
1da177e4
LT
91 .cra_u = {
92 .digest = {
93 .dia_digestsize= CHKSUM_DIGEST_SIZE,
94 .dia_setkey = chksum_setkey,
95 .dia_init = chksum_init,
96 .dia_update = chksum_update,
97 .dia_final = chksum_final
98 }
99 }
100};
101
102static int __init init(void)
103{
104 return crypto_register_alg(&alg);
105}
106
107static void __exit fini(void)
108{
109 crypto_unregister_alg(&alg);
110}
111
112module_init(init);
113module_exit(fini);
114
115MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
116MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
117MODULE_LICENSE("GPL");