verify.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*############################################################################
  2. # Copyright 2016-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. /*!
  17. * \file
  18. * \brief Verify implementation.
  19. */
  20. #include <string.h>
  21. #include "epid/common/src/endian_convert.h"
  22. #include "epid/verifier/api.h"
  23. #include "epid/verifier/src/context.h"
  24. /// Handle SDK Error with Break
  25. #define BREAK_ON_EPID_ERROR(ret) \
  26. if (kEpidNoErr != (ret)) { \
  27. break; \
  28. }
  29. static size_t EpidGetSignatureRlCount(EpidSignature const* sig) {
  30. if (!sig)
  31. return 0;
  32. else
  33. return ntohl(sig->n2);
  34. }
  35. static size_t EpidGetGroupRlCount(GroupRl const* rl) {
  36. if (!rl)
  37. return 0;
  38. else
  39. return ntohl(rl->n3);
  40. }
  41. static size_t EpidGetPrivRlCount(PrivRl const* rl) {
  42. if (!rl)
  43. return 0;
  44. else
  45. return ntohl(rl->n1);
  46. }
  47. static size_t EpidGetSigRlCount(SigRl const* rl) {
  48. if (!rl)
  49. return 0;
  50. else
  51. return ntohl(rl->n2);
  52. }
  53. static size_t EpidGetVerifierRlCount(VerifierRl const* rl) {
  54. if (!rl)
  55. return 0;
  56. else
  57. return ntohl(rl->n4);
  58. }
  59. // implements section 4.1.2 "Verify algorithm" from Intel(R) EPID 2.0 Spec
  60. EpidStatus EpidVerify(VerifierCtx const* ctx, EpidSignature const* sig,
  61. size_t sig_len, void const* msg, size_t msg_len) {
  62. // Step 1. Setup
  63. size_t const sig_header_len = (sizeof(EpidSignature) - sizeof(NrProof));
  64. EpidStatus sts = kEpidErr;
  65. size_t rl_count = 0;
  66. size_t i;
  67. if (!ctx || !sig) {
  68. return kEpidBadArgErr;
  69. }
  70. if (!msg && (0 != msg_len)) {
  71. // if message is non-empty it must have both length and content
  72. return kEpidBadArgErr;
  73. }
  74. if (!ctx->epid2_params || !ctx->pub_key) {
  75. return kEpidBadArgErr;
  76. }
  77. if (sig_len < sig_header_len) {
  78. return kEpidBadArgErr;
  79. }
  80. rl_count = EpidGetSignatureRlCount(sig);
  81. if (rl_count > (SIZE_MAX - sig_header_len) / sizeof(sig->sigma[0]) ||
  82. (rl_count * sizeof(sig->sigma[0])) + sig_header_len != sig_len) {
  83. return kEpidBadArgErr;
  84. }
  85. // Step 2. The verifier verifies the basic signature Sigma0 as follows:
  86. sts = EpidVerifyBasicSig(ctx, &sig->sigma0, msg, msg_len);
  87. if (sts != kEpidNoErr) {
  88. // p. If any of the above verifications fails, the verifier aborts and
  89. // outputs 1
  90. return kEpidSigInvalid;
  91. }
  92. // Step 3. If GroupRL is provided,
  93. if (ctx->group_rl) {
  94. // a. The verifier verifies that gid does not match any entry in GroupRL.
  95. size_t grouprl_count = EpidGetGroupRlCount(ctx->group_rl);
  96. for (i = 0; i < grouprl_count; ++i) {
  97. if (0 == memcmp(&ctx->pub_key->gid, &ctx->group_rl->gid[i],
  98. sizeof(ctx->pub_key->gid))) {
  99. // b. If gid matches an entry in GroupRL, aborts and returns 2.
  100. return kEpidSigRevokedInGroupRl;
  101. }
  102. }
  103. }
  104. // Step 4. If PrivRL is provided,
  105. if (ctx->priv_rl) {
  106. size_t privrl_count = EpidGetPrivRlCount(ctx->priv_rl);
  107. // a. The verifier verifies that gid in the public key and in PrivRL match.
  108. // If mismatch, abort and return "operation failed".
  109. if (0 != memcmp(&ctx->pub_key->gid, &ctx->priv_rl->gid,
  110. sizeof(ctx->pub_key->gid))) {
  111. return kEpidBadArgErr;
  112. }
  113. // b. For i = 0, ..., n1-1, the verifier computes t4 =G1.exp(B, f[i]) and
  114. // verifies that G1.isEqual(t4, K) = false. A faster private-key revocation
  115. // check algorithm is provided in Section 4.5.
  116. for (i = 0; i < privrl_count; ++i) {
  117. sts = EpidCheckPrivRlEntry(ctx, &sig->sigma0, &ctx->priv_rl->f[i]);
  118. if (sts != kEpidNoErr) {
  119. // c. If the above step fails, the verifier aborts and output 3.
  120. return kEpidSigRevokedInPrivRl;
  121. }
  122. }
  123. }
  124. // Step 5. If SigRL is provided,
  125. if (ctx->sig_rl) {
  126. size_t sigrl_count = EpidGetSigRlCount(ctx->sig_rl);
  127. // a. The verifier verifies that gid in the public key and in SigRL match.
  128. // If mismatch, abort and return "operation failed".
  129. if (0 != memcmp(&ctx->pub_key->gid, &ctx->sig_rl->gid,
  130. sizeof(ctx->pub_key->gid))) {
  131. return kEpidBadArgErr;
  132. }
  133. // b. The verifier verifies that RLver in Sigma and in SigRL match. If
  134. // mismatch, abort and output "operation failed".
  135. if (0 != memcmp(&ctx->sig_rl->version, &sig->rl_ver,
  136. sizeof(ctx->sig_rl->version))) {
  137. return kEpidErr;
  138. }
  139. // c. The verifier verifies that n2 in Sigma and in SigRL match. If
  140. // mismatch, abort and output "operation failed".
  141. if (sigrl_count != rl_count) {
  142. return kEpidBadArgErr;
  143. }
  144. // d. For i = 0, ..., n2-1, the verifier verifies nrVerify(B, K, B[i],
  145. // K[i], Sigma[i]) = true. The details of nrVerify() will be given in the
  146. // next subsection.
  147. for (i = 0; i < sigrl_count; ++i) {
  148. sts = EpidNrVerify(ctx, &sig->sigma0, msg, msg_len, &ctx->sig_rl->bk[i],
  149. &sig->sigma[i]);
  150. if (sts != kEpidNoErr) {
  151. // e. If the above step fails, the verifier aborts and output 4.
  152. return kEpidSigRevokedInSigRl;
  153. }
  154. }
  155. }
  156. // Step 6. If VerifierRL is provided,
  157. if (ctx->verifier_rl) {
  158. // a. The verifier verifies that gid in the public key and in VerifierRL
  159. // match. If mismatch, abort and return "operation failed".
  160. if (0 != memcmp(&ctx->pub_key->gid, &ctx->verifier_rl->gid,
  161. sizeof(ctx->pub_key->gid))) {
  162. return kEpidBadArgErr;
  163. }
  164. // b. The verifier verifies that B in the signature and in VerifierRL
  165. // match. If mismatch, go to step 7.
  166. if (0 ==
  167. memcmp(&ctx->verifier_rl->B, &sig->sigma0.B, sizeof(sig->sigma0.B))) {
  168. size_t verifierrl_count = EpidGetVerifierRlCount(ctx->verifier_rl);
  169. // c. For i = 0, ..., n4-1, the verifier verifies that K != K[i].
  170. for (i = 0; i < verifierrl_count; ++i) {
  171. if (0 == memcmp(&ctx->verifier_rl->K[i], &sig->sigma0.K,
  172. sizeof(sig->sigma0.K))) {
  173. // d. If the above step fails, the verifier aborts and output 5.
  174. return kEpidSigRevokedInVerifierRl;
  175. }
  176. }
  177. }
  178. }
  179. // Step 7. If all the above verifications succeed, the verifier outputs 0.
  180. return kEpidSigValid;
  181. }