commit-simulator-test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <cstring>
  19. #include "gtest/gtest.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/prng-testhelper.h"
  24. #include "epid/member/tpm2/unittests/tpm2-testhelper.h"
  25. extern "C" {
  26. #include "epid/common/src/epid2params.h"
  27. #include "epid/member/tpm2/commit.h"
  28. #include "epid/member/tpm2/context.h"
  29. #include "epid/member/tpm2/load_external.h"
  30. #include "epid/member/tpm2/sign.h"
  31. #include "epid/member/tpm2/src/state.h"
  32. }
  33. namespace {
  34. TEST_F(EpidTpm2Test, CommitComputeKLESha256) {
  35. // Testing step i and j of the "C.2.3 Tpm2Commit()"
  36. Epid20Params params;
  37. EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
  38. EcPointObj p1(&params.G1, this->kP1Str);
  39. FfElementObj y2(&params.fq, this->kY2Sha256Str);
  40. uint16_t counter = 0;
  41. Prng my_prng;
  42. Epid2ParamsObj epid2params;
  43. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, &this->kMemberFValue, epid2params);
  44. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha256));
  45. THROW_ON_EPIDERR(Tpm2LoadExternal(tpm, &this->kMemberFValue));
  46. EXPECT_EQ(kEpidNoErr,
  47. Tpm2Commit(tpm, p1, this->kS2Sha256.data(), this->kS2Sha256.size(),
  48. y2, k, l, e, &counter));
  49. THROW_ON_EPIDERR(Tpm2ReleaseCounter(tpm, counter));
  50. EcPointObj p1_exp_r(&params.G1), p2_exp_r(&params.G1);
  51. G1ElemStr p1_exp_r_str, p2_exp_r_str;
  52. EcPointObj p2(&params.G1, this->kP2Sha256Str);
  53. Prng the_same_prng;
  54. FfElementObj r(&params.fp);
  55. BigNumStr zero = {0};
  56. FpElemStr r_str = {0};
  57. THROW_ON_EPIDERR(
  58. FfGetRandom(params.fp, &zero, &Prng::Generate, &the_same_prng, r));
  59. THROW_ON_EPIDERR(WriteFfElement(params.fp, r, &r_str, sizeof(r_str)));
  60. THROW_ON_EPIDERR(EcExp(params.G1, p1, (BigNumStr const*)&r_str, p1_exp_r));
  61. THROW_ON_EPIDERR(
  62. WriteEcPoint(params.G1, p1_exp_r, &p1_exp_r_str, sizeof(p1_exp_r_str)));
  63. THROW_ON_EPIDERR(EcExp(params.G1, p2, (BigNumStr const*)&r_str, p2_exp_r));
  64. THROW_ON_EPIDERR(
  65. WriteEcPoint(params.G1, p2_exp_r, &p2_exp_r_str, sizeof(p2_exp_r_str)));
  66. G1ElemStr k_str, l_str, e_str;
  67. THROW_ON_EPIDERR(WriteEcPoint(params.G1, k, &k_str, sizeof(k_str)));
  68. THROW_ON_EPIDERR(WriteEcPoint(params.G1, l, &l_str, sizeof(l_str)));
  69. THROW_ON_EPIDERR(WriteEcPoint(params.G1, e, &e_str, sizeof(e_str)));
  70. EXPECT_EQ(this->kP2Sha256ExpF, k_str);
  71. EXPECT_EQ(p2_exp_r_str, l_str);
  72. EXPECT_EQ(p1_exp_r_str, e_str);
  73. }
  74. TEST_F(EpidTpm2Test, CommitComputeKLESha384) {
  75. // Testing step i and j of the "C.2.3 Tpm2Commit()"
  76. Epid20Params params;
  77. EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
  78. EcPointObj p1(&params.G1, this->kP1Str);
  79. FfElementObj y2(&params.fq, this->kY2Sha384Str);
  80. uint16_t counter = 0;
  81. Prng my_prng;
  82. Epid2ParamsObj epid2params;
  83. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, &this->kMemberFValue, epid2params);
  84. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha384));
  85. THROW_ON_EPIDERR(Tpm2LoadExternal(tpm, &this->kMemberFValue));
  86. EXPECT_EQ(kEpidNoErr,
  87. Tpm2Commit(tpm, p1, this->kS2Sha384.data(), this->kS2Sha384.size(),
  88. y2, k, l, e, &counter));
  89. THROW_ON_EPIDERR(Tpm2ReleaseCounter(tpm, counter));
  90. EcPointObj p1_exp_r(&params.G1), p2_exp_r(&params.G1);
  91. G1ElemStr p1_exp_r_str, p2_exp_r_str;
  92. EcPointObj p2(&params.G1, this->kP2Sha384Str);
  93. Prng the_same_prng;
  94. FfElementObj r(&params.fp);
  95. BigNumStr zero = {0};
  96. FpElemStr r_str = {0};
  97. THROW_ON_EPIDERR(
  98. FfGetRandom(params.fp, &zero, &Prng::Generate, &the_same_prng, r));
  99. THROW_ON_EPIDERR(WriteFfElement(params.fp, r, &r_str, sizeof(r_str)));
  100. THROW_ON_EPIDERR(EcExp(params.G1, p1, (BigNumStr const*)&r_str, p1_exp_r));
  101. THROW_ON_EPIDERR(
  102. WriteEcPoint(params.G1, p1_exp_r, &p1_exp_r_str, sizeof(p1_exp_r_str)));
  103. THROW_ON_EPIDERR(EcExp(params.G1, p2, (BigNumStr const*)&r_str, p2_exp_r));
  104. THROW_ON_EPIDERR(
  105. WriteEcPoint(params.G1, p2_exp_r, &p2_exp_r_str, sizeof(p2_exp_r_str)));
  106. G1ElemStr k_str, l_str, e_str;
  107. THROW_ON_EPIDERR(WriteEcPoint(params.G1, k, &k_str, sizeof(k_str)));
  108. THROW_ON_EPIDERR(WriteEcPoint(params.G1, l, &l_str, sizeof(l_str)));
  109. THROW_ON_EPIDERR(WriteEcPoint(params.G1, e, &e_str, sizeof(e_str)));
  110. EXPECT_EQ(this->kP2Sha384ExpF, k_str);
  111. EXPECT_EQ(p2_exp_r_str, l_str);
  112. EXPECT_EQ(p1_exp_r_str, e_str);
  113. }
  114. TEST_F(EpidTpm2Test, CommitComputeKLESha512) {
  115. // Testing step i and j of the "C.2.3 Tpm2Commit()"
  116. Epid20Params params;
  117. EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
  118. EcPointObj p1(&params.G1, this->kP1Str);
  119. FfElementObj y2(&params.fq, this->kY2Sha512Str);
  120. uint16_t counter = 0;
  121. Prng my_prng;
  122. Epid2ParamsObj epid2params;
  123. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, &this->kMemberFValue, epid2params);
  124. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha512));
  125. THROW_ON_EPIDERR(Tpm2LoadExternal(tpm, &this->kMemberFValue));
  126. EXPECT_EQ(kEpidNoErr,
  127. Tpm2Commit(tpm, p1, this->kS2Sha512.data(), this->kS2Sha512.size(),
  128. y2, k, l, e, &counter));
  129. THROW_ON_EPIDERR(Tpm2ReleaseCounter(tpm, counter));
  130. EcPointObj p1_exp_r(&params.G1), p2_exp_r(&params.G1);
  131. G1ElemStr p1_exp_r_str, p2_exp_r_str;
  132. EcPointObj p2(&params.G1, this->kP2Sha512Str);
  133. Prng the_same_prng;
  134. FfElementObj r(&params.fp);
  135. BigNumStr zero = {0};
  136. FpElemStr r_str = {0};
  137. THROW_ON_EPIDERR(
  138. FfGetRandom(params.fp, &zero, &Prng::Generate, &the_same_prng, r));
  139. THROW_ON_EPIDERR(WriteFfElement(params.fp, r, &r_str, sizeof(r_str)));
  140. THROW_ON_EPIDERR(EcExp(params.G1, p1, (BigNumStr const*)&r_str, p1_exp_r));
  141. THROW_ON_EPIDERR(
  142. WriteEcPoint(params.G1, p1_exp_r, &p1_exp_r_str, sizeof(p1_exp_r_str)));
  143. THROW_ON_EPIDERR(EcExp(params.G1, p2, (BigNumStr const*)&r_str, p2_exp_r));
  144. THROW_ON_EPIDERR(
  145. WriteEcPoint(params.G1, p2_exp_r, &p2_exp_r_str, sizeof(p2_exp_r_str)));
  146. G1ElemStr k_str, l_str, e_str;
  147. THROW_ON_EPIDERR(WriteEcPoint(params.G1, k, &k_str, sizeof(k_str)));
  148. THROW_ON_EPIDERR(WriteEcPoint(params.G1, l, &l_str, sizeof(l_str)));
  149. THROW_ON_EPIDERR(WriteEcPoint(params.G1, e, &e_str, sizeof(e_str)));
  150. EXPECT_EQ(this->kP2Sha512ExpF, k_str);
  151. EXPECT_EQ(p2_exp_r_str, l_str);
  152. EXPECT_EQ(p1_exp_r_str, e_str);
  153. }
  154. TEST_F(EpidTpm2Test, CommitComputeKLESha512256) {
  155. // Testing step i and j of the "C.2.3 Tpm2Commit()"
  156. Epid20Params params;
  157. EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
  158. EcPointObj p1(&params.G1, this->kP1Str);
  159. FfElementObj y2(&params.fq, this->kY2Sha512256Str);
  160. uint16_t counter = 0;
  161. Prng my_prng;
  162. Epid2ParamsObj epid2params;
  163. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, &this->kMemberFValue, epid2params);
  164. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha512_256));
  165. THROW_ON_EPIDERR(Tpm2LoadExternal(tpm, &this->kMemberFValue));
  166. EXPECT_EQ(kEpidNoErr,
  167. Tpm2Commit(tpm, p1, this->kS2Sha512256.data(),
  168. this->kS2Sha512256.size(), y2, k, l, e, &counter));
  169. THROW_ON_EPIDERR(Tpm2ReleaseCounter(tpm, counter));
  170. EcPointObj p1_exp_r(&params.G1), p2_exp_r(&params.G1);
  171. G1ElemStr p1_exp_r_str, p2_exp_r_str;
  172. EcPointObj p2(&params.G1, this->kP2Sha512256Str);
  173. Prng the_same_prng;
  174. FfElementObj r(&params.fp);
  175. BigNumStr zero = {0};
  176. FpElemStr r_str = {0};
  177. THROW_ON_EPIDERR(
  178. FfGetRandom(params.fp, &zero, &Prng::Generate, &the_same_prng, r));
  179. THROW_ON_EPIDERR(WriteFfElement(params.fp, r, &r_str, sizeof(r_str)));
  180. THROW_ON_EPIDERR(EcExp(params.G1, p1, (BigNumStr const*)&r_str, p1_exp_r));
  181. THROW_ON_EPIDERR(
  182. WriteEcPoint(params.G1, p1_exp_r, &p1_exp_r_str, sizeof(p1_exp_r_str)));
  183. THROW_ON_EPIDERR(EcExp(params.G1, p2, (BigNumStr const*)&r_str, p2_exp_r));
  184. THROW_ON_EPIDERR(
  185. WriteEcPoint(params.G1, p2_exp_r, &p2_exp_r_str, sizeof(p2_exp_r_str)));
  186. G1ElemStr k_str, l_str, e_str;
  187. THROW_ON_EPIDERR(WriteEcPoint(params.G1, k, &k_str, sizeof(k_str)));
  188. THROW_ON_EPIDERR(WriteEcPoint(params.G1, l, &l_str, sizeof(l_str)));
  189. THROW_ON_EPIDERR(WriteEcPoint(params.G1, e, &e_str, sizeof(e_str)));
  190. EXPECT_EQ(this->kP2Sha512256ExpF, k_str);
  191. EXPECT_EQ(p2_exp_r_str, l_str);
  192. EXPECT_EQ(p1_exp_r_str, e_str);
  193. }
  194. TEST_F(EpidTpm2Test, CommitComputeEOnly) {
  195. // Testing step j excuding i of the "C.2.3 Tpm2Commit()"
  196. Epid20Params params;
  197. EcPointObj k(&params.G1), l(&params.G1), e(&params.G1);
  198. EcPointObj p1(&params.G1, this->kP1Str);
  199. FfElementObj y2(&params.fq, this->kY2Sha512Str);
  200. uint16_t counter = 0;
  201. Prng my_prng;
  202. Epid2ParamsObj epid2params;
  203. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, &this->kMemberFValue, epid2params);
  204. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha512));
  205. THROW_ON_EPIDERR(Tpm2LoadExternal(tpm, &this->kMemberFValue));
  206. EXPECT_EQ(kEpidNoErr,
  207. Tpm2Commit(tpm, p1, nullptr, 0, nullptr, k, l, e, &counter));
  208. THROW_ON_EPIDERR(Tpm2ReleaseCounter(tpm, counter));
  209. EcPointObj p1_exp_r(&params.G1);
  210. G1ElemStr p1_exp_r_str;
  211. Prng the_same_prng;
  212. FfElementObj r(&params.fp);
  213. BigNumStr zero = {0};
  214. FpElemStr r_str = {0};
  215. THROW_ON_EPIDERR(
  216. FfGetRandom(params.fp, &zero, &Prng::Generate, &the_same_prng, r));
  217. THROW_ON_EPIDERR(WriteFfElement(params.fp, r, &r_str, sizeof(r_str)));
  218. THROW_ON_EPIDERR(EcExp(params.G1, p1, (BigNumStr const*)&r_str, p1_exp_r));
  219. THROW_ON_EPIDERR(
  220. WriteEcPoint(params.G1, p1_exp_r, &p1_exp_r_str, sizeof(p1_exp_r_str)));
  221. G1ElemStr e_str;
  222. THROW_ON_EPIDERR(WriteEcPoint(params.G1, e, &e_str, sizeof(e_str)));
  223. EXPECT_EQ(p1_exp_r_str, e_str);
  224. }
  225. TEST_F(EpidTpm2Test, CommitComputeEOnlyWithDefaultP1) {
  226. // Testing step k excuding i of the "C.2.3 Tpm2Commit()"
  227. Epid20Params params;
  228. EcPointObj e(&params.G1);
  229. EcPointObj p1(&params.G1, this->kP1Str);
  230. FfElementObj y2(&params.fq, this->kY2Sha512Str);
  231. uint16_t counter = 0;
  232. Prng my_prng;
  233. Epid2ParamsObj epid2params;
  234. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, &this->kMemberFValue, epid2params);
  235. THROW_ON_EPIDERR(Tpm2SetHashAlg(tpm, kSha512));
  236. THROW_ON_EPIDERR(Tpm2LoadExternal(tpm, &this->kMemberFValue));
  237. EXPECT_EQ(kEpidNoErr, Tpm2Commit(tpm, nullptr, nullptr, 0, nullptr, nullptr,
  238. nullptr, e, &counter));
  239. THROW_ON_EPIDERR(Tpm2ReleaseCounter(tpm, counter));
  240. EcPointObj g1(&params.G1, this->kg1Str);
  241. EcPointObj g1_exp_r(&params.G1);
  242. G1ElemStr g1_exp_r_str;
  243. Prng the_same_prng;
  244. FfElementObj r(&params.fp);
  245. BigNumStr zero = {0};
  246. FpElemStr r_str = {0};
  247. THROW_ON_EPIDERR(
  248. FfGetRandom(params.fp, &zero, &Prng::Generate, &the_same_prng, r));
  249. THROW_ON_EPIDERR(WriteFfElement(params.fp, r, &r_str, sizeof(r_str)));
  250. THROW_ON_EPIDERR(EcExp(params.G1, g1, (BigNumStr const*)&r_str, g1_exp_r));
  251. THROW_ON_EPIDERR(
  252. WriteEcPoint(params.G1, g1_exp_r, &g1_exp_r_str, sizeof(g1_exp_r_str)));
  253. G1ElemStr e_str;
  254. THROW_ON_EPIDERR(WriteEcPoint(params.G1, e, &e_str, sizeof(e_str)));
  255. EXPECT_EQ(g1_exp_r_str, e_str);
  256. }
  257. } // namespace