validatekey.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /// Non-sensitive member context implementation
  17. /*! \file */
  18. #include "epid/member/src/validatekey.h"
  19. #include <stddef.h>
  20. #include "epid/common/math/ecgroup.h"
  21. #include "epid/common/math/finitefield.h"
  22. #include "epid/common/math/pairing.h"
  23. #include "epid/common/src/epid2params.h"
  24. #include "epid/common/src/memory.h"
  25. #include "epid/common/types.h" // MemberPrecomp
  26. #include "epid/member/src/context.h"
  27. #include "epid/member/src/privateexp.h"
  28. /// Handle Intel(R) EPID Error with Break
  29. #define BREAK_ON_EPID_ERROR(ret) \
  30. if (kEpidNoErr != (ret)) { \
  31. break; \
  32. }
  33. bool EpidMemberIsKeyValid(MemberCtx* ctx, G1ElemStr const* A_str,
  34. FpElemStr const* x_str, G1ElemStr const* h1_str,
  35. G2ElemStr const* w_str) {
  36. bool key_is_valid = false;
  37. EcPoint* t1 = NULL;
  38. EcPoint* t2 = NULL;
  39. FfElement* t3 = NULL;
  40. FfElement* t4 = NULL;
  41. EcPoint* A = NULL;
  42. EcPoint* h1 = NULL;
  43. EcPoint* w = NULL;
  44. if (!ctx || !A_str || !x_str || !h1_str || !w_str || !ctx->epid2_params) {
  45. return false;
  46. }
  47. do {
  48. EpidStatus sts = kEpidErr;
  49. EcGroup* G1 = ctx->epid2_params->G1;
  50. EcGroup* G2 = ctx->epid2_params->G2;
  51. FiniteField* GT = ctx->epid2_params->GT;
  52. EcPoint* g1 = ctx->epid2_params->g1;
  53. EcPoint* g2 = ctx->epid2_params->g2;
  54. PairingState* ps_ctx = ctx->epid2_params->pairing_state;
  55. if (!ctx->is_provisioned && !ctx->is_initially_provisioned) {
  56. sts = EpidMemberInitialProvision(ctx);
  57. BREAK_ON_EPID_ERROR(sts);
  58. }
  59. // 2. The member computes t1 = G2.sscmExp(g2, x).
  60. sts = NewEcPoint(G2, &t1);
  61. BREAK_ON_EPID_ERROR(sts);
  62. sts = EcSscmExp(G2, g2, (BigNumStr const*)x_str, t1);
  63. BREAK_ON_EPID_ERROR(sts);
  64. // 3. The member computes t1 = G2.mul(t1, w).
  65. sts = NewEcPoint(G2, &w);
  66. BREAK_ON_EPID_ERROR(sts);
  67. sts = ReadEcPoint(G2, w_str, sizeof(*w_str), w);
  68. BREAK_ON_EPID_ERROR(sts);
  69. sts = EcMul(G2, t1, w, t1);
  70. BREAK_ON_EPID_ERROR(sts);
  71. // 4. The member computes t3 = pairing(A, t1).
  72. sts = NewFfElement(GT, &t3);
  73. BREAK_ON_EPID_ERROR(sts);
  74. sts = NewEcPoint(G1, &A);
  75. BREAK_ON_EPID_ERROR(sts);
  76. sts = ReadEcPoint(G1, A_str, sizeof(*A_str), A);
  77. BREAK_ON_EPID_ERROR(sts);
  78. sts = Pairing(ps_ctx, A, t1, t3);
  79. BREAK_ON_EPID_ERROR(sts);
  80. // 5. The member computes t2 = G1.sscmExp(h1, f).
  81. sts = NewEcPoint(G1, &t2);
  82. BREAK_ON_EPID_ERROR(sts);
  83. sts = NewEcPoint(G1, &h1);
  84. BREAK_ON_EPID_ERROR(sts);
  85. sts = ReadEcPoint(G1, h1_str, sizeof(*h1_str), h1);
  86. BREAK_ON_EPID_ERROR(sts);
  87. sts = EpidPrivateExp(ctx, h1, t2);
  88. BREAK_ON_EPID_ERROR(sts);
  89. // 6. The member computes t2 = G1.mul(t2, g1).
  90. sts = EcMul(G1, t2, g1, t2);
  91. BREAK_ON_EPID_ERROR(sts);
  92. // Step 7. The member computes t4 = pairing(t2, g2).
  93. sts = NewFfElement(GT, &t4);
  94. BREAK_ON_EPID_ERROR(sts);
  95. sts = Pairing(ps_ctx, t2, g2, t4);
  96. BREAK_ON_EPID_ERROR(sts);
  97. // 8. If GT.isEqual(t3, t4) = false, reports bad private key.
  98. sts = FfIsEqual(GT, t3, t4, &key_is_valid);
  99. if (kEpidNoErr != sts) {
  100. key_is_valid = false;
  101. BREAK_ON_EPID_ERROR(sts);
  102. }
  103. } while (0);
  104. DeleteEcPoint(&t1);
  105. DeleteEcPoint(&t2);
  106. DeleteFfElement(&t3);
  107. DeleteFfElement(&t4);
  108. DeleteEcPoint(&A);
  109. DeleteEcPoint(&h1);
  110. DeleteEcPoint(&w);
  111. return key_is_valid;
  112. }