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