signbasic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. /// EpidSignBasic implementation.
  17. /*! \file */
  18. #include "epid/member/src/signbasic.h"
  19. #include <string.h> // memset
  20. #include "epid/common/math/ecgroup.h"
  21. #include "epid/common/math/finitefield.h"
  22. #include "epid/common/src/endian_convert.h"
  23. #include "epid/common/src/epid2params.h"
  24. #include "epid/common/src/hashsize.h"
  25. #include "epid/common/src/memory.h"
  26. #include "epid/member/api.h"
  27. #include "epid/member/src/allowed_basenames.h"
  28. #include "epid/member/src/context.h"
  29. #include "epid/member/src/hash_basename.h"
  30. #include "epid/member/src/presig-internal.h"
  31. #include "epid/member/src/sign_commitment.h"
  32. #include "epid/member/tpm2/commit.h"
  33. #include "epid/member/tpm2/sign.h"
  34. /// Handle SDK Error with Break
  35. #define BREAK_ON_EPID_ERROR(ret) \
  36. if (kEpidNoErr != (ret)) { \
  37. break; \
  38. }
  39. /// Count of elements in array
  40. #define COUNT_OF(A) (sizeof(A) / sizeof((A)[0]))
  41. EpidStatus EpidSignBasic(MemberCtx const* ctx, void const* msg, size_t msg_len,
  42. void const* basename, size_t basename_len,
  43. BasicSignature* sig, BigNumStr* rnd_bsn) {
  44. EpidStatus sts = kEpidErr;
  45. EcPoint* B = NULL;
  46. EcPoint* t = NULL; // temp value in G1
  47. EcPoint* k = NULL;
  48. EcPoint* e = NULL;
  49. FfElement* R2 = NULL;
  50. FfElement* p2y = NULL;
  51. FfElement* t1 = NULL;
  52. FfElement* t2 = NULL;
  53. FfElement* a = NULL;
  54. FfElement* b = NULL;
  55. FfElement* rx = NULL;
  56. FfElement* ra = NULL;
  57. FfElement* rb = NULL;
  58. struct p2x_t {
  59. uint32_t i;
  60. uint8_t bsn[1];
  61. }* p2x = NULL;
  62. FfElement* t3 = NULL; // temporary for multiplication
  63. FfElement* c = NULL;
  64. uint8_t* digest = NULL;
  65. PreComputedSignature curr_presig = {0};
  66. if (!ctx || !sig) {
  67. return kEpidBadArgErr;
  68. }
  69. if (!msg && (0 != msg_len)) {
  70. // if message is non-empty it must have both length and content
  71. return kEpidBadArgErr;
  72. }
  73. if (!basename && (0 != basename_len)) {
  74. // if basename is non-empty it must have both length and content
  75. return kEpidBadArgErr;
  76. }
  77. if (!ctx->epid2_params) {
  78. return kEpidBadArgErr;
  79. }
  80. do {
  81. FiniteField* Fp = ctx->epid2_params->Fp;
  82. SignCommitOutput commit_out = {0};
  83. FpElemStr c_str = {0};
  84. EcGroup* G1 = ctx->epid2_params->G1;
  85. FiniteField* GT = ctx->epid2_params->GT;
  86. FiniteField* Fq = ctx->epid2_params->Fq;
  87. PairingState* ps_ctx = ctx->epid2_params->pairing_state;
  88. const BigNumStr kOne = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  89. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
  90. BigNumStr t1_str = {0};
  91. BigNumStr t2_str = {0};
  92. size_t digest_size = 0;
  93. uint16_t* rf_ctr = (uint16_t*)&ctx->rf_ctr;
  94. FfElement const* x = ctx->x;
  95. if (basename) {
  96. if (!IsBasenameAllowed(ctx->allowed_basenames, basename, basename_len)) {
  97. sts = kEpidBadArgErr;
  98. BREAK_ON_EPID_ERROR(sts);
  99. }
  100. }
  101. sts = NewEcPoint(G1, &B);
  102. BREAK_ON_EPID_ERROR(sts);
  103. sts = NewEcPoint(G1, &k);
  104. BREAK_ON_EPID_ERROR(sts);
  105. sts = NewEcPoint(G1, &t);
  106. BREAK_ON_EPID_ERROR(sts);
  107. sts = NewEcPoint(G1, &e);
  108. BREAK_ON_EPID_ERROR(sts);
  109. sts = NewFfElement(GT, &R2);
  110. BREAK_ON_EPID_ERROR(sts);
  111. sts = NewFfElement(Fq, &p2y);
  112. BREAK_ON_EPID_ERROR(sts);
  113. sts = NewFfElement(Fp, &t1);
  114. BREAK_ON_EPID_ERROR(sts);
  115. sts = NewFfElement(Fp, &t2);
  116. BREAK_ON_EPID_ERROR(sts);
  117. p2x = (struct p2x_t*)SAFE_ALLOC(sizeof(struct p2x_t) + basename_len - 1);
  118. if (!p2x) {
  119. sts = kEpidMemAllocErr;
  120. break;
  121. }
  122. sts = NewFfElement(Fp, &a);
  123. BREAK_ON_EPID_ERROR(sts);
  124. sts = NewFfElement(Fp, &b);
  125. BREAK_ON_EPID_ERROR(sts);
  126. sts = NewFfElement(Fp, &rx);
  127. BREAK_ON_EPID_ERROR(sts);
  128. sts = NewFfElement(Fp, &ra);
  129. BREAK_ON_EPID_ERROR(sts);
  130. sts = NewFfElement(Fp, &rb);
  131. BREAK_ON_EPID_ERROR(sts);
  132. sts = MemberGetPreSig((MemberCtx*)ctx, &curr_presig);
  133. BREAK_ON_EPID_ERROR(sts);
  134. // 3. If the pre-computed signature pre-sigma exists, the member
  135. // loads (B, K, T, a, b, rx, rf, ra, rb, R1, R2) from
  136. // pre-sigma. Refer to Section 4.4 for the computation of
  137. // these values.
  138. sts = ReadFfElement(Fp, &curr_presig.a, sizeof(curr_presig.a), a);
  139. BREAK_ON_EPID_ERROR(sts);
  140. sts = ReadFfElement(Fp, &curr_presig.b, sizeof(curr_presig.b), b);
  141. BREAK_ON_EPID_ERROR(sts);
  142. sts = ReadFfElement(Fp, &curr_presig.rx, sizeof(curr_presig.rx), rx);
  143. BREAK_ON_EPID_ERROR(sts);
  144. sts = ReadFfElement(Fp, &curr_presig.ra, sizeof(curr_presig.ra), ra);
  145. BREAK_ON_EPID_ERROR(sts);
  146. sts = ReadFfElement(Fp, &curr_presig.rb, sizeof(curr_presig.rb), rb);
  147. BREAK_ON_EPID_ERROR(sts);
  148. // If the basename is provided, use it, otherwise use presig B
  149. if (basename) {
  150. // 3.a. The member computes (B, i2, y2) = G1.tpmHash(bsn).
  151. sts = EcHash(G1, basename, basename_len, ctx->hash_alg, B, &p2x->i);
  152. BREAK_ON_EPID_ERROR(sts);
  153. p2x->i = htonl(p2x->i);
  154. sts = WriteEcPoint(G1, B, &commit_out.B, sizeof(commit_out.B));
  155. BREAK_ON_EPID_ERROR(sts);
  156. sts = ReadFfElement(Fq, &commit_out.B.y, sizeof(commit_out.B.y), p2y);
  157. BREAK_ON_EPID_ERROR(sts);
  158. // b.i. (KTPM, LTPM, ETPM, counterTPM) = TPM2_Commit(P1=h1,(s2, y2) = (i2
  159. // || bsn, y2)).
  160. // b.ii.K = KTPM.
  161. if (0 !=
  162. memcpy_S((void*)p2x->bsn, basename_len, basename, basename_len)) {
  163. sts = kEpidBadArgErr;
  164. break;
  165. }
  166. sts =
  167. Tpm2Commit(ctx->tpm2_ctx, ctx->h1, p2x, sizeof(p2x->i) + basename_len,
  168. p2y, k, t, e, (uint16_t*)&ctx->rf_ctr);
  169. BREAK_ON_EPID_ERROR(sts);
  170. sts = WriteEcPoint(G1, k, &commit_out.K, sizeof(commit_out.K));
  171. BREAK_ON_EPID_ERROR(sts);
  172. // c.i. The member computes R1 = LTPM.
  173. sts = WriteEcPoint(G1, t, &commit_out.R1, sizeof(commit_out.R1));
  174. BREAK_ON_EPID_ERROR(sts);
  175. // c.ii. e12rf = pairing(ETPM, g2)
  176. sts = Pairing(ps_ctx, e, ctx->epid2_params->g2, R2);
  177. BREAK_ON_EPID_ERROR(sts);
  178. // c.iii. R2 = GT.sscmMultiExp(ea2, t1, e12rf, 1, e22, t2, e2w,ra).
  179. // 4.i. The member computes t1 = (- rx) mod p.
  180. sts = FfNeg(Fp, rx, t1);
  181. BREAK_ON_EPID_ERROR(sts);
  182. // 4.j. The member computes t2 = (rb - a * rx) mod p.
  183. sts = FfMul(Fp, a, rx, t2);
  184. BREAK_ON_EPID_ERROR(sts);
  185. sts = FfNeg(Fp, t2, t2);
  186. BREAK_ON_EPID_ERROR(sts);
  187. sts = FfAdd(Fp, rb, t2, t2);
  188. BREAK_ON_EPID_ERROR(sts);
  189. sts = WriteFfElement(Fp, t1, &t1_str, sizeof(t1_str));
  190. BREAK_ON_EPID_ERROR(sts);
  191. sts = WriteFfElement(Fp, t2, &t2_str, sizeof(t2_str));
  192. BREAK_ON_EPID_ERROR(sts);
  193. {
  194. FfElement const* points[4];
  195. BigNumStr const* exponents[4];
  196. points[0] = ctx->ea2;
  197. points[1] = R2;
  198. points[2] = ctx->e22;
  199. points[3] = ctx->e2w;
  200. exponents[0] = &t1_str;
  201. exponents[1] = &kOne;
  202. exponents[2] = &t2_str;
  203. exponents[3] = (BigNumStr*)&curr_presig.ra;
  204. sts = FfMultiExp(GT, points, exponents, COUNT_OF(points), R2);
  205. BREAK_ON_EPID_ERROR(sts);
  206. }
  207. sts = WriteFfElement(GT, R2, &commit_out.R2, sizeof(commit_out.R2));
  208. BREAK_ON_EPID_ERROR(sts);
  209. // d. The member over-writes the counterTPM, B, K, R1 and R2 values.
  210. } else {
  211. if (!rnd_bsn) {
  212. sts = kEpidBadArgErr;
  213. break;
  214. }
  215. sts = ReadEcPoint(G1, &curr_presig.B, sizeof(curr_presig.B), B);
  216. BREAK_ON_EPID_ERROR(sts);
  217. commit_out.B = curr_presig.B;
  218. commit_out.K = curr_presig.K;
  219. commit_out.R1 = curr_presig.R1;
  220. ((MemberCtx*)ctx)->rf_ctr = curr_presig.rf_ctr;
  221. commit_out.R2 = curr_presig.R2;
  222. *rnd_bsn = curr_presig.rnd_bsn;
  223. }
  224. commit_out.T = curr_presig.T;
  225. sts = HashSignCommitment(Fp, ctx->hash_alg, &ctx->pub_key, &commit_out, msg,
  226. msg_len, &c_str);
  227. BREAK_ON_EPID_ERROR(sts);
  228. digest_size = EpidGetHashSize(ctx->hash_alg);
  229. digest = (uint8_t*)SAFE_ALLOC(digest_size);
  230. if (!digest) {
  231. sts = kEpidNoMemErr;
  232. break;
  233. }
  234. memcpy_S(digest + digest_size - sizeof(c_str), sizeof(c_str), &c_str,
  235. sizeof(c_str));
  236. sts = NewFfElement(Fp, &t3);
  237. BREAK_ON_EPID_ERROR(sts);
  238. sts = NewFfElement(Fp, &c);
  239. BREAK_ON_EPID_ERROR(sts);
  240. sts = ReadFfElement(Fp, &c_str, sizeof(c_str), c);
  241. BREAK_ON_EPID_ERROR(sts);
  242. // 7. The member computes sx = (rx + c * x) mod p.
  243. sts = FfMul(Fp, c, x, t3);
  244. BREAK_ON_EPID_ERROR(sts);
  245. sts = FfAdd(Fp, rx, t3, t3);
  246. BREAK_ON_EPID_ERROR(sts);
  247. sts = WriteFfElement(Fp, t3, &sig->sx, sizeof(sig->sx));
  248. BREAK_ON_EPID_ERROR(sts);
  249. // 8. The member computes sf = (rf + c * f) mod p.
  250. sts = Tpm2Sign(ctx->tpm2_ctx, digest, digest_size, *rf_ctr, NULL, t3);
  251. BREAK_ON_EPID_ERROR(sts);
  252. sts = WriteFfElement(Fp, t3, &sig->sf, sizeof(sig->sf));
  253. BREAK_ON_EPID_ERROR(sts);
  254. // 9. The member computes sa = (ra + c * a) mod p.
  255. sts = FfMul(Fp, c, a, t3);
  256. BREAK_ON_EPID_ERROR(sts);
  257. sts = FfAdd(Fp, ra, t3, t3);
  258. BREAK_ON_EPID_ERROR(sts);
  259. sts = WriteFfElement(Fp, t3, &sig->sa, sizeof(sig->sa));
  260. BREAK_ON_EPID_ERROR(sts);
  261. // 10. The member computes sb = (rb + c * b) mod p.
  262. sts = FfMul(Fp, c, b, t3);
  263. BREAK_ON_EPID_ERROR(sts);
  264. sts = FfAdd(Fp, rb, t3, t3);
  265. BREAK_ON_EPID_ERROR(sts);
  266. sts = WriteFfElement(Fp, t3, &sig->sb, sizeof(sig->sb));
  267. BREAK_ON_EPID_ERROR(sts);
  268. sig->B = commit_out.B;
  269. sig->K = commit_out.K;
  270. sig->T = commit_out.T;
  271. sig->c = c_str;
  272. sts = kEpidNoErr;
  273. } while (0);
  274. if (sts != kEpidNoErr) {
  275. (void)Tpm2ReleaseCounter(ctx->tpm2_ctx, (uint16_t)ctx->rf_ctr);
  276. (void)Tpm2ReleaseCounter(ctx->tpm2_ctx, curr_presig.rf_ctr);
  277. } else if (basename) {
  278. (void)Tpm2ReleaseCounter(ctx->tpm2_ctx, curr_presig.rf_ctr);
  279. }
  280. EpidZeroMemory(&curr_presig, sizeof(curr_presig));
  281. DeleteEcPoint(&B);
  282. DeleteEcPoint(&k);
  283. DeleteEcPoint(&t);
  284. DeleteEcPoint(&e);
  285. DeleteFfElement(&R2);
  286. DeleteFfElement(&p2y);
  287. DeleteFfElement(&t1);
  288. DeleteFfElement(&t2);
  289. DeleteFfElement(&a);
  290. DeleteFfElement(&b);
  291. DeleteFfElement(&rx);
  292. DeleteFfElement(&ra);
  293. DeleteFfElement(&rb);
  294. SAFE_FREE(p2x);
  295. DeleteFfElement(&t3);
  296. DeleteFfElement(&c);
  297. SAFE_FREE(digest);
  298. return sts;
  299. }