dirhash.c revision d1154eb460efe588eaed3d439c1caaca149fa362
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/* 2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * dirhash.c -- Calculate the hash of a directory entry 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright (c) 2001 Daniel Phillips 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * 6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright (c) 2002 Theodore Ts'o. 7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * 8ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * %Begin-Header% 98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * This file may be redistributed under the terms of the GNU Library 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * General Public License, version 2. 11c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com * %End-Header% 128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 136db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com 148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "config.h" 1516edff2b1cbd80e36456138f8631711a585205bareed@google.com#include <stdio.h> 1690bf427001fd4f6d9fcee88911deb015aeb4ab7cmike@reedtribe.org#include <string.h> 178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "ext2_fs.h" 19fd4be26c4202ae91f0f7cf2c03e44b5169d885ebreed@google.com#include "ext2fs.h" 2090bf427001fd4f6d9fcee88911deb015aeb4ab7cmike@reedtribe.org 2190bf427001fd4f6d9fcee88911deb015aeb4ab7cmike@reedtribe.org/* 2290bf427001fd4f6d9fcee88911deb015aeb4ab7cmike@reedtribe.org * Keyed 32-bit hash function using TEA in a Davis-Meyer function 2390bf427001fd4f6d9fcee88911deb015aeb4ab7cmike@reedtribe.org * H0 = Key 246db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com * Hi = E Mi(Hi-1) + Hi-1 256db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com * 268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * (see Applied Cryptography, 2nd edition, p448). 278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * 28d252db03d9650013b545ef9781fe993c07f8f314reed@android.com * Jeremy Fitzhardinge <jeremy@zip.com.au> 1998 298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * 308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * This code is made available under the terms of the GPL 316db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com */ 326db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com#define DELTA 0x9E3779B9 336db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com 346db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.comstatic void TEA_transform(__u32 buf[4], __u32 const in[]) 356db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com{ 366db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com __u32 sum = 0; 376db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com __u32 b0 = buf[0], b1 = buf[1]; 386db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com __u32 a = in[0], b = in[1], c = in[2], d = in[3]; 396db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com int n = 16; 406db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com 416db9375b4f695c68a4e56e38bcd70f983440c2d5reed@google.com do { 428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sum += DELTA; 438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); 458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } while(--n); 468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4716edff2b1cbd80e36456138f8631711a585205bareed@google.com buf[0] += b0; 4890bf427001fd4f6d9fcee88911deb015aeb4ab7cmike@reedtribe.org buf[1] += b1; 4990bf427001fd4f6d9fcee88911deb015aeb4ab7cmike@reedtribe.org} 5090bf427001fd4f6d9fcee88911deb015aeb4ab7cmike@reedtribe.org 5190bf427001fd4f6d9fcee88911deb015aeb4ab7cmike@reedtribe.org/* F, G and H are basic MD4 functions: selection, majority, parity */ 528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) 538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z))) 548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define H(x, y, z) ((x) ^ (y) ^ (z)) 558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* 578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * The generic round function. The application is so specific that 588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * we don't bother protecting all the arguments with parens, as is generally 598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * good macro practice, in favor of extra legibility. 608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Rotation is separate from addition to prevent recomputation 618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define ROUND(f, a, b, c, d, x, s) \ 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s))) 648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define K1 0 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define K2 013240474631UL 668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define K3 015666365641UL 6716edff2b1cbd80e36456138f8631711a585205bareed@google.com 688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* 6954924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com * Basic cut-down MD4 transform. Returns only 32 bits of result. 7054924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com */ 71d2700eec7eb2e26beb206b88a0f0b6f3c5f49118djsollen@google.comstatic void halfMD4Transform (__u32 buf[4], __u32 const in[]) 728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3]; 7416edff2b1cbd80e36456138f8631711a585205bareed@google.com 75d2700eec7eb2e26beb206b88a0f0b6f3c5f49118djsollen@google.com /* Round 1 */ 7690bf427001fd4f6d9fcee88911deb015aeb4ab7cmike@reedtribe.org ROUND(F, a, b, c, d, in[0] + K1, 3); 778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ROUND(F, d, a, b, c, in[1] + K1, 7); 788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ROUND(F, c, d, a, b, in[2] + K1, 11); 79e28b917669fc4677b2f1c0a08c4711b651cbf1a1reed@google.com ROUND(F, b, c, d, a, in[3] + K1, 19); 8018dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com ROUND(F, a, b, c, d, in[4] + K1, 3); 8118dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com ROUND(F, d, a, b, c, in[5] + K1, 7); 8218dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com ROUND(F, c, d, a, b, in[6] + K1, 11); 8318dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com ROUND(F, b, c, d, a, in[7] + K1, 19); 8418dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com 8518dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com /* Round 2 */ 8618dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com ROUND(G, a, b, c, d, in[1] + K2, 3); 8718dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com ROUND(G, d, a, b, c, in[3] + K2, 5); 882b2ede3e713065e1bac461787b0aafb03eaf871fdjsollen@google.com ROUND(G, c, d, a, b, in[5] + K2, 9); 8918dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com ROUND(G, b, c, d, a, in[7] + K2, 13); 9018dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com ROUND(G, a, b, c, d, in[0] + K2, 3); 9154924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com ROUND(G, d, a, b, c, in[2] + K2, 5); 9218dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com ROUND(G, c, d, a, b, in[4] + K2, 9); 932b2ede3e713065e1bac461787b0aafb03eaf871fdjsollen@google.com ROUND(G, b, c, d, a, in[6] + K2, 13); 9418dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com 9518dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com /* Round 3 */ 9618dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com ROUND(H, a, b, c, d, in[3] + K3, 3); 9718dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com ROUND(H, d, a, b, c, in[7] + K3, 9); 9818dc47731f4b37d8896b51f1b92ab31abd78b5a0reed@google.com ROUND(H, c, d, a, b, in[2] + K3, 11); 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ROUND(H, b, c, d, a, in[6] + K3, 15); 100a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com ROUND(H, a, b, c, d, in[1] + K3, 3); 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ROUND(H, d, a, b, c, in[5] + K3, 9); 102a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com 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; 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 */ 120static ext2_dirhash_t dx_hack_hash (const char *name, int len, 121 int unsigned_flag) 122{ 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 128 while (len--) { 129 if (unsigned_flag) 130 c = (int) *ucp++; 131 else 132 c = (int) *scp++; 133 hash = hash1 + (hash0 ^ (c * 7152373)); 134 135 if (hash & 0x80000000) hash -= 0x7fffffff; 136 hash1 = hash0; 137 hash0 = hash; 138 } 139 return (hash0 << 1); 140} 141 142static void str2hashbuf(const char *msg, int len, __u32 *buf, int num, 143 int unsigned_flag) 144{ 145 __u32 pad, val; 146 int i, c; 147 const unsigned char *ucp = (const unsigned char *) msg; 148 const signed char *scp = (const signed char *) msg; 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; 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 */ 190errcode_t ext2fs_dirhash(int version, const char *name, int len, 191 const __u32 *seed, 192 ext2_dirhash_t *ret_hash, 193 ext2_dirhash_t *ret_minor_hash) 194{ 195 __u32 hash; 196 __u32 minor_hash = 0; 197 const char *p; 198 int i; 199 __u32 in[8], buf[4]; 200 int unsigned_flag = 0; 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; 207 208 /* Check to see if the seed is all zero's */ 209 if (seed) { 210 for (i=0; i < 4; i++) { 211 if (seed[i]) 212 break; 213 } 214 if (i < 4) 215 memcpy(buf, seed, sizeof(buf)); 216 } 217 218 switch (version) { 219 case EXT2_HASH_LEGACY_UNSIGNED: 220 unsigned_flag++; 221 /* fallthrough */ 222 case EXT2_HASH_LEGACY: 223 hash = dx_hack_hash(name, len, unsigned_flag); 224 break; 225 case EXT2_HASH_HALF_MD4_UNSIGNED: 226 unsigned_flag++; 227 /* fallthrough */ 228 case EXT2_HASH_HALF_MD4: 229 p = name; 230 while (len > 0) { 231 str2hashbuf(p, len, in, 8, unsigned_flag); 232 halfMD4Transform(buf, in); 233 len -= 32; 234 p += 32; 235 } 236 minor_hash = buf[2]; 237 hash = buf[1]; 238 break; 239 case EXT2_HASH_TEA_UNSIGNED: 240 unsigned_flag++; 241 /* fallthrough */ 242 case EXT2_HASH_TEA: 243 p = name; 244 while (len > 0) { 245 str2hashbuf(p, len, in, 4, unsigned_flag); 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: 254 *ret_hash = 0; 255 return EXT2_ET_DIRHASH_UNSUPP; 256 } 257 *ret_hash = hash & ~1; 258 if (ret_minor_hash) 259 *ret_minor_hash = minor_hash; 260 return 0; 261} 262