sys-pcr-extension.int.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /***********************************************************************
  3. * Copyright (c) 2017-2018, Intel Corporation
  4. *
  5. * All rights reserved.
  6. ***********************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "tss2_sys.h"
  14. #define LOGMODULE test
  15. #include "util/log.h"
  16. #include "test.h"
  17. #include "sys-util.h"
  18. #define PCR_8 8
  19. /**
  20. * This program contains integration test for SYS Tss2_Sys_PCR_Read
  21. * and Tss2_Sys_PCR_Extend. This is an use case scenario on PCR extend.
  22. * First, we will get the list of PCR available through getcapability
  23. * SYS. Then, PCR_Read SYS is called to list out the PCR value and
  24. * PCR_Extend SYS is called next to update the PCR value. Last,
  25. * PCR_Read SYS is called again to check the PCR values are changed.
  26. */
  27. int
  28. test_invoke (TSS2_SYS_CONTEXT *sys_context)
  29. {
  30. TSS2_RC rc;
  31. TPMI_YES_NO more_data;
  32. TPMS_CAPABILITY_DATA capability_data;
  33. UINT16 i, digest_size;
  34. TPML_PCR_SELECTION pcr_selection;
  35. UINT32 pcr_update_counter_before_extend;
  36. UINT32 pcr_update_counter_after_extend;
  37. UINT8 pcr_before_extend[20];
  38. UINT8 pcr_after_extend[20];
  39. TPML_DIGEST pcr_values;
  40. TPML_DIGEST_VALUES digests;
  41. TPML_PCR_SELECTION pcr_selection_out;
  42. TSS2L_SYS_AUTH_COMMAND sessions_data = {
  43. .count = 1,
  44. .auths = {{.sessionHandle = TPM2_RH_PW,
  45. .sessionAttributes = 0,
  46. .nonce={.size=0},
  47. .hmac={.size=0}}}};
  48. LOG_INFO("PCR Extension tests started.");
  49. rc = Tss2_Sys_GetCapability(sys_context, 0, TPM2_CAP_PCR_PROPERTIES, TPM2_PT_PCR_COUNT, 1, &more_data, &capability_data, 0);
  50. if (rc != TSS2_RC_SUCCESS) {
  51. LOG_ERROR("GetCapability FAILED! Response Code : 0x%x", rc);
  52. exit(1);
  53. }
  54. digests.count = 1;
  55. digests.digests[0].hashAlg = TPM2_ALG_SHA1;
  56. digest_size = GetDigestSize( digests.digests[0].hashAlg );
  57. for( i = 0; i < digest_size; i++ )
  58. {
  59. digests.digests[0].digest.sha1[i] = (UINT8)(i % 256);
  60. }
  61. pcr_selection.count = 1;
  62. pcr_selection.pcrSelections[0].hash = TPM2_ALG_SHA1;
  63. pcr_selection.pcrSelections[0].sizeofSelect = 3;
  64. pcr_selection.pcrSelections[0].pcrSelect[0] = 0;
  65. pcr_selection.pcrSelections[0].pcrSelect[1] = 0;
  66. pcr_selection.pcrSelections[0].pcrSelect[2] = 0;
  67. pcr_selection.pcrSelections[0].pcrSelect[PCR_8 / 8] = 1 << (PCR_8 % 8);
  68. rc = Tss2_Sys_PCR_Read(sys_context, 0, &pcr_selection, &pcr_update_counter_before_extend, &pcr_selection_out, &pcr_values, 0);
  69. if (rc != TSS2_RC_SUCCESS) {
  70. LOG_ERROR("PCR_Read FAILED! Response Code : 0x%x", rc);
  71. exit(1);
  72. }
  73. memcpy(&(pcr_before_extend[0]), &(pcr_values.digests[0].buffer[0]), pcr_values.digests[0].size);
  74. rc = Tss2_Sys_PCR_Extend(sys_context, PCR_8, &sessions_data, &digests, 0);
  75. if (rc != TSS2_RC_SUCCESS) {
  76. LOG_ERROR("PCR_Extend FAILED! Response Code : 0x%x", rc);
  77. exit(1);
  78. }
  79. rc = Tss2_Sys_PCR_Read(sys_context, 0, &pcr_selection, &pcr_update_counter_after_extend, &pcr_selection_out, &pcr_values, 0);
  80. if (rc != TSS2_RC_SUCCESS) {
  81. LOG_ERROR("PCR_Read FAILED! Response Code : 0x%x", rc);
  82. exit(1);
  83. }
  84. memcpy(&(pcr_after_extend[0]), &(pcr_values.digests[0].buffer[0]), pcr_values.digests[0].size);
  85. if(pcr_update_counter_before_extend == pcr_update_counter_after_extend) {
  86. LOG_ERROR("ERROR!! pcr_update_counter didn't change value");
  87. exit(1);
  88. }
  89. if(memcmp(&(pcr_before_extend[0]), &(pcr_after_extend[0]), 20) == 0) {
  90. LOG_ERROR("ERROR!! PCR didn't change value");
  91. exit(1);
  92. }
  93. LOG_INFO("PCR Extension Test Passed!");
  94. return 0;
  95. }