esys-ecdh-keygen.int.c 5.5 KB

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