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 /// Tiny NrProve unit tests.
17 /*! \file */
18 
19 #ifndef SHARED
20 #include "epid/common-testhelper/epid_gtest-testhelper.h"
21 #include "epid/member/tiny/unittests/member-testhelper.h"
22 #include "gtest/gtest.h"
23 
24 extern "C" {
25 #include "epid/member/tiny/src/native_types.h"
26 #include "epid/member/tiny/src/nrprove.h"
27 #include "epid/member/tiny/src/serialize.h"
28 #include "epid/member/tiny/src/signbasic.h"
29 }
30 
31 #include "epid/common-testhelper/errors-testhelper.h"
32 #include "epid/common-testhelper/prng-testhelper.h"
33 #include "epid/common-testhelper/verifier_wrapper-testhelper.h"
34 namespace {
35 
TEST_F(EpidMemberTest,NrProveFailsGivenInvalidSigRlEntry)36 TEST_F(EpidMemberTest, NrProveFailsGivenInvalidSigRlEntry) {
37   Prng my_prng;
38   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
39                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
40 
41   auto msg = this->kTest1Msg;
42   SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
43   NrProof proof;
44   // make sure that can generate NrProof using incorrupt sig_rl_enty
45   THROW_ON_EPIDERR(EpidNrProve(member, msg.data(), msg.size(), &this->kBasicSig,
46                                &sig_rl->bk[0], &proof));
47   SigRlEntry sig_rl_enty_invalid_k = sig_rl->bk[0];
48   sig_rl_enty_invalid_k.k.x.data.data[31]++;  // make it not in EC group
49   EXPECT_EQ(kEpidBadArgErr,
50             EpidNrProve(member, msg.data(), msg.size(), &this->kBasicSig,
51                         &sig_rl_enty_invalid_k, &proof));
52 
53   SigRlEntry sig_rl_enty_invalid_b = sig_rl->bk[0];
54   sig_rl_enty_invalid_b.b.x.data.data[31]++;  // make it not in EC group
55   EXPECT_EQ(kEpidBadArgErr,
56             EpidNrProve(member, msg.data(), msg.size(), &this->kBasicSig,
57                         &sig_rl_enty_invalid_b, &proof));
58 }
59 
TEST_F(EpidMemberTest,NrProveFailsGivenInvalidBasicSig)60 TEST_F(EpidMemberTest, NrProveFailsGivenInvalidBasicSig) {
61   Prng my_prng;
62   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
63                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
64 
65   auto msg = this->kTest1Msg;
66   SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
67 
68   NrProof proof;
69   // make sure that can generate NrProof using incorrupt basic sig
70   THROW_ON_EPIDERR(EpidNrProve(member, msg.data(), msg.size(), &this->kBasicSig,
71                                &sig_rl->bk[0], &proof));
72   // invalid basic sig is only when K value is invalid!!
73   NativeBasicSignature basic_sig_invalid_K = this->kBasicSig;
74   basic_sig_invalid_K.K.x.limbs.word[0]++;  // make it not in EC group
75   EXPECT_EQ(kEpidBadArgErr,
76             EpidNrProve(member, msg.data(), msg.size(), &basic_sig_invalid_K,
77                         &sig_rl->bk[0], &proof));
78 }
79 
TEST_F(EpidMemberTest,GeneratesNrProofForEmptyMessage)80 TEST_F(EpidMemberTest, GeneratesNrProofForEmptyMessage) {
81   Prng my_prng;
82   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
83                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
84 
85   BasicSignature basic_sig;
86   SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
87 
88   NrProof proof;
89 
90   EXPECT_EQ(kEpidNoErr, EpidNrProve(member, nullptr, 0, &this->kBasicSig,
91                                     &sig_rl->bk[0], &proof));
92 
93   BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
94   // Check proof by doing an NrVerify
95   VerifierCtxObj ctx(this->kGroupPublicKey);
96   EXPECT_EQ(kEpidNoErr,
97             EpidNrVerify(ctx, &basic_sig, nullptr, 0, &sig_rl->bk[0], &proof));
98 }
99 
TEST_F(EpidMemberTest,GeneratesNrProofForMsgContainingAllPossibleBytes)100 TEST_F(EpidMemberTest, GeneratesNrProofForMsgContainingAllPossibleBytes) {
101   Prng my_prng;
102   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
103                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
104 
105   BasicSignature basic_sig;
106   auto msg = this->kData_0_255;
107   SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
108 
109   NrProof proof;
110   EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(),
111                                     &this->kBasicSig, &sig_rl->bk[0], &proof));
112 
113   // Check proof by doing an NrVerify
114   BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
115   VerifierCtxObj ctx(this->kGroupPublicKey);
116   EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
117                                      &sig_rl->bk[0], &proof));
118 }
119 
TEST_F(EpidMemberTest,GeneratesNrProof)120 TEST_F(EpidMemberTest, GeneratesNrProof) {
121   Prng my_prng;
122   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
123                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
124 
125   BasicSignature basic_sig;
126   auto msg = this->kTest1Msg;
127   SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
128 
129   NrProof proof;
130   EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(),
131                                     &this->kBasicSig, &sig_rl->bk[0], &proof));
132 
133   // Check proof by doing an NrVerify
134   BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
135   VerifierCtxObj ctx(this->kGroupPublicKey);
136   EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
137                                      &sig_rl->bk[0], &proof));
138 }
139 
TEST_F(EpidMemberTest,GeneratesNrProofUsingSha512)140 TEST_F(EpidMemberTest, GeneratesNrProofUsingSha512) {
141   Prng my_prng;
142   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
143                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
144 
145   BasicSignature basic_sig;
146   auto msg = this->kTest1Msg;
147   SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
148   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha512));
149   NrProof proof;
150   EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(),
151                                     &this->kBasicSig, &sig_rl->bk[0], &proof));
152 
153   // Check proof by doing an NrVerify
154   BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
155   VerifierCtxObj ctx(this->kGroupPublicKey);
156   EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
157                                      &sig_rl->bk[0], &proof));
158 }
159 
TEST_F(EpidMemberTest,GeneratesNrProofUsingSha256)160 TEST_F(EpidMemberTest, GeneratesNrProofUsingSha256) {
161   Prng my_prng;
162   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
163                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
164 
165   BasicSignature basic_sig;
166   auto msg = this->kTest1Msg;
167   SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
168   THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
169   NrProof proof;
170   EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(),
171                                     &this->kBasicSig, &sig_rl->bk[0], &proof));
172 
173   // Check proof by doing an NrVerify
174   BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
175   VerifierCtxObj ctx(this->kGroupPublicKey);
176   THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
177   EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
178                                      &sig_rl->bk[0], &proof));
179 }
180 }  // namespace
181 #endif  // SHARED
182