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.
25 * Copyright 2012 Xyratex Technology Limited
29 * This is crypto api shash wrappers to zlib_adler32.
32 #include <linux/module.h>
33 #include <linux/zutil.h>
34 #ifdef HAVE_STRUCT_SHASH_ALG
35 #include <crypto/internal/hash.h>
37 #include <linux/crypto.h>
41 #define CHKSUM_BLOCK_SIZE 1
42 #define CHKSUM_DIGEST_SIZE 4
45 static u32 __adler32(u32 cksum, unsigned char const *p, size_t len)
47 return zlib_adler32(cksum, p, len);
50 static int adler32_cra_init(struct crypto_tfm *tfm)
52 u32 *key = crypto_tfm_ctx(tfm);
59 #ifdef HAVE_STRUCT_SHASH_ALG
60 static int adler32_setkey(struct crypto_shash *hash, const u8 *key,
63 u32 *mctx = crypto_shash_ctx(hash);
65 if (keylen != sizeof(u32)) {
66 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
73 static int adler32_init(struct shash_desc *desc)
75 u32 *mctx = crypto_shash_ctx(desc->tfm);
76 u32 *cksump = shash_desc_ctx(desc);
83 static int adler32_update(struct shash_desc *desc, const u8 *data,
86 u32 *cksump = shash_desc_ctx(desc);
88 *cksump = __adler32(*cksump, data, len);
91 static int __adler32_finup(u32 *cksump, const u8 *data, unsigned int len,
94 *(u32 *)out = __adler32(*cksump, data, len);
98 static int adler32_finup(struct shash_desc *desc, const u8 *data,
99 unsigned int len, u8 *out)
101 return __adler32_finup(shash_desc_ctx(desc), data, len, out);
104 static int adler32_final(struct shash_desc *desc, u8 *out)
106 u32 *cksump = shash_desc_ctx(desc);
108 *(u32 *)out = *cksump;
112 static int adler32_digest(struct shash_desc *desc, const u8 *data,
113 unsigned int len, u8 *out)
115 return __adler32_finup(crypto_shash_ctx(desc->tfm), data, len,
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,
128 .cra_name = "adler32",
129 .cra_driver_name = "adler32-zlib",
131 .cra_blocksize = CHKSUM_BLOCK_SIZE,
132 .cra_ctxsize = sizeof(u32),
133 .cra_module = THIS_MODULE,
134 .cra_init = adler32_cra_init,
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)
142 static int adler32_digest_setkey(struct crypto_tfm *tfm, const u8 *key,
146 u32 *mctx = crypto_tfm_ctx(tfm);
148 if (keylen != sizeof(u32)) {
149 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
152 *mctx = le32_to_cpup((__le32 *)key);
156 static void adler32_digest_init(struct crypto_tfm *tfm)
158 u32 *mctx = crypto_tfm_ctx(tfm);
163 static void adler32_digest_update(struct crypto_tfm *tfm, const u8 *data,
166 u32 *crcp = crypto_tfm_ctx(tfm);
168 *crcp = __adler32(*crcp, data, len);
171 static void adler32_digest_final(struct crypto_tfm *tfm, u8 *out)
173 u32 *chksum = crypto_tfm_ctx(tfm);
175 *(__le32 *)out = cpu_to_le32p(chksum);
178 static struct crypto_alg alg = {
179 .cra_name = "adler32",
180 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
181 .cra_driver_name = "adler32-zlib",
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),
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
198 #endif /* HAVE_STRUCT_SHASH_ALG */
201 int cfs_crypto_adler32_register(void)
203 #ifdef HAVE_STRUCT_SHASH_ALG
204 return crypto_register_shash(&alg);
206 return crypto_register_alg(&alg);
209 EXPORT_SYMBOL(cfs_crypto_adler32_register);
211 void cfs_crypto_adler32_unregister(void)
213 #ifdef HAVE_STRUCT_SHASH_ALG
214 crypto_unregister_shash(&alg);
216 crypto_unregister_alg(&alg);
219 EXPORT_SYMBOL(cfs_crypto_adler32_unregister);