load_external.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*############################################################################
  2. # Copyright 2017 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /*!
  17. * \file
  18. * \brief TPM2_LoadExternal command implementation.
  19. */
  20. #include "epid/member/tpm2/load_external.h"
  21. #include "epid/common/math/ecgroup.h"
  22. #include "epid/common/src/epid2params.h"
  23. #include "epid/common/src/memory.h"
  24. #include "epid/member/tpm2/ibm_tss/conversion.h"
  25. #include "epid/member/tpm2/ibm_tss/printtss.h"
  26. #include "epid/member/tpm2/ibm_tss/state.h"
  27. #include "tss2/TPM_Types.h"
  28. #include "tss2/tss.h"
  29. /// Handle Intel(R) EPID Error with Break
  30. #define BREAK_ON_EPID_ERROR(ret) \
  31. if (kEpidNoErr != (ret)) { \
  32. break; \
  33. }
  34. EpidStatus Tpm2LoadExternal(Tpm2Ctx* ctx, FpElemStr const* f_str) {
  35. EpidStatus sts = kEpidErr;
  36. TPM_RC rc = TPM_RC_SUCCESS;
  37. EcPoint* pub = NULL;
  38. FfElement* f = NULL;
  39. TPMI_ALG_HASH tpm_hash_alg = TPM_ALG_NULL;
  40. if (!ctx || !ctx->epid2_params || !f_str) {
  41. return kEpidBadArgErr;
  42. }
  43. do {
  44. LoadExternal_In in = {0};
  45. LoadExternal_Out out;
  46. G1ElemStr pub_str = {0};
  47. TPMS_ECC_PARMS* ecc_details = &in.inPublic.publicArea.parameters.eccDetail;
  48. EcGroup* G1 = ctx->epid2_params->G1;
  49. EcPoint* g1 = ctx->epid2_params->g1;
  50. sts = NewFfElement(ctx->epid2_params->Fp, &f);
  51. BREAK_ON_EPID_ERROR(sts);
  52. // verify that f is valid
  53. sts = ReadFfElement(ctx->epid2_params->Fp, f_str, sizeof(*f_str), f);
  54. BREAK_ON_EPID_ERROR(sts);
  55. if (ctx->key_handle) {
  56. FlushContext_In in_fc;
  57. in_fc.flushHandle = ctx->key_handle;
  58. TSS_Execute(ctx->tss, NULL, (COMMAND_PARAMETERS*)&in_fc, NULL,
  59. TPM_CC_FlushContext, TPM_RH_NULL, NULL, 0);
  60. if (rc != TPM_RC_SUCCESS) {
  61. print_tpm2_response_code("TPM2_FlushContext", rc);
  62. }
  63. ctx->key_handle = 0;
  64. }
  65. sts = NewEcPoint(G1, &pub);
  66. BREAK_ON_EPID_ERROR(sts);
  67. sts = EcExp(G1, g1, (BigNumStr const*)f_str, pub);
  68. BREAK_ON_EPID_ERROR(sts);
  69. sts = WriteEcPoint(G1, pub, &pub_str, sizeof(pub_str));
  70. BREAK_ON_EPID_ERROR(sts);
  71. tpm_hash_alg = EpidtoTpm2HashAlg(ctx->hash_alg);
  72. if (tpm_hash_alg == TPM_ALG_NULL) {
  73. sts = kEpidHashAlgorithmNotSupported;
  74. break;
  75. }
  76. in.hierarchy = TPM_RH_NULL;
  77. in.inPublic.size = sizeof(TPM2B_PUBLIC);
  78. in.inPublic.publicArea.type = TPM_ALG_ECC;
  79. in.inPublic.publicArea.nameAlg = tpm_hash_alg;
  80. in.inPublic.publicArea.objectAttributes.val =
  81. TPMA_OBJECT_NODA | TPMA_OBJECT_USERWITHAUTH | TPMA_OBJECT_SIGN;
  82. in.inPublic.publicArea.authPolicy.t.size = 0;
  83. ecc_details->symmetric.algorithm = TPM_ALG_NULL;
  84. ecc_details->scheme.scheme = TPM_ALG_ECDAA;
  85. ecc_details->scheme.details.ecdaa.hashAlg = tpm_hash_alg;
  86. ecc_details->scheme.details.ecdaa.count = 0;
  87. ecc_details->curveID = TPM_ECC_BN_P256;
  88. ecc_details->kdf.scheme = TPM_ALG_NULL;
  89. sts = ReadTpm2FfElement(&pub_str.x.data,
  90. &in.inPublic.publicArea.unique.ecc.x);
  91. BREAK_ON_EPID_ERROR(sts);
  92. sts = ReadTpm2FfElement(&pub_str.y.data,
  93. &in.inPublic.publicArea.unique.ecc.y);
  94. BREAK_ON_EPID_ERROR(sts);
  95. in.inPrivate.t.size = sizeof(in.inPrivate.t.sensitiveArea);
  96. in.inPrivate.t.sensitiveArea.sensitiveType = TPM_ALG_ECC;
  97. sts = ReadTpm2FfElement(&f_str->data,
  98. &in.inPrivate.t.sensitiveArea.sensitive.ecc);
  99. BREAK_ON_EPID_ERROR(sts);
  100. rc = TSS_Execute(ctx->tss, (RESPONSE_PARAMETERS*)&out,
  101. (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_LoadExternal,
  102. TPM_RH_NULL, NULL, 0);
  103. if (rc != TPM_RC_SUCCESS) {
  104. print_tpm2_response_code("TPM2_LoadExternal", rc);
  105. if (TPM_RC_BINDING == rc || TPM_RC_ECC_POINT == rc ||
  106. TPM_RC_KEY_SIZE == rc)
  107. sts = kEpidBadArgErr;
  108. else
  109. sts = kEpidErr;
  110. break;
  111. }
  112. ctx->key_handle = out.objectHandle;
  113. } while (0);
  114. DeleteEcPoint(&pub);
  115. DeleteFfElement(&f);
  116. return sts;
  117. }