dirhash.c revision d3f917989badf78d1f97654e46d60d1f3d25cd17
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * dirhash.c -- Calculate the hash of a directory entry 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (c) 2001 Daniel Phillips 5c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch * 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (c) 2002 Theodore Ts'o. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * %Begin-Header% 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This file may be redistributed under the terms of the GNU Public 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * License. 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * %End-Header% 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 14#include <stdio.h> 15#include <string.h> 16 17#include "ext2_fs.h" 18#include "ext2fs.h" 19 20/* 21 * Keyed 32-bit hash function using TEA in a Davis-Meyer function 22 * H0 = Key 23 * Hi = E Mi(Hi-1) + Hi-1 24 * 25 * (see Applied Cryptography, 2nd edition, p448). 26 * 27 * Jeremy Fitzhardinge <jeremy@zip.com.au> 1998 28 * 29 * This code is made available under the terms of the GPL 30 */ 31#define DELTA 0x9E3779B9 32 33static void TEA_transform(__u32 buf[4], __u32 const in[]) 34{ 35 __u32 sum = 0; 36 __u32 b0 = buf[0], b1 = buf[1]; 37 __u32 a = in[0], b = in[1], c = in[2], d = in[3]; 38 int n = 16; 39 40 do { 41 sum += DELTA; 42 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); 43 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); 44 } while(--n); 45 46 buf[0] += b0; 47 buf[1] += b1; 48} 49 50/* F, G and H are basic MD4 functions: selection, majority, parity */ 51#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) 52#define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z))) 53#define H(x, y, z) ((x) ^ (y) ^ (z)) 54 55/* 56 * The generic round function. The application is so specific that 57 * we don't bother protecting all the arguments with parens, as is generally 58 * good macro practice, in favor of extra legibility. 59 * Rotation is separate from addition to prevent recomputation 60 */ 61#define ROUND(f, a, b, c, d, x, s) \ 62 (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s))) 63#define K1 0 64#define K2 013240474631UL 65#define K3 015666365641UL 66 67/* 68 * Basic cut-down MD4 transform. Returns only 32 bits of result. 69 */ 70static void halfMD4Transform (__u32 buf[4], __u32 const in[]) 71{ 72 __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3]; 73 74 /* Round 1 */ 75 ROUND(F, a, b, c, d, in[0] + K1, 3); 76 ROUND(F, d, a, b, c, in[1] + K1, 7); 77 ROUND(F, c, d, a, b, in[2] + K1, 11); 78 ROUND(F, b, c, d, a, in[3] + K1, 19); 79 ROUND(F, a, b, c, d, in[4] + K1, 3); 80 ROUND(F, d, a, b, c, in[5] + K1, 7); 81 ROUND(F, c, d, a, b, in[6] + K1, 11); 82 ROUND(F, b, c, d, a, in[7] + K1, 19); 83 84 /* Round 2 */ 85 ROUND(G, a, b, c, d, in[1] + K2, 3); 86 ROUND(G, d, a, b, c, in[3] + K2, 5); 87 ROUND(G, c, d, a, b, in[5] + K2, 9); 88 ROUND(G, b, c, d, a, in[7] + K2, 13); 89 ROUND(G, a, b, c, d, in[0] + K2, 3); 90 ROUND(G, d, a, b, c, in[2] + K2, 5); 91 ROUND(G, c, d, a, b, in[4] + K2, 9); 92 ROUND(G, b, c, d, a, in[6] + K2, 13); 93 94 /* Round 3 */ 95 ROUND(H, a, b, c, d, in[3] + K3, 3); 96 ROUND(H, d, a, b, c, in[7] + K3, 9); 97 ROUND(H, c, d, a, b, in[2] + K3, 11); 98 ROUND(H, b, c, d, a, in[6] + K3, 15); 99 ROUND(H, a, b, c, d, in[1] + K3, 3); 100 ROUND(H, d, a, b, c, in[5] + K3, 9); 101 ROUND(H, c, d, a, b, in[0] + K3, 11); 102 ROUND(H, b, c, d, a, in[4] + K3, 15); 103 104 buf[0] += a; 105 buf[1] += b; 106 buf[2] += c; 107 buf[3] += d; 108} 109 110#undef ROUND 111#undef F 112#undef G 113#undef H 114#undef K1 115#undef K2 116#undef K3 117 118/* The old legacy hash */ 119static ext2_dirhash_t dx_hack_hash (const char *name, int len) 120{ 121 __u32 hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9; 122 while (len--) { 123 __u32 hash = hash1 + (hash0 ^ (*name++ * 7152373)); 124 125 if (hash & 0x80000000) hash -= 0x7fffffff; 126 hash1 = hash0; 127 hash0 = hash; 128 } 129 return (hash0 << 1); 130} 131 132static void str2hashbuf(const char *msg, int len, __u32 *buf, int num) 133{ 134 __u32 pad, val; 135 int i; 136 137 pad = (__u32)len | ((__u32)len << 8); 138 pad |= pad << 16; 139 140 val = pad; 141 if (len > num*4) 142 len = num * 4; 143 for (i=0; i < len; i++) { 144 if ((i % 4) == 0) 145 val = pad; 146 val = msg[i] + (val << 8); 147 if ((i % 4) == 3) { 148 *buf++ = val; 149 val = pad; 150 num--; 151 } 152 } 153 if (--num >= 0) 154 *buf++ = val; 155 while (--num >= 0) 156 *buf++ = pad; 157} 158 159/* 160 * Returns the hash of a filename. If len is 0 and name is NULL, then 161 * this function can be used to test whether or not a hash version is 162 * supported. 163 * 164 * The seed is an 4 longword (32 bits) "secret" which can be used to 165 * uniquify a hash. If the seed is all zero's, then some default seed 166 * may be used. 167 * 168 * A particular hash version specifies whether or not the seed is 169 * represented, and whether or not the returned hash is 32 bits or 64 170 * bits. 32 bit hashes will return 0 for the minor hash. 171 */ 172errcode_t ext2fs_dirhash(int version, const char *name, int len, 173 const __u32 *seed, 174 ext2_dirhash_t *ret_hash, 175 ext2_dirhash_t *ret_minor_hash) 176{ 177 __u32 hash; 178 __u32 minor_hash = 0; 179 const char *p; 180 int i; 181 __u32 in[8], buf[4]; 182 183 /* Initialize the default seed for the hash checksum functions */ 184 buf[0] = 0x67452301; 185 buf[1] = 0xefcdab89; 186 buf[2] = 0x98badcfe; 187 buf[3] = 0x10325476; 188 189 /* Check to see if the seed is all zero's */ 190 if (seed) { 191 for (i=0; i < 4; i++) { 192 if (seed[i]) 193 break; 194 } 195 if (i < 4) 196 memcpy(buf, seed, sizeof(buf)); 197 } 198 199 switch (version) { 200 case EXT2_HASH_LEGACY: 201 hash = dx_hack_hash(name, len); 202 break; 203 case EXT2_HASH_HALF_MD4: 204 p = name; 205 while (len > 0) { 206 str2hashbuf(p, len, in, 8); 207 halfMD4Transform(buf, in); 208 len -= 32; 209 p += 32; 210 } 211 minor_hash = buf[2]; 212 hash = buf[1]; 213 break; 214 case EXT2_HASH_TEA: 215 p = name; 216 while (len > 0) { 217 str2hashbuf(p, len, in, 4); 218 TEA_transform(buf, in); 219 len -= 16; 220 p += 16; 221 } 222 hash = buf[0]; 223 minor_hash = buf[1]; 224 break; 225 default: 226 *ret_hash = 0; 227 return EXT2_ET_DIRHASH_UNSUPP; 228 } 229 *ret_hash = hash & ~1; 230 if (ret_minor_hash) 231 *ret_minor_hash = minor_hash; 232 return 0; 233} 234