fapi-get-random.int.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "tss2_fapi.h"
  11. #include "test-fapi.h"
  12. #define LOGMODULE test
  13. #include "util/log.h"
  14. #include "util/aux_util.h"
  15. /** Test the FAPI function FAPI_GetRandom and async invocations.
  16. *
  17. * Tested FAPI commands:
  18. * - Fapi_Provision()
  19. * - Fapi_GetRandom_Async()
  20. * - Fapi_GetRandom_Finish()
  21. * - Fapi_GetPollHandles()
  22. * - Fapi_GetRandom()
  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_get_random(FAPI_CONTEXT *context)
  31. {
  32. TSS2_RC r;
  33. FAPI_POLL_HANDLE *handles;
  34. size_t num_handles;
  35. /* Ensure that more than one call of Esys_GetRandom is necessary */
  36. size_t bytesRequested = sizeof(TPMU_HA) + 10;
  37. uint8_t *randomBytes = NULL;
  38. r = Fapi_Provision(context, NULL, NULL, NULL);
  39. goto_if_error(r, "Error Fapi_Provision", error);
  40. r = Fapi_GetRandom_Async(context, bytesRequested);
  41. goto_if_error(r, "GetRandom_Async", error);
  42. do {
  43. r = Fapi_GetPollHandles(context, &handles, &num_handles);
  44. if (r == TSS2_RC_SUCCESS) {
  45. poll(handles, num_handles, 99);
  46. Fapi_Free(handles);
  47. } else if (r != TSS2_FAPI_RC_NO_HANDLE) {
  48. LOG_ERROR("GetPollHandles failed");
  49. goto error;
  50. }
  51. r = Fapi_GetRandom_Finish(context, &randomBytes);
  52. } while (r == TSS2_FAPI_RC_TRY_AGAIN);
  53. goto_if_error(r, "Error Fapi_GetRandom_Finish", error);
  54. ASSERT(randomBytes != NULL);
  55. Fapi_Free(randomBytes);
  56. randomBytes = NULL;
  57. r = Fapi_GetRandom(context, bytesRequested, &randomBytes);
  58. goto_if_error(r, "Error Fapi_GetRandom", error);
  59. ASSERT(randomBytes != NULL);
  60. Fapi_Free(randomBytes);
  61. /* Cleanup */
  62. r = Fapi_Delete(context, "/");
  63. goto_if_error(r, "Error Fapi_Delete", error);
  64. return TSS2_RC_SUCCESS;
  65. error:
  66. Fapi_Delete(context, "/");
  67. return EXIT_FAILURE;
  68. }
  69. int
  70. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  71. {
  72. return test_fapi_get_random(fapi_context);
  73. }