tatepairing-test.cc 9.7 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 Intel(R) EPID 1.1 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/1.1/epid_params-testhelper.h"
  24. #include "epid/common-testhelper/errors-testhelper.h"
  25. #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
  26. extern "C" {
  27. #include "epid/common/math/tatepairing.h"
  28. }
  29. /// compares Epid11GtElemStr values
  30. bool operator==(Epid11GtElemStr const& lhs, Epid11GtElemStr const& rhs) {
  31. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  32. }
  33. namespace {
  34. class Epid11PairingTest : public ::testing::Test {
  35. public:
  36. virtual void SetUp() { params = new Epid11ParamsObj(); }
  37. virtual void TearDown() { delete params; }
  38. static const Epid11G1ElemStr kGaElemStr;
  39. static const Epid11G2ElemStr kGbElemStr;
  40. Epid11ParamsObj* params;
  41. };
  42. const Epid11G1ElemStr Epid11PairingTest::kGaElemStr = {
  43. 0x02, 0x5A, 0xC4, 0xC5, 0xCD, 0x7D, 0xAA, 0xFD, 0x26, 0xE5, 0x0B,
  44. 0xA9, 0xB4, 0xE1, 0x72, 0xA1, 0x65, 0x2D, 0x84, 0xAD, 0x34, 0x34,
  45. 0xF8, 0x62, 0x98, 0x6A, 0x15, 0xBE, 0xEA, 0xE3, 0xCC, 0x56, 0x05,
  46. 0x70, 0x5F, 0x4F, 0x11, 0xAF, 0x45, 0xCF, 0x04, 0x1B, 0x96, 0xAD,
  47. 0xEB, 0x26, 0xEE, 0x95, 0x65, 0x4B, 0xD3, 0xD6, 0x5C, 0x13, 0x76,
  48. 0xB7, 0x7A, 0xA1, 0xC6, 0xDA, 0xED, 0x5A, 0x40, 0xCE};
  49. const Epid11G2ElemStr Epid11PairingTest::kGbElemStr = {
  50. 0x02, 0x10, 0x9A, 0xF4, 0x06, 0x32, 0x30, 0x89, 0xCB, 0x95, 0xE9, 0x55,
  51. 0x0E, 0x9D, 0xAF, 0x0E, 0x98, 0xCD, 0xCA, 0xDC, 0xB1, 0xFF, 0xFC, 0xD1,
  52. 0x45, 0x66, 0xBB, 0x86, 0x46, 0x1E, 0x8C, 0x30, 0x04, 0x78, 0x53, 0xE1,
  53. 0x3F, 0x96, 0xC5, 0xE4, 0x15, 0x23, 0x7B, 0x1F, 0x3F, 0x2C, 0xD3, 0x95,
  54. 0x40, 0xBC, 0x7A, 0x31, 0x1F, 0x14, 0x38, 0x9E, 0x1A, 0xA5, 0xD6, 0x63,
  55. 0x10, 0x91, 0xE4, 0xD3, 0x00, 0xB4, 0x02, 0xBC, 0x47, 0xFA, 0xA6, 0x29,
  56. 0x82, 0x0B, 0xB1, 0xD5, 0xFF, 0xF2, 0xE6, 0xB0, 0xC6, 0xAE, 0xE8, 0x7B,
  57. 0x91, 0xD9, 0xEE, 0x66, 0x07, 0x1F, 0xFD, 0xA2, 0xE7, 0x02, 0x66, 0xDD,
  58. 0x05, 0x2E, 0xF8, 0xC6, 0xC1, 0x6A, 0xEF, 0x3C, 0xC1, 0x95, 0xF6, 0x26,
  59. 0xCE, 0x5E, 0x55, 0xD1, 0x64, 0x13, 0x28, 0xB1, 0x18, 0x57, 0xD8, 0x1B,
  60. 0x84, 0xFA, 0xEC, 0x7E, 0x5D, 0x99, 0x06, 0x49, 0x05, 0x73, 0x35, 0xA9,
  61. 0xA7, 0xF2, 0xA1, 0x92, 0x5F, 0x3E, 0x7C, 0xDF, 0xAC, 0xFE, 0x0F, 0xF5,
  62. 0x08, 0xD0, 0x3C, 0xAE, 0xCD, 0x58, 0x00, 0x5F, 0xD0, 0x84, 0x7E, 0xEA,
  63. 0x63, 0x57, 0xFE, 0xC6, 0x01, 0x56, 0xDA, 0xF3, 0x72, 0x61, 0xDA, 0xC6,
  64. 0x93, 0xB0, 0xAC, 0xEF, 0xAA, 0xD4, 0x51, 0x6D, 0xCA, 0x71, 0x1E, 0x06,
  65. 0x73, 0xEA, 0x83, 0xB2, 0xB1, 0x99, 0x4A, 0x4D, 0x4A, 0x0D, 0x35, 0x07};
  66. ///////////////////////////////////////////////////////////////////////
  67. // NewEpid11PairingState / DeleteEpid11PairingState
  68. // test that delete works in a "normal" valid case.
  69. TEST_F(Epid11PairingTest, DeleteWorksGivenNewlyCreatedPairingState) {
  70. Epid11PairingState* ps = nullptr;
  71. THROW_ON_EPIDERR(NewEpid11PairingState(this->params->G1, this->params->G2,
  72. this->params->GT, &ps));
  73. EXPECT_NO_THROW(DeleteEpid11PairingState(&ps));
  74. }
  75. // test that delete works if there is nothing to do
  76. TEST_F(Epid11PairingTest, DeleteWorksGivenNullPointer) {
  77. EXPECT_NO_THROW(DeleteEpid11PairingState(nullptr));
  78. Epid11PairingState* ps = nullptr;
  79. EXPECT_NO_THROW(DeleteEpid11PairingState(&ps));
  80. }
  81. // test that new succeeds with valid parameters
  82. TEST_F(Epid11PairingTest, NewSucceedsGivenValidParameters) {
  83. Epid11PairingState* ps = nullptr;
  84. EXPECT_EQ(kEpidNoErr,
  85. NewEpid11PairingState(this->params->G1, this->params->G2,
  86. this->params->GT, &ps));
  87. DeleteEpid11PairingState(&ps);
  88. }
  89. // test that new fails if any options are NULL
  90. TEST_F(Epid11PairingTest, NewFailsGivenNullParameters) {
  91. Epid11PairingState* ps = nullptr;
  92. EXPECT_EQ(kEpidBadArgErr, NewEpid11PairingState(nullptr, this->params->G2,
  93. this->params->GT, &ps));
  94. DeleteEpid11PairingState(&ps);
  95. EXPECT_EQ(kEpidBadArgErr, NewEpid11PairingState(this->params->G1, nullptr,
  96. this->params->GT, &ps));
  97. DeleteEpid11PairingState(&ps);
  98. EXPECT_EQ(
  99. kEpidBadArgErr,
  100. NewEpid11PairingState(this->params->G1, this->params->G2, nullptr, &ps));
  101. DeleteEpid11PairingState(&ps);
  102. EXPECT_EQ(kEpidBadArgErr,
  103. NewEpid11PairingState(this->params->G1, this->params->G2,
  104. this->params->GT, nullptr));
  105. }
  106. // test that new checks that G1 is valid
  107. TEST_F(Epid11PairingTest, NewFailsGivenInvalidG1) {
  108. Epid11PairingState* ps = nullptr;
  109. EXPECT_EQ(kEpidBadArgErr,
  110. NewEpid11PairingState(this->params->G2, this->params->G2,
  111. this->params->GT, &ps));
  112. DeleteEpid11PairingState(&ps);
  113. }
  114. // test that new checks that G2 is valid
  115. TEST_F(Epid11PairingTest, NewFailsGivenInvalidG2) {
  116. Epid11PairingState* ps = nullptr;
  117. EXPECT_EQ(kEpidBadArgErr,
  118. NewEpid11PairingState(this->params->G1, this->params->G1,
  119. this->params->GT, &ps));
  120. DeleteEpid11PairingState(&ps);
  121. }
  122. // test that new checks that GT is valid
  123. TEST_F(Epid11PairingTest, NewFailsGivenInvalidGT) {
  124. FiniteFieldObj GFp;
  125. Epid11PairingState* ps = nullptr;
  126. EXPECT_EQ(kEpidBadArgErr, NewEpid11PairingState(this->params->G1,
  127. this->params->G2, GFp, &ps));
  128. DeleteEpid11PairingState(&ps);
  129. }
  130. ///////////////////////////////////////////////////////////////////////
  131. // Pairing
  132. TEST_F(Epid11PairingTest, PairingWorksFromG1AndG2ToGt) {
  133. Epid11GtElemStr r_expected_str = {
  134. 0x02, 0xE1, 0x84, 0x16, 0x53, 0x10, 0x0E, 0xEC, 0xFB, 0xDE, 0xF3, 0x5E,
  135. 0x2E, 0x26, 0xEE, 0x45, 0x0C, 0xD7, 0x97, 0xA7, 0x35, 0x43, 0x08, 0x5E,
  136. 0x03, 0xB9, 0xFE, 0x91, 0x8A, 0x02, 0x14, 0xB4, 0x07, 0x7F, 0x8A, 0x5E,
  137. 0xFD, 0xE1, 0x83, 0xC9, 0xCE, 0x1C, 0xC9, 0xF1, 0xCC, 0xB0, 0x52, 0x81,
  138. 0xAD, 0x80, 0x2D, 0x13, 0x1C, 0x32, 0xEC, 0xAF, 0xA0, 0x8B, 0x66, 0x05,
  139. 0x0A, 0x89, 0x26, 0xAD, 0x06, 0x75, 0x3B, 0x3B, 0xE5, 0xFB, 0x62, 0x20,
  140. 0xA8, 0xC3, 0x91, 0xC6, 0x26, 0xC6, 0x58, 0x71, 0xB1, 0x85, 0x06, 0xBD,
  141. 0xAE, 0x06, 0x51, 0xF9, 0x86, 0x2A, 0xC1, 0x5A, 0x11, 0xBA, 0x17, 0xE1,
  142. 0x01, 0x4B, 0x22, 0x66, 0xEB, 0xCF, 0x7E, 0x2B, 0xE7, 0x0A, 0xF2, 0x77,
  143. 0x1C, 0xE6, 0x48, 0x8F, 0x3E, 0xD8, 0x7D, 0x71, 0xF1, 0x78, 0x4C, 0x80,
  144. 0x93, 0xF8, 0x08, 0xB7, 0xCB, 0xAF, 0x04, 0xDF, 0x04, 0x5C, 0x19, 0x3C,
  145. 0xD3, 0x29, 0x11, 0xE7, 0xC5, 0x58, 0x68, 0xEA, 0x65, 0xBB, 0x48, 0x5F,
  146. 0x3A, 0x62, 0xD9, 0x62, 0x40, 0x57, 0x53, 0x19, 0x9B, 0xB5, 0x6C, 0x52,
  147. 0x0C, 0x33, 0x27, 0x14, 0x06, 0x6A, 0xAD, 0xB0, 0x38, 0x41, 0xD0, 0xA5,
  148. 0x37, 0x54, 0xC5, 0x3E, 0x3B, 0x5F, 0x1A, 0xAF, 0x75, 0x8F, 0xCA, 0x42,
  149. 0xB9, 0xA6, 0x1E, 0x18, 0xB2, 0x6B, 0x31, 0x7D, 0x5C, 0xC6, 0xE8, 0xDC};
  150. Epid11GtElemStr r_str = {0};
  151. FfElementObj r(&this->params->GT);
  152. EcPointObj ga_elem(&this->params->G1, this->kGaElemStr);
  153. EcPointObj gb_elem(&this->params->G2, this->kGbElemStr);
  154. Epid11PairingState* ps = nullptr;
  155. THROW_ON_EPIDERR(NewEpid11PairingState(this->params->G1, this->params->G2,
  156. this->params->GT, &ps));
  157. EXPECT_EQ(kEpidNoErr, Epid11Pairing(ps, ga_elem, gb_elem, r));
  158. DeleteEpid11PairingState(&ps);
  159. THROW_ON_EPIDERR(WriteFfElement(this->params->GT, r, &r_str, sizeof(r_str)));
  160. EXPECT_EQ(r_expected_str, r_str);
  161. }
  162. TEST_F(Epid11PairingTest, PairingGivenPointAtInfinityReturns1) {
  163. Epid11GtElemStr r_expected_str = {0};
  164. r_expected_str.a[0].a[0].data.data[31] = 1;
  165. Epid11GtElemStr r_str = {0};
  166. FfElementObj r(&this->params->GT);
  167. EcPointObj ga_elem(&this->params->G1);
  168. EcPointObj gb_elem(&this->params->G2, this->kGbElemStr);
  169. Epid11PairingState* ps = nullptr;
  170. THROW_ON_EPIDERR(NewEpid11PairingState(this->params->G1, this->params->G2,
  171. this->params->GT, &ps));
  172. EXPECT_EQ(kEpidNoErr, Epid11Pairing(ps, ga_elem, gb_elem, r));
  173. DeleteEpid11PairingState(&ps);
  174. THROW_ON_EPIDERR(WriteFfElement(this->params->GT, r, &r_str, sizeof(r_str)));
  175. EXPECT_EQ(r_expected_str, r_str);
  176. }
  177. TEST_F(Epid11PairingTest, PairingFailsOnPointMissmatch) {
  178. FfElementObj r(&this->params->GT);
  179. EcPointObj ga_elem(&this->params->G1, this->kGaElemStr);
  180. EcPointObj gb_elem(&this->params->G2, this->kGbElemStr);
  181. Epid11PairingState* ps = nullptr;
  182. THROW_ON_EPIDERR(NewEpid11PairingState(this->params->G1, this->params->G2,
  183. this->params->GT, &ps));
  184. EXPECT_EQ(kEpidBadArgErr, Epid11Pairing(ps, gb_elem, ga_elem, r));
  185. DeleteEpid11PairingState(&ps);
  186. }
  187. TEST_F(Epid11PairingTest, PairingFailsOnInvalidPointInG1) {
  188. FfElementObj r(&this->params->GT);
  189. EcPointObj ga_elem(&this->params->G3);
  190. EcPointObj gb_elem(&this->params->G2, this->kGbElemStr);
  191. Epid11PairingState* ps = nullptr;
  192. THROW_ON_EPIDERR(NewEpid11PairingState(this->params->G1, this->params->G2,
  193. this->params->GT, &ps));
  194. EXPECT_EQ(kEpidBadArgErr, Epid11Pairing(ps, gb_elem, ga_elem, r));
  195. DeleteEpid11PairingState(&ps);
  196. }
  197. } // namespace