blob: a882d9e4e63e741fd90ec2d6183cee337c841c54 [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 {
28 u32 crc;
Herbert Xu25cdbcd2006-08-06 23:03:08 +100029 u32 key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
31
32/*
33 * Steps through buffer one byte at at time, calculates reflected
34 * crc using table.
35 */
36
Herbert Xu6c2bb982006-05-16 22:09:29 +100037static void chksum_init(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Herbert Xu6c2bb982006-05-16 22:09:29 +100039 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Herbert Xu25cdbcd2006-08-06 23:03:08 +100041 mctx->crc = mctx->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
44/*
45 * Setting the seed allows arbitrary accumulators and flexible XOR policy
46 * If your algorithm starts with ~0, then XOR with ~0 before you set
47 * the seed.
48 */
Herbert Xu6c2bb982006-05-16 22:09:29 +100049static int chksum_setkey(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +100050 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Herbert Xu6c2bb982006-05-16 22:09:29 +100052 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 if (keylen != sizeof(mctx->crc)) {
Herbert Xu560c06a2006-08-13 14:16:39 +100055 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return -EINVAL;
57 }
Herbert Xu25cdbcd2006-08-06 23:03:08 +100058 mctx->key = le32_to_cpu(*(__le32 *)key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return 0;
60}
61
Herbert Xu6c2bb982006-05-16 22:09:29 +100062static void chksum_update(struct crypto_tfm *tfm, const u8 *data,
63 unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Herbert Xu6c2bb982006-05-16 22:09:29 +100065 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Herbert Xu25cdbcd2006-08-06 23:03:08 +100067 mctx->crc = crc32c(mctx->crc, data, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Herbert Xu6c2bb982006-05-16 22:09:29 +100070static void chksum_final(struct crypto_tfm *tfm, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Herbert Xu6c2bb982006-05-16 22:09:29 +100072 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Herbert Xu25cdbcd2006-08-06 23:03:08 +100074 *(__le32 *)out = ~cpu_to_le32(mctx->crc);
75}
76
Herbert Xu5773a3e2008-07-08 20:54:28 +080077static int crc32c_cra_init_old(struct crypto_tfm *tfm)
Herbert Xu25cdbcd2006-08-06 23:03:08 +100078{
79 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
80
81 mctx->key = ~0;
82 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Herbert Xu5773a3e2008-07-08 20:54:28 +080085static struct crypto_alg old_alg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 .cra_name = "crc32c",
87 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
88 .cra_blocksize = CHKSUM_BLOCK_SIZE,
89 .cra_ctxsize = sizeof(struct chksum_ctx),
90 .cra_module = THIS_MODULE,
Herbert Xu5773a3e2008-07-08 20:54:28 +080091 .cra_list = LIST_HEAD_INIT(old_alg.cra_list),
92 .cra_init = crc32c_cra_init_old,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .cra_u = {
94 .digest = {
95 .dia_digestsize= CHKSUM_DIGEST_SIZE,
96 .dia_setkey = chksum_setkey,
97 .dia_init = chksum_init,
98 .dia_update = chksum_update,
99 .dia_final = chksum_final
100 }
101 }
102};
103
Herbert Xu5773a3e2008-07-08 20:54:28 +0800104/*
105 * Setting the seed allows arbitrary accumulators and flexible XOR policy
106 * If your algorithm starts with ~0, then XOR with ~0 before you set
107 * the seed.
108 */
109static int crc32c_setkey(struct crypto_ahash *hash, const u8 *key,
110 unsigned int keylen)
111{
112 u32 *mctx = crypto_ahash_ctx(hash);
113
114 if (keylen != sizeof(u32)) {
115 crypto_ahash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
116 return -EINVAL;
117 }
118 *mctx = le32_to_cpup((__le32 *)key);
119 return 0;
120}
121
122static int crc32c_init(struct ahash_request *req)
123{
124 u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
125 u32 *crcp = ahash_request_ctx(req);
126
127 *crcp = *mctx;
128 return 0;
129}
130
131static int crc32c_update(struct ahash_request *req)
132{
133 struct crypto_hash_walk walk;
134 u32 *crcp = ahash_request_ctx(req);
135 u32 crc = *crcp;
136 int nbytes;
137
138 for (nbytes = crypto_hash_walk_first(req, &walk); nbytes;
139 nbytes = crypto_hash_walk_done(&walk, 0))
140 crc = crc32c(crc, walk.data, nbytes);
141
142 *crcp = crc;
143 return 0;
144}
145
146static int crc32c_final(struct ahash_request *req)
147{
148 u32 *crcp = ahash_request_ctx(req);
149
150 *(__le32 *)req->result = ~cpu_to_le32p(crcp);
151 return 0;
152}
153
154static int crc32c_digest(struct ahash_request *req)
155{
156 struct crypto_hash_walk walk;
157 u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
158 u32 crc = *mctx;
159 int nbytes;
160
161 for (nbytes = crypto_hash_walk_first(req, &walk); nbytes;
162 nbytes = crypto_hash_walk_done(&walk, 0))
163 crc = crc32c(crc, walk.data, nbytes);
164
165 *(__le32 *)req->result = ~cpu_to_le32(crc);
166 return 0;
167}
168
169static int crc32c_cra_init(struct crypto_tfm *tfm)
170{
171 u32 *key = crypto_tfm_ctx(tfm);
172
173 *key = ~0;
174
175 tfm->crt_ahash.reqsize = sizeof(u32);
176
177 return 0;
178}
179
180static struct crypto_alg alg = {
181 .cra_name = "crc32c",
182 .cra_driver_name = "crc32c-generic",
183 .cra_priority = 100,
184 .cra_flags = CRYPTO_ALG_TYPE_AHASH,
185 .cra_blocksize = CHKSUM_BLOCK_SIZE,
186 .cra_alignmask = 3,
187 .cra_ctxsize = sizeof(u32),
188 .cra_module = THIS_MODULE,
189 .cra_list = LIST_HEAD_INIT(alg.cra_list),
190 .cra_init = crc32c_cra_init,
191 .cra_type = &crypto_ahash_type,
192 .cra_u = {
193 .ahash = {
194 .digestsize = CHKSUM_DIGEST_SIZE,
195 .setkey = crc32c_setkey,
196 .init = crc32c_init,
197 .update = crc32c_update,
198 .final = crc32c_final,
199 .digest = crc32c_digest,
200 }
201 }
202};
203
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800204static int __init crc32c_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Herbert Xu5773a3e2008-07-08 20:54:28 +0800206 int err;
207
208 err = crypto_register_alg(&old_alg);
209 if (err)
210 return err;
211
212 err = crypto_register_alg(&alg);
213 if (err)
214 crypto_unregister_alg(&old_alg);
215
216 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800219static void __exit crc32c_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 crypto_unregister_alg(&alg);
Herbert Xu5773a3e2008-07-08 20:54:28 +0800222 crypto_unregister_alg(&old_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800225module_init(crc32c_mod_init);
226module_exit(crc32c_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
229MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
230MODULE_LICENSE("GPL");