tpm2_ecdhkeygen.c 2.8 KB

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