tpm2_ecdhzgen.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_options.h"
  9. typedef struct tpm_ecdhzgen_ctx tpm_ecdhzgen_ctx;
  10. struct tpm_ecdhzgen_ctx {
  11. struct {
  12. const char *ctx_path;
  13. const char *auth_str;
  14. tpm2_loaded_object object;
  15. } ecc_key;
  16. const char *ecdh_pub_path;
  17. const char *ecdh_Z_path;
  18. TPM2B_ECC_POINT *Z;
  19. TPM2B_ECC_POINT Q;
  20. };
  21. static tpm_ecdhzgen_ctx ctx;
  22. static bool on_option(char key, char *value) {
  23. switch (key) {
  24. case 'c':
  25. ctx.ecc_key.ctx_path = value;
  26. break;
  27. case 'p':
  28. ctx.ecc_key.auth_str = value;
  29. break;
  30. case 'u':
  31. ctx.ecdh_pub_path = value;
  32. break;
  33. case 'o':
  34. ctx.ecdh_Z_path = value;
  35. break;
  36. };
  37. return true;
  38. }
  39. static bool tpm2_tool_onstart(tpm2_options **opts) {
  40. static struct option topts[] = {
  41. { "key-context", required_argument, NULL, 'c' },
  42. { "key-auth", required_argument, NULL, 'p' },
  43. { "public", required_argument, NULL, 'u' },
  44. { "output", required_argument, NULL, 'o' },
  45. };
  46. *opts = tpm2_options_new("c:p:u:o:", ARRAY_LEN(topts), topts,
  47. on_option, NULL, 0);
  48. return *opts != NULL;
  49. }
  50. static tool_rc check_options(void) {
  51. if (!ctx.ecc_key.ctx_path) {
  52. LOG_ERR("Specify an ecc public key handle for context");
  53. return tool_rc_option_error;
  54. }
  55. if (!ctx.ecdh_Z_path) {
  56. LOG_ERR("Specify path to save the ecdh secret or Z point");
  57. return tool_rc_option_error;
  58. }
  59. return tool_rc_success;
  60. }
  61. static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
  62. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.ecc_key.ctx_path,
  63. ctx.ecc_key.auth_str, &ctx.ecc_key.object, false,
  64. TPM2_HANDLES_FLAGS_TRANSIENT|TPM2_HANDLES_FLAGS_PERSISTENT);
  65. if (rc != tool_rc_success) {
  66. LOG_ERR("Failed to load object/ auth");
  67. return rc;
  68. }
  69. bool result = true;
  70. result = files_load_ecc_point(ctx.ecdh_pub_path, &ctx.Q);
  71. if (!result) {
  72. LOG_ERR("Failed to load public input ECC point Q");
  73. return tool_rc_general_error;
  74. }
  75. return tool_rc_success;
  76. }
  77. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  78. UNUSED(flags);
  79. // Check input options and arguments
  80. tool_rc rc = check_options();
  81. if (rc != tool_rc_success) {
  82. return rc;
  83. }
  84. // Process inputs
  85. rc = process_inputs(ectx);
  86. if (rc != tool_rc_success) {
  87. return rc;
  88. }
  89. // ESAPI call
  90. rc = tpm2_ecdhzgen(ectx, &ctx.ecc_key.object, &ctx.Z, &ctx.Q);
  91. if (rc != tool_rc_success) {
  92. return rc;
  93. }
  94. // Process outputs
  95. bool result = files_save_ecc_point(ctx.Z, ctx.ecdh_Z_path);
  96. if (!result) {
  97. LOG_ERR("Failed to write out the public");
  98. return tool_rc_general_error;
  99. }
  100. return tool_rc_success;
  101. }
  102. // Register this tool with tpm2_tool.c
  103. TPM2_TOOL_REGISTER("ecdhzgen", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)