esys-ecdh-zgen.int.c 6.3 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. #define LOGMODULE test
  13. #include "util/log.h"
  14. #include "util/aux_util.h"
  15. /** This test is intended to test Esys_ECDH_ZGen.
  16. * based on an ECC key
  17. * created with Esys_CreatePrimary and a dummy ECC point.
  18. *
  19. * Tested ESYS commands:
  20. * - Esys_CreatePrimary() (M)
  21. * - Esys_ECDH_ZGen() (M)
  22. * - Esys_FlushContext() (M)
  23. * - Esys_StartAuthSession() (M)
  24. *
  25. * @param[in,out] esys_context The ESYS_CONTEXT.
  26. * @retval EXIT_FAILURE
  27. * @retval EXIT_SUCCESS
  28. */
  29. int
  30. test_esys_ecdh_zgen(ESYS_CONTEXT * esys_context)
  31. {
  32. TSS2_RC r;
  33. ESYS_TR eccHandle = ESYS_TR_NONE;
  34. ESYS_TR session = ESYS_TR_NONE;
  35. TPMT_SYM_DEF symmetric = {
  36. .algorithm = TPM2_ALG_AES,
  37. .keyBits = { .aes = 128 },
  38. .mode = {.aes = TPM2_ALG_CFB}
  39. };
  40. TPMA_SESSION sessionAttributes;
  41. TPM2B_NONCE nonceCaller = {
  42. .size = 20,
  43. .buffer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
  44. };
  45. TPM2B_PUBLIC *outPublic = NULL;
  46. TPM2B_CREATION_DATA *creationData = NULL;
  47. TPM2B_DIGEST *creationHash = NULL;
  48. TPMT_TK_CREATION *creationTicket = NULL;
  49. TPM2B_ECC_POINT *outPoint = NULL;
  50. memset(&sessionAttributes, 0, sizeof sessionAttributes);
  51. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  52. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  53. &nonceCaller,
  54. TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA256,
  55. &session);
  56. goto_if_error(r, "Error: During initialization of session", error);
  57. TPM2B_SENSITIVE_CREATE inSensitive = {
  58. .size = 0,
  59. .sensitive = {
  60. .userAuth = {
  61. .size = 0,
  62. .buffer = {0}
  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. .keyBits.aes = 128,
  87. .mode.aes = TPM2_ALG_CFB,
  88. },
  89. .scheme = {
  90. .scheme = TPM2_ALG_ECDH,
  91. .details = {.ecdh = {.hashAlg = TPM2_ALG_SHA256}
  92. }
  93. },
  94. .curveID = TPM2_ECC_NIST_P256,
  95. .kdf = {.scheme = TPM2_ALG_NULL }
  96. },
  97. .unique.ecc = {
  98. .x = {.size = 0,.buffer = {}},
  99. .y = {.size = 0,.buffer = {}}
  100. }
  101. ,
  102. }
  103. };
  104. LOG_INFO("\nECC key will be created.");
  105. TPM2B_PUBLIC inPublic = inPublicECC;
  106. TPM2B_DATA outsideInfo = {
  107. .size = 0,
  108. .buffer = {}
  109. ,
  110. };
  111. TPML_PCR_SELECTION creationPCR = {
  112. .count = 0,
  113. };
  114. TPM2B_AUTH authValue = {
  115. .size = 0,
  116. .buffer = {}
  117. };
  118. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  119. goto_if_error(r, "Error: TR_SetAuth", error);
  120. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, session,
  121. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitive, &inPublic,
  122. &outsideInfo, &creationPCR, &eccHandle,
  123. &outPublic, &creationData, &creationHash,
  124. &creationTicket);
  125. goto_if_error(r, "Error esys create primary", error);
  126. TPM2B_ECC_POINT inPoint= {
  127. .size = 0,
  128. .point = {
  129. .x = {
  130. .size = 32,
  131. .buffer = {
  132. 0x25, 0xdb, 0x1f, 0x8b, 0xbc, 0xfa, 0xbc, 0x31,
  133. 0xf8, 0x17, 0x6a, 0xcb, 0xb2, 0xf8, 0x40, 0xa3,
  134. 0xb6, 0xa5, 0xd3, 0x40, 0x65, 0x9d, 0x37, 0xee,
  135. 0xd9, 0xfd, 0x52, 0x47, 0xf5, 0x14, 0xd5, 0x98
  136. },
  137. },
  138. .y = {
  139. .size = 32,
  140. .buffer = {
  141. 0xed, 0x62, 0x3e, 0x3d, 0xd2, 0x09, 0x08, 0xcf,
  142. 0x58, 0x3c, 0x81, 0x4b, 0xbf, 0x65, 0x7e, 0x08,
  143. 0xab, 0x9f, 0x40, 0xff, 0xea, 0x51, 0xda, 0x21,
  144. 0x29, 0x8c, 0xe2, 0x4d, 0xeb, 0x34, 0x4c, 0xcc
  145. }
  146. }
  147. }
  148. };
  149. r = Esys_ECDH_ZGen(
  150. esys_context,
  151. eccHandle,
  152. ESYS_TR_PASSWORD,
  153. ESYS_TR_NONE,
  154. ESYS_TR_NONE,
  155. &inPoint,
  156. &outPoint);
  157. goto_if_error(r, "Error: ECDH_ZGen", error);
  158. r = Esys_FlushContext(esys_context, eccHandle);
  159. goto_if_error(r, "Error during FlushContext", error);
  160. r = Esys_FlushContext(esys_context, session);
  161. goto_if_error(r, "Flushing context", error);
  162. Esys_Free(outPublic);
  163. Esys_Free(creationData);
  164. Esys_Free(creationHash);
  165. Esys_Free(creationTicket);
  166. Esys_Free(outPoint);
  167. return EXIT_SUCCESS;
  168. error:
  169. LOG_ERROR("\nError Code: %x\n", r);
  170. if (session != ESYS_TR_NONE) {
  171. if (Esys_FlushContext(esys_context, session) != TSS2_RC_SUCCESS) {
  172. LOG_ERROR("Cleanup session failed.");
  173. }
  174. }
  175. if (eccHandle != ESYS_TR_NONE) {
  176. if (Esys_FlushContext(esys_context, eccHandle) != TSS2_RC_SUCCESS) {
  177. LOG_ERROR("Cleanup eccHandle failed.");
  178. }
  179. }
  180. Esys_Free(outPublic);
  181. Esys_Free(creationData);
  182. Esys_Free(creationHash);
  183. Esys_Free(creationTicket);
  184. Esys_Free(outPoint);
  185. return EXIT_FAILURE;
  186. }
  187. int
  188. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  189. return test_esys_ecdh_zgen(esys_context);
  190. }