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 #include <crypto/internal/hash.h>
35 #include "linux-crypto.h"
36
37 #define CHKSUM_BLOCK_SIZE 1
38 #define CHKSUM_DIGEST_SIZE 4
39
__adler32(u32 cksum,unsigned char const * p,size_t len)40 static u32 __adler32(u32 cksum, unsigned char const *p, size_t len)
41 {
42 return zlib_adler32(cksum, p, len);
43 }
44
adler32_cra_init(struct crypto_tfm * tfm)45 static int adler32_cra_init(struct crypto_tfm *tfm)
46 {
47 u32 *key = crypto_tfm_ctx(tfm);
48
49 *key = 1;
50
51 return 0;
52 }
53
adler32_setkey(struct crypto_shash * hash,const u8 * key,unsigned int keylen)54 static int adler32_setkey(struct crypto_shash *hash, const u8 *key,
55 unsigned int keylen)
56 {
57 u32 *mctx = crypto_shash_ctx(hash);
58
59 if (keylen != sizeof(u32)) {
60 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
61 return -EINVAL;
62 }
63 *mctx = *(u32 *)key;
64 return 0;
65 }
66
adler32_init(struct shash_desc * desc)67 static int adler32_init(struct shash_desc *desc)
68 {
69 u32 *mctx = crypto_shash_ctx(desc->tfm);
70 u32 *cksump = shash_desc_ctx(desc);
71
72 *cksump = *mctx;
73
74 return 0;
75 }
76
adler32_update(struct shash_desc * desc,const u8 * data,unsigned int len)77 static int adler32_update(struct shash_desc *desc, const u8 *data,
78 unsigned int len)
79 {
80 u32 *cksump = shash_desc_ctx(desc);
81
82 *cksump = __adler32(*cksump, data, len);
83 return 0;
84 }
__adler32_finup(u32 * cksump,const u8 * data,unsigned int len,u8 * out)85 static int __adler32_finup(u32 *cksump, const u8 *data, unsigned int len,
86 u8 *out)
87 {
88 *(u32 *)out = __adler32(*cksump, data, len);
89 return 0;
90 }
91
adler32_finup(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)92 static int adler32_finup(struct shash_desc *desc, const u8 *data,
93 unsigned int len, u8 *out)
94 {
95 return __adler32_finup(shash_desc_ctx(desc), data, len, out);
96 }
97
adler32_final(struct shash_desc * desc,u8 * out)98 static int adler32_final(struct shash_desc *desc, u8 *out)
99 {
100 u32 *cksump = shash_desc_ctx(desc);
101
102 *(u32 *)out = *cksump;
103 return 0;
104 }
105
adler32_digest(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)106 static int adler32_digest(struct shash_desc *desc, const u8 *data,
107 unsigned int len, u8 *out)
108 {
109 return __adler32_finup(crypto_shash_ctx(desc->tfm), data, len,
110 out);
111 }
112 static struct shash_alg alg = {
113 .setkey = adler32_setkey,
114 .init = adler32_init,
115 .update = adler32_update,
116 .final = adler32_final,
117 .finup = adler32_finup,
118 .digest = adler32_digest,
119 .descsize = sizeof(u32),
120 .digestsize = CHKSUM_DIGEST_SIZE,
121 .base = {
122 .cra_name = "adler32",
123 .cra_driver_name = "adler32-zlib",
124 .cra_priority = 100,
125 .cra_blocksize = CHKSUM_BLOCK_SIZE,
126 .cra_ctxsize = sizeof(u32),
127 .cra_module = THIS_MODULE,
128 .cra_init = adler32_cra_init,
129 }
130 };
131
132
cfs_crypto_adler32_register(void)133 int cfs_crypto_adler32_register(void)
134 {
135 return crypto_register_shash(&alg);
136 }
137
cfs_crypto_adler32_unregister(void)138 void cfs_crypto_adler32_unregister(void)
139 {
140 crypto_unregister_shash(&alg);
141 }
142