~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/common/util/md5_hash.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
 
 
17
 
 
18
#include "md5_hash.hpp"
 
19
 
 
20
#ifdef WORDS_BIGENDIAN
 
21
#define HIGHFIRST 1
 
22
#endif
 
23
 
 
24
/*
 
25
 * This code implements the MD5 message-digest algorithm.
 
26
 * The algorithm is due to Ron Rivest.  This code was
 
27
 * written by Colin Plumb in 1993, no copyright is claimed.
 
28
 * This code is in the public domain; do with it what you wish.
 
29
 *
 
30
 * Equivalent code is available from RSA Data Security, Inc.
 
31
 * This code has been tested against that, and is equivalent,
 
32
 * except that you don't need to include two pages of legalese
 
33
 * with every copy.
 
34
 *
 
35
 * The code has been modified by Mikael Ronstroem to handle
 
36
 * calculating a hash value of a key that is always a multiple
 
37
 * of 4 bytes long. Word 0 of the calculated 4-word hash value
 
38
 * is returned as the hash value.
 
39
 */
 
40
 
 
41
#ifndef HIGHFIRST
 
42
#define byteReverse(buf, len)   /* Nothing */
 
43
#else
 
44
void byteReverse(unsigned char *buf, unsigned longs);
 
45
/*
 
46
 * Note: this code is harmless on little-endian machines.
 
47
 */
 
48
void byteReverse(unsigned char *buf, unsigned longs)
 
