tpm2_commit.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <string.h>
  3. #include "files.h"
  4. #include "log.h"
  5. #include "object.h"
  6. #include "tpm2.h"
  7. #include "tpm2_tool.h"
  8. #include "tpm2_auth_util.h"
  9. #include "tpm2_options.h"
  10. typedef struct tpm_ecephemeral_ctx tpm_ecephemeral_ctx;
  11. struct tpm_ecephemeral_ctx {
  12. struct {
  13. const char *ctx_path;
  14. const char *auth_str;
  15. tpm2_loaded_object object;
  16. } signing_key;
  17. char *basepoint_x_coordinate_data_path;
  18. char *basepoint_y_data_path;
  19. char *eccpoint_M_data_path;
  20. char *eccpoint_K_data_path;
  21. char *eccpoint_L_data_path;
  22. char *eccpoint_E_data_path;
  23. char *commit_counter_path;
  24. TPM2B_ECC_POINT P1;
  25. TPM2B_SENSITIVE_DATA s2;
  26. TPM2B_ECC_PARAMETER y2;
  27. TPM2B_ECC_POINT *K;
  28. TPM2B_ECC_POINT *L;
  29. TPM2B_ECC_POINT *E;
  30. uint16_t counter;
  31. };
  32. static tpm_ecephemeral_ctx ctx;
  33. static bool on_option(char key, char *value) {
  34. switch (key) {
  35. case 'p':
  36. ctx.signing_key.auth_str = value;
  37. break;
  38. case 'c':
  39. ctx.signing_key.ctx_path = value;
  40. break;
  41. case 0:
  42. ctx.basepoint_y_data_path = value;
  43. break;
  44. case 1:
  45. ctx.eccpoint_M_data_path = value;
  46. break;
  47. case 2:
  48. ctx.eccpoint_K_data_path = value;
  49. break;
  50. case 3:
  51. ctx.eccpoint_L_data_path = value;
  52. break;
  53. case 'u':
  54. ctx.eccpoint_E_data_path = value;
  55. break;
  56. case 't':
  57. ctx.commit_counter_path = value;
  58. break;
  59. };
  60. return true;
  61. }
  62. static bool on_args(int argc, char **argv) {
  63. if (argc > 1) {
  64. LOG_ERR("Specify single argument with file input containing the "
  65. "octet array used to derive x-coordinate of a base point");
  66. return false;
  67. }
  68. ctx.basepoint_x_coordinate_data_path = strcmp("-", argv[0]) ? argv[0] : NULL;
  69. return files_load_bytes_from_buffer_or_file_or_stdin(NULL,
  70. ctx.basepoint_x_coordinate_data_path, &ctx.s2.size, ctx.s2.buffer);
  71. }
  72. static bool tpm2_tool_onstart(tpm2_options **opts) {
  73. static struct option topts[] = {
  74. { "auth", required_argument, NULL, 'p' },
  75. { "context", required_argument, NULL, 'c' },
  76. { "basepoint-y", required_argument, NULL, 0 },
  77. { "eccpoint-P", required_argument, NULL, 1 },
  78. { "eccpoint-K", required_argument, NULL, 2 },
  79. { "eccpoint-L", required_argument, NULL, 3 },
  80. { "public", required_argument, NULL, 'u' },
  81. { "counter", required_argument, NULL, 't' },
  82. };
  83. *opts = tpm2_options_new("p:c:t:u:", ARRAY_LEN(topts), topts,
  84. on_option, on_args, 0);
  85. return *opts != NULL;
  86. }
  87. static tool_rc check_options(void) {
  88. if (!ctx.signing_key.ctx_path) {
  89. LOG_ERR("Specify a signing key");
  90. return tool_rc_option_error;
  91. }
  92. if (ctx.basepoint_y_data_path && !ctx.basepoint_x_coordinate_data_path) {
  93. LOG_ERR("Specify parameter data for basepoint X coordinate derivation");
  94. return tool_rc_option_error;
  95. }
  96. return tool_rc_success;
  97. }
  98. static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
  99. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.signing_key.ctx_path,
  100. ctx.signing_key.auth_str, &ctx.signing_key.object, false,
  101. TPM2_HANDLES_FLAGS_TRANSIENT|TPM2_HANDLES_FLAGS_PERSISTENT);
  102. if (rc != tool_rc_success) {
  103. return rc;
  104. }
  105. bool result = true;
  106. if (ctx.basepoint_x_coordinate_data_path) {
  107. result = files_load_ecc_point(ctx.eccpoint_M_data_path, &ctx.P1);
  108. }
  109. if (!result) {
  110. LOG_ERR("Failed to load input ECC point P1");
  111. return tool_rc_general_error;
  112. }
  113. if (ctx.basepoint_y_data_path) {
  114. result = files_load_ecc_parameter(ctx.basepoint_y_data_path, &ctx.y2);
  115. }
  116. if (!result) {
  117. LOG_ERR("Failed to load input ECC parameter y2");
  118. return tool_rc_general_error;
  119. }
  120. return tool_rc_success;
  121. }
  122. static tool_rc process_outputs(void) {
  123. bool result = files_save_ecc_point(ctx.K, ctx.eccpoint_K_data_path);
  124. if (!result) {
  125. LOG_ERR("Failed to write out the ECC point K");
  126. return tool_rc_general_error;
  127. }
  128. result = files_save_ecc_point(ctx.L, ctx.eccpoint_L_data_path);
  129. if (!result) {
  130. LOG_ERR("Failed to write out the ECC point L");
  131. return tool_rc_general_error;
  132. }
  133. result = files_save_ecc_point(ctx.E, ctx.eccpoint_E_data_path);
  134. if (!result) {
  135. LOG_ERR("Failed to write out the ECC point E");
  136. return tool_rc_general_error;
  137. }
  138. FILE *fp = fopen(ctx.commit_counter_path, "wb");
  139. result = files_write_16(fp, ctx.counter);
  140. fclose(fp);
  141. if (!result) {
  142. LOG_ERR("Failed to write out the ECC commit count");
  143. return tool_rc_general_error;
  144. }
  145. return tool_rc_success;
  146. }
  147. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  148. UNUSED(flags);
  149. // Check input options and arguments
  150. tool_rc rc = check_options();
  151. if (rc != tool_rc_success) {
  152. return rc;
  153. }
  154. // Process inputs
  155. rc = process_inputs(ectx);
  156. if (rc != tool_rc_success) {
  157. return rc;
  158. }
  159. // ESAPI call
  160. rc = tpm2_commit(ectx, &ctx.signing_key.object, &ctx.P1, &ctx.s2, &ctx.y2,
  161. &ctx.K, &ctx.L, &ctx.E, &ctx.counter);
  162. if (rc != tool_rc_success) {
  163. return rc;
  164. }
  165. // Process outputs
  166. rc = process_outputs();
  167. return rc;
  168. }
  169. // Register this tool with tpm2_tool.c
  170. TPM2_TOOL_REGISTER("commit", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)