1 /*############################################################################
2   # Copyright 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 Provision compressed unit tests.
20  */
21 #include <cstring>
22 #include <vector>
23 
24 #include "epid/common-testhelper/epid_gtest-testhelper.h"
25 #include "gtest/gtest.h"
26 
27 #include "epid/common-testhelper/errors-testhelper.h"
28 #include "epid/common-testhelper/mem_params-testhelper.h"
29 #include "epid/common-testhelper/prng-testhelper.h"
30 #include "epid/member/unittests/member-testhelper.h"
31 
32 extern "C" {
33 #include "epid/member/api.h"
34 #include "epid/member/src/context.h"
35 }
36 
37 namespace {
38 
ProvisionCompressedAndStart(MemberCtx * ctx,GroupPubKey const * pub_key,CompressedPrivKey const * compressed_privkey,MemberPrecomp const * precomp_str)39 EpidStatus ProvisionCompressedAndStart(
40     MemberCtx* ctx, GroupPubKey const* pub_key,
41     CompressedPrivKey const* compressed_privkey,
42     MemberPrecomp const* precomp_str) {
43   EpidStatus sts;
44   sts = EpidProvisionCompressed(ctx, pub_key, compressed_privkey, precomp_str);
45   if (sts != kEpidNoErr) {
46     return sts;
47   }
48   sts = EpidMemberStartup(ctx);
49   return sts;
50 }
51 
TEST_F(EpidMemberTest,ProvisionCompressedFailsGivenNullParameters)52 TEST_F(EpidMemberTest, ProvisionCompressedFailsGivenNullParameters) {
53   Prng prng;
54   GroupPubKey pub_key = this->kGrpXKey;
55   CompressedPrivKey priv_key = this->kGrpXMember9CompressedKey;
56   // Note: this MemberPrecomp is for the wrong group, however it should not
57   // be checked in Provision because doing so would negate the performance
58   // boost of using the precomp.
59   MemberPrecomp precomp = this->kMemberPrecomp;
60   MemberParams params = {0};
61   SetMemberParams(&Prng::Generate, &prng, nullptr, &params);
62   MemberCtxObj member(&params);
63 
64   EXPECT_EQ(kEpidBadArgErr,
65             EpidProvisionCompressed(nullptr, &pub_key, &priv_key, &precomp));
66   EXPECT_EQ(kEpidBadArgErr,
67             EpidProvisionCompressed(member, nullptr, &priv_key, &precomp));
68   EXPECT_EQ(kEpidBadArgErr,
69             EpidProvisionCompressed(member, &pub_key, nullptr, &precomp));
70   EXPECT_EQ(kEpidBadArgErr,
71             EpidProvisionCompressed(nullptr, &pub_key, &priv_key, nullptr));
72   EXPECT_EQ(kEpidBadArgErr,
73             EpidProvisionCompressed(member, nullptr, &priv_key, nullptr));
74   EXPECT_EQ(kEpidBadArgErr,
75             EpidProvisionCompressed(member, &pub_key, nullptr, nullptr));
76 }
77 
TEST_F(EpidMemberTest,ProvisionCompressedSucceedsGivenValidParameters)78 TEST_F(EpidMemberTest, ProvisionCompressedSucceedsGivenValidParameters) {
79   Prng prng;
80   GroupPubKey pub_key = this->kGrpXKey;
81   CompressedPrivKey priv_key = this->kGrpXMember9CompressedKey;
82   // Note: this MemberPrecomp is for the wrong group, however it should not
83   // be checked in Provision because doing so would negate the performance
84   // boost of using the precomp.
85   MemberPrecomp precomp = this->kMemberPrecomp;
86   MemberParams params = {0};
87   SetMemberParams(&Prng::Generate, &prng, nullptr, &params);
88   MemberCtxObj member(&params);
89 
90   EXPECT_EQ(kEpidNoErr,
91             EpidProvisionCompressed(member, &pub_key, &priv_key, &precomp));
92   EXPECT_EQ(kEpidNoErr,
93             EpidProvisionCompressed(member, &pub_key, &priv_key, nullptr));
94 }
95 
TEST_F(EpidMemberTest,ProvisionCompressedFailsForInvalidGroupPubKey)96 TEST_F(EpidMemberTest, ProvisionCompressedFailsForInvalidGroupPubKey) {
97   Prng prng;
98   GroupPubKey pub_key = this->kGrpXKey;
99   CompressedPrivKey priv_key = this->kGrpXMember9CompressedKey;
100   // Note: this MemberPrecomp is for the wrong group, however it should not
101   // be checked in Provision because doing so would negate the performance
102   // boost of using the precomp.
103   MemberPrecomp precomp = this->kMemberPrecomp;
104   MemberParams params = {0};
105   SetMemberParams(&Prng::Generate, &prng, nullptr, &params);
106   MemberCtxObj member(&params);
107 
108   pub_key = this->kGrpXKey;
109   pub_key.h1.x.data.data[0]++;
110   EXPECT_EQ(kEpidBadArgErr,
111             ProvisionCompressedAndStart(member, &pub_key, &priv_key, &precomp));
112   EXPECT_EQ(kEpidBadArgErr,
113             ProvisionCompressedAndStart(member, &pub_key, &priv_key, nullptr));
114 
115   pub_key = this->kGrpXKey;
116   pub_key.h1.y.data.data[0]++;
117   EXPECT_EQ(kEpidBadArgErr,
118             ProvisionCompressedAndStart(member, &pub_key, &priv_key, &precomp));
119   EXPECT_EQ(kEpidBadArgErr,
120             ProvisionCompressedAndStart(member, &pub_key, &priv_key, nullptr));
121 
122   pub_key = this->kGrpXKey;
123   pub_key.w.x[0].data.data[0]++;
124   EXPECT_EQ(kEpidBadArgErr,
125             ProvisionCompressedAndStart(member, &pub_key, &priv_key, &precomp));
126   EXPECT_EQ(kEpidBadArgErr,
127             ProvisionCompressedAndStart(member, &pub_key, &priv_key, nullptr));
128 
129   pub_key = this->kGrpXKey;
130   pub_key.w.x[1].data.data[0]++;
131   EXPECT_EQ(kEpidBadArgErr,
132             ProvisionCompressedAndStart(member, &pub_key, &priv_key, &precomp));
133   EXPECT_EQ(kEpidBadArgErr,
134             ProvisionCompressedAndStart(member, &pub_key, &priv_key, nullptr));
135 
136   pub_key = this->kGrpXKey;
137   pub_key.w.y[0].data.data[0]++;
138   EXPECT_EQ(kEpidBadArgErr,
139             ProvisionCompressedAndStart(member, &pub_key, &priv_key, &precomp));
140   EXPECT_EQ(kEpidBadArgErr,
141             ProvisionCompressedAndStart(member, &pub_key, &priv_key, nullptr));
142 
143   pub_key = this->kGrpXKey;
144   pub_key.w.y[1].data.data[0]++;
145   EXPECT_EQ(kEpidBadArgErr,
146             ProvisionCompressedAndStart(member, &pub_key, &priv_key, &precomp));
147   EXPECT_EQ(kEpidBadArgErr,
148             ProvisionCompressedAndStart(member, &pub_key, &priv_key, nullptr));
149 }
150 
TEST_F(EpidMemberTest,ProvisionCompressedFailsForInvalidPrivateKey)151 TEST_F(EpidMemberTest, ProvisionCompressedFailsForInvalidPrivateKey) {
152   Prng prng;
153   GroupPubKey pub_key = this->kGrpXKey;
154   CompressedPrivKey priv_key = this->kGrpXMember9CompressedKey;
155   // Note: this MemberPrecomp is for the wrong group, however it should not
156   // be checked in Provision because doing so would negate the performance
157   // boost of using the precomp.
158   MemberPrecomp precomp = this->kMemberPrecomp;
159   MemberParams params = {0};
160   SetMemberParams(&Prng::Generate, &prng, nullptr, &params);
161   MemberCtxObj member(&params);
162 
163   priv_key = this->kGrpXMember9CompressedKey;
164   priv_key.ax.data.data[0]++;
165   EXPECT_EQ(kEpidBadArgErr,
166             ProvisionCompressedAndStart(member, &pub_key, &priv_key, &precomp));
167   EXPECT_EQ(kEpidBadArgErr,
168             ProvisionCompressedAndStart(member, &pub_key, &priv_key, nullptr));
169 
170   priv_key = this->kGrpXMember9CompressedKey;
171   priv_key.seed.data[0]++;
172   EXPECT_EQ(kEpidBadArgErr,
173             ProvisionCompressedAndStart(member, &pub_key, &priv_key, &precomp));
174   EXPECT_EQ(kEpidBadArgErr,
175             ProvisionCompressedAndStart(member, &pub_key, &priv_key, nullptr));
176 }
177 
178 }  // namespace
179