OLD | NEW |
| (Empty) |
1 // Copyright 2004-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 /* | |
17 * md5_opt.c V1.0 - optimized md5c.c from RFC1321 reference implementation | |
18 * | |
19 * Copyright (c) 1995 University of Southern California. | |
20 * All rights reserved. | |
21 * | |
22 * Redistribution and use in source and binary forms are permitted | |
23 * provided that the above copyright notice and this paragraph are | |
24 * duplicated in all such forms and that any documentation, advertising | |
25 * materials, and other materials related to such distribution and use | |
26 * acknowledge that the software was developed by the University of | |
27 * Southern California, Information Sciences Institute. The name of the | |
28 * University may not be used to endorse or promote products derived from | |
29 * this software without specific prior written permission. | |
30 * | |
31 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | |
32 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | |
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
34 * | |
35 J. Touch / touch@isi.edu | |
36 5/1/95 | |
37 | |
38 */ | |
39 /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm | |
40 */ | |
41 | |
42 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All | |
43 rights reserved. | |
44 | |
45 License to copy and use this software is granted provided that it | |
46 is identified as the "RSA Data Security, Inc. MD5 Message-Digest | |
47 Algorithm" in all material mentioning or referencing this software | |
48 or this function. | |
49 | |
50 License is also granted to make and use derivative works provided | |
51 that such works are identified as "derived from the RSA Data | |
52 Security, Inc. MD5 Message-Digest Algorithm" in all material | |
53 mentioning or referencing the derived work. | |
54 | |
55 RSA Data Security, Inc. makes no representations concerning either | |
56 the merchantability of this software or the suitability of this | |
57 software for any particular purpose. It is provided "as is" | |
58 without express or implied warranty of any kind. | |
59 | |
60 These notices must be retained in any copies of any part of this | |
61 documentation and/or software. | |
62 */ | |
63 | |
64 #include "omaha/base/md5.h" | |
65 #include "omaha/base/debug.h" | |
66 | |
67 namespace omaha { | |
68 | |
69 #if (defined(i386) || defined (__i386__) || defined (_M_IX86) || defined(__alpha
)) // little-endian | |
70 #undef REORDER | |
71 #else | |
72 #define REORDER 1 | |
73 #endif | |
74 | |
75 // Constants for MD5Transform routine | |
76 #define kS11 7 | |
77 #define kS12 12 | |
78 #define kS13 17 | |
79 #define kS14 22 | |
80 #define kS21 5 | |
81 #define kS22 9 | |
82 #define kS23 14 | |
83 #define kS24 20 | |
84 #define kS31 4 | |
85 #define kS32 11 | |
86 #define kS33 16 | |
87 #define kS34 23 | |
88 #define kS41 6 | |
89 #define kS42 10 | |
90 #define kS43 15 | |
91 #define kS44 21 | |
92 | |
93 static void MD5Transform (uint32 [4], unsigned char [64]); | |
94 static void Encode (unsigned char *, uint32 *, unsigned int); | |
95 #ifdef REORDER | |
96 static void Decode (uint32 *, unsigned char *, unsigned int); | |
97 #endif | |
98 | |
99 // SELECTANY static unsigned char PADDING[64] = { | |
100 static unsigned char PADDING[64] = { | |
101 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
104 }; | |
105 | |
106 // F, G, H and I are basic MD5 functions. | |
107 #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) | |
108 #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) | |
109 #define H(x, y, z) ((x) ^ (y) ^ (z)) | |
110 #define I(x, y, z) ((y) ^ ((x) | (~z))) | |
111 | |
112 // ROTATE_LEFT rotates x left n bits. | |
113 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) | |
114 | |
115 // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. | |
116 // Rotation is separate from addition to prevent recomputation. | |
117 #define FF(a, b, c, d, x, s, ac) { \ | |
118 (a) += F ((b), (c), (d)) + (x) + (uint32)(ac); \ | |
119 (a) = ROTATE_LEFT ((a), (s)); \ | |
120 (a) += (b); \ | |
121 } | |
122 #define GG(a, b, c, d, x, s, ac) { \ | |
123 (a) += G ((b), (c), (d)) + (x) + (uint32)(ac); \ | |
124 (a) = ROTATE_LEFT ((a), (s)); \ | |
125 (a) += (b); \ | |
126 } | |
127 #define HH(a, b, c, d, x, s, ac) { \ | |
128 (a) += H ((b), (c), (d)) + (x) + (uint32)(ac); \ | |
129 (a) = ROTATE_LEFT ((a), (s)); \ | |
130 (a) += (b); \ | |
131 } | |
132 #define II(a, b, c, d, x, s, ac) { \ | |
133 (a) += I ((b), (c), (d)) + (x) + (uint32)(ac); \ | |
134 (a) = ROTATE_LEFT ((a), (s)); \ | |
135 (a) += (b); \ | |
136 } | |
137 | |
138 // MD5 initialization. Begins an MD5 operation, writing a new context. | |
139 void MD5Init (MD5_CTX *context) { | |
140 ASSERT(context, (L"")); | |
141 | |
142 context->count[0] = context->count[1] = 0; | |
143 // Load magic initialization constants. | |
144 context->state[0] = 0x67452301; | |
145 context->state[1] = 0xefcdab89; | |
146 context->state[2] = 0x98badcfe; | |
147 context->state[3] = 0x10325476; | |
148 } | |
149 | |
150 // MD5 block update operation. Continues an MD5 message-digest | |
151 // operation, processing another message block, and updating the context. | |
152 void MD5Update (MD5_CTX *context, unsigned char *input, unsigned int inputLen) { | |
153 ASSERT(input, (L"")); | |
154 ASSERT(context, (L"")); | |
155 | |
156 unsigned int i, index, partLen; | |
157 | |
158 // Compute number of bytes mod 64 | |
159 index = (unsigned int)((context->count[0] >> 3) & 0x3F); | |
160 | |
161 // Update number of bits | |
162 if ((context->count[0] += ((uint32)inputLen << 3)) < ((uint32)inputLen << 3)
) | |
163 context->count[1]++; | |
164 | |
165 context->count[1] += ((uint32)inputLen >> 29); | |
166 partLen = 64 - index; | |
167 | |
168 // Transform as many times as possible | |
169 if (inputLen >= partLen) { | |
170 memcpy ((POINTER)&context->buffer[index], (POINTER)input, partLen); | |
171 MD5Transform (context->state, context->buffer); | |
172 | |
173 for (i = partLen; i + 63 < inputLen; i += 64) MD5Transform (context->stat
e, &input[i]); | |
174 index = 0; | |
175 } | |
176 else | |
177 i = 0; | |
178 | |
179 // Buffer remaining input | |
180 memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i); | |
181 } | |
182 | |
183 // MD5 finalization. Ends an MD5 message-digest operation, writing the | |
184 // the message digest and zeroizing the context. | |
185 void MD5Final (unsigned char digest[16], MD5_CTX *context) { | |
186 ASSERT(context, (L"")); | |
187 | |
188 unsigned char bits[8]; | |
189 unsigned int index, padLen; | |
190 | |
191 // Save number of bits | |
192 Encode (bits, context->count, 8); | |
193 | |
194 // Pad out to 56 mod 64. | |
195 index = (unsigned int)((context->count[0] >> 3) & 0x3f); | |
196 padLen = (index < 56) ? (56 - index) : (120 - index); | |
197 MD5Update (context, PADDING, padLen); | |
198 | |
199 // Append length (before padding) | |
200 MD5Update (context, bits, 8); | |
201 // Store state in digest | |
202 Encode (digest, context->state, 16); | |
203 | |
204 // Zeroize sensitive information. | |
205 memset ((POINTER)context, 0, sizeof (*context)); | |
206 } | |
207 | |
208 // MD5 basic transformation. Transforms state based on block. | |
209 void MD5Transform (uint32 state[4], unsigned char block[64]) { | |
210 // USC/ISI J. Touch - encourage pushing state variables into registers | |
211 register uint32 a = state[0], b = state[1], c = state[2], d = state[3]; | |
212 | |
213 /* USC/ISI J. Touch | |
214 decode and using copied data vs. direct use of input buffer | |
215 depends on whether reordering is required, which is a combination | |
216 of the byte order of the architecture and the command-line option | |
217 override | |
218 */ | |
219 | |
220 #ifdef REORDER | |
221 uint32 x[16]; | |
222 | |
223 Decode (x, block, 64); | |
224 | |
225 /* Round 1 */ | |
226 FF (a, b, c, d, x[ 0], kS11, 0xd76aa478); /* 1 */ | |
227 FF (d, a, b, c, x[ 1], kS12, 0xe8c7b756); /* 2 */ | |
228 FF (c, d, a, b, x[ 2], kS13, 0x242070db); /* 3 */ | |
229 FF (b, c, d, a, x[ 3], kS14, 0xc1bdceee); /* 4 */ | |
230 FF (a, b, c, d, x[ 4], kS11, 0xf57c0faf); /* 5 */ | |
231 FF (d, a, b, c, x[ 5], kS12, 0x4787c62a); /* 6 */ | |
232 FF (c, d, a, b, x[ 6], kS13, 0xa8304613); /* 7 */ | |
233 FF (b, c, d, a, x[ 7], kS14, 0xfd469501); /* 8 */ | |
234 FF (a, b, c, d, x[ 8], kS11, 0x698098d8); /* 9 */ | |
235 FF (d, a, b, c, x[ 9], kS12, 0x8b44f7af); /* 10 */ | |
236 FF (c, d, a, b, x[10], kS13, 0xffff5bb1); /* 11 */ | |
237 FF (b, c, d, a, x[11], kS14, 0x895cd7be); /* 12 */ | |
238 FF (a, b, c, d, x[12], kS11, 0x6b901122); /* 13 */ | |
239 FF (d, a, b, c, x[13], kS12, 0xfd987193); /* 14 */ | |
240 FF (c, d, a, b, x[14], kS13, 0xa679438e); /* 15 */ | |
241 FF (b, c, d, a, x[15], kS14, 0x49b40821); /* 16 */ | |
242 | |
243 /* Round 2 */ | |
244 GG (a, b, c, d, x[ 1], kS21, 0xf61e2562); /* 17 */ | |
245 GG (d, a, b, c, x[ 6], kS22, 0xc040b340); /* 18 */ | |
246 GG (c, d, a, b, x[11], kS23, 0x265e5a51); /* 19 */ | |
247 GG (b, c, d, a, x[ 0], kS24, 0xe9b6c7aa); /* 20 */ | |
248 GG (a, b, c, d, x[ 5], kS21, 0xd62f105d); /* 21 */ | |
249 GG (d, a, b, c, x[10], kS22, 0x2441453); /* 22 */ | |
250 GG (c, d, a, b, x[15], kS23, 0xd8a1e681); /* 23 */ | |
251 GG (b, c, d, a, x[ 4], kS24, 0xe7d3fbc8); /* 24 */ | |
252 GG (a, b, c, d, x[ 9], kS21, 0x21e1cde6); /* 25 */ | |
253 GG (d, a, b, c, x[14], kS22, 0xc33707d6); /* 26 */ | |
254 GG (c, d, a, b, x[ 3], kS23, 0xf4d50d87); /* 27 */ | |
255 GG (b, c, d, a, x[ 8], kS24, 0x455a14ed); /* 28 */ | |
256 GG (a, b, c, d, x[13], kS21, 0xa9e3e905); /* 29 */ | |
257 GG (d, a, b, c, x[ 2], kS22, 0xfcefa3f8); /* 30 */ | |
258 GG (c, d, a, b, x[ 7], kS23, 0x676f02d9); /* 31 */ | |
259 GG (b, c, d, a, x[12], kS24, 0x8d2a4c8a); /* 32 */ | |
260 | |
261 /* Round 3 */ | |
262 HH (a, b, c, d, x[ 5], kS31, 0xfffa3942); /* 33 */ | |
263 HH (d, a, b, c, x[ 8], kS32, 0x8771f681); /* 34 */ | |
264 HH (c, d, a, b, x[11], kS33, 0x6d9d6122); /* 35 */ | |
265 HH (b, c, d, a, x[14], kS34, 0xfde5380c); /* 36 */ | |
266 HH (a, b, c, d, x[ 1], kS31, 0xa4beea44); /* 37 */ | |
267 HH (d, a, b, c, x[ 4], kS32, 0x4bdecfa9); /* 38 */ | |
268 HH (c, d, a, b, x[ 7], kS33, 0xf6bb4b60); /* 39 */ | |
269 HH (b, c, d, a, x[10], kS34, 0xbebfbc70); /* 40 */ | |
270 HH (a, b, c, d, x[13], kS31, 0x289b7ec6); /* 41 */ | |
271 HH (d, a, b, c, x[ 0], kS32, 0xeaa127fa); /* 42 */ | |
272 HH (c, d, a, b, x[ 3], kS33, 0xd4ef3085); /* 43 */ | |
273 HH (b, c, d, a, x[ 6], kS34, 0x4881d05); /* 44 */ | |
274 HH (a, b, c, d, x[ 9], kS31, 0xd9d4d039); /* 45 */ | |
275 HH (d, a, b, c, x[12], kS32, 0xe6db99e5); /* 46 */ | |
276 HH (c, d, a, b, x[15], kS33, 0x1fa27cf8); /* 47 */ | |
277 HH (b, c, d, a, x[ 2], kS34, 0xc4ac5665); /* 48 */ | |
278 | |
279 /* Round 4 */ | |
280 II (a, b, c, d, x[ 0], kS41, 0xf4292244); /* 49 */ | |
281 II (d, a, b, c, x[ 7], kS42, 0x432aff97); /* 50 */ | |
282 II (c, d, a, b, x[14], kS43, 0xab9423a7); /* 51 */ | |
283 II (b, c, d, a, x[ 5], kS44, 0xfc93a039); /* 52 */ | |
284 II (a, b, c, d, x[12], kS41, 0x655b59c3); /* 53 */ | |
285 II (d, a, b, c, x[ 3], kS42, 0x8f0ccc92); /* 54 */ | |
286 II (c, d, a, b, x[10], kS43, 0xffeff47d); /* 55 */ | |
287 II (b, c, d, a, x[ 1], kS44, 0x85845dd1); /* 56 */ | |
288 II (a, b, c, d, x[ 8], kS41, 0x6fa87e4f); /* 57 */ | |
289 II (d, a, b, c, x[15], kS42, 0xfe2ce6e0); /* 58 */ | |
290 II (c, d, a, b, x[ 6], kS43, 0xa3014314); /* 59 */ | |
291 II (b, c, d, a, x[13], kS44, 0x4e0811a1); /* 60 */ | |
292 II (a, b, c, d, x[ 4], kS41, 0xf7537e82); /* 61 */ | |
293 II (d, a, b, c, x[11], kS42, 0xbd3af235); /* 62 */ | |
294 II (c, d, a, b, x[ 2], kS43, 0x2ad7d2bb); /* 63 */ | |
295 II (b, c, d, a, x[ 9], kS44, 0xeb86d391); /* 64 */ | |
296 | |
297 #else | |
298 // USC/ISI J. Touch | |
299 // omit reordering, and use the input block as source data | |
300 /* Round 1 */ | |
301 FF (a, b, c, d, ((uint32 *)block)[ 0], kS11, 0xd76aa478); /* 1 */ | |
302 FF (d, a, b, c, ((uint32 *)block)[ 1], kS12, 0xe8c7b756); /* 2 */ | |
303 FF (c, d, a, b, ((uint32 *)block)[ 2], kS13, 0x242070db); /* 3 */ | |
304 FF (b, c, d, a, ((uint32 *)block)[ 3], kS14, 0xc1bdceee); /* 4 */ | |
305 FF (a, b, c, d, ((uint32 *)block)[ 4], kS11, 0xf57c0faf); /* 5 */ | |
306 FF (d, a, b, c, ((uint32 *)block)[ 5], kS12, 0x4787c62a); /* 6 */ | |
307 FF (c, d, a, b, ((uint32 *)block)[ 6], kS13, 0xa8304613); /* 7 */ | |
308 FF (b, c, d, a, ((uint32 *)block)[ 7], kS14, 0xfd469501); /* 8 */ | |
309 FF (a, b, c, d, ((uint32 *)block)[ 8], kS11, 0x698098d8); /* 9 */ | |
310 FF (d, a, b, c, ((uint32 *)block)[ 9], kS12, 0x8b44f7af); /* 10 */ | |
311 FF (c, d, a, b, ((uint32 *)block)[10], kS13, 0xffff5bb1); /* 11 */ | |
312 FF (b, c, d, a, ((uint32 *)block)[11], kS14, 0x895cd7be); /* 12 */ | |
313 FF (a, b, c, d, ((uint32 *)block)[12], kS11, 0x6b901122); /* 13 */ | |
314 FF (d, a, b, c, ((uint32 *)block)[13], kS12, 0xfd987193); /* 14 */ | |
315 FF (c, d, a, b, ((uint32 *)block)[14], kS13, 0xa679438e); /* 15 */ | |
316 FF (b, c, d, a, ((uint32 *)block)[15], kS14, 0x49b40821); /* 16 */ | |
317 | |
318 /* Round 2 */ | |
319 GG (a, b, c, d, ((uint32 *)block)[ 1], kS21, 0xf61e2562); /* 17 */ | |
320 GG (d, a, b, c, ((uint32 *)block)[ 6], kS22, 0xc040b340); /* 18 */ | |
321 GG (c, d, a, b, ((uint32 *)block)[11], kS23, 0x265e5a51); /* 19 */ | |
322 GG (b, c, d, a, ((uint32 *)block)[ 0], kS24, 0xe9b6c7aa); /* 20 */ | |
323 GG (a, b, c, d, ((uint32 *)block)[ 5], kS21, 0xd62f105d); /* 21 */ | |
324 GG (d, a, b, c, ((uint32 *)block)[10], kS22, 0x2441453); /* 22 */ | |
325 GG (c, d, a, b, ((uint32 *)block)[15], kS23, 0xd8a1e681); /* 23 */ | |
326 GG (b, c, d, a, ((uint32 *)block)[ 4], kS24, 0xe7d3fbc8); /* 24 */ | |
327 GG (a, b, c, d, ((uint32 *)block)[ 9], kS21, 0x21e1cde6); /* 25 */ | |
328 GG (d, a, b, c, ((uint32 *)block)[14], kS22, 0xc33707d6); /* 26 */ | |
329 GG (c, d, a, b, ((uint32 *)block)[ 3], kS23, 0xf4d50d87); /* 27 */ | |
330 GG (b, c, d, a, ((uint32 *)block)[ 8], kS24, 0x455a14ed); /* 28 */ | |
331 GG (a, b, c, d, ((uint32 *)block)[13], kS21, 0xa9e3e905); /* 29 */ | |
332 GG (d, a, b, c, ((uint32 *)block)[ 2], kS22, 0xfcefa3f8); /* 30 */ | |
333 GG (c, d, a, b, ((uint32 *)block)[ 7], kS23, 0x676f02d9); /* 31 */ | |
334 GG (b, c, d, a, ((uint32 *)block)[12], kS24, 0x8d2a4c8a); /* 32 */ | |
335 | |
336 /* Round 3 */ | |
337 HH (a, b, c, d, ((uint32 *)block)[ 5], kS31, 0xfffa3942); /* 33 */ | |
338 HH (d, a, b, c, ((uint32 *)block)[ 8], kS32, 0x8771f681); /* 34 */ | |
339 HH (c, d, a, b, ((uint32 *)block)[11], kS33, 0x6d9d6122); /* 35 */ | |
340 HH (b, c, d, a, ((uint32 *)block)[14], kS34, 0xfde5380c); /* 36 */ | |
341 HH (a, b, c, d, ((uint32 *)block)[ 1], kS31, 0xa4beea44); /* 37 */ | |
342 HH (d, a, b, c, ((uint32 *)block)[ 4], kS32, 0x4bdecfa9); /* 38 */ | |
343 HH (c, d, a, b, ((uint32 *)block)[ 7], kS33, 0xf6bb4b60); /* 39 */ | |
344 HH (b, c, d, a, ((uint32 *)block)[10], kS34, 0xbebfbc70); /* 40 */ | |
345 HH (a, b, c, d, ((uint32 *)block)[13], kS31, 0x289b7ec6); /* 41 */ | |
346 HH (d, a, b, c, ((uint32 *)block)[ 0], kS32, 0xeaa127fa); /* 42 */ | |
347 HH (c, d, a, b, ((uint32 *)block)[ 3], kS33, 0xd4ef3085); /* 43 */ | |
348 HH (b, c, d, a, ((uint32 *)block)[ 6], kS34, 0x4881d05); /* 44 */ | |
349 HH (a, b, c, d, ((uint32 *)block)[ 9], kS31, 0xd9d4d039); /* 45 */ | |
350 HH (d, a, b, c, ((uint32 *)block)[12], kS32, 0xe6db99e5); /* 46 */ | |
351 HH (c, d, a, b, ((uint32 *)block)[15], kS33, 0x1fa27cf8); /* 47 */ | |
352 HH (b, c, d, a, ((uint32 *)block)[ 2], kS34, 0xc4ac5665); /* 48 */ | |
353 | |
354 /* Round 4 */ | |
355 II (a, b, c, d, ((uint32 *)block)[ 0], kS41, 0xf4292244); /* 49 */ | |
356 II (d, a, b, c, ((uint32 *)block)[ 7], kS42, 0x432aff97); /* 50 */ | |
357 II (c, d, a, b, ((uint32 *)block)[14], kS43, 0xab9423a7); /* 51 */ | |
358 II (b, c, d, a, ((uint32 *)block)[ 5], kS44, 0xfc93a039); /* 52 */ | |
359 II (a, b, c, d, ((uint32 *)block)[12], kS41, 0x655b59c3); /* 53 */ | |
360 II (d, a, b, c, ((uint32 *)block)[ 3], kS42, 0x8f0ccc92); /* 54 */ | |
361 II (c, d, a, b, ((uint32 *)block)[10], kS43, 0xffeff47d); /* 55 */ | |
362 II (b, c, d, a, ((uint32 *)block)[ 1], kS44, 0x85845dd1); /* 56 */ | |
363 II (a, b, c, d, ((uint32 *)block)[ 8], kS41, 0x6fa87e4f); /* 57 */ | |
364 II (d, a, b, c, ((uint32 *)block)[15], kS42, 0xfe2ce6e0); /* 58 */ | |
365 II (c, d, a, b, ((uint32 *)block)[ 6], kS43, 0xa3014314); /* 59 */ | |
366 II (b, c, d, a, ((uint32 *)block)[13], kS44, 0x4e0811a1); /* 60 */ | |
367 II (a, b, c, d, ((uint32 *)block)[ 4], kS41, 0xf7537e82); /* 61 */ | |
368 II (d, a, b, c, ((uint32 *)block)[11], kS42, 0xbd3af235); /* 62 */ | |
369 II (c, d, a, b, ((uint32 *)block)[ 2], kS43, 0x2ad7d2bb); /* 63 */ | |
370 II (b, c, d, a, ((uint32 *)block)[ 9], kS44, 0xeb86d391); /* 64 */ | |
371 #endif // REORDER | |
372 | |
373 state[0] += a; | |
374 state[1] += b; | |
375 state[2] += c; | |
376 state[3] += d; | |
377 | |
378 #ifdef REORDER | |
379 memset ((POINTER)x, 0, sizeof (x)); // zero sensitive information | |
380 #endif | |
381 } | |
382 | |
383 // Encodes input (uint32) into output (unsigned char). Assumes len is a multiple
of 4. | |
384 void Encode (unsigned char *output, uint32 *input, unsigned int len) { | |
385 ASSERT(input, (L"")); | |
386 ASSERT(output, (L"")); | |
387 | |
388 unsigned int i, j; | |
389 | |
390 for (i = 0, j = 0; j < len; i++, j += 4) { | |
391 output[j] = (unsigned char)(input[i] & 0xff); | |
392 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); | |
393 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); | |
394 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); | |
395 } | |
396 } | |
397 | |
398 #ifdef REORDER | |
399 | |
400 // Decodes input (unsigned char) into output (uint32). Assumes len is a multiple
of 4. | |
401 void Decode (uint32 *output, unsigned char *input, unsigned int len) { | |
402 ASSERT(input, (L"")); | |
403 ASSERT(output, (L"")); | |
404 | |
405 register uint32 out,other; | |
406 | |
407 // for (i = 0, j = 0; j < len; i++, j += 4) | |
408 // output[i] = ((uint32)input[j]) | (((uint32)input[j+1]) << 8) | | |
409 // (((uint32)input[j+2]) << 16) | (((uint32)input[j+3]) << 24); | |
410 | |
411 // USC/ISI J. Touch | |
412 // these are optimized swap routines, in C code they cost more in "computati
on" operations, but less in | |
413 // "loads" than the above code, and run substantially faster as a result | |
414 #if (!defined(hpux)) | |
415 #define swapbyte(src,dst) { \ | |
416 out = ROTATE_LEFT((src),16); \ | |
417 other = out >> 8; \ | |
418 other &= 0x00ff00ff; \ | |
419 out &= 0x00ff00ff; \ | |
420 out <<= 8; \ | |
421 (dst) = out | other; \ | |
422 } | |
423 #else | |
424 #define swapbyte(src,dst) { \ | |
425 (dst) = (ROTATE_LEFT((src),8) & 0x00ff00ff) | ROTATE_LEFT((src) & 0x00ff00f
f,24); \ | |
426 } | |
427 #endif | |
428 | |
429 // USC/ISI J. Touch | |
430 // unroll the loop above, because the code runs faster with constants for in
dices than even with variable indices | |
431 // as conventional (automatic) unrolling would perform (!) | |
432 swapbyte(((uint32 *)input)[0],output[0]); | |
433 swapbyte(((uint32 *)input)[1],output[1]); | |
434 swapbyte(((uint32 *)input)[2],output[2]); | |
435 swapbyte(((uint32 *)input)[3],output[3]); | |
436 swapbyte(((uint32 *)input)[4],output[4]); | |
437 swapbyte(((uint32 *)input)[5],output[5]); | |
438 swapbyte(((uint32 *)input)[6],output[6]); | |
439 swapbyte(((uint32 *)input)[7],output[7]); | |
440 swapbyte(((uint32 *)input)[8],output[8]); | |
441 swapbyte(((uint32 *)input)[9],output[9]); | |
442 swapbyte(((uint32 *)input)[10],output[10]); | |
443 swapbyte(((uint32 *)input)[11],output[11]); | |
444 swapbyte(((uint32 *)input)[12],output[12]); | |
445 swapbyte(((uint32 *)input)[13],output[13]); | |
446 swapbyte(((uint32 *)input)[14],output[14]); | |
447 swapbyte(((uint32 *)input)[15],output[15]); | |
448 } | |
449 | |
450 #endif // #ifdef REORDER | |
451 | |
452 } // namespace omaha | |
453 | |
OLD | NEW |