sign.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /// Tpm2Sign implementation.
  17. /*! \file */
  18. #include "epid/member/tpm2/sign.h"
  19. #include <stddef.h>
  20. #include <string.h>
  21. #include <tss2/tss.h>
  22. #include "epid/common/math/finitefield.h"
  23. #include "epid/common/src/epid2params.h"
  24. #include "epid/common/src/hashsize.h"
  25. #include "epid/common/src/memory.h"
  26. #include "epid/common/types.h"
  27. #include "epid/member/tpm2/getrandom.h"
  28. #include "epid/member/tpm2/ibm_tss/conversion.h"
  29. #include "epid/member/tpm2/ibm_tss/printtss.h"
  30. #include "epid/member/tpm2/ibm_tss/state.h"
  31. /// Handle Intel(R) EPID Error with Break
  32. #define BREAK_ON_EPID_ERROR(ret) \
  33. if (kEpidNoErr != (ret)) { \
  34. break; \
  35. }
  36. /// Bit 7 binary mask
  37. #define BIT7 0x080
  38. /// Binary 00011111
  39. #define BITS0500 0x3f
  40. EpidStatus Tpm2Sign(Tpm2Ctx* ctx, void const* digest, size_t digest_len,
  41. uint16_t counter, FfElement* k, FfElement* s) {
  42. EpidStatus sts = kEpidErr;
  43. TPM_RC rc = TPM_RC_SUCCESS;
  44. if (!ctx || !ctx->epid2_params || !digest || !s) {
  45. return kEpidBadArgErr;
  46. }
  47. if (0 == digest_len || EpidGetHashSize(ctx->hash_alg) != digest_len) {
  48. return kEpidBadArgErr;
  49. }
  50. if (ctx->key_handle == 0) {
  51. return kEpidBadArgErr;
  52. }
  53. do {
  54. TPMI_SH_AUTH_SESSION sessionHandle0 = TPM_RS_PW;
  55. unsigned int sessionAttributes0 = 0;
  56. Sign_In in = {0};
  57. Sign_Out out;
  58. FiniteField* Fp = ctx->epid2_params->Fp;
  59. FpElemStr k_str;
  60. FpElemStr s_str;
  61. in.keyHandle = ctx->key_handle;
  62. if (0 != memcpy_S(in.digest.t.buffer, sizeof(in.digest.t.buffer), digest,
  63. digest_len)) {
  64. sts = kEpidErr;
  65. break;
  66. }
  67. in.digest.t.size = (uint16_t)digest_len;
  68. in.inScheme.scheme = TPM_ALG_ECDAA;
  69. in.inScheme.details.ecdaa.hashAlg = EpidtoTpm2HashAlg(ctx->hash_alg);
  70. in.inScheme.details.ecdaa.count = counter;
  71. /* proof that digest was created by the TPM (NULL ticket) */
  72. /* Table 91 - Definition of TPMT_TK_HASHCHECK Structure */
  73. in.validation.tag = TPM_ST_HASHCHECK;
  74. in.validation.hierarchy = TPM_RH_NULL;
  75. in.validation.digest.t.size = 0;
  76. rc =
  77. TSS_Execute(ctx->tss, (RESPONSE_PARAMETERS*)&out,
  78. (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_Sign, sessionHandle0,
  79. NULL, sessionAttributes0, TPM_RH_NULL, NULL, 0);
  80. if (rc != TPM_RC_SUCCESS) {
  81. print_tpm2_response_code("TPM2_Sign", rc);
  82. // workaround based on Table 2:15 to filter response code format defining
  83. // handle, session, or parameter number modifier if bit 7 is 1 error is
  84. // RC_FMT1
  85. if ((rc & BIT7) != 0) {
  86. rc = rc & (BITS0500 | RC_FMT1);
  87. if (TPM_RC_VALUE == rc) {
  88. sts = kEpidBadArgErr;
  89. }
  90. } else {
  91. sts = kEpidErr;
  92. }
  93. break;
  94. }
  95. if (k) {
  96. sts = WriteTpm2FfElement(&out.signature.signature.ecdaa.signatureR,
  97. (OctStr256*)&k_str);
  98. BREAK_ON_EPID_ERROR(sts);
  99. sts = ReadFfElement(Fp, &k_str, sizeof(k_str), k);
  100. BREAK_ON_EPID_ERROR(sts);
  101. }
  102. sts = WriteTpm2FfElement(&out.signature.signature.ecdaa.signatureS,
  103. (OctStr256*)&s_str);
  104. BREAK_ON_EPID_ERROR(sts);
  105. sts = ReadFfElement(Fp, &s_str, sizeof(s_str), s);
  106. BREAK_ON_EPID_ERROR(sts);
  107. } while (0);
  108. return sts;
  109. }
  110. EpidStatus Tpm2ReleaseCounter(Tpm2Ctx* ctx, uint16_t counter) {
  111. EpidStatus sts = kEpidErr;
  112. if (!ctx) {
  113. return kEpidBadArgErr;
  114. }
  115. do {
  116. TPM_RC rc = TPM_RC_SUCCESS;
  117. TPMI_SH_AUTH_SESSION sessionHandle0 = TPM_RS_PW;
  118. unsigned int sessionAttributes0 = 0;
  119. Sign_In in = {0};
  120. Sign_Out out;
  121. in.keyHandle = ctx->key_handle;
  122. in.digest.t.size = (uint16_t)EpidGetHashSize(ctx->hash_alg);
  123. memset(in.digest.t.buffer, 0x1, (size_t)in.digest.t.size);
  124. in.inScheme.scheme = TPM_ALG_ECDAA;
  125. in.inScheme.details.ecdaa.hashAlg = EpidtoTpm2HashAlg(ctx->hash_alg);
  126. in.inScheme.details.ecdaa.count = counter;
  127. in.validation.tag = TPM_ST_HASHCHECK;
  128. in.validation.hierarchy = TPM_RH_NULL;
  129. in.validation.digest.t.size = 0;
  130. rc =
  131. TSS_Execute(ctx->tss, (RESPONSE_PARAMETERS*)&out,
  132. (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_Sign, sessionHandle0,
  133. NULL, sessionAttributes0, TPM_RH_NULL, NULL, 0);
  134. if (rc != TPM_RC_SUCCESS && (rc & (BITS0500 | RC_FMT1)) != TPM_RC_VALUE) {
  135. print_tpm2_response_code("Tpm2ReleaseCounter: TPM2_Sign", rc);
  136. sts = kEpidErr;
  137. break;
  138. }
  139. sts = kEpidNoErr;
  140. } while (0);
  141. return sts;
  142. }