commit.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /// Tpm2Commit implementation.
  17. /*! \file */
  18. #include "epid/member/tpm2/commit.h"
  19. #include <tss2/TPM_Types.h>
  20. #include <tss2/tss.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. /// Handle Intel(R) EPID Error with Break
  28. #define BREAK_ON_EPID_ERROR(ret) \
  29. if (kEpidNoErr != (ret)) { \
  30. break; \
  31. }
  32. /// Bit 7 binary mask
  33. #define BIT7 0x080
  34. /// Binary 00011111
  35. #define BITS0500 0x3f
  36. EpidStatus Tpm2Commit(Tpm2Ctx* ctx, EcPoint const* p1, void const* s2,
  37. size_t s2_len, FfElement const* y2, EcPoint* k,
  38. EcPoint* l, EcPoint* e, uint16_t* counter) {
  39. EpidStatus sts = kEpidErr;
  40. TPM_RC rc = TPM_RC_SUCCESS;
  41. if (!ctx || !ctx->epid2_params || !ctx->key_handle) {
  42. return kEpidBadArgErr;
  43. }
  44. if (s2 && s2_len <= 0) {
  45. return kEpidBadArgErr;
  46. }
  47. if ((!s2 && y2) || (s2 && !y2)) {
  48. return kEpidBadArgErr;
  49. }
  50. if (s2 && (!k || !l)) {
  51. return kEpidBadArgErr;
  52. }
  53. if (!e || !counter) {
  54. return kEpidBadArgErr;
  55. }
  56. if (s2_len > UINT16_MAX) {
  57. return kEpidBadArgErr;
  58. }
  59. do {
  60. FiniteField* Fq = ctx->epid2_params->Fq;
  61. EcGroup* G1 = ctx->epid2_params->G1;
  62. Commit_In in = {0};
  63. Commit_Out out;
  64. TPMI_SH_AUTH_SESSION sessionHandle0 = TPM_RS_PW;
  65. unsigned int sessionAttributes0 = 0;
  66. in.signHandle = ctx->key_handle;
  67. if (p1) {
  68. G1ElemStr p1_str = {0};
  69. sts = WriteEcPoint(G1, p1, &p1_str, sizeof(p1_str));
  70. BREAK_ON_EPID_ERROR(sts);
  71. sts = ReadTpm2EcPoint(&p1_str, &in.P1);
  72. BREAK_ON_EPID_ERROR(sts);
  73. }
  74. if (s2) {
  75. FqElemStr y2_str = {0};
  76. sts = WriteFfElement(Fq, y2, &y2_str, sizeof(y2_str));
  77. BREAK_ON_EPID_ERROR(sts);
  78. sts = ReadTpm2FfElement(&y2_str.data, &in.y2);
  79. BREAK_ON_EPID_ERROR(sts);
  80. in.s2.t.size = (UINT16)s2_len;
  81. if (0 != memcpy_S(&in.s2.t.buffer, sizeof(in.s2.t.buffer), s2, s2_len)) {
  82. sts = kEpidBadArgErr;
  83. break;
  84. }
  85. }
  86. rc = TSS_Execute(ctx->tss, (RESPONSE_PARAMETERS*)&out,
  87. (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_Commit,
  88. sessionHandle0, NULL, sessionAttributes0, TPM_RH_NULL,
  89. NULL, 0);
  90. if (rc != TPM_RC_SUCCESS) {
  91. print_tpm2_response_code("TPM2_Commit", rc);
  92. // workaround based on Table 2:15 to filter response code format defining
  93. // handle, session, or parameter number modifier if bit 7 is 1 error is
  94. // RC_FMT1
  95. if ((rc & BIT7) != 0) {
  96. rc = rc & (BITS0500 | RC_FMT1);
  97. if (TPM_RC_ATTRIBUTES == rc || TPM_RC_ECC_POINT == rc ||
  98. TPM_RC_HASH == rc || TPM_RC_KEY == rc || TPM_RC_SCHEME == rc ||
  99. TPM_RC_SIZE == rc)
  100. sts = kEpidBadArgErr;
  101. else
  102. sts = kEpidErr;
  103. } else {
  104. if (TPM_RC_NO_RESULT == rc)
  105. sts = kEpidBadArgErr;
  106. else
  107. sts = kEpidErr;
  108. }
  109. break;
  110. }
  111. if (out.E.size > 0) {
  112. G1ElemStr e_str = {0};
  113. sts = WriteTpm2EcPoint(&out.E, &e_str);
  114. BREAK_ON_EPID_ERROR(sts);
  115. sts = ReadEcPoint(G1, &e_str, sizeof(e_str), e);
  116. BREAK_ON_EPID_ERROR(sts);
  117. }
  118. if (out.K.size > 0 && k) {
  119. G1ElemStr k_str = {0};
  120. sts = WriteTpm2EcPoint(&out.K, &k_str);
  121. BREAK_ON_EPID_ERROR(sts);
  122. sts = ReadEcPoint(G1, &k_str, sizeof(k_str), k);
  123. BREAK_ON_EPID_ERROR(sts);
  124. }
  125. if (out.L.size > 0 && l) {
  126. G1ElemStr l_str = {0};
  127. sts = WriteTpm2EcPoint(&out.L, &l_str);
  128. BREAK_ON_EPID_ERROR(sts);
  129. sts = ReadEcPoint(G1, &l_str, sizeof(l_str), l);
  130. BREAK_ON_EPID_ERROR(sts);
  131. }
  132. *counter = out.counter;
  133. } while (0);
  134. return sts;
  135. }