Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | |
| 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 Bunk | 0fb2dae | 2005-11-08 16:49:52 +0100 | [diff] [blame^] | 22 | #include <linux/reiserfs_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/types.h> |
| 24 | #include <asm/bug.h> |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | u32 keyed_hash(const signed char *msg, int len) |
| 52 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 53 | u32 k[] = { 0x9464a485, 0x542e1a94, 0x3e846bff, 0xb75bcfc3 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | u32 h0 = k[0], h1 = k[1]; |
| 56 | u32 a, b, c, d; |
| 57 | u32 pad; |
| 58 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 60 | // assert(len >= 0 && len < 256); |
| 61 | |
| 62 | pad = (u32) len | ((u32) len << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | pad |= pad << 16; |
| 64 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 65 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | TEACORE(PARTROUNDS); |
| 78 | |
| 79 | len -= 16; |
| 80 | msg += 16; |
| 81 | } |
| 82 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 83 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | d = pad; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 93 | for (i = 12; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | d <<= 8; |
| 95 | d |= msg[i]; |
| 96 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 97 | } 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | c = d = pad; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 104 | for (i = 8; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | c <<= 8; |
| 106 | c |= msg[i]; |
| 107 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 108 | } else if (len >= 4) { |
| 109 | a = (u32) msg[0] | |
| 110 | (u32) msg[1] << 8 | (u32) msg[2] << 16 | (u32) msg[3] << 24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | b = c = d = pad; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 113 | for (i = 4; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | b <<= 8; |
| 115 | b |= msg[i]; |
| 116 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 117 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | a = b = c = d = pad; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 119 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | a <<= 8; |
| 121 | a |= msg[i]; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | TEACORE(FULLROUNDS); |
| 126 | |
| 127 | /* return 0;*/ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 128 | return h0 ^ h1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 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 Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 134 | u32 yura_hash(const signed char *msg, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 136 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 173 | u32 r5_hash(const signed char *msg, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 175 | u32 a = 0; |
| 176 | while (*msg) { |
| 177 | a += *msg << 4; |
| 178 | a += *msg >> 4; |
| 179 | a *= 11; |
| 180 | msg++; |
| 181 | } |
| 182 | return a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |