crypto: crc32c - Switch to shash
[pandora-kernel.git] / crypto / crc32c.c
1 /* 
2  * Cryptographic API.
3  *
4  * CRC32C chksum
5  *
6  * This module file is a wrapper to invoke the lib/crc32c routines.
7  *
8  * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
9  *
10  * 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  */
16
17 #include <crypto/internal/hash.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/string.h>
21 #include <linux/crc32c.h>
22 #include <linux/kernel.h>
23
24 #define CHKSUM_BLOCK_SIZE       1
25 #define CHKSUM_DIGEST_SIZE      4
26
27 struct chksum_ctx {
28         u32 key;
29 };
30
31 struct chksum_desc_ctx {
32         u32 crc;
33 };
34
35 /*
36  * Steps through buffer one byte at at time, calculates reflected 
37  * crc using table.
38  */
39
40 static int chksum_init(struct shash_desc *desc)
41 {
42         struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
43         struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
44
45         ctx->crc = mctx->key;
46
47         return 0;
48 }
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  */
55 static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
56                          unsigned int keylen)
57 {
58         struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
59
60         if (keylen != sizeof(mctx->key)) {
61                 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
62                 return -EINVAL;
63         }
64         mctx->key = le32_to_cpu(*(__le32 *)key);
65         return 0;
66 }
67
68 static int chksum_update(struct shash_desc *desc, const u8 *data,
69                          unsigned int length)
70 {
71         struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
72
73         ctx->crc = crc32c(ctx->crc, data, length);
74         return 0;
75 }
76
77 static int chksum_final(struct shash_desc *desc, u8 *out)
78 {
79         struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
80
81         *(__le32 *)out = ~cpu_to_le32p(&ctx->crc);
82         return 0;
83 }
84
85 static 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
91 static 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
99 static 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
107 static int crc32c_cra_init(struct crypto_tfm *tfm)
108 {
109         struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
110
111         mctx->key = ~0;
112         return 0;
113 }
114
115 static 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,
133         }
134 };
135
136 static int __init crc32c_mod_init(void)
137 {
138         return crypto_register_shash(&alg);
139 }
140
141 static void __exit crc32c_mod_fini(void)
142 {
143         crypto_unregister_shash(&alg);
144 }
145
146 module_init(crc32c_mod_init);
147 module_exit(crc32c_mod_fini);
148
149 MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
150 MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
151 MODULE_LICENSE("GPL");