context.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /// TPM context implementation.
  17. /*! \file */
  18. #include "epid/member/tpm2/context.h"
  19. #include <tss2/TPM_Types.h>
  20. #include <tss2/tss.h>
  21. #include "epid/common/math/finitefield.h"
  22. #include "epid/common/src/epid2params.h"
  23. #include "epid/common/src/memory.h"
  24. #include "epid/member/tpm2/getrandom.h"
  25. #include "epid/member/tpm2/ibm_tss/printtss.h"
  26. #include "epid/member/tpm2/ibm_tss/state.h"
  27. #include "epid/member/tpm_member.h"
  28. /// Handle Intel(R) EPID Error with Break
  29. #define BREAK_ON_EPID_ERROR(ret) \
  30. if (kEpidNoErr != (ret)) { \
  31. break; \
  32. }
  33. /// Deletes key from TPM
  34. /*!
  35. \param[in,out] ctx
  36. TPM context.
  37. \returns ::EpidStatus
  38. */
  39. void Tpm2FlushKey(Tpm2Ctx* ctx);
  40. /// Flag that indicates that context was already created
  41. bool is_context_already_created = false;
  42. /// Internal Random function as a BitSupplier
  43. static int __STDCALL tpm2_rnd_func(unsigned int* rand_data, int num_bits,
  44. void* user_data) {
  45. return Tpm2GetRandom((Tpm2Ctx*)user_data, num_bits, rand_data);
  46. }
  47. EpidStatus Tpm2CreateContext(MemberParams const* params,
  48. Epid2Params_ const* epid2_params,
  49. BitSupplier* rnd_func, void** rnd_param,
  50. const FpElemStr** f, Tpm2Ctx** ctx) {
  51. EpidStatus sts = kEpidNoErr;
  52. TPM_RC rc = TPM_RC_FAILURE;
  53. Tpm2Ctx* tpm_ctx = NULL;
  54. FfElement* ff_elem = NULL;
  55. if (!params || !epid2_params || !rnd_func || !rnd_param || !f || !ctx) {
  56. return kEpidBadArgErr;
  57. }
  58. if (is_context_already_created) {
  59. return kEpidBadArgErr;
  60. }
  61. is_context_already_created = true;
  62. tpm_ctx = SAFE_ALLOC(sizeof(Tpm2Ctx));
  63. if (!tpm_ctx) {
  64. return kEpidMemAllocErr;
  65. }
  66. do {
  67. if (params->f) {
  68. FiniteField* Fp = epid2_params->Fp;
  69. // Validate f
  70. sts = NewFfElement(Fp, &ff_elem);
  71. BREAK_ON_EPID_ERROR(sts);
  72. sts = ReadFfElement(Fp, params->f, sizeof(*params->f), ff_elem);
  73. BREAK_ON_EPID_ERROR(sts);
  74. }
  75. tpm_ctx->epid2_params = epid2_params;
  76. tpm_ctx->key_handle = 0;
  77. tpm_ctx->hash_alg = kInvalidHashAlg;
  78. rc = TSS_Create(&tpm_ctx->tss);
  79. if (rc != TPM_RC_SUCCESS) {
  80. sts = kEpidErr;
  81. break;
  82. }
  83. *ctx = tpm_ctx;
  84. *rnd_func = tpm2_rnd_func;
  85. *rnd_param = *ctx;
  86. *f = params->f;
  87. sts = kEpidNoErr;
  88. } while (0);
  89. DeleteFfElement(&ff_elem);
  90. if (kEpidNoErr != sts) {
  91. Tpm2DeleteContext(&tpm_ctx);
  92. *ctx = NULL;
  93. }
  94. return sts;
  95. }
  96. void Tpm2DeleteContext(Tpm2Ctx** ctx) {
  97. is_context_already_created = false;
  98. if (ctx && *ctx) {
  99. Tpm2FlushKey(*ctx);
  100. TSS_Delete((*ctx)->tss);
  101. SAFE_FREE(*ctx);
  102. }
  103. }
  104. EpidStatus Tpm2SetHashAlg(Tpm2Ctx* ctx, HashAlg hash_alg) {
  105. if (!ctx) return kEpidBadArgErr;
  106. if (kSha256 != hash_alg && kSha384 != hash_alg && kSha512 != hash_alg &&
  107. kSha512_256 != hash_alg)
  108. return kEpidHashAlgorithmNotSupported;
  109. // can not change hash alg of existing TPM2 key object
  110. if (ctx->key_handle) return kEpidOutOfSequenceError;
  111. ctx->hash_alg = hash_alg;
  112. return kEpidNoErr;
  113. }
  114. void Tpm2ResetContext(Tpm2Ctx** ctx) {
  115. if (ctx && *ctx) {
  116. Tpm2FlushKey(*ctx);
  117. }
  118. }
  119. void Tpm2FlushKey(Tpm2Ctx* ctx) {
  120. if (ctx->key_handle) {
  121. TPM_RC rc;
  122. FlushContext_In in;
  123. in.flushHandle = ctx->key_handle;
  124. rc = TSS_Execute(ctx->tss, NULL, (COMMAND_PARAMETERS*)&in, NULL,
  125. TPM_CC_FlushContext, TPM_RH_NULL, NULL, 0);
  126. if (rc != TPM_RC_SUCCESS) {
  127. print_tpm2_response_code("TPM2_FlushContext", rc);
  128. }
  129. ctx->key_handle = 0;
  130. }
  131. }