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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
|
/** @file
AT authetication module for using AT DXE driver.
This driver uses the AT protocol, HECI Protocol and AT Platform Policy to implement Theft Deterrence Technology AM module.
@copyright
Copyright (c) 2004 - 2012 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.
This file contains an 'Intel Peripheral Driver' and uniquely
identified as "Intel Reference Module" and is
licensed for Intel CPUs and chipsets under the terms of your
license agreement with Intel or your vendor. This file may
be modified by the user, subject to additional terms of the
license agreement
**/
//
// External include files do NOT need to be explicitly specified in real EDKII
// environment
//
#include "AtAmHelper.h"
const CHAR8 *PEMCodes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-!=";
const CHAR8 *LineBreak = " \n\t";
/**
Convert Hex values into Base32 values
@param[in] encodedStr Array of encoded BASE values.
@param[in] encodedLen Length of encoded values.
@param[out] rawData Array of Hex strings that needs to be encoded into BASE32.
@param[out] rawDataLen Length of Hex values.
@retval EFI_BUFFER_TOO_SMALL Buffer to store encoded string too small.
@retval EFI_SUCCESS Initialization complete.
**/
EFI_STATUS
Base32Encode (
IN UINT8 *encodedStr,
IN UINTN *encodedLen,
OUT UINT8 *rawData,
OUT UINTN rawDataLen
)
{
UINTN i;
UINTN j;
UINT8 value;
INTN shift;
INTN shifttable[] = { 3, -2, 1, -4, -1, 2, -3, 0 };
if (encodedLen && *encodedLen < (rawDataLen * 8) / 5) {
DEBUG ((EFI_D_ERROR, "ATAm::Base32Encode: Buffer to store encoded string too small"));
return EFI_BUFFER_TOO_SMALL;
}
for (i = 0, j = 0; i < rawDataLen; j++) {
shift = shifttable[j % 8];
if (shift > 0) {
value = (UINT8) (rawData[i] >> shift);
} else {
value = (UINT8) (rawData[i] << (-shift));
if (i++ < rawDataLen - 1) {
value |= (rawData[i] >> (8 + shift));
}
}
value &= 0x1f;
if (25 >= value) {
value += 'A';
} else {
value += '2' - 26;
}
if (encodedStr && encodedLen) {
encodedStr[j] = value;
}
}
if (encodedStr && encodedLen) {
*encodedLen = j;
encodedStr[j] = L'\0';
}
return EFI_SUCCESS;
}
/**
Convert Base32 values into Hex values.
@param[in] encodedStr Array of Decimal numbers.
@param[out] decodedData Converted Hex values.
@retval Length of the Hex Strings that expected.
**/
INTN
Base32Decode (
IN UINT8 *encodedStr,
IN UINT8 *decodedData
)
{
UINTN i;
UINTN j;
INTN retVal;
UINT8 value;
UINTN decodedLen;
INTN shifttable[] = { 3, -2, 1, -4, -1, 2, -3, 0 };
INTN shift;
UINTN encodedLen;
retVal = 1;
encodedLen = AsciiStrLen ((char *) encodedStr);
decodedLen = (encodedLen * 5) / 8;
decodedData[0] = 0;
for (i = 0, j = 0; i < encodedLen; i++) {
if ('A' <= encodedStr[i] && 'Z' >= encodedStr[i]) {
value = encodedStr[i] - 'A';
} else if ('a' <= encodedStr[i] && 'z' >= encodedStr[i]) {
value = encodedStr[i] - 'a';
} else if ('2' <= encodedStr[i] && '7' >= encodedStr[i]) {
value = encodedStr[i] - '2' + 26;
} else {
retVal = 0;
break;
}
shift = shifttable[i % 8];
if (shift > 0) {
decodedData[j] |= (value << shift);
} else {
decodedData[j] |= (value >> (-shift));
///
/// Pack the bits that are going to fall off due to right shift in next byte
/// (left justfied) unless decoding the last character. In case of last
/// character, the extra bits are just padding to make the length of input
/// bits a multiple of 5 while encoding - so ignore them
///
if (i != encodedLen - 1) {
decodedData[++j] = (UINT8) (value << (8 + shift));
}
}
}
return retVal;
}
/**
Decimal large (BASE10) into hex value.
@param[in][out] decStr Array of Decimal numbers.
@param[out] u8Hex Converted Hex values.
@param[in] hexIndex Length of the Hex Strings that expected
@retval None
**/
VOID
DecimalToHexString (
IN OUT CHAR8 *decStr,
OUT UINT8 *u8Hex,
IN UINTN hexIndex
)
{
UINTN i;
UINTN j;
UINTN Remainder;
CHAR8 Quotient;
UINTN num;
UINTN len;
UINTN leadingZero;
leadingZero = 1;
len = AsciiStrLen ((CHAR8 *) decStr);
Remainder = decStr[0] - '0';
if (len > 1) {
Remainder = Remainder * 10 + decStr[1] - '0';
}
if (len < 3) {
u8Hex[hexIndex] = (UINT8) Remainder;
return;
}
i = 2;
j = 0;
while (i < len) {
num = Remainder * 10 + decStr[i] - '0';
Quotient = (CHAR8) (num / 256);
if (!leadingZero || Quotient) {
decStr[j++] = '0' + Quotient;
leadingZero = 0;
}
Remainder = num % 256;
i++;
}
decStr[j] = L'\0';
u8Hex[hexIndex--] = (UINT8) Remainder;
if (decStr[0] != L'\0') {
DecimalToHexString (decStr, u8Hex, hexIndex);
}
return;
}
/**
Convert the CHAR8 ASCII into CHAR16 Unicode strings.
@param[in] AsciiString Ascii String.
@param[out] UnicodeString Buffer for converted Unicode string.
@retval None
**/
VOID
Uint8ToUnicode (
IN CHAR8 *AsciiString,
OUT CHAR16 *UnicodeString
)
{
UINT8 Index;
Index = 0;
while (AsciiString[Index] != 0) {
UnicodeString[Index] = (CHAR16) AsciiString[Index];
Index++;
}
}
/**
Decode a PEM-encoded character.
@param[in] pemChar PEM-encoded character.
@retval Value orresponding to the character, or PEM_INVALID_CHARACTER if the input character could not be decoded.
**/
UINT8
DecodePEMChar (
IN UINT8 pemChar
)
{
if ((pemChar >= 'A') && (pemChar <= 'Z')) {
return pemChar - 'A';
}
if ((pemChar >= 'a') && (pemChar <= 'z')) {
return (pemChar - 'a') + 26;
}
if ((pemChar >= '0') && (pemChar <= '9')) {
return (pemChar - '0') + 52;
}
if (pemChar == '-') {
return 62;
}
if (pemChar == '!') {
return 63;
}
if (pemChar == '=') {
return PEM_PAD_CHAR;
}
DEBUG ((EFI_D_ERROR, "DecodePEMChar: Invalid character %d\n", pemChar));
return PEM_INVALID_CHAR;
}
/**
Decode character in ASCII to hexadecimal
@param[in] base16char Character in ASCII
@retval Value in hexadecimal.
**/
UINT8
DecodeBase16Char (
IN UINT8 base16char
)
{
if (base16char >= '0' && base16char <= '9') {
return base16char - '0';
} else if (base16char >= 'A' && base16char <= 'F') {
return base16char - 'A' + 0xA;
} else if (base16char >= 'a' && base16char <= 'f') {
return base16char - 'a' + 0xA;
}
return 0;
}
/**
Decode ASCII string to hexadecimal string
@param[in] pString Pointer of ASCII string
@param[in] stringLen Size of the ASCII string
@param[out] pDecodedData Pointer of hexadecimal string
@param[out] pBufLength Pointer to buffer length returned
@retval FALSE Failed to decode the string by invalid length or buffer size
@retval TRUE The hexadecimal string returned
**/
BOOLEAN
Base16Decode (
IN UINT8 *pString,
IN UINT32 stringLen,
OUT UINT8 *pDecodedData,
OUT UINT32 *pBufLength
)
{
UINT32 i;
if (stringLen % 2 != 0) {
DEBUG ((EFI_D_ERROR, "Cannot decode hexadecimal string -- invalid length\n"));
} else if (*pBufLength < stringLen / 2) {
DEBUG ((EFI_D_ERROR, "Base16 decode - output buffer too small\n"));
} else {
for (i = 0; i < (UINT32) stringLen / 2; i++) {
pDecodedData[i] = DecodeBase16Char (pString[2 * i]) << 4;
pDecodedData[i] |= DecodeBase16Char (pString[2 * i + 1]);
}
*pBufLength = i;
return TRUE;
}
return FALSE;
}
/**
Encode string to ASCII code
@param[in] pData Pointer of string to be encoded
@param[in] dataLength Size of the string to be
@param[out] pEncodedData Pointer of encoded string
@param[out] pBufLength Pointer to buffer length returned
@retval FALSE Failed to encode the string by invalid length or buffer size
@retval TRUE The encoded string returned
**/
BOOLEAN
Base16Encode (
IN UINT8 *pData,
IN UINT32 dataLength,
OUT UINT8 *pEncodedData,
OUT UINT32 *pBufLength
)
{
UINT32 i;
if (*pBufLength < 2 * dataLength) {
DEBUG ((EFI_D_ERROR, "Cannot encode data -- output buffer too small\n"));
return FALSE;
}
DEBUG ((EFI_D_ERROR, "Base16Encode dataLength : %d\n", dataLength));
for (i = 0; i < dataLength; i++) {
AsciiSPrint ((CHAR8 *) &pEncodedData[2 * i], 3 * sizeof (UINT8), "%02x", pData[i] & 0xFF);
}
DEBUG ((EFI_D_ERROR, "Base16Encode AsciiSPrint : %s\n", (char *) pEncodedData));
DEBUG ((EFI_D_ERROR, "Base16Encode Sprint : %s\n", (char *) pEncodedData));
*pBufLength = 2 * dataLength;
return TRUE;
}
/**
Decode PEM-Encoded data
@param[in] pPEMString Pointer to PEM-Encoded buffer
@param[in] lineLength Length in bytes
@param[in] pDecodedData Pointer to buffer to receive decoded bytes
@param[in] pBufLength Pointer to the size of the output buffer. Upon return this will
contain the length of the decoded data
@retval Returns status code indicating outcome of operation
**/
BOOLEAN
PEMSMSDecode (
IN UINT8 *pPEMString,
IN UINT32 lineLength,
OUT UINT8 *pDecodedData,
OUT UINT32 *pBufLength
)
{
UINT8 *pStr;
UINT32 curOutLen;
UINT8 *pOut;
UINT32 i;
UINT32 padBytes;
UINT8 val1;
UINT8 val2;
UINT8 decodedValue[4];
UINT32 ii;
UINT32 unitCount;
UINT32 decodedBytes;
pStr = pPEMString;
curOutLen = *pBufLength;
pOut = pDecodedData;
padBytes = 0;
if (pStr != NULL) {
if ((lineLength % 4) != 0) {
DEBUG ((EFI_D_ERROR, "Cannot decode PEM string -- invalid length -1\n"));
return FALSE;
}
unitCount = lineLength / 4;
for (i = 0; (i < unitCount) && (padBytes == 0); i++) {
val1 = DecodePEMChar (*pStr++);
val2 = DecodePEMChar (*pStr++);
if ((val1 == PEM_INVALID_CHAR) || (val2 == PEM_INVALID_CHAR)) {
DEBUG ((EFI_D_ERROR, "Cannot decode PEM string -- invalid character -2\n"));
return FALSE;
}
decodedValue[0] = ((val1 << 2) | (val2 >> 4));
val1 = DecodePEMChar (*pStr++);
if (val1 == PEM_INVALID_CHAR) {
DEBUG ((EFI_D_ERROR, "Cannot decode PEM string -- invalid character -3\n"));
return FALSE;
}
if (val1 != PEM_PAD_CHAR) {
decodedValue[1] = ((val2 << 4) | (val1 >> 2));
val2 = DecodePEMChar (*pStr++);
if (val2 == PEM_INVALID_CHAR) {
DEBUG ((EFI_D_ERROR, "Cannot decode PEM string -- invalid character -4\n"));
return FALSE;
}
if (val2 == PEM_PAD_CHAR) {
padBytes = 1;
} else {
decodedValue[2] = ((val1 << 6) | val2);
}
} else {
val2 = DecodePEMChar (*pStr++);
if (val2 != PEM_PAD_CHAR) {
DEBUG ((EFI_D_ERROR, "Cannot decode PEM string -- invalid encoding -4\n"));
return FALSE;
}
padBytes = 2;
}
decodedBytes = (3 - padBytes);
if (curOutLen < decodedBytes) {
DEBUG ((EFI_D_ERROR, "Cannot decode PEM string -- out buffer too small-5\n"));
return FALSE;
}
for (ii = 0; ii < decodedBytes; ii++) {
pOut[ii] = decodedValue[ii];
}
curOutLen -= decodedBytes;
pOut += decodedBytes;
}
}
*pBufLength = *pBufLength - curOutLen;
return TRUE;
}
/**
Encoded data
@param[in] pData Pointer to buffer
@param[in] dataLength Length in bytes
@param[out] pPEMString Pointer to buffer to receive encoded bytes
@param[out] pBufLength Pointer to the size of the output buffer. Upon return this will
contain the length of the encoded data
@retval Returns status code indicating outcome of operation
**/
BOOLEAN
PEMSMSEncode (
IN UINT8 *pData,
IN UINT32 dataLength,
OUT UINT8 *pPEMString,
OUT UINT32 *pBufLength
)
{
UINT32 encodeUnits;
UINT32 paddedInputLength;
UINT32 padBytes;
UINT32 outLength;
UINT8 encodeBuf[3];
UINT32 i;
UINT8 *pOctet;
UINT32 ipos;
UINT32 opos;
UINT32 index;
encodeUnits = (dataLength + 2) / 3;
paddedInputLength = encodeUnits * 3;
padBytes = paddedInputLength - dataLength;
outLength = encodeUnits * 4;
pOctet = pData;
ipos = 0;
opos = 0;
outLength = encodeUnits * 4;
if (outLength % 64) {
outLength += ((outLength / 64) + 1);
} else {
outLength += (outLength / 64);
}
outLength++;
DEBUG ((EFI_D_ERROR, "ATAM: Pbuflength: %d OutLength: %d \n", *pBufLength, outLength));
if (*pBufLength < outLength) {
DEBUG ((EFI_D_ERROR, "ATAM: Failed \n"));
return FALSE;
}
for (i = 0; i < encodeUnits; i++) {
if ((i == encodeUnits - 1) && (padBytes > 0)) {
//
// Encode last piece, with padding
//
encodeBuf[0] = *pOctet++;
encodeBuf[1] = (padBytes == 1) ? encodeBuf[1] = *pOctet++ : 0;
encodeBuf[2] = 0;
ipos += (3 - padBytes);
index = encodeBuf[0] >> 2;
pPEMString[opos++] = PEMCodes[index];
index = ((encodeBuf[0] & 0x03) << 4) | (encodeBuf[1] >> 4);
pPEMString[opos++] = PEMCodes[index];
if (padBytes == 1) {
index = ((encodeBuf[1] & 0x0f) << 2) | (encodeBuf[2] >> 6);
pPEMString[opos++] = PEMCodes[index];
pPEMString[opos++] = '=';
} else {
pPEMString[opos++] = '=';
pPEMString[opos++] = '=';
}
} else {
//
// Encode next 3 bytes
//
encodeBuf[0] = *pOctet++;
encodeBuf[1] = *pOctet++;
encodeBuf[2] = *pOctet++;
ipos += 3;
index = encodeBuf[0] >> 2;
pPEMString[opos++] = PEMCodes[index];
index = ((encodeBuf[0] & 0x03) << 4) | (encodeBuf[1] >> 4);
pPEMString[opos++] = PEMCodes[index];
index = ((encodeBuf[1] & 0x0f) << 2) | (encodeBuf[2] >> 6);
pPEMString[opos++] = PEMCodes[index];
index = encodeBuf[2] & 0x3f;
pPEMString[opos++] = PEMCodes[index];
}
}
pPEMString[opos] = 0;
*pBufLength = opos;
return TRUE;
}
#ifdef EFI_DEBUG
/**
This routine displays the debug message in ASCII
@param[in] Message Message to be displayed
@param[in] Length Length of the message
@retval None
**/
VOID
ShowBuffer (
IN UINT8 *Message,
IN UINT32 Length
)
{
UINT32 LineBreak;
UINT32 Index;
LineBreak = 0;
Index = 0;
while (Length-- > 0) {
if (LineBreak == 0) {
DEBUG ((EFI_D_ERROR, "%02x: ", (Index & 0xF0)));
}
DEBUG ((EFI_D_ERROR, "%02x ", Message[Index++]));
LineBreak++;
if (LineBreak == 16) {
DEBUG ((EFI_D_ERROR, "\n"));
LineBreak = 0;
}
if (LineBreak == 8) {
DEBUG ((EFI_D_ERROR, "- "));
}
}
DEBUG ((EFI_D_ERROR, "\n"));
return ;
}
#endif // End Of EFI_DEBUG
|