pairing-test.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 Pairing unit tests.
  19. */
  20. #include <cstring>
  21. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  22. #include "gtest/gtest.h"
  23. #include "epid/common-testhelper/ecgroup_wrapper-testhelper.h"
  24. #include "epid/common-testhelper/ecpoint_wrapper-testhelper.h"
  25. #include "epid/common-testhelper/epid_params-testhelper.h"
  26. #include "epid/common-testhelper/errors-testhelper.h"
  27. #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
  28. #include "epid/common-testhelper/finite_field_wrapper-testhelper.h"
  29. extern "C" {
  30. #include "epid/common/math/pairing.h"
  31. #include "epid/common/math/src/ecgroup-internal.h"
  32. #include "epid/common/math/src/finitefield-internal.h"
  33. #include "epid/common/math/src/pairing-internal.h"
  34. }
  35. /// compares Fq12ElemStr values
  36. bool operator==(GtElemStr const& lhs, GtElemStr const& rhs) {
  37. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  38. }
  39. namespace {
  40. class PairingTest : public Epid20Params, public ::testing::Test {
  41. public:
  42. static const BigNumStr t_str;
  43. static const G1ElemStr ga_elem_str;
  44. static const G2ElemStr gb_elem_str;
  45. virtual void SetUp() { params = new Epid20Params(); }
  46. virtual void TearDown() { delete params; }
  47. Epid20Params* params;
  48. };
  49. const BigNumStr PairingTest::t_str = {
  50. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x00, 0x68, 0x82, 0xF5, 0xC0, 0x30, 0xB0, 0xA8, 0x01};
  53. const G1ElemStr PairingTest::ga_elem_str = {
  54. 0xd7, 0xe2, 0xf9, 0x37, 0x21, 0x0f, 0x09, 0x97, 0x0f, 0xca, 0xa6,
  55. 0x03, 0x7d, 0x91, 0xc3, 0x75, 0x8a, 0xc9, 0x44, 0x11, 0xfc, 0xaa,
  56. 0x55, 0x67, 0xba, 0xce, 0xaf, 0x8d, 0xf6, 0x7c, 0x84, 0x83, 0x04,
  57. 0xb7, 0xa6, 0xff, 0x9f, 0x0d, 0x26, 0x73, 0xaf, 0x6c, 0xd0, 0x0a,
  58. 0xf6, 0x13, 0xc9, 0x44, 0x3f, 0xf0, 0x82, 0x58, 0x48, 0x59, 0x03,
  59. 0x3f, 0x88, 0xe2, 0x46, 0xd6, 0x0f, 0x93, 0x42, 0x4b,
  60. };
  61. const G2ElemStr PairingTest::gb_elem_str = {
  62. 0x3f, 0x4c, 0xb5, 0x2d, 0xbc, 0x72, 0xb0, 0x9c, 0x6f, 0xb2, 0xb5, 0xc1,
  63. 0xdc, 0xfb, 0xda, 0x35, 0x91, 0xa6, 0x8d, 0x51, 0x37, 0x70, 0xe2, 0x17,
  64. 0xad, 0x53, 0x23, 0xdc, 0xa3, 0xc3, 0xfd, 0x4c, 0x90, 0xfa, 0x4f, 0xa2,
  65. 0xcb, 0x35, 0xf3, 0x50, 0x5e, 0x8e, 0xf4, 0xce, 0x7f, 0xb0, 0x8a, 0x69,
  66. 0x49, 0xdf, 0xf5, 0x4f, 0xb0, 0xc1, 0xd7, 0xf9, 0xb8, 0xfb, 0x89, 0xd1,
  67. 0xb6, 0xf8, 0x74, 0x04, 0xef, 0xc6, 0x60, 0x05, 0x62, 0xf3, 0x17, 0x5a,
  68. 0x80, 0xf4, 0x4b, 0x97, 0x08, 0x3e, 0x43, 0xa1, 0x44, 0x4c, 0x54, 0x86,
  69. 0x16, 0x20, 0xb9, 0xcc, 0xfb, 0xbd, 0x00, 0x5f, 0xc8, 0x01, 0xfb, 0x5b,
  70. 0xc1, 0x6e, 0x2b, 0x46, 0xe2, 0x04, 0x70, 0xeb, 0xa2, 0xaa, 0x86, 0x5a,
  71. 0x35, 0x14, 0x0e, 0xc9, 0xdf, 0xba, 0x9b, 0x6f, 0x3a, 0xca, 0x94, 0x9c,
  72. 0x44, 0x89, 0x94, 0xa3, 0xeb, 0x61, 0x8b, 0x01,
  73. };
  74. ///////////////////////////////////////////////////////////////////////
  75. // NewPairingState / DeletePairingState
  76. // test that delete works in a "normal" valid case.
  77. TEST_F(PairingTest, DeleteWorksGivenNewlyCreatedPairingState) {
  78. PairingState* ps = nullptr;
  79. EpidStatus sts = kEpidNoErr;
  80. EXPECT_EQ(kEpidNoErr,
  81. NewPairingState(this->params->G1, this->params->G2,
  82. this->params->GT, &this->t_str, true, &ps));
  83. EXPECT_EQ(kEpidNoErr, sts);
  84. EXPECT_NO_THROW(DeletePairingState(&ps));
  85. }
  86. // test that delete works if there is nothing to do
  87. TEST_F(PairingTest, DeleteWorksGivenNullPointer) {
  88. EXPECT_NO_THROW(DeletePairingState(nullptr));
  89. PairingState* ps = nullptr;
  90. EXPECT_NO_THROW(DeletePairingState(&ps));
  91. }
  92. // test that new succeeds with valid parameters
  93. TEST_F(PairingTest, NewSucceedsGivenValidParameters) {
  94. PairingState* ps = nullptr;
  95. EXPECT_EQ(kEpidNoErr,
  96. NewPairingState(this->params->G1, this->params->G2,
  97. this->params->GT, &this->t_str, true, &ps));
  98. DeletePairingState(&ps);
  99. }
  100. // test that new fails if any options are NULL
  101. TEST_F(PairingTest, NewFailsGivenNullParameters) {
  102. PairingState* ps = nullptr;
  103. EXPECT_EQ(kEpidBadArgErr,
  104. NewPairingState(nullptr, this->params->G2, this->params->GT,
  105. &this->t_str, true, &ps));
  106. DeletePairingState(&ps);
  107. EXPECT_EQ(kEpidBadArgErr,
  108. NewPairingState(this->params->G1, nullptr, this->params->GT,
  109. &this->t_str, true, &ps));
  110. DeletePairingState(&ps);
  111. EXPECT_EQ(kEpidBadArgErr, NewPairingState(this->params->G1, this->params->G2,
  112. nullptr, &this->t_str, true, &ps));
  113. DeletePairingState(&ps);
  114. EXPECT_EQ(kEpidBadArgErr,
  115. NewPairingState(this->params->G1, this->params->G2,
  116. this->params->GT, nullptr, true, &ps));
  117. DeletePairingState(&ps);
  118. EXPECT_EQ(kEpidBadArgErr,
  119. NewPairingState(this->params->G1, this->params->G2,
  120. this->params->GT, &this->t_str, true, nullptr));
  121. }
  122. // test that new checks that G1 is valid
  123. TEST_F(PairingTest, NewFailsGivenInvalidG1) {
  124. PairingState* ps = nullptr;
  125. EcGroup ga;
  126. ga.ipp_ec = nullptr;
  127. ga.scratch_buffer = nullptr;
  128. EXPECT_EQ(kEpidBadArgErr,
  129. NewPairingState(&ga, this->params->G2, this->params->GT,
  130. &this->t_str, true, &ps));
  131. DeletePairingState(&ps);
  132. }
  133. // test that new checks that G2 is valid
  134. TEST_F(PairingTest, NewFailsGivenInvalidG2) {
  135. PairingState* ps = nullptr;
  136. EcGroup gb;
  137. gb.ipp_ec = nullptr;
  138. gb.scratch_buffer = nullptr;
  139. EXPECT_EQ(kEpidBadArgErr,
  140. NewPairingState(this->params->G1, &gb, this->params->GT,
  141. &this->t_str, true, &ps));
  142. DeletePairingState(&ps);
  143. }
  144. // test that new checks that GT is valid
  145. TEST_F(PairingTest, NewFailsGivenInvalidGT) {
  146. PairingState* ps = nullptr;
  147. FiniteField ff;
  148. ff.ipp_ff = nullptr;
  149. EXPECT_EQ(kEpidBadArgErr, NewPairingState(this->params->G1, this->params->G2,
  150. &ff, &this->t_str, true, &ps));
  151. DeletePairingState(&ps);
  152. }
  153. ///////////////////////////////////////////////////////////////////////
  154. // Pairing
  155. TEST_F(PairingTest, PairingWorksFromG1AndG2ToGt) {
  156. const bool neg = true;
  157. GtElemStr r_expected_str = {
  158. 0xba, 0x10, 0x1f, 0xf6, 0x46, 0x8b, 0xe9, 0x32, 0x4f, 0xc0, 0xa5, 0x01,
  159. 0xad, 0x5e, 0xe2, 0x31, 0x16, 0x29, 0x96, 0xed, 0xa7, 0xde, 0x4c, 0xe1,
  160. 0xd2, 0x8d, 0x33, 0xca, 0x50, 0xab, 0x7b, 0xc6, 0x15, 0xeb, 0x79, 0xf4,
  161. 0xeb, 0xde, 0x30, 0xb6, 0xc4, 0x07, 0x7c, 0x42, 0xcb, 0x04, 0x54, 0xf2,
  162. 0x1f, 0x4d, 0x1f, 0xc0, 0xdf, 0xa2, 0x2b, 0x9e, 0x34, 0xc4, 0x4c, 0x84,
  163. 0x14, 0xd3, 0x62, 0x07, 0xf1, 0x8b, 0x84, 0xd1, 0x46, 0x57, 0xb6, 0xe7,
  164. 0x80, 0xe1, 0x46, 0x49, 0x1c, 0x0d, 0xef, 0x81, 0x31, 0xb0, 0xbe, 0x8c,
  165. 0xb9, 0x08, 0xd0, 0xd3, 0xc4, 0x56, 0xca, 0xad, 0xf9, 0x1d, 0x75, 0x19,
  166. 0x3f, 0xee, 0x7c, 0x43, 0xc1, 0xfa, 0x4e, 0x50, 0xb7, 0x19, 0x01, 0x00,
  167. 0x6f, 0xd5, 0x16, 0xb6, 0xf4, 0x85, 0xe0, 0xeb, 0x2e, 0x5f, 0x0a, 0x7e,
  168. 0xf8, 0xac, 0xbc, 0x05, 0xec, 0x73, 0xb5, 0x57, 0xe3, 0xb3, 0x18, 0x29,
  169. 0xbb, 0xef, 0x86, 0x50, 0x87, 0xcf, 0x70, 0xba, 0x13, 0x8b, 0xb1, 0xb6,
  170. 0x2d, 0x6f, 0x65, 0x3d, 0xa1, 0x0b, 0xe3, 0x92, 0xc5, 0x72, 0x86, 0x6a,
  171. 0xb3, 0xeb, 0xe0, 0xe5, 0xda, 0x0e, 0x57, 0x87, 0xd5, 0xa9, 0x61, 0xa5,
  172. 0x1e, 0xcb, 0x04, 0x86, 0xcd, 0xc3, 0x18, 0x2a, 0x36, 0xa0, 0x81, 0x73,
  173. 0xe7, 0x13, 0x87, 0x80, 0x8d, 0x1a, 0xfe, 0x6e, 0x4b, 0xa3, 0x13, 0x03,
  174. 0x66, 0x9e, 0x80, 0x4d, 0x8a, 0xaa, 0x00, 0x95, 0x72, 0xce, 0xbb, 0x51,
  175. 0xe8, 0x01, 0x09, 0x41, 0xd3, 0x63, 0x28, 0x05, 0xa4, 0xbe, 0xd6, 0x41,
  176. 0xa6, 0x2f, 0x5f, 0xbf, 0x0b, 0x13, 0xb4, 0x54, 0x5b, 0x50, 0x65, 0xdc,
  177. 0x6f, 0x29, 0xd6, 0xda, 0xbf, 0xc2, 0x06, 0xea, 0x3b, 0xb2, 0xf1, 0xd4,
  178. 0x26, 0x5c, 0x92, 0x6b, 0x95, 0x6d, 0x88, 0xab, 0x8f, 0xc6, 0x9d, 0x31,
  179. 0xe4, 0x9b, 0x71, 0x49, 0xe0, 0xce, 0x97, 0x8f, 0xc9, 0x9f, 0xbc, 0xa8,
  180. 0x4a, 0xc6, 0xaa, 0x4a, 0xc8, 0x0d, 0x2a, 0x60, 0x1a, 0x43, 0x40, 0x03,
  181. 0xb3, 0x53, 0x30, 0x98, 0x1f, 0x3f, 0xdf, 0x5c, 0x0f, 0xf0, 0x84, 0x8e,
  182. 0x5a, 0x5d, 0x41, 0xd2, 0x47, 0x78, 0x6d, 0x9f, 0x89, 0xce, 0xf5, 0x8e,
  183. 0xb6, 0x54, 0xa2, 0x26, 0xe5, 0x40, 0x39, 0x5c, 0x59, 0x08, 0xb3, 0xda,
  184. 0xf5, 0xf8, 0xa0, 0x18, 0x33, 0x57, 0xd1, 0x72, 0xbb, 0xba, 0x6c, 0xed,
  185. 0xe8, 0xa0, 0x5e, 0xc8, 0x81, 0xc5, 0xac, 0x15, 0x1b, 0xd0, 0xe6, 0xc8,
  186. 0x92, 0xf9, 0x43, 0x03, 0x5a, 0x00, 0x42, 0xe3, 0x49, 0xa5, 0xf7, 0x19,
  187. 0x78, 0x8a, 0x39, 0x89, 0x32, 0xae, 0xbf, 0x4d, 0x4b, 0xb3, 0x33, 0x76,
  188. 0x16, 0xfd, 0x0b, 0xfe, 0x42, 0x1e, 0x17, 0x37, 0x2a, 0x04, 0xea, 0x26,
  189. 0xba, 0x6e, 0x2c, 0x36, 0xaf, 0x35, 0x1b, 0x75, 0x6d, 0x17, 0xdc, 0x8e,
  190. };
  191. GtElemStr r_str = {0};
  192. FfElementObj r(&this->params->GT);
  193. EcPointObj ga_elem(&this->params->G1, this->ga_elem_str);
  194. EcPointObj gb_elem(&this->params->G2, this->gb_elem_str);
  195. PairingState* ps = nullptr;
  196. THROW_ON_EPIDERR(NewPairingState(this->params->G1, this->params->G2,
  197. this->params->GT, &this->t_str, neg, &ps));
  198. EXPECT_EQ(kEpidNoErr, Pairing(ps, ga_elem, gb_elem, r));
  199. DeletePairingState(&ps);
  200. THROW_ON_EPIDERR(WriteFfElement(this->params->GT, r, &r_str, sizeof(r_str)));
  201. EXPECT_EQ(r_expected_str, r_str);
  202. }
  203. // test that pairing fails if any options are NULL
  204. TEST_F(PairingTest, PairingFailsGivenNullParameters) {
  205. const bool neg = true;
  206. FfElementObj r(&this->params->GT);
  207. EcPointObj ga_elem(&this->params->G1, this->ga_elem_str);
  208. EcPointObj gb_elem(&this->params->G2, this->gb_elem_str);
  209. PairingState* ps = nullptr;
  210. THROW_ON_EPIDERR(NewPairingState(this->params->G1, this->params->G2,
  211. this->params->GT, &this->t_str, neg, &ps));
  212. EXPECT_EQ(kEpidBadArgErr, Pairing(NULL, ga_elem, gb_elem, r));
  213. EXPECT_EQ(kEpidBadArgErr, Pairing(ps, NULL, gb_elem, r));
  214. EXPECT_EQ(kEpidBadArgErr, Pairing(ps, ga_elem, NULL, r));
  215. EXPECT_EQ(kEpidBadArgErr, Pairing(ps, ga_elem, gb_elem, NULL));
  216. DeletePairingState(&ps);
  217. }
  218. // test that pairing checks that the first parameter of the pairing is in a
  219. // EcGroup, that was used to create pairing state.
  220. TEST_F(PairingTest, PairingFailsGivenInvalidGaElem) {
  221. const bool neg = true;
  222. FfElementObj r(&this->params->GT);
  223. // put G2 element instead of G1
  224. EcPointObj mismatched_ga_elem(&this->params->G2, this->gb_elem_str);
  225. EcPointObj gb_elem(&this->params->G2, this->gb_elem_str);
  226. PairingState* ps = nullptr;
  227. THROW_ON_EPIDERR(NewPairingState(this->params->G1, this->params->G2,
  228. this->params->GT, &this->t_str, neg, &ps));
  229. EXPECT_EQ(kEpidBadArgErr, Pairing(ps, mismatched_ga_elem, gb_elem, r));
  230. DeletePairingState(&ps);
  231. }
  232. // test that pairing checks that the second parameter of the pairing is in a
  233. // EcGroup, that was used to create pairing state.
  234. TEST_F(PairingTest, PairingFailsGivenInvalidGbElem) {
  235. const bool neg = true;
  236. FfElementObj r(&this->params->GT);
  237. EcPointObj ga_elem(&this->params->G1, this->ga_elem_str);
  238. // put G1 element instead of G2
  239. EcPointObj mismatched_gb_elem(&this->params->G1, this->ga_elem_str);
  240. PairingState* ps = nullptr;
  241. THROW_ON_EPIDERR(NewPairingState(this->params->G1, this->params->G2,
  242. this->params->GT, &this->t_str, neg, &ps));
  243. EXPECT_EQ(kEpidBadArgErr, Pairing(ps, ga_elem, mismatched_gb_elem, r));
  244. DeletePairingState(&ps);
  245. }
  246. } // namespace