Tpm2Capability.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /** @file
  2. Implement TPM2 Capability related command.
  3. Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <IndustryStandard/UefiTcgPlatform.h>
  7. #include <Library/Tpm2CommandLib.h>
  8. #include <Library/Tpm2DeviceLib.h>
  9. #include <Library/BaseMemoryLib.h>
  10. #include <Library/BaseLib.h>
  11. #include <Library/DebugLib.h>
  12. #pragma pack(1)
  13. typedef struct {
  14. TPM2_COMMAND_HEADER Header;
  15. TPM_CAP Capability;
  16. UINT32 Property;
  17. UINT32 PropertyCount;
  18. } TPM2_GET_CAPABILITY_COMMAND;
  19. typedef struct {
  20. TPM2_RESPONSE_HEADER Header;
  21. TPMI_YES_NO MoreData;
  22. TPMS_CAPABILITY_DATA CapabilityData;
  23. } TPM2_GET_CAPABILITY_RESPONSE;
  24. typedef struct {
  25. TPM2_COMMAND_HEADER Header;
  26. TPMT_PUBLIC_PARMS Parameters;
  27. } TPM2_TEST_PARMS_COMMAND;
  28. typedef struct {
  29. TPM2_RESPONSE_HEADER Header;
  30. } TPM2_TEST_PARMS_RESPONSE;
  31. #pragma pack()
  32. #define TPMA_CC_COMMANDINDEX_MASK 0x2000FFFF
  33. /**
  34. This command returns various information regarding the TPM and its current state.
  35. The capability parameter determines the category of data returned. The property parameter
  36. selects the first value of the selected category to be returned. If there is no property
  37. that corresponds to the value of property, the next higher value is returned, if it exists.
  38. The moreData parameter will have a value of YES if there are more values of the requested
  39. type that were not returned.
  40. If no next capability exists, the TPM will return a zero-length list and moreData will have
  41. a value of NO.
  42. NOTE:
  43. To simplify this function, leave returned CapabilityData for caller to unpack since there are
  44. many capability categories and only few categories will be used in firmware. It means the caller
  45. need swap the byte order for the fields in CapabilityData.
  46. @param[in] Capability Group selection; determines the format of the response.
  47. @param[in] Property Further definition of information.
  48. @param[in] PropertyCount Number of properties of the indicated type to return.
  49. @param[out] MoreData Flag to indicate if there are more values of this type.
  50. @param[out] CapabilityData The capability data.
  51. @retval EFI_SUCCESS Operation completed successfully.
  52. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  53. **/
  54. EFI_STATUS
  55. EFIAPI
  56. Tpm2GetCapability (
  57. IN TPM_CAP Capability,
  58. IN UINT32 Property,
  59. IN UINT32 PropertyCount,
  60. OUT TPMI_YES_NO *MoreData,
  61. OUT TPMS_CAPABILITY_DATA *CapabilityData
  62. )
  63. {
  64. EFI_STATUS Status;
  65. TPM2_GET_CAPABILITY_COMMAND SendBuffer;
  66. TPM2_GET_CAPABILITY_RESPONSE RecvBuffer;
  67. UINT32 SendBufferSize;
  68. UINT32 RecvBufferSize;
  69. //
  70. // Construct command
  71. //
  72. SendBuffer.Header.tag = SwapBytes16 (TPM_ST_NO_SESSIONS);
  73. SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_GetCapability);
  74. SendBuffer.Capability = SwapBytes32 (Capability);
  75. SendBuffer.Property = SwapBytes32 (Property);
  76. SendBuffer.PropertyCount = SwapBytes32 (PropertyCount);
  77. SendBufferSize = (UINT32)sizeof (SendBuffer);
  78. SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
  79. //
  80. // send Tpm command
  81. //
  82. RecvBufferSize = sizeof (RecvBuffer);
  83. Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
  84. if (EFI_ERROR (Status)) {
  85. return Status;
  86. }
  87. if (RecvBufferSize <= sizeof (TPM2_RESPONSE_HEADER) + sizeof (UINT8)) {
  88. return EFI_DEVICE_ERROR;
  89. }
  90. //
  91. // Fail if command failed
  92. //
  93. if (SwapBytes32 (RecvBuffer.Header.responseCode) != TPM_RC_SUCCESS) {
  94. DEBUG ((DEBUG_ERROR, "Tpm2GetCapability: Response Code error! 0x%08x\r\n", SwapBytes32 (RecvBuffer.Header.responseCode)));
  95. return EFI_DEVICE_ERROR;
  96. }
  97. //
  98. // Return the response
  99. //
  100. *MoreData = RecvBuffer.MoreData;
  101. //
  102. // Does not unpack all possible property here, the caller should unpack it and note the byte order.
  103. //
  104. CopyMem (CapabilityData, &RecvBuffer.CapabilityData, RecvBufferSize - sizeof (TPM2_RESPONSE_HEADER) - sizeof (UINT8));
  105. return EFI_SUCCESS;
  106. }
  107. /**
  108. This command returns the information of TPM Family.
  109. This function parse the value got from TPM2_GetCapability and return the Family.
  110. @param[out] Family The Family of TPM. (a 4-octet character string)
  111. @retval EFI_SUCCESS Operation completed successfully.
  112. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  113. **/
  114. EFI_STATUS
  115. EFIAPI
  116. Tpm2GetCapabilityFamily (
  117. OUT CHAR8 *Family
  118. )
  119. {
  120. TPMS_CAPABILITY_DATA TpmCap;
  121. TPMI_YES_NO MoreData;
  122. EFI_STATUS Status;
  123. Status = Tpm2GetCapability (
  124. TPM_CAP_TPM_PROPERTIES,
  125. TPM_PT_FAMILY_INDICATOR,
  126. 1,
  127. &MoreData,
  128. &TpmCap
  129. );
  130. if (EFI_ERROR (Status)) {
  131. return Status;
  132. }
  133. CopyMem (Family, &TpmCap.data.tpmProperties.tpmProperty->value, 4);
  134. return EFI_SUCCESS;
  135. }
  136. /**
  137. This command returns the information of TPM manufacture ID.
  138. This function parse the value got from TPM2_GetCapability and return the TPM manufacture ID.
  139. @param[out] ManufactureId The manufacture ID of TPM.
  140. @retval EFI_SUCCESS Operation completed successfully.
  141. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  142. **/
  143. EFI_STATUS
  144. EFIAPI
  145. Tpm2GetCapabilityManufactureID (
  146. OUT UINT32 *ManufactureId
  147. )
  148. {
  149. TPMS_CAPABILITY_DATA TpmCap;
  150. TPMI_YES_NO MoreData;
  151. EFI_STATUS Status;
  152. Status = Tpm2GetCapability (
  153. TPM_CAP_TPM_PROPERTIES,
  154. TPM_PT_MANUFACTURER,
  155. 1,
  156. &MoreData,
  157. &TpmCap
  158. );
  159. if (EFI_ERROR (Status)) {
  160. return Status;
  161. }
  162. *ManufactureId = TpmCap.data.tpmProperties.tpmProperty->value;
  163. return EFI_SUCCESS;
  164. }
  165. /**
  166. This command returns the information of TPM FirmwareVersion.
  167. This function parse the value got from TPM2_GetCapability and return the TPM FirmwareVersion.
  168. @param[out] FirmwareVersion1 The FirmwareVersion1.
  169. @param[out] FirmwareVersion2 The FirmwareVersion2.
  170. @retval EFI_SUCCESS Operation completed successfully.
  171. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  172. **/
  173. EFI_STATUS
  174. EFIAPI
  175. Tpm2GetCapabilityFirmwareVersion (
  176. OUT UINT32 *FirmwareVersion1,
  177. OUT UINT32 *FirmwareVersion2
  178. )
  179. {
  180. TPMS_CAPABILITY_DATA TpmCap;
  181. TPMI_YES_NO MoreData;
  182. EFI_STATUS Status;
  183. Status = Tpm2GetCapability (
  184. TPM_CAP_TPM_PROPERTIES,
  185. TPM_PT_FIRMWARE_VERSION_1,
  186. 1,
  187. &MoreData,
  188. &TpmCap
  189. );
  190. if (EFI_ERROR (Status)) {
  191. return Status;
  192. }
  193. *FirmwareVersion1 = SwapBytes32 (TpmCap.data.tpmProperties.tpmProperty->value);
  194. Status = Tpm2GetCapability (
  195. TPM_CAP_TPM_PROPERTIES,
  196. TPM_PT_FIRMWARE_VERSION_2,
  197. 1,
  198. &MoreData,
  199. &TpmCap
  200. );
  201. if (EFI_ERROR (Status)) {
  202. return Status;
  203. }
  204. *FirmwareVersion2 = SwapBytes32 (TpmCap.data.tpmProperties.tpmProperty->value);
  205. return EFI_SUCCESS;
  206. }
  207. /**
  208. This command returns the information of the maximum value for commandSize and responseSize in a command.
  209. This function parse the value got from TPM2_GetCapability and return the max command size and response size
  210. @param[out] MaxCommandSize The maximum value for commandSize in a command.
  211. @param[out] MaxResponseSize The maximum value for responseSize in a command.
  212. @retval EFI_SUCCESS Operation completed successfully.
  213. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  214. **/
  215. EFI_STATUS
  216. EFIAPI
  217. Tpm2GetCapabilityMaxCommandResponseSize (
  218. OUT UINT32 *MaxCommandSize,
  219. OUT UINT32 *MaxResponseSize
  220. )
  221. {
  222. TPMS_CAPABILITY_DATA TpmCap;
  223. TPMI_YES_NO MoreData;
  224. EFI_STATUS Status;
  225. Status = Tpm2GetCapability (
  226. TPM_CAP_TPM_PROPERTIES,
  227. TPM_PT_MAX_COMMAND_SIZE,
  228. 1,
  229. &MoreData,
  230. &TpmCap
  231. );
  232. if (EFI_ERROR (Status)) {
  233. return Status;
  234. }
  235. *MaxCommandSize = SwapBytes32 (TpmCap.data.tpmProperties.tpmProperty->value);
  236. Status = Tpm2GetCapability (
  237. TPM_CAP_TPM_PROPERTIES,
  238. TPM_PT_MAX_RESPONSE_SIZE,
  239. 1,
  240. &MoreData,
  241. &TpmCap
  242. );
  243. if (EFI_ERROR (Status)) {
  244. return Status;
  245. }
  246. *MaxResponseSize = SwapBytes32 (TpmCap.data.tpmProperties.tpmProperty->value);
  247. return EFI_SUCCESS;
  248. }
  249. /**
  250. This command returns Returns a list of TPMS_ALG_PROPERTIES. Each entry is an
  251. algorithm ID and a set of properties of the algorithm.
  252. This function parse the value got from TPM2_GetCapability and return the list.
  253. @param[out] AlgList List of algorithm.
  254. @retval EFI_SUCCESS Operation completed successfully.
  255. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  256. **/
  257. EFI_STATUS
  258. EFIAPI
  259. Tpm2GetCapabilitySupportedAlg (
  260. OUT TPML_ALG_PROPERTY *AlgList
  261. )
  262. {
  263. TPMS_CAPABILITY_DATA TpmCap;
  264. TPMI_YES_NO MoreData;
  265. UINTN Index;
  266. EFI_STATUS Status;
  267. Status = Tpm2GetCapability (
  268. TPM_CAP_ALGS,
  269. 1,
  270. MAX_CAP_ALGS,
  271. &MoreData,
  272. &TpmCap
  273. );
  274. if (EFI_ERROR (Status)) {
  275. return Status;
  276. }
  277. CopyMem (AlgList, &TpmCap.data.algorithms, sizeof (TPML_ALG_PROPERTY));
  278. AlgList->count = SwapBytes32 (AlgList->count);
  279. if (AlgList->count > MAX_CAP_ALGS) {
  280. DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilitySupportedAlg - AlgList->count error %x\n", AlgList->count));
  281. return EFI_DEVICE_ERROR;
  282. }
  283. for (Index = 0; Index < AlgList->count; Index++) {
  284. AlgList->algProperties[Index].alg = SwapBytes16 (AlgList->algProperties[Index].alg);
  285. WriteUnaligned32 ((UINT32 *)&AlgList->algProperties[Index].algProperties, SwapBytes32 (ReadUnaligned32 ((UINT32 *)&AlgList->algProperties[Index].algProperties)));
  286. }
  287. return EFI_SUCCESS;
  288. }
  289. /**
  290. This command returns the information of TPM LockoutCounter.
  291. This function parse the value got from TPM2_GetCapability and return the LockoutCounter.
  292. @param[out] LockoutCounter The LockoutCounter of TPM.
  293. @retval EFI_SUCCESS Operation completed successfully.
  294. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  295. **/
  296. EFI_STATUS
  297. EFIAPI
  298. Tpm2GetCapabilityLockoutCounter (
  299. OUT UINT32 *LockoutCounter
  300. )
  301. {
  302. TPMS_CAPABILITY_DATA TpmCap;
  303. TPMI_YES_NO MoreData;
  304. EFI_STATUS Status;
  305. Status = Tpm2GetCapability (
  306. TPM_CAP_TPM_PROPERTIES,
  307. TPM_PT_LOCKOUT_COUNTER,
  308. 1,
  309. &MoreData,
  310. &TpmCap
  311. );
  312. if (EFI_ERROR (Status)) {
  313. return Status;
  314. }
  315. *LockoutCounter = SwapBytes32 (TpmCap.data.tpmProperties.tpmProperty->value);
  316. return EFI_SUCCESS;
  317. }
  318. /**
  319. This command returns the information of TPM LockoutInterval.
  320. This function parse the value got from TPM2_GetCapability and return the LockoutInterval.
  321. @param[out] LockoutInterval The LockoutInterval of TPM.
  322. @retval EFI_SUCCESS Operation completed successfully.
  323. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  324. **/
  325. EFI_STATUS
  326. EFIAPI
  327. Tpm2GetCapabilityLockoutInterval (
  328. OUT UINT32 *LockoutInterval
  329. )
  330. {
  331. TPMS_CAPABILITY_DATA TpmCap;
  332. TPMI_YES_NO MoreData;
  333. EFI_STATUS Status;
  334. Status = Tpm2GetCapability (
  335. TPM_CAP_TPM_PROPERTIES,
  336. TPM_PT_LOCKOUT_INTERVAL,
  337. 1,
  338. &MoreData,
  339. &TpmCap
  340. );
  341. if (EFI_ERROR (Status)) {
  342. return Status;
  343. }
  344. *LockoutInterval = SwapBytes32 (TpmCap.data.tpmProperties.tpmProperty->value);
  345. return EFI_SUCCESS;
  346. }
  347. /**
  348. This command returns the information of TPM InputBufferSize.
  349. This function parse the value got from TPM2_GetCapability and return the InputBufferSize.
  350. @param[out] InputBufferSize The InputBufferSize of TPM.
  351. the maximum size of a parameter (typically, a TPM2B_MAX_BUFFER)
  352. @retval EFI_SUCCESS Operation completed successfully.
  353. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  354. **/
  355. EFI_STATUS
  356. EFIAPI
  357. Tpm2GetCapabilityInputBufferSize (
  358. OUT UINT32 *InputBufferSize
  359. )
  360. {
  361. TPMS_CAPABILITY_DATA TpmCap;
  362. TPMI_YES_NO MoreData;
  363. EFI_STATUS Status;
  364. Status = Tpm2GetCapability (
  365. TPM_CAP_TPM_PROPERTIES,
  366. TPM_PT_INPUT_BUFFER,
  367. 1,
  368. &MoreData,
  369. &TpmCap
  370. );
  371. if (EFI_ERROR (Status)) {
  372. return Status;
  373. }
  374. *InputBufferSize = SwapBytes32 (TpmCap.data.tpmProperties.tpmProperty->value);
  375. return EFI_SUCCESS;
  376. }
  377. /**
  378. This command returns the information of TPM PCRs.
  379. This function parse the value got from TPM2_GetCapability and return the PcrSelection.
  380. @param[out] Pcrs The Pcr Selection
  381. @retval EFI_SUCCESS Operation completed successfully.
  382. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  383. **/
  384. EFI_STATUS
  385. EFIAPI
  386. Tpm2GetCapabilityPcrs (
  387. OUT TPML_PCR_SELECTION *Pcrs
  388. )
  389. {
  390. TPMS_CAPABILITY_DATA TpmCap;
  391. TPMI_YES_NO MoreData;
  392. EFI_STATUS Status;
  393. UINTN Index;
  394. Status = Tpm2GetCapability (
  395. TPM_CAP_PCRS,
  396. 0,
  397. 1,
  398. &MoreData,
  399. &TpmCap
  400. );
  401. if (EFI_ERROR (Status)) {
  402. return Status;
  403. }
  404. Pcrs->count = SwapBytes32 (TpmCap.data.assignedPCR.count);
  405. if (Pcrs->count > HASH_COUNT) {
  406. DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilityPcrs - Pcrs->count error %x\n", Pcrs->count));
  407. return EFI_DEVICE_ERROR;
  408. }
  409. for (Index = 0; Index < Pcrs->count; Index++) {
  410. Pcrs->pcrSelections[Index].hash = SwapBytes16 (TpmCap.data.assignedPCR.pcrSelections[Index].hash);
  411. Pcrs->pcrSelections[Index].sizeofSelect = TpmCap.data.assignedPCR.pcrSelections[Index].sizeofSelect;
  412. if (Pcrs->pcrSelections[Index].sizeofSelect > PCR_SELECT_MAX) {
  413. DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilityPcrs - sizeofSelect error %x\n", Pcrs->pcrSelections[Index].sizeofSelect));
  414. return EFI_DEVICE_ERROR;
  415. }
  416. CopyMem (Pcrs->pcrSelections[Index].pcrSelect, TpmCap.data.assignedPCR.pcrSelections[Index].pcrSelect, Pcrs->pcrSelections[Index].sizeofSelect);
  417. }
  418. return EFI_SUCCESS;
  419. }
  420. /**
  421. This function will query the TPM to determine which hashing algorithms
  422. are supported and which PCR banks are currently active.
  423. @param[out] TpmHashAlgorithmBitmap A bitmask containing the algorithms supported by the TPM.
  424. @param[out] ActivePcrBanks A bitmask containing the PCRs currently allocated.
  425. @retval EFI_SUCCESS TPM was successfully queried and return values can be trusted.
  426. @retval Others An error occurred, likely in communication with the TPM.
  427. **/
  428. EFI_STATUS
  429. EFIAPI
  430. Tpm2GetCapabilitySupportedAndActivePcrs (
  431. OUT UINT32 *TpmHashAlgorithmBitmap,
  432. OUT UINT32 *ActivePcrBanks
  433. )
  434. {
  435. EFI_STATUS Status;
  436. TPML_PCR_SELECTION Pcrs;
  437. UINTN Index;
  438. UINT8 ActivePcrBankCount;
  439. //
  440. // Get supported PCR
  441. //
  442. Status = Tpm2GetCapabilityPcrs (&Pcrs);
  443. DEBUG ((DEBUG_INFO, "Supported PCRs - Count = %08x\n", Pcrs.count));
  444. ActivePcrBankCount = 0;
  445. //
  446. // If error, assume that we have at least SHA-1 (and return the error.)
  447. //
  448. if (EFI_ERROR (Status)) {
  449. DEBUG ((DEBUG_ERROR, "GetSupportedAndActivePcrs - Tpm2GetCapabilityPcrs fail!\n"));
  450. *TpmHashAlgorithmBitmap = HASH_ALG_SHA1;
  451. *ActivePcrBanks = HASH_ALG_SHA1;
  452. ActivePcrBankCount = 1;
  453. }
  454. //
  455. // Otherwise, process the return data to determine what algorithms are supported
  456. // and currently allocated.
  457. //
  458. else {
  459. *TpmHashAlgorithmBitmap = 0;
  460. *ActivePcrBanks = 0;
  461. for (Index = 0; Index < Pcrs.count; Index++) {
  462. switch (Pcrs.pcrSelections[Index].hash) {
  463. case TPM_ALG_SHA1:
  464. DEBUG ((DEBUG_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA1 present.\n"));
  465. *TpmHashAlgorithmBitmap |= HASH_ALG_SHA1;
  466. if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
  467. DEBUG ((DEBUG_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA1 active.\n"));
  468. *ActivePcrBanks |= HASH_ALG_SHA1;
  469. ActivePcrBankCount++;
  470. }
  471. break;
  472. case TPM_ALG_SHA256:
  473. DEBUG ((DEBUG_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA256 present.\n"));
  474. *TpmHashAlgorithmBitmap |= HASH_ALG_SHA256;
  475. if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
  476. DEBUG ((DEBUG_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA256 active.\n"));
  477. *ActivePcrBanks |= HASH_ALG_SHA256;
  478. ActivePcrBankCount++;
  479. }
  480. break;
  481. case TPM_ALG_SHA384:
  482. DEBUG ((DEBUG_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA384 present.\n"));
  483. *TpmHashAlgorithmBitmap |= HASH_ALG_SHA384;
  484. if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
  485. DEBUG ((DEBUG_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA384 active.\n"));
  486. *ActivePcrBanks |= HASH_ALG_SHA384;
  487. ActivePcrBankCount++;
  488. }
  489. break;
  490. case TPM_ALG_SHA512:
  491. DEBUG ((DEBUG_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA512 present.\n"));
  492. *TpmHashAlgorithmBitmap |= HASH_ALG_SHA512;
  493. if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
  494. DEBUG ((DEBUG_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA512 active.\n"));
  495. *ActivePcrBanks |= HASH_ALG_SHA512;
  496. ActivePcrBankCount++;
  497. }
  498. break;
  499. case TPM_ALG_SM3_256:
  500. DEBUG ((DEBUG_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SM3_256 present.\n"));
  501. *TpmHashAlgorithmBitmap |= HASH_ALG_SM3_256;
  502. if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
  503. DEBUG ((DEBUG_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SM3_256 active.\n"));
  504. *ActivePcrBanks |= HASH_ALG_SM3_256;
  505. ActivePcrBankCount++;
  506. }
  507. break;
  508. default:
  509. DEBUG ((DEBUG_VERBOSE, "GetSupportedAndActivePcrs - Unsupported bank 0x%04x.\n", Pcrs.pcrSelections[Index].hash));
  510. continue;
  511. break;
  512. }
  513. }
  514. }
  515. DEBUG ((DEBUG_INFO, "GetSupportedAndActivePcrs - Count = %08x\n", ActivePcrBankCount));
  516. return Status;
  517. }
  518. /**
  519. This command returns the information of TPM AlgorithmSet.
  520. This function parse the value got from TPM2_GetCapability and return the AlgorithmSet.
  521. @param[out] AlgorithmSet The AlgorithmSet of TPM.
  522. @retval EFI_SUCCESS Operation completed successfully.
  523. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  524. **/
  525. EFI_STATUS
  526. EFIAPI
  527. Tpm2GetCapabilityAlgorithmSet (
  528. OUT UINT32 *AlgorithmSet
  529. )
  530. {
  531. TPMS_CAPABILITY_DATA TpmCap;
  532. TPMI_YES_NO MoreData;
  533. EFI_STATUS Status;
  534. Status = Tpm2GetCapability (
  535. TPM_CAP_TPM_PROPERTIES,
  536. TPM_PT_ALGORITHM_SET,
  537. 1,
  538. &MoreData,
  539. &TpmCap
  540. );
  541. if (EFI_ERROR (Status)) {
  542. return Status;
  543. }
  544. *AlgorithmSet = SwapBytes32 (TpmCap.data.tpmProperties.tpmProperty->value);
  545. return EFI_SUCCESS;
  546. }
  547. /**
  548. This function will query if the command is supported.
  549. @param[In] Command TPM_CC command starts from TPM_CC_FIRST.
  550. @param[out] IsCmdImpl The command is supported or not.
  551. @retval EFI_SUCCESS Operation completed successfully.
  552. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  553. **/
  554. EFI_STATUS
  555. EFIAPI
  556. Tpm2GetCapabilityIsCommandImplemented (
  557. IN TPM_CC Command,
  558. OUT BOOLEAN *IsCmdImpl
  559. )
  560. {
  561. TPMS_CAPABILITY_DATA TpmCap;
  562. TPMI_YES_NO MoreData;
  563. EFI_STATUS Status;
  564. UINT32 Attribute;
  565. Status = Tpm2GetCapability (
  566. TPM_CAP_COMMANDS,
  567. Command,
  568. 1,
  569. &MoreData,
  570. &TpmCap
  571. );
  572. if (EFI_ERROR (Status)) {
  573. return Status;
  574. }
  575. CopyMem (&Attribute, &TpmCap.data.command.commandAttributes[0], sizeof (UINT32));
  576. *IsCmdImpl = (Command == (SwapBytes32 (Attribute) & TPMA_CC_COMMANDINDEX_MASK));
  577. return EFI_SUCCESS;
  578. }
  579. /**
  580. This command is used to check to see if specific combinations of algorithm parameters are supported.
  581. @param[in] Parameters Algorithm parameters to be validated
  582. @retval EFI_SUCCESS Operation completed successfully.
  583. @retval EFI_DEVICE_ERROR Unexpected device behavior.
  584. **/
  585. EFI_STATUS
  586. EFIAPI
  587. Tpm2TestParms (
  588. IN TPMT_PUBLIC_PARMS *Parameters
  589. )
  590. {
  591. EFI_STATUS Status;
  592. TPM2_TEST_PARMS_COMMAND SendBuffer;
  593. TPM2_TEST_PARMS_RESPONSE RecvBuffer;
  594. UINT32 SendBufferSize;
  595. UINT32 RecvBufferSize;
  596. UINT8 *Buffer;
  597. //
  598. // Construct command
  599. //
  600. SendBuffer.Header.tag = SwapBytes16 (TPM_ST_NO_SESSIONS);
  601. SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_TestParms);
  602. Buffer = (UINT8 *)&SendBuffer.Parameters;
  603. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->type));
  604. Buffer += sizeof (UINT16);
  605. switch (Parameters->type) {
  606. case TPM_ALG_KEYEDHASH:
  607. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.keyedHashDetail.scheme.scheme));
  608. Buffer += sizeof (UINT16);
  609. switch (Parameters->parameters.keyedHashDetail.scheme.scheme) {
  610. case TPM_ALG_HMAC:
  611. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.keyedHashDetail.scheme.details.hmac.hashAlg));
  612. Buffer += sizeof (UINT16);
  613. break;
  614. case TPM_ALG_XOR:
  615. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.keyedHashDetail.scheme.details.xor.hashAlg));
  616. Buffer += sizeof (UINT16);
  617. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.keyedHashDetail.scheme.details.xor.kdf));
  618. Buffer += sizeof (UINT16);
  619. break;
  620. default:
  621. return EFI_INVALID_PARAMETER;
  622. }
  623. case TPM_ALG_SYMCIPHER:
  624. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.symDetail.algorithm));
  625. Buffer += sizeof (UINT16);
  626. switch (Parameters->parameters.symDetail.algorithm) {
  627. case TPM_ALG_AES:
  628. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.symDetail.keyBits.aes));
  629. Buffer += sizeof (UINT16);
  630. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.symDetail.mode.aes));
  631. Buffer += sizeof (UINT16);
  632. break;
  633. case TPM_ALG_SM4:
  634. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.symDetail.keyBits.SM4));
  635. Buffer += sizeof (UINT16);
  636. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.symDetail.mode.SM4));
  637. Buffer += sizeof (UINT16);
  638. break;
  639. case TPM_ALG_XOR:
  640. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.symDetail.keyBits.xor));
  641. Buffer += sizeof (UINT16);
  642. break;
  643. case TPM_ALG_NULL:
  644. break;
  645. default:
  646. return EFI_INVALID_PARAMETER;
  647. }
  648. break;
  649. case TPM_ALG_RSA:
  650. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.rsaDetail.symmetric.algorithm));
  651. Buffer += sizeof (UINT16);
  652. switch (Parameters->parameters.rsaDetail.symmetric.algorithm) {
  653. case TPM_ALG_AES:
  654. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.rsaDetail.symmetric.keyBits.aes));
  655. Buffer += sizeof (UINT16);
  656. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.rsaDetail.symmetric.mode.aes));
  657. Buffer += sizeof (UINT16);
  658. break;
  659. case TPM_ALG_SM4:
  660. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.rsaDetail.symmetric.keyBits.SM4));
  661. Buffer += sizeof (UINT16);
  662. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.rsaDetail.symmetric.mode.SM4));
  663. Buffer += sizeof (UINT16);
  664. break;
  665. case TPM_ALG_NULL:
  666. break;
  667. default:
  668. return EFI_INVALID_PARAMETER;
  669. }
  670. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.rsaDetail.scheme.scheme));
  671. Buffer += sizeof (UINT16);
  672. switch (Parameters->parameters.rsaDetail.scheme.scheme) {
  673. case TPM_ALG_RSASSA:
  674. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.rsaDetail.scheme.details.rsassa.hashAlg));
  675. Buffer += sizeof (UINT16);
  676. break;
  677. case TPM_ALG_RSAPSS:
  678. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.rsaDetail.scheme.details.rsapss.hashAlg));
  679. Buffer += sizeof (UINT16);
  680. break;
  681. case TPM_ALG_RSAES:
  682. break;
  683. case TPM_ALG_OAEP:
  684. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.rsaDetail.scheme.details.oaep.hashAlg));
  685. Buffer += sizeof (UINT16);
  686. break;
  687. case TPM_ALG_NULL:
  688. break;
  689. default:
  690. return EFI_INVALID_PARAMETER;
  691. }
  692. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.rsaDetail.keyBits));
  693. Buffer += sizeof (UINT16);
  694. WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32 (Parameters->parameters.rsaDetail.exponent));
  695. Buffer += sizeof (UINT32);
  696. break;
  697. case TPM_ALG_ECC:
  698. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.symmetric.algorithm));
  699. Buffer += sizeof (UINT16);
  700. switch (Parameters->parameters.eccDetail.symmetric.algorithm) {
  701. case TPM_ALG_AES:
  702. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.symmetric.keyBits.aes));
  703. Buffer += sizeof (UINT16);
  704. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.symmetric.mode.aes));
  705. Buffer += sizeof (UINT16);
  706. break;
  707. case TPM_ALG_SM4:
  708. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.symmetric.keyBits.SM4));
  709. Buffer += sizeof (UINT16);
  710. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.symmetric.mode.SM4));
  711. Buffer += sizeof (UINT16);
  712. break;
  713. case TPM_ALG_NULL:
  714. break;
  715. default:
  716. return EFI_INVALID_PARAMETER;
  717. }
  718. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.scheme.scheme));
  719. Buffer += sizeof (UINT16);
  720. switch (Parameters->parameters.eccDetail.scheme.scheme) {
  721. case TPM_ALG_ECDSA:
  722. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.scheme.details.ecdsa.hashAlg));
  723. Buffer += sizeof (UINT16);
  724. break;
  725. case TPM_ALG_ECDAA:
  726. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.scheme.details.ecdaa.hashAlg));
  727. Buffer += sizeof (UINT16);
  728. break;
  729. case TPM_ALG_ECSCHNORR:
  730. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.scheme.details.ecSchnorr.hashAlg));
  731. Buffer += sizeof (UINT16);
  732. break;
  733. case TPM_ALG_ECDH:
  734. break;
  735. case TPM_ALG_NULL:
  736. break;
  737. default:
  738. return EFI_INVALID_PARAMETER;
  739. }
  740. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.curveID));
  741. Buffer += sizeof (UINT16);
  742. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.kdf.scheme));
  743. Buffer += sizeof (UINT16);
  744. switch (Parameters->parameters.eccDetail.kdf.scheme) {
  745. case TPM_ALG_MGF1:
  746. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.kdf.details.mgf1.hashAlg));
  747. Buffer += sizeof (UINT16);
  748. break;
  749. case TPM_ALG_KDF1_SP800_108:
  750. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.kdf.details.kdf1_sp800_108.hashAlg));
  751. Buffer += sizeof (UINT16);
  752. break;
  753. case TPM_ALG_KDF1_SP800_56a:
  754. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.kdf.details.kdf1_SP800_56a.hashAlg));
  755. Buffer += sizeof (UINT16);
  756. break;
  757. case TPM_ALG_KDF2:
  758. WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Parameters->parameters.eccDetail.kdf.details.kdf2.hashAlg));
  759. Buffer += sizeof (UINT16);
  760. break;
  761. case TPM_ALG_NULL:
  762. break;
  763. default:
  764. return EFI_INVALID_PARAMETER;
  765. }
  766. break;
  767. default:
  768. return EFI_INVALID_PARAMETER;
  769. }
  770. SendBufferSize = (UINT32)((UINTN)Buffer - (UINTN)&SendBuffer);
  771. SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
  772. //
  773. // send Tpm command
  774. //
  775. RecvBufferSize = sizeof (RecvBuffer);
  776. Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
  777. if (EFI_ERROR (Status)) {
  778. return Status;
  779. }
  780. if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
  781. DEBUG ((DEBUG_ERROR, "Tpm2TestParms - RecvBufferSize Error - %x\n", RecvBufferSize));
  782. return EFI_DEVICE_ERROR;
  783. }
  784. if (SwapBytes32 (RecvBuffer.Header.responseCode) != TPM_RC_SUCCESS) {
  785. DEBUG ((DEBUG_ERROR, "Tpm2TestParms - responseCode - %x\n", SwapBytes32 (RecvBuffer.Header.responseCode)));
  786. return EFI_UNSUPPORTED;
  787. }
  788. return EFI_SUCCESS;
  789. }