blob: b21b93f2bb903da8604a2eebf8fc35732b6093eb [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 *
Herbert Xu5773a3e2008-07-08 20:54:28 +08008 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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)
13 * any later version.
14 *
15 */
Herbert Xu5773a3e2008-07-08 20:54:28 +080016
17#include <crypto/internal/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/crc32c.h>
Herbert Xu25cdbcd2006-08-06 23:03:08 +100022#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Herbert Xu5773a3e2008-07-08 20:54:28 +080024#define CHKSUM_BLOCK_SIZE 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define CHKSUM_DIGEST_SIZE 4
26
27struct chksum_ctx {
Herbert Xu25cdbcd2006-08-06 23:03:08 +100028 u32 key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029};
30
Herbert Xufaccc4b2008-09-09 17:23:07 +100031struct chksum_desc_ctx {
32 u32 crc;
33};
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
36 * Steps through buffer one byte at at time, calculates reflected
37 * crc using table.
38 */
39
Herbert Xufaccc4b2008-09-09 17:23:07 +100040static int chksum_init(struct shash_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Herbert Xufaccc4b2008-09-09 17:23:07 +100042 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
43 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Herbert Xufaccc4b2008-09-09 17:23:07 +100045 ctx->crc = mctx->key;
46
47 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50/*
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
53 * the seed.
54 */
Herbert Xufaccc4b2008-09-09 17:23:07 +100055static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +100056 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Herbert Xufaccc4b2008-09-09 17:23:07 +100058 struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Herbert Xufaccc4b2008-09-09 17:23:07 +100060 if (keylen != sizeof(mctx->key)) {
61 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return -EINVAL;
63 }
Herbert Xu25cdbcd2006-08-06 23:03:08 +100064 mctx->key = le32_to_cpu(*(__le32 *)key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return 0;
66}
67
Herbert Xufaccc4b2008-09-09 17:23:07 +100068static int chksum_update(struct shash_desc *desc, const u8 *data,
69 unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Herbert Xufaccc4b2008-09-09 17:23:07 +100071 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Herbert Xufaccc4b2008-09-09 17:23:07 +100073 ctx->crc = crc32c(ctx->crc, data, length);
74 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Herbert Xufaccc4b2008-09-09 17:23:07 +100077static int chksum_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Herbert Xufaccc4b2008-09-09 17:23:07 +100079 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
80
81 *(__le32 *)out = ~cpu_to_le32p(&ctx->crc);
82 return 0;
Herbert Xu25cdbcd2006-08-06 23:03:08 +100083}
84
Herbert Xufaccc4b2008-09-09 17:23:07 +100085static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
86{
87 *(__le32 *)out = ~cpu_to_le32(crc32c(*crcp, data, len));
88 return 0;
89}
90
91static int chksum_finup(struct shash_desc *desc, const u8 *data,
92 unsigned int len, u8 *out)
93{
94 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
95
96 return __chksum_finup(&ctx->crc, data, len, out);
97}
98
99static int chksum_digest(struct shash_desc *desc, const u8 *data,
100 unsigned int length, u8 *out)
101{
102 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
103
104 return __chksum_finup(&mctx->key, data, length, out);
105}
106
107static int crc32c_cra_init(struct crypto_tfm *tfm)
Herbert Xu25cdbcd2006-08-06 23:03:08 +1000108{
109 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
110
111 mctx->key = ~0;
112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Herbert Xufaccc4b2008-09-09 17:23:07 +1000115static struct shash_alg alg = {
116 .digestsize = CHKSUM_DIGEST_SIZE,
117 .setkey = chksum_setkey,
118 .init = chksum_init,
119 .update = chksum_update,
120 .final = chksum_final,
121 .finup = chksum_finup,
122 .digest = chksum_digest,
123 .descsize = sizeof(struct chksum_desc_ctx),
124 .base = {
125 .cra_name = "crc32c",
126 .cra_driver_name = "crc32c-generic",
127 .cra_priority = 100,
128 .cra_blocksize = CHKSUM_BLOCK_SIZE,
129 .cra_alignmask = 3,
130 .cra_ctxsize = sizeof(struct chksum_ctx),
131 .cra_module = THIS_MODULE,
132 .cra_init = crc32c_cra_init,
Herbert Xu5773a3e2008-07-08 20:54:28 +0800133 }
134};
135
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800136static int __init crc32c_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Herbert Xufaccc4b2008-09-09 17:23:07 +1000138 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800141static void __exit crc32c_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Herbert Xufaccc4b2008-09-09 17:23:07 +1000143 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800146module_init(crc32c_mod_init);
147module_exit(crc32c_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
150MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
151MODULE_LICENSE("GPL");