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
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <stdlib.h>
12
13 #include "tss2_fapi.h"
14
15 #define LOGMODULE test
16 #include "util/log.h"
17 #include "util/aux_util.h"
18
19 /** Test the FAPI function FAPI_GetRandom and async invocations.
20 *
21 * Tested FAPI commands:
22 * - Fapi_Provision()
23 * - Fapi_GetRandom_Async()
24 * - Fapi_GetRandom_Finish()
25 * - Fapi_GetPollHandles()
26 * - Fapi_GetRandom()
27 * - Fapi_Delete()
28 *
29 * @param[in,out] context The FAPI_CONTEXT.
30 * @retval EXIT_FAILURE
31 * @retval EXIT_SUCCESS
32 */
33 int
test_fapi_get_random(FAPI_CONTEXT * context)34 test_fapi_get_random(FAPI_CONTEXT *context)
35 {
36
37 TSS2_RC r;
38 FAPI_POLL_HANDLE *handles;
39 size_t num_handles;
40 /* Ensure that more than one call of Esys_GetRandom is necessary */
41 size_t bytesRequested = sizeof(TPMU_HA) + 10;
42 uint8_t *randomBytes;
43
44 r = Fapi_Provision(context, NULL, NULL, NULL);
45 goto_if_error(r, "Error Fapi_Provision", error);
46
47 r = Fapi_GetRandom_Async(context, bytesRequested);
48 goto_if_error(r, "GetRandom_Async", error);
49
50 do {
51 r = Fapi_GetPollHandles(context, &handles, &num_handles);
52 if (r == TSS2_RC_SUCCESS) {
53 poll(handles, num_handles, -1);
54 Fapi_Free(handles);
55 } else if (r != TSS2_FAPI_RC_NO_HANDLE) {
56 LOG_ERROR("GetPollHandles failed");
57 goto error;
58 }
59
60 r = Fapi_GetRandom_Finish(context, &randomBytes);
61 } while (r == TSS2_FAPI_RC_TRY_AGAIN);
62 goto_if_error(r, "Error Fapi_GetRandom_Finish", error);
63
64 Fapi_Free(randomBytes);
65
66 r = Fapi_GetRandom(context, bytesRequested, &randomBytes);
67 goto_if_error(r, "Error Fapi_GetRandom", error);
68
69 Fapi_Free(randomBytes);
70
71 /* Cleanup */
72 r = Fapi_Delete(context, "/HS/SRK");
73 goto_if_error(r, "Error Fapi_Delete", error);
74
75 return TSS2_RC_SUCCESS;
76
77 error:
78 Fapi_Delete(context, "/HS/SRK");
79 return EXIT_FAILURE;
80 }
81
82 int
test_invoke_fapi(FAPI_CONTEXT * fapi_context)83 test_invoke_fapi(FAPI_CONTEXT *fapi_context)
84 {
85 return test_fapi_get_random(fapi_context);
86 }
87