Merge branch 'x86-build-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6.git] / crypto / crc32.c
blob9d1c41569898a96e05b418344545ae6d01e8d00e
1 /* GPL HEADER START
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License version 2 for more details (a copy is included
13 * in the LICENSE file that accompanied this code).
15 * You should have received a copy of the GNU General Public License
16 * version 2 along with this program; If not, see http://www.gnu.org/licenses
18 * Please visit http://www.xyratex.com/contact if you need additional
19 * information or have any questions.
21 * GPL HEADER END
25 * Copyright 2012 Xyratex Technology Limited
29 * This is crypto api shash wrappers to crc32_le.
32 #include <linux/crc32.h>
33 #include <crypto/internal/hash.h>
34 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/string.h>
37 #include <linux/kernel.h>
39 #define CHKSUM_BLOCK_SIZE 1
40 #define CHKSUM_DIGEST_SIZE 4
42 static u32 __crc32_le(u32 crc, unsigned char const *p, size_t len)
44 return crc32_le(crc, p, len);
47 /** No default init with ~0 */
48 static int crc32_cra_init(struct crypto_tfm *tfm)
50 u32 *key = crypto_tfm_ctx(tfm);
52 *key = 0;
54 return 0;
59 * Setting the seed allows arbitrary accumulators and flexible XOR policy
60 * If your algorithm starts with ~0, then XOR with ~0 before you set
61 * the seed.
63 static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
64 unsigned int keylen)
66 u32 *mctx = crypto_shash_ctx(hash);
68 if (keylen != sizeof(u32)) {
69 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
70 return -EINVAL;
72 *mctx = le32_to_cpup((__le32 *)key);
73 return 0;
76 static int crc32_init(struct shash_desc *desc)
78 u32 *mctx = crypto_shash_ctx(desc->tfm);
79 u32 *crcp = shash_desc_ctx(desc);
81 *crcp = *mctx;
83 return 0;
86 static int crc32_update(struct shash_desc *desc, const u8 *data,
87 unsigned int len)
89 u32 *crcp = shash_desc_ctx(desc);
91 *crcp = __crc32_le(*crcp, data, len);
92 return 0;
95 /* No final XOR 0xFFFFFFFF, like crc32_le */
96 static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
97 u8 *out)
99 *(__le32 *)out = cpu_to_le32(__crc32_le(*crcp, data, len));
100 return 0;
103 static int crc32_finup(struct shash_desc *desc, const u8 *data,
104 unsigned int len, u8 *out)
106 return __crc32_finup(shash_desc_ctx(desc), data, len, out);
109 static int crc32_final(struct shash_desc *desc, u8 *out)
111 u32 *crcp = shash_desc_ctx(desc);
113 *(__le32 *)out = cpu_to_le32p(crcp);
114 return 0;
117 static int crc32_digest(struct shash_desc *desc, const u8 *data,
118 unsigned int len, u8 *out)
120 return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
121 out);
123 static struct shash_alg alg = {
124 .setkey = crc32_setkey,
125 .init = crc32_init,
126 .update = crc32_update,
127 .final = crc32_final,
128 .finup = crc32_finup,
129 .digest = crc32_digest,
130 .descsize = sizeof(u32),
131 .digestsize = CHKSUM_DIGEST_SIZE,
132 .base = {
133 .cra_name = "crc32",
134 .cra_driver_name = "crc32-table",
135 .cra_priority = 100,
136 .cra_blocksize = CHKSUM_BLOCK_SIZE,
137 .cra_ctxsize = sizeof(u32),
138 .cra_module = THIS_MODULE,
139 .cra_init = crc32_cra_init,
143 static int __init crc32_mod_init(void)
145 return crypto_register_shash(&alg);
148 static void __exit crc32_mod_fini(void)
150 crypto_unregister_shash(&alg);
153 module_init(crc32_mod_init);
154 module_exit(crc32_mod_fini);
156 MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
157 MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
158 MODULE_LICENSE("GPL");