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