blob: 0fa744392a4c35fe12d08cd1e46fabcc5c560eb9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Xu25cdbcd2006-08-06 23:03:08 +100019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#define CHKSUM_BLOCK_SIZE 32
22#define CHKSUM_DIGEST_SIZE 4
23
24struct chksum_ctx {
25 u32 crc;
Herbert Xu25cdbcd2006-08-06 23:03:08 +100026 u32 key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027};
28
29/*
30 * Steps through buffer one byte at at time, calculates reflected
31 * crc using table.
32 */
33
Herbert Xu6c2bb982006-05-16 22:09:29 +100034static void chksum_init(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Herbert Xu6c2bb982006-05-16 22:09:29 +100036 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Herbert Xu25cdbcd2006-08-06 23:03:08 +100038 mctx->crc = mctx->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
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 Xu6c2bb982006-05-16 22:09:29 +100046static int chksum_setkey(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +100047 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Herbert Xu6c2bb982006-05-16 22:09:29 +100049 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 if (keylen != sizeof(mctx->crc)) {
Herbert Xu560c06a2006-08-13 14:16:39 +100052 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return -EINVAL;
54 }
Herbert Xu25cdbcd2006-08-06 23:03:08 +100055 mctx->key = le32_to_cpu(*(__le32 *)key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return 0;
57}
58
Herbert Xu6c2bb982006-05-16 22:09:29 +100059static void chksum_update(struct crypto_tfm *tfm, const u8 *data,
60 unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Herbert Xu6c2bb982006-05-16 22:09:29 +100062 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Herbert Xu25cdbcd2006-08-06 23:03:08 +100064 mctx->crc = crc32c(mctx->crc, data, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Herbert Xu6c2bb982006-05-16 22:09:29 +100067static void chksum_final(struct crypto_tfm *tfm, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Herbert Xu6c2bb982006-05-16 22:09:29 +100069 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Herbert Xu25cdbcd2006-08-06 23:03:08 +100071 *(__le32 *)out = ~cpu_to_le32(mctx->crc);
72}
73
74static 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 Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static 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 Xu25cdbcd2006-08-06 23:03:08 +100089 .cra_init = crc32c_cra_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 .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
101static int __init init(void)
102{
103 return crypto_register_alg(&alg);
104}
105
106static void __exit fini(void)
107{
108 crypto_unregister_alg(&alg);
109}
110
111module_init(init);
112module_exit(fini);
113
114MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
115MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
116MODULE_LICENSE("GPL");