Whamcloud - gitweb
LU-3963 libcfs: remove proc handler wrappers
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-crypto-crc32.c
1 /* GPL HEADER START
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
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.
8  *
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).
14  *
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
17  *
18  * Please  visit http://www.xyratex.com/contact if you need additional
19  * information or have any questions.
20  *
21  * GPL HEADER END
22  */
23
24 /*
25  * Copyright 2012 Xyratex Technology Limited
26  */
27
28 /*
29  * This is crypto api shash wrappers to crc32_le.
30  */
31
32 #include <linux/module.h>
33 #include <linux/crc32.h>
34 #include <crypto/internal/hash.h>
35
36 #define CHKSUM_BLOCK_SIZE       1
37 #define CHKSUM_DIGEST_SIZE      4
38
39 static u32 __crc32_le(u32 crc, unsigned char const *p, size_t len)
40 {
41         return crc32_le(crc, p, len);
42 }
43
44 /** No default init with ~0 */
45 static int crc32_cra_init(struct crypto_tfm *tfm)
46 {
47         u32 *key = crypto_tfm_ctx(tfm);
48
49         *key = 0;
50
51         return 0;
52 }
53
54
55 /*
56  * Setting the seed allows arbitrary accumulators and flexible XOR policy
57  * If your algorithm starts with ~0, then XOR with ~0 before you set
58  * the seed.
59  */
60 static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
61                         unsigned int keylen)
62 {
63         u32 *mctx = crypto_shash_ctx(hash);
64
65         if (keylen != sizeof(u32)) {
66                 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
67                 return -EINVAL;
68         }
69         *mctx = le32_to_cpup((__le32 *)key);
70         return 0;
71 }
72
73 static int crc32_init(struct shash_desc *desc)
74 {
75         u32 *mctx = crypto_shash_ctx(desc->tfm);
76         u32 *crcp = shash_desc_ctx(desc);
77
78         *crcp = *mctx;
79
80         return 0;
81 }
82
83 static int crc32_update(struct shash_desc *desc, const u8 *data,
84                         unsigned int len)
85 {
86         u32 *crcp = shash_desc_ctx(desc);
87
88         *crcp = __crc32_le(*crcp, data, len);
89         return 0;
90 }
91 /* No final XOR 0xFFFFFFFF, like crc32_le */
92 static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
93                          u8 *out)
94 {
95         *(__le32 *)out = cpu_to_le32(__crc32_le(*crcp, data, len));
96         return 0;
97 }
98
99 static int crc32_finup(struct shash_desc *desc, const u8 *data,
100                        unsigned int len, u8 *out)
101 {
102         return __crc32_finup(shash_desc_ctx(desc), data, len, out);
103 }
104
105 static int crc32_final(struct shash_desc *desc, u8 *out)
106 {
107         u32 *crcp = shash_desc_ctx(desc);
108
109         *(__le32 *)out = cpu_to_le32p(crcp);
110         return 0;
111 }
112
113 static int crc32_digest(struct shash_desc *desc, const u8 *data,
114                         unsigned int len, u8 *out)
115 {
116         return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
117                              out);
118 }
119 static struct shash_alg alg = {
120         .setkey         = crc32_setkey,
121         .init           = crc32_init,
122         .update         = crc32_update,
123         .final          = crc32_final,
124         .finup          = crc32_finup,
125         .digest         = crc32_digest,
126         .descsize       = sizeof(u32),
127         .digestsize     = CHKSUM_DIGEST_SIZE,
128         .base           = {
129                 .cra_name               = "crc32",
130                 .cra_driver_name        = "crc32-table",
131                 .cra_priority           = 100,
132                 .cra_blocksize          = CHKSUM_BLOCK_SIZE,
133                 .cra_ctxsize            = sizeof(u32),
134                 .cra_module             = THIS_MODULE,
135                 .cra_init               = crc32_cra_init,
136         }
137 };
138
139 int cfs_crypto_crc32_register(void)
140 {
141         return crypto_register_shash(&alg);
142 }
143 EXPORT_SYMBOL(cfs_crypto_crc32_register);
144
145 void cfs_crypto_crc32_unregister(void)
146 {
147         crypto_unregister_shash(&alg);
148 }
149 EXPORT_SYMBOL(cfs_crypto_crc32_unregister);