This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Crypto.c
373 lines (318 loc) · 10.3 KB
/
Crypto.c
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/PrintLib.h>
#include <Library/BaseLib.h>
#include <Library/ShellCEntryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>
#include <stdlib.h>
#include <Protocol/RuntimeCrypt.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/BaseCryptLib.h>
//
// Max Known Digest Size is SHA512 Output (64 bytes) by far
//
#define MAX_DIGEST_SIZE 64
//
// Convert CHAR8 Ascii to CHAR16 Unicode
//
extern VOID AsciiToUnicodeSize( CHAR8 *String, UINT32 length, CHAR16 *UniString);
//
// Message string for digest validation
//
extern EFI_GUID gEfiRuntimeCryptProtocolGuid;
//
// The Initialization Vector for Aes-128 CBC Encryption
//
CONST UINT8 Aes128CbcIvec[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
//
// Convert UINT8(unsigned char) to CHAR8(char)
//
VOID
UintToCharSize( UINT8 *UintStr,
UINT32 length,
CHAR8 *CharStr)
{
CHAR8 Convert[16];
int len = length;
while (!(*UintStr == '\0' && *(UintStr+1) == '\0') && len > 0) {
AsciiSPrint(Convert, 1024, "%02x", 0xff & *(UintStr++));
AsciiStrCatS(CharStr, 1024, Convert);
len--;
}
*CharStr = '\0';
AsciiPrint("\n\n[Debug] The Converted Str: %s", CharStr);
}
//
// Sha256 Encrytion: Hex in Unicode format will be returned to Buffer
//
EFI_STATUS
Sha256CryptoData(
IN CHAR8 *HashData,
OUT CHAR16 *Buffer,
OUT UINT8 *Record
)
{
EFI_RUNTIME_CRYPT_PROTOCOL *mCryptProtocol = NULL;
EFI_STATUS Status;
UINT8 Digest[MAX_DIGEST_SIZE];
UINTN CtxSize;
VOID *HashCtx;
UINTN DataSize;
UINTN Index;
CHAR16 Convert[128];
DataSize = AsciiStrLen (HashData);
Status = gBS->LocateProtocol(
&gEfiRuntimeCryptProtocolGuid,
NULL,
(VOID **) &mCryptProtocol);
if (EFI_ERROR(Status)) {
Print(L"Can't find the runtime cryptographic protocol\n");
return Status;
}
ZeroMem (Digest, MAX_DIGEST_SIZE);
CtxSize = mCryptProtocol->Sha256GetContextSize ();
HashCtx = AllocatePool (CtxSize);
CHAR16 PrintBuffer[2048];
AsciiToUnicodeSize(HashData, 2048, PrintBuffer);
Print(L"[Debug] Data to be Hashed: %s\n", PrintBuffer);
Status = mCryptProtocol->Sha256Init (HashCtx);
if (!Status) {
Print (L"[Fail] Sha256 Init Falied [%d]\n", Status);
return EFI_ABORTED;
}
Status = mCryptProtocol->Sha256Update (HashCtx, HashData, DataSize);
if (!Status) {
Print (L"[Fail] Sha256 Update Falied [%d]\n", Status);
return EFI_ABORTED;
}
Status = mCryptProtocol->Sha256Final (HashCtx, Digest);
if (!Status) {
Print (L"[Fail] Sha256 Final Falied [%d]\n", Status);
return EFI_ABORTED;
}
Print(L"[Debug] The Sha256 EncrptData:\n");
for (Index = 0; Index < SHA256_DIGEST_SIZE; Index++) {
Print (L"%2X ",Digest[Index]);
*(Record + Index) = Digest[Index];
UnicodeSPrint(Convert, sizeof(Convert), L"%02x", 0xff & Digest[Index]);
StrCatS(Buffer, 4096, Convert);
}
Print (L"\n");
FreePool (HashCtx);
return EFI_SUCCESS;
}
EFI_STATUS
GenerateRsaCbcKey (
IN UINT64 Material,
OUT UINT8 *AesCbcKey
)
{
EFI_STATUS Status;
CHAR16 Buffer[4096];
CHAR8 HashBuf[2048];
UINT8 Digest[64];
ZeroMem (Buffer, sizeof (Buffer));
ZeroMem (HashBuf, sizeof (HashBuf));
AsciiSPrint(HashBuf, sizeof(Material)*4, "%d", Material);
Status = Sha256CryptoData(HashBuf, Buffer, Digest);
// retrieve the first 16 bytes of the sha256 as aes key
for (int Index = 0; Index < 16; Index++){
AesCbcKey[Index] = Digest[Index];
}
Print(L"The 16-bit key AES Encyption...\n");
for (int Index = 0; Index < 16; Index++){
Print(L"%02x ", AesCbcKey[Index]);
}
Print(L"\n\n[Debug] The Size of AesCbcKey: %d bytes\n", sizeof(AesCbcKey));
Print(L"[Debug] The Last Byte in AesCbcKey: %02x \n", AesCbcKey[16]);
// Print(L"Output Hex value: %s\n", Buffer);
return Status;
}
EFI_STATUS
AesCryptoData (
IN UINT64 Material,
IN CHAR8 *CryptData,
OUT UINT8 *RsaBuf,
IN UINTN Size
)
{
VOID *AesCtx;
BOOLEAN Result;
UINT8 Decrypt[256];
UINT8 AesCbcKey[16];
EFI_STATUS Status;
CHAR16 OutputBuffer[1024];
ZeroMem(Decrypt, sizeof (Decrypt));
ZeroMem(AesCbcKey, sizeof (AesCbcKey));
AesCtx = AllocatePool (1024);
Print(L"[Debug] Sizeof AesCbcKey: %d\n", sizeof(AesCbcKey));
Status = GenerateRsaCbcKey ( Material, AesCbcKey );
if (EFI_ERROR(Status)){
Print(L"[Fail] Get AesCbcKey Failed: %d\n", Status);
return Status;
}
Print(L"[Debug] After Sizeof AesCbcKey: %d\n[Debug] ", sizeof(AesCbcKey));
for (int Index = 0; Index < 16; Index++){
Print(L"%02x ", AesCbcKey[Index]);
}
Result = AesInit(AesCtx, AesCbcKey, 128);
if (!Result) {
Print (L"[Fail] AES Init\n");
return EFI_ABORTED;
}
// Extend the lenth s.t. 16 | StrLen(HashBuf)
UINT8 Redudency = 16 - ((AsciiStrLen(CryptData) % 16));
for (int index = 0 ; index < Redudency; index++){
AsciiStrCatS(CryptData, 1024, "=");
}
// Examine the Correctness of Prerequisite
if (AesCtx == NULL || RsaBuf == NULL || (AsciiStrLen(CryptData) % 16) != 0) {
Print(L"AesCtx or Output or AsciiStrLen %d wrong\n", AsciiStrLen(CryptData));
}
if ( Aes128CbcIvec == NULL || RsaBuf == NULL) {
Print(L"The Ivec or Output not right\n");
}
// [Debug] Dump the Data to be Encrypted
AsciiToUnicodeSize(CryptData, 2048, OutputBuffer);
Print(L"\n[Debug] Data encrypted by Aes: %s\n", OutputBuffer);
Print(L"\n[Debug] Data encrypted by Aes in Hex\n");
for (int Tag = 0; Tag < Size; Tag++) {
Print(L"%02x ", CryptData[Tag]);
}
// Status = StrnCpyS(DestKey, 1024, Buffer, 16);
// Print(L"AES Encryption Key: %s\n", Key);
// CHAR16 PrintBuffer[2048];
// ZeroMem (PrintBuffer, 2048);
Print(L"\n\n[Debug] AES Encryption CBC Mode...");
Result = AesCbcEncrypt( AesCtx,
(UINT8*)CryptData,
AsciiStrLen(CryptData),
Aes128CbcIvec,
RsaBuf );
if (!Result) {
Print (L"[Fail] AES Cbc Encrypt \n");
return EFI_ABORTED;
}
Print(L"\n[Debug] The AES-128 CBC Encryption OutPut...\n");
for (int Tag = 0; Tag < Size; Tag++) {
Print(L"%02x ", RsaBuf[Tag]);
}
// Check the Aes Decryption
Print(L"\n\n[Debug] AES Decryption Check...\n");
Result = AesCbcDecrypt (AesCtx,
RsaBuf,
AsciiStrLen(CryptData),
Aes128CbcIvec,
Decrypt);
if (!Result) {
Print (L"[Fail] Unable to performa Aes Decyption\n");
return EFI_ABORTED;
}
Print(L"[Debug] Data decrypted by Aes in Hex\n");
for (int Tag = 0; Tag < Size; Tag++) {
Print(L"%02x ", Decrypt[Tag]);
}
return Status;
}
EFI_STATUS
AesDecryptoData (
IN UINT64 Nounce,
IN CHAR8 *RecvBuffer,
OUT UINT8 *DecryptData
)
{
VOID *AesCtx;
BOOLEAN Result;
UINT8 AesCbcKey[16];
EFI_STATUS Status = EFI_SUCCESS;
AesCtx = AllocatePool (1024);
Status = GenerateRsaCbcKey ( Nounce, AesCbcKey );
if (EFI_ERROR(Status)){
Print(L"[Fail] Get AesCbcKey for Decryption Failed: %d\n", Status);
return Status;
}
Result = AesInit(AesCtx, AesCbcKey, 128);
if (!Result) {
Print (L"[Fail] AesCtx for Decryption Init\n");
return EFI_ABORTED;
}
//
// Clean the Received Data (The last character is +)
//
CHAR8 Clean[64];
CHAR8 *LenEnd = AsciiStrStr(RecvBuffer, (CHAR8*)"+");
UINTN Len = LenEnd - RecvBuffer;
Print(L"[Debug] The Read-in Len is : %d\n", Len);
AsciiStrnCpyS(Clean, 128, RecvBuffer, Len);
Result = AesCbcDecrypt (AesCtx,
(UINT8*)Clean,
AsciiStrLen(Clean),
Aes128CbcIvec,
DecryptData);
if (!Result) {
CHAR16 PrintBuffer[64];
AsciiToUnicodeSize(RecvBuffer, 128, PrintBuffer);
Print (L"[Fail] Aes Decyption Failed for %s\n", PrintBuffer);
return EFI_ABORTED;
}
Print(L"[Info] Data Decrypted by Aes in Hex\n");
for (int Tag = 0; Tag < 64; Tag++) {
Print(L"%02x ", DecryptData[Tag]);
}
return Status;
}
//
// Rsa Encryption Implementation Wrapper with OpenSSL
//
EFI_STATUS
RsaEncryptoData (
IN VOID *RsaCtx,
IN CHAR8 *DataBuffer,
OUT UINT8 *EncryptData
)
{
UINTN KeySize;
UINT8 *ExponentKey;
UINT8 *ModulusKey;
EFI_STATUS Status;
//
// Assert RsaCtx != NULL
//
if (RsaCtx == NULL) {
Print(L"[Fail] Cannot Generate Rsa Ctx...\n");
}
//
// Retrieve the Tag-designated Rsa key N/E from established RsaCtx
//
KeySize = 0;
Status = RsaGetKey(RsaCtx, RsaKeyN, NULL, &KeySize);
if (!Status || KeySize !=0) {
Print(L"[Fail] Cannot Retrieve N Key from RSaCtx\n");
}
ExponentKey = AllocatePool (KeySize);
Status = RsaGetKey(RsaCtx, RsaKeyN, ExponentKey, &KeySize);
if (!Status || KeySize !=0) {
Print(L"[Fail] Cannot Retrieve RsaKeyN Buffer from RSaCtx\n");
}
KeySize = 0;
Status = RsaGetKey(RsaCtx, RsaKeyE, NULL, &KeySize);
if (!Status || KeySize !=0) {
Print(L"[Fail] Cannot Retrieve E Key from RSaCtx\n");
}
ModulusKey = AllocatePool(KeySize);
Status = RsaGetKey(RsaCtx, RsaKeyE, ModulusKey, &KeySize);
if (!Status || KeySize !=0) {
Print(L"[Fail] Cannot Retrieve RsaKeyE Buffer from RSaCtx\n");
}
//
// Check Invalid RsaKey Components
//
if (!RsaCheckKey (RsaCtx)) {
Print(L"[Fail] RsaKey Components Invalid\n");
return EFI_ABORTED;
}
return Status;
}