Tpm2Capability.c 29 KB

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