3 Copyright (c) 2007 Intel Corporation. All rights reserved
4 This software and associated documentation (if any) is furnished
5 under a license and may only be used or copied in accordance
6 with the terms of the license. Except as permitted by such
7 license, no part of this software or documentation may be
8 reproduced, stored in a retrieval system, or transmitted in any
9 form or by any means without the express written consent of
18 Implementation of MD5 algorithm
24 STATIC CONST UINT32 MD5_K
[][2] = {
31 STATIC CONST UINT32 MD5_S
[][4] = {
38 STATIC CONST UINT32 MD5_T
[] = {
39 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE,
40 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,
41 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE,
42 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821,
43 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA,
44 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8,
45 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED,
46 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A,
47 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C,
48 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70,
49 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05,
50 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665,
51 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039,
52 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1,
53 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1,
54 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391
57 STATIC CONST UINT8 Md5HashPadding
[] =
59 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
78 // ROTATE_LEFT rotates x left n bits.
80 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
83 #define SB S[(j + 1) & 3]
84 #define SC S[(j + 2) & 3]
85 #define SD S[(j + 3) & 3]
88 // TF1, TF2, TF3, TF4 are basic MD5 transform functions
90 UINT32
TF1 (UINT32 A
, UINT32 B
, UINT32 C
)
92 return (A
& B
) | (~A
& C
);
95 UINT32
TF2 (UINT32 A
, UINT32 B
, UINT32 C
)
97 return (A
& C
) | (B
& ~C
);
100 UINT32
TF3 (UINT32 A
, UINT32 B
, UINT32 C
)
105 UINT32
TF4 (UINT32 A
, UINT32 B
, UINT32 C
)
112 (*MD5_TRANSFORM_FUNC
) (
118 STATIC CONST MD5_TRANSFORM_FUNC MD5_F
[] = {
134 GC_TODO: Add function description
138 Md5Ctx - GC_TODO: add argument description
142 GC_TODO: add return values
148 UINT32 S
[MD5_HASHSIZE
>> 2];
153 X
= (UINT32
*) Md5Ctx
->M
;
156 // Copy MD5 states to S
158 NetCopyMem (S
, Md5Ctx
->States
, MD5_HASHSIZE
);
161 for (i
= 0; i
< 4; i
++) {
163 for (j
= 16; j
> 0; j
--) {
164 SA
+= (*MD5_F
[i
]) (SB
, SC
, SD
) + X
[k
] + MD5_T
[t
];
165 SA
= ROTATE_LEFT (SA
, MD5_S
[i
][j
& 3]);
175 for (i
= 0; i
< 4; i
++) {
176 Md5Ctx
->States
[i
] += S
[i
];
184 IN CONST UINT8
*Data
,
191 GC_TODO: Add function description
195 Md5Ctx - GC_TODO: add argument description
196 Data - GC_TODO: add argument description
197 DataLen - GC_TODO: add argument description
201 GC_TODO: add return values
207 for (Limit
= 64 - Md5Ctx
->Count
; DataLen
>= 64 - Md5Ctx
->Count
; Limit
= 64) {
208 NetCopyMem (Md5Ctx
->M
+ Md5Ctx
->Count
, (VOID
*)Data
, Limit
);
209 MD5Transform (Md5Ctx
);
216 NetCopyMem (Md5Ctx
->M
+ Md5Ctx
->Count
, (VOID
*)Data
, DataLen
);
217 Md5Ctx
->Count
+= DataLen
;
228 GC_TODO: Add function description
232 Md5Ctx - GC_TODO: add argument description
236 EFI_SUCCESS - GC_TODO: Add description for return value
240 NetZeroMem (Md5Ctx
, sizeof (*Md5Ctx
));
243 // Set magic initialization constants.
245 Md5Ctx
->States
[0] = 0x67452301;
246 Md5Ctx
->States
[1] = 0xefcdab89;
247 Md5Ctx
->States
[2] = 0x98badcfe;
248 Md5Ctx
->States
[3] = 0x10325476;
263 GC_TODO: Add function description
267 Md5Ctx - GC_TODO: add argument description
268 Data - GC_TODO: add argument description
269 DataLen - GC_TODO: add argument description
273 EFI_SUCCESS - GC_TODO: Add description for return value
277 if (EFI_ERROR (Md5Ctx
->Status
)) {
278 return Md5Ctx
->Status
;
281 MD5UpdateBlock (Md5Ctx
, (CONST UINT8
*) Data
, DataLen
);
282 Md5Ctx
->Length
+= DataLen
;
295 GC_TODO: Add function description
299 Md5Ctx - GC_TODO: add argument description
300 HashVal - GC_TODO: add argument description
304 EFI_SUCCESS - GC_TODO: Add description for return value
310 if (Md5Ctx
->Status
== EFI_ALREADY_STARTED
) {
312 // Store Hashed value & Zeroize sensitive context information.
314 NetCopyMem (HashVal
, (UINT8
*) Md5Ctx
->States
, MD5_HASHSIZE
);
315 NetZeroMem ((UINT8
*)Md5Ctx
, sizeof (*Md5Ctx
));
320 if (EFI_ERROR (Md5Ctx
->Status
)) {
321 return Md5Ctx
->Status
;
324 PadLength
= Md5Ctx
->Count
>= 56 ? 120 : 56;
325 PadLength
-= Md5Ctx
->Count
;
326 MD5UpdateBlock (Md5Ctx
, Md5HashPadding
, PadLength
);
327 Md5Ctx
->Length
= LShiftU64 (Md5Ctx
->Length
, 3);
328 MD5UpdateBlock (Md5Ctx
, (CONST UINT8
*) &Md5Ctx
->Length
, 8);
330 NetZeroMem (Md5Ctx
->M
, sizeof (Md5Ctx
->M
));
332 Md5Ctx
->Status
= EFI_ALREADY_STARTED
;
333 return MD5Final (Md5Ctx
, HashVal
);