summaryrefslogtreecommitdiff
path: root/EDK/MiniSetup/PasswordEncode/PasswordEncode.c
blob: 8af67e90b7c9ae1412e84083d0bd3dabfe7132e2 (plain)
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
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
//**                                                             **//
//**         (C)Copyright 2010, American Megatrends, Inc.        **//
//**                                                             **//
//**                     All Rights Reserved.                    **//
//**                                                             **//
//**   5555 Oakbrook Pkwy, Building 200,Norcross, Georgia 30093  **//
//**                                                             **//
//**                     Phone (770)-246-8600                    **//
//**                                                             **//
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
// $Archive: /Alaska/BIN/Modules/AMITSE2_0/AMITSE/PasswordEncode/PasswordEncode.c $
//
//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
// Name:				PasswordEncode.c
//
// Description:	This file contains code to handle password encoding feature
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>
//


#include "PasswordEncode.h"

INTN MemCmp(VOID* pDestination, VOID* pSource, UINTN Length);

BOOLEAN TseEfiCompareGuid (IN EFI_GUID *Guid1,IN EFI_GUID *Guid2)
{
	return !MemCmp(Guid1,Guid2,sizeof(EFI_GUID));
}

VOID
TseMemCopy (
  IN VOID   *Destination,
  IN VOID   *Source,
  IN UINTN  Length
  )
/*++

Routine Description:

  Copy Length bytes from Source to Destination.

Arguments:

  Destination - Target of copy

  Source      - Place to copy from

  Length      - Number of bytes to copy

Returns:

  None

--*/
{
  CHAR8 *Destination8;
  CHAR8 *Source8;

  if (Source < Destination) {
    Destination8  = (CHAR8 *) Destination + Length - 1;
    Source8       = (CHAR8 *) Source + Length - 1;
    while (Length--) {
      *(Destination8--) = *(Source8--);
    }
  } else {
    Destination8  = (CHAR8 *) Destination;
    Source8       = (CHAR8 *) Source;
    while (Length--) {
      *(Destination8++) = *(Source8++);
    }
  }
}


//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:  Hash
//
// Description:    Allows creating a hash of an arbitrary message digest using one or more hash algorithms
//
// Input:
//      This          Pointer to the AMI_DIGITAL_SIGNATURE_PROTOCOL instance.
//      HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
//      num_elem      Number of blocks to be passed via next argument:addr[]
//      addr[]        Pointer to array of UINT8* addresses of data blocks to be hashed
//      len           Pointer to array of integers containing length of each block listed by addr[]
//      Hash          Holds the resulting hash computed from the message.
//
// Output:    
//      EFI_SUCCESS           Hash returned successfully.
//      EFI_INVALID_PARAMETER Message or Hash is NULL
//      EFI_UNSUPPORTED       The algorithm specified by HashAlgorithm is not supported by this
//                            driver. Or extend is TRUE and the algorithm doesn't support extending the hash.
//
//<AMI_PHDR_END>
//**********************************************************************
#if TSE_HASH_PASSWORD
EFI_STATUS Hash(
	IN CONST EFI_GUID               *HashAlgorithm,
	IN UINTN                        num_elem,
	IN CONST UINT8                  *stringToHash[],
	IN CONST UINTN                  *HashSize,
	OUT UINT8                       *HashOutput
	)
{
	BOOLEAN     bSha1 = FALSE, bSha256 = FALSE;
	UINT32      HashLen=SHA256_DIGEST_SIZE;

	// Support only SHA1 & SHA256 hashes
	if(TseEfiCompareGuid((EFI_GUID*)HashAlgorithm, &gEfiHashAlgorithmSha1Guid))
	{
		bSha1 = TRUE;
		HashLen = SHA1_DIGEST_SIZE;
	}
	else 
		if(TseEfiCompareGuid((EFI_GUID*)HashAlgorithm, &gEfiHashAlgorithmSha256Guid))
		{
			bSha256 = TRUE;
			HashLen = SHA256_DIGEST_SIZE;
		}
		else
			return EFI_UNSUPPORTED;

	MemSet(HashOutput, HashLen, 0);

	if(bSha1)
		sha1_vector(num_elem, stringToHash, HashSize, HashOutput);
	else
		sha256_vector(num_elem, stringToHash, HashSize, HashOutput);

	return  EFI_SUCCESS;
}
#endif

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	PasswordEncodeLocal
//
// Description:	Encodes the input string
//
// Input:		Password : Password array to be encrypted. Encryped
//              password is returned in the same array.
//              MaxSize : Max size of Password
//
// Output:		VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT8 HashOutput[20];
VOID TsePasswordEncodeLocal( CHAR16 *Password, UINTN MaxSize);
VOID PasswordEncode( CHAR16 *Password, UINTN MaxSize)
{
#if TSE_HASH_PASSWORD
	UINTN	ii;
	EFI_STATUS Status;
	UINTN HashSize = SHA1_DIGEST_SIZE;

	if (IsPasswordSupportNonCaseSensitive ())
	{
		for ( ii = 0; ii < MaxSize/2; ii++ )
			Password[ii] = ((Password[ii]>=L'a')&&(Password[ii]<=L'z'))?(Password[ii]+L'A'-L'a'):Password[ii];
	}

	Status = Hash(&gEfiHashAlgorithmSha1Guid, TRUE, (CONST UINT8**)&Password, (CONST UINTN*)&MaxSize, (UINT8*)&HashOutput);
   if (!EFI_ERROR (Status))
   {
     	MemSet (Password, MaxSize, 0);
   	TseMemCopy ((UINT8*)Password, (UINT8*)HashOutput, HashSize);
   }
#else
   TsePasswordEncodeLocal (Password, MaxSize);
#endif
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	IsPasswordSupportNonCaseSensitive
//
// Description:	Returns SETUP_PASSWORD_NON_CASE_SENSITIVE token value
//
// Input:		void
//
// Output:		BOOLEAN
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN IsPasswordSupportNonCaseSensitive()
{
#if SETUP_PASSWORD_NON_CASE_SENSITIVE
	return TRUE;
#endif
	return FALSE;
}

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**     5555 Oakbrook Pkwy, Building 200,Norcross, Georgia 30093     **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************