tpm2_testparms.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <string.h>
  3. #include "log.h"
  4. #include "tpm2_tool.h"
  5. #include "tpm2_alg_util.h"
  6. #include "tpm2_options.h"
  7. typedef struct tpm_testparms_ctx tpm_testparms_ctx;
  8. struct tpm_testparms_ctx {
  9. TPMT_PUBLIC_PARMS inputalg;
  10. };
  11. static tpm_testparms_ctx ctx;
  12. static tool_rc tpm_testparms(ESYS_CONTEXT *ectx) {
  13. TSS2_RC rval = Esys_TestParms(ectx, ESYS_TR_NONE, ESYS_TR_NONE,
  14. ESYS_TR_NONE, &(ctx.inputalg));
  15. /*
  16. * TODO: this is a good candidate for flatten support via Tss2_RC_Decode(rval);
  17. */
  18. if (rval != TSS2_RC_SUCCESS) {
  19. if ((rval & (TPM2_RC_P | TPM2_RC_1)) == (TPM2_RC_P | TPM2_RC_1)) {
  20. rval &= ~(TPM2_RC_P | TPM2_RC_1);
  21. switch (rval) {
  22. case TPM2_RC_CURVE:
  23. LOG_ERR("Specified elliptic curve is unsupported");
  24. break;
  25. case TPM2_RC_HASH:
  26. LOG_ERR("Specified hash is unsupported");
  27. break;
  28. case TPM2_RC_SCHEME:
  29. LOG_ERR("Specified signing scheme is unsupported or "
  30. "incompatible");
  31. break;
  32. case TPM2_RC_KDF:
  33. LOG_ERR("Specified key derivation function is unsupported");
  34. break;
  35. case TPM2_RC_MGF:
  36. LOG_ERR("Specified mask generation function is unsupported");
  37. break;
  38. case TPM2_RC_KEY_SIZE:
  39. LOG_ERR("Specified key size is unsupported");
  40. break;
  41. case TPM2_RC_SYMMETRIC:
  42. LOG_ERR(
  43. "Specified symmetric algorithm or key length is "
  44. "unsupported");
  45. break;
  46. case TPM2_RC_ASYMMETRIC:
  47. LOG_ERR("Specified asymmetric algorithm is unsupported");
  48. break;
  49. case TPM2_RC_MODE:
  50. LOG_ERR("Specified symmetric mode unsupported");
  51. break;
  52. case TPM2_RC_VALUE:
  53. default:
  54. LOG_ERR("Unsupported algorithm specification");
  55. break;
  56. }
  57. return tool_rc_unsupported;
  58. }
  59. LOG_PERR(Esys_TestParms, rval);
  60. return tool_rc_general_error;
  61. }
  62. return tool_rc_success;
  63. }
  64. static bool on_arg(int argc, char **argv) {
  65. if (argc < 1) {
  66. LOG_ERR("Expected one algorithm specification, got: 0");
  67. return false;
  68. }
  69. TPM2B_PUBLIC algorithm = { 0 };
  70. if (!tpm2_alg_util_handle_ext_alg(argv[0], &algorithm)) {
  71. LOG_ERR("Invalid or unsupported by the tool : %s", argv[0]);
  72. return false;
  73. }
  74. ctx.inputalg.type = algorithm.publicArea.type;
  75. memcpy(&ctx.inputalg.parameters, &algorithm.publicArea.parameters,
  76. sizeof(TPMU_PUBLIC_PARMS));
  77. return true;
  78. }
  79. static bool tpm2_tool_onstart(tpm2_options **opts) {
  80. *opts = tpm2_options_new(NULL, 0, NULL, NULL, on_arg, 0);
  81. return *opts != NULL;
  82. }
  83. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  84. UNUSED(flags);
  85. return tpm_testparms(ectx);
  86. }
  87. // Register this tool with tpm2_tool.c
  88. TPM2_TOOL_REGISTER("testparms", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)