esys-zgen-2phase.int.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_ECDH_ZGen.
  17. *
  18. * The test is based on an ECC key created with Esys_CreatePrimary
  19. * and data produced by the command Esys_EC_Ephemeral.
  20. *
  21. * Tested ESYS commands:
  22. * - Esys_CreatePrimary() (M)
  23. * - Esys_ECDH_ZGen() (M)
  24. * - Esys_EC_Ephemeral() (F)
  25. * - Esys_FlushContext() (M)
  26. * - Esys_StartAuthSession() (M)
  27. * - Esys_ZGen_2Phase() (O)
  28. *
  29. * @param[in,out] esys_context The ESYS_CONTEXT.
  30. * @retval EXIT_FAILURE
  31. * @retval EXIT_SKIP
  32. * @retval EXIT_SUCCESS
  33. */
  34. int
  35. test_esys_zgen_2phase(ESYS_CONTEXT * esys_context)
  36. {
  37. TSS2_RC r;
  38. ESYS_TR eccHandle = ESYS_TR_NONE;
  39. int failure_return = EXIT_FAILURE;
  40. ESYS_TR session = ESYS_TR_NONE;
  41. TPMT_SYM_DEF symmetric = {
  42. .algorithm = TPM2_ALG_AES,
  43. .keyBits = { .aes = 128 },
  44. .mode = {.aes = TPM2_ALG_CFB}
  45. };
  46. TPMA_SESSION sessionAttributes;
  47. TPM2B_NONCE nonceCaller = {
  48. .size = 20,
  49. .buffer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  50. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
  51. };
  52. TPM2B_PUBLIC *outPublic = NULL;
  53. TPM2B_CREATION_DATA *creationData = NULL;
  54. TPM2B_DIGEST *creationHash = NULL;
  55. TPMT_TK_CREATION *creationTicket = NULL;
  56. TPM2B_ECC_POINT *outZ1 = NULL;
  57. TPM2B_ECC_POINT *outZ2 = NULL;
  58. TPM2B_ECC_POINT *Q = NULL;
  59. memset(&sessionAttributes, 0, sizeof sessionAttributes);
  60. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  61. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  62. &nonceCaller,
  63. TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA256,
  64. &session);
  65. goto_if_error(r, "Error: During initialization of session", error);
  66. TPM2B_SENSITIVE_CREATE inSensitive = {
  67. .size = 0,
  68. .sensitive = {
  69. .userAuth = {
  70. .size = 0,
  71. .buffer = {0}
  72. },
  73. .data = {
  74. .size = 0,
  75. .buffer = {0}
  76. }
  77. }
  78. };
  79. TPM2B_PUBLIC inPublicECC = {
  80. .size = 0,
  81. .publicArea = {
  82. .type = TPM2_ALG_ECC,
  83. .nameAlg = TPM2_ALG_SHA256,
  84. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  85. TPMA_OBJECT_DECRYPT |
  86. TPMA_OBJECT_FIXEDTPM |
  87. TPMA_OBJECT_FIXEDPARENT |
  88. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  89. .authPolicy = {
  90. .size = 0,
  91. },
  92. .parameters.eccDetail = {
  93. .symmetric = {
  94. .algorithm = TPM2_ALG_NULL,
  95. .keyBits.aes = 128,
  96. .mode.aes = TPM2_ALG_CFB,
  97. },
  98. .scheme = {
  99. .scheme = TPM2_ALG_ECDH,
  100. .details = {.ecdh = {.hashAlg = TPM2_ALG_SHA256}
  101. }
  102. },
  103. .curveID = TPM2_ECC_NIST_P256,
  104. .kdf = {.scheme = TPM2_ALG_NULL }
  105. },
  106. .unique.ecc = {
  107. .x = {.size = 0,.buffer = {}},
  108. .y = {.size = 0,.buffer = {}}
  109. }
  110. ,
  111. }
  112. };
  113. LOG_INFO("\nECC key will be created.");
  114. TPM2B_PUBLIC inPublic = inPublicECC;
  115. TPM2B_DATA outsideInfo = {
  116. .size = 0,
  117. .buffer = {}
  118. ,
  119. };
  120. TPML_PCR_SELECTION creationPCR = {
  121. .count = 0,
  122. };
  123. TPM2B_AUTH authValue = {
  124. .size = 0,
  125. .buffer = {}
  126. };
  127. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  128. goto_if_error(r, "Error: TR_SetAuth", error);
  129. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, session,
  130. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitive, &inPublic,
  131. &outsideInfo, &creationPCR, &eccHandle,
  132. &outPublic, &creationData, &creationHash,
  133. &creationTicket);
  134. goto_if_error(r, "Error esys create primary", error);
  135. TPMI_ECC_CURVE curveID = TPM2_ECC_NIST_P256;
  136. UINT16 counter;
  137. r = Esys_EC_Ephemeral(
  138. esys_context,
  139. ESYS_TR_NONE,
  140. ESYS_TR_NONE,
  141. ESYS_TR_NONE,
  142. curveID,
  143. &Q,
  144. &counter);
  145. if ((r == TPM2_RC_COMMAND_CODE) ||
  146. (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_RC_LAYER)) ||
  147. (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_TPM_RC_LAYER))) {
  148. LOG_WARNING("Command TPM2_Ephemeral not supported by TPM.");
  149. failure_return = EXIT_SKIP;
  150. goto error;
  151. }
  152. goto_if_error(r, "Error: EC_Ephemeral", error);
  153. TPM2B_ECC_POINT inQsB = {
  154. .size = 0,
  155. .point = outPublic->publicArea.unique.ecc
  156. };
  157. TPMI_ECC_KEY_EXCHANGE inScheme = TPM2_ALG_ECDH;
  158. TPM2B_ECC_POINT inQeB = *Q;
  159. r = Esys_ZGen_2Phase(
  160. esys_context,
  161. eccHandle,
  162. ESYS_TR_PASSWORD,
  163. ESYS_TR_NONE,
  164. ESYS_TR_NONE,
  165. &inQsB,
  166. &inQeB,
  167. inScheme,
  168. counter,
  169. &outZ1,
  170. &outZ2);
  171. if ((r == TPM2_RC_COMMAND_CODE) ||
  172. (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_RC_LAYER)) ||
  173. (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_TPM_RC_LAYER))) {
  174. LOG_WARNING("Command TPM2_ZGen_2Phase not supported by TPM.");
  175. failure_return = EXIT_SKIP;
  176. goto error;
  177. }
  178. goto_if_error(r, "Error: ZGen_2Phase", error);
  179. r = Esys_FlushContext(esys_context, eccHandle);
  180. goto_if_error(r, "Flushing context", error);
  181. r = Esys_FlushContext(esys_context, session);
  182. goto_if_error(r, "Flushing context", error);
  183. Esys_Free(outPublic);
  184. Esys_Free(creationData);
  185. Esys_Free(creationHash);
  186. Esys_Free(creationTicket);
  187. Esys_Free(outZ1);
  188. Esys_Free(outZ2);
  189. Esys_Free(Q);
  190. return EXIT_SUCCESS;
  191. error:
  192. LOG_ERROR("\nError Code: %x\n", r);
  193. if (eccHandle != ESYS_TR_NONE) {
  194. if (Esys_FlushContext(esys_context, eccHandle) != TSS2_RC_SUCCESS) {
  195. LOG_ERROR("Cleanup eccHandle failed.");
  196. }
  197. }
  198. if (session != ESYS_TR_NONE) {
  199. if (Esys_FlushContext(esys_context, session) != TSS2_RC_SUCCESS) {
  200. LOG_ERROR("Cleanup session failed.");
  201. }
  202. }
  203. Esys_Free(outPublic);
  204. Esys_Free(creationData);
  205. Esys_Free(creationHash);
  206. Esys_Free(creationTicket);
  207. Esys_Free(outZ1);
  208. Esys_Free(outZ2);
  209. Esys_Free(Q);
  210. return failure_return;
  211. }
  212. int
  213. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  214. return test_esys_zgen_2phase(esys_context);
  215. }