Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 1 | /* |
| 2 | * dirhash.c -- Calculate the hash of a directory entry |
| 3 | * |
| 4 | * Copyright (c) 2001 Daniel Phillips |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 5 | * |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 6 | * Copyright (c) 2002 Theodore Ts'o. |
| 7 | * |
| 8 | * %Begin-Header% |
Theodore Ts'o | 543547a | 2010-05-17 21:31:56 -0400 | [diff] [blame] | 9 | * This file may be redistributed under the terms of the GNU Library |
| 10 | * General Public License, version 2. |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 11 | * %End-Header% |
| 12 | */ |
| 13 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 14 | #include "config.h" |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 15 | #include <stdio.h> |
Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame] | 16 | #include <string.h> |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 17 | |
| 18 | #include "ext2_fs.h" |
| 19 | #include "ext2fs.h" |
| 20 | |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 21 | /* |
| 22 | * Keyed 32-bit hash function using TEA in a Davis-Meyer function |
| 23 | * H0 = Key |
| 24 | * Hi = E Mi(Hi-1) + Hi-1 |
| 25 | * |
| 26 | * (see Applied Cryptography, 2nd edition, p448). |
| 27 | * |
| 28 | * Jeremy Fitzhardinge <jeremy@zip.com.au> 1998 |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 29 | * |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 30 | * This code is made available under the terms of the GPL |
| 31 | */ |
| 32 | #define DELTA 0x9E3779B9 |
| 33 | |
| 34 | static void TEA_transform(__u32 buf[4], __u32 const in[]) |
| 35 | { |
| 36 | __u32 sum = 0; |
| 37 | __u32 b0 = buf[0], b1 = buf[1]; |
| 38 | __u32 a = in[0], b = in[1], c = in[2], d = in[3]; |
| 39 | int n = 16; |
| 40 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 41 | do { |
| 42 | sum += DELTA; |
| 43 | b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); |
| 44 | b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 45 | } while(--n); |
| 46 | |
| 47 | buf[0] += b0; |
| 48 | buf[1] += b1; |
| 49 | } |
| 50 | |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 51 | /* F, G and H are basic MD4 functions: selection, majority, parity */ |
| 52 | #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) |
| 53 | #define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z))) |
| 54 | #define H(x, y, z) ((x) ^ (y) ^ (z)) |
| 55 | |
| 56 | /* |
| 57 | * The generic round function. The application is so specific that |
| 58 | * we don't bother protecting all the arguments with parens, as is generally |
| 59 | * good macro practice, in favor of extra legibility. |
| 60 | * Rotation is separate from addition to prevent recomputation |
| 61 | */ |
| 62 | #define ROUND(f, a, b, c, d, x, s) \ |
| 63 | (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s))) |
| 64 | #define K1 0 |
| 65 | #define K2 013240474631UL |
| 66 | #define K3 015666365641UL |
| 67 | |
| 68 | /* |
| 69 | * Basic cut-down MD4 transform. Returns only 32 bits of result. |
| 70 | */ |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 71 | static void halfMD4Transform (__u32 buf[4], __u32 const in[]) |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 72 | { |
| 73 | __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3]; |
| 74 | |
| 75 | /* Round 1 */ |
| 76 | ROUND(F, a, b, c, d, in[0] + K1, 3); |
| 77 | ROUND(F, d, a, b, c, in[1] + K1, 7); |
| 78 | ROUND(F, c, d, a, b, in[2] + K1, 11); |
| 79 | ROUND(F, b, c, d, a, in[3] + K1, 19); |
| 80 | ROUND(F, a, b, c, d, in[4] + K1, 3); |
| 81 | ROUND(F, d, a, b, c, in[5] + K1, 7); |
| 82 | ROUND(F, c, d, a, b, in[6] + K1, 11); |
| 83 | ROUND(F, b, c, d, a, in[7] + K1, 19); |
| 84 | |
| 85 | /* Round 2 */ |
| 86 | ROUND(G, a, b, c, d, in[1] + K2, 3); |
| 87 | ROUND(G, d, a, b, c, in[3] + K2, 5); |
| 88 | ROUND(G, c, d, a, b, in[5] + K2, 9); |
| 89 | ROUND(G, b, c, d, a, in[7] + K2, 13); |
| 90 | ROUND(G, a, b, c, d, in[0] + K2, 3); |
| 91 | ROUND(G, d, a, b, c, in[2] + K2, 5); |
| 92 | ROUND(G, c, d, a, b, in[4] + K2, 9); |
| 93 | ROUND(G, b, c, d, a, in[6] + K2, 13); |
| 94 | |
| 95 | /* Round 3 */ |
| 96 | ROUND(H, a, b, c, d, in[3] + K3, 3); |
| 97 | ROUND(H, d, a, b, c, in[7] + K3, 9); |
| 98 | ROUND(H, c, d, a, b, in[2] + K3, 11); |
| 99 | ROUND(H, b, c, d, a, in[6] + K3, 15); |
| 100 | ROUND(H, a, b, c, d, in[1] + K3, 3); |
| 101 | ROUND(H, d, a, b, c, in[5] + K3, 9); |
| 102 | ROUND(H, c, d, a, b, in[0] + K3, 11); |
| 103 | ROUND(H, b, c, d, a, in[4] + K3, 15); |
| 104 | |
| 105 | buf[0] += a; |
| 106 | buf[1] += b; |
| 107 | buf[2] += c; |
| 108 | buf[3] += d; |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | #undef ROUND |
| 112 | #undef F |
| 113 | #undef G |
| 114 | #undef H |
| 115 | #undef K1 |
| 116 | #undef K2 |
| 117 | #undef K3 |
| 118 | |
| 119 | /* The old legacy hash */ |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 120 | static ext2_dirhash_t dx_hack_hash (const char *name, int len, |
| 121 | int unsigned_flag) |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 122 | { |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 123 | __u32 hash, hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9; |
| 124 | const unsigned char *ucp = (const unsigned char *) name; |
| 125 | const signed char *scp = (const signed char *) name; |
| 126 | int c; |
| 127 | |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 128 | while (len--) { |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 129 | if (unsigned_flag) |
| 130 | c = (int) *ucp++; |
| 131 | else |
| 132 | c = (int) *scp++; |
| 133 | hash = hash1 + (hash0 ^ (c * 7152373)); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 134 | |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 135 | if (hash & 0x80000000) hash -= 0x7fffffff; |
| 136 | hash1 = hash0; |
| 137 | hash0 = hash; |
| 138 | } |
Theodore Ts'o | 1acb01b | 2002-03-12 13:41:31 -0500 | [diff] [blame] | 139 | return (hash0 << 1); |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 142 | static void str2hashbuf(const char *msg, int len, __u32 *buf, int num, |
| 143 | int unsigned_flag) |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 144 | { |
| 145 | __u32 pad, val; |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 146 | int i, c; |
| 147 | const unsigned char *ucp = (const unsigned char *) msg; |
| 148 | const signed char *scp = (const signed char *) msg; |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 149 | |
| 150 | pad = (__u32)len | ((__u32)len << 8); |
| 151 | pad |= pad << 16; |
| 152 | |
| 153 | val = pad; |
| 154 | if (len > num*4) |
| 155 | len = num * 4; |
| 156 | for (i=0; i < len; i++) { |
| 157 | if ((i % 4) == 0) |
| 158 | val = pad; |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 159 | if (unsigned_flag) |
| 160 | c = (int) ucp[i]; |
| 161 | else |
| 162 | c = (int) scp[i]; |
| 163 | |
| 164 | val = c + (val << 8); |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 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 | |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 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. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 181 | * |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 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. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 185 | * |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 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. |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 189 | */ |
| 190 | errcode_t ext2fs_dirhash(int version, const char *name, int len, |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 191 | const __u32 *seed, |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 192 | ext2_dirhash_t *ret_hash, |
| 193 | ext2_dirhash_t *ret_minor_hash) |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 194 | { |
| 195 | __u32 hash; |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 196 | __u32 minor_hash = 0; |
Theodore Ts'o | cc90bdf | 2002-07-23 13:11:53 -0400 | [diff] [blame] | 197 | const char *p; |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 198 | int i; |
| 199 | __u32 in[8], buf[4]; |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 200 | int unsigned_flag = 0; |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 201 | |
| 202 | /* Initialize the default seed for the hash checksum functions */ |
| 203 | buf[0] = 0x67452301; |
| 204 | buf[1] = 0xefcdab89; |
| 205 | buf[2] = 0x98badcfe; |
| 206 | buf[3] = 0x10325476; |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 207 | |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 208 | /* Check to see if the seed is all zero's */ |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 209 | if (seed) { |
| 210 | for (i=0; i < 4; i++) { |
| 211 | if (seed[i]) |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 212 | break; |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 213 | } |
| 214 | if (i < 4) |
| 215 | memcpy(buf, seed, sizeof(buf)); |
| 216 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 217 | |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 218 | switch (version) { |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 219 | case EXT2_HASH_LEGACY_UNSIGNED: |
| 220 | unsigned_flag++; |
Eric Sandeen | 9e30fb2 | 2011-09-16 15:49:23 -0500 | [diff] [blame] | 221 | /* fallthrough */ |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 222 | case EXT2_HASH_LEGACY: |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 223 | hash = dx_hack_hash(name, len, unsigned_flag); |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 224 | break; |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 225 | case EXT2_HASH_HALF_MD4_UNSIGNED: |
| 226 | unsigned_flag++; |
Eric Sandeen | 9e30fb2 | 2011-09-16 15:49:23 -0500 | [diff] [blame] | 227 | /* fallthrough */ |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 228 | case EXT2_HASH_HALF_MD4: |
| 229 | p = name; |
| 230 | while (len > 0) { |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 231 | str2hashbuf(p, len, in, 8, unsigned_flag); |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 232 | halfMD4Transform(buf, in); |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 233 | len -= 32; |
| 234 | p += 32; |
| 235 | } |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 236 | minor_hash = buf[2]; |
| 237 | hash = buf[1]; |
| 238 | break; |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 239 | case EXT2_HASH_TEA_UNSIGNED: |
| 240 | unsigned_flag++; |
Eric Sandeen | 9e30fb2 | 2011-09-16 15:49:23 -0500 | [diff] [blame] | 241 | /* fallthrough */ |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 242 | case EXT2_HASH_TEA: |
| 243 | p = name; |
| 244 | while (len > 0) { |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 245 | str2hashbuf(p, len, in, 4, unsigned_flag); |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 246 | TEA_transform(buf, in); |
| 247 | len -= 16; |
| 248 | p += 16; |
| 249 | } |
| 250 | hash = buf[0]; |
| 251 | minor_hash = buf[1]; |
| 252 | break; |
| 253 | default: |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 254 | *ret_hash = 0; |
| 255 | return EXT2_ET_DIRHASH_UNSUPP; |
| 256 | } |
Theodore Ts'o | b33278c | 2002-08-17 10:52:51 -0400 | [diff] [blame] | 257 | *ret_hash = hash & ~1; |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 258 | if (ret_minor_hash) |
| 259 | *ret_minor_hash = minor_hash; |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 260 | return 0; |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 261 | } |