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/zutil.h>
33 #ifdef HAVE_STRUCT_SHASH_ALG
34 #include <crypto/internal/hash.h>
36 #include <linux/crypto.h>
40 #define CHKSUM_BLOCK_SIZE 1
41 #define CHKSUM_DIGEST_SIZE 4
44 static u32 __adler32(u32 cksum, unsigned char const *p, size_t len)
46 return zlib_adler32(cksum, p, len);
49 static int adler32_cra_init(struct crypto_tfm *tfm)
51 u32 *key = crypto_tfm_ctx(tfm);
58 #ifdef HAVE_STRUCT_SHASH_ALG
59 static int adler32_setkey(struct crypto_shash *hash, const u8 *key,
62 u32 *mctx = crypto_shash_ctx(hash);
64 if (keylen != sizeof(u32)) {
65 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
72 static int adler32_init(struct shash_desc *desc)
74 u32 *mctx = crypto_shash_ctx(desc->tfm);
75 u32 *cksump = shash_desc_ctx(desc);
82 static int adler32_update(struct shash_desc *desc, const u8 *data,
85 u32 *cksump = shash_desc_ctx(desc);
87 *cksump = __adler32(*cksump, data, len);
90 static int __adler32_finup(u32 *cksump, const u8 *data, unsigned int len,
93 *(u32 *)out = __adler32(*cksump, data, len);
97 static int adler32_finup(struct shash_desc *desc, const u8 *data,
98 unsigned int len, u8 *out)
100 return __adler32_finup(shash_desc_ctx(desc), data, len, out);
103 static int adler32_final(struct shash_desc *desc, u8 *out)
105 u32 *cksump = shash_desc_ctx(desc);
107 *(u32 *)out = *cksump;
111 static int adler32_digest(struct shash_desc *desc, const u8 *data,
112 unsigned int len, u8 *out)
114 return __adler32_finup(crypto_shash_ctx(desc->tfm), data, len,
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,
127 .cra_name = "adler32",
128 .cra_driver_name = "adler32-zlib",
130 .cra_blocksize = CHKSUM_BLOCK_SIZE,
131 .cra_ctxsize = sizeof(u32),
132 .cra_module = THIS_MODULE,
133 .cra_init = adler32_cra_init,
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)
141 static int adler32_digest_setkey(struct crypto_tfm *tfm, const u8 *key,
145 u32 *mctx = crypto_tfm_ctx(tfm);
147 if (keylen != sizeof(u32)) {
148 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
151 *mctx = le32_to_cpup((__le32 *)key);
155 static void adler32_digest_init(struct crypto_tfm *tfm)
157 u32 *mctx = crypto_tfm_ctx(tfm);
162 static void adler32_digest_update(struct crypto_tfm *tfm, const u8 *data,
165 u32 *crcp = crypto_tfm_ctx(tfm);
167 *crcp = __adler32(*crcp, data, len);
170 static void adler32_digest_final(struct crypto_tfm *tfm, u8 *out)
172 u32 *chksum = crypto_tfm_ctx(tfm);
174 *(__le32 *)out = cpu_to_le32p(chksum);
177 static struct crypto_alg alg = {
178 .cra_name = "adler32",
179 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
180 .cra_driver_name = "adler32-zlib",
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),
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
197 #endif /* HAVE_STRUCT_SHASH_ALG */
200 int cfs_crypto_adler32_register(void)
202 #ifdef HAVE_STRUCT_SHASH_ALG
203 return crypto_register_shash(&alg);
205 return crypto_register_alg(&alg);
208 EXPORT_SYMBOL(cfs_crypto_adler32_register);
210 void cfs_crypto_adler32_unregister(void)
212 #ifdef HAVE_STRUCT_SHASH_ALG
213 crypto_unregister_shash(&alg);
215 crypto_unregister_alg(&alg);
218 EXPORT_SYMBOL(cfs_crypto_adler32_unregister);