blob: a3ec238fd9e0347bef211170353137fa5250f949 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/*
3 * Keyed 32-bit hash function using TEA in a Davis-Meyer function
4 * H0 = Key
5 * Hi = E Mi(Hi-1) + Hi-1
6 *
7 * (see Applied Cryptography, 2nd edition, p448).
8 *
9 * Jeremy Fitzhardinge <jeremy@zip.com.au> 1998
10 *
11 * Jeremy has agreed to the contents of reiserfs/README. -Hans
12 * Yura's function is added (04/07/2000)
13 */
14
15//
16// keyed_hash
17// yura_hash
18// r5_hash
19//
20
21#include <linux/kernel.h>
Adrian Bunk0fb2dae2005-11-08 16:49:52 +010022#include <linux/reiserfs_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/types.h>
24#include <asm/bug.h>
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define DELTA 0x9E3779B9
27#define FULLROUNDS 10 /* 32 is overkill, 16 is strong crypto */
28#define PARTROUNDS 6 /* 6 gets complete mixing */
29
30/* a, b, c, d - data; h0, h1 - accumulated hash */
31#define TEACORE(rounds) \
32 do { \
33 u32 sum = 0; \
34 int n = rounds; \
35 u32 b0, b1; \
36 \
37 b0 = h0; \
38 b1 = h1; \
39 \
40 do \
41 { \
42 sum += DELTA; \
43 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); \
44 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); \
45 } while(--n); \
46 \
47 h0 += b0; \
48 h1 += b1; \
49 } while(0)
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051u32 keyed_hash(const signed char *msg, int len)
52{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070053 u32 k[] = { 0x9464a485, 0x542e1a94, 0x3e846bff, 0xb75bcfc3 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 u32 h0 = k[0], h1 = k[1];
56 u32 a, b, c, d;
57 u32 pad;
58 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070060 // assert(len >= 0 && len < 256);
61
62 pad = (u32) len | ((u32) len << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 pad |= pad << 16;
64
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070065 while (len >= 16) {
66 a = (u32) msg[0] |
67 (u32) msg[1] << 8 | (u32) msg[2] << 16 | (u32) msg[3] << 24;
68 b = (u32) msg[4] |
69 (u32) msg[5] << 8 | (u32) msg[6] << 16 | (u32) msg[7] << 24;
70 c = (u32) msg[8] |
71 (u32) msg[9] << 8 |
72 (u32) msg[10] << 16 | (u32) msg[11] << 24;
73 d = (u32) msg[12] |
74 (u32) msg[13] << 8 |
75 (u32) msg[14] << 16 | (u32) msg[15] << 24;
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 TEACORE(PARTROUNDS);
78
79 len -= 16;
80 msg += 16;
81 }
82
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070083 if (len >= 12) {
84 a = (u32) msg[0] |
85 (u32) msg[1] << 8 | (u32) msg[2] << 16 | (u32) msg[3] << 24;
86 b = (u32) msg[4] |
87 (u32) msg[5] << 8 | (u32) msg[6] << 16 | (u32) msg[7] << 24;
88 c = (u32) msg[8] |
89 (u32) msg[9] << 8 |
90 (u32) msg[10] << 16 | (u32) msg[11] << 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 d = pad;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070093 for (i = 12; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 d <<= 8;
95 d |= msg[i];
96 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070097 } else if (len >= 8) {
98 a = (u32) msg[0] |
99 (u32) msg[1] << 8 | (u32) msg[2] << 16 | (u32) msg[3] << 24;
100 b = (u32) msg[4] |
101 (u32) msg[5] << 8 | (u32) msg[6] << 16 | (u32) msg[7] << 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 c = d = pad;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700104 for (i = 8; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 c <<= 8;
106 c |= msg[i];
107 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700108 } else if (len >= 4) {
109 a = (u32) msg[0] |
110 (u32) msg[1] << 8 | (u32) msg[2] << 16 | (u32) msg[3] << 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 b = c = d = pad;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700113 for (i = 4; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 b <<= 8;
115 b |= msg[i];
116 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700117 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 a = b = c = d = pad;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700119 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 a <<= 8;
121 a |= msg[i];
122 }
123 }
124
125 TEACORE(FULLROUNDS);
126
127/* return 0;*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700128 return h0 ^ h1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131/* What follows in this file is copyright 2000 by Hans Reiser, and the
132 * licensing of what follows is governed by reiserfs/README */
133
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700134u32 yura_hash(const signed char *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700136 int j, pow;
137 u32 a, c;
138 int i;
139
140 for (pow = 1, i = 1; i < len; i++)
141 pow = pow * 10;
142
143 if (len == 1)
144 a = msg[0] - 48;
145 else
146 a = (msg[0] - 48) * pow;
147
148 for (i = 1; i < len; i++) {
149 c = msg[i] - 48;
150 for (pow = 1, j = i; j < len - 1; j++)
151 pow = pow * 10;
152 a = a + c * pow;
153 }
154
155 for (; i < 40; i++) {
156 c = '0' - 48;
157 for (pow = 1, j = i; j < len - 1; j++)
158 pow = pow * 10;
159 a = a + c * pow;
160 }
161
162 for (; i < 256; i++) {
163 c = i;
164 for (pow = 1, j = i; j < len - 1; j++)
165 pow = pow * 10;
166 a = a + c * pow;
167 }
168
169 a = a << 7;
170 return a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700173u32 r5_hash(const signed char *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700175 u32 a = 0;
176 while (*msg) {
177 a += *msg << 4;
178 a += *msg >> 4;
179 a *= 11;
180 msg++;
181 }
182 return a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}