tatepairing-test.cc 9.7 KB

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