commit-test.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /// Tpm2Commit unit tests.
  17. /*! \file */
  18. #include "gtest/gtest.h"
  19. #include "epid/common-testhelper/ecpoint_wrapper-testhelper.h"
  20. #include "epid/common-testhelper/epid2params_wrapper-testhelper.h"
  21. #include "epid/common-testhelper/epid_params-testhelper.h"
  22. #include "epid/common-testhelper/errors-testhelper.h"
  23. #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
  24. #include "epid/common-testhelper/prng-testhelper.h"
  25. #include "epid/member/tpm2/unittests/tpm2-testhelper.h"
  26. extern "C" {
  27. #include "epid/common/src/endian_convert.h"
  28. #include "epid/member/tpm2/commit.h"
  29. #include "epid/member/tpm2/context.h"
  30. #include "epid/member/tpm2/load_external.h"
  31. #include "epid/member/tpm2/sign.h"
  32. }
  33. namespace {
  34. TEST_F(EpidTpm2Test, CommitFailsGivenNullParameters) {
  35. Epid20Params params;
  36. EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
  37. EcPointObj p1(&params.G1, this->kP1Str);
  38. FfElementObj y2(&params.fq, this->kY2Sha256Str);
  39. uint16_t counter = 0;
  40. Prng my_prng;
  41. Epid2ParamsObj epid2params;
  42. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, &this->kMemberFValue, epid2params);
  43. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha256));
  44. THROW_ON_EPIDERR(Tpm2LoadExternal(tpm, &this->kMemberFValue));
  45. EXPECT_EQ(kEpidBadArgErr,
  46. Tpm2Commit(nullptr, p1, this->kS2Sha256.data(),
  47. this->kS2Sha256.size(), y2, k, l, e, &counter));
  48. EXPECT_EQ(kEpidBadArgErr, Tpm2Commit(tpm, p1, nullptr, this->kS2Sha256.size(),
  49. y2, k, l, e, &counter));
  50. // Testing step a of the "C.2.3 Tpm2Commit()"
  51. EXPECT_EQ(kEpidBadArgErr,
  52. Tpm2Commit(tpm, p1, this->kS2Sha256.data(), this->kS2Sha256.size(),
  53. nullptr, k, l, e, &counter));
  54. // Testing step a of the "C.2.3 Tpm2Commit()"
  55. EXPECT_EQ(kEpidBadArgErr,
  56. Tpm2Commit(tpm, p1, this->kS2Sha256.data(), this->kS2Sha256.size(),
  57. y2, nullptr, l, e, &counter));
  58. EXPECT_EQ(kEpidBadArgErr,
  59. Tpm2Commit(tpm, p1, this->kS2Sha256.data(), this->kS2Sha256.size(),
  60. y2, k, nullptr, e, &counter));
  61. EXPECT_EQ(kEpidBadArgErr,
  62. Tpm2Commit(tpm, p1, this->kS2Sha256.data(), this->kS2Sha256.size(),
  63. y2, k, l, nullptr, &counter));
  64. EXPECT_EQ(kEpidBadArgErr,
  65. Tpm2Commit(tpm, p1, this->kS2Sha256.data(), this->kS2Sha256.size(),
  66. y2, k, l, e, nullptr));
  67. }
  68. TEST_F(EpidTpm2Test, CommitFailsGivenInvalidLength) {
  69. Epid20Params params;
  70. EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
  71. EcPointObj p1(&params.G1, this->kP1Str);
  72. FfElementObj y2(&params.fq, this->kY2Sha256Str);
  73. uint16_t counter = 0;
  74. Prng my_prng;
  75. Epid2ParamsObj epid2params;
  76. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, &this->kMemberFValue, epid2params);
  77. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha256));
  78. THROW_ON_EPIDERR(Tpm2LoadExternal(tpm, &this->kMemberFValue));
  79. EXPECT_EQ(kEpidBadArgErr, Tpm2Commit(tpm, p1, this->kS2Sha256.data(), 0, y2,
  80. k, l, e, &counter));
  81. EXPECT_EQ(kEpidBadArgErr,
  82. Tpm2Commit(tpm, p1, this->kS2Sha256.data(), sizeof(uint16_t) - 1,
  83. y2, k, l, e, &counter));
  84. }
  85. TEST_F(EpidTpm2Test, CommitFailsGivenPrivateKeyDoesNotExists) {
  86. // Testing step d of the "C.2.3 Tpm2Commit()"
  87. Epid20Params params;
  88. EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
  89. EcPointObj p1(&params.G1, this->kP1Str);
  90. FfElementObj y2(&params.fq, this->kY2Sha256Str);
  91. uint16_t counter = 0;
  92. Prng my_prng;
  93. Epid2ParamsObj epid2params;
  94. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, NULL, epid2params);
  95. EXPECT_EQ(kEpidBadArgErr,
  96. Tpm2Commit(tpm, p1, this->kS2Sha256.data(), this->kS2Sha256.size(),
  97. y2, k, l, e, &counter));
  98. }
  99. TEST_F(EpidTpm2Test, CommitFailsGivenS2y2NotOnCurve) {
  100. // Testing step d of the "C.2.3 Tpm2Commit()"
  101. Epid20Params params;
  102. EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
  103. EcPointObj p1(&params.G1, this->kP1Str);
  104. FqElemStr invalid_kY2Sha256Str = this->kY2Sha256Str;
  105. invalid_kY2Sha256Str.data.data[31]++; // make point not belong to the group
  106. FfElementObj invalid_y2(&params.fq, invalid_kY2Sha256Str);
  107. uint16_t counter = 0;
  108. Prng my_prng;
  109. Epid2ParamsObj epid2params;
  110. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, &this->kMemberFValue, epid2params);
  111. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha256));
  112. THROW_ON_EPIDERR(Tpm2LoadExternal(tpm, &this->kMemberFValue));
  113. EXPECT_EQ(kEpidBadArgErr,
  114. Tpm2Commit(tpm, p1, this->kS2Sha256.data(), this->kS2Sha256.size(),
  115. invalid_y2, k, l, e, &counter));
  116. }
  117. TEST_F(EpidTpm2Test, CommitFailsIfResultIsAtInfinity) {
  118. // Testing step l of the "C.2.3 Tpm2Commit()"
  119. Epid20Params params;
  120. EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
  121. G1ElemStr infinity_str = {0};
  122. EcPointObj infinity(&params.G1, infinity_str);
  123. FfElementObj y2(&params.fq, this->kY2Sha256Str);
  124. uint16_t counter = 0;
  125. Prng my_prng;
  126. Epid2ParamsObj epid2params;
  127. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, &this->kMemberFValue, epid2params);
  128. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha256));
  129. THROW_ON_EPIDERR(Tpm2LoadExternal(tpm, &this->kMemberFValue));
  130. EXPECT_EQ(kEpidBadArgErr,
  131. Tpm2Commit(tpm, infinity, this->kS2Sha256.data(),
  132. this->kS2Sha256.size(), y2, k, l, e, &counter));
  133. }
  134. TEST_F(EpidTpm2Test, CommitCanUseKeyLoadedByLoadExternal) {
  135. Prng prng;
  136. Epid2ParamsObj epid2params;
  137. Epid20Params params;
  138. FpElemStr f_str = this->kMemberFValue;
  139. EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
  140. FfElementObj y2(&params.fq, this->kY2Sha256Str);
  141. uint16_t counter = 0;
  142. Tpm2CtxObj tpm(&Prng::Generate, &prng, &f_str, epid2params);
  143. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha256));
  144. EXPECT_EQ(kEpidNoErr, Tpm2LoadExternal(tpm, &f_str));
  145. EXPECT_EQ(kEpidNoErr,
  146. Tpm2Commit(tpm, nullptr, this->kS2Sha256.data(),
  147. this->kS2Sha256.size(), y2, k, l, e, &counter));
  148. THROW_ON_EPIDERR(Tpm2ReleaseCounter(tpm, counter));
  149. // k = (x2, y2) ^ f, where x2 = Hash(s2)
  150. G1ElemStr k_str;
  151. THROW_ON_EPIDERR(WriteEcPoint(params.G1, k, &k_str, sizeof(k_str)));
  152. EXPECT_EQ(this->kP2Sha256ExpF, k_str);
  153. }
  154. TEST_F(EpidTpm2Test, CommitReturnsSameLEForSameP1P2) {
  155. Prng prng;
  156. Epid2ParamsObj epid2params;
  157. Epid20Params params;
  158. FpElemStr f_str = this->kMemberFValue;
  159. // create TPM context
  160. Tpm2CtxObj tpm(&Prng::Generate, &prng, &f_str, epid2params);
  161. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha256));
  162. // load f value
  163. EXPECT_EQ(kEpidNoErr, Tpm2LoadExternal(tpm, &f_str));
  164. // commit(P1=p2, P2=p2) => k = p2^f, l = p2^r, e = p2^r
  165. FfElementObj y2(&params.fq, this->kY2Sha256Str);
  166. EcPointObj p2(&params.G1, kP2Sha256Str);
  167. EcPointObj p2_exp_f(&params.G1, kP2Sha256ExpF);
  168. EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
  169. uint16_t counter = 0;
  170. EXPECT_EQ(kEpidNoErr,
  171. Tpm2Commit(tpm, p2, this->kS2Sha256.data(), this->kS2Sha256.size(),
  172. y2, k, l, e, &counter));
  173. THROW_ON_EPIDERR(Tpm2ReleaseCounter(tpm, counter));
  174. G1ElemStr l_str, e_str;
  175. THROW_ON_EPIDERR(WriteEcPoint(params.G1, l, &l_str, sizeof(l_str)));
  176. THROW_ON_EPIDERR(WriteEcPoint(params.G1, e, &e_str, sizeof(e_str)));
  177. EXPECT_EQ(l_str, e_str);
  178. }
  179. TEST_F(EpidTpm2Test, CommitCanBeUsedTwice) {
  180. Prng prng;
  181. Epid2ParamsObj epid2params;
  182. Epid20Params params;
  183. FpElemStr f_str = this->kMemberFValue;
  184. // create TPM context
  185. Tpm2CtxObj tpm(&Prng::Generate, &prng, &f_str, epid2params);
  186. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha256));
  187. // load f value
  188. EXPECT_EQ(kEpidNoErr, Tpm2LoadExternal(tpm, &f_str));
  189. EcPointObj p1(&params.G1, this->kP1Str);
  190. EcPointObj e(&params.G1);
  191. uint16_t ctr1 = 0, ctr2 = 0;
  192. EXPECT_EQ(kEpidNoErr, Tpm2Commit(tpm, p1, nullptr, 0, nullptr, nullptr,
  193. nullptr, e, &ctr1));
  194. EXPECT_EQ(kEpidNoErr, Tpm2Commit(tpm, p1, nullptr, 0, nullptr, nullptr,
  195. nullptr, e, &ctr2));
  196. THROW_ON_EPIDERR(Tpm2ReleaseCounter(tpm, ctr1));
  197. THROW_ON_EPIDERR(Tpm2ReleaseCounter(tpm, ctr2));
  198. }
  199. TEST_F(EpidTpm2Test, CommitCanUseHashFromEcHashSha256) {
  200. HashAlg halg = kSha256;
  201. Prng prng;
  202. Epid2ParamsObj epid2params;
  203. FpElemStr f_str = this->kMemberFValue;
  204. Tpm2CtxObj tpm(&Prng::Generate, &prng, &f_str, epid2params);
  205. Epid20Params params;
  206. EcPointObj R(&params.G1), k(&params.G1), l(&params.G1), e(&params.G1);
  207. FfElementObj y(&params.fq);
  208. uint32_t i = 0;
  209. uint16_t counter = 0;
  210. G1ElemStr R_str = {0};
  211. std::vector<uint8_t> bsn = {'b', 's', 'n', '0'};
  212. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, halg));
  213. EXPECT_EQ(kEpidNoErr, Tpm2LoadExternal(tpm, &f_str));
  214. EXPECT_EQ(kEpidNoErr,
  215. EcHash(epid2params.G1(), bsn.data(), bsn.size(), halg, R, &i));
  216. i = ntohl(i);
  217. THROW_ON_EPIDERR(WriteEcPoint(epid2params.G1(), R, &R_str, sizeof(R_str)));
  218. THROW_ON_EPIDERR(
  219. ReadFfElement(params.fq.get(), &R_str.y, sizeof(R_str.y), y));
  220. std::vector<uint8_t> digest((uint8_t*)&i, (uint8_t*)&i + sizeof(i));
  221. digest.reserve(digest.size() + bsn.size());
  222. digest.insert(digest.end(), bsn.begin(), bsn.end());
  223. EXPECT_EQ(kEpidNoErr, Tpm2Commit(tpm, nullptr, digest.data(), digest.size(),
  224. y, k, l, e, &counter));
  225. Tpm2ReleaseCounter(tpm, counter);
  226. }
  227. #ifndef TPM_TSS
  228. TEST_F(EpidTpm2Test, CommitCanUseHashFromEcHashSha384) {
  229. HashAlg halg = kSha384;
  230. Prng prng;
  231. Epid2ParamsObj epid2params;
  232. FpElemStr f_str = this->kMemberFValue;
  233. Tpm2CtxObj tpm(&Prng::Generate, &prng, &f_str, epid2params);
  234. Epid20Params params;
  235. EcPointObj R(&params.G1), k(&params.G1), l(&params.G1), e(&params.G1);
  236. FfElementObj y(&params.fq);
  237. uint32_t i = 0;
  238. uint16_t counter = 0;
  239. G1ElemStr R_str = {0};
  240. std::vector<uint8_t> bsn = {'b', 's', 'n', '0'};
  241. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, halg));
  242. EXPECT_EQ(kEpidNoErr, Tpm2LoadExternal(tpm, &f_str));
  243. EXPECT_EQ(kEpidNoErr,
  244. EcHash(epid2params.G1(), bsn.data(), bsn.size(), halg, R, &i));
  245. i = ntohl(i);
  246. THROW_ON_EPIDERR(WriteEcPoint(epid2params.G1(), R, &R_str, sizeof(R_str)));
  247. THROW_ON_EPIDERR(
  248. ReadFfElement(params.fq.get(), &R_str.y, sizeof(R_str.y), y));
  249. std::vector<uint8_t> digest((uint8_t*)&i, (uint8_t*)&i + sizeof(i));
  250. digest.reserve(digest.size() + bsn.size());
  251. digest.insert(digest.end(), bsn.begin(), bsn.end());
  252. EXPECT_EQ(kEpidNoErr, Tpm2Commit(tpm, nullptr, digest.data(), digest.size(),
  253. y, k, l, e, &counter));
  254. Tpm2ReleaseCounter(tpm, counter);
  255. }
  256. TEST_F(EpidTpm2Test, CommitCanUseHashFromEcHashSha512) {
  257. HashAlg halg = kSha512;
  258. Prng prng;
  259. Epid2ParamsObj epid2params;
  260. FpElemStr f_str = this->kMemberFValue;
  261. Tpm2CtxObj tpm(&Prng::Generate, &prng, &f_str, epid2params);
  262. Epid20Params params;
  263. EcPointObj R(&params.G1), k(&params.G1), l(&params.G1), e(&params.G1);
  264. FfElementObj y(&params.fq);
  265. uint32_t i = 0;
  266. uint16_t counter = 0;
  267. G1ElemStr R_str = {0};
  268. std::vector<uint8_t> bsn = {'b', 's', 'n', '0'};
  269. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, halg));
  270. EXPECT_EQ(kEpidNoErr, Tpm2LoadExternal(tpm, &f_str));
  271. EXPECT_EQ(kEpidNoErr,
  272. EcHash(epid2params.G1(), bsn.data(), bsn.size(), halg, R, &i));
  273. i = ntohl(i);
  274. THROW_ON_EPIDERR(WriteEcPoint(epid2params.G1(), R, &R_str, sizeof(R_str)));
  275. THROW_ON_EPIDERR(
  276. ReadFfElement(params.fq.get(), &R_str.y, sizeof(R_str.y), y));
  277. std::vector<uint8_t> digest((uint8_t*)&i, (uint8_t*)&i + sizeof(i));
  278. digest.reserve(digest.size() + bsn.size());
  279. digest.insert(digest.end(), bsn.begin(), bsn.end());
  280. EXPECT_EQ(kEpidNoErr, Tpm2Commit(tpm, nullptr, digest.data(), digest.size(),
  281. y, k, l, e, &counter));
  282. Tpm2ReleaseCounter(tpm, counter);
  283. }
  284. #endif // TPM_TSS
  285. } // namespace