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