join.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /// Join Request related implementation.
  17. /*! \file */
  18. #include <epid/member/api.h>
  19. #include "epid/common/src/epid2params.h"
  20. #include "epid/common/src/grouppubkey.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/src/join_commitment.h"
  26. #include "epid/member/src/privateexp.h"
  27. #include "epid/member/src/resize.h"
  28. #include "epid/member/tpm2/commit.h"
  29. #include "epid/member/tpm2/sign.h"
  30. /// Handle SDK Error with Break
  31. #define BREAK_ON_EPID_ERROR(ret) \
  32. if (kEpidNoErr != (ret)) { \
  33. break; \
  34. }
  35. EpidStatus EpidCreateJoinRequest(MemberCtx* ctx, GroupPubKey const* pub_key,
  36. IssuerNonce const* ni,
  37. JoinRequest* join_request) {
  38. EpidStatus sts = kEpidErr;
  39. GroupPubKey_* pub_key_ = NULL;
  40. EcPoint* t = NULL; // temporary used for F and R
  41. EcPoint* h1 = NULL;
  42. EcPoint* K = NULL;
  43. EcPoint* l = NULL;
  44. EcPoint* e = NULL;
  45. FfElement* k = NULL;
  46. FfElement* s = NULL;
  47. uint8_t* digest = NULL;
  48. if (!ctx || !pub_key || !ni || !join_request || !ctx->epid2_params) {
  49. return kEpidBadArgErr;
  50. }
  51. if (kSha256 != ctx->hash_alg && kSha384 != ctx->hash_alg &&
  52. kSha512 != ctx->hash_alg && kSha512_256 != ctx->hash_alg) {
  53. return kEpidBadArgErr;
  54. }
  55. do {
  56. JoinRequest request = {0};
  57. G1ElemStr R = {0};
  58. EcGroup* G1 = ctx->epid2_params->G1;
  59. FiniteField* Fp = ctx->epid2_params->Fp;
  60. size_t digest_size = 0;
  61. if (!ctx->is_provisioned && !ctx->is_initially_provisioned) {
  62. sts = EpidMemberInitialProvision(ctx);
  63. BREAK_ON_EPID_ERROR(sts);
  64. }
  65. // validate public key by creating
  66. sts = CreateGroupPubKey(pub_key, ctx->epid2_params->G1,
  67. ctx->epid2_params->G2, &pub_key_);
  68. BREAK_ON_EPID_ERROR(sts);
  69. sts = NewEcPoint(G1, &t);
  70. BREAK_ON_EPID_ERROR(sts);
  71. sts = NewEcPoint(G1, &h1);
  72. BREAK_ON_EPID_ERROR(sts);
  73. sts = ReadEcPoint(G1, &pub_key->h1, sizeof(pub_key->h1), h1);
  74. BREAK_ON_EPID_ERROR(sts);
  75. // 2. The member computes F = G1.sscmExp(h1, f).
  76. sts = EpidPrivateExp(ctx, h1, t);
  77. BREAK_ON_EPID_ERROR(sts);
  78. sts = WriteEcPoint(G1, t, &request.F, sizeof(request.F));
  79. BREAK_ON_EPID_ERROR(sts);
  80. // 1. The member chooses a random integer r from [1, p-1].
  81. // 3. The member computes R = G1.sscmExp(h1, r).
  82. sts = NewEcPoint(G1, &K);
  83. BREAK_ON_EPID_ERROR(sts);
  84. sts = NewEcPoint(G1, &l);
  85. BREAK_ON_EPID_ERROR(sts);
  86. sts = NewEcPoint(G1, &e);
  87. BREAK_ON_EPID_ERROR(sts);
  88. sts =
  89. Tpm2Commit(ctx->tpm2_ctx, h1, NULL, 0, NULL, K, l, e, &(ctx->join_ctr));
  90. BREAK_ON_EPID_ERROR(sts);
  91. sts = WriteEcPoint(G1, e, &R, sizeof(R));
  92. BREAK_ON_EPID_ERROR(sts);
  93. sts = HashJoinCommitment(ctx->epid2_params->Fp, ctx->hash_alg, pub_key,
  94. &request.F, &R, ni, &request.c);
  95. BREAK_ON_EPID_ERROR(sts);
  96. // Extend value c to be of a digest size.
  97. digest_size = EpidGetHashSize(ctx->hash_alg);
  98. digest = (uint8_t*)SAFE_ALLOC(digest_size);
  99. if (!digest) {
  100. sts = kEpidMemAllocErr;
  101. break;
  102. }
  103. sts = ResizeOctStr(&request.c, sizeof(request.c), digest, digest_size);
  104. BREAK_ON_EPID_ERROR(sts);
  105. // Step 5. The member computes s = (r + c * f) mod p.
  106. sts = NewFfElement(Fp, &k);
  107. BREAK_ON_EPID_ERROR(sts);
  108. sts = NewFfElement(Fp, &s);
  109. BREAK_ON_EPID_ERROR(sts);
  110. sts = Tpm2Sign(ctx->tpm2_ctx, digest, digest_size, ctx->join_ctr, k, s);
  111. BREAK_ON_EPID_ERROR(sts);
  112. sts = WriteFfElement(Fp, s, &request.s, sizeof(request.s));
  113. BREAK_ON_EPID_ERROR(sts);
  114. // Step 6. The output join request is (F, c, s).
  115. *join_request = request;
  116. sts = kEpidNoErr;
  117. } while (0);
  118. if (sts != kEpidNoErr) {
  119. (void)Tpm2ReleaseCounter(ctx->tpm2_ctx, ctx->join_ctr);
  120. }
  121. DeleteEcPoint(&t);
  122. DeleteEcPoint(&h1);
  123. DeleteEcPoint(&K);
  124. DeleteEcPoint(&l);
  125. DeleteEcPoint(&e);
  126. DeleteFfElement(&k);
  127. DeleteFfElement(&s);
  128. SAFE_FREE(digest);
  129. DeleteGroupPubKey(&pub_key_);
  130. return sts;
  131. }