esys-commit.int.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #include "test-esys.h"
  13. #define LOGMODULE test
  14. #include "util/log.h"
  15. #include "util/aux_util.h"
  16. /** This test is intended to test Esys_Commit.
  17. * based on an ECC key
  18. * created with Esys_CreatePrimary Esys_Commit is called with a point
  19. * from the primary key.
  20. *
  21. * Tested ESYS commands:
  22. * - Esys_Commit() (M)
  23. * - Esys_CreatePrimary() (M)
  24. * - Esys_FlushContext() (M)
  25. * - Esys_StartAuthSession() (M)
  26. *
  27. * @param[in,out] esys_context The ESYS_CONTEXT.
  28. * @retval EXIT_FAILURE
  29. * @retval EXIT_SKIP
  30. * @retval EXIT_SUCCESS
  31. */
  32. int
  33. test_esys_commit(ESYS_CONTEXT * esys_context)
  34. {
  35. TSS2_RC r;
  36. ESYS_TR eccHandle = ESYS_TR_NONE;
  37. ESYS_TR session = ESYS_TR_NONE;
  38. int failure_return = EXIT_FAILURE;
  39. TPM2B_PUBLIC *outPublic = NULL;
  40. TPM2B_CREATION_DATA *creationData = NULL;
  41. TPM2B_DIGEST *creationHash = NULL;
  42. TPMT_TK_CREATION *creationTicket = NULL;
  43. TPM2B_ECC_POINT *K = NULL;
  44. TPM2B_ECC_POINT *L = NULL;
  45. TPM2B_ECC_POINT *E = NULL;
  46. TPMT_SYM_DEF symmetric = {
  47. .algorithm = TPM2_ALG_AES,
  48. .keyBits = { .aes = 128 },
  49. .mode = {.aes = TPM2_ALG_CFB}
  50. };
  51. TPMA_SESSION sessionAttributes;
  52. TPM2B_NONCE nonceCaller = {
  53. .size = 20,
  54. .buffer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
  55. };
  56. memset(&sessionAttributes, 0, sizeof sessionAttributes);
  57. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  58. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  59. &nonceCaller,
  60. TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA256,
  61. &session);
  62. goto_if_error(r, "Error: During initialization of session", error);
  63. TPM2B_SENSITIVE_CREATE inSensitive = {
  64. .size = 0,
  65. .sensitive = {
  66. .userAuth = {
  67. .size = 0,
  68. .buffer = {0}
  69. },
  70. .data = {
  71. .size = 0,
  72. .buffer = {0}
  73. }
  74. }
  75. };
  76. TPM2B_PUBLIC inPublicECC = {
  77. .size = 0,
  78. .publicArea = {
  79. .type = TPM2_ALG_ECC,
  80. .nameAlg = TPM2_ALG_SHA256,
  81. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  82. TPMA_OBJECT_SIGN_ENCRYPT |
  83. TPMA_OBJECT_FIXEDTPM |
  84. TPMA_OBJECT_FIXEDPARENT |
  85. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  86. .authPolicy = {
  87. .size = 0,
  88. },
  89. .parameters.eccDetail = {
  90. .symmetric = {
  91. .algorithm = TPM2_ALG_NULL,
  92. .keyBits.aes = 128,
  93. .mode.aes = TPM2_ALG_CFB,
  94. },
  95. .scheme = {
  96. .scheme = TPM2_ALG_ECDAA,
  97. .details = {.ecdh = {.hashAlg = TPM2_ALG_SHA256}
  98. }
  99. },
  100. .curveID = TPM2_ECC_NIST_P256,
  101. .kdf = {.scheme = TPM2_ALG_NULL }
  102. },
  103. .unique.ecc = {
  104. .x = {.size = 0,.buffer = {}},
  105. .y = {.size = 0,.buffer = {}}
  106. }
  107. ,
  108. }
  109. };
  110. LOG_INFO("\nECC key will be created.");
  111. TPM2B_PUBLIC inPublic = inPublicECC;
  112. TPM2B_DATA outsideInfo = {
  113. .size = 0,
  114. .buffer = {}
  115. ,
  116. };
  117. TPML_PCR_SELECTION creationPCR = {
  118. .count = 0,
  119. };
  120. TPM2B_AUTH authValue = {
  121. .size = 0,
  122. .buffer = {}
  123. };
  124. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  125. goto_if_error(r, "Error: TR_SetAuth", error);
  126. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, session,
  127. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitive, &inPublic,
  128. &outsideInfo, &creationPCR, &eccHandle,
  129. &outPublic, &creationData, &creationHash,
  130. &creationTicket);
  131. if (base_rc(r) == (TPM2_RC_SCHEME | TPM2_RC_P | TPM2_RC_2)) {
  132. LOG_WARNING("Scheme ECDAA not supported by TPM.");
  133. failure_return = EXIT_SKIP;
  134. goto error;
  135. }
  136. goto_if_error(r, "Error esys create primary", error);
  137. TPM2B_ECC_POINT P1 = {0};
  138. TPM2B_SENSITIVE_DATA s2 = {0};
  139. TPM2B_ECC_PARAMETER y2 = {0};
  140. UINT16 counter;
  141. r = Esys_Commit(esys_context, eccHandle,
  142. session, ESYS_TR_NONE, ESYS_TR_NONE,
  143. &P1, &s2, &y2,
  144. &K, &L, &E, &counter);
  145. goto_if_error(r, "Error: Commit", error);
  146. r = Esys_FlushContext(esys_context, eccHandle);
  147. goto_if_error(r, "Flushing context", error);
  148. eccHandle = ESYS_TR_NONE;
  149. r = Esys_FlushContext(esys_context, session);
  150. goto_if_error(r, "Error: FlushContext", error);
  151. session = ESYS_TR_NONE;
  152. Esys_Free(outPublic);
  153. Esys_Free(creationData);
  154. Esys_Free(creationHash);
  155. Esys_Free(creationTicket);
  156. Esys_Free(K);
  157. Esys_Free(L);
  158. Esys_Free(E);
  159. return EXIT_SUCCESS;
  160. error:
  161. LOG_ERROR("\nError Code: %x\n", r);
  162. if (eccHandle != ESYS_TR_NONE) {
  163. if (Esys_FlushContext(esys_context, eccHandle) != TSS2_RC_SUCCESS) {
  164. LOG_ERROR("Cleanup eccHandle failed.");
  165. }
  166. }
  167. if (session != ESYS_TR_NONE) {
  168. if (Esys_FlushContext(esys_context, session) != TSS2_RC_SUCCESS) {
  169. LOG_ERROR("Cleanup session failed.");
  170. }
  171. }
  172. Esys_Free(outPublic);
  173. Esys_Free(creationData);
  174. Esys_Free(creationHash);
  175. Esys_Free(creationTicket);
  176. Esys_Free(K);
  177. Esys_Free(L);
  178. Esys_Free(E);
  179. return failure_return;
  180. }
  181. int
  182. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  183. return test_esys_commit(esys_context);
  184. }