test_tpm2_header.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdarg.h>
  3. #include <stdbool.h>
  4. #include <stddef.h>
  5. #include <setjmp.h>
  6. #include <cmocka.h>
  7. #include "tpm2_header.h"
  8. static void test_tpm_command_header(void **state) {
  9. (void) state;
  10. UINT8 command_bytes[] = {
  11. 0x80, 0x01, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x01, 0x7a, 0x00, 0x00,
  12. 0x00, 0x06, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x7f
  13. };
  14. tpm2_command_header *c = tpm2_command_header_from_bytes(command_bytes);
  15. assert_true(c->tag == 0x0180);
  16. assert_true(c->size == 0x16000000);
  17. assert_true(c->command_code == 0x7a010000);
  18. /* everything from bytes should be the same as the byte array */
  19. assert_memory_equal(c->bytes, command_bytes, sizeof(command_bytes));
  20. /* everything from data should be the same */
  21. assert_memory_equal(c->data, command_bytes + 10,
  22. sizeof(command_bytes) - 10);
  23. TPMI_ST_COMMAND_TAG tag = tpm2_command_header_get_tag(c);
  24. UINT32 size_with_header = tpm2_command_header_get_size(c, true);
  25. UINT32 size_with_out_header = tpm2_command_header_get_size(c, false);
  26. TPM2_CC cc = tpm2_command_header_get_code(c);
  27. assert_true(tag == 0x8001);
  28. assert_true(size_with_header == 0x16);
  29. assert_true(size_with_out_header == 0x16 - TPM2_COMMAND_HEADER_SIZE);
  30. assert_true(cc == 0x17a);
  31. }
  32. static void test_tpm_response_header(void **state) {
  33. (void) state;
  34. unsigned char response_bytes[] = {
  35. 0x80, 0x01, 0x00, 0x00, 0x02, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36. 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00, 0x32,
  37. 0x2e, 0x30, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x01, 0x03, 0x00,
  39. 0x00, 0x00, 0xbf, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x07, 0xdf, 0x00,
  40. 0x00, 0x01, 0x05, 0x49, 0x42, 0x4d, 0x20, 0x00, 0x00, 0x01, 0x06, 0x53,
  41. 0x57, 0x20, 0x20, 0x00, 0x00, 0x01, 0x07, 0x20, 0x54, 0x50, 0x4d, 0x00,
  42. 0x00, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x09, 0x00,
  43. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x00, 0x00, 0x00, 0x01, 0x00,
  44. 0x00, 0x01, 0x0b, 0x20, 0x14, 0x07, 0x11, 0x00, 0x00, 0x01, 0x0c, 0x00,
  45. 0x11, 0x06, 0x28, 0x00, 0x00, 0x01, 0x0d, 0x00, 0x00, 0x04, 0x00, 0x00,
  46. 0x00, 0x01, 0x0e, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x01, 0x0f, 0x00,
  47. 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x00, 0x03, 0x00,
  48. 0x00, 0x01, 0x11, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x01, 0x12, 0x00,
  49. 0x00, 0x00, 0x18, 0x00, 0x00, 0x01, 0x13, 0x00, 0x00, 0x00, 0x03, 0x00,
  50. 0x00, 0x01, 0x14, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x01, 0x16, 0x00,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x17, 0x00, 0x00, 0x08, 0x00, 0x00,
  52. 0x00, 0x01, 0x18, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x01, 0x19, 0x00,
  53. 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x1a, 0x00, 0x00, 0x00, 0x0b, 0x00,
  54. 0x00, 0x01, 0x1b, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x01, 0x1c, 0x00,
  55. 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1d, 0x00, 0x00, 0x00, 0xff, 0x00,
  56. 0x00, 0x01, 0x1e, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x1f, 0x00,
  57. 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x20, 0x00, 0x00, 0x00, 0x30, 0x00,
  58. 0x00, 0x01, 0x21, 0x00, 0x00, 0x05, 0xf0, 0x00, 0x00, 0x01, 0x22, 0x00,
  59. 0x00, 0x01, 0x34, 0x00, 0x00, 0x01, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x01, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x25, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x01, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x28, 0x00,
  63. 0x00, 0x00, 0x80, 0x00, 0x00, 0x01, 0x29, 0x00, 0x00, 0x00, 0x6a, 0x00,
  64. 0x00, 0x01, 0x2a, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x01, 0x2b, 0x00,
  65. 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x2c, 0x00, 0x00, 0x04, 0x00, 0x00,
  66. 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x01, 0x00,
  67. 0x00, 0x00, 0x0f, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x04, 0x00,
  69. 0x00, 0x00, 0x03, 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x02, 0x06, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x02, 0x07, 0x00,
  71. 0x00, 0x00, 0x03, 0x00, 0x00, 0x02, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. 0x00, 0x02, 0x09, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x02, 0x0a, 0x00,
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0b, 0x00, 0x00, 0x00, 0x20, 0x00,
  74. 0x00, 0x02, 0x0c, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x02, 0x0d, 0x00,
  75. 0x00, 0x00, 0x03, 0x00, 0x00, 0x02, 0x0e, 0x00, 0x00, 0x00, 0x02, 0x00,
  76. 0x00, 0x02, 0x0f, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x02, 0x10, 0x00,
  77. 0x00, 0x03, 0xe8, 0x00, 0x00, 0x02, 0x11, 0x00, 0x00, 0x03, 0xe8, 0x00,
  78. 0x00, 0x02, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x13, 0x00,
  79. 0x00
  80. };
  81. tpm2_response_header *r = tpm2_response_header_from_bytes(response_bytes);
  82. assert_true(r->tag == 0x0180);
  83. assert_true(r->size == 0x1b020000);
  84. assert_true(r->response_code == 0x00);
  85. /* everything from bytes should be the same as the byte array */
  86. assert_memory_equal(r->bytes, response_bytes, sizeof(response_bytes));
  87. /* everything from data should be the same */
  88. assert_memory_equal(r->data, response_bytes + 10,
  89. sizeof(response_bytes) - 10);
  90. TPMI_ST_COMMAND_TAG tag = tpm2_response_header_get_tag(r);
  91. UINT32 size_with_header = tpm2_response_header_get_size(r, true);
  92. UINT32 size_with_out_header = tpm2_response_header_get_size(r, false);
  93. TSS2_RC rc = tpm2_response_header_get_code(r);
  94. assert_true(tag == 0x8001);
  95. assert_true(size_with_header == 0x21b);
  96. assert_true(size_with_out_header == 0x21b - TPM2_RESPONSE_HEADER_SIZE);
  97. assert_true(rc == 0x00);
  98. }
  99. /* link required symbol, but tpm2_tool.c declares it AND main, which
  100. * we have a main below for cmocka tests.
  101. */
  102. bool output_enabled = true;
  103. int main(int argc, char* argv[]) {
  104. (void) argc;
  105. (void) argv;
  106. const struct CMUnitTest tests[] = {
  107. cmocka_unit_test(test_tpm_command_header),
  108. cmocka_unit_test(test_tpm_response_header),
  109. };
  110. return cmocka_run_group_tests(tests, NULL, NULL);
  111. }