xref: /freebsd/sys/fs/ext2fs/ext2_hash.c (revision 97cb52fa9aefd90fad38790fded50905aeeb9b9e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/htree.h>
64 #include <fs/ext2fs/inode.h>
65 #include <fs/ext2fs/ext2_mount.h>
66 #include <fs/ext2fs/ext2_extern.h>
67 
68 /* F, G, and H are MD4 functions */
69 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
70 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
71 #define H(x, y, z) ((x) ^ (y) ^ (z))
72 
73 /* ROTATE_LEFT rotates x left n bits */
74 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
75 
76 /*
77  * FF, GG, and HH are transformations for rounds 1, 2, and 3.
78  * Rotation is separated from addition to prevent recomputation.
79  */
80 #define FF(a, b, c, d, x, s) { \
81 	(a) += F ((b), (c), (d)) + (x); \
82 	(a) = ROTATE_LEFT ((a), (s)); \
83 }
84 
85 #define GG(a, b, c, d, x, s) { \
86 	(a) += G ((b), (c), (d)) + (x) + (uint32_t)0x5A827999; \
87 	(a) = ROTATE_LEFT ((a), (s)); \
88 }
89 
90 #define HH(a, b, c, d, x, s) { \
91 	(a) += H ((b), (c), (d)) + (x) + (uint32_t)0x6ED9EBA1; \
92 	(a) = ROTATE_LEFT ((a), (s)); \
93 }
94 
95 /*
96  * MD4 basic transformation.  It transforms state based on block.
97  *
98  * This is a half md4 algorithm since Linux uses this algorithm for dir
99  * index.  This function is derived from the RSA Data Security, Inc. MD4
100  * Message-Digest Algorithm and was modified as necessary.
101  *
102  * The return value of this function is uint32_t in Linux, but actually we don't
103  * need to check this value, so in our version this function doesn't return any
104  * value.
105  */
106 static void
107 ext2_half_md4(uint32_t hash[4], uint32_t data[8])
108 {
109 	uint32_t a = hash[0], b = hash[1], c = hash[2], d = hash[3];
110 
111 	/* Round 1 */
112 	FF(a, b, c, d, data[0],  3);
113 	FF(d, a, b, c, data[1],  7);
114 	FF(c, d, a, b, data[2], 11);
115 	FF(b, c, d, a, data[3], 19);
116 	FF(a, b, c, d, data[4],  3);
117 	FF(d, a, b, c, data[5],  7);
118 	FF(c, d, a, b, data[6], 11);
119 	FF(b, c, d, a, data[7], 19);
120 
121 	/* Round 2 */
122 	GG(a, b, c, d, data[1],  3);
123 	GG(d, a, b, c, data[3],  5);
124 	GG(c, d, a, b, data[5],  9);
125 	GG(b, c, d, a, data[7], 13);
126 	GG(a, b, c, d, data[0],  3);
127 	GG(d, a, b, c, data[2],  5);
128 	GG(c, d, a, b, data[4],  9);
129 	GG(b, c, d, a, data[6], 13);
130 
131 	/* Round 3 */
132 	HH(a, b, c, d, data[3],  3);
133 	HH(d, a, b, c, data[7],  9);
134 	HH(c, d, a, b, data[2], 11);
135 	HH(b, c, d, a, data[6], 15);
136 	HH(a, b, c, d, data[1],  3);
137 	HH(d, a, b, c, data[5],  9);
138 	HH(c, d, a, b, data[0], 11);
139 	HH(b, c, d, a, data[4], 15);
140 
141 	hash[0] += a;
142 	hash[1] += b;
143 	hash[2] += c;
144 	hash[3] += d;
145 }
146 
147 /*
148  * Tiny Encryption Algorithm.
149  */
150 static void
151 ext2_tea(uint32_t hash[4], uint32_t data[8])
152 {
153 	uint32_t tea_delta = 0x9E3779B9;
154 	uint32_t sum;
155 	uint32_t x = hash[0], y = hash[1];
156 	int n = 16;
157 	int i = 1;
158 
159 	while (n-- > 0) {
160 		sum = i * tea_delta;
161 		x += ((y << 4) + data[0]) ^ (y + sum) ^ ((y >> 5) + data[1]);
162 		y += ((x << 4) + data[2]) ^ (x + sum) ^ ((x >> 5) + data[3]);
163 		i++;
164 	}
165 
166 	hash[0] += x;
167 	hash[1] += y;
168 }
169 
170 static uint32_t
171 ext2_legacy_hash(const char *name, int len, int unsigned_char)
172 {
173 	uint32_t h0, h1 = 0x12A3FE2D, h2 = 0x37ABE8F9;
174 	uint32_t multi = 0x6D22F5;
175 	const unsigned char *uname = (const unsigned char *)name;
176 	const signed char *sname = (const signed char *)name;
177 	int val, i;
178 
179 	for (i = 0; i < len; i++) {
180 		if (unsigned_char)
181 			val = (u_int)*uname++;
182 		else
183 			val = (int)*sname++;
184 
185 		h0 = h2 + (h1 ^ (val * multi));
186 		if (h0 & 0x80000000)
187 			h0 -= 0x7FFFFFFF;
188 		h2 = h1;
189 		h1 = h0;
190 	}
191 
192 	return (h1 << 1);
193 }
194 
195 static void
196 ext2_prep_hashbuf(const char *src, int slen, uint32_t *dst, int dlen,
197     int unsigned_char)
198 {
199 	uint32_t padding = slen | (slen << 8) | (slen << 16) | (slen << 24);
200 	uint32_t buf_val;
201 	const unsigned char *ubuf = (const unsigned char *)src;
202 	const signed char *sbuf = (const signed char *)src;
203 	int len, i;
204 	int buf_byte;
205 
206 	if (slen > dlen)
207 		len = dlen;
208 	else
209 		len = slen;
210 
211 	buf_val = padding;
212 
213 	for (i = 0; i < len; i++) {
214 		if (unsigned_char)
215 			buf_byte = (u_int)ubuf[i];
216 		else
217 			buf_byte = (int)sbuf[i];
218 
219 		if ((i % 4) == 0)
220 			buf_val = padding;
221 
222 		buf_val <<= 8;
223 		buf_val += buf_byte;
224 
225 		if ((i % 4) == 3) {
226 			*dst++ = buf_val;
227 			dlen -= sizeof(uint32_t);
228 			buf_val = padding;
229 		}
230 	}
231 
232 	dlen -= sizeof(uint32_t);
233 	if (dlen >= 0)
234 		*dst++ = buf_val;
235 
236 	dlen -= sizeof(uint32_t);
237 	while (dlen >= 0) {
238 		*dst++ = padding;
239 		dlen -= sizeof(uint32_t);
240 	}
241 }
242 
243 int
244 ext2_htree_hash(const char *name, int len,
245     uint32_t *hash_seed, int hash_version,
246     uint32_t *hash_major, uint32_t *hash_minor)
247 {
248 	uint32_t hash[4];
249 	uint32_t data[8];
250 	uint32_t major = 0, minor = 0;
251 	int unsigned_char = 0;
252 
253 	if (!name || !hash_major)
254 		return (-1);
255 
256 	if (len < 1 || len > 255)
257 		goto error;
258 
259 	hash[0] = 0x67452301;
260 	hash[1] = 0xEFCDAB89;
261 	hash[2] = 0x98BADCFE;
262 	hash[3] = 0x10325476;
263 
264 	if (hash_seed)
265 		memcpy(hash, hash_seed, sizeof(hash));
266 
267 	switch (hash_version) {
268 	case EXT2_HTREE_TEA_UNSIGNED:
269 		unsigned_char = 1;
270 		/* FALLTHROUGH */
271 	case EXT2_HTREE_TEA:
272 		while (len > 0) {
273 			ext2_prep_hashbuf(name, len, data, 16, unsigned_char);
274 			ext2_tea(hash, data);
275 			len -= 16;
276 			name += 16;
277 		}
278 		major = hash[0];
279 		minor = hash[1];
280 		break;
281 	case EXT2_HTREE_LEGACY_UNSIGNED:
282 		unsigned_char = 1;
283 		/* FALLTHROUGH */
284 	case EXT2_HTREE_LEGACY:
285 		major = ext2_legacy_hash(name, len, unsigned_char);
286 		break;
287 	case EXT2_HTREE_HALF_MD4_UNSIGNED:
288 		unsigned_char = 1;
289 		/* FALLTHROUGH */
290 	case EXT2_HTREE_HALF_MD4:
291 		while (len > 0) {
292 			ext2_prep_hashbuf(name, len, data, 32, unsigned_char);
293 			ext2_half_md4(hash, data);
294 			len -= 32;
295 			name += 32;
296 		}
297 		major = hash[1];
298 		minor = hash[2];
299 		break;
300 	default:
301 		goto error;
302 	}
303 
304 	major &= ~1;
305 	if (major == (EXT2_HTREE_EOF << 1))
306 		major = (EXT2_HTREE_EOF - 1) << 1;
307 	*hash_major = major;
308 	if (hash_minor)
309 		*hash_minor = minor;
310 
311 	return (0);
312 
313 error:
314 	*hash_major = 0;
315 	if (hash_minor)
316 		*hash_minor = 0;
317 	return (-1);
318 }
319