Whamcloud - gitweb
libext2fs: verify the block numbers for the allocation bitmaps are valid
[tools/e2fsprogs.git] / lib / ext2fs / dirhash.c
1 /*
2  * dirhash.c -- Calculate the hash of a directory entry
3  *
4  * Copyright (c) 2001  Daniel Phillips
5  *
6  * Copyright (c) 2002 Theodore Ts'o.
7  *
8  * %Begin-Header%
9  * This file may be redistributed under the terms of the GNU Library
10  * General Public License, version 2.
11  * %End-Header%
12  */
13
14 #include "config.h"
15 #include <stdio.h>
16 #include <string.h>
17 #include <limits.h>
18
19 #include "ext2_fs.h"
20 #include "ext2fs.h"
21 #include "ext2fsP.h"
22
23 /*
24  * Keyed 32-bit hash function using TEA in a Davis-Meyer function
25  *   H0 = Key
26  *   Hi = E Mi(Hi-1) + Hi-1
27  *
28  * (see Applied Cryptography, 2nd edition, p448).
29  *
30  * Jeremy Fitzhardinge <jeremy@zip.com.au> 1998
31  *
32  * This code is made available under the terms of the GPL
33  */
34 #define DELTA 0x9E3779B9
35
36 static void TEA_transform(__u32 buf[4], __u32 const in[])
37 {
38         __u32   sum = 0;
39         __u32   b0 = buf[0], b1 = buf[1];
40         __u32   a = in[0], b = in[1], c = in[2], d = in[3];
41         int     n = 16;
42
43         do {
44                 sum += DELTA;
45                 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
46                 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
47         } while(--n);
48
49         buf[0] += b0;
50         buf[1] += b1;
51 }
52
53 /* F, G and H are basic MD4 functions: selection, majority, parity */
54 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
55 #define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
56 #define H(x, y, z) ((x) ^ (y) ^ (z))
57
58 /*
59  * The generic round function.  The application is so specific that
60  * we don't bother protecting all the arguments with parens, as is generally
61  * good macro practice, in favor of extra legibility.
62  * Rotation is separate from addition to prevent recomputation
63  */
64 #define ROUND(f, a, b, c, d, x, s)      \
65         (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s)))
66 #define K1 0
67 #define K2 013240474631UL
68 #define K3 015666365641UL
69
70 /*
71  * Basic cut-down MD4 transform.  Returns only 32 bits of result.
72  */
73 static void halfMD4Transform (__u32 buf[4], __u32 const in[])
74 {
75         __u32   a = buf[0], b = buf[1], c = buf[2], d = buf[3];
76
77         /* Round 1 */
78         ROUND(F, a, b, c, d, in[0] + K1,  3);
79         ROUND(F, d, a, b, c, in[1] + K1,  7);
80         ROUND(F, c, d, a, b, in[2] + K1, 11);
81         ROUND(F, b, c, d, a, in[3] + K1, 19);
82         ROUND(F, a, b, c, d, in[4] + K1,  3);
83         ROUND(F, d, a, b, c, in[5] + K1,  7);
84         ROUND(F, c, d, a, b, in[6] + K1, 11);
85         ROUND(F, b, c, d, a, in[7] + K1, 19);
86
87         /* Round 2 */
88         ROUND(G, a, b, c, d, in[1] + K2,  3);
89         ROUND(G, d, a, b, c, in[3] + K2,  5);
90         ROUND(G, c, d, a, b, in[5] + K2,  9);
91         ROUND(G, b, c, d, a, in[7] + K2, 13);
92         ROUND(G, a, b, c, d, in[0] + K2,  3);
93         ROUND(G, d, a, b, c, in[2] + K2,  5);
94         ROUND(G, c, d, a, b, in[4] + K2,  9);
95         ROUND(G, b, c, d, a, in[6] + K2, 13);
96
97         /* Round 3 */
98         ROUND(H, a, b, c, d, in[3] + K3,  3);
99         ROUND(H, d, a, b, c, in[7] + K3,  9);
100         ROUND(H, c, d, a, b, in[2] + K3, 11);
101         ROUND(H, b, c, d, a, in[6] + K3, 15);
102         ROUND(H, a, b, c, d, in[1] + K3,  3);
103         ROUND(H, d, a, b, c, in[5] + K3,  9);
104         ROUND(H, c, d, a, b, in[0] + K3, 11);
105         ROUND(H, b, c, d, a, in[4] + K3, 15);
106
107         buf[0] += a;
108         buf[1] += b;
109         buf[2] += c;
110         buf[3] += d;
111 }
112
113 #undef ROUND
114 #undef F
115 #undef G
116 #undef H
117 #undef K1
118 #undef K2
119 #undef K3
120
121 /* The old legacy hash */
122 static ext2_dirhash_t dx_hack_hash (const char *name, int len,
123                                     int unsigned_flag)
124 {
125         __u32 hash, hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
126         const unsigned char *ucp = (const unsigned char *) name;
127         const signed char *scp = (const signed char *) name;
128         int c;
129
130         while (len--) {
131                 if (unsigned_flag)
132                         c = (int) *ucp++;
133                 else
134                         c = (int) *scp++;
135                 hash = hash1 + (hash0 ^ (c * 7152373));
136
137                 if (hash & 0x80000000) hash -= 0x7fffffff;
138                 hash1 = hash0;
139                 hash0 = hash;
140         }
141         return (hash0 << 1);
142 }
143
144 static void str2hashbuf(const char *msg, int len, __u32 *buf, int num,
145                         int unsigned_flag)
146 {
147         __u32   pad, val;
148         int     i, c;
149         const unsigned char *ucp = (const unsigned char *) msg;
150         const signed char *scp = (const signed char *) msg;
151
152         pad = (__u32)len | ((__u32)len << 8);
153         pad |= pad << 16;
154
155         val = pad;
156         if (len > num*4)
157                 len = num * 4;
158         for (i=0; i < len; i++) {
159                 if (unsigned_flag)
160                         c = (int) ucp[i];
161                 else
162                         c = (int) scp[i];
163
164                 val = c + (val << 8);
165                 if ((i % 4) == 3) {
166                         *buf++ = val;
167                         val = pad;
168                         num--;
169                 }
170         }
171         if (--num >= 0)
172                 *buf++ = val;
173         while (--num >= 0)
174                 *buf++ = pad;
175 }
176
177 /*
178  * Returns the hash of a filename.  If len is 0 and name is NULL, then
179  * this function can be used to test whether or not a hash version is
180  * supported.
181  *
182  * The seed is an 4 longword (32 bits) "secret" which can be used to
183  * uniquify a hash.  If the seed is all zero's, then some default seed
184  * may be used.
185  *
186  * A particular hash version specifies whether or not the seed is
187  * represented, and whether or not the returned hash is 32 bits or 64
188  * bits.  32 bit hashes will return 0 for the minor hash.
189  *
190  * This function doesn't do any normalization or casefolding of the
191  * input string.  To take charset encoding into account, use
192  * ext2fs_dirhash2.
193  *
194  */
195 errcode_t ext2fs_dirhash(int version, const char *name, int len,
196                          const __u32 *seed,
197                          ext2_dirhash_t *ret_hash,
198                          ext2_dirhash_t *ret_minor_hash)
199 {
200         __u32   hash;
201         __u32   minor_hash = 0;
202         const char      *p;
203         int             i;
204         __u32           in[8], buf[4];
205         int             unsigned_flag = 0;
206
207         /* Initialize the default seed for the hash checksum functions */
208         buf[0] = 0x67452301;
209         buf[1] = 0xefcdab89;
210         buf[2] = 0x98badcfe;
211         buf[3] = 0x10325476;
212
213         /* Check to see if the seed is all zero's */
214         if (seed) {
215                 for (i=0; i < 4; i++) {
216                         if (seed[i])
217                                 break;
218                 }
219                 if (i < 4)
220                         memcpy(buf, seed, sizeof(buf));
221         }
222
223         switch (version) {
224         case EXT2_HASH_LEGACY_UNSIGNED:
225                 unsigned_flag++;
226                 /* fallthrough */
227         case EXT2_HASH_LEGACY:
228                 hash = dx_hack_hash(name, len, unsigned_flag);
229                 break;
230         case EXT2_HASH_HALF_MD4_UNSIGNED:
231                 unsigned_flag++;
232                 /* fallthrough */
233         case EXT2_HASH_HALF_MD4:
234                 p = name;
235                 while (len > 0) {
236                         str2hashbuf(p, len, in, 8, unsigned_flag);
237                         halfMD4Transform(buf, in);
238                         len -= 32;
239                         p += 32;
240                 }
241                 minor_hash = buf[2];
242                 hash = buf[1];
243                 break;
244         case EXT2_HASH_TEA_UNSIGNED:
245                 unsigned_flag++;
246                 /* fallthrough */
247         case EXT2_HASH_TEA:
248                 p = name;
249                 while (len > 0) {
250                         str2hashbuf(p, len, in, 4, unsigned_flag);
251                         TEA_transform(buf, in);
252                         len -= 16;
253                         p += 16;
254                 }
255                 hash = buf[0];
256                 minor_hash = buf[1];
257                 break;
258         default:
259                 *ret_hash = 0;
260                 return EXT2_ET_DIRHASH_UNSUPP;
261         }
262         *ret_hash = hash & ~1;
263         if (ret_minor_hash)
264                 *ret_minor_hash = minor_hash;
265         return 0;
266 }
267
268 /*
269  * Returns the hash of a filename considering normalization and
270  * casefolding.  This is a wrapper around ext2fs_dirhash with string
271  * encoding support based on the nls_table and the flags. Check
272  * ext2fs_dirhash for documentation on the input and output parameters.
273  */
274 errcode_t ext2fs_dirhash2(int version, const char *name, int len,
275                           const struct ext2fs_nls_table *charset,
276                           int hash_flags, const __u32 *seed,
277                           ext2_dirhash_t *ret_hash,
278                           ext2_dirhash_t *ret_minor_hash)
279 {
280         errcode_t r;
281         int dlen;
282
283         if (len && charset && (hash_flags & EXT4_CASEFOLD_FL)) {
284                 char buff[PATH_MAX];
285
286                 dlen = charset->ops->casefold(charset,
287                               (const unsigned char *) name, len,
288                               (unsigned char *) buff, sizeof(buff));
289                 if (dlen < 0) {
290                         if (dlen == -EINVAL)
291                                 goto opaque_seq;
292
293                         return dlen;
294                 }
295                 r = ext2fs_dirhash(version, buff, dlen, seed, ret_hash,
296                                    ret_minor_hash);
297                 return r;
298         }
299
300 opaque_seq:
301         return ext2fs_dirhash(version, name, len, seed, ret_hash,
302                               ret_minor_hash);
303 }