tpm2_zgen2phase.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include "files.h"
  3. #include "log.h"
  4. #include "object.h"
  5. #include "tpm2.h"
  6. #include "tpm2_tool.h"
  7. #include "tpm2_auth_util.h"
  8. #include "tpm2_alg_util.h"
  9. #include "tpm2_options.h"
  10. typedef struct tpm_ecdhzgen_ctx tpm_ecdhzgen_ctx;
  11. struct tpm_ecdhzgen_ctx {
  12. struct {
  13. const char *ctx_path;
  14. const char *auth_str;
  15. tpm2_loaded_object object;
  16. } ecc_key;
  17. const char *output_z1_path;
  18. const char *output_z2_path;
  19. TPM2B_ECC_POINT *Z1;
  20. TPM2B_ECC_POINT *Z2;
  21. const char *static_public_path;
  22. const char *ephemeral_public_path;
  23. TPM2B_ECC_POINT Q1;
  24. TPM2B_ECC_POINT Q2;
  25. UINT16 commit_counter;
  26. TPMI_ECC_KEY_EXCHANGE keyexchange_scheme;
  27. };
  28. static tpm_ecdhzgen_ctx ctx = {
  29. .keyexchange_scheme = TPM2_ALG_ECDH,
  30. };
  31. static bool on_option(char key, char *value) {
  32. bool result = true;
  33. switch (key) {
  34. case 'c':
  35. ctx.ecc_key.ctx_path = value;
  36. break;
  37. case 'p':
  38. ctx.ecc_key.auth_str = value;
  39. break;
  40. case 's':
  41. ctx.keyexchange_scheme = tpm2_alg_util_from_optarg(value,
  42. tpm2_alg_util_flags_sig);
  43. break;
  44. case 't':
  45. result = tpm2_util_string_to_uint16(value, &ctx.commit_counter);
  46. if (!result) {
  47. LOG_ERR("Could not convert commit counter to number, got: \"%s\"",
  48. value);
  49. return false;
  50. }
  51. break;
  52. case 0:
  53. ctx.static_public_path = value;
  54. break;
  55. case 1:
  56. ctx.ephemeral_public_path = value;
  57. break;
  58. case 2:
  59. ctx.output_z1_path = value;
  60. break;
  61. case 3:
  62. ctx.output_z2_path = value;
  63. break;
  64. };
  65. return true;
  66. }
  67. static bool tpm2_tool_onstart(tpm2_options **opts) {
  68. static struct option topts[] = {
  69. { "key-context", required_argument, NULL, 'c' },
  70. { "key-auth", required_argument, NULL, 'p' },
  71. { "scheme", required_argument, NULL, 's' },
  72. { "counter", required_argument, NULL, 't' },
  73. { "static-public", required_argument, NULL, 0 },
  74. { "ephemeral-public", required_argument, NULL, 1 },
  75. { "output-Z1", required_argument, NULL, 2 },
  76. { "output-Z2", required_argument, NULL, 3 },
  77. };
  78. *opts = tpm2_options_new("c:p:s:t:", ARRAY_LEN(topts), topts,
  79. on_option, NULL, 0);
  80. return *opts != NULL;
  81. }
  82. static tool_rc check_options(void) {
  83. if (ctx.keyexchange_scheme != TPM2_ALG_ECDH &&
  84. ctx.keyexchange_scheme != TPM2_ALG_ECMQV &&
  85. ctx.keyexchange_scheme != TPM2_ALG_SM2) {
  86. LOG_ERR("Unknown signing scheme");
  87. return tool_rc_general_error;
  88. }
  89. if (!ctx.ecc_key.ctx_path) {
  90. LOG_ERR("Specify an ecc key handle for context");
  91. return tool_rc_option_error;
  92. }
  93. if (!ctx.static_public_path) {
  94. LOG_ERR("Specify path to read the static public data from.");
  95. return tool_rc_option_error;
  96. }
  97. if (!ctx.ephemeral_public_path) {
  98. LOG_ERR("Specify path to read the ephemeral public data from.");
  99. return tool_rc_option_error;
  100. }
  101. if (!ctx.output_z1_path) {
  102. LOG_ERR("Specify path to save the Z1 data.");
  103. return tool_rc_option_error;
  104. }
  105. if (!ctx.output_z2_path) {
  106. LOG_ERR("Specify path to save the Z2 data.");
  107. return tool_rc_option_error;
  108. }
  109. return tool_rc_success;
  110. }
  111. static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
  112. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.ecc_key.ctx_path,
  113. ctx.ecc_key.auth_str, &ctx.ecc_key.object, false,
  114. TPM2_HANDLES_FLAGS_TRANSIENT|TPM2_HANDLES_FLAGS_PERSISTENT);
  115. if (rc != tool_rc_success) {
  116. return rc;
  117. }
  118. bool result = true;
  119. result = files_load_ecc_point(ctx.static_public_path, &ctx.Q1);
  120. if (!result) {
  121. LOG_ERR("Failed to load static public input ECC point Q1");
  122. return tool_rc_general_error;
  123. }
  124. result = files_load_ecc_point(ctx.ephemeral_public_path, &ctx.Q2);
  125. if (!result) {
  126. LOG_ERR("Failed to load static public input ECC point Q2");
  127. return tool_rc_general_error;
  128. }
  129. return tool_rc_success;
  130. }
  131. static tool_rc process_outputs(void) {
  132. bool result = files_save_ecc_point(ctx.Z1, ctx.output_z1_path);
  133. if (!result) {
  134. LOG_ERR("Failed to write out the ECC point Z1");
  135. return tool_rc_general_error;
  136. }
  137. result = files_save_ecc_point(ctx.Z2, ctx.output_z2_path);
  138. if (!result) {
  139. LOG_ERR("Failed to write out the ECC point Z2");
  140. return tool_rc_general_error;
  141. }
  142. return tool_rc_success;
  143. }
  144. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  145. UNUSED(flags);
  146. // Check input options and arguments
  147. tool_rc rc = check_options();
  148. if (rc != tool_rc_success) {
  149. return rc;
  150. }
  151. // Process inputs
  152. rc = process_inputs(ectx);
  153. if (rc != tool_rc_success) {
  154. return rc;
  155. }
  156. // ESAPI call
  157. rc = tpm2_zgen2phase(ectx, &ctx.ecc_key.object, &ctx.Q1, &ctx.Q2, &ctx.Z1,
  158. &ctx.Z2, ctx.keyexchange_scheme, ctx.commit_counter);
  159. if (rc != tool_rc_success) {
  160. return rc;
  161. }
  162. // Process ouputs
  163. rc = process_outputs();
  164. return rc;
  165. }
  166. // Register this tool with tpm2_tool.c
  167. TPM2_TOOL_REGISTER("zgen2phase", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)