aboutsummaryrefslogtreecommitdiff
path: root/epid/member/tiny/unittests/internal/nr_prove-test.cc
blob: e95cf2454d457b0e76d344dd1c928f23ae6540b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*############################################################################
  # Copyright 2017 Intel Corporation
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  ############################################################################*/
/// Tiny NrProve unit tests.
/*! \file */

#ifndef SHARED
#include "epid/common-testhelper/epid_gtest-testhelper.h"
#include "epid/member/tiny/unittests/member-testhelper.h"
#include "gtest/gtest.h"

extern "C" {
#include "epid/member/tiny/src/native_types.h"
#include "epid/member/tiny/src/nrprove.h"
#include "epid/member/tiny/src/serialize.h"
#include "epid/member/tiny/src/signbasic.h"
}

#include "epid/common-testhelper/errors-testhelper.h"
#include "epid/common-testhelper/prng-testhelper.h"
#include "epid/common-testhelper/verifier_wrapper-testhelper.h"
namespace {

TEST_F(EpidMemberTest, NrProveFailsGivenInvalidSigRlEntry) {
  Prng my_prng;
  MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
                      this->kMemberPrecomp, &Prng::Generate, &my_prng);

  auto msg = this->kTest1Msg;
  SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  NrProof proof;
  // make sure that can generate NrProof using incorrupt sig_rl_enty
  THROW_ON_EPIDERR(EpidNrProve(member, msg.data(), msg.size(), &this->kBasicSig,
                               &sig_rl->bk[0], &proof));
  SigRlEntry sig_rl_enty_invalid_k = sig_rl->bk[0];
  sig_rl_enty_invalid_k.k.x.data.data[31]++;  // make it not in EC group
  EXPECT_EQ(kEpidBadArgErr,
            EpidNrProve(member, msg.data(), msg.size(), &this->kBasicSig,
                        &sig_rl_enty_invalid_k, &proof));

  SigRlEntry sig_rl_enty_invalid_b = sig_rl->bk[0];
  sig_rl_enty_invalid_b.b.x.data.data[31]++;  // make it not in EC group
  EXPECT_EQ(kEpidBadArgErr,
            EpidNrProve(member, msg.data(), msg.size(), &this->kBasicSig,
                        &sig_rl_enty_invalid_b, &proof));
}

TEST_F(EpidMemberTest, NrProveFailsGivenInvalidBasicSig) {
  Prng my_prng;
  MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
                      this->kMemberPrecomp, &Prng::Generate, &my_prng);

  auto msg = this->kTest1Msg;
  SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());

  NrProof proof;
  // make sure that can generate NrProof using incorrupt basic sig
  THROW_ON_EPIDERR(EpidNrProve(member, msg.data(), msg.size(), &this->kBasicSig,
                               &sig_rl->bk[0], &proof));
  // invalid basic sig is only when K value is invalid!!
  NativeBasicSignature basic_sig_invalid_K = this->kBasicSig;
  basic_sig_invalid_K.K.x.limbs.word[0]++;  // make it not in EC group
  EXPECT_EQ(kEpidBadArgErr,
            EpidNrProve(member, msg.data(), msg.size(), &basic_sig_invalid_K,
                        &sig_rl->bk[0], &proof));
}

TEST_F(EpidMemberTest, GeneratesNrProofForEmptyMessage) {
  Prng my_prng;
  MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
                      this->kMemberPrecomp, &Prng::Generate, &my_prng);

  BasicSignature basic_sig;
  SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());

  NrProof proof;

  EXPECT_EQ(kEpidNoErr, EpidNrProve(member, nullptr, 0, &this->kBasicSig,
                                    &sig_rl->bk[0], &proof));

  BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
  // Check proof by doing an NrVerify
  VerifierCtxObj ctx(this->kGroupPublicKey);
  EXPECT_EQ(kEpidNoErr,
            EpidNrVerify(ctx, &basic_sig, nullptr, 0, &sig_rl->bk[0], &proof));
}

TEST_F(EpidMemberTest, GeneratesNrProofForMsgContainingAllPossibleBytes) {
  Prng my_prng;
  MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
                      this->kMemberPrecomp, &Prng::Generate, &my_prng);

  BasicSignature basic_sig;
  auto msg = this->kData_0_255;
  SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());

  NrProof proof;
  EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(),
                                    &this->kBasicSig, &sig_rl->bk[0], &proof));

  // Check proof by doing an NrVerify
  BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
  VerifierCtxObj ctx(this->kGroupPublicKey);
  EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
                                     &sig_rl->bk[0], &proof));
}

TEST_F(EpidMemberTest, GeneratesNrProof) {
  Prng my_prng;
  MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
                      this->kMemberPrecomp, &Prng::Generate, &my_prng);

  BasicSignature basic_sig;
  auto msg = this->kTest1Msg;
  SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());

  NrProof proof;
  EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(),
                                    &this->kBasicSig, &sig_rl->bk[0], &proof));

  // Check proof by doing an NrVerify
  BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
  VerifierCtxObj ctx(this->kGroupPublicKey);
  EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
                                     &sig_rl->bk[0], &proof));
}

TEST_F(EpidMemberTest, GeneratesNrProofUsingSha512) {
  Prng my_prng;
  MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
                      this->kMemberPrecomp, &Prng::Generate, &my_prng);

  BasicSignature basic_sig;
  auto msg = this->kTest1Msg;
  SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha512));
  NrProof proof;
  EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(),
                                    &this->kBasicSig, &sig_rl->bk[0], &proof));

  // Check proof by doing an NrVerify
  BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
  VerifierCtxObj ctx(this->kGroupPublicKey);
  EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
                                     &sig_rl->bk[0], &proof));
}

TEST_F(EpidMemberTest, GeneratesNrProofUsingSha256) {
  Prng my_prng;
  MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
                      this->kMemberPrecomp, &Prng::Generate, &my_prng);

  BasicSignature basic_sig;
  auto msg = this->kTest1Msg;
  SigRl const* sig_rl = reinterpret_cast<const SigRl*>(this->kSigRlData.data());
  THROW_ON_EPIDERR(EpidMemberSetHashAlg(member, kSha256));
  NrProof proof;
  EXPECT_EQ(kEpidNoErr, EpidNrProve(member, msg.data(), msg.size(),
                                    &this->kBasicSig, &sig_rl->bk[0], &proof));

  // Check proof by doing an NrVerify
  BasicSignatureSerialize(&basic_sig, &this->kBasicSig);
  VerifierCtxObj ctx(this->kGroupPublicKey);
  THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
  EXPECT_EQ(kEpidNoErr, EpidNrVerify(ctx, &basic_sig, msg.data(), msg.size(),
                                     &sig_rl->bk[0], &proof));
}
}  // namespace
#endif  // SHARED