verifybasic.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. /*!
  17. * \file
  18. * \brief VerifyBasicSig implementation.
  19. */
  20. #include "epid/common/src/memory.h"
  21. #include "epid/verifier/api.h"
  22. #include "epid/verifier/src/context.h"
  23. /// Handle SDK Error with Break
  24. #define BREAK_ON_EPID_ERROR(ret) \
  25. if (kEpidNoErr != (ret)) { \
  26. break; \
  27. }
  28. /// Count of elements in array
  29. #define COUNT_OF(A) (sizeof(A) / sizeof((A)[0]))
  30. EpidStatus EpidVerifyBasicSig(VerifierCtx const* ctx, BasicSignature const* sig,
  31. void const* msg, size_t msg_len) {
  32. EpidStatus res = kEpidNotImpl;
  33. EcPoint* B = NULL;
  34. EcPoint* K = NULL;
  35. EcPoint* T = NULL;
  36. EcPoint* R1 = NULL;
  37. EcPoint* t4 = NULL;
  38. EcPoint* t1 = NULL;
  39. FfElement* R2 = NULL;
  40. FfElement* t2 = NULL;
  41. FfElement* c = NULL;
  42. FfElement* sx = NULL;
  43. FfElement* sf = NULL;
  44. FfElement* sa = NULL;
  45. FfElement* sb = NULL;
  46. FfElement* nc = NULL;
  47. FfElement* nsx = NULL;
  48. FfElement* c_hash = NULL;
  49. if (!ctx || !sig) return kEpidBadArgErr;
  50. if (!msg && (0 != msg_len)) {
  51. // if message is non-empty it must have both length and content
  52. return kEpidBadArgErr;
  53. }
  54. if (!ctx->epid2_params || !ctx->pub_key) return kEpidBadArgErr;
  55. do {
  56. bool cmp_result = false;
  57. BigNumStr c_str = {0};
  58. BigNumStr sf_str = {0};
  59. BigNumStr nc_str = {0};
  60. BigNumStr nsx_str = {0};
  61. BigNumStr sb_str = {0};
  62. BigNumStr sa_str = {0};
  63. // handy shorthands:
  64. EcGroup* G1 = ctx->epid2_params->G1;
  65. EcGroup* G2 = ctx->epid2_params->G2;
  66. FiniteField* GT = ctx->epid2_params->GT;
  67. FiniteField* Fp = ctx->epid2_params->Fp;
  68. EcPoint* g1 = ctx->epid2_params->g1;
  69. EcPoint* g2 = ctx->epid2_params->g2;
  70. EcPoint* w = ctx->pub_key->w;
  71. CommitValues commit_values = ctx->commit_values;
  72. EcPoint* basename_hash = ctx->basename_hash;
  73. if (!G1 || !G2 || !GT || !Fp || !g1 || !g2 || !w) {
  74. res = kEpidBadArgErr;
  75. BREAK_ON_EPID_ERROR(res);
  76. }
  77. // The following variables B, K, T, R1, t4 (elements of G1), t1
  78. // (element of G2), R2, t2 (elements of GT), c, sx, sf, sa, sb,
  79. // nc, nsx, t3 (256-bit integers) are used.
  80. res = NewEcPoint(G1, &B);
  81. BREAK_ON_EPID_ERROR(res);
  82. res = NewEcPoint(G1, &K);
  83. BREAK_ON_EPID_ERROR(res);
  84. res = NewEcPoint(G1, &T);
  85. BREAK_ON_EPID_ERROR(res);
  86. res = NewEcPoint(G1, &R1);
  87. BREAK_ON_EPID_ERROR(res);
  88. res = NewEcPoint(G1, &t4);
  89. BREAK_ON_EPID_ERROR(res);
  90. res = NewEcPoint(G2, &t1);
  91. BREAK_ON_EPID_ERROR(res);
  92. res = NewFfElement(GT, &R2);
  93. BREAK_ON_EPID_ERROR(res);
  94. res = NewFfElement(GT, &t2);
  95. BREAK_ON_EPID_ERROR(res);
  96. res = NewFfElement(Fp, &c);
  97. BREAK_ON_EPID_ERROR(res);
  98. res = NewFfElement(Fp, &sx);
  99. BREAK_ON_EPID_ERROR(res);
  100. res = NewFfElement(Fp, &sf);
  101. BREAK_ON_EPID_ERROR(res);
  102. res = NewFfElement(Fp, &sa);
  103. BREAK_ON_EPID_ERROR(res);
  104. res = NewFfElement(Fp, &sb);
  105. BREAK_ON_EPID_ERROR(res);
  106. res = NewFfElement(Fp, &nc);
  107. BREAK_ON_EPID_ERROR(res);
  108. res = NewFfElement(Fp, &nsx);
  109. BREAK_ON_EPID_ERROR(res);
  110. res = NewFfElement(Fp, &c_hash);
  111. BREAK_ON_EPID_ERROR(res);
  112. // 1. The verifier expect pre-computation is done (e12, e22, e2w,
  113. // eg12). Refer to Section 3.6 for the computation of these
  114. // values.
  115. // 2. The verifier verifies the basic signature sigma0 as follows:
  116. // a. The verifier verifies G1.inGroup(B) = true.
  117. res = ReadEcPoint(G1, &(sig->B), sizeof(sig->B), B);
  118. if (kEpidNoErr != res) {
  119. if (kEpidBadArgErr == res) {
  120. res = kEpidSigInvalid;
  121. }
  122. break;
  123. }
  124. // b. The verifier verifies that G1.isIdentity(B) is false.
  125. res = EcIsIdentity(G1, B, &cmp_result);
  126. BREAK_ON_EPID_ERROR(res);
  127. if (cmp_result != false) {
  128. res = kEpidSigInvalid;
  129. break;
  130. }
  131. // c. If bsn is provided, the verifier verifies B =
  132. // G1.hash(bsn).
  133. if (basename_hash) {
  134. res = EcIsEqual(G1, basename_hash, B, &cmp_result);
  135. BREAK_ON_EPID_ERROR(res);
  136. if (cmp_result != true) {
  137. res = kEpidSigInvalid;
  138. break;
  139. }
  140. }
  141. // d. The verifier verifies G1.inGroup(K) = true.
  142. res = ReadEcPoint(G1, &(sig->K), sizeof(sig->K), K);
  143. if (kEpidNoErr != res) {
  144. if (kEpidBadArgErr == res) {
  145. res = kEpidSigInvalid;
  146. }
  147. break;
  148. }
  149. // e. The verifier verifies G1.inGroup(T) = true.
  150. res = ReadEcPoint(G1, &(sig->T), sizeof(sig->T), T);
  151. if (kEpidNoErr != res) {
  152. if (kEpidBadArgErr == res) {
  153. res = kEpidSigInvalid;
  154. }
  155. break;
  156. }
  157. // f. The verifier verifies c, sx, sf, sa, sb in [0, p-1].
  158. res = ReadFfElement(Fp, &(sig->c), sizeof(sig->c), c);
  159. if (kEpidNoErr != res) {
  160. if (kEpidBadArgErr == res) {
  161. res = kEpidSigInvalid;
  162. }
  163. break;
  164. }
  165. res = WriteFfElement(Fp, c, &c_str, sizeof(c_str));
  166. BREAK_ON_EPID_ERROR(res);
  167. res = ReadFfElement(Fp, &(sig->sx), sizeof(sig->sx), sx);
  168. if (kEpidNoErr != res) {
  169. if (kEpidBadArgErr == res) {
  170. res = kEpidSigInvalid;
  171. }
  172. break;
  173. }
  174. res = ReadFfElement(Fp, &(sig->sf), sizeof(sig->sf), sf);
  175. if (kEpidNoErr != res) {
  176. if (kEpidBadArgErr == res) {
  177. res = kEpidSigInvalid;
  178. }
  179. break;
  180. }
  181. res = ReadFfElement(Fp, &(sig->sa), sizeof(sig->sa), sa);
  182. if (kEpidNoErr != res) {
  183. if (kEpidBadArgErr == res) {
  184. res = kEpidSigInvalid;
  185. }
  186. break;
  187. }
  188. res = ReadFfElement(Fp, &(sig->sb), sizeof(sig->sb), sb);
  189. if (kEpidNoErr != res) {
  190. if (kEpidBadArgErr == res) {
  191. res = kEpidSigInvalid;
  192. }
  193. break;
  194. }
  195. // g. The verifier computes nc = (-c) mod p.
  196. res = FfNeg(Fp, c, nc);
  197. BREAK_ON_EPID_ERROR(res);
  198. // h. The verifier computes nsx = (-sx) mod p.
  199. res = FfNeg(Fp, sx, nsx);
  200. BREAK_ON_EPID_ERROR(res);
  201. // i. The verifier computes R1 = G1.multiExp(B, sf, K, nc).
  202. res = WriteFfElement(Fp, sf, &sf_str, sizeof(sf_str));
  203. BREAK_ON_EPID_ERROR(res);
  204. res = WriteFfElement(Fp, nc, &nc_str, sizeof(nc_str));
  205. BREAK_ON_EPID_ERROR(res);
  206. {
  207. EcPoint const* points[2];
  208. BigNumStr const* exponents[2];
  209. points[0] = B;
  210. points[1] = K;
  211. exponents[0] = &sf_str;
  212. exponents[1] = &nc_str;
  213. res = EcMultiExp(G1, points, exponents, COUNT_OF(points), R1);
  214. BREAK_ON_EPID_ERROR(res);
  215. }
  216. // j. The verifier computes t1 = G2.multiExp(g2, nsx, w, nc).
  217. res = WriteFfElement(Fp, nsx, &nsx_str, sizeof(nsx_str));
  218. BREAK_ON_EPID_ERROR(res);
  219. {
  220. EcPoint const* points[2];
  221. BigNumStr const* exponents[2];
  222. points[0] = g2;
  223. points[1] = w;
  224. exponents[0] = &nsx_str;
  225. exponents[1] = &nc_str;
  226. res = EcMultiExp(G2, points, exponents, COUNT_OF(points), t1);
  227. BREAK_ON_EPID_ERROR(res);
  228. }
  229. // k. The verifier computes R2 = pairing(T, t1).
  230. res = Pairing(ctx->epid2_params->pairing_state, T, t1, R2);
  231. BREAK_ON_EPID_ERROR(res);
  232. // l. The verifier compute t2 = GT.multiExp(e12, sf, e22, sb,
  233. // e2w, sa, eg12, c).
  234. res = WriteFfElement(Fp, sb, &sb_str, sizeof(sb_str));
  235. BREAK_ON_EPID_ERROR(res);
  236. res = WriteFfElement(Fp, sa, &sa_str, sizeof(sa_str));
  237. BREAK_ON_EPID_ERROR(res);
  238. {
  239. FfElement const* points[4];
  240. BigNumStr const* exponents[4];
  241. points[0] = ctx->e12;
  242. points[1] = ctx->e22;
  243. points[2] = ctx->e2w;
  244. points[3] = ctx->eg12;
  245. exponents[0] = &sf_str;
  246. exponents[1] = &sb_str;
  247. exponents[2] = &sa_str;
  248. exponents[3] = &c_str;
  249. res = FfMultiExp(GT, points, exponents, COUNT_OF(points), t2);
  250. BREAK_ON_EPID_ERROR(res);
  251. }
  252. // m. The verifier compute R2 = GT.mul(R2, t2).
  253. res = FfMul(GT, R2, t2, R2);
  254. BREAK_ON_EPID_ERROR(res);
  255. // n. The verifier compute t3 = Fp.hash(p || g1 || g2 || h1 ||
  256. // h2 || w || B || K || T || R1 || R2).
  257. // o. The verifier verifies c = Fp.hash(t3 || m).
  258. res = SetCalculatedCommitValues(&sig->B, &sig->K, &sig->T, R1, G1, R2, GT,
  259. &commit_values);
  260. BREAK_ON_EPID_ERROR(res);
  261. res = CalculateCommitmentHash(&commit_values, Fp, ctx->hash_alg, msg,
  262. msg_len, c_hash);
  263. BREAK_ON_EPID_ERROR(res);
  264. res = FfIsEqual(Fp, c, c_hash, &cmp_result);
  265. BREAK_ON_EPID_ERROR(res);
  266. if (cmp_result != true) {
  267. // p. If any of the above verifications fails, the verifier
  268. // aborts and outputs 1.
  269. res = kEpidSigInvalid;
  270. break;
  271. }
  272. res = kEpidNoErr;
  273. } while (0);
  274. DeleteEcPoint(&B);
  275. DeleteEcPoint(&K);
  276. DeleteEcPoint(&T);
  277. DeleteEcPoint(&R1);
  278. DeleteEcPoint(&t4);
  279. DeleteEcPoint(&t1);
  280. DeleteFfElement(&R2);
  281. DeleteFfElement(&t2);
  282. DeleteFfElement(&c);
  283. DeleteFfElement(&sx);
  284. DeleteFfElement(&sf);
  285. DeleteFfElement(&sa);
  286. DeleteFfElement(&sb);
  287. DeleteFfElement(&nc);
  288. DeleteFfElement(&nsx);
  289. DeleteFfElement(&c_hash);
  290. return (res);
  291. }