• 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 Epid11VerifierCreate unit tests.
20  */
21 
22 #include <cstring>
23 
24 #include "epid/common-testhelper/epid_gtest-testhelper.h"
25 #include "gtest/gtest.h"
26 
27 extern "C" {
28 #include "epid/verifier/1.1/api.h"
29 #include "epid/verifier/1.1/src/context.h"
30 }
31 
32 #include "epid/common-testhelper/1.1/verifier_wrapper-testhelper.h"
33 #include "epid/common-testhelper/errors-testhelper.h"
34 #include "epid/verifier/1.1/unittests/verifier-testhelper.h"
operator ==(Epid11VerifierPrecomp const & lhs,Epid11VerifierPrecomp const & rhs)35 bool operator==(Epid11VerifierPrecomp const& lhs,
36                 Epid11VerifierPrecomp const& rhs) {
37   return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
38 }
39 namespace {
40 //////////////////////////////////////////////////////////////////////////
41 // Epid11VerifierCreate Tests
TEST_F(Epid11VerifierTest,CreateFailsGivenNullPointer)42 TEST_F(Epid11VerifierTest, CreateFailsGivenNullPointer) {
43   Epid11VerifierCtx* ctx = nullptr;
44   EXPECT_EQ(kEpidBadArgErr,
45             Epid11VerifierCreate(&this->kPubKeyStr, &this->kVerifierPrecompStr,
46                                  nullptr));
47   Epid11VerifierDelete(&ctx);
48   EXPECT_EQ(kEpidBadArgErr,
49             Epid11VerifierCreate(nullptr, &this->kVerifierPrecompStr, &ctx));
50   Epid11VerifierDelete(&ctx);
51 }
TEST_F(Epid11VerifierTest,CreateSucceedsGivenNullPrecomp)52 TEST_F(Epid11VerifierTest, CreateSucceedsGivenNullPrecomp) {
53   Epid11VerifierCtx* ctx = nullptr;
54   EXPECT_EQ(kEpidNoErr, Epid11VerifierCreate(&this->kPubKeyStr, nullptr, &ctx));
55   Epid11VerifierDelete(&ctx);
56 }
TEST_F(Epid11VerifierTest,CreateSucceedsGivenValidPrecomp)57 TEST_F(Epid11VerifierTest, CreateSucceedsGivenValidPrecomp) {
58   Epid11VerifierCtx* ctx = nullptr;
59   EXPECT_EQ(kEpidNoErr, Epid11VerifierCreate(&this->kPubKeyStr,
60                                              &this->kVerifierPrecompStr, &ctx));
61   Epid11VerifierDelete(&ctx);
62 }
TEST_F(Epid11VerifierTest,CreateFailsGivenInvalidPubkey)63 TEST_F(Epid11VerifierTest, CreateFailsGivenInvalidPubkey) {
64   Epid11VerifierCtx* ctx = nullptr;
65   Epid11GroupPubKey pubkey_with_bad_h1 = this->kPubKeyStr;
66   pubkey_with_bad_h1.h1.x.data.data[31]++;  // munge h1 so not in G1
67   EXPECT_EQ(kEpidBadArgErr,
68             Epid11VerifierCreate(&pubkey_with_bad_h1, nullptr, &ctx));
69   Epid11VerifierDelete(&ctx);
70   Epid11GroupPubKey pubkey_with_bad_h2 = this->kPubKeyStr;
71   pubkey_with_bad_h2.h2.x.data.data[31]++;  // munge h2 so not in G1
72   EXPECT_EQ(kEpidBadArgErr,
73             Epid11VerifierCreate(&pubkey_with_bad_h2, nullptr, &ctx));
74   Epid11VerifierDelete(&ctx);
75   Epid11GroupPubKey pubkey_with_bad_w = this->kPubKeyStr;
76   pubkey_with_bad_w.w.x[0].data.data[31]++;  // munge w so not in G2
77   EXPECT_EQ(kEpidBadArgErr,
78             Epid11VerifierCreate(&pubkey_with_bad_w, nullptr, &ctx));
79   Epid11VerifierDelete(&ctx);
80 }
TEST_F(Epid11VerifierTest,CreateFailsGivenBadGroupIdInPrecomp)81 TEST_F(Epid11VerifierTest, CreateFailsGivenBadGroupIdInPrecomp) {
82   Epid11VerifierCtx* ctx = nullptr;
83   // tweak GID
84   auto verifier_precomp = this->kVerifierPrecompStr;
85   verifier_precomp.gid.data[0] = ~verifier_precomp.gid.data[0];
86   EXPECT_EQ(kEpidBadArgErr,
87             Epid11VerifierCreate(&this->kPubKeyStr, &verifier_precomp, &ctx));
88 }
89 //////////////////////////////////////////////////////////////////////////
90 // Epid11VerifierDelete Tests
TEST_F(Epid11VerifierTest,DeleteNullsVerifierCtx)91 TEST_F(Epid11VerifierTest, DeleteNullsVerifierCtx) {
92   Epid11VerifierCtx* ctx = nullptr;
93   THROW_ON_EPIDERR(Epid11VerifierCreate(&this->kPubKeyStr, nullptr, &ctx));
94   Epid11VerifierDelete(&ctx);
95   EXPECT_EQ(nullptr, ctx);
96 }
TEST_F(Epid11VerifierTest,DeleteWorksGivenNullVerifierCtx)97 TEST_F(Epid11VerifierTest, DeleteWorksGivenNullVerifierCtx) {
98   Epid11VerifierDelete(nullptr);
99   Epid11VerifierCtx* ctx = nullptr;
100   Epid11VerifierDelete(&ctx);
101 }
102 
103 //////////////////////////////////////////////////////////////////////////
104 // Epid11VerifierWritePrecomp
TEST_F(Epid11VerifierTest,WritePrecompFailsGivenNullPointer)105 TEST_F(Epid11VerifierTest, WritePrecompFailsGivenNullPointer) {
106   Epid11VerifierPrecomp precomp;
107   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
108   Epid11VerifierCtx* ctx = verifier;
109   EXPECT_EQ(kEpidBadArgErr, Epid11VerifierWritePrecomp(nullptr, &precomp));
110   EXPECT_EQ(kEpidBadArgErr, Epid11VerifierWritePrecomp(ctx, nullptr));
111 }
TEST_F(Epid11VerifierTest,WritePrecompSucceedGivenValidArgument)112 TEST_F(Epid11VerifierTest, WritePrecompSucceedGivenValidArgument) {
113   Epid11VerifierPrecomp precomp;
114   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
115   Epid11VerifierCtx* ctx = verifier;
116   EXPECT_EQ(kEpidNoErr, Epid11VerifierWritePrecomp(ctx, &precomp));
117   Epid11VerifierPrecomp expected_precomp = this->kVerifierPrecompStr;
118   EXPECT_EQ(expected_precomp, precomp);
119 
120   Epid11VerifierCtxObj verifier2(this->kPubKeyStr);
121   Epid11VerifierCtx* ctx2 = verifier2;
122   EXPECT_EQ(kEpidNoErr, Epid11VerifierWritePrecomp(ctx2, &precomp));
123   EXPECT_EQ(expected_precomp, precomp);
124 }
125 
126 //////////////////////////////////////////////////////////////////////////
127 // Epid11VerifierSetPrivRl
TEST_F(Epid11VerifierTest,SetPrivRlFailsGivenNullPointer)128 TEST_F(Epid11VerifierTest, SetPrivRlFailsGivenNullPointer) {
129   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
130   Epid11PrivRl prl = {0};
131   prl.gid = this->kPubKeyStr.gid;
132   EXPECT_EQ(kEpidBadArgErr,
133             Epid11VerifierSetPrivRl(nullptr, &prl, sizeof(prl)));
134   EXPECT_EQ(kEpidBadArgErr,
135             Epid11VerifierSetPrivRl(verifier, nullptr, sizeof(prl)));
136 }
137 
TEST_F(Epid11VerifierTest,SetPrivRlFailsGivenZeroSize)138 TEST_F(Epid11VerifierTest, SetPrivRlFailsGivenZeroSize) {
139   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
140   Epid11PrivRl prl = {0};
141   prl.gid = this->kPubKeyStr.gid;
142   EXPECT_EQ(kEpidBadArgErr, Epid11VerifierSetPrivRl(verifier, &prl, 0));
143 }
144 
145 // Size parameter must be at least big enough for n1 == 0 case
TEST_F(Epid11VerifierTest,SetPrivRlFailsGivenTooSmallSize)146 TEST_F(Epid11VerifierTest, SetPrivRlFailsGivenTooSmallSize) {
147   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
148   Epid11PrivRl prl = {0};
149   prl.gid = this->kPubKeyStr.gid;
150   EXPECT_EQ(kEpidBadArgErr,
151             Epid11VerifierSetPrivRl(verifier, &prl,
152                                     (sizeof(prl) - sizeof(prl.f)) - 1));
153   prl.n1 = this->kOctStr32_1;
154   EXPECT_EQ(kEpidBadArgErr,
155             Epid11VerifierSetPrivRl(verifier, &prl,
156                                     (sizeof(prl) - sizeof(prl.f)) - 1));
157 }
158 
159 // Size parameter must be cross-checked with n1 value in priv_rl
TEST_F(Epid11VerifierTest,SetPrivRlFailsGivenN1TooBigForSize)160 TEST_F(Epid11VerifierTest, SetPrivRlFailsGivenN1TooBigForSize) {
161   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
162   Epid11PrivRl prl = {0};
163   prl.gid = this->kPubKeyStr.gid;
164   prl.n1 = this->kOctStr32_1;
165   EXPECT_EQ(kEpidBadArgErr, Epid11VerifierSetPrivRl(
166                                 verifier, &prl, sizeof(prl) - sizeof(prl.f)));
167 }
168 
TEST_F(Epid11VerifierTest,SetPrivRlFailsGivenN1TooSmallForSize)169 TEST_F(Epid11VerifierTest, SetPrivRlFailsGivenN1TooSmallForSize) {
170   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
171   Epid11PrivRl prl = {0};
172   prl.gid = this->kPubKeyStr.gid;
173   EXPECT_EQ(kEpidBadArgErr,
174             Epid11VerifierSetPrivRl(verifier, &prl, sizeof(prl)));
175 }
176 
TEST_F(Epid11VerifierTest,SetPrivRlPassesGivenDefaultPrivRl)177 TEST_F(Epid11VerifierTest, SetPrivRlPassesGivenDefaultPrivRl) {
178   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
179   Epid11PrivRl prl = {0};
180   prl.gid = this->kPubKeyStr.gid;
181   EXPECT_EQ(kEpidNoErr, Epid11VerifierSetPrivRl(verifier, &prl,
182                                                 sizeof(prl) - sizeof(prl.f)));
183 }
184 
TEST_F(Epid11VerifierTest,SetPrivRlPassesGivenPrivRlWithSingleElement)185 TEST_F(Epid11VerifierTest, SetPrivRlPassesGivenPrivRlWithSingleElement) {
186   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
187   Epid11PrivRl prl = {0};
188   prl.gid = this->kPubKeyStr.gid;
189   prl.n1 = this->kOctStr32_1;
190   EXPECT_EQ(kEpidNoErr, Epid11VerifierSetPrivRl(verifier, &prl, sizeof(prl)));
191 }
192 
TEST_F(Epid11VerifierTest,SetPrivRlFailsGivenBadGroupId)193 TEST_F(Epid11VerifierTest, SetPrivRlFailsGivenBadGroupId) {
194   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
195   Epid11PrivRl prl = {0};
196   prl.gid = this->kPubKeyStr.gid;
197   prl.gid.data[0] = ~prl.gid.data[0];
198   prl.n1 = this->kOctStr32_1;
199   EXPECT_EQ(kEpidBadArgErr,
200             Epid11VerifierSetPrivRl(verifier, &prl, sizeof(prl)));
201 }
202 
TEST_F(Epid11VerifierTest,SetPrivRlFailsGivenOldVersion)203 TEST_F(Epid11VerifierTest, SetPrivRlFailsGivenOldVersion) {
204   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
205   Epid11PrivRl prl = {0};
206   prl.gid = this->kPubKeyStr.gid;
207   prl.version = this->kOctStr32_1;
208   EXPECT_EQ(kEpidNoErr, Epid11VerifierSetPrivRl(verifier, &prl,
209                                                 sizeof(prl) - sizeof(prl.f)));
210   OctStr32 octstr32_0 = {0x00, 0x00, 0x00, 0x00};
211   prl.version = octstr32_0;
212   EXPECT_EQ(kEpidBadArgErr, Epid11VerifierSetPrivRl(
213                                 verifier, &prl, sizeof(prl) - sizeof(prl.f)));
214 }
215 
216 //////////////////////////////////////////////////////////////////////////
217 // Epid11VerifierSetSigRl
TEST_F(Epid11VerifierTest,SetSigRlFailsGivenNullPointer)218 TEST_F(Epid11VerifierTest, SetSigRlFailsGivenNullPointer) {
219   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
220   Epid11SigRl* empty_sig_rl = (Epid11SigRl*)this->kEmptySigRl.data();
221   EXPECT_EQ(kEpidBadArgErr,
222             Epid11VerifierSetSigRl(nullptr, empty_sig_rl, sizeof(Epid11SigRl)));
223   EXPECT_EQ(kEpidBadArgErr,
224             Epid11VerifierSetSigRl(verifier, nullptr, sizeof(Epid11SigRl)));
225 }
226 
TEST_F(Epid11VerifierTest,SetSigRlFailsGivenZeroSize)227 TEST_F(Epid11VerifierTest, SetSigRlFailsGivenZeroSize) {
228   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
229   Epid11SigRl* empty_sig_rl = (Epid11SigRl*)this->kEmptySigRl.data();
230   EXPECT_EQ(kEpidBadArgErr, Epid11VerifierSetSigRl(verifier, empty_sig_rl, 0));
231 }
232 
233 // Size parameter must be at least big enough for n2 == 0 case
TEST_F(Epid11VerifierTest,SetSigRlFailsGivenTooSmallSize)234 TEST_F(Epid11VerifierTest, SetSigRlFailsGivenTooSmallSize) {
235   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
236   std::vector<uint8_t> empty_sig_rl_buf(this->kEmptySigRl);
237   Epid11SigRl* empty_sig_rl = (Epid11SigRl*)empty_sig_rl_buf.data();
238   EXPECT_EQ(kEpidBadArgErr,
239             Epid11VerifierSetSigRl(
240                 verifier, empty_sig_rl,
241                 (sizeof(*empty_sig_rl) - sizeof(empty_sig_rl->bk)) - 1));
242   empty_sig_rl->n2 = this->kOctStr32_1;
243   EXPECT_EQ(kEpidBadArgErr,
244             Epid11VerifierSetSigRl(
245                 verifier, empty_sig_rl,
246                 (sizeof(*empty_sig_rl) - sizeof(empty_sig_rl->bk)) - 1));
247 }
248 
TEST_F(Epid11VerifierTest,SetSigRlFailsGivenN2TooBigForSize)249 TEST_F(Epid11VerifierTest, SetSigRlFailsGivenN2TooBigForSize) {
250   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
251   std::vector<uint8_t> empty_sig_rl_buf(this->kEmptySigRl);
252   Epid11SigRl* empty_sig_rl = (Epid11SigRl*)empty_sig_rl_buf.data();
253   empty_sig_rl->n2 = this->kOctStr32_1;
254   EXPECT_EQ(
255       kEpidBadArgErr,
256       Epid11VerifierSetSigRl(verifier, empty_sig_rl,
257                              sizeof(*empty_sig_rl) - sizeof(empty_sig_rl->bk)));
258 }
259 
TEST_F(Epid11VerifierTest,SetSigRlFailsGivenN2TooSmallForSize)260 TEST_F(Epid11VerifierTest, SetSigRlFailsGivenN2TooSmallForSize) {
261   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
262   std::vector<uint8_t> empty_sig_rl_buf(this->kEmptySigRl);
263   Epid11SigRl* empty_sig_rl = (Epid11SigRl*)empty_sig_rl_buf.data();
264   EXPECT_EQ(kEpidBadArgErr, Epid11VerifierSetSigRl(verifier, empty_sig_rl,
265                                                    sizeof(*empty_sig_rl)));
266 }
267 
TEST_F(Epid11VerifierTest,SetSigRlWorksGivenSigRlWithNoElements)268 TEST_F(Epid11VerifierTest, SetSigRlWorksGivenSigRlWithNoElements) {
269   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
270   std::vector<uint8_t> empty_sig_rl_buf(this->kEmptySigRl);
271   Epid11SigRl* empty_sig_rl = (Epid11SigRl*)empty_sig_rl_buf.data();
272   size_t sig_rl_size = empty_sig_rl_buf.size() * sizeof(uint8_t);
273   EXPECT_EQ(kEpidNoErr,
274             Epid11VerifierSetSigRl(verifier, empty_sig_rl, sig_rl_size));
275 }
276 
TEST_F(Epid11VerifierTest,SetSigRlWorksGivenSigRlWithOneElement)277 TEST_F(Epid11VerifierTest, SetSigRlWorksGivenSigRlWithOneElement) {
278   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
279 
280   uint8_t sig_rl_data_n2_one[] = {
281       // gid
282       0x00, 0x00, 0x00, 0x7b,
283       // rev
284       0x00, 0x00, 0x00, 0x7b,
285       // n2
286       0x00, 0x00, 0x00, 0x01,
287       // bks
288       // bk1
289       0x67, 0x58, 0xb2, 0x9c, 0xad, 0x61, 0x1f, 0xfb, 0x74, 0x23, 0xea, 0x40,
290       0xe9, 0x66, 0x26, 0xb0, 0x43, 0xdc, 0x7e, 0xc7, 0x48, 0x88, 0x56, 0x59,
291       0xf3, 0x35, 0x9f, 0xdb, 0xfa, 0xa2, 0x49, 0x51, 0x85, 0x35, 0x42, 0x50,
292       0x8e, 0x79, 0x79, 0xc0, 0x6c, 0xcc, 0x39, 0x0b, 0xad, 0x3b, 0x39, 0x33,
293       0xae, 0xb2, 0xa1, 0xc5, 0x28, 0x6f, 0x48, 0x3a, 0xd2, 0x63, 0x5d, 0xfb,
294       0x1b, 0x1f, 0x8a, 0x63, 0x84, 0xdc, 0x2d, 0xad, 0x3b, 0x98, 0x3f, 0xc3,
295       0x8e, 0x18, 0xd7, 0xea, 0x18, 0x50, 0x0c, 0x50, 0x42, 0x77, 0xb2, 0x59,
296       0xf5, 0xd5, 0x38, 0xc3, 0x8d, 0x57, 0xf4, 0xe7, 0xb8, 0x74, 0x5a, 0x9e,
297       0x32, 0x75, 0xd1, 0xb4, 0xb3, 0x64, 0xbc, 0x23, 0xcd, 0x98, 0x29, 0x7a,
298       0x77, 0x51, 0xfc, 0x26, 0x81, 0x41, 0x9b, 0xf6, 0x21, 0xad, 0xc1, 0xd9,
299       0xab, 0x30, 0x25, 0x8d, 0x0c, 0x3b, 0x62, 0xe2};
300   Epid11SigRl* sig_rl = reinterpret_cast<Epid11SigRl*>(sig_rl_data_n2_one);
301   EXPECT_EQ(kEpidNoErr,
302             Epid11VerifierSetSigRl(verifier, sig_rl, sizeof(*sig_rl)));
303 }
304 
TEST_F(Epid11VerifierTest,SetSigRlWorksGivenSigRlWithTwoElement)305 TEST_F(Epid11VerifierTest, SetSigRlWorksGivenSigRlWithTwoElement) {
306   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
307   Epid11VerifierCtx* ctx = verifier;
308   Epid11SigRl const* sig_rl =
309       reinterpret_cast<Epid11SigRl const*>(this->kSigRl.data());
310   size_t sig_rl_size = this->kSigRl.size() * sizeof(uint8_t);
311   EXPECT_EQ(kEpidNoErr, Epid11VerifierSetSigRl(ctx, sig_rl, sig_rl_size));
312 }
313 
TEST_F(Epid11VerifierTest,SetSigRlFailsGivenBadGroupId)314 TEST_F(Epid11VerifierTest, SetSigRlFailsGivenBadGroupId) {
315   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
316   std::vector<uint8_t> empty_sig_rl_buf(this->kEmptySigRl);
317   Epid11SigRl* empty_sig_rl = (Epid11SigRl*)empty_sig_rl_buf.data();
318   empty_sig_rl->gid.data[0] = ~empty_sig_rl->gid.data[0];
319   EXPECT_EQ(
320       kEpidBadArgErr,
321       Epid11VerifierSetSigRl(verifier, empty_sig_rl,
322                              sizeof(*empty_sig_rl) - sizeof(empty_sig_rl->bk)));
323 }
324 
TEST_F(Epid11VerifierTest,SetSigRlFailsGivenOldVersion)325 TEST_F(Epid11VerifierTest, SetSigRlFailsGivenOldVersion) {
326   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
327   std::vector<uint8_t> empty_sig_rl_buf(this->kEmptySigRl);
328   Epid11SigRl* empty_sig_rl = (Epid11SigRl*)empty_sig_rl_buf.data();
329   empty_sig_rl->version = this->kOctStr32_1;
330   EXPECT_EQ(kEpidNoErr, Epid11VerifierSetSigRl(
331                             verifier, empty_sig_rl,
332                             sizeof(*empty_sig_rl) - sizeof(empty_sig_rl->bk)));
333   OctStr32 octstr32_0 = {0x00, 0x00, 0x00, 0x00};
334   empty_sig_rl->version = octstr32_0;
335   EXPECT_EQ(
336       kEpidBadArgErr,
337       Epid11VerifierSetSigRl(verifier, empty_sig_rl,
338                              sizeof(*empty_sig_rl) - sizeof(empty_sig_rl->bk)));
339 }
340 
341 //////////////////////////////////////////////////////////////////////////
342 // Epid11VerifierSetGroupRl
TEST_F(Epid11VerifierTest,SetGroupRlFailsGivenNullPointer)343 TEST_F(Epid11VerifierTest, SetGroupRlFailsGivenNullPointer) {
344   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
345   std::vector<uint8_t> group_rl(this->kGroupRl3GidBuf);
346   Epid11GroupRl* grl = (Epid11GroupRl*)group_rl.data();
347   EXPECT_EQ(kEpidBadArgErr,
348             Epid11VerifierSetGroupRl(nullptr, grl, group_rl.size()));
349   EXPECT_EQ(kEpidBadArgErr,
350             Epid11VerifierSetGroupRl(verifier, nullptr, group_rl.size()));
351 }
352 
TEST_F(Epid11VerifierTest,SetGroupRlFailsGivenSizeZero)353 TEST_F(Epid11VerifierTest, SetGroupRlFailsGivenSizeZero) {
354   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
355   std::vector<uint8_t> group_rl(this->kGroupRl3GidBuf);
356   Epid11GroupRl* grl = (Epid11GroupRl*)group_rl.data();
357   EXPECT_EQ(kEpidBadArgErr, Epid11VerifierSetGroupRl(verifier, grl, 0));
358 }
359 
TEST_F(Epid11VerifierTest,SetGroupRlFailsGivenSizeTooSmall)360 TEST_F(Epid11VerifierTest, SetGroupRlFailsGivenSizeTooSmall) {
361   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
362   std::vector<uint8_t> group_rl(this->kGroupRl3GidBuf);
363   Epid11GroupRl* grl = (Epid11GroupRl*)group_rl.data();
364   size_t grl_size = group_rl.size() - sizeof(grl->gid[0]);
365   EXPECT_EQ(kEpidBadArgErr,
366             Epid11VerifierSetGroupRl(verifier, grl, grl_size - 1));
367 }
368 
TEST_F(Epid11VerifierTest,SetGroupRlFailsGivenSizeTooLarge)369 TEST_F(Epid11VerifierTest, SetGroupRlFailsGivenSizeTooLarge) {
370   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
371   std::vector<uint8_t> group_rl(this->kGroupRl3GidBuf);
372   Epid11GroupRl* grl = (Epid11GroupRl*)group_rl.data();
373   size_t grl_size = group_rl.size() - sizeof(grl->gid[0]);
374   EXPECT_EQ(kEpidBadArgErr,
375             Epid11VerifierSetGroupRl(verifier, grl, grl_size + 1));
376 }
377 
TEST_F(Epid11VerifierTest,SetGroupRlFailsGivenN3ZeroAndGroupRLSizeTooBig)378 TEST_F(Epid11VerifierTest, SetGroupRlFailsGivenN3ZeroAndGroupRLSizeTooBig) {
379   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
380   std::vector<uint8_t> group_rl_3gid_n0_buf(this->kGroupRl3GidBuf);
381   group_rl_3gid_n0_buf[7] = 0x00;
382   Epid11GroupRl* group_rl = (Epid11GroupRl*)group_rl_3gid_n0_buf.data();
383   EXPECT_EQ(kEpidBadArgErr,
384             Epid11VerifierSetGroupRl(verifier, group_rl,
385                                      group_rl_3gid_n0_buf.size()));
386 }
387 
TEST_F(Epid11VerifierTest,SetGroupRlFailsGivenN3TooSmall)388 TEST_F(Epid11VerifierTest, SetGroupRlFailsGivenN3TooSmall) {
389   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
390   std::vector<uint8_t> group_rl_3gid_n2_buf(this->kGroupRl3GidBuf);
391   group_rl_3gid_n2_buf[7] = 0x02;
392   Epid11GroupRl* group_rl = (Epid11GroupRl*)group_rl_3gid_n2_buf.data();
393   EXPECT_EQ(kEpidBadArgErr,
394             Epid11VerifierSetGroupRl(verifier, group_rl,
395                                      group_rl_3gid_n2_buf.size()));
396 }
397 
TEST_F(Epid11VerifierTest,SetGroupRlFailsGivenN3TooLarge)398 TEST_F(Epid11VerifierTest, SetGroupRlFailsGivenN3TooLarge) {
399   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
400   std::vector<uint8_t> group_rl_3gid_n4_buf(this->kGroupRl3GidBuf);
401   group_rl_3gid_n4_buf[7] = 0x04;
402   Epid11GroupRl* group_rl = (Epid11GroupRl*)group_rl_3gid_n4_buf.data();
403   EXPECT_EQ(kEpidBadArgErr,
404             Epid11VerifierSetGroupRl(verifier, group_rl,
405                                      group_rl_3gid_n4_buf.size()));
406 }
407 
TEST_F(Epid11VerifierTest,SetGroupRlSucceedsGivenEmptyRL)408 TEST_F(Epid11VerifierTest, SetGroupRlSucceedsGivenEmptyRL) {
409   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
410   Epid11GroupRl* empty_grl = (Epid11GroupRl*)this->kGroupRlEmptyBuf.data();
411   size_t grl_size = this->kGroupRlEmptyBuf.size();
412   EXPECT_EQ(kEpidNoErr,
413             Epid11VerifierSetGroupRl(verifier, empty_grl, grl_size));
414 }
TEST_F(Epid11VerifierTest,SetGroupRlSucceedsGivenRLWith3gid)415 TEST_F(Epid11VerifierTest, SetGroupRlSucceedsGivenRLWith3gid) {
416   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
417   Epid11GroupRl* group_rl = (Epid11GroupRl*)this->kGroupRl3GidBuf.data();
418   EXPECT_EQ(kEpidNoErr, Epid11VerifierSetGroupRl(verifier, group_rl,
419                                                  this->kGroupRl3GidBuf.size()));
420 }
421 
TEST_F(Epid11VerifierTest,SetGroupRlFailsGivenOldVersion)422 TEST_F(Epid11VerifierTest, SetGroupRlFailsGivenOldVersion) {
423   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
424   Epid11GroupRl* group_rl = (Epid11GroupRl*)this->kGroupRl3GidBuf.data();
425   EXPECT_EQ(kEpidNoErr, Epid11VerifierSetGroupRl(verifier, group_rl,
426                                                  this->kGroupRl3GidBuf.size()));
427   Epid11GroupRl* empty_grl = (Epid11GroupRl*)this->kGroupRlEmptyBuf.data();
428   EXPECT_EQ(kEpidBadArgErr,
429             Epid11VerifierSetGroupRl(verifier, empty_grl,
430                                      this->kGroupRlEmptyBuf.size()));
431 }
432 
433 //////////////////////////////////////////////////////////////////////////
434 // Epid11VerifierSetBasename
TEST_F(Epid11VerifierTest,DefaultBasenameIsNull)435 TEST_F(Epid11VerifierTest, DefaultBasenameIsNull) {
436   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
437   Epid11VerifierCtx* ctx = verifier;
438   EXPECT_EQ(nullptr, ctx->basename);
439 }
TEST_F(Epid11VerifierTest,SetBasenameFailsGivenNullContext)440 TEST_F(Epid11VerifierTest, SetBasenameFailsGivenNullContext) {
441   auto& basename = this->kBsn0;
442   EXPECT_EQ(kEpidBadArgErr, Epid11VerifierSetBasename(nullptr, basename.data(),
443                                                       basename.size()));
444 }
TEST_F(Epid11VerifierTest,SetBasenameFailsGivenNullBasenameAndNonzeroLength)445 TEST_F(Epid11VerifierTest, SetBasenameFailsGivenNullBasenameAndNonzeroLength) {
446   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
447   Epid11VerifierCtx* ctx = verifier;
448   auto& basename = this->kBsn0;
449   EXPECT_EQ(kEpidBadArgErr,
450             Epid11VerifierSetBasename(ctx, nullptr, basename.size()));
451 }
TEST_F(Epid11VerifierTest,SetBasenameSucceedsGivenValidParameters)452 TEST_F(Epid11VerifierTest, SetBasenameSucceedsGivenValidParameters) {
453   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
454   Epid11VerifierCtx* ctx = verifier;
455   auto& basename = this->kBsn0;
456   EXPECT_EQ(kEpidNoErr,
457             Epid11VerifierSetBasename(ctx, basename.data(), basename.size()));
458   EXPECT_EQ(basename.size(), ctx->basename_len);
459   EXPECT_EQ(0, memcmp(basename.data(), ctx->basename, ctx->basename_len));
460   EXPECT_NE(nullptr, ctx->basename_hash);
461 }
TEST_F(Epid11VerifierTest,SetBasenameAcceptsZeroLengthBasename)462 TEST_F(Epid11VerifierTest, SetBasenameAcceptsZeroLengthBasename) {
463   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
464   Epid11VerifierCtx* ctx = verifier;
465   EXPECT_EQ(kEpidNoErr, Epid11VerifierSetBasename(ctx, "", 0));
466   EXPECT_EQ((size_t)0, ctx->basename_len);
467   EXPECT_NE(nullptr, ctx->basename_hash);
468 }
TEST_F(Epid11VerifierTest,SetBasenameResetsBasenameGivenNullBasename)469 TEST_F(Epid11VerifierTest, SetBasenameResetsBasenameGivenNullBasename) {
470   Epid11VerifierCtxObj verifier(this->kPubKeyStr, this->kVerifierPrecompStr);
471   Epid11VerifierCtx* ctx = verifier;
472   auto& basename = this->kBsn0;
473   THROW_ON_EPIDERR(
474       Epid11VerifierSetBasename(ctx, basename.data(), basename.size()));
475   THROW_ON_EPIDERR(Epid11VerifierSetBasename(ctx, nullptr, 0));
476   EXPECT_EQ(nullptr, ctx->basename_hash);
477 }
TEST_F(Epid11VerifierTest,SetBasenameAcceptsBsnContainingAllPossibleBytes)478 TEST_F(Epid11VerifierTest, SetBasenameAcceptsBsnContainingAllPossibleBytes) {
479   Epid11VerifierCtxObj verifier(this->kPubKeyStrForMsg0_255,
480                                 this->kVerifierPrecompStr);
481   Epid11VerifierCtx* ctx = verifier;
482   auto& basename = this->kData_0_255;
483   EXPECT_EQ(kEpidNoErr,
484             Epid11VerifierSetBasename(ctx, basename.data(), basename.size()));
485   EXPECT_EQ(basename.size(), ctx->basename_len);
486   EXPECT_EQ(0, memcmp(basename.data(), ctx->basename, ctx->basename_len));
487   EXPECT_NE(nullptr, ctx->basename_hash);
488 }
489 }  // namespace
490