2008-04-17 10:28:51 +02:00
|
|
|
/** @file
|
2008-12-10 07:18:18 +01:00
|
|
|
Implementation of MD5 algorithm.
|
2007-12-24 03:20:21 +01:00
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
Copyright (c) 2004 - 2008, Intel Corporation.<BR>
|
2008-01-22 09:07:35 +01:00
|
|
|
All rights reserved. This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
2007-12-24 03:20:21 +01:00
|
|
|
|
2008-04-17 10:28:51 +02:00
|
|
|
**/
|
2007-12-24 03:20:21 +01:00
|
|
|
|
|
|
|
#include "Md5.h"
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
CONST UINT32 Md5_Data[][2] = {
|
2007-12-24 03:20:21 +01:00
|
|
|
{ 0, 1 },
|
|
|
|
{ 1, 5 },
|
|
|
|
{ 5, 3 },
|
|
|
|
{ 0, 7 }
|
|
|
|
};
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
CONST UINT32 Md5_S[][4] = {
|
2007-12-24 03:20:21 +01:00
|
|
|
{ 7, 22, 17, 12 },
|
|
|
|
{ 5, 20, 14, 9 },
|
|
|
|
{ 4, 23, 16 ,11 },
|
|
|
|
{ 6, 21, 15, 10 },
|
|
|
|
};
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
CONST UINT32 Md5_T[] = {
|
2007-12-24 03:20:21 +01:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2008-10-30 07:05:06 +01:00
|
|
|
CONST UINT8 Md5HashPadding[] =
|
2007-12-24 03:20:21 +01:00
|
|
|
{
|
|
|
|
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))))
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
#define SA MedStates[Index2 & 3]
|
|
|
|
#define SB MedStates[(Index2 + 1) & 3]
|
|
|
|
#define SC MedStates[(Index2 + 2) & 3]
|
|
|
|
#define SD MedStates[(Index2 + 3) & 3]
|
2007-12-24 03:20:21 +01:00
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
/**
|
|
|
|
Tf1 is one basic MD5 transform function.
|
|
|
|
|
|
|
|
@param[in] A A 32-bit quantity.
|
|
|
|
@param[in] B A 32-bit quantity.
|
|
|
|
@param[in] C A 32-bit quantity.
|
|
|
|
|
|
|
|
@return Output was produced as a 32-bit quantity based on the
|
|
|
|
three 32-bit input quantity.
|
|
|
|
**/
|
|
|
|
UINT32
|
|
|
|
Tf1 (
|
|
|
|
UINT32 A,
|
|
|
|
UINT32 B,
|
|
|
|
UINT32 C
|
|
|
|
)
|
2007-12-24 03:20:21 +01:00
|
|
|
{
|
|
|
|
return (A & B) | (~A & C);
|
|
|
|
}
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
/**
|
|
|
|
Tf2 is one basic MD5 transform function.
|
|
|
|
|
|
|
|
@param[in] A A 32-bit quantity.
|
|
|
|
@param[in] B A 32-bit quantity.
|
|
|
|
@param[in] C A 32-bit quantity.
|
|
|
|
|
|
|
|
@return Output was produced as a 32-bit quantity based on the
|
|
|
|
three 32-bit input quantity.
|
|
|
|
**/
|
|
|
|
UINT32
|
|
|
|
Tf2 (
|
|
|
|
UINT32 A,
|
|
|
|
UINT32 B,
|
|
|
|
UINT32 C
|
|
|
|
)
|
2007-12-24 03:20:21 +01:00
|
|
|
{
|
|
|
|
return (A & C) | (B & ~C);
|
|
|
|
}
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
/**
|
|
|
|
Tf3 is one basic MD5 transform function.
|
|
|
|
|
|
|
|
@param[in] A A 32-bit quantity.
|
|
|
|
@param[in] B A 32-bit quantity.
|
|
|
|
@param[in] C A 32-bit quantity.
|
|
|
|
|
|
|
|
@return Output was produced as a 32-bit quantity based on the
|
|
|
|
three 32-bit input quantity.
|
|
|
|
**/
|
|
|
|
UINT32
|
|
|
|
Tf3 (
|
|
|
|
UINT32 A,
|
|
|
|
UINT32 B,
|
|
|
|
UINT32 C
|
|
|
|
)
|
2007-12-24 03:20:21 +01:00
|
|
|
{
|
|
|
|
return A ^ B ^ C;
|
|
|
|
}
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
/**
|
|
|
|
Tf4 is one basic MD5 transform function.
|
|
|
|
|
|
|
|
@param[in] A A 32-bit quantity.
|
|
|
|
@param[in] B A 32-bit quantity.
|
|
|
|
@param[in] C A 32-bit quantity.
|
|
|
|
|
|
|
|
@return Output was produced as a 32-bit quantity based on the
|
|
|
|
three 32-bit input quantity.
|
|
|
|
**/
|
|
|
|
UINT32
|
|
|
|
Tf4 (
|
|
|
|
UINT32 A,
|
|
|
|
UINT32 B,
|
|
|
|
UINT32 C
|
|
|
|
)
|
2007-12-24 03:20:21 +01:00
|
|
|
{
|
|
|
|
return B ^ (A | ~C);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef
|
|
|
|
UINT32
|
|
|
|
(*MD5_TRANSFORM_FUNC) (
|
|
|
|
IN UINT32 A,
|
|
|
|
IN UINT32 B,
|
|
|
|
IN UINT32 C
|
|
|
|
);
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
CONST MD5_TRANSFORM_FUNC Md5_F[] = {
|
|
|
|
Tf1,
|
|
|
|
Tf2,
|
|
|
|
Tf3,
|
|
|
|
Tf4
|
2007-12-24 03:20:21 +01:00
|
|
|
};
|
|
|
|
|
2008-04-17 10:28:51 +02:00
|
|
|
/**
|
2008-12-10 07:18:18 +01:00
|
|
|
Perform the MD5 transform on 64 bytes data segment.
|
2008-04-17 10:28:51 +02:00
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
@param[in] Md5Ctx It includes the data segment for Md5 transform.
|
2008-04-17 10:28:51 +02:00
|
|
|
**/
|
2007-12-24 03:20:21 +01:00
|
|
|
VOID
|
|
|
|
MD5Transform (
|
|
|
|
IN MD5_CTX *Md5Ctx
|
|
|
|
)
|
|
|
|
{
|
2008-12-10 07:18:18 +01:00
|
|
|
UINT32 Index1;
|
|
|
|
UINT32 Index2;
|
|
|
|
UINT32 MedStates[MD5_HASHSIZE >> 2];
|
|
|
|
UINT32 *Data;
|
|
|
|
UINT32 IndexD;
|
|
|
|
UINT32 IndexT;
|
2007-12-24 03:20:21 +01:00
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
Data = (UINT32 *) Md5Ctx->M;
|
2007-12-24 03:20:21 +01:00
|
|
|
|
|
|
|
//
|
2008-12-10 07:18:18 +01:00
|
|
|
// Copy MD5 states to MedStates
|
2007-12-24 03:20:21 +01:00
|
|
|
//
|
2008-12-10 07:18:18 +01:00
|
|
|
CopyMem (MedStates, Md5Ctx->States, MD5_HASHSIZE);
|
|
|
|
|
|
|
|
IndexT = 0;
|
|
|
|
for (Index1 = 0; Index1 < 4; Index1++) {
|
|
|
|
IndexD = Md5_Data[Index1][0];
|
|
|
|
for (Index2 = 16; Index2 > 0; Index2--) {
|
|
|
|
SA += (*Md5_F[Index1]) (SB, SC, SD) + Data[IndexD] + Md5_T[IndexT];
|
|
|
|
SA = ROTATE_LEFT (SA, Md5_S[Index1][Index2 & 3]);
|
2007-12-24 03:20:21 +01:00
|
|
|
SA += SB;
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
IndexD += Md5_Data[Index1][1];
|
|
|
|
IndexD &= 15;
|
2007-12-24 03:20:21 +01:00
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
IndexT++;
|
2007-12-24 03:20:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
for (Index1 = 0; Index1 < 4; Index1++) {
|
|
|
|
Md5Ctx->States[Index1] += MedStates[Index1];
|
2007-12-24 03:20:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-17 10:28:51 +02:00
|
|
|
/**
|
|
|
|
Copy data segment into the M field of MD5_CTX structure for later transform.
|
|
|
|
If the length of data segment is larger than 64 bytes, then does the transform
|
|
|
|
immediately and the generated Md5 code is stored in the States field of MD5_CTX
|
|
|
|
data struct for later accumulation.
|
|
|
|
All of Md5 code generated for the sequential 64-bytes data segaments are be
|
|
|
|
accumulated in MD5Final() function.
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
@param[in] Md5Ctx The data structure of storing the original data
|
2008-04-17 10:28:51 +02:00
|
|
|
segment and the final result.
|
2008-12-10 07:18:18 +01:00
|
|
|
@param[in] Data The data wanted to be transformed.
|
|
|
|
@param[in] DataLen The length of data.
|
2008-04-17 10:28:51 +02:00
|
|
|
**/
|
2007-12-24 03:20:21 +01:00
|
|
|
VOID
|
|
|
|
MD5UpdateBlock (
|
|
|
|
IN MD5_CTX *Md5Ctx,
|
|
|
|
IN CONST UINT8 *Data,
|
|
|
|
IN UINTN DataLen
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINTN Limit;
|
|
|
|
|
|
|
|
for (Limit = 64 - Md5Ctx->Count; DataLen >= 64 - Md5Ctx->Count; Limit = 64) {
|
2008-02-14 10:40:22 +01:00
|
|
|
CopyMem (Md5Ctx->M + Md5Ctx->Count, (VOID *)Data, Limit);
|
2007-12-24 03:20:21 +01:00
|
|
|
MD5Transform (Md5Ctx);
|
|
|
|
|
|
|
|
Md5Ctx->Count = 0;
|
|
|
|
Data += Limit;
|
|
|
|
DataLen -= Limit;
|
|
|
|
}
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
CopyMem (Md5Ctx->M + Md5Ctx->Count, (VOID *)Data, DataLen);
|
2007-12-24 03:20:21 +01:00
|
|
|
Md5Ctx->Count += DataLen;
|
|
|
|
}
|
|
|
|
|
2008-04-17 10:28:51 +02:00
|
|
|
/**
|
|
|
|
Initialize four 32-bits chaining variables and use them to do the Md5 transform.
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
@param[in] Md5Ctx The data structure of Md5.
|
2008-04-17 10:28:51 +02:00
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
@retval EFI_SUCCESS Initialization is ok.
|
2008-04-17 10:28:51 +02:00
|
|
|
**/
|
2007-12-24 03:20:21 +01:00
|
|
|
EFI_STATUS
|
|
|
|
MD5Init (
|
|
|
|
IN MD5_CTX *Md5Ctx
|
|
|
|
)
|
|
|
|
{
|
2008-02-14 10:40:22 +01:00
|
|
|
ZeroMem (Md5Ctx, sizeof (*Md5Ctx));
|
2007-12-24 03:20:21 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// Set magic initialization constants.
|
|
|
|
//
|
|
|
|
Md5Ctx->States[0] = 0x67452301;
|
|
|
|
Md5Ctx->States[1] = 0xefcdab89;
|
|
|
|
Md5Ctx->States[2] = 0x98badcfe;
|
|
|
|
Md5Ctx->States[3] = 0x10325476;
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-04-17 10:28:51 +02:00
|
|
|
/**
|
|
|
|
the external interface of Md5 algorithm
|
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
@param[in] Md5Ctx The data structure of storing the original data
|
2008-04-17 10:28:51 +02:00
|
|
|
segment and the final result.
|
2008-12-10 07:18:18 +01:00
|
|
|
@param[in] Data The data wanted to be transformed.
|
|
|
|
@param[in] DataLen The length of data.
|
2008-04-17 10:28:51 +02:00
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
@retval EFI_SUCCESS The transform is ok.
|
|
|
|
@retval Others Some unexpected errors happened.
|
2008-04-17 10:28:51 +02:00
|
|
|
**/
|
2007-12-24 03:20:21 +01:00
|
|
|
EFI_STATUS
|
|
|
|
MD5Update (
|
|
|
|
IN MD5_CTX *Md5Ctx,
|
|
|
|
IN VOID *Data,
|
|
|
|
IN UINTN DataLen
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (EFI_ERROR (Md5Ctx->Status)) {
|
|
|
|
return Md5Ctx->Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
MD5UpdateBlock (Md5Ctx, (CONST UINT8 *) Data, DataLen);
|
|
|
|
Md5Ctx->Length += DataLen;
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-04-17 10:28:51 +02:00
|
|
|
/**
|
2008-12-10 07:18:18 +01:00
|
|
|
Accumulate the MD5 value of every data segment and generate the finial
|
|
|
|
result according to MD5 algorithm.
|
2008-04-17 10:28:51 +02:00
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
@param[in] Md5Ctx The data structure of storing the original data
|
2008-04-17 10:28:51 +02:00
|
|
|
segment and the final result.
|
2008-12-10 07:18:18 +01:00
|
|
|
@param[out] HashVal The final 128-bits output.
|
2008-04-17 10:28:51 +02:00
|
|
|
|
2008-12-10 07:18:18 +01:00
|
|
|
@retval EFI_SUCCESS The transform is ok.
|
|
|
|
@retval Others Some unexpected errors happened.
|
2008-04-17 10:28:51 +02:00
|
|
|
**/
|
2007-12-24 03:20:21 +01:00
|
|
|
EFI_STATUS
|
|
|
|
MD5Final (
|
|
|
|
IN MD5_CTX *Md5Ctx,
|
|
|
|
OUT UINT8 *HashVal
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINTN PadLength;
|
|
|
|
|
|
|
|
if (Md5Ctx->Status == EFI_ALREADY_STARTED) {
|
|
|
|
//
|
|
|
|
// Store Hashed value & Zeroize sensitive context information.
|
|
|
|
//
|
2008-02-14 10:40:22 +01:00
|
|
|
CopyMem (HashVal, (UINT8 *) Md5Ctx->States, MD5_HASHSIZE);
|
|
|
|
ZeroMem ((UINT8 *)Md5Ctx, sizeof (*Md5Ctx));
|
2007-12-24 03:20:21 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
ZeroMem (Md5Ctx->M, sizeof (Md5Ctx->M));
|
2007-12-24 03:20:21 +01:00
|
|
|
Md5Ctx->Length = 0;
|
|
|
|
Md5Ctx->Status = EFI_ALREADY_STARTED;
|
|
|
|
return MD5Final (Md5Ctx, HashVal);
|
|
|
|
}
|
|
|
|
|