tpm2_header.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #ifndef TPM2_HEADER_H
  3. #define TPM2_HEADER_H
  4. #include <stdbool.h>
  5. #include <tss2/tss2_sys.h>
  6. #include "tpm2_util.h"
  7. #define TPM2_COMMAND_HEADER_SIZE (sizeof(tpm2_command_header))
  8. #define TPM2_RESPONSE_HEADER_SIZE (sizeof(tpm2_response_header))
  9. #define TPM2_MAX_SIZE 4096
  10. typedef union tpm2_command_header tpm2_command_header;
  11. union tpm2_command_header {
  12. struct {
  13. TPMI_ST_COMMAND_TAG tag; // uint16
  14. UINT32 size; //
  15. TPM2_CC command_code;
  16. UINT8 data[];
  17. }__attribute__((packed));
  18. UINT8 bytes[0];
  19. };
  20. typedef union tpm2_response_header tpm2_response_header;
  21. union tpm2_response_header {
  22. struct {
  23. TPM2_ST tag;
  24. UINT32 size;
  25. TSS2_RC response_code;
  26. UINT8 data[];
  27. }__attribute__((packed));
  28. UINT8 bytes[0];
  29. };
  30. /**
  31. * Converts a byte-array to a tpm2_command_header struct.
  32. * @param h
  33. * The byte array to convert to a tpm2_command_header.
  34. * @return
  35. * A converted byte array.
  36. */
  37. static inline tpm2_command_header *tpm2_command_header_from_bytes(UINT8 *h) {
  38. return (tpm2_command_header *) h;
  39. }
  40. /**
  41. * Converts a byte-array to a tpm2_response_header struct.
  42. * @param h
  43. * The byte array to convert to a tpm2_response_header.
  44. * @return
  45. * A converted byte array.
  46. */
  47. static inline tpm2_response_header *tpm2_response_header_from_bytes(UINT8 *h) {
  48. return (tpm2_response_header *) h;
  49. }
  50. /**
  51. * Retrieves the command tag from a command converting to host
  52. * endianess.
  53. * @param command
  54. * @return
  55. */
  56. static inline TPMI_ST_COMMAND_TAG tpm2_command_header_get_tag(
  57. tpm2_command_header *command) {
  58. return tpm2_util_ntoh_16(command->tag);
  59. }
  60. /**
  61. * Retrieves the command size from a command converting to host
  62. * endianess.
  63. * @param command
  64. * @param include_header
  65. * @return
  66. */
  67. static inline UINT32 tpm2_command_header_get_size(tpm2_command_header *command,
  68. bool include_header) {
  69. UINT32 size = tpm2_util_ntoh_32(command->size);
  70. return include_header ? size : size - TPM2_COMMAND_HEADER_SIZE;
  71. }
  72. /**
  73. * Retrieves the command code from a command converting to host
  74. * endianess.
  75. * @param command
  76. * @return
  77. */
  78. static inline TPM2_CC tpm2_command_header_get_code(tpm2_command_header *command) {
  79. return tpm2_util_ntoh_32(command->command_code);
  80. }
  81. /**
  82. * Retrieves command data, if present.
  83. * @param command
  84. * The command to check for following data.
  85. * @return The command data or NULL if not present.
  86. */
  87. static inline UINT8 *tpm2_command_header_get_data(tpm2_command_header *command) {
  88. UINT32 size = tpm2_command_header_get_size(command, false);
  89. return size ? command->data : NULL;
  90. }
  91. /**
  92. * Retrieves the response size from a response header converting to host
  93. * endianess.
  94. * @param response_header
  95. * @param include_header
  96. * @return
  97. */
  98. static inline UINT32 tpm2_response_header_get_size(
  99. tpm2_response_header *response, bool include_header) {
  100. UINT32 size = tpm2_util_ntoh_32(response->size);
  101. return include_header ? size : size - TPM2_RESPONSE_HEADER_SIZE;
  102. }
  103. /**
  104. * Retrieves the response tag from a response header converting to host
  105. * endianess.
  106. * @param response_header
  107. * @return
  108. */
  109. static inline TPM2_ST tpm2_response_header_get_tag(
  110. tpm2_response_header *response) {
  111. return tpm2_util_ntoh_16(response->tag);
  112. }
  113. /**
  114. * Retrieves the response code from a response header converting to host
  115. * endianess.
  116. * @param response_header
  117. * @return
  118. */
  119. static inline TSS2_RC tpm2_response_header_get_code(
  120. tpm2_response_header *response) {
  121. return tpm2_util_ntoh_32(response->response_code);
  122. }
  123. /**
  124. * Retrieves response data, if present.
  125. * @param response_header
  126. * The response_header to check for following data.
  127. * @return The response data or NULL if not present.
  128. */
  129. static inline UINT8 *tpm2_response_header_get_data(
  130. tpm2_response_header *response) {
  131. UINT32 size = tpm2_response_header_get_size(response, false);
  132. return size ? response->data : NULL;
  133. }
  134. #endif /* TPM2_HEADER_H */