esys-create-primary-hmac.int.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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_esys.h"
  11. #include "esys_iutil.h"
  12. #define LOGMODULE test
  13. #include "util/log.h"
  14. #include "util/aux_util.h"
  15. /** This test is intended to test Esys_CreatePrimary with hmac verification.
  16. *
  17. * The test can be executed with RSA or ECC keys. ECC will be used if
  18. * ECC is defined.
  19. *
  20. * Tested ESYS commands:
  21. * - Esys_CreatePrimary() (M)
  22. * - Esys_FlushContext() (M)
  23. * - Esys_StartAuthSession() (M)
  24. *
  25. * Used compiler defines: TEST_ECC
  26. *
  27. * @param[in,out] esys_context The ESYS_CONTEXT.
  28. * @retval EXIT_FAILURE
  29. * @retval EXIT_SUCCESS
  30. */
  31. int
  32. test_esys_create_primary_hmac(ESYS_CONTEXT * esys_context)
  33. {
  34. TSS2_RC r;
  35. ESYS_TR objectHandle = ESYS_TR_NONE;
  36. ESYS_TR session = ESYS_TR_NONE;
  37. TPMT_SYM_DEF symmetric = { .algorithm = TPM2_ALG_NULL };
  38. TPM2B_PUBLIC *outPublic = NULL;
  39. TPM2B_CREATION_DATA *creationData = NULL;
  40. TPM2B_DIGEST *creationHash = NULL;
  41. TPMT_TK_CREATION *creationTicket = NULL;
  42. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  43. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  44. NULL,
  45. TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA256,
  46. &session);
  47. goto_if_error(r, "Error: During initialization of session", error);
  48. TPM2B_SENSITIVE_CREATE inSensitive = {
  49. .size = 0,
  50. .sensitive = {
  51. .userAuth = {
  52. .size = 0,
  53. .buffer = {0}
  54. ,
  55. },
  56. .data = {
  57. .size = 0,
  58. .buffer = {0}
  59. }
  60. }
  61. };
  62. #ifdef TEST_ECC
  63. TPM2B_PUBLIC inPublicECC = {
  64. .size = 0,
  65. .publicArea = {
  66. .type = TPM2_ALG_ECC,
  67. .nameAlg = TPM2_ALG_SHA256,
  68. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  69. TPMA_OBJECT_RESTRICTED |
  70. TPMA_OBJECT_SIGN_ENCRYPT |
  71. TPMA_OBJECT_FIXEDTPM |
  72. TPMA_OBJECT_FIXEDPARENT |
  73. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  74. .authPolicy = {
  75. .size = 0,
  76. },
  77. .parameters.eccDetail = {
  78. .symmetric = {
  79. .algorithm = TPM2_ALG_NULL,
  80. .keyBits.aes = 128,
  81. .mode.aes = TPM2_ALG_CFB,
  82. },
  83. .scheme = {
  84. .scheme = TPM2_ALG_ECDSA,
  85. .details = {.ecdsa =
  86. {.hashAlg = TPM2_ALG_SHA256}
  87. }
  88. },
  89. .curveID = TPM2_ECC_NIST_P256,
  90. .kdf = {.scheme =
  91. TPM2_ALG_NULL,.details = {}
  92. }
  93. },
  94. .unique.ecc = {
  95. .x = {.size = 0,.buffer = {}},
  96. .y = {.size = 0,.buffer = {}}
  97. }
  98. ,
  99. }
  100. };
  101. LOG_INFO("\nECC key will be created.");
  102. TPM2B_PUBLIC inPublic = inPublicECC;
  103. #else
  104. TPM2B_PUBLIC inPublicRSA = {
  105. .size = 0,
  106. .publicArea = {
  107. .type = TPM2_ALG_RSA,
  108. .nameAlg = TPM2_ALG_SHA256,
  109. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  110. TPMA_OBJECT_RESTRICTED |
  111. TPMA_OBJECT_DECRYPT |
  112. TPMA_OBJECT_FIXEDTPM |
  113. TPMA_OBJECT_FIXEDPARENT |
  114. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  115. .authPolicy = {
  116. .size = 0,
  117. },
  118. .parameters.rsaDetail = {
  119. .symmetric = {
  120. .algorithm = TPM2_ALG_AES,
  121. .keyBits.aes = 128,
  122. .mode.aes = TPM2_ALG_CFB,
  123. },
  124. .scheme = {
  125. .scheme =
  126. TPM2_ALG_NULL,
  127. },
  128. .keyBits = 2048,
  129. .exponent = 0,
  130. },
  131. .unique.rsa = {
  132. .size = 0,
  133. .buffer = {}
  134. ,
  135. }
  136. }
  137. };
  138. LOG_INFO("\nRSA key will be created.");
  139. TPM2B_PUBLIC inPublic = inPublicRSA;
  140. #endif
  141. TPM2B_DATA outsideInfo = {
  142. .size = 0,
  143. .buffer = {}
  144. ,
  145. };
  146. TPML_PCR_SELECTION creationPCR = {
  147. .count = 0,
  148. };
  149. TPM2B_AUTH authValue = {
  150. .size = 0,
  151. .buffer = {}
  152. };
  153. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  154. goto_if_error(r, "Error: TR_SetAuth", error);
  155. RSRC_NODE_T *objectHandle_node;
  156. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, session,
  157. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitive, &inPublic,
  158. &outsideInfo, &creationPCR, &objectHandle,
  159. &outPublic, &creationData, &creationHash,
  160. &creationTicket);
  161. goto_if_error(r, "Error esys create primary", error);
  162. r = esys_GetResourceObject(esys_context, objectHandle,
  163. &objectHandle_node);
  164. goto_if_error(r, "Error Esys GetResourceObject", error);
  165. ESYS_TR tpmHandle MAYBE_UNUSED = objectHandle_node->rsrc.handle;
  166. LOG_INFO("Created Primary with TPM handle 0x%08x...", tpmHandle);
  167. r = Esys_FlushContext(esys_context, objectHandle);
  168. goto_if_error(r, "Error during FlushContext", error);
  169. LOG_INFO("Done with handle 0x%08x...", tpmHandle);
  170. r = Esys_FlushContext(esys_context, session);
  171. goto_if_error(r, "Flushing context", error);
  172. Esys_Free(outPublic);
  173. Esys_Free(creationData);
  174. Esys_Free(creationHash);
  175. Esys_Free(creationTicket);
  176. return EXIT_SUCCESS;
  177. error:
  178. LOG_ERROR("\nError Code: %x\n", r);
  179. if (session != ESYS_TR_NONE) {
  180. if (Esys_FlushContext(esys_context, session) != TSS2_RC_SUCCESS) {
  181. LOG_ERROR("Cleanup session failed.");
  182. }
  183. }
  184. if (objectHandle != ESYS_TR_NONE) {
  185. if (Esys_FlushContext(esys_context, objectHandle) != TSS2_RC_SUCCESS) {
  186. LOG_ERROR("Cleanup objectHandle failed.");
  187. }
  188. }
  189. Esys_Free(outPublic);
  190. Esys_Free(creationData);
  191. Esys_Free(creationHash);
  192. Esys_Free(creationTicket);
  193. return EXIT_FAILURE;
  194. }
  195. int
  196. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  197. return test_esys_create_primary_hmac(esys_context);
  198. }