fapi-unseal.int.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*******************************************************************************
  3. * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
  4. * All rights reserved.
  5. *******************************************************************************/
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "tss2_esys.h"
  12. #include "tss2_fapi.h"
  13. #include "test-fapi.h"
  14. #define LOGMODULE test
  15. #include "util/log.h"
  16. #include "util/aux_util.h"
  17. /** Test the FAPI functions for sealing.
  18. *
  19. * Tested FAPI commands:
  20. * - Fapi_Provision()
  21. * - Fapi_CreateSeal()
  22. * - Fapi_Unseal()
  23. * - Fapi_Delete()
  24. *
  25. * @param[in,out] context The FAPI_CONTEXT.
  26. * @retval EXIT_FAILURE
  27. * @retval EXIT_SUCCESS
  28. */
  29. int
  30. test_fapi_unseal(FAPI_CONTEXT *context)
  31. {
  32. TSS2_RC r;
  33. size_t resultSize;
  34. uint8_t *result = NULL;
  35. TPM2B_DIGEST digest = {
  36. .size = 20,
  37. .buffer = {
  38. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  39. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
  40. }
  41. };
  42. r = Fapi_Provision(context, NULL, NULL, NULL);
  43. goto_if_error(r, "Error Fapi_Provision", error);
  44. #ifdef PERSISTENT
  45. r = Fapi_CreateSeal(context, "/HS/SRK/mySealObject", "noDa,0x81000004",
  46. digest.size,
  47. "", "", &digest.buffer[0]);
  48. #else
  49. r = Fapi_CreateSeal(context, "/HS/SRK/mySealObject", "noDa,0x81000004",
  50. digest.size,
  51. "", "", &digest.buffer[0]);
  52. #endif
  53. goto_if_error(r, "Error Fapi_CreateSeal", error);
  54. r = Fapi_Unseal(context, "/HS/SRK/mySealObject", &result,
  55. &resultSize);
  56. goto_if_error(r, "Error Fapi_CreateSeal", error);
  57. ASSERT(result != NULL);
  58. if (resultSize != digest.size ||
  59. memcmp(result, &digest.buffer[0], resultSize) != 0) {
  60. LOG_ERROR("Error: unealed data not equal to origin");
  61. goto error;
  62. }
  63. SAFE_FREE(result);
  64. r = Fapi_Delete(context, "/HS/SRK/mySealObject");
  65. goto_if_error(r, "Error Fapi_Delete", error);
  66. r = Fapi_CreateSeal(context, "/HS/SRK/myRandomSealObject", "noDa",
  67. 128,
  68. "", "", NULL);
  69. goto_if_error(r, "Error Fapi_CreateSeal", error);
  70. result = NULL;
  71. r = Fapi_Unseal(context, "/HS/SRK/myRandomSealObject", &result,
  72. &resultSize);
  73. goto_if_error(r, "Error Fapi_CreateSeal", error);
  74. ASSERT(result != NULL);
  75. LOGBLOB_INFO(result, resultSize, "Unsealed random data");
  76. if (resultSize != 128) {
  77. LOG_ERROR("Error: Random data has wrong size.");
  78. goto error;
  79. }
  80. /* Check the tests related to SRK deleting. */
  81. r = Fapi_Delete(context, "/HS");
  82. if (r != TSS2_FAPI_RC_BAD_PATH)
  83. goto_if_error(r, "Error Fapi_Delete", error);
  84. r = Fapi_Delete(context, "/HE");
  85. goto_if_error(r, "Error Fapi_Delete", error);
  86. r = Fapi_Delete(context, "/HN");
  87. goto_if_error(r, "Error Fapi_Delete", error);
  88. r = Fapi_Delete(context, "/LOCKOUT");
  89. goto_if_error(r, "Error Fapi_Delete", error);
  90. r = Fapi_Delete(context, "/HS/SRK/myRandomSealObject");
  91. goto_if_error(r, "Error Fapi_Delete", error);
  92. r = Fapi_Delete(context, "/HS");
  93. goto_if_error(r, "Error Fapi_Delete", error);
  94. SAFE_FREE(result);
  95. return EXIT_SUCCESS;
  96. error:
  97. Fapi_Delete(context, "/");
  98. SAFE_FREE(result);
  99. return EXIT_FAILURE;
  100. }
  101. int
  102. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  103. {
  104. return test_fapi_unseal(fapi_context);
  105. }