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 /// EpidPrivateExp unit tests.
17 /*! \file */
18 #include "epid/common-testhelper/epid_gtest-testhelper.h"
19 #include "gtest/gtest.h"
20 
21 extern "C" {
22 #include "epid/member/src/privateexp.h"
23 }
24 
25 #include "epid/common-testhelper/epid2params_wrapper-testhelper.h"
26 #include "epid/common-testhelper/epid_params-testhelper.h"
27 #include "epid/common-testhelper/errors-testhelper.h"
28 #include "epid/common-testhelper/prng-testhelper.h"
29 #include "epid/member/unittests/member-testhelper.h"
30 
31 namespace {
32 
33 ////////////////////////////////////////////////
34 //  EpidPrivateExp
TEST_F(EpidMemberTest,EpidPrivateExpFailsGivenNullPointer)35 TEST_F(EpidMemberTest, EpidPrivateExpFailsGivenNullPointer) {
36   Prng my_prng;
37   Epid2ParamsObj epid2params;
38   MemberCtxObj member(&Prng::Generate, &my_prng);
39   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
40   THROW_ON_EPIDERR(EpidProvisionKey(member, &this->kGroupPublicKey,
41                                     &this->kMemberPrivateKey,
42                                     &this->kMemberPrecomp));
43   THROW_ON_EPIDERR(EpidMemberStartup(member));
44 
45   Epid20Params params;
46   EcPointObj a(&params.G1, this->kGroupPublicKey.h1), r(&params.G1);
47 
48   EXPECT_EQ(kEpidBadArgErr, EpidPrivateExp(nullptr, a, r));
49   EXPECT_EQ(kEpidBadArgErr, EpidPrivateExp(member, nullptr, r));
50   EXPECT_EQ(kEpidBadArgErr, EpidPrivateExp(member, a, nullptr));
51 }
52 
TEST_F(EpidMemberTest,EpidPrivateExpFailsArgumentsMismatch)53 TEST_F(EpidMemberTest, EpidPrivateExpFailsArgumentsMismatch) {
54   Prng my_prng;
55   Epid2ParamsObj epid2params;
56   MemberCtxObj member(&Prng::Generate, &my_prng);
57   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
58   THROW_ON_EPIDERR(EpidProvisionKey(member, &this->kGroupPublicKey,
59                                     &this->kMemberPrivateKey,
60                                     &this->kMemberPrecomp));
61   THROW_ON_EPIDERR(EpidMemberStartup(member));
62 
63   Epid20Params params;
64   EcPointObj a(&params.G1, this->kGroupPublicKey.h1), r(&params.G1);
65   EcPointObj g2(&params.G2, this->kGroupPublicKey.w);
66 
67   EXPECT_EQ(kEpidBadArgErr, EpidPrivateExp(member, g2, r));
68   EXPECT_EQ(kEpidBadArgErr, EpidPrivateExp(member, a, g2));
69   EXPECT_EQ(kEpidBadArgErr, EpidPrivateExp(member, g2, g2));
70 }
71 
TEST_F(EpidMemberTest,EpidPrivateExpSucceedsGivenValidParametersForSha256)72 TEST_F(EpidMemberTest, EpidPrivateExpSucceedsGivenValidParametersForSha256) {
73   Prng my_prng;
74   Epid2ParamsObj epid2params;
75   MemberCtxObj member(&Prng::Generate, &my_prng);
76   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
77   THROW_ON_EPIDERR(EpidProvisionKey(member, &this->kGroupPublicKey,
78                                     &this->kMemberPrivateKey,
79                                     &this->kMemberPrecomp));
80   THROW_ON_EPIDERR(EpidMemberStartup(member));
81 
82   Epid20Params params;
83   EcPointObj a(&params.G1, this->kGroupPublicKey.h1), r(&params.G1),
84       r_expected(&params.G1);
85 
86   G1ElemStr r_str, r_expected_str;
87 
88   EXPECT_EQ(kEpidNoErr, EpidPrivateExp(member, a, r));
89 
90   THROW_ON_EPIDERR(EcExp(
91       params.G1, a, (BigNumStr const*)&this->kMemberPrivateKey.f, r_expected));
92 
93   THROW_ON_EPIDERR(WriteEcPoint(params.G1, r, &r_str, sizeof(r_str)));
94   THROW_ON_EPIDERR(WriteEcPoint(params.G1, r_expected, &r_expected_str,
95                                 sizeof(r_expected_str)));
96   EXPECT_EQ(r_expected_str, r_str);
97 }
98 
99 #ifndef TPM_TSS
TEST_F(EpidMemberTest,EpidPrivateExpSucceedsGivenValidParametersForSha384)100 TEST_F(EpidMemberTest, EpidPrivateExpSucceedsGivenValidParametersForSha384) {
101   Prng my_prng;
102   Epid2ParamsObj epid2params;
103   MemberCtxObj member(&Prng::Generate, &my_prng);
104   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha384));
105   THROW_ON_EPIDERR(EpidProvisionKey(member, &this->kGroupPublicKey,
106                                     &this->kMemberPrivateKey,
107                                     &this->kMemberPrecomp));
108   THROW_ON_EPIDERR(EpidMemberStartup(member));
109 
110   Epid20Params params;
111   EcPointObj a(&params.G1, this->kGroupPublicKey.h1), r(&params.G1),
112       r_expected(&params.G1);
113 
114   G1ElemStr r_str, r_expected_str;
115 
116   EXPECT_EQ(kEpidNoErr, EpidPrivateExp(member, a, r));
117 
118   THROW_ON_EPIDERR(EcExp(
119       params.G1, a, (BigNumStr const*)&this->kMemberPrivateKey.f, r_expected));
120 
121   THROW_ON_EPIDERR(WriteEcPoint(params.G1, r, &r_str, sizeof(r_str)));
122   THROW_ON_EPIDERR(WriteEcPoint(params.G1, r_expected, &r_expected_str,
123                                 sizeof(r_expected_str)));
124   EXPECT_EQ(r_expected_str, r_str);
125 }
126 #endif
127 
TEST_F(EpidMemberTest,PROTECTED_EpidPrivateExpSucceedsByCredentialForSha256_EPS0)128 TEST_F(EpidMemberTest,
129        PROTECTED_EpidPrivateExpSucceedsByCredentialForSha256_EPS0) {
130   Prng my_prng;
131   Epid2ParamsObj epid2params;
132   MemberCtxObj member(&Prng::Generate, &my_prng);
133   BigNumStr const f_str = {0x7a, 0x57, 0x41, 0x5b, 0x85, 0x44, 0x0e, 0x2b,
134                            0xb3, 0xcc, 0xa7, 0x99, 0x6d, 0x19, 0x79, 0x45,
135                            0x04, 0xb8, 0x94, 0x07, 0x47, 0x14, 0xed, 0x8d,
136                            0xf4, 0x1e, 0x7d, 0xa0, 0x17, 0xc5, 0xc4, 0x10};
137   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
138 
139   Epid20Params params;
140   EcPointObj a(&params.G1, this->kGroupPublicKey.h1), r(&params.G1),
141       r_expected(&params.G1);
142 
143   G1ElemStr r_str, r_expected_str;
144 
145   EXPECT_EQ(kEpidNoErr, EpidPrivateExp(member, a, r));
146 
147   THROW_ON_EPIDERR(EcExp(params.G1, a, (BigNumStr const*)&f_str, r_expected));
148 
149   THROW_ON_EPIDERR(WriteEcPoint(params.G1, r, &r_str, sizeof(r_str)));
150   THROW_ON_EPIDERR(WriteEcPoint(params.G1, r_expected, &r_expected_str,
151                                 sizeof(r_expected_str)));
152   EXPECT_EQ(r_expected_str, r_str);
153 }
154 
155 }  // namespace
156