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