startup.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /// EpidMemberStartup implementation.
  17. /*!
  18. * \file
  19. */
  20. #include <epid/member/api.h>
  21. #include <string.h>
  22. #include "epid/common/math/ecgroup.h"
  23. #include "epid/common/math/finitefield.h"
  24. #include "epid/common/src/epid2params.h"
  25. #include "epid/common/types.h" // MemberPrecomp
  26. #include "epid/member/src/context.h"
  27. #include "epid/member/src/precomp.h"
  28. #include "epid/member/src/storage.h"
  29. /// Handle SDK Error with Break
  30. #define BREAK_ON_EPID_ERROR(ret) \
  31. if (kEpidNoErr != (ret)) { \
  32. break; \
  33. }
  34. static EpidStatus MemberReadPrecomputation(MemberCtx* ctx,
  35. MemberPrecomp const* precomp);
  36. EpidStatus EpidMemberStartup(MemberCtx* ctx) {
  37. EpidStatus sts = kEpidErr;
  38. uint32_t const nv_index = 0x01c10100;
  39. if (!ctx) {
  40. return kEpidBadArgErr;
  41. }
  42. do {
  43. EcGroup* G1 = ctx->epid2_params->G1;
  44. EcGroup* G2 = ctx->epid2_params->G2;
  45. FiniteField* Fp = ctx->epid2_params->Fp;
  46. EcPoint* A = (EcPoint*)ctx->A;
  47. FfElement* x = (FfElement*)ctx->x;
  48. EcPoint* h1 = (EcPoint*)ctx->h1;
  49. EcPoint* h2 = (EcPoint*)ctx->h2;
  50. EcPoint* w = (EcPoint*)ctx->w;
  51. sts = EpidNvReadMembershipCredential(ctx->tpm2_ctx, nv_index, &ctx->pub_key,
  52. &ctx->credential);
  53. BREAK_ON_EPID_ERROR(sts);
  54. if (!ctx->precomp_ready) {
  55. sts = PrecomputeMemberPairing(ctx->epid2_params, &ctx->pub_key,
  56. &ctx->credential.A, &ctx->precomp);
  57. BREAK_ON_EPID_ERROR(sts);
  58. ctx->precomp_ready = true;
  59. }
  60. if (!ctx->is_provisioned && !ctx->is_initially_provisioned) {
  61. sts = EpidMemberInitialProvision(ctx);
  62. BREAK_ON_EPID_ERROR(sts);
  63. }
  64. sts = ReadEcPoint(G1, &ctx->credential.A, sizeof(ctx->credential.A), A);
  65. BREAK_ON_EPID_ERROR(sts);
  66. sts = ReadFfElement(Fp, &ctx->credential.x, sizeof(ctx->credential.x), x);
  67. BREAK_ON_EPID_ERROR(sts);
  68. sts = ReadEcPoint(G1, &ctx->pub_key.h1, sizeof(ctx->pub_key.h1), h1);
  69. BREAK_ON_EPID_ERROR(sts);
  70. sts = ReadEcPoint(G1, &ctx->pub_key.h2, sizeof(ctx->pub_key.h2), h2);
  71. BREAK_ON_EPID_ERROR(sts);
  72. sts = ReadEcPoint(G2, &ctx->pub_key.w, sizeof(ctx->pub_key.w), w);
  73. BREAK_ON_EPID_ERROR(sts);
  74. sts = MemberReadPrecomputation(ctx, &ctx->precomp);
  75. BREAK_ON_EPID_ERROR(sts);
  76. sts = kEpidNoErr;
  77. } while (0);
  78. return sts;
  79. }
  80. static EpidStatus MemberReadPrecomputation(MemberCtx* ctx,
  81. MemberPrecomp const* precomp) {
  82. EpidStatus sts = kEpidErr;
  83. if (!ctx || !precomp || !ctx->epid2_params || !ctx) {
  84. return kEpidBadArgErr;
  85. }
  86. do {
  87. FiniteField* GT = ctx->epid2_params->GT;
  88. FfElement* e12 = (FfElement*)ctx->e12;
  89. FfElement* e22 = (FfElement*)ctx->e22;
  90. FfElement* e2w = (FfElement*)ctx->e2w;
  91. FfElement* ea2 = (FfElement*)ctx->ea2;
  92. sts = ReadFfElement(GT, &precomp->e12, sizeof(precomp->e12), e12);
  93. BREAK_ON_EPID_ERROR(sts);
  94. sts = ReadFfElement(GT, &precomp->e22, sizeof(precomp->e22), e22);
  95. BREAK_ON_EPID_ERROR(sts);
  96. sts = ReadFfElement(GT, &precomp->e2w, sizeof(precomp->e2w), e2w);
  97. BREAK_ON_EPID_ERROR(sts);
  98. sts = ReadFfElement(GT, &precomp->ea2, sizeof(precomp->ea2), ea2);
  99. BREAK_ON_EPID_ERROR(sts);
  100. sts = kEpidNoErr;
  101. } while (0);
  102. return sts;
  103. }