Whamcloud - gitweb
LU-1201 checksum: add libcfs crypto hash
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-crypto-adler.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 zlib_adler32.
30  */
31
32 #include <linux/zutil.h>
33 #ifdef HAVE_STRUCT_SHASH_ALG
34 #include <crypto/internal/hash.h>
35 #else
36 #include <linux/crypto.h>
37 #endif
38
39
40 #define CHKSUM_BLOCK_SIZE       1
41 #define CHKSUM_DIGEST_SIZE      4
42
43
44 static u32 __adler32(u32 cksum, unsigned char const *p, size_t len)
45 {
46         return zlib_adler32(cksum, p, len);
47 }
48
49 static int adler32_cra_init(struct crypto_tfm *tfm)
50 {
51         u32 *key = crypto_tfm_ctx(tfm);
52
53         *key = 1;
54
55         return 0;
56 }
57
58 #ifdef HAVE_STRUCT_SHASH_ALG
59 static int adler32_setkey(struct crypto_shash *hash, const u8 *key,
60                           unsigned int keylen)
61 {
62         u32 *mctx = crypto_shash_ctx(hash);
63
64         if (keylen != sizeof(u32)) {
65                 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
66                 return -EINVAL;
67         }
68         *mctx = *(u32 *)key;
69         return 0;
70 }
71
72 static int adler32_init(struct shash_desc *desc)
73 {
74         u32 *mctx = crypto_shash_ctx(desc->tfm);
75         u32 *cksump = shash_desc_ctx(desc);
76
77         *cksump = *mctx;
78
79         return 0;
80 }
81
82 static int adler32_update(struct shash_desc *desc, const u8 *data,
83                           unsigned int len)
84 {
85         u32 *cksump = shash_desc_ctx(desc);
86
87         *cksump = __adler32(*cksump, data, len);
88         return 0;
89 }
90 static int __adler32_finup(u32 *cksump, const u8 *data, unsigned int len,
91                            u8 *out)
92 {
93         *(u32 *)out = __adler32(*cksump, data, len);
94         return 0;
95 }
96
97 static int adler32_finup(struct shash_desc *desc, const u8 *data,
98                          unsigned int len, u8 *out)
99 {
100         return __adler32_finup(shash_desc_ctx(desc), data, len, out);
101 }
102
103 static int adler32_final(struct shash_desc *desc, u8 *out)
104 {
105         u32 *cksump = shash_desc_ctx(desc);
106
107         *(u32 *)out = *cksump;
108         return 0;
109 }
110
111 static int adler32_digest(struct shash_desc *desc, const u8 *data,
112                           unsigned int len, u8 *out)
113 {
114         return __adler32_finup(crypto_shash_ctx(desc->tfm), data, len,
115                                     out);
116 }
117 static struct shash_alg alg = {
118         .setkey         = adler32_setkey,
119         .init           = adler32_init,
120         .update         = adler32_update,
121         .final          = adler32_final,
122         .finup          = adler32_finup,
123         .digest         = adler32_digest,
124         .descsize       = sizeof(u32),
125         .digestsize     = CHKSUM_DIGEST_SIZE,
126         .base           = {
127                 .cra_name               = "adler32",
128                 .cra_driver_name        = "adler32-zlib",
129                 .cra_priority           = 100,
130                 .cra_blocksize          = CHKSUM_BLOCK_SIZE,
131                 .cra_ctxsize            = sizeof(u32),
132                 .cra_module             = THIS_MODULE,
133                 .cra_init               = adler32_cra_init,
134         }
135 };
136 #else   /* HAVE_STRUCT_SHASH_ALG */
137 #ifdef HAVE_DIGEST_SETKEY_FLAGS
138 static int adler32_digest_setkey(struct crypto_tfm *tfm, const u8 *key,
139                                  unsigned int keylen, u32 *flags)
140 #else
141 static int adler32_digest_setkey(struct crypto_tfm *tfm, const u8 *key,
142                                  unsigned int keylen)
143 #endif
144 {
145         u32 *mctx = crypto_tfm_ctx(tfm);
146
147         if (keylen != sizeof(u32)) {
148                 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
149                 return -EINVAL;
150         }
151         *mctx = le32_to_cpup((__le32 *)key);
152         return 0;
153 }
154
155 static void adler32_digest_init(struct crypto_tfm *tfm)
156 {
157         u32 *mctx = crypto_tfm_ctx(tfm);
158
159         *mctx = 0;
160
161 }
162 static void adler32_digest_update(struct crypto_tfm *tfm, const u8 *data,
163                                   unsigned int len)
164 {
165         u32 *crcp = crypto_tfm_ctx(tfm);
166
167         *crcp = __adler32(*crcp, data, len);
168 }
169
170 static void adler32_digest_final(struct crypto_tfm *tfm, u8 *out)
171 {
172         u32 *chksum = crypto_tfm_ctx(tfm);
173
174         *(__le32 *)out = cpu_to_le32p(chksum);
175 }
176
177 static struct crypto_alg alg = {
178         .cra_name               = "adler32",
179         .cra_flags              = CRYPTO_ALG_TYPE_DIGEST,
180         .cra_driver_name        = "adler32-zlib",
181         .cra_priority           = 100,
182         .cra_blocksize          = CHKSUM_BLOCK_SIZE,
183         .cra_ctxsize            = sizeof(u32),
184         .cra_module             = THIS_MODULE,
185         .cra_init               = adler32_cra_init,
186         .cra_list               = LIST_HEAD_INIT(alg.cra_list),
187         .cra_u                  = {
188                 .digest         = {
189                                 .dia_digestsize = CHKSUM_DIGEST_SIZE,
190                                 .dia_setkey     = adler32_digest_setkey,
191                                 .dia_init       = adler32_digest_init,
192                                 .dia_update     = adler32_digest_update,
193                                 .dia_final      = adler32_digest_final
194                 }
195         }
196 };
197 #endif  /* HAVE_STRUCT_SHASH_ALG */
198
199
200 int cfs_crypto_adler32_register(void)
201 {
202 #ifdef HAVE_STRUCT_SHASH_ALG
203         return crypto_register_shash(&alg);
204 #else
205         return crypto_register_alg(&alg);
206 #endif
207 }
208 EXPORT_SYMBOL(cfs_crypto_adler32_register);
209
210 void cfs_crypto_adler32_unregister(void)
211 {
212 #ifdef HAVE_STRUCT_SHASH_ALG
213         crypto_unregister_shash(&alg);
214 #else
215         crypto_unregister_alg(&alg);
216 #endif
217 }
218 EXPORT_SYMBOL(cfs_crypto_adler32_unregister);