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 #include <crypto/internal/hash.h>
37 #define CHKSUM_BLOCK_SIZE 1
38 #define CHKSUM_DIGEST_SIZE 4
41 static u32 __adler32(u32 cksum, unsigned char const *p, size_t len)
43 return zlib_adler32(cksum, p, len);
46 static int adler32_cra_init(struct crypto_tfm *tfm)
48 u32 *key = crypto_tfm_ctx(tfm);
55 static int adler32_setkey(struct crypto_shash *hash, const u8 *key,
58 u32 *mctx = crypto_shash_ctx(hash);
60 if (keylen != sizeof(u32)) {
61 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
68 static int adler32_init(struct shash_desc *desc)
70 u32 *mctx = crypto_shash_ctx(desc->tfm);
71 u32 *cksump = shash_desc_ctx(desc);
78 static int adler32_update(struct shash_desc *desc, const u8 *data,
81 u32 *cksump = shash_desc_ctx(desc);
83 *cksump = __adler32(*cksump, data, len);
86 static int __adler32_finup(u32 *cksump, const u8 *data, unsigned int len,
89 *(u32 *)out = __adler32(*cksump, data, len);
93 static int adler32_finup(struct shash_desc *desc, const u8 *data,
94 unsigned int len, u8 *out)
96 return __adler32_finup(shash_desc_ctx(desc), data, len, out);
99 static int adler32_final(struct shash_desc *desc, u8 *out)
101 u32 *cksump = shash_desc_ctx(desc);
103 *(u32 *)out = *cksump;
107 static int adler32_digest(struct shash_desc *desc, const u8 *data,
108 unsigned int len, u8 *out)
110 return __adler32_finup(crypto_shash_ctx(desc->tfm), data, len,
113 static struct shash_alg alg = {
114 .setkey = adler32_setkey,
115 .init = adler32_init,
116 .update = adler32_update,
117 .final = adler32_final,
118 .finup = adler32_finup,
119 .digest = adler32_digest,
120 .descsize = sizeof(u32),
121 .digestsize = CHKSUM_DIGEST_SIZE,
123 .cra_name = "adler32",
124 .cra_driver_name = "adler32-zlib",
126 .cra_blocksize = CHKSUM_BLOCK_SIZE,
127 .cra_ctxsize = sizeof(u32),
128 .cra_module = THIS_MODULE,
129 .cra_init = adler32_cra_init,
133 int cfs_crypto_adler32_register(void)
135 return crypto_register_shash(&alg);
137 EXPORT_SYMBOL(cfs_crypto_adler32_register);
139 void cfs_crypto_adler32_unregister(void)
141 crypto_unregister_shash(&alg);
143 EXPORT_SYMBOL(cfs_crypto_adler32_unregister);