49
{
 
50
    Uint32 t;
 
51
    do {
 
52
        t = (Uint32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
 
53
            ((unsigned) buf[1] << 8 | buf[0]);
 
54
        *(Uint32 *) buf = t;
 
55
        buf += 4;
 
56
    } while (--longs);
 
57
}
 
58
#endif
 
59
 
 
60
/* The four core functions - F1 is optimized somewhat */
 
61
 
 
62
/* #define F1(x, y, z) (x & y | ~x & z) */
 
63
#define F1(x, y, z) (z ^ (x & (y ^ z)))
 
64
#define F2(x, y, z) F1(z, x, y)
 
65
#define F3(x, y, z) (x ^ y ^ z)
 
66
#define F4(x, y, z) (y ^ (x | ~z))
 
67
 
 
68
/* This is the central step in the MD5 algorithm. */
 
69
#define MD5STEP(f, w, x, y, z, data, s) \
 
70
        ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
 
71
 
 
72
/*
 
73
 * The core of the MD5 algorithm, this alters an existing MD5 hash to
 
74
 * reflect the addition of 16 longwords of new data.  MD5Update blocks
 
75
 * the data and converts bytes into longwords for this routine.
 
76
 */
 
77
static void MD5Transform(Uint32 buf[4], Uint32 const in[16])
 
78
{
 
79
    register Uint32 a, b, c, d;
 
80
 
 
81
    a = buf[0];
 
82
    b = buf[1];
 
83
    c = buf[2];
 
84
    d = buf[3];
 
85
 
 
86
    MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
 
87
    MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
 
88
    MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
 
89
    MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
 
90
    MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
 
91
    MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
 
92
    MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
 
93
    MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
 
94
    MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
 
95
    MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
 
96
    MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
 
97
    MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
 
98
    MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
 
99
    MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
 
100
    MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
 
101
    MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
 
102
 
 
103
    MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
 
104
    MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
 
105
    MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
 
106
    MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
 
107
    MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
 
108
    MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
 
109
    MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
 
110
    MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
 
111
    MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
 
112
    MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
 
113
    MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
 
114
    MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
 
115
    MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
 
116
    MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
 
117
    MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
 
118
    MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
 
119
 
 
120
    MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
 
121
    MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
 
122
    MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
 
123
    MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
 
124
    MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
 
125
    MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
 
126
    MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
 
127
    MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
 
128
    MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
 
129
    MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
 
130
    MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
 
131
    MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
 
132
    MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
 
133
    MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
 
134
    MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
 
135
    MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
 
136
 
 
137
    MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
 
138
    MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
 
139
    MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
 
140
    MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
 
141
    MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
 
142
    MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
 
143
    MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
 
144
    MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
 
145
    MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
 
146
    MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
 
147
    MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
 
148
    MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
 
149
    MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
 
150
    MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
 
151
    MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
 
152
    MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
 
153
 
 
154
    buf[0] += a;
 
155
    buf[1] += b;
 
156
    buf[2] += c;
 
157
    buf[3] += d;
 
158
}
 
159
 
 
160
/*
 
161
 * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
 
162
 * initialization constants.
 
163
 */
 
164
void md5_hash(Uint32 result[4], const Uint64* keybuf, Uint32 no_of_32_words)
 
165
{
 
166
  /**
 
167
   * This is the external interface of the module
 
168
   * It is assumed that keybuf is placed on 8 byte
 
169
   * alignment.
 
170
   */
 
171
  Uint32 i;
 
172
  Uint32 buf[4];
 
173
  Uint64 transform64_buf[8];
 
174
  Uint32* transform32_buf;
 
175
  Uint32 len = no_of_32_words << 2;
 
176
  const Uint64* key64buf = (const Uint64*)keybuf;
 
177
  const Uint32* key32buf = (const Uint32*)keybuf;
 
178
 
 
179
  transform32_buf = (Uint32*)&transform64_buf[0];
 
180
  buf[0] = 0x67452301;
 
181
  buf[1] = 0xefcdab89;
 
182
  buf[2] = 0x98badcfe;
 
183
  buf[3] = 0x10325476;
 
184
 
 
185
  while (no_of_32_words >= 16) {
 
186
    transform64_buf[0] = key64buf[0];
 
187
    transform64_buf[1] = key64buf[1];
 
188
    transform64_buf[2] = key64buf[2];
 
189
    transform64_buf[3] = key64buf[3];
 
190
    transform64_buf[4] = key64buf[4];
 
191
    transform64_buf[5] = key64buf[5];
 
192
    transform64_buf[6] = key64buf[6];
 
193
    transform64_buf[7] = key64buf[7];
 
194
    no_of_32_words -= 16;
 
195
    key64buf += 8;
 
196
    byteReverse((unsigned char *)transform32_buf, 16);
 
197
    MD5Transform(buf, transform32_buf);
 
198
  }
 
199
 
 
200
  key32buf = (const Uint32*)key64buf;
 
201
  transform64_buf[0] = 0;
 
202
  transform64_buf[1] = 0;
 
203
  transform64_buf[2] = 0;
 
204
  transform64_buf[3] = 0;
 
205
  transform64_buf[4] = 0;
 
206
  transform64_buf[5] = 0;
 
207
  transform64_buf[6] = 0;
 
208
  transform64_buf[7] = (Uint64)len;
 
209
 
 
210
  for (i = 0; i < no_of_32_words; i++)
 
211
    transform32_buf[i] = key32buf[i];
 
212
  transform32_buf[no_of_32_words] = 0x80000000;
 
213
 
 
214
  if (no_of_32_words < 14) {
 
215
    byteReverse((unsigned char *)transform32_buf, 16);
 
216
    MD5Transform(buf, transform32_buf);
 
217
  } else {
 
218
    if (no_of_32_words == 14)
 
219
      transform32_buf[15] = 0;
 
220
    MD5Transform(buf, transform32_buf);
 
221
    transform64_buf[0] = 0;
 
222
    transform64_buf[1] = 0;
 
223
    transform64_buf[2] = 0;
 
224
    transform64_buf[3] = 0;
 
225
    transform64_buf[4] = 0;
 
226
    transform64_buf[5] = 0;
 
227
    transform64_buf[6] = 0;
 
228
    transform64_buf[7] = (Uint64)len;
 
229
    byteReverse((unsigned char *)transform32_buf, 16);
 
230
    MD5Transform(buf, transform32_buf);    
 
231
  }
 
232
 
 
233
  result[0] = buf[0];
 
234
  result[1] = buf[1];
 
235
  result[2] = buf[2];
 
236
  result[3] = buf[3];
 
237
}
 
238