1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*++
Copyright (c) 2007 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.
Module Name:
Md5.c
Abstract:
Implementation of MD5 algorithm
--*/
#include "Md5.h"
STATIC CONST UINT32 MD5_K[][2] = {
{ 0, 1 },
{ 1, 5 },
{ 5, 3 },
{ 0, 7 }
};
STATIC CONST UINT32 MD5_S[][4] = {
{ 7, 22, 17, 12 },
{ 5, 20, 14, 9 },
{ 4, 23, 16 ,11 },
{ 6, 21, 15, 10 },
};
STATIC CONST UINT32 MD5_T[] = {
0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE,
0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,
0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE,
0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821,
0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA,
0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8,
0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED,
0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A,
0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C,
0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70,
0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05,
0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665,
0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039,
0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1,
0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1,
0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391
};
STATIC CONST UINT8 Md5HashPadding[] =
{
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
//
// ROTATE_LEFT rotates x left n bits.
//
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
#define SA S[j & 3]
#define SB S[(j + 1) & 3]
#define SC S[(j + 2) & 3]
#define SD S[(j + 3) & 3]
//
// TF1, TF2, TF3, TF4 are basic MD5 transform functions
//
UINT32 TF1 (UINT32 A, UINT32 B, UINT32 C)
{
return (A & B) | (~A & C);
}
UINT32 TF2 (UINT32 A, UINT32 B, UINT32 C)
{
return (A & C) | (B & ~C);
}
UINT32 TF3 (UINT32 A, UINT32 B, UINT32 C)
{
return A ^ B ^ C;
}
UINT32 TF4 (UINT32 A, UINT32 B, UINT32 C)
{
return B ^ (A | ~C);
}
typedef
UINT32
(*MD5_TRANSFORM_FUNC) (
IN UINT32 A,
IN UINT32 B,
IN UINT32 C
);
STATIC CONST MD5_TRANSFORM_FUNC MD5_F[] = {
TF1,
TF2,
TF3,
TF4
};
STATIC
VOID
MD5Transform (
IN MD5_CTX *Md5Ctx
)
/*++
Routine Description:
GC_TODO: Add function description
Arguments:
Md5Ctx - GC_TODO: add argument description
Returns:
GC_TODO: add return values
--*/
{
UINT32 i;
UINT32 j;
UINT32 S[MD5_HASHSIZE >> 2];
UINT32 *X;
UINT32 k;
UINT32 t;
X = (UINT32 *) Md5Ctx->M;
//
// Copy MD5 states to S
//
NetCopyMem (S, Md5Ctx->States, MD5_HASHSIZE);
t = 0;
for (i = 0; i < 4; i++) {
k = MD5_K[i][0];
for (j = 16; j > 0; j--) {
SA += (*MD5_F[i]) (SB, SC, SD) + X[k] + MD5_T[t];
SA = ROTATE_LEFT (SA, MD5_S[i][j & 3]);
SA += SB;
k += MD5_K[i][1];
k &= 15;
t++;
}
}
for (i = 0; i < 4; i++) {
Md5Ctx->States[i] += S[i];
}
}
STATIC
VOID
MD5UpdateBlock (
IN MD5_CTX *Md5Ctx,
IN CONST UINT8 *Data,
IN UINTN DataLen
)
/*++
Routine Description:
GC_TODO: Add function description
Arguments:
Md5Ctx - GC_TODO: add argument description
Data - GC_TODO: add argument description
DataLen - GC_TODO: add argument description
Returns:
GC_TODO: add return values
--*/
{
UINTN Limit;
for (Limit = 64 - Md5Ctx->Count; DataLen >= 64 - Md5Ctx->Count; Limit = 64) {
NetCopyMem (Md5Ctx->M + Md5Ctx->Count, (VOID *)Data, Limit);
MD5Transform (Md5Ctx);
Md5Ctx->Count = 0;
Data += Limit;
DataLen -= Limit;
}
NetCopyMem (Md5Ctx->M + Md5Ctx->Count, (VOID *)Data, DataLen);
Md5Ctx->Count += DataLen;
}
EFI_STATUS
MD5Init (
IN MD5_CTX *Md5Ctx
)
/*++
Routine Description:
GC_TODO: Add function description
Arguments:
Md5Ctx - GC_TODO: add argument description
Returns:
EFI_SUCCESS - GC_TODO: Add description for return value
--*/
{
NetZeroMem (Md5Ctx, sizeof (*Md5Ctx));
//
// Set magic initialization constants.
//
Md5Ctx->States[0] = 0x67452301;
Md5Ctx->States[1] = 0xefcdab89;
Md5Ctx->States[2] = 0x98badcfe;
Md5Ctx->States[3] = 0x10325476;
return EFI_SUCCESS;
}
EFI_STATUS
MD5Update (
IN MD5_CTX *Md5Ctx,
IN VOID *Data,
IN UINTN DataLen
)
/*++
Routine Description:
GC_TODO: Add function description
Arguments:
Md5Ctx - GC_TODO: add argument description
Data - GC_TODO: add argument description
DataLen - GC_TODO: add argument description
Returns:
EFI_SUCCESS - GC_TODO: Add description for return value
--*/
{
if (EFI_ERROR (Md5Ctx->Status)) {
return Md5Ctx->Status;
}
MD5UpdateBlock (Md5Ctx, (CONST UINT8 *) Data, DataLen);
Md5Ctx->Length += DataLen;
return EFI_SUCCESS;
}
EFI_STATUS
MD5Final (
IN MD5_CTX *Md5Ctx,
OUT UINT8 *HashVal
)
/*++
Routine Description:
GC_TODO: Add function description
Arguments:
Md5Ctx - GC_TODO: add argument description
HashVal - GC_TODO: add argument description
Returns:
EFI_SUCCESS - GC_TODO: Add description for return value
--*/
{
UINTN PadLength;
if (Md5Ctx->Status == EFI_ALREADY_STARTED) {
//
// Store Hashed value & Zeroize sensitive context information.
//
NetCopyMem (HashVal, (UINT8 *) Md5Ctx->States, MD5_HASHSIZE);
NetZeroMem ((UINT8 *)Md5Ctx, sizeof (*Md5Ctx));
return EFI_SUCCESS;
}
if (EFI_ERROR (Md5Ctx->Status)) {
return Md5Ctx->Status;
}
PadLength = Md5Ctx->Count >= 56 ? 120 : 56;
PadLength -= Md5Ctx->Count;
MD5UpdateBlock (Md5Ctx, Md5HashPadding, PadLength);
Md5Ctx->Length = LShiftU64 (Md5Ctx->Length, 3);
MD5UpdateBlock (Md5Ctx, (CONST UINT8 *) &Md5Ctx->Length, 8);
NetZeroMem (Md5Ctx->M, sizeof (Md5Ctx->M));
Md5Ctx->Length = 0;
Md5Ctx->Status = EFI_ALREADY_STARTED;
return MD5Final (Md5Ctx, HashVal);
}
|