presig.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*############################################################################
  2. # Copyright 2016 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 EpidComputePreSig implementation.
  19. */
  20. #include "epid/member/src/context.h"
  21. /// Handle SDK Error with Break
  22. #define BREAK_ON_EPID_ERROR(ret) \
  23. if (kEpidNoErr != (ret)) { \
  24. break; \
  25. }
  26. /// Count of elements in array
  27. #define COUNT_OF(A) (sizeof(A) / sizeof((A)[0]))
  28. EpidStatus EpidComputePreSig(MemberCtx const* ctx,
  29. PreComputedSignature* precompsig) {
  30. EpidStatus res = kEpidNotImpl;
  31. EcPoint* B = NULL;
  32. EcPoint* K = NULL;
  33. EcPoint* T = NULL;
  34. EcPoint* R1 = NULL;
  35. FfElement* R2 = NULL;
  36. FfElement* a = NULL;
  37. FfElement* b = NULL;
  38. FfElement* rx = NULL;
  39. FfElement* rf = NULL;
  40. FfElement* ra = NULL;
  41. FfElement* rb = NULL;
  42. FfElement* t1 = NULL;
  43. FfElement* t2 = NULL;
  44. FfElement* f = NULL;
  45. if (!ctx || !precompsig) return kEpidBadArgErr;
  46. if (!ctx->epid2_params || !ctx->pub_key || !ctx->priv_key)
  47. return kEpidBadArgErr;
  48. do {
  49. // handy shorthands:
  50. EcGroup* G1 = ctx->epid2_params->G1;
  51. FiniteField* GT = ctx->epid2_params->GT;
  52. FiniteField* Fp = ctx->epid2_params->Fp;
  53. EcPoint* h2 = ctx->pub_key->h2;
  54. EcPoint* A = ctx->priv_key->A;
  55. FfElement* x = ctx->priv_key->x;
  56. BigNumStr f_str = {0};
  57. BigNumStr a_str = {0};
  58. BigNumStr t1_str = {0};
  59. BigNumStr rf_str = {0};
  60. BigNumStr t2_str = {0};
  61. BigNumStr ra_str = {0};
  62. static const BigNumStr one = {
  63. {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}};
  64. if (!G1 || !GT || !Fp || !h2 || !A || !x || !ctx->priv_key->f ||
  65. !ctx->e12 || !ctx->e22 || !ctx->e2w || !ctx->ea2) {
  66. res = kEpidBadArgErr;
  67. BREAK_ON_EPID_ERROR(res);
  68. }
  69. f = ctx->priv_key->f;
  70. // The following variables B, K, T, R1 (elements of G1), R2
  71. // (elements of GT), a, b, rx, rf, ra, rb, t1, t2 (256-bit
  72. // integers) are used.
  73. res = NewEcPoint(G1, &B);
  74. BREAK_ON_EPID_ERROR(res);
  75. res = NewEcPoint(G1, &K);
  76. BREAK_ON_EPID_ERROR(res);
  77. res = NewEcPoint(G1, &T);
  78. BREAK_ON_EPID_ERROR(res);
  79. res = NewEcPoint(G1, &R1);
  80. BREAK_ON_EPID_ERROR(res);
  81. res = NewFfElement(GT, &R2);
  82. BREAK_ON_EPID_ERROR(res);
  83. res = NewFfElement(Fp, &a);
  84. BREAK_ON_EPID_ERROR(res);
  85. res = NewFfElement(Fp, &b);
  86. BREAK_ON_EPID_ERROR(res);
  87. res = NewFfElement(Fp, &rx);
  88. BREAK_ON_EPID_ERROR(res);
  89. res = NewFfElement(Fp, &rf);
  90. BREAK_ON_EPID_ERROR(res);
  91. res = NewFfElement(Fp, &ra);
  92. BREAK_ON_EPID_ERROR(res);
  93. res = NewFfElement(Fp, &rb);
  94. BREAK_ON_EPID_ERROR(res);
  95. res = NewFfElement(Fp, &t1);
  96. BREAK_ON_EPID_ERROR(res);
  97. res = NewFfElement(Fp, &t2);
  98. BREAK_ON_EPID_ERROR(res);
  99. // 1. The member expects the pre-computation is done (e12, e22, e2w,
  100. // ea2). Refer to Section 3.5 for the computation of these
  101. // values.
  102. // 2. The member verifies gid in public key matches gid in private
  103. // key.
  104. // 3. The member computes B = G1.getRandom().
  105. res = EcGetRandom(G1, ctx->rnd_func, ctx->rnd_param, B);
  106. BREAK_ON_EPID_ERROR(res);
  107. // 4. The member computes K = G1.sscmExp(B, f).
  108. res = WriteFfElement(Fp, f, &f_str, sizeof(f_str));
  109. BREAK_ON_EPID_ERROR(res);
  110. res = EcExp(G1, B, &f_str, K);
  111. BREAK_ON_EPID_ERROR(res);
  112. // 5. The member chooses randomly an integers a from [1, p-1].
  113. res = FfGetRandom(Fp, &one, ctx->rnd_func, ctx->rnd_param, a);
  114. BREAK_ON_EPID_ERROR(res);
  115. // 6. The member computes T = G1.sscmExp(h2, a).
  116. res = WriteFfElement(Fp, a, &a_str, sizeof(a_str));
  117. BREAK_ON_EPID_ERROR(res);
  118. res = EcExp(G1, h2, &a_str, T);
  119. BREAK_ON_EPID_ERROR(res);
  120. // 7. The member computes T = G1.mul(T, A).
  121. res = EcMul(G1, T, A, T);
  122. BREAK_ON_EPID_ERROR(res);
  123. // 8. The member computes b = (a * x) mod p.
  124. res = FfMul(Fp, a, x, b);
  125. BREAK_ON_EPID_ERROR(res);
  126. // 9. The member chooses rx, rf, ra, rb randomly from [1, p-1].
  127. res = FfGetRandom(Fp, &one, ctx->rnd_func, ctx->rnd_param, rx);
  128. BREAK_ON_EPID_ERROR(res);
  129. res = FfGetRandom(Fp, &one, ctx->rnd_func, ctx->rnd_param, rf);
  130. BREAK_ON_EPID_ERROR(res);
  131. res = FfGetRandom(Fp, &one, ctx->rnd_func, ctx->rnd_param, ra);
  132. BREAK_ON_EPID_ERROR(res);
  133. res = FfGetRandom(Fp, &one, ctx->rnd_func, ctx->rnd_param, rb);
  134. BREAK_ON_EPID_ERROR(res);
  135. // 10. The member computes t1 = (- rx) mod p.
  136. res = FfNeg(Fp, rx, t1);
  137. BREAK_ON_EPID_ERROR(res);
  138. // 11. The member computes t2 = (rb - a * rx) mod p.
  139. res = FfMul(Fp, a, rx, t2);
  140. BREAK_ON_EPID_ERROR(res);
  141. res = FfNeg(Fp, t2, t2);
  142. BREAK_ON_EPID_ERROR(res);
  143. res = FfAdd(Fp, rb, t2, t2);
  144. BREAK_ON_EPID_ERROR(res);
  145. // 12. The member computes R1 = G1.sscmExp(B, rf).
  146. res = WriteFfElement(Fp, rf, &rf_str, sizeof(rf_str));
  147. BREAK_ON_EPID_ERROR(res);
  148. res = EcExp(G1, B, &rf_str, R1);
  149. BREAK_ON_EPID_ERROR(res);
  150. // 13. The member computes R2 = GT.sscmMultiExp(ea2, t1, e12, rf,
  151. // e22, t2, e2w, ra).
  152. res = WriteFfElement(Fp, t1, &t1_str, sizeof(t1_str));
  153. BREAK_ON_EPID_ERROR(res);
  154. res = WriteFfElement(Fp, t2, &t2_str, sizeof(t2_str));
  155. BREAK_ON_EPID_ERROR(res);
  156. res = WriteFfElement(Fp, ra, &ra_str, sizeof(ra_str));
  157. BREAK_ON_EPID_ERROR(res);
  158. {
  159. FfElement const* points[4];
  160. BigNumStr const* exponents[4];
  161. points[0] = ctx->ea2;
  162. points[1] = ctx->e12;
  163. points[2] = ctx->e22;
  164. points[3] = ctx->e2w;
  165. exponents[0] = &t1_str;
  166. exponents[1] = &rf_str;
  167. exponents[2] = &t2_str;
  168. exponents[3] = &ra_str;
  169. res = FfMultiExp(GT, points, exponents, COUNT_OF(points), R2);
  170. BREAK_ON_EPID_ERROR(res);
  171. }
  172. // 14. The member sets and outputs pre-sigma = (B, K, T, a, b, rx,
  173. // rf, ra, rb, R1, R2).
  174. res = WriteEcPoint(G1, B, &precompsig->B, sizeof(precompsig->B));
  175. BREAK_ON_EPID_ERROR(res);
  176. res = WriteEcPoint(G1, K, &precompsig->K, sizeof(precompsig->K));
  177. BREAK_ON_EPID_ERROR(res);
  178. res = WriteEcPoint(G1, T, &precompsig->T, sizeof(precompsig->T));
  179. BREAK_ON_EPID_ERROR(res);
  180. res = WriteFfElement(Fp, a, &precompsig->a, sizeof(precompsig->a));
  181. BREAK_ON_EPID_ERROR(res);
  182. res = WriteFfElement(Fp, b, &precompsig->b, sizeof(precompsig->b));
  183. BREAK_ON_EPID_ERROR(res);
  184. res = WriteFfElement(Fp, rx, &precompsig->rx, sizeof(precompsig->rx));
  185. BREAK_ON_EPID_ERROR(res);
  186. res = WriteFfElement(Fp, rf, &precompsig->rf, sizeof(precompsig->rf));
  187. BREAK_ON_EPID_ERROR(res);
  188. res = WriteFfElement(Fp, ra, &precompsig->ra, sizeof(precompsig->ra));
  189. BREAK_ON_EPID_ERROR(res);
  190. res = WriteFfElement(Fp, rb, &precompsig->rb, sizeof(precompsig->rb));
  191. BREAK_ON_EPID_ERROR(res);
  192. res = WriteEcPoint(G1, R1, &precompsig->R1, sizeof(precompsig->R1));
  193. BREAK_ON_EPID_ERROR(res);
  194. res = WriteFfElement(GT, R2, &precompsig->R2, sizeof(precompsig->R2));
  195. BREAK_ON_EPID_ERROR(res);
  196. // 15. The member stores pre-sigma in the secure storage of the
  197. // member.
  198. res = kEpidNoErr;
  199. } while (0);
  200. f = NULL;
  201. DeleteEcPoint(&B);
  202. DeleteEcPoint(&K);
  203. DeleteEcPoint(&T);
  204. DeleteEcPoint(&R1);
  205. DeleteFfElement(&R2);
  206. DeleteFfElement(&a);
  207. DeleteFfElement(&b);
  208. DeleteFfElement(&rx);
  209. DeleteFfElement(&rf);
  210. DeleteFfElement(&ra);
  211. DeleteFfElement(&rb);
  212. DeleteFfElement(&t1);
  213. DeleteFfElement(&t2);
  214. return (res);
  215. }