2 * Copyright (c) 2010, 2013 Zheng Liu <lz@freebsd.org>
3 * Copyright (c) 2012, Vyacheslav Matyushin
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * The following notice applies to the code in ext2_half_md4():
33 * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
35 * License to copy and use this software is granted provided that it
36 * is identified as the "RSA Data Security, Inc. MD4 Message-Digest
37 * Algorithm" in all material mentioning or referencing this software
40 * License is also granted to make and use derivative works provided
41 * that such works are identified as "derived from the RSA Data
42 * Security, Inc. MD4 Message-Digest Algorithm" in all material
43 * mentioning or referencing the derived work.
45 * RSA Data Security, Inc. makes no representations concerning either
46 * the merchantability of this software or the suitability of this
47 * software for any particular purpose. It is provided "as is"
48 * without express or implied warranty of any kind.
50 * These notices must be retained in any copies of any part of this
51 * documentation and/or software.
54 #include <sys/param.h>
55 #include <sys/systm.h>
57 #include <sys/vnode.h>
59 #include <sys/mount.h>
61 #include <fs/ext2fs/htree.h>
62 #include <fs/ext2fs/inode.h>
63 #include <fs/ext2fs/ext2_mount.h>
64 #include <fs/ext2fs/ext2_extern.h>
66 /* F, G, and H are MD4 functions */
67 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
68 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
69 #define H(x, y, z) ((x) ^ (y) ^ (z))
71 /* ROTATE_LEFT rotates x left n bits */
72 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
75 * FF, GG, and HH are transformations for rounds 1, 2, and 3.
76 * Rotation is separated from addition to prevent recomputation.
78 #define FF(a, b, c, d, x, s) { \
79 (a) += F ((b), (c), (d)) + (x); \
80 (a) = ROTATE_LEFT ((a), (s)); \
83 #define GG(a, b, c, d, x, s) { \
84 (a) += G ((b), (c), (d)) + (x) + (uint32_t)0x5A827999; \
85 (a) = ROTATE_LEFT ((a), (s)); \
88 #define HH(a, b, c, d, x, s) { \
89 (a) += H ((b), (c), (d)) + (x) + (uint32_t)0x6ED9EBA1; \
90 (a) = ROTATE_LEFT ((a), (s)); \
94 * MD4 basic transformation. It transforms state based on block.
96 * This is a half md4 algorithm since Linux uses this algorithm for dir
97 * index. This function is derived from the RSA Data Security, Inc. MD4
98 * Message-Digest Algorithm and was modified as necessary.
100 * The return value of this function is uint32_t in Linux, but actually we don't
101 * need to check this value, so in our version this function doesn't return any
105 ext2_half_md4(uint32_t hash[4], uint32_t data[8])
107 uint32_t a = hash[0], b = hash[1], c = hash[2], d = hash[3];
110 FF(a, b, c, d, data[0], 3);
111 FF(d, a, b, c, data[1], 7);
112 FF(c, d, a, b, data[2], 11);
113 FF(b, c, d, a, data[3], 19);
114 FF(a, b, c, d, data[4], 3);
115 FF(d, a, b, c, data[5], 7);
116 FF(c, d, a, b, data[6], 11);
117 FF(b, c, d, a, data[7], 19);
120 GG(a, b, c, d, data[1], 3);
121 GG(d, a, b, c, data[3], 5);
122 GG(c, d, a, b, data[5], 9);
123 GG(b, c, d, a, data[7], 13);
124 GG(a, b, c, d, data[0], 3);
125 GG(d, a, b, c, data[2], 5);
126 GG(c, d, a, b, data[4], 9);
127 GG(b, c, d, a, data[6], 13);
130 HH(a, b, c, d, data[3], 3);
131 HH(d, a, b, c, data[7], 9);
132 HH(c, d, a, b, data[2], 11);
133 HH(b, c, d, a, data[6], 15);
134 HH(a, b, c, d, data[1], 3);
135 HH(d, a, b, c, data[5], 9);
136 HH(c, d, a, b, data[0], 11);
137 HH(b, c, d, a, data[4], 15);
146 * Tiny Encryption Algorithm.
149 ext2_tea(uint32_t hash[4], uint32_t data[8])
151 uint32_t tea_delta = 0x9E3779B9;
153 uint32_t x = hash[0], y = hash[1];
159 x += ((y << 4) + data[0]) ^ (y + sum) ^ ((y >> 5) + data[1]);
160 y += ((x << 4) + data[2]) ^ (x + sum) ^ ((x >> 5) + data[3]);
169 ext2_legacy_hash(const char *name, int len, int unsigned_char)
171 uint32_t h0, h1 = 0x12A3FE2D, h2 = 0x37ABE8F9;
172 uint32_t multi = 0x6D22F5;
173 const unsigned char *uname = (const unsigned char *)name;
174 const signed char *sname = (const signed char *)name;
177 for (i = 0; i < len; i++) {
179 val = (u_int)*uname++;
183 h0 = h2 + (h1 ^ (val * multi));
194 ext2_prep_hashbuf(const char *src, int slen, uint32_t *dst, int dlen,
197 uint32_t padding = slen | (slen << 8) | (slen << 16) | (slen << 24);
199 const unsigned char *ubuf = (const unsigned char *)src;
200 const signed char *sbuf = (const signed char *)src;
211 for (i = 0; i < len; i++) {
213 buf_byte = (u_int)ubuf[i];
215 buf_byte = (int)sbuf[i];
225 dlen -= sizeof(uint32_t);
230 dlen -= sizeof(uint32_t);
234 dlen -= sizeof(uint32_t);
237 dlen -= sizeof(uint32_t);
242 ext2_htree_hash(const char *name, int len,
243 uint32_t *hash_seed, int hash_version,
244 uint32_t *hash_major, uint32_t *hash_minor)
248 uint32_t major = 0, minor = 0;
249 int unsigned_char = 0;
251 if (!name || !hash_major)
254 if (len < 1 || len > 255)
257 hash[0] = 0x67452301;
258 hash[1] = 0xEFCDAB89;
259 hash[2] = 0x98BADCFE;
260 hash[3] = 0x10325476;
263 memcpy(hash, hash_seed, sizeof(hash));
265 switch (hash_version) {
266 case EXT2_HTREE_TEA_UNSIGNED:
271 ext2_prep_hashbuf(name, len, data, 16, unsigned_char);
272 ext2_tea(hash, data);
279 case EXT2_HTREE_LEGACY_UNSIGNED:
282 case EXT2_HTREE_LEGACY:
283 major = ext2_legacy_hash(name, len, unsigned_char);
285 case EXT2_HTREE_HALF_MD4_UNSIGNED:
288 case EXT2_HTREE_HALF_MD4:
290 ext2_prep_hashbuf(name, len, data, 32, unsigned_char);
291 ext2_half_md4(hash, data);
303 if (major == (EXT2_HTREE_EOF << 1))
304 major = (EXT2_HTREE_EOF - 1) << 1;