clang-format: lwext4 modules
[lwext4.git] / lwext4 / ext4_hash.c
1 /*\r
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)\r
3  *\r
4  * FreeBSD:\r
5  * Copyright (c) 2010, 2013 Zheng Liu <lz@freebsd.org>\r
6  * Copyright (c) 2012, Vyacheslav Matyushin\r
7  * All rights reserved.\r
8  *\r
9  * Redistribution and use in source and binary forms, with or without\r
10  * modification, are permitted provided that the following conditions\r
11  * are met:\r
12  * 1. Redistributions of source code must retain the above copyright\r
13  *    notice, this list of conditions and the following disclaimer.\r
14  * 2. Redistributions in binary form must reproduce the above copyright\r
15  *    notice, this list of conditions and the following disclaimer in the\r
16  *    documentation and/or other materials provided with the distribution.\r
17  *\r
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
28  * SUCH DAMAGE.\r
29  *\r
30  */\r
31 \r
32 /*\r
33  * The following notice applies to the code in ext2_half_md4():\r
34  *\r
35  * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.\r
36  *\r
37  * License to copy and use this software is granted provided that it\r
38  * is identified as the "RSA Data Security, Inc. MD4 Message-Digest\r
39  * Algorithm" in all material mentioning or referencing this software\r
40  * or this function.\r
41  *\r
42  * License is also granted to make and use derivative works provided\r
43  * that such works are identified as "derived from the RSA Data\r
44  * Security, Inc. MD4 Message-Digest Algorithm" in all material\r
45  * mentioning or referencing the derived work.\r
46  *\r
47  * RSA Data Security, Inc. makes no representations concerning either\r
48  * the merchantability of this software or the suitability of this\r
49  * software for any particular purpose. It is provided "as is"\r
50  * without express or implied warranty of any kind.\r
51  *\r
52  * These notices must be retained in any copies of any part of this\r
53  * documentation and/or software.\r
54  */\r
55 \r
56 /** @addtogroup lwext4\r
57  * @{\r
58  */\r
59 /**\r
60  * @file  ext4_hash.c\r
61  * @brief Directory indexing hash functions.\r
62  */\r
63 \r
64 #include <ext4_config.h>\r
65 #include <ext4_types.h>\r
66 #include <string.h>\r
67 #include <ext4_errno.h>\r
68 \r
69 /* F, G, and H are MD4 functions */\r
70 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))\r
71 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))\r
72 #define H(x, y, z) ((x) ^ (y) ^ (z))\r
73 \r
74 /* ROTATE_LEFT rotates x left n bits */\r
75 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))\r
76 \r
77 /*\r
78  * FF, GG, and HH are transformations for rounds 1, 2, and 3.\r
79  * Rotation is separated from addition to prevent recomputation.\r
80  */\r
81 #define FF(a, b, c, d, x, s)                                                   \\r
82     {                                                                          \\r
83         (a) += F((b), (c), (d)) + (x);                                         \\r
84         (a) = ROTATE_LEFT((a), (s));                                           \\r
85     \\r
86 }\r
87 \r
88 #define GG(a, b, c, d, x, s)                                                   \\r
89     {                                                                          \\r
90         (a) += G((b), (c), (d)) + (x) + (uint32_t)0x5A827999;                  \\r
91         (a) = ROTATE_LEFT((a), (s));                                           \\r
92     \\r
93 }\r
94 \r
95 #define HH(a, b, c, d, x, s)                                                   \\r
96     {                                                                          \\r
97         (a) += H((b), (c), (d)) + (x) + (uint32_t)0x6ED9EBA1;                  \\r
98         (a) = ROTATE_LEFT((a), (s));                                           \\r
99     \\r
100 }\r
101 \r
102 /*\r
103  * MD4 basic transformation.  It transforms state based on block.\r
104  *\r
105  * This is a half md4 algorithm since Linux uses this algorithm for dir\r
106  * index.  This function is derived from the RSA Data Security, Inc. MD4\r
107  * Message-Digest Algorithm and was modified as necessary.\r
108  *\r
109  * The return value of this function is uint32_t in Linux, but actually we don't\r
110  * need to check this value, so in our version this function doesn't return any\r
111  * value.\r
112  */\r
113 static void ext2_half_md4(uint32_t hash[4], uint32_t data[8])\r
114 {\r
115     uint32_t a = hash[0], b = hash[1], c = hash[2], d = hash[3];\r
116 \r
117     /* Round 1 */\r
118     FF(a, b, c, d, data[0], 3);\r
119     FF(d, a, b, c, data[1], 7);\r
120     FF(c, d, a, b, data[2], 11);\r
121     FF(b, c, d, a, data[3], 19);\r
122     FF(a, b, c, d, data[4], 3);\r
123     FF(d, a, b, c, data[5], 7);\r
124     FF(c, d, a, b, data[6], 11);\r
125     FF(b, c, d, a, data[7], 19);\r
126 \r
127     /* Round 2 */\r
128     GG(a, b, c, d, data[1], 3);\r
129     GG(d, a, b, c, data[3], 5);\r
130     GG(c, d, a, b, data[5], 9);\r
131     GG(b, c, d, a, data[7], 13);\r
132     GG(a, b, c, d, data[0], 3);\r
133     GG(d, a, b, c, data[2], 5);\r
134     GG(c, d, a, b, data[4], 9);\r
135     GG(b, c, d, a, data[6], 13);\r
136 \r
137     /* Round 3 */\r
138     HH(a, b, c, d, data[3], 3);\r
139     HH(d, a, b, c, data[7], 9);\r
140     HH(c, d, a, b, data[2], 11);\r
141     HH(b, c, d, a, data[6], 15);\r
142     HH(a, b, c, d, data[1], 3);\r
143     HH(d, a, b, c, data[5], 9);\r
144     HH(c, d, a, b, data[0], 11);\r
145     HH(b, c, d, a, data[4], 15);\r
146 \r
147     hash[0] += a;\r
148     hash[1] += b;\r
149     hash[2] += c;\r
150     hash[3] += d;\r
151 }\r
152 \r
153 /*\r
154  * Tiny Encryption Algorithm.\r
155  */\r
156 static void ext2_tea(uint32_t hash[4], uint32_t data[8])\r
157 {\r
158     uint32_t tea_delta = 0x9E3779B9;\r
159     uint32_t sum;\r
160     uint32_t x = hash[0], y = hash[1];\r
161     int n = 16;\r
162     int i = 1;\r
163 \r
164     while (n-- > 0) {\r
165         sum = i * tea_delta;\r
166         x += ((y << 4) + data[0]) ^ (y + sum) ^ ((y >> 5) + data[1]);\r
167         y += ((x << 4) + data[2]) ^ (x + sum) ^ ((x >> 5) + data[3]);\r
168         i++;\r
169     }\r
170 \r
171     hash[0] += x;\r
172     hash[1] += y;\r
173 }\r
174 \r
175 static uint32_t ext2_legacy_hash(const char *name, int len, int unsigned_char)\r
176 {\r
177     uint32_t h0, h1 = 0x12A3FE2D, h2 = 0x37ABE8F9;\r
178     uint32_t multi = 0x6D22F5;\r
179     const unsigned char *uname = (const unsigned char *)name;\r
180     const signed char *sname = (const signed char *)name;\r
181     int val, i;\r
182 \r
183     for (i = 0; i < len; i++) {\r
184         if (unsigned_char)\r
185             val = (unsigned int)*uname++;\r
186         else\r
187             val = (int)*sname++;\r
188 \r
189         h0 = h2 + (h1 ^ (val * multi));\r
190         if (h0 & 0x80000000)\r
191             h0 -= 0x7FFFFFFF;\r
192         h2 = h1;\r
193         h1 = h0;\r
194     }\r
195 \r
196     return (h1 << 1);\r
197 }\r
198 \r
199 static void ext2_prep_hashbuf(const char *src, uint32_t slen, uint32_t *dst,\r
200                               int dlen, int unsigned_char)\r
201 {\r
202     uint32_t padding = slen | (slen << 8) | (slen << 16) | (slen << 24);\r
203     uint32_t buf_val;\r
204     int len, i;\r
205     int buf_byte;\r
206     const unsigned char *ubuf = (const unsigned char *)src;\r
207     const signed char *sbuf = (const signed char *)src;\r
208 \r
209     if (slen > (uint32_t)dlen)\r
210         len = dlen;\r
211     else\r
212         len = slen;\r
213 \r
214     buf_val = padding;\r
215 \r
216     for (i = 0; i < len; i++) {\r
217         if (unsigned_char)\r
218             buf_byte = (unsigned int)ubuf[i];\r
219         else\r
220             buf_byte = (int)sbuf[i];\r
221 \r
222         if ((i % 4) == 0)\r
223             buf_val = padding;\r
224 \r
225         buf_val <<= 8;\r
226         buf_val += buf_byte;\r
227 \r
228         if ((i % 4) == 3) {\r
229             *dst++ = buf_val;\r
230             dlen -= sizeof(uint32_t);\r
231             buf_val = padding;\r
232         }\r
233     }\r
234 \r
235     dlen -= sizeof(uint32_t);\r
236     if (dlen >= 0)\r
237         *dst++ = buf_val;\r
238 \r
239     dlen -= sizeof(uint32_t);\r
240     while (dlen >= 0) {\r
241         *dst++ = padding;\r
242         dlen -= sizeof(uint32_t);\r
243     }\r
244 }\r
245 \r
246 int ext2_htree_hash(const char *name, int len, const uint32_t *hash_seed,\r
247                     int hash_version, uint32_t *hash_major,\r
248                     uint32_t *hash_minor)\r
249 {\r
250     uint32_t hash[4];\r
251     uint32_t data[8];\r
252     uint32_t major = 0, minor = 0;\r
253     int unsigned_char = 0;\r
254 \r
255     if (!name || !hash_major)\r
256         return (-1);\r
257 \r
258     if (len < 1 || len > 255)\r
259         goto error;\r
260 \r
261     hash[0] = 0x67452301;\r
262     hash[1] = 0xEFCDAB89;\r
263     hash[2] = 0x98BADCFE;\r
264     hash[3] = 0x10325476;\r
265 \r
266     if (hash_seed)\r
267         memcpy(hash, hash_seed, sizeof(hash));\r
268 \r
269     switch (hash_version) {\r
270     case EXT2_HTREE_TEA_UNSIGNED:\r
271         unsigned_char = 1;\r
272     case EXT2_HTREE_TEA:\r
273         while (len > 0) {\r
274             ext2_prep_hashbuf(name, len, data, 16, unsigned_char);\r
275             ext2_tea(hash, data);\r
276             len -= 16;\r
277             name += 16;\r
278         }\r
279         major = hash[0];\r
280         minor = hash[1];\r
281         break;\r
282     case EXT2_HTREE_LEGACY_UNSIGNED:\r
283         unsigned_char = 1;\r
284     case EXT2_HTREE_LEGACY:\r
285         major = ext2_legacy_hash(name, len, unsigned_char);\r
286         break;\r
287     case EXT2_HTREE_HALF_MD4_UNSIGNED:\r
288         unsigned_char = 1;\r
289     case EXT2_HTREE_HALF_MD4:\r
290         while (len > 0) {\r
291             ext2_prep_hashbuf(name, len, data, 32, unsigned_char);\r
292             ext2_half_md4(hash, data);\r
293             len -= 32;\r
294             name += 32;\r
295         }\r
296         major = hash[1];\r
297         minor = hash[2];\r
298         break;\r
299     default:\r
300         goto error;\r
301     }\r
302 \r
303     major &= ~1;\r
304     if (major == (EXT2_HTREE_EOF << 1))\r
305         major = (EXT2_HTREE_EOF - 1) << 1;\r
306     *hash_major = major;\r
307     if (hash_minor)\r
308         *hash_minor = minor;\r
309 \r
310     return EOK;\r
311 \r
312 error:\r
313     *hash_major = 0;\r
314     if (hash_minor)\r
315         *hash_minor = 0;\r
316     return ENOTSUP;\r
317 }\r
318 \r
319 /**\r
320  * @}\r
321  */\r