signbasic-test.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. /// SignBasic unit tests.
  17. /*! \file */
  18. #include <cstring>
  19. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  20. #include "gtest/gtest.h"
  21. extern "C" {
  22. #include "epid/member/api.h"
  23. #include "epid/member/src/signbasic.h"
  24. #include "epid/verifier/api.h"
  25. }
  26. #include "epid/common-testhelper/errors-testhelper.h"
  27. #include "epid/common-testhelper/prng-testhelper.h"
  28. #include "epid/common-testhelper/verifier_wrapper-testhelper.h"
  29. #include "epid/member/unittests/member-testhelper.h"
  30. bool operator==(BigNumStr const& lhs, BigNumStr const& rhs) {
  31. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  32. }
  33. namespace {
  34. /// Count of elements in array
  35. #define COUNT_OF(A) (sizeof(A) / sizeof((A)[0]))
  36. /////////////////////////////////////////////////////////////////////////
  37. // Simple error cases
  38. TEST_F(EpidMemberTest, SignBasicFailsGivenNullParameters) {
  39. Prng my_prng;
  40. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  41. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  42. auto& msg = this->kMsg0;
  43. auto& bsn = this->kBsn0;
  44. BasicSignature basic_sig;
  45. EXPECT_EQ(kEpidBadArgErr,
  46. EpidSignBasic(nullptr, msg.data(), msg.size(), bsn.data(),
  47. bsn.size(), &basic_sig, nullptr));
  48. EXPECT_EQ(kEpidBadArgErr,
  49. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  50. bsn.size(), nullptr, nullptr));
  51. EXPECT_EQ(kEpidBadArgErr,
  52. EpidSignBasic(member, nullptr, msg.size(), bsn.data(), bsn.size(),
  53. &basic_sig, nullptr));
  54. EXPECT_EQ(kEpidBadArgErr,
  55. EpidSignBasic(member, msg.data(), msg.size(), nullptr, bsn.size(),
  56. &basic_sig, nullptr));
  57. }
  58. TEST_F(EpidMemberTest, SignBasicFailsGivenNullBasenameAndNullRandomBasename) {
  59. Prng my_prng;
  60. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  61. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  62. auto& msg = this->kMsg0;
  63. BasicSignature basic_sig;
  64. EXPECT_EQ(kEpidBadArgErr, EpidSignBasic(member, msg.data(), msg.size(),
  65. nullptr, 0, &basic_sig, nullptr));
  66. }
  67. TEST_F(EpidMemberTest, SignBasicDoesNotComputeRandomBasenameGivenBasename) {
  68. Prng my_prng;
  69. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  70. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  71. auto& msg = this->kMsg0;
  72. auto& bsn = this->kBsn0;
  73. BigNumStr rnd_bsn = {0};
  74. BigNumStr zero = {0};
  75. BasicSignature basic_sig;
  76. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  77. EXPECT_EQ(kEpidNoErr,
  78. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  79. bsn.size(), &basic_sig, &rnd_bsn));
  80. EXPECT_EQ(zero, rnd_bsn);
  81. // verify basic signature
  82. VerifierCtxObj ctx(this->kGroupPublicKey);
  83. THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
  84. EXPECT_EQ(kEpidSigValid,
  85. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  86. }
  87. TEST_F(EpidMemberTest, SignBasicFailsForBasenameWithoutRegisteredBasenames) {
  88. Prng my_prng;
  89. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  90. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  91. auto& msg = this->kMsg0;
  92. auto& bsn = this->kBsn0;
  93. BasicSignature basic_sig;
  94. EXPECT_EQ(kEpidBadArgErr,
  95. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  96. bsn.size(), &basic_sig, nullptr));
  97. }
  98. TEST_F(EpidMemberTest, SignBasicFailsForUnregisteredBasename) {
  99. Prng my_prng;
  100. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  101. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  102. auto& msg = this->kMsg0;
  103. auto& bsn0 = this->kBsn0;
  104. auto& bsn1 = this->kBsn1;
  105. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn0.data(), bsn0.size()));
  106. BasicSignature basic_sig;
  107. EXPECT_EQ(kEpidBadArgErr,
  108. EpidSignBasic(member, msg.data(), msg.size(), bsn1.data(),
  109. bsn1.size(), &basic_sig, nullptr));
  110. }
  111. /////////////////////////////////////////////////////////////////////////
  112. // Anonymity
  113. TEST_F(EpidMemberTest, BasicSignaturesOfSameMessageAreDifferent) {
  114. Prng my_prng;
  115. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  116. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  117. auto& msg = this->kMsg0;
  118. BasicSignature basic_sig1 = {0};
  119. BasicSignature basic_sig2 = {0};
  120. BigNumStr rnd_bsn = {0};
  121. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  122. 0, &basic_sig1, &rnd_bsn));
  123. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  124. 0, &basic_sig2, &rnd_bsn));
  125. EXPECT_NE(0, memcmp(&basic_sig1, &basic_sig2, sizeof(BasicSignature)));
  126. }
  127. TEST_F(EpidMemberTest,
  128. BasicSignaturesOfSameMessageWithSameBasenameAreDifferent) {
  129. Prng my_prng;
  130. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  131. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  132. auto& msg = this->kMsg0;
  133. auto& bsn = this->kBsn0;
  134. BasicSignature basic_sig1;
  135. BasicSignature basic_sig2;
  136. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  137. EXPECT_EQ(kEpidNoErr,
  138. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  139. bsn.size(), &basic_sig1, nullptr));
  140. EXPECT_EQ(kEpidNoErr,
  141. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  142. bsn.size(), &basic_sig2, nullptr));
  143. EXPECT_NE(0, memcmp(&basic_sig1, &basic_sig2, sizeof(BasicSignature)));
  144. }
  145. /////////////////////////////////////////////////////////////////////////
  146. // Variable basename
  147. TEST_F(EpidMemberTest, SignBasicSucceedsUsingRandomBase) {
  148. Prng my_prng;
  149. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  150. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  151. auto& msg = this->kMsg0;
  152. BasicSignature basic_sig;
  153. BigNumStr rnd_bsn = {0};
  154. BigNumStr zero = {0};
  155. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  156. 0, &basic_sig, &rnd_bsn));
  157. EXPECT_NE(0, memcmp(&rnd_bsn, &zero, sizeof(BigNumStr)));
  158. // verify basic signature
  159. VerifierCtxObj ctx(this->kGroupPublicKey);
  160. EXPECT_EQ(kEpidSigValid,
  161. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  162. }
  163. TEST_F(EpidMemberTest,
  164. PROTECTED_SignBasicSucceedsUsingRandomBaseWithCredential_EPS0) {
  165. Prng my_prng;
  166. MemberCtxObj member(
  167. this->kEps0GroupPublicKey,
  168. *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
  169. &Prng::Generate, &my_prng);
  170. auto& msg = this->kMsg0;
  171. BasicSignature basic_sig;
  172. BigNumStr rnd_bsn = {0};
  173. BigNumStr zero = {0};
  174. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  175. 0, &basic_sig, &rnd_bsn));
  176. EXPECT_NE(0, memcmp(&rnd_bsn, &zero, sizeof(BigNumStr)));
  177. // verify basic signature
  178. VerifierCtxObj ctx(this->kEps0GroupPublicKey);
  179. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
  180. EXPECT_EQ(kEpidSigValid,
  181. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  182. }
  183. TEST_F(EpidMemberTest, SignBasicSucceedsUsingBasename) {
  184. Prng my_prng;
  185. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  186. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  187. auto& msg = this->kMsg0;
  188. auto& bsn = this->kBsn0;
  189. BasicSignature basic_sig;
  190. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  191. EXPECT_EQ(kEpidNoErr,
  192. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  193. bsn.size(), &basic_sig, nullptr));
  194. // verify basic signature
  195. VerifierCtxObj ctx(this->kGroupPublicKey);
  196. THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
  197. EXPECT_EQ(kEpidSigValid,
  198. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  199. }
  200. TEST_F(EpidMemberTest,
  201. PROTECTED_SignBasicSucceedsUsingBasenameWithCredential_EPS0) {
  202. Prng my_prng;
  203. MemberCtxObj member(
  204. this->kEps0GroupPublicKey,
  205. *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
  206. &Prng::Generate, &my_prng);
  207. auto& msg = this->kMsg0;
  208. auto& bsn = this->kBsn0;
  209. BasicSignature basic_sig;
  210. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  211. EXPECT_EQ(kEpidNoErr,
  212. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  213. bsn.size(), &basic_sig, nullptr));
  214. // verify basic signature
  215. VerifierCtxObj ctx(this->kEps0GroupPublicKey);
  216. THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
  217. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
  218. EXPECT_EQ(kEpidSigValid,
  219. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  220. }
  221. TEST_F(EpidMemberTest, SignBasicSucceedsUsingBasenameUsingIKGFData) {
  222. Prng my_prng;
  223. GroupPubKey grp_public_key = *reinterpret_cast<const GroupPubKey*>(
  224. this->kGroupPublicKeyDataIkgf.data());
  225. PrivKey mbr_private_key =
  226. *reinterpret_cast<const PrivKey*>(this->kMemberPrivateKeyDataIkgf.data());
  227. MemberCtxObj member(grp_public_key, mbr_private_key, &Prng::Generate,
  228. &my_prng);
  229. auto& msg = this->kMsg0;
  230. auto& bsn = this->kBsn0;
  231. BasicSignature basic_sig;
  232. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  233. EXPECT_EQ(kEpidNoErr,
  234. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  235. bsn.size(), &basic_sig, nullptr));
  236. // verify basic signature
  237. VerifierCtxObj ctx(grp_public_key);
  238. THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
  239. EXPECT_EQ(kEpidSigValid,
  240. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  241. }
  242. TEST_F(EpidMemberTest,
  243. SignBasicSucceedsUsingRandomBaseWithRegisteredBasenames) {
  244. Prng my_prng;
  245. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  246. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  247. auto& msg = this->kMsg0;
  248. auto& bsn = this->kBsn0;
  249. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  250. BasicSignature basic_sig;
  251. BigNumStr rnd_bsn = {0};
  252. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  253. 0, &basic_sig, &rnd_bsn));
  254. // verify basic signature
  255. VerifierCtxObj ctx(this->kGroupPublicKey);
  256. EXPECT_EQ(kEpidSigValid,
  257. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  258. }
  259. TEST_F(EpidMemberTest,
  260. SignBasicSucceedsUsingRandomBaseWithoutRegisteredBasenames) {
  261. Prng my_prng;
  262. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  263. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  264. auto& msg = this->kMsg0;
  265. BasicSignature basic_sig;
  266. BigNumStr rnd_bsn = {0};
  267. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  268. 0, &basic_sig, &rnd_bsn));
  269. // verify basic signature
  270. VerifierCtxObj ctx(this->kGroupPublicKey);
  271. EXPECT_EQ(kEpidSigValid,
  272. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  273. }
  274. #ifndef TPM_TSS
  275. TEST_F(EpidMemberTest, SignBasicSucceedsUsingBsnContainingAllPossibleBytes) {
  276. Prng my_prng;
  277. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  278. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  279. auto& msg = this->kMsg0;
  280. auto& bsn = this->kData_0_255;
  281. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  282. BasicSignature basic_sig;
  283. EXPECT_EQ(kEpidNoErr,
  284. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  285. bsn.size(), &basic_sig, nullptr));
  286. VerifierCtxObj ctx(this->kGroupPublicKey);
  287. THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
  288. EXPECT_EQ(kEpidSigValid,
  289. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  290. }
  291. #endif
  292. TEST_F(EpidMemberTest,
  293. PROTECTED_SignBasicSucceedsAllPossibleBytesForCredential_EPS0) {
  294. Prng my_prng;
  295. MemberCtxObj member(
  296. this->kEps0GroupPublicKey,
  297. *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
  298. &Prng::Generate, &my_prng);
  299. auto& msg = this->kMsg0;
  300. auto& bsn = this->kData_0_255;
  301. // 0 - 123
  302. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), 124));
  303. BasicSignature basic_sig;
  304. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(),
  305. bsn.data(), 124, &basic_sig, nullptr));
  306. VerifierCtxObj ctx1(this->kEps0GroupPublicKey);
  307. THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx1, bsn.data(), 124));
  308. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx1, kSha256));
  309. EXPECT_EQ(kEpidSigValid,
  310. EpidVerifyBasicSig(ctx1, &basic_sig, msg.data(), msg.size()));
  311. // 124 - 247
  312. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data() + 124, 124));
  313. EXPECT_EQ(kEpidNoErr,
  314. EpidSignBasic(member, msg.data(), msg.size(), bsn.data() + 124, 124,
  315. &basic_sig, nullptr));
  316. VerifierCtxObj ctx2(this->kEps0GroupPublicKey);
  317. THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx2, bsn.data() + 124, 124));
  318. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx2, kSha256));
  319. EXPECT_EQ(kEpidSigValid,
  320. EpidVerifyBasicSig(ctx2, &basic_sig, msg.data(), msg.size()));
  321. // 248 - 255
  322. THROW_ON_EPIDERR(
  323. EpidRegisterBasename(member, bsn.data() + 124 * 2, 256 - 124 * 2));
  324. EXPECT_EQ(kEpidNoErr,
  325. EpidSignBasic(member, msg.data(), msg.size(), bsn.data() + 124 * 2,
  326. 256 - 124 * 2, &basic_sig, nullptr));
  327. VerifierCtxObj ctx3(this->kEps0GroupPublicKey);
  328. THROW_ON_EPIDERR(
  329. EpidVerifierSetBasename(ctx3, bsn.data() + 124 * 2, 256 - 124 * 2));
  330. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx3, kSha256));
  331. EXPECT_EQ(kEpidSigValid,
  332. EpidVerifyBasicSig(ctx3, &basic_sig, msg.data(), msg.size()));
  333. }
  334. /////////////////////////////////////////////////////////////////////////
  335. // Variable hash alg
  336. TEST_F(EpidMemberTest, SignBasicSucceedsUsingSha256HashAlg) {
  337. Prng my_prng;
  338. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey, kSha256,
  339. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  340. auto& msg = this->kMsg0;
  341. auto& bsn = this->kBsn0;
  342. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  343. BasicSignature basic_sig;
  344. EXPECT_EQ(kEpidNoErr,
  345. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  346. bsn.size(), &basic_sig, nullptr));
  347. // verify basic signature
  348. VerifierCtxObj ctx(this->kGroupPublicKey);
  349. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
  350. EXPECT_EQ(kEpidSigValid,
  351. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  352. }
  353. TEST_F(EpidMemberTest,
  354. PROTECTED_SignBasicSucceedsUsingSha256HashAlgWithCredential_EPS0) {
  355. Prng my_prng;
  356. MemberCtxObj member(
  357. this->kEps0GroupPublicKey,
  358. *(MembershipCredential const*)&this->kEps0MemberPrivateKey, kSha256,
  359. &Prng::Generate, &my_prng);
  360. auto& msg = this->kMsg0;
  361. auto& bsn = this->kBsn0;
  362. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  363. BasicSignature basic_sig;
  364. EXPECT_EQ(kEpidNoErr,
  365. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  366. bsn.size(), &basic_sig, nullptr));
  367. // verify basic signature
  368. VerifierCtxObj ctx(this->kEps0GroupPublicKey);
  369. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
  370. EXPECT_EQ(kEpidSigValid,
  371. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  372. }
  373. TEST_F(EpidMemberTest, SignBasicSucceedsUsingSha384HashAlg) {
  374. Prng my_prng;
  375. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey, kSha384,
  376. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  377. auto& msg = this->kMsg0;
  378. auto& bsn = this->kBsn0;
  379. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  380. BasicSignature basic_sig;
  381. EXPECT_EQ(kEpidNoErr,
  382. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  383. bsn.size(), &basic_sig, nullptr));
  384. // verify basic signature
  385. VerifierCtxObj ctx(this->kGroupPublicKey);
  386. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha384));
  387. EXPECT_EQ(kEpidSigValid,
  388. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  389. }
  390. TEST_F(EpidMemberTest, SignBasicSucceedsUsingSha512HashAlg) {
  391. Prng my_prng;
  392. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey, kSha512,
  393. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  394. auto& msg = this->kMsg0;
  395. auto& bsn = this->kBsn0;
  396. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  397. BasicSignature basic_sig;
  398. EXPECT_EQ(kEpidNoErr,
  399. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  400. bsn.size(), &basic_sig, nullptr));
  401. // verify basic signature
  402. VerifierCtxObj ctx(this->kGroupPublicKey);
  403. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha512));
  404. EXPECT_EQ(kEpidSigValid,
  405. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  406. }
  407. TEST_F(EpidMemberTest, SignBasicSucceedsUsingSha512256HashAlg) {
  408. Prng my_prng;
  409. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  410. kSha512_256, this->kMemberPrecomp, &Prng::Generate,
  411. &my_prng);
  412. auto& msg = this->kMsg0;
  413. auto& bsn = this->kBsn0;
  414. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  415. BasicSignature basic_sig;
  416. EXPECT_EQ(kEpidNoErr,
  417. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  418. bsn.size(), &basic_sig, nullptr));
  419. // verify basic signature
  420. VerifierCtxObj ctx(this->kGroupPublicKey);
  421. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha512_256));
  422. EXPECT_EQ(kEpidSigValid,
  423. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  424. }
  425. /////////////////////////////////////////////////////////////////////////
  426. TEST_F(EpidMemberTest, SignBasicConsumesPrecomputedSignatures) {
  427. Prng my_prng;
  428. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  429. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  430. THROW_ON_EPIDERR(EpidAddPreSigs(member, 3));
  431. auto& msg = this->kMsg0;
  432. BasicSignature basic_sig;
  433. auto& bsn = this->kBsn0;
  434. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  435. // use 1 precomputed signature
  436. ASSERT_EQ(kEpidNoErr,
  437. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  438. bsn.size(), &basic_sig, nullptr));
  439. EXPECT_EQ((size_t)2, EpidGetNumPreSigs(member));
  440. }
  441. TEST_F(EpidMemberTest, SignBasicSucceedsWithPrecomputedSignatures) {
  442. Prng my_prng;
  443. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  444. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  445. auto& msg = this->kMsg0;
  446. BasicSignature basic_sig;
  447. BigNumStr rnd_bsn = {0};
  448. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  449. 0, &basic_sig, &rnd_bsn));
  450. // verify basic signature
  451. VerifierCtxObj ctx(this->kGroupPublicKey);
  452. EXPECT_EQ(kEpidSigValid,
  453. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  454. }
  455. TEST_F(EpidMemberTest,
  456. PROTECTED_SignBasicSucceedsUsingPrecompSigWithCredential_EPS0) {
  457. Prng my_prng;
  458. MemberCtxObj member(
  459. this->kEps0GroupPublicKey,
  460. *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
  461. &Prng::Generate, &my_prng);
  462. THROW_ON_EPIDERR(EpidAddPreSigs(member, 1));
  463. auto& msg = this->kMsg0;
  464. BasicSignature basic_sig;
  465. BigNumStr rnd_bsn = {0};
  466. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  467. 0, &basic_sig, &rnd_bsn));
  468. VerifierCtxObj ctx(this->kEps0GroupPublicKey);
  469. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
  470. EXPECT_EQ(kEpidSigValid,
  471. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  472. }
  473. TEST_F(EpidMemberTest, SignBasicSucceedsWithoutPrecomputedSignatures) {
  474. Prng my_prng;
  475. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  476. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  477. THROW_ON_EPIDERR(EpidAddPreSigs(member, 1));
  478. auto& msg = this->kMsg0;
  479. BasicSignature basic_sig;
  480. auto& bsn = this->kBsn0;
  481. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  482. ASSERT_EQ(kEpidNoErr,
  483. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  484. bsn.size(), &basic_sig, nullptr));
  485. // test sign without precomputed signatures
  486. EXPECT_EQ(kEpidNoErr,
  487. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  488. bsn.size(), &basic_sig, nullptr));
  489. // verify basic signature
  490. VerifierCtxObj ctx(this->kGroupPublicKey);
  491. THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
  492. EXPECT_EQ(kEpidSigValid,
  493. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  494. }
  495. /////////////////////////////////////////////////////////////////////////
  496. // Variable messages
  497. TEST_F(EpidMemberTest, SignBasicSucceedsGivenEmptyMessage) {
  498. Prng my_prng;
  499. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  500. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  501. auto& msg = this->kMsg0;
  502. auto& bsn = this->kBsn0;
  503. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  504. BasicSignature basic_sig;
  505. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), 0, bsn.data(),
  506. bsn.size(), &basic_sig, nullptr));
  507. VerifierCtxObj ctx(this->kGroupPublicKey);
  508. THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
  509. EXPECT_EQ(kEpidSigValid, EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), 0));
  510. }
  511. TEST_F(EpidMemberTest, SignBasicSucceedsWithShortMessage) {
  512. // check: 1, 13, 128, 256, 512, 1021, 1024 bytes
  513. // 13 and 1021 are primes
  514. Prng my_prng;
  515. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  516. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  517. BasicSignature basic_sig;
  518. BigNumStr rnd_bsn = {0};
  519. VerifierCtxObj ctx(this->kGroupPublicKey);
  520. size_t lengths[] = {1, 13, 128, 256,
  521. 512, 1021, 1024}; // have desired lengths to loop over
  522. std::vector<uint8_t> msg(
  523. lengths[COUNT_OF(lengths) - 1]); // allocate message for max size
  524. for (size_t n = 0; n < msg.size(); n++) {
  525. msg.at(n) = (uint8_t)n;
  526. }
  527. for (auto length : lengths) {
  528. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), length, nullptr, 0,
  529. &basic_sig, &rnd_bsn))
  530. << "EpidSignBasic for message_len: " << length << " failed";
  531. EXPECT_EQ(kEpidNoErr,
  532. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), length))
  533. << "EpidVerifyBasicSig for message_len: " << length << " failed";
  534. }
  535. }
  536. TEST_F(EpidMemberTest, SignBasicSucceedsWithLongMessage) {
  537. Prng my_prng;
  538. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  539. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  540. BasicSignature basic_sig;
  541. BigNumStr rnd_bsn = {0};
  542. VerifierCtxObj ctx(this->kGroupPublicKey);
  543. { // 1000000
  544. std::vector<uint8_t> msg(1000000); // allocate message for max size
  545. for (size_t n = 0; n < msg.size(); n++) {
  546. msg.at(n) = (uint8_t)n;
  547. }
  548. EXPECT_EQ(kEpidNoErr, EpidSignBasic(member, msg.data(), msg.size(), nullptr,
  549. 0, &basic_sig, &rnd_bsn))
  550. << "EpidSignBasic for message_len: " << 1000000 << " failed";
  551. EXPECT_EQ(kEpidNoErr,
  552. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()))
  553. << "EpidVerifyBasicSig for message_len: " << 1000000 << " failed";
  554. }
  555. }
  556. TEST_F(EpidMemberTest, SignBasicSucceedsWithMsgContainingAllPossibleBytes) {
  557. Prng my_prng;
  558. MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
  559. this->kMemberPrecomp, &Prng::Generate, &my_prng);
  560. auto& msg = this->kData_0_255;
  561. auto& bsn = this->kBsn0;
  562. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  563. BasicSignature basic_sig;
  564. EXPECT_EQ(kEpidNoErr,
  565. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  566. bsn.size(), &basic_sig, nullptr));
  567. VerifierCtxObj ctx(this->kGroupPublicKey);
  568. THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
  569. EXPECT_EQ(kEpidSigValid,
  570. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  571. }
  572. TEST_F(EpidMemberTest,
  573. PROTECTED_SignBasicSucceedsMsgAllPossibleBytesForCredential_EPS0) {
  574. Prng my_prng;
  575. MemberCtxObj member(
  576. this->kEps0GroupPublicKey,
  577. *(MembershipCredential const*)&this->kEps0MemberPrivateKey,
  578. &Prng::Generate, &my_prng);
  579. auto& msg = this->kData_0_255;
  580. auto& bsn = this->kBsn0;
  581. BasicSignature basic_sig;
  582. THROW_ON_EPIDERR(EpidRegisterBasename(member, bsn.data(), bsn.size()));
  583. EXPECT_EQ(kEpidNoErr,
  584. EpidSignBasic(member, msg.data(), msg.size(), bsn.data(),
  585. bsn.size(), &basic_sig, nullptr));
  586. VerifierCtxObj ctx(this->kEps0GroupPublicKey);
  587. THROW_ON_EPIDERR(EpidVerifierSetBasename(ctx, bsn.data(), bsn.size()));
  588. THROW_ON_EPIDERR(EpidVerifierSetHashAlg(ctx, kSha256));
  589. EXPECT_EQ(kEpidSigValid,
  590. EpidVerifyBasicSig(ctx, &basic_sig, msg.data(), msg.size()));
  591. }
  592. } // namespace