privateexp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /// Member private exponentiation implementation
  17. /*! \file */
  18. #include "epid/member/src/privateexp.h"
  19. #include "epid/common/math/ecgroup.h"
  20. #include "epid/common/src/epid2params.h"
  21. #include "epid/common/src/hashsize.h"
  22. #include "epid/common/src/memory.h"
  23. #include "epid/common/types.h"
  24. #include "epid/member/src/context.h"
  25. #include "epid/member/tpm2/commit.h"
  26. #include "epid/member/tpm2/sign.h"
  27. /// Handle Intel(R) EPID Error with Break
  28. #define BREAK_ON_EPID_ERROR(ret) \
  29. if (kEpidNoErr != (ret)) { \
  30. break; \
  31. }
  32. EpidStatus EpidPrivateExp(MemberCtx* ctx, EcPoint const* a, EcPoint* r) {
  33. EpidStatus sts = kEpidErr;
  34. BigNumStr tmp_ff_str = {0};
  35. uint16_t counter = 0;
  36. EcPoint* k_pt = NULL;
  37. EcPoint* l_pt = NULL;
  38. EcPoint* e_pt = NULL;
  39. EcPoint* t1 = NULL;
  40. EcPoint* h = NULL;
  41. FfElement* k = NULL;
  42. FfElement* s = NULL;
  43. size_t digest_len = 0;
  44. uint8_t* digest = NULL;
  45. if (!ctx || !ctx->epid2_params || !a || !r) {
  46. return kEpidBadArgErr;
  47. }
  48. digest_len = EpidGetHashSize(ctx->hash_alg);
  49. digest = SAFE_ALLOC(digest_len);
  50. if (!digest) {
  51. return kEpidMemAllocErr;
  52. }
  53. memset(digest, 0, digest_len);
  54. digest[digest_len - 1] = 1;
  55. do {
  56. FiniteField* Fp = ctx->epid2_params->Fp;
  57. EcGroup* G1 = ctx->epid2_params->G1;
  58. if (!ctx->is_provisioned && !ctx->is_initially_provisioned) {
  59. sts = EpidMemberInitialProvision(ctx);
  60. BREAK_ON_EPID_ERROR(sts);
  61. }
  62. // (K_PT, L_PT, E_PT, counter) = TPM2_Commit(P1=B')
  63. sts = NewEcPoint(G1, &k_pt);
  64. BREAK_ON_EPID_ERROR(sts);
  65. sts = NewEcPoint(G1, &l_pt);
  66. BREAK_ON_EPID_ERROR(sts);
  67. sts = NewEcPoint(G1, &e_pt);
  68. BREAK_ON_EPID_ERROR(sts);
  69. sts = NewEcPoint(G1, &t1);
  70. BREAK_ON_EPID_ERROR(sts);
  71. sts = NewEcPoint(G1, &h);
  72. BREAK_ON_EPID_ERROR(sts);
  73. sts =
  74. Tpm2Commit(ctx->tpm2_ctx, a, NULL, 0, NULL, k_pt, l_pt, e_pt, &counter);
  75. BREAK_ON_EPID_ERROR(sts);
  76. // (k, s) = TPM2_Sign(c=1, counter)
  77. sts = NewFfElement(Fp, &k);
  78. BREAK_ON_EPID_ERROR(sts);
  79. sts = NewFfElement(Fp, &s);
  80. BREAK_ON_EPID_ERROR(sts);
  81. sts = Tpm2Sign(ctx->tpm2_ctx, digest, digest_len, counter, k, s);
  82. BREAK_ON_EPID_ERROR(sts);
  83. // k = Fq.inv(k)
  84. sts = FfInv(Fp, k, k);
  85. BREAK_ON_EPID_ERROR(sts);
  86. // t1 = G1.sscmExp(B', s)
  87. sts = WriteFfElement(Fp, s, &tmp_ff_str, sizeof(tmp_ff_str));
  88. BREAK_ON_EPID_ERROR(sts);
  89. sts = EcSscmExp(G1, a, &tmp_ff_str, t1);
  90. BREAK_ON_EPID_ERROR(sts);
  91. // E_PT = G1.inv(E_PT)
  92. sts = EcInverse(G1, e_pt, e_pt);
  93. BREAK_ON_EPID_ERROR(sts);
  94. // h = G1.mul(t1, E_PT)
  95. sts = EcMul(G1, t1, e_pt, h);
  96. BREAK_ON_EPID_ERROR(sts);
  97. // h = G1.sscmExp(h, k)
  98. sts = WriteFfElement(Fp, k, &tmp_ff_str, sizeof(tmp_ff_str));
  99. BREAK_ON_EPID_ERROR(sts);
  100. sts = EcSscmExp(G1, h, &tmp_ff_str, r);
  101. BREAK_ON_EPID_ERROR(sts);
  102. } while (0);
  103. if (sts != kEpidNoErr) {
  104. (void)Tpm2ReleaseCounter(ctx->tpm2_ctx, counter);
  105. }
  106. DeleteFfElement(&s);
  107. DeleteFfElement(&k);
  108. DeleteEcPoint(&e_pt);
  109. DeleteEcPoint(&l_pt);
  110. DeleteEcPoint(&k_pt);
  111. DeleteEcPoint(&t1);
  112. DeleteEcPoint(&h);
  113. EpidZeroMemory(&tmp_ff_str, sizeof(tmp_ff_str));
  114. SAFE_FREE(digest);
  115. return sts;
  116. }