nrverify-test.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 Epid11NrVerify unit tests.
  19. */
  20. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  21. #include "gtest/gtest.h"
  22. extern "C" {
  23. #include "epid/common/1.1/types.h"
  24. #include "epid/verifier/1.1/api.h"
  25. #include "epid/verifier/1.1/src/context.h"
  26. }
  27. #include "epid/common-testhelper/1.1/verifier_wrapper-testhelper.h"
  28. #include "epid/common-testhelper/errors-testhelper.h"
  29. #include "epid/verifier/1.1/unittests/verifier-testhelper.h"
  30. namespace {
  31. /////////////////////////////////////////////////////////////////////////
  32. // Simple Errors
  33. TEST_F(Epid11VerifierTest, NrVerifyFailsGivenNullParameters) {
  34. Epid11VerifierCtxObj verifier(this->kPubKeyStr);
  35. Epid11Signature const* epid_signature =
  36. reinterpret_cast<Epid11Signature const*>(
  37. this->kSigGrpXMember0Sha256RandbaseMsg0N2One.data());
  38. Epid11SigRl const* sig_rl =
  39. reinterpret_cast<Epid11SigRl const*>(this->kSigRl.data());
  40. EXPECT_EQ(kEpidBadArgErr,
  41. Epid11NrVerify(nullptr, &epid_signature->sigma0, this->kMsg0.data(),
  42. this->kMsg0.size(), &sig_rl->bk[0],
  43. &epid_signature->sigma[0]));
  44. EXPECT_EQ(
  45. kEpidBadArgErr,
  46. Epid11NrVerify(verifier, nullptr, this->kMsg0.data(), this->kMsg0.size(),
  47. &sig_rl->bk[0], &epid_signature->sigma[0]));
  48. EXPECT_EQ(kEpidBadArgErr,
  49. Epid11NrVerify(verifier, &epid_signature->sigma0, nullptr,
  50. this->kMsg0.size(), &sig_rl->bk[0],
  51. &epid_signature->sigma[0]));
  52. EXPECT_EQ(
  53. kEpidBadArgErr,
  54. Epid11NrVerify(verifier, &epid_signature->sigma0, this->kMsg0.data(),
  55. this->kMsg0.size(), nullptr, &epid_signature->sigma[0]));
  56. EXPECT_EQ(
  57. kEpidBadArgErr,
  58. Epid11NrVerify(verifier, &epid_signature->sigma0, this->kMsg0.data(),
  59. this->kMsg0.size(), &sig_rl->bk[0], nullptr));
  60. }
  61. /////////////////////////////////////////////////////////////////////
  62. // Reject
  63. TEST_F(Epid11VerifierTest, NrVerifyRejectsTotalMsgSizeOutOfRangeOfInt) {
  64. Epid11VerifierCtxObj verifier(this->kPubKeyStr);
  65. Epid11Signature const* epid_signature =
  66. reinterpret_cast<Epid11Signature const*>(
  67. this->kSigGrpXMember0Sha256RandbaseMsg0N2One.data());
  68. Epid11SigRl const* sig_rl =
  69. reinterpret_cast<Epid11SigRl const*>(this->kSigRl.data());
  70. // Since before hashing some other data will be concatenated to commit
  71. // message, passing msg with size==UINT_MAX is causes out of range for
  72. // this concatenated msg
  73. Epid11NrProof nr_proof = epid_signature->sigma[0];
  74. EXPECT_EQ(kEpidBadArgErr, Epid11NrVerify(verifier, &epid_signature->sigma0,
  75. this->kMsg0.data(), 0xffffffff,
  76. &sig_rl->bk[0], &nr_proof));
  77. #if (SIZE_MAX >= 0x100000001) // When size_t value allowed to be 0x100000001
  78. EXPECT_EQ(kEpidBadArgErr, Epid11NrVerify(verifier, &epid_signature->sigma0,
  79. this->kMsg0.data(), 0x100000001,
  80. &sig_rl->bk[0], &nr_proof));
  81. #endif
  82. }
  83. TEST_F(Epid11VerifierTest, NrVerifyRejectsSigWithTNotInG3) {
  84. // 4.2.2 step 2 - The verifier verifies that G3.inGroup(T) = true.
  85. Epid11VerifierCtxObj verifier(this->kPubKeyStr);
  86. Epid11Signature const* epid_signature =
  87. reinterpret_cast<Epid11Signature const*>(
  88. this->kSigGrpXMember0Sha256RandbaseMsg0N2One.data());
  89. Epid11SigRl const* sig_rl =
  90. reinterpret_cast<Epid11SigRl const*>(this->kSigRl.data());
  91. Epid11NrProof nr_proof = epid_signature->sigma[0];
  92. nr_proof.T.x.data.data[0]++;
  93. EXPECT_EQ(
  94. kEpidBadArgErr,
  95. Epid11NrVerify(verifier, &epid_signature->sigma0, this->kMsg0.data(),
  96. this->kMsg0.size(), &sig_rl->bk[0], &nr_proof));
  97. }
  98. TEST_F(Epid11VerifierTest, NrVerifyRejectsSigWithTIdentityOfG3) {
  99. // 4.2.2 step 3 - The verifier verifies that G3.isIdentity(T) = false.
  100. Epid11VerifierCtxObj verifier(this->kPubKeyStr);
  101. Epid11Signature const* epid_signature =
  102. reinterpret_cast<Epid11Signature const*>(
  103. this->kSigGrpXMember0Sha256RandbaseMsg0N2One.data());
  104. Epid11SigRl const* sig_rl =
  105. reinterpret_cast<Epid11SigRl const*>(this->kSigRl.data());
  106. Epid11NrProof nr_proof = epid_signature->sigma[0];
  107. nr_proof.T = this->kG3IdentityStr;
  108. EXPECT_EQ(
  109. kEpidBadArgErr,
  110. Epid11NrVerify(verifier, &epid_signature->sigma0, this->kMsg0.data(),
  111. this->kMsg0.size(), &sig_rl->bk[0], &nr_proof));
  112. }
  113. TEST_F(Epid11VerifierTest, NrVerifyRejectsSigWithSmuNotInRange) {
  114. // 4.2.2 step 4 - The verifier verifies that smu, snu in [0, p'-1].
  115. Epid11VerifierCtxObj verifier(this->kPubKeyStr);
  116. Epid11Signature const* epid_signature =
  117. reinterpret_cast<Epid11Signature const*>(
  118. this->kSigGrpXMember0Sha256RandbaseMsg0N2One.data());
  119. Epid11SigRl const* sig_rl =
  120. reinterpret_cast<Epid11SigRl const*>(this->kSigRl.data());
  121. Epid11NrProof nr_proof = epid_signature->sigma[0];
  122. nr_proof.smu.data = this->kParamsStr.p.data;
  123. EXPECT_EQ(
  124. kEpidBadArgErr,
  125. Epid11NrVerify(verifier, &epid_signature->sigma0, this->kMsg0.data(),
  126. this->kMsg0.size(), &sig_rl->bk[0], &nr_proof));
  127. }
  128. TEST_F(Epid11VerifierTest, NrVerifyRejectsSigWithSnuNotInRange) {
  129. // 4.2.2 step 4 - The verifier verifies that smu, snu in [0, p'-1].
  130. Epid11VerifierCtxObj verifier(this->kPubKeyStr);
  131. Epid11Signature const* epid_signature =
  132. reinterpret_cast<Epid11Signature const*>(
  133. this->kSigGrpXMember0Sha256RandbaseMsg0N2One.data());
  134. Epid11SigRl const* sig_rl =
  135. reinterpret_cast<Epid11SigRl const*>(this->kSigRl.data());
  136. Epid11NrProof nr_proof = epid_signature->sigma[0];
  137. nr_proof.snu.data = this->kParamsStr.p.data;
  138. EXPECT_EQ(
  139. kEpidBadArgErr,
  140. Epid11NrVerify(verifier, &epid_signature->sigma0, this->kMsg0.data(),
  141. this->kMsg0.size(), &sig_rl->bk[0], &nr_proof));
  142. }
  143. // 4.2.2 step 5 - The verifier computes nc = (- c) mod p'.
  144. // This Step is not testable
  145. // 4.2.2 step 6 - The verifier computes R1 = G3.multiExp(K, smu, B, snu).
  146. // This Step is not testable
  147. // 4.2.2 step 7 - The verifier computes R2 = G3.multiExp(K', smu, B', snu,
  148. // T, nc).
  149. // This Step is not testable
  150. TEST_F(Epid11VerifierTest, NrVerifyRejectsSigWithInvalidCommitment) {
  151. // 4.2.2 step 8 - The verifier verifies c = Hash(p' || g3 || B || K || B' ||
  152. // K' || T || R1 || R2 || mSize || m).
  153. Epid11VerifierCtxObj verifier(this->kPubKeyStr);
  154. Epid11Signature const* epid_signature =
  155. reinterpret_cast<Epid11Signature const*>(
  156. this->kSigGrpXMember0Sha256RandbaseMsg0N2One.data());
  157. Epid11SigRl const* sig_rl =
  158. reinterpret_cast<Epid11SigRl const*>(this->kSigRl.data());
  159. std::vector<uint8_t> test_msg = this->kMsg0;
  160. test_msg[0]++;
  161. EXPECT_EQ(kEpidBadArgErr,
  162. Epid11NrVerify(verifier, &epid_signature->sigma0, test_msg.data(),
  163. test_msg.size(), &sig_rl->bk[0],
  164. &epid_signature->sigma[0]));
  165. }
  166. TEST_F(Epid11VerifierTest, NrVerifyRejectsSigWithMismatchCommitmentSize) {
  167. // 4.2.2 step 8 - The verifier verifies c = Hash(p' || g3 || B || K || B' ||
  168. // K' || T || R1 || R2 || mSize || m).
  169. Epid11VerifierCtxObj verifier(this->kPubKeyStr);
  170. Epid11Signature const* epid_signature =
  171. reinterpret_cast<Epid11Signature const*>(
  172. this->kSigGrpXMember0Sha256RandbaseMsg0N2One.data());
  173. Epid11SigRl const* sig_rl =
  174. reinterpret_cast<Epid11SigRl const*>(this->kSigRl.data());
  175. std::vector<uint8_t> test_msg = this->kMsg0;
  176. EXPECT_EQ(kEpidBadArgErr,
  177. Epid11NrVerify(verifier, &epid_signature->sigma0, test_msg.data(),
  178. test_msg.size() - 1, &sig_rl->bk[0],
  179. &epid_signature->sigma[0]));
  180. }
  181. /////////////////////////////////////////////////////////////////////
  182. // Accept
  183. // 4.2.2 step 9 - If all the above verifications succeed, the verifier
  184. // outputs true. If any of the above verifications fails,
  185. // the verifier aborts and outputs false
  186. TEST_F(Epid11VerifierTest, NrVerifyAcceptsSigWithRandomBaseName) {
  187. Epid11VerifierCtxObj verifier(this->kPubKeyStr);
  188. Epid11Signature const* epid_signature =
  189. reinterpret_cast<Epid11Signature const*>(
  190. this->kSigGrpXMember0Sha256RandbaseMsg0N2One.data());
  191. Epid11SigRl const* sig_rl =
  192. reinterpret_cast<Epid11SigRl const*>(this->kSigRl.data());
  193. EXPECT_EQ(kEpidSigValid,
  194. Epid11NrVerify(verifier, &epid_signature->sigma0,
  195. this->kMsg0.data(), this->kMsg0.size(),
  196. &sig_rl->bk[0], &epid_signature->sigma[0]));
  197. }
  198. TEST_F(Epid11VerifierTest, NrVerifyAcceptsMsgContainingAllPossibleBytes) {
  199. Epid11VerifierCtxObj verifier(this->kPubKeyStrForMsg0_255);
  200. Epid11Signature const* epid_signature =
  201. (Epid11Signature*)kSigGrp01Member0Sha256kBsn0Data_0_255.data();
  202. Epid11SigRl const* sig_rl =
  203. reinterpret_cast<Epid11SigRl const*>(this->kSigRlForMsg0_255.data());
  204. EXPECT_EQ(kEpidSigValid,
  205. Epid11NrVerify(verifier, &epid_signature->sigma0,
  206. this->kData_0_255.data(), this->kData_0_255.size(),
  207. &sig_rl->bk[0], &epid_signature->sigma[0]));
  208. }
  209. } // namespace