blob: 08d0508c2d3915ea6d305eb82992be1c555e1436 [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>
22#include <asm/types.h>
23#include <asm/bug.h>
24
25
26#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
51
52u32 keyed_hash(const signed char *msg, int len)
53{
54 u32 k[] = { 0x9464a485, 0x542e1a94, 0x3e846bff, 0xb75bcfc3};
55
56 u32 h0 = k[0], h1 = k[1];
57 u32 a, b, c, d;
58 u32 pad;
59 int i;
60
61 // assert(len >= 0 && len < 256);
62
63 pad = (u32)len | ((u32)len << 8);
64 pad |= pad << 16;
65
66 while(len >= 16)
67 {
68 a = (u32)msg[ 0] |
69 (u32)msg[ 1] << 8 |
70 (u32)msg[ 2] << 16|
71 (u32)msg[ 3] << 24;
72 b = (u32)msg[ 4] |
73 (u32)msg[ 5] << 8 |
74 (u32)msg[ 6] << 16|
75 (u32)msg[ 7] << 24;
76 c = (u32)msg[ 8] |
77 (u32)msg[ 9] << 8 |
78 (u32)msg[10] << 16|
79 (u32)msg[11] << 24;
80 d = (u32)msg[12] |
81 (u32)msg[13] << 8 |
82 (u32)msg[14] << 16|
83 (u32)msg[15] << 24;
84
85 TEACORE(PARTROUNDS);
86
87 len -= 16;
88 msg += 16;
89 }
90
91 if (len >= 12)
92 {
93 a = (u32)msg[ 0] |
94 (u32)msg[ 1] << 8 |
95 (u32)msg[ 2] << 16|
96 (u32)msg[ 3] << 24;
97 b = (u32)msg[ 4] |
98 (u32)msg[ 5] << 8 |
99 (u32)msg[ 6] << 16|
100 (u32)msg[ 7] << 24;
101 c = (u32)msg[ 8] |
102 (u32)msg[ 9] << 8 |
103 (u32)msg[10] << 16|
104 (u32)msg[11] << 24;
105
106 d = pad;
107 for(i = 12; i < len; i++)
108 {
109 d <<= 8;
110 d |= msg[i];
111 }
112 }
113 else if (len >= 8)
114 {
115 a = (u32)msg[ 0] |
116 (u32)msg[ 1] << 8 |
117 (u32)msg[ 2] << 16|
118 (u32)msg[ 3] << 24;
119 b = (u32)msg[ 4] |
120 (u32)msg[ 5] << 8 |
121 (u32)msg[ 6] << 16|
122 (u32)msg[ 7] << 24;
123
124 c = d = pad;
125 for(i = 8; i < len; i++)
126 {
127 c <<= 8;
128 c |= msg[i];
129 }
130 }
131 else if (len >= 4)
132 {
133 a = (u32)msg[ 0] |
134 (u32)msg[ 1] << 8 |
135 (u32)msg[ 2] << 16|
136 (u32)msg[ 3] << 24;
137
138 b = c = d = pad;
139 for(i = 4; i < len; i++)
140 {
141 b <<= 8;
142 b |= msg[i];
143 }
144 }
145 else
146 {
147 a = b = c = d = pad;
148 for(i = 0; i < len; i++)
149 {
150 a <<= 8;
151 a |= msg[i];
152 }
153 }
154
155 TEACORE(FULLROUNDS);
156
157/* return 0;*/
158 return h0^h1;
159}
160
161/* What follows in this file is copyright 2000 by Hans Reiser, and the
162 * licensing of what follows is governed by reiserfs/README */
163
164u32 yura_hash (const signed char *msg, int len)
165{
166 int j, pow;
167 u32 a, c;
168 int i;
169
170 for (pow=1,i=1; i < len; i++) pow = pow * 10;
171
172 if (len == 1)
173 a = msg[0]-48;
174 else
175 a = (msg[0] - 48) * pow;
176
177 for (i=1; i < len; i++) {
178 c = msg[i] - 48;
179 for (pow=1,j=i; j < len-1; j++) pow = pow * 10;
180 a = a + c * pow;
181 }
182
183 for (; i < 40; i++) {
184 c = '0' - 48;
185 for (pow=1,j=i; j < len-1; j++) pow = pow * 10;
186 a = a + c * pow;
187 }
188
189 for (; i < 256; i++) {
190 c = i;
191 for (pow=1,j=i; j < len-1; j++) pow = pow * 10;
192 a = a + c * pow;
193 }
194
195 a = a << 7;
196 return a;
197}
198
199u32 r5_hash (const signed char *msg, int len)
200{
201 u32 a=0;
202 while(*msg) {
203 a += *msg << 4;
204 a += *msg >> 4;
205 a *= 11;
206 msg++;
207 }
208 return a;
209}