1 /*- 2 * SPDX-License-Identifier: (BSD-2-Clause-FreeBSD AND RSA-MD) 3 * 4 * Copyright (c) 2010, 2013 Zheng Liu <lz@freebsd.org> 5 * Copyright (c) 2012, Vyacheslav Matyushin 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 /* 33 * The following notice applies to the code in ext2_half_md4(): 34 * 35 * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved. 36 * 37 * License to copy and use this software is granted provided that it 38 * is identified as the "RSA Data Security, Inc. MD4 Message-Digest 39 * Algorithm" in all material mentioning or referencing this software 40 * or this function. 41 * 42 * License is also granted to make and use derivative works provided 43 * that such works are identified as "derived from the RSA Data 44 * Security, Inc. MD4 Message-Digest Algorithm" in all material 45 * mentioning or referencing the derived work. 46 * 47 * RSA Data Security, Inc. makes no representations concerning either 48 * the merchantability of this software or the suitability of this 49 * software for any particular purpose. It is provided "as is" 50 * without express or implied warranty of any kind. 51 * 52 * These notices must be retained in any copies of any part of this 53 * documentation and/or software. 54 */ 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/conf.h> 59 #include <sys/vnode.h> 60 #include <sys/stat.h> 61 #include <sys/mount.h> 62 63 #include <fs/ext2fs/ext2fs.h> 64 #include <fs/ext2fs/htree.h> 65 #include <fs/ext2fs/inode.h> 66 #include <fs/ext2fs/ext2_mount.h> 67 #include <fs/ext2fs/ext2_extern.h> 68 69 /* F, G, and H are MD4 functions */ 70 #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) 71 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) 72 #define H(x, y, z) ((x) ^ (y) ^ (z)) 73 74 /* ROTATE_LEFT rotates x left n bits */ 75 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) 76 77 /* 78 * FF, GG, and HH are transformations for rounds 1, 2, and 3. 79 * Rotation is separated from addition to prevent recomputation. 80 */ 81 #define FF(a, b, c, d, x, s) { \ 82 (a) += F ((b), (c), (d)) + (x); \ 83 (a) = ROTATE_LEFT ((a), (s)); \ 84 } 85 86 #define GG(a, b, c, d, x, s) { \ 87 (a) += G ((b), (c), (d)) + (x) + (uint32_t)0x5A827999; \ 88 (a) = ROTATE_LEFT ((a), (s)); \ 89 } 90 91 #define HH(a, b, c, d, x, s) { \ 92 (a) += H ((b), (c), (d)) + (x) + (uint32_t)0x6ED9EBA1; \ 93 (a) = ROTATE_LEFT ((a), (s)); \ 94 } 95 96 /* 97 * MD4 basic transformation. It transforms state based on block. 98 * 99 * This is a half md4 algorithm since Linux uses this algorithm for dir 100 * index. This function is derived from the RSA Data Security, Inc. MD4 101 * Message-Digest Algorithm and was modified as necessary. 102 * 103 * The return value of this function is uint32_t in Linux, but actually we don't 104 * need to check this value, so in our version this function doesn't return any 105 * value. 106 */ 107 static void 108 ext2_half_md4(uint32_t hash[4], uint32_t data[8]) 109 { 110 uint32_t a = hash[0], b = hash[1], c = hash[2], d = hash[3]; 111 112 /* Round 1 */ 113 FF(a, b, c, d, data[0], 3); 114 FF(d, a, b, c, data[1], 7); 115 FF(c, d, a, b, data[2], 11); 116 FF(b, c, d, a, data[3], 19); 117 FF(a, b, c, d, data[4], 3); 118 FF(d, a, b, c, data[5], 7); 119 FF(c, d, a, b, data[6], 11); 120 FF(b, c, d, a, data[7], 19); 121 122 /* Round 2 */ 123 GG(a, b, c, d, data[1], 3); 124 GG(d, a, b, c, data[3], 5); 125 GG(c, d, a, b, data[5], 9); 126 GG(b, c, d, a, data[7], 13); 127 GG(a, b, c, d, data[0], 3); 128 GG(d, a, b, c, data[2], 5); 129 GG(c, d, a, b, data[4], 9); 130 GG(b, c, d, a, data[6], 13); 131 132 /* Round 3 */ 133 HH(a, b, c, d, data[3], 3); 134 HH(d, a, b, c, data[7], 9); 135 HH(c, d, a, b, data[2], 11); 136 HH(b, c, d, a, data[6], 15); 137 HH(a, b, c, d, data[1], 3); 138 HH(d, a, b, c, data[5], 9); 139 HH(c, d, a, b, data[0], 11); 140 HH(b, c, d, a, data[4], 15); 141 142 hash[0] += a; 143 hash[1] += b; 144 hash[2] += c; 145 hash[3] += d; 146 } 147 148 /* 149 * Tiny Encryption Algorithm. 150 */ 151 static void 152 ext2_tea(uint32_t hash[4], uint32_t data[8]) 153 { 154 uint32_t tea_delta = 0x9E3779B9; 155 uint32_t sum; 156 uint32_t x = hash[0], y = hash[1]; 157 int n = 16; 158 int i = 1; 159 160 while (n-- > 0) { 161 sum = i * tea_delta; 162 x += ((y << 4) + data[0]) ^ (y + sum) ^ ((y >> 5) + data[1]); 163 y += ((x << 4) + data[2]) ^ (x + sum) ^ ((x >> 5) + data[3]); 164 i++; 165 } 166 167 hash[0] += x; 168 hash[1] += y; 169 } 170 171 static uint32_t 172 ext2_legacy_hash(const char *name, int len, int unsigned_char) 173 { 174 uint32_t h0, h1 = 0x12A3FE2D, h2 = 0x37ABE8F9; 175 uint32_t multi = 0x6D22F5; 176 const unsigned char *uname = (const unsigned char *)name; 177 const signed char *sname = (const signed char *)name; 178 int val, i; 179 180 for (i = 0; i < len; i++) { 181 if (unsigned_char) 182 val = (u_int)*uname++; 183 else 184 val = (int)*sname++; 185 186 h0 = h2 + (h1 ^ (val * multi)); 187 if (h0 & 0x80000000) 188 h0 -= 0x7FFFFFFF; 189 h2 = h1; 190 h1 = h0; 191 } 192 193 return (h1 << 1); 194 } 195 196 static void 197 ext2_prep_hashbuf(const char *src, int slen, uint32_t *dst, int dlen, 198 int unsigned_char) 199 { 200 uint32_t padding = slen | (slen << 8) | (slen << 16) | (slen << 24); 201 uint32_t buf_val; 202 const unsigned char *ubuf = (const unsigned char *)src; 203 const signed char *sbuf = (const signed char *)src; 204 int len, i; 205 int buf_byte; 206 207 if (slen > dlen) 208 len = dlen; 209 else 210 len = slen; 211 212 buf_val = padding; 213 214 for (i = 0; i < len; i++) { 215 if (unsigned_char) 216 buf_byte = (u_int)ubuf[i]; 217 else 218 buf_byte = (int)sbuf[i]; 219 220 if ((i % 4) == 0) 221 buf_val = padding; 222 223 buf_val <<= 8; 224 buf_val += buf_byte; 225 226 if ((i % 4) == 3) { 227 *dst++ = buf_val; 228 dlen -= sizeof(uint32_t); 229 buf_val = padding; 230 } 231 } 232 233 dlen -= sizeof(uint32_t); 234 if (dlen >= 0) 235 *dst++ = buf_val; 236 237 dlen -= sizeof(uint32_t); 238 while (dlen >= 0) { 239 *dst++ = padding; 240 dlen -= sizeof(uint32_t); 241 } 242 } 243 244 int 245 ext2_htree_hash(const char *name, int len, 246 uint32_t *hash_seed, int hash_version, 247 uint32_t *hash_major, uint32_t *hash_minor) 248 { 249 uint32_t hash[4]; 250 uint32_t data[8]; 251 uint32_t major = 0, minor = 0; 252 int unsigned_char = 0; 253 254 if (!name || !hash_major) 255 return (-1); 256 257 if (len < 1 || len > 255) 258 goto error; 259 260 hash[0] = 0x67452301; 261 hash[1] = 0xEFCDAB89; 262 hash[2] = 0x98BADCFE; 263 hash[3] = 0x10325476; 264 265 if (hash_seed) 266 memcpy(hash, hash_seed, sizeof(hash)); 267 268 switch (hash_version) { 269 case EXT2_HTREE_TEA_UNSIGNED: 270 unsigned_char = 1; 271 /* FALLTHROUGH */ 272 case EXT2_HTREE_TEA: 273 while (len > 0) { 274 ext2_prep_hashbuf(name, len, data, 16, unsigned_char); 275 ext2_tea(hash, data); 276 len -= 16; 277 name += 16; 278 } 279 major = hash[0]; 280 minor = hash[1]; 281 break; 282 case EXT2_HTREE_LEGACY_UNSIGNED: 283 unsigned_char = 1; 284 /* FALLTHROUGH */ 285 case EXT2_HTREE_LEGACY: 286 major = ext2_legacy_hash(name, len, unsigned_char); 287 break; 288 case EXT2_HTREE_HALF_MD4_UNSIGNED: 289 unsigned_char = 1; 290 /* FALLTHROUGH */ 291 case EXT2_HTREE_HALF_MD4: 292 while (len > 0) { 293 ext2_prep_hashbuf(name, len, data, 32, unsigned_char); 294 ext2_half_md4(hash, data); 295 len -= 32; 296 name += 32; 297 } 298 major = hash[1]; 299 minor = hash[2]; 300 break; 301 default: 302 goto error; 303 } 304 305 major &= ~1; 306 if (major == (EXT2_HTREE_EOF << 1)) 307 major = (EXT2_HTREE_EOF - 1) << 1; 308 *hash_major = major; 309 if (hash_minor) 310 *hash_minor = minor; 311 312 return (0); 313 314 error: 315 *hash_major = 0; 316 if (hash_minor) 317 *hash_minor = 0; 318 return (-1); 319 } 320