2 * Amiga Generic Set - set of libraries and includes to ease sw development for all Amiga platforms
3 * Copyright (C) 2001-2011 Tomasz Wiszkowski Tomasz.Wiszkowski at gmail.com.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <LibC/LibC.h>
23 using namespace GenNS
;
42 static unsigned char PADDING
[64] = {
43 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
48 /* F, G, H and I are basic MD5 functions.
50 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
51 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
52 #define H(x, y, z) ((x) ^ (y) ^ (z))
53 #define I(x, y, z) ((y) ^ ((x) | (~z)))
55 /* ROTATE_LEFT rotates x left n bits.
57 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
59 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
60 Rotation is separate from addition to prevent recomputation.
62 #define FF(a, b, c, d, x, s, ac) { \
63 (a) += F ((b), (c), (d)) + (x) + (unsigned int)(ac); \
64 (a) = ROTATE_LEFT ((a), (s)); \
67 #define GG(a, b, c, d, x, s, ac) { \
68 (a) += G ((b), (c), (d)) + (x) + (unsigned int)(ac); \
69 (a) = ROTATE_LEFT ((a), (s)); \
72 #define HH(a, b, c, d, x, s, ac) { \
73 (a) += H ((b), (c), (d)) + (x) + (unsigned int)(ac); \
74 (a) = ROTATE_LEFT ((a), (s)); \
77 #define II(a, b, c, d, x, s, ac) { \
78 (a) += I ((b), (c), (d)) + (x) + (unsigned int)(ac); \
79 (a) = ROTATE_LEFT ((a), (s)); \
92 void SumMD5::Initialize()
94 count
[0] = count
[1] = 0;
96 state
[0] = 0x67452301;
97 state
[1] = 0xefcdab89;
98 state
[2] = 0x98badcfe;
99 state
[3] = 0x10325476;
102 void SumMD5::Update(const uint8
*input
, uint32 inputLen
)
104 uint32 i
, index
, partLen
;
106 /* Compute number of bytes mod 64 */
107 index
= ((count
[0] >> 3) & 0x3F);
109 /* Update number of bits */
110 if ((count
[0] += (inputLen
<< 3)) < (inputLen
<< 3))
113 count
[1] += inputLen
>> 29;
115 partLen
= 64 - index
;
117 if (inputLen
>= partLen
)
119 memcpy(&buffer
[index
], input
, partLen
);
121 MD5Transform(buffer
);
123 for (i
= partLen
; i
+ 63 < inputLen
; i
+= 64)
124 MD5Transform(&input
[i
]);
131 /* Buffer remaining input */
132 memcpy(&buffer
[index
], &input
[i
], inputLen
- i
);
135 void SumMD5::Finalize()
141 /* Save number of bits */
142 Encode(bits
, count
, 8);
144 /* Pad out to 56 mod 64.
146 index
= (uint32
) ((count
[0] >> 3) & 0x3f);
147 padLen
= (index
< 56) ? (56 - index
) : (120 - index
);
148 Update(PADDING
, padLen
);
150 /* Append length (before padding) */
154 void SumMD5::GetSum(uint8 digest
[16])
156 Encode(digest
, state
, 16);
159 void SumMD5::MD5Transform(const uint8
*block
)
167 Decode(x
, block
, 64);
170 FF(a
, b
, c
, d
, x
[0], S11
, 0xd76aa478); /* 1 */
171 FF(d
, a
, b
, c
, x
[1], S12
, 0xe8c7b756); /* 2 */
172 FF(c
, d
, a
, b
, x
[2], S13
, 0x242070db); /* 3 */
173 FF(b
, c
, d
, a
, x
[3], S14
, 0xc1bdceee); /* 4 */
174 FF(a
, b
, c
, d
, x
[4], S11
, 0xf57c0faf); /* 5 */
175 FF(d
, a
, b
, c
, x
[5], S12
, 0x4787c62a); /* 6 */
176 FF(c
, d
, a
, b
, x
[6], S13
, 0xa8304613); /* 7 */
177 FF(b
, c
, d
, a
, x
[7], S14
, 0xfd469501); /* 8 */
178 FF(a
, b
, c
, d
, x
[8], S11
, 0x698098d8); /* 9 */
179 FF(d
, a
, b
, c
, x
[9], S12
, 0x8b44f7af); /* 10 */
180 FF(c
, d
, a
, b
, x
[10], S13
, 0xffff5bb1); /* 11 */
181 FF(b
, c
, d
, a
, x
[11], S14
, 0x895cd7be); /* 12 */
182 FF(a
, b
, c
, d
, x
[12], S11
, 0x6b901122); /* 13 */
183 FF(d
, a
, b
, c
, x
[13], S12
, 0xfd987193); /* 14 */
184 FF(c
, d
, a
, b
, x
[14], S13
, 0xa679438e); /* 15 */
185 FF(b
, c
, d
, a
, x
[15], S14
, 0x49b40821); /* 16 */
188 GG(a
, b
, c
, d
, x
[1], S21
, 0xf61e2562); /* 17 */
189 GG(d
, a
, b
, c
, x
[6], S22
, 0xc040b340); /* 18 */
190 GG(c
, d
, a
, b
, x
[11], S23
, 0x265e5a51); /* 19 */
191 GG(b
, c
, d
, a
, x
[0], S24
, 0xe9b6c7aa); /* 20 */
192 GG(a
, b
, c
, d
, x
[5], S21
, 0xd62f105d); /* 21 */
193 GG(d
, a
, b
, c
, x
[10], S22
, 0x2441453); /* 22 */
194 GG(c
, d
, a
, b
, x
[15], S23
, 0xd8a1e681); /* 23 */
195 GG(b
, c
, d
, a
, x
[4], S24
, 0xe7d3fbc8); /* 24 */
196 GG(a
, b
, c
, d
, x
[9], S21
, 0x21e1cde6); /* 25 */
197 GG(d
, a
, b
, c
, x
[14], S22
, 0xc33707d6); /* 26 */
198 GG(c
, d
, a
, b
, x
[3], S23
, 0xf4d50d87); /* 27 */
199 GG(b
, c
, d
, a
, x
[8], S24
, 0x455a14ed); /* 28 */
200 GG(a
, b
, c
, d
, x
[13], S21
, 0xa9e3e905); /* 29 */
201 GG(d
, a
, b
, c
, x
[2], S22
, 0xfcefa3f8); /* 30 */
202 GG(c
, d
, a
, b
, x
[7], S23
, 0x676f02d9); /* 31 */
203 GG(b
, c
, d
, a
, x
[12], S24
, 0x8d2a4c8a); /* 32 */
206 HH(a
, b
, c
, d
, x
[5], S31
, 0xfffa3942); /* 33 */
207 HH(d
, a
, b
, c
, x
[8], S32
, 0x8771f681); /* 34 */
208 HH(c
, d
, a
, b
, x
[11], S33
, 0x6d9d6122); /* 35 */
209 HH(b
, c
, d
, a
, x
[14], S34
, 0xfde5380c); /* 36 */
210 HH(a
, b
, c
, d
, x
[1], S31
, 0xa4beea44); /* 37 */
211 HH(d
, a
, b
, c
, x
[4], S32
, 0x4bdecfa9); /* 38 */
212 HH(c
, d
, a
, b
, x
[7], S33
, 0xf6bb4b60); /* 39 */
213 HH(b
, c
, d
, a
, x
[10], S34
, 0xbebfbc70); /* 40 */
214 HH(a
, b
, c
, d
, x
[13], S31
, 0x289b7ec6); /* 41 */
215 HH(d
, a
, b
, c
, x
[0], S32
, 0xeaa127fa); /* 42 */
216 HH(c
, d
, a
, b
, x
[3], S33
, 0xd4ef3085); /* 43 */
217 HH(b
, c
, d
, a
, x
[6], S34
, 0x4881d05); /* 44 */
218 HH(a
, b
, c
, d
, x
[9], S31
, 0xd9d4d039); /* 45 */
219 HH(d
, a
, b
, c
, x
[12], S32
, 0xe6db99e5); /* 46 */
220 HH(c
, d
, a
, b
, x
[15], S33
, 0x1fa27cf8); /* 47 */
221 HH(b
, c
, d
, a
, x
[2], S34
, 0xc4ac5665); /* 48 */
224 II(a
, b
, c
, d
, x
[0], S41
, 0xf4292244); /* 49 */
225 II(d
, a
, b
, c
, x
[7], S42
, 0x432aff97); /* 50 */
226 II(c
, d
, a
, b
, x
[14], S43
, 0xab9423a7); /* 51 */
227 II(b
, c
, d
, a
, x
[5], S44
, 0xfc93a039); /* 52 */
228 II(a
, b
, c
, d
, x
[12], S41
, 0x655b59c3); /* 53 */
229 II(d
, a
, b
, c
, x
[3], S42
, 0x8f0ccc92); /* 54 */
230 II(c
, d
, a
, b
, x
[10], S43
, 0xffeff47d); /* 55 */
231 II(b
, c
, d
, a
, x
[1], S44
, 0x85845dd1); /* 56 */
232 II(a
, b
, c
, d
, x
[8], S41
, 0x6fa87e4f); /* 57 */
233 II(d
, a
, b
, c
, x
[15], S42
, 0xfe2ce6e0); /* 58 */
234 II(c
, d
, a
, b
, x
[6], S43
, 0xa3014314); /* 59 */
235 II(b
, c
, d
, a
, x
[13], S44
, 0x4e0811a1); /* 60 */
236 II(a
, b
, c
, d
, x
[4], S41
, 0xf7537e82); /* 61 */
237 II(d
, a
, b
, c
, x
[11], S42
, 0xbd3af235); /* 62 */
238 II(c
, d
, a
, b
, x
[2], S43
, 0x2ad7d2bb); /* 63 */
239 II(b
, c
, d
, a
, x
[9], S44
, 0xeb86d391); /* 64 */
246 /* Zeroize sensitive information. */
247 memset(x
, 0, sizeof (x
));
250 void SumMD5::Encode(uint8
*output
, const uint32
*input
, uint32 len
)
254 for (i
= 0, j
= 0; j
< len
; i
++, j
+= 4) {
255 output
[j
] = (uint8
) (input
[i
] & 0xff);
256 output
[j
+ 1] = (uint8
) ((input
[i
] >> 8) & 0xff);
257 output
[j
+ 2] = (uint8
) ((input
[i
] >> 16) & 0xff);
258 output
[j
+ 3] = (uint8
) ((input
[i
] >> 24) & 0xff);
262 void SumMD5::Decode(uint32
* output
, const uint8
* input
, uint32 len
)
266 for (i
= 0, j
= 0; j
< len
; i
++, j
+= 4)
268 ((uint32
) input
[j
]) |
269 (((uint32
) input
[j
+ 1]) << 8) |
270 (((uint32
) input
[j
+ 2]) << 16) |
271 (((uint32
) input
[j
+ 3]) << 24);
274 void SumMD5::GetSum(uint32 x
[4])
280 x
[0] = x
[1] = x
[2] = x
[3] = 0;
282 for (int16 i
=0; i
<4; i
++)
284 x
[0] <<= 8; x
[0] |= arr
[i
];
285 x
[1] <<= 8; x
[1] |= arr
[i
+4];
286 x
[2] <<= 8; x
[2] |= arr
[i
+8];
287 x
[3] <<= 8; x
[3] |= arr
[i
+12];
291 void SumMD5::GetSum(uint64 x
[2])
299 for (int16 i
=0; i
<8; i
++)
301 x
[0] <<= 8; x
[0] |= arr
[i
];
302 x
[1] <<= 8; x
[1] |= arr
[i
+8];