blob: 953362423a5c6c7aaeceb6dda819ba7c701134b7 [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 Xu06ace7a2005-10-30 21:25:15 +110019#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/byteorder.h>
21
22#define CHKSUM_BLOCK_SIZE 32
23#define CHKSUM_DIGEST_SIZE 4
24
25struct chksum_ctx {
26 u32 crc;
27};
28
29/*
30 * Steps through buffer one byte at at time, calculates reflected
31 * crc using table.
32 */
33
34static void chksum_init(void *ctx)
35{
36 struct chksum_ctx *mctx = ctx;
37
38 mctx->crc = ~(u32)0; /* common usage */
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 */
46static int chksum_setkey(void *ctx, const u8 *key, unsigned int keylen,
47 u32 *flags)
48{
49 struct chksum_ctx *mctx = ctx;
50
51 if (keylen != sizeof(mctx->crc)) {
52 if (flags)
53 *flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
54 return -EINVAL;
55 }
56 mctx->crc = __cpu_to_le32(*(u32 *)key);
57 return 0;
58}
59
60static void chksum_update(void *ctx, const u8 *data, unsigned int length)
61{
62 struct chksum_ctx *mctx = ctx;
63 u32 mcrc;
64
65 mcrc = crc32c(mctx->crc, data, (size_t)length);
66
67 mctx->crc = mcrc;
68}
69
70static void chksum_final(void *ctx, u8 *out)
71{
72 struct chksum_ctx *mctx = ctx;
73 u32 mcrc = (mctx->crc ^ ~(u32)0);
74
75 *(u32 *)out = __le32_to_cpu(mcrc);
76}
77
78static struct crypto_alg alg = {
79 .cra_name = "crc32c",
80 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
81 .cra_blocksize = CHKSUM_BLOCK_SIZE,
82 .cra_ctxsize = sizeof(struct chksum_ctx),
83 .cra_module = THIS_MODULE,
84 .cra_list = LIST_HEAD_INIT(alg.cra_list),
85 .cra_u = {
86 .digest = {
87 .dia_digestsize= CHKSUM_DIGEST_SIZE,
88 .dia_setkey = chksum_setkey,
89 .dia_init = chksum_init,
90 .dia_update = chksum_update,
91 .dia_final = chksum_final
92 }
93 }
94};
95
96static int __init init(void)
97{
98 return crypto_register_alg(&alg);
99}
100
101static void __exit fini(void)
102{
103 crypto_unregister_alg(&alg);
104}
105
106module_init(init);
107module_exit(fini);
108
109MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
110MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
111MODULE_LICENSE("GPL");