2 Implementation of MD5 algorithm.
4 Copyright (c) 2004 - 2008, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 CONST UINT32 Md5_Data
[][2] = {
24 CONST UINT32 Md5_S
[][4] = {
31 CONST UINT32 Md5_T
[] = {
32 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE,
33 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,
34 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE,
35 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821,
36 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA,
37 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8,
38 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED,
39 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A,
40 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C,
41 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70,
42 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05,
43 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665,
44 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039,
45 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1,
46 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1,
47 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391
50 CONST UINT8 Md5HashPadding
[] =
52 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 0x00, 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
71 // ROTATE_LEFT rotates x left n bits.
73 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
75 #define SA MedStates[Index2 & 3]
76 #define SB MedStates[(Index2 + 1) & 3]
77 #define SC MedStates[(Index2 + 2) & 3]
78 #define SD MedStates[(Index2 + 3) & 3]
81 Tf1 is one basic MD5 transform function.
83 @param[in] A A 32-bit quantity.
84 @param[in] B A 32-bit quantity.
85 @param[in] C A 32-bit quantity.
87 @return Output was produced as a 32-bit quantity based on the
88 three 32-bit input quantity.
97 return (A
& B
) | (~A
& C
);
101 Tf2 is one basic MD5 transform function.
103 @param[in] A A 32-bit quantity.
104 @param[in] B A 32-bit quantity.
105 @param[in] C A 32-bit quantity.
107 @return Output was produced as a 32-bit quantity based on the
108 three 32-bit input quantity.
117 return (A
& C
) | (B
& ~C
);
121 Tf3 is one basic MD5 transform function.
123 @param[in] A A 32-bit quantity.
124 @param[in] B A 32-bit quantity.
125 @param[in] C A 32-bit quantity.
127 @return Output was produced as a 32-bit quantity based on the
128 three 32-bit input quantity.
141 Tf4 is one basic MD5 transform function.
143 @param[in] A A 32-bit quantity.
144 @param[in] B A 32-bit quantity.
145 @param[in] C A 32-bit quantity.
147 @return Output was produced as a 32-bit quantity based on the
148 three 32-bit input quantity.
162 (*MD5_TRANSFORM_FUNC
) (
168 CONST MD5_TRANSFORM_FUNC Md5_F
[] = {
176 Perform the MD5 transform on 64 bytes data segment.
178 @param[in, out] Md5Ctx It includes the data segment for Md5 transform.
182 IN OUT MD5_CTX
*Md5Ctx
187 UINT32 MedStates
[MD5_HASHSIZE
>> 2];
192 Data
= (UINT32
*) Md5Ctx
->M
;
195 // Copy MD5 states to MedStates
197 CopyMem (MedStates
, Md5Ctx
->States
, MD5_HASHSIZE
);
200 for (Index1
= 0; Index1
< 4; Index1
++) {
201 IndexD
= Md5_Data
[Index1
][0];
202 for (Index2
= 16; Index2
> 0; Index2
--) {
203 SA
+= (*Md5_F
[Index1
]) (SB
, SC
, SD
) + Data
[IndexD
] + Md5_T
[IndexT
];
204 SA
= ROTATE_LEFT (SA
, Md5_S
[Index1
][Index2
& 3]);
207 IndexD
+= Md5_Data
[Index1
][1];
214 for (Index1
= 0; Index1
< 4; Index1
++) {
215 Md5Ctx
->States
[Index1
] += MedStates
[Index1
];
220 Copy data segment into the M field of MD5_CTX structure for later transform.
221 If the length of data segment is larger than 64 bytes, then does the transform
222 immediately and the generated Md5 code is stored in the States field of MD5_CTX
223 data struct for later accumulation.
224 All of Md5 code generated for the sequential 64-bytes data segaments are be
225 accumulated in MD5Final() function.
227 @param[in, out] Md5Ctx The data structure of storing the original data
228 segment and the final result.
229 @param[in] Data The data wanted to be transformed.
230 @param[in] DataLen The length of data.
234 IN OUT MD5_CTX
*Md5Ctx
,
235 IN CONST UINT8
*Data
,
241 for (Limit
= 64 - Md5Ctx
->Count
; DataLen
>= 64 - Md5Ctx
->Count
; Limit
= 64) {
242 CopyMem (Md5Ctx
->M
+ Md5Ctx
->Count
, (VOID
*)Data
, Limit
);
243 MD5Transform (Md5Ctx
);
250 CopyMem (Md5Ctx
->M
+ Md5Ctx
->Count
, (VOID
*)Data
, DataLen
);
251 Md5Ctx
->Count
+= DataLen
;
255 Initialize four 32-bits chaining variables and use them to do the Md5 transform.
257 @param[out] Md5Ctx The data structure of Md5.
259 @retval EFI_SUCCESS Initialization is ok.
266 ZeroMem (Md5Ctx
, sizeof (*Md5Ctx
));
269 // Set magic initialization constants.
271 Md5Ctx
->States
[0] = 0x67452301;
272 Md5Ctx
->States
[1] = 0xefcdab89;
273 Md5Ctx
->States
[2] = 0x98badcfe;
274 Md5Ctx
->States
[3] = 0x10325476;
280 the external interface of Md5 algorithm
282 @param[in, out] Md5Ctx The data structure of storing the original data
283 segment and the final result.
284 @param[in] Data The data wanted to be transformed.
285 @param[in] DataLen The length of data.
287 @retval EFI_SUCCESS The transform is ok.
288 @retval Others Other errors as indicated.
292 IN OUT MD5_CTX
*Md5Ctx
,
297 if (EFI_ERROR (Md5Ctx
->Status
)) {
298 return Md5Ctx
->Status
;
301 MD5UpdateBlock (Md5Ctx
, (CONST UINT8
*) Data
, DataLen
);
302 Md5Ctx
->Length
+= DataLen
;
307 Accumulate the MD5 value of every data segment and generate the finial
308 result according to MD5 algorithm.
310 @param[in, out] Md5Ctx The data structure of storing the original data
311 segment and the final result.
312 @param[out] HashVal The final 128-bits output.
314 @retval EFI_SUCCESS The transform is ok.
315 @retval Others Other errors as indicated.
319 IN OUT MD5_CTX
*Md5Ctx
,
325 if (Md5Ctx
->Status
== EFI_ALREADY_STARTED
) {
327 // Store Hashed value & Zeroize sensitive context information.
329 CopyMem (HashVal
, (UINT8
*) Md5Ctx
->States
, MD5_HASHSIZE
);
330 ZeroMem ((UINT8
*)Md5Ctx
, sizeof (*Md5Ctx
));
335 if (EFI_ERROR (Md5Ctx
->Status
)) {
336 return Md5Ctx
->Status
;
339 PadLength
= Md5Ctx
->Count
>= 56 ? 120 : 56;
340 PadLength
-= Md5Ctx
->Count
;
341 MD5UpdateBlock (Md5Ctx
, Md5HashPadding
, PadLength
);
342 Md5Ctx
->Length
= LShiftU64 (Md5Ctx
->Length
, 3);
343 MD5UpdateBlock (Md5Ctx
, (CONST UINT8
*) &Md5Ctx
->Length
, 8);
345 ZeroMem (Md5Ctx
->M
, sizeof (Md5Ctx
->M
));
347 Md5Ctx
->Status
= EFI_ALREADY_STARTED
;
348 return MD5Final (Md5Ctx
, HashVal
);