update file header

update function header
update a lot of variable naming
add some function header and adjust function layout


git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@6969 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
ywu21 2008-12-10 06:18:18 +00:00
parent 02aa4e52ed
commit 242354a22e
1 changed files with 116 additions and 78 deletions

View File

@ -1,7 +1,7 @@
/** @file /** @file
Implementation of MD5 algorithm Implementation of MD5 algorithm.
Copyright (c) 2004 - 2008, Intel Corporation Copyright (c) 2004 - 2008, Intel Corporation.<BR>
All rights reserved. This program and the accompanying materials All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License 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 which accompanies this distribution. The full text of the license may be found at
@ -10,33 +10,25 @@ http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
Md5.c
Abstract:
Implementation of MD5 algorithm
**/ **/
#include "Md5.h" #include "Md5.h"
CONST UINT32 MD5_K[][2] = { CONST UINT32 Md5_Data[][2] = {
{ 0, 1 }, { 0, 1 },
{ 1, 5 }, { 1, 5 },
{ 5, 3 }, { 5, 3 },
{ 0, 7 } { 0, 7 }
}; };
CONST UINT32 MD5_S[][4] = { CONST UINT32 Md5_S[][4] = {
{ 7, 22, 17, 12 }, { 7, 22, 17, 12 },
{ 5, 20, 14, 9 }, { 5, 20, 14, 9 },
{ 4, 23, 16 ,11 }, { 4, 23, 16 ,11 },
{ 6, 21, 15, 10 }, { 6, 21, 15, 10 },
}; };
CONST UINT32 MD5_T[] = { CONST UINT32 Md5_T[] = {
0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE, 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE,
0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501, 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,
0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE, 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE,
@ -80,30 +72,87 @@ CONST UINT8 Md5HashPadding[] =
// //
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
#define SA S[j & 3] #define SA MedStates[Index2 & 3]
#define SB S[(j + 1) & 3] #define SB MedStates[(Index2 + 1) & 3]
#define SC S[(j + 2) & 3] #define SC MedStates[(Index2 + 2) & 3]
#define SD S[(j + 3) & 3] #define SD MedStates[(Index2 + 3) & 3]
// /**
// TF1, TF2, TF3, TF4 are basic MD5 transform functions Tf1 is one basic MD5 transform function.
//
UINT32 TF1 (UINT32 A, UINT32 B, UINT32 C) @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
)
{ {
return (A & B) | (~A & C); return (A & B) | (~A & C);
} }
UINT32 TF2 (UINT32 A, UINT32 B, UINT32 C) /**
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
)
{ {
return (A & C) | (B & ~C); return (A & C) | (B & ~C);
} }
UINT32 TF3 (UINT32 A, UINT32 B, UINT32 C) /**
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
)
{ {
return A ^ B ^ C; return A ^ B ^ C;
} }
UINT32 TF4 (UINT32 A, UINT32 B, UINT32 C) /**
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
)
{ {
return B ^ (A | ~C); return B ^ (A | ~C);
} }
@ -116,57 +165,54 @@ UINT32
IN UINT32 C IN UINT32 C
); );
CONST MD5_TRANSFORM_FUNC MD5_F[] = { CONST MD5_TRANSFORM_FUNC Md5_F[] = {
TF1, Tf1,
TF2, Tf2,
TF3, Tf3,
TF4 Tf4
}; };
/** /**
Perform the MD5 transform on 64 bytes data segment Perform the MD5 transform on 64 bytes data segment.
@param Md5Ctx[in] it includes the data segment for Md5 transform
@retval NONE.
@param[in] Md5Ctx It includes the data segment for Md5 transform.
**/ **/
VOID VOID
MD5Transform ( MD5Transform (
IN MD5_CTX *Md5Ctx IN MD5_CTX *Md5Ctx
) )
{ {
UINT32 i; UINT32 Index1;
UINT32 j; UINT32 Index2;
UINT32 S[MD5_HASHSIZE >> 2]; UINT32 MedStates[MD5_HASHSIZE >> 2];
UINT32 *X; UINT32 *Data;
UINT32 k; UINT32 IndexD;
UINT32 t; UINT32 IndexT;
X = (UINT32 *) Md5Ctx->M; Data = (UINT32 *) Md5Ctx->M;
// //
// Copy MD5 states to S // Copy MD5 states to MedStates
// //
CopyMem (S, Md5Ctx->States, MD5_HASHSIZE); CopyMem (MedStates, Md5Ctx->States, MD5_HASHSIZE);
t = 0; IndexT = 0;
for (i = 0; i < 4; i++) { for (Index1 = 0; Index1 < 4; Index1++) {
k = MD5_K[i][0]; IndexD = Md5_Data[Index1][0];
for (j = 16; j > 0; j--) { for (Index2 = 16; Index2 > 0; Index2--) {
SA += (*MD5_F[i]) (SB, SC, SD) + X[k] + MD5_T[t]; SA += (*Md5_F[Index1]) (SB, SC, SD) + Data[IndexD] + Md5_T[IndexT];
SA = ROTATE_LEFT (SA, MD5_S[i][j & 3]); SA = ROTATE_LEFT (SA, Md5_S[Index1][Index2 & 3]);
SA += SB; SA += SB;
k += MD5_K[i][1]; IndexD += Md5_Data[Index1][1];
k &= 15; IndexD &= 15;
t++; IndexT++;
} }
} }
for (i = 0; i < 4; i++) { for (Index1 = 0; Index1 < 4; Index1++) {
Md5Ctx->States[i] += S[i]; Md5Ctx->States[Index1] += MedStates[Index1];
} }
} }
@ -178,14 +224,10 @@ MD5Transform (
All of Md5 code generated for the sequential 64-bytes data segaments are be All of Md5 code generated for the sequential 64-bytes data segaments are be
accumulated in MD5Final() function. accumulated in MD5Final() function.
@param Md5Ctx[in] the data structure of storing the original data @param[in] Md5Ctx The data structure of storing the original data
segment and the final result. segment and the final result.
@param[in] Data The data wanted to be transformed.
@param Data[in] the data wanted to be transformed @param[in] DataLen The length of data.
@param DataLen[in] the length of data
@retval NONE.
**/ **/
VOID VOID
MD5UpdateBlock ( MD5UpdateBlock (
@ -212,10 +254,9 @@ MD5UpdateBlock (
/** /**
Initialize four 32-bits chaining variables and use them to do the Md5 transform. Initialize four 32-bits chaining variables and use them to do the Md5 transform.
@param Md5Ctx[in] the data structure of Md5 @param[in] Md5Ctx The data structure of Md5.
@retval EFI_SUCCESS initialization is ok
@retval EFI_SUCCESS Initialization is ok.
**/ **/
EFI_STATUS EFI_STATUS
MD5Init ( MD5Init (
@ -238,15 +279,13 @@ MD5Init (
/** /**
the external interface of Md5 algorithm the external interface of Md5 algorithm
@param Md5Ctx[in] the data structure of storing the original data @param[in] Md5Ctx The data structure of storing the original data
segment and the final result. segment and the final result.
@param[in] Data The data wanted to be transformed.
@param[in] DataLen The length of data.
@param Data[in] the data wanted to be transformed. @retval EFI_SUCCESS The transform is ok.
@retval Others Some unexpected errors happened.
@param DataLen[in] the length of data.
@retval EFI_SUCCESS the transform is ok.
**/ **/
EFI_STATUS EFI_STATUS
MD5Update ( MD5Update (
@ -265,16 +304,15 @@ MD5Update (
} }
/** /**
accumulate the MD5 value of every data segment and generate the finial Accumulate the MD5 value of every data segment and generate the finial
result according to MD5 algorithm result according to MD5 algorithm.
@param Md5Ctx[in] the data structure of storing the original data @param[in] Md5Ctx The data structure of storing the original data
segment and the final result. segment and the final result.
@param[out] HashVal The final 128-bits output.
@param HashVal[out] the final 128-bits output. @retval EFI_SUCCESS The transform is ok.
@retval Others Some unexpected errors happened.
@retval EFI_SUCCESS the transform is ok.
**/ **/
EFI_STATUS EFI_STATUS
MD5Final ( MD5Final (