signbasic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 EpidSignBasic implementation.
  19. */
  20. #include <string.h> // memset
  21. #include "epid/common/src/stack.h"
  22. #include "epid/member/api.h"
  23. #include "epid/member/src/context.h"
  24. /// Handle SDK Error with Break
  25. #define BREAK_ON_EPID_ERROR(ret) \
  26. if (kEpidNoErr != (ret)) { \
  27. break; \
  28. }
  29. EpidStatus EpidSignBasic(MemberCtx const* ctx, void const* msg, size_t msg_len,
  30. void const* basename, size_t basename_len,
  31. BasicSignature* sig) {
  32. EpidStatus result = kEpidErr;
  33. // Values to be affected by basename
  34. EcPoint* B = NULL;
  35. EcPoint* K = NULL;
  36. EcPoint* R1 = NULL;
  37. // data from presig
  38. EcPoint* T = NULL;
  39. FfElement* a = NULL;
  40. FfElement* b = NULL;
  41. FfElement* rx = NULL;
  42. FfElement* rf = NULL;
  43. FfElement* ra = NULL;
  44. FfElement* rb = NULL;
  45. FfElement* R2 = NULL;
  46. // final calculatoin data
  47. FfElement* sx = NULL;
  48. FfElement* sf = NULL;
  49. FfElement* sa = NULL;
  50. FfElement* sb = NULL;
  51. FfElement* c_hash = NULL;
  52. // priv key data, need to clear after use
  53. BigNumStr f_str = {0};
  54. if (!ctx || !sig) {
  55. return kEpidBadArgErr;
  56. }
  57. if (!msg && (0 != msg_len)) {
  58. // if message is non-empty it must have both length and content
  59. return kEpidBadArgErr;
  60. }
  61. if (!basename && (0 != basename_len)) {
  62. // if basename is non-empty it must have both length and content
  63. return kEpidBadArgErr;
  64. }
  65. if (!ctx->epid2_params || !ctx->priv_key || !ctx->epid2_params->G1 ||
  66. !ctx->epid2_params->GT || !ctx->epid2_params->Fp || !ctx->priv_key->f) {
  67. return kEpidBadArgErr;
  68. }
  69. do {
  70. PreComputedSignature curr_presig;
  71. G1ElemStr B_str = {0};
  72. G1ElemStr K_str = {0};
  73. CommitValues commit_values = ctx->commit_values;
  74. // create all required elemnts
  75. result = NewEcPoint(ctx->epid2_params->G1, &B);
  76. BREAK_ON_EPID_ERROR(result);
  77. result = NewEcPoint(ctx->epid2_params->G1, &K);
  78. BREAK_ON_EPID_ERROR(result);
  79. result = NewEcPoint(ctx->epid2_params->G1, &R1);
  80. BREAK_ON_EPID_ERROR(result);
  81. result = NewEcPoint(ctx->epid2_params->G1, &T);
  82. BREAK_ON_EPID_ERROR(result);
  83. result = NewFfElement(ctx->epid2_params->GT, &R2);
  84. BREAK_ON_EPID_ERROR(result);
  85. result = NewFfElement(ctx->epid2_params->Fp, &sx);
  86. BREAK_ON_EPID_ERROR(result);
  87. result = NewFfElement(ctx->epid2_params->Fp, &sf);
  88. BREAK_ON_EPID_ERROR(result);
  89. result = NewFfElement(ctx->epid2_params->Fp, &sa);
  90. BREAK_ON_EPID_ERROR(result);
  91. result = NewFfElement(ctx->epid2_params->Fp, &sb);
  92. BREAK_ON_EPID_ERROR(result);
  93. result = NewFfElement(ctx->epid2_params->Fp, &c_hash);
  94. BREAK_ON_EPID_ERROR(result);
  95. result = NewFfElement(ctx->epid2_params->Fp, &a);
  96. BREAK_ON_EPID_ERROR(result);
  97. result = NewFfElement(ctx->epid2_params->Fp, &b);
  98. BREAK_ON_EPID_ERROR(result);
  99. result = NewFfElement(ctx->epid2_params->Fp, &rx);
  100. BREAK_ON_EPID_ERROR(result);
  101. result = NewFfElement(ctx->epid2_params->Fp, &rf);
  102. BREAK_ON_EPID_ERROR(result);
  103. result = NewFfElement(ctx->epid2_params->Fp, &ra);
  104. BREAK_ON_EPID_ERROR(result);
  105. result = NewFfElement(ctx->epid2_params->Fp, &rb);
  106. BREAK_ON_EPID_ERROR(result);
  107. if (StackGetSize(ctx->presigs)) {
  108. // Use existing pre-computed signature
  109. if (!StackPopN(ctx->presigs, 1, &curr_presig)) {
  110. result = kEpidErr;
  111. break;
  112. }
  113. } else {
  114. // generate a new pre-computed signature
  115. result = EpidComputePreSig(ctx, &curr_presig);
  116. BREAK_ON_EPID_ERROR(result);
  117. }
  118. // 3. If the pre-computed signature pre-sigma exists, the member
  119. // loads (B, K, T, a, b, rx, rf, ra, rb, R1, R2) from
  120. // pre-sigma. Refer to Section 4.4 for the computation of
  121. // these values.
  122. result = ReadEcPoint(ctx->epid2_params->G1, &curr_presig.B,
  123. sizeof(curr_presig.B), B);
  124. BREAK_ON_EPID_ERROR(result);
  125. result = ReadEcPoint(ctx->epid2_params->G1, &curr_presig.K,
  126. sizeof(curr_presig.K), K);
  127. BREAK_ON_EPID_ERROR(result);
  128. result = ReadEcPoint(ctx->epid2_params->G1, &curr_presig.T,
  129. sizeof(curr_presig.T), T);
  130. BREAK_ON_EPID_ERROR(result);
  131. result = ReadFfElement(ctx->epid2_params->Fp, &curr_presig.a,
  132. sizeof(curr_presig.a), a);
  133. BREAK_ON_EPID_ERROR(result);
  134. result = ReadFfElement(ctx->epid2_params->Fp, &curr_presig.b,
  135. sizeof(curr_presig.b), b);
  136. BREAK_ON_EPID_ERROR(result);
  137. result = ReadFfElement(ctx->epid2_params->Fp, &curr_presig.rx,
  138. sizeof(curr_presig.rx), rx);
  139. BREAK_ON_EPID_ERROR(result);
  140. result = ReadFfElement(ctx->epid2_params->Fp, &curr_presig.rf,
  141. sizeof(curr_presig.rf), rf);
  142. BREAK_ON_EPID_ERROR(result);
  143. result = ReadFfElement(ctx->epid2_params->Fp, &curr_presig.ra,
  144. sizeof(curr_presig.ra), ra);
  145. BREAK_ON_EPID_ERROR(result);
  146. result = ReadFfElement(ctx->epid2_params->Fp, &curr_presig.rb,
  147. sizeof(curr_presig.rb), rb);
  148. BREAK_ON_EPID_ERROR(result);
  149. result = ReadEcPoint(ctx->epid2_params->G1, &curr_presig.R1,
  150. sizeof(curr_presig.R1), R1);
  151. BREAK_ON_EPID_ERROR(result);
  152. result = ReadFfElement(ctx->epid2_params->GT, &curr_presig.R2,
  153. sizeof(curr_presig.R2), R2);
  154. BREAK_ON_EPID_ERROR(result);
  155. if (basename) {
  156. // If basename is provided, the member does the following:
  157. // make sure basename is registered/allowed
  158. if (!ContainsBasename(ctx->allowed_basenames, basename, basename_len)) {
  159. result = kEpidBadArgErr;
  160. break;
  161. } else {
  162. // basename valid, can modify parameters
  163. // a. The member computes B = G1.hash(bsn).
  164. result = EcHash(ctx->epid2_params->G1, basename, basename_len,
  165. ctx->hash_alg, B);
  166. BREAK_ON_EPID_ERROR(result);
  167. // b. The member computes K = G1.sscmExp(B, f), where B comes
  168. // from step a.
  169. result = WriteFfElement(ctx->epid2_params->Fp, ctx->priv_key->f, &f_str,
  170. sizeof(f_str));
  171. BREAK_ON_EPID_ERROR(result);
  172. result = EcSscmExp(ctx->epid2_params->G1, B, &f_str, K);
  173. BREAK_ON_EPID_ERROR(result);
  174. // c. The member computes R1 = G1.sscmExp(B, rf), where B comes
  175. // from step a.
  176. result = EcSscmExp(ctx->epid2_params->G1, B,
  177. (const BigNumStr*)&curr_presig.rf, R1);
  178. BREAK_ON_EPID_ERROR(result);
  179. // d. The member over-writes the B, K, and R1 values.
  180. }
  181. }
  182. // 5. The member computes t3 = Fp.hash(p || g1 || g2 || h1 || h2
  183. // || w || B || K || T || R1 || R2). Refer to Section 7.1 for
  184. // hash operation over a prime field.
  185. // 6. The member computes c = Fp.hash(t3 || m).
  186. result = WriteEcPoint(ctx->epid2_params->G1, B, &B_str, sizeof(B_str));
  187. BREAK_ON_EPID_ERROR(result);
  188. result = WriteEcPoint(ctx->epid2_params->G1, K, &K_str, sizeof(K_str));
  189. BREAK_ON_EPID_ERROR(result);
  190. result = SetCalculatedCommitValues(&B_str, &K_str, &curr_presig.T, R1,
  191. ctx->epid2_params->G1, R2,
  192. ctx->epid2_params->GT, &commit_values);
  193. BREAK_ON_EPID_ERROR(result);
  194. result = CalculateCommitmentHash(&commit_values, ctx->epid2_params->Fp,
  195. ctx->hash_alg, msg, msg_len, c_hash);
  196. BREAK_ON_EPID_ERROR(result);
  197. // 7. The member computes sx = (rx + c * x) mod p.
  198. result = FfMul(ctx->epid2_params->Fp, c_hash, ctx->priv_key->x, sx);
  199. BREAK_ON_EPID_ERROR(result);
  200. result = FfAdd(ctx->epid2_params->Fp, rx, sx, sx);
  201. // 8. The member computes sf = (rf + c * f) mod p.
  202. result = FfMul(ctx->epid2_params->Fp, c_hash, ctx->priv_key->f, sf);
  203. BREAK_ON_EPID_ERROR(result);
  204. result = FfAdd(ctx->epid2_params->Fp, rf, sf, sf);
  205. BREAK_ON_EPID_ERROR(result);
  206. // 9. The member computes sa = (ra + c * a) mod p.
  207. result = FfMul(ctx->epid2_params->Fp, c_hash, a, sa);
  208. BREAK_ON_EPID_ERROR(result);
  209. result = FfAdd(ctx->epid2_params->Fp, ra, sa, sa);
  210. BREAK_ON_EPID_ERROR(result);
  211. // 10. The member computes sb = (rb + c * b) mod p.
  212. result = FfMul(ctx->epid2_params->Fp, c_hash, b, sb);
  213. BREAK_ON_EPID_ERROR(result);
  214. result = FfAdd(ctx->epid2_params->Fp, rb, sb, sb);
  215. BREAK_ON_EPID_ERROR(result);
  216. // 11. The member sets sigma0 = (B, K, T, c, sx, sf, sa, sb).
  217. result = WriteEcPoint(ctx->epid2_params->G1, B, &sig->B, sizeof(sig->B));
  218. BREAK_ON_EPID_ERROR(result);
  219. result = WriteEcPoint(ctx->epid2_params->G1, K, &sig->K, sizeof(sig->K));
  220. BREAK_ON_EPID_ERROR(result);
  221. result = WriteEcPoint(ctx->epid2_params->G1, T, &sig->T, sizeof(sig->T));
  222. BREAK_ON_EPID_ERROR(result);
  223. result =
  224. WriteFfElement(ctx->epid2_params->Fp, c_hash, &sig->c, sizeof(sig->c));
  225. BREAK_ON_EPID_ERROR(result);
  226. result =
  227. WriteFfElement(ctx->epid2_params->Fp, sx, &sig->sx, sizeof(sig->sx));
  228. BREAK_ON_EPID_ERROR(result);
  229. result =
  230. WriteFfElement(ctx->epid2_params->Fp, sf, &sig->sf, sizeof(sig->sf));
  231. BREAK_ON_EPID_ERROR(result);
  232. result =
  233. WriteFfElement(ctx->epid2_params->Fp, sa, &sig->sa, sizeof(sig->sa));
  234. BREAK_ON_EPID_ERROR(result);
  235. result =
  236. WriteFfElement(ctx->epid2_params->Fp, sb, &sig->sb, sizeof(sig->sb));
  237. BREAK_ON_EPID_ERROR(result);
  238. result = kEpidNoErr;
  239. } while (0);
  240. // remove all data
  241. DeleteEcPoint(&B);
  242. DeleteEcPoint(&K);
  243. DeleteEcPoint(&R1);
  244. DeleteEcPoint(&T);
  245. DeleteFfElement(&R2);
  246. DeleteFfElement(&sx);
  247. DeleteFfElement(&sf);
  248. DeleteFfElement(&sa);
  249. DeleteFfElement(&sb);
  250. DeleteFfElement(&c_hash);
  251. DeleteFfElement(&a);
  252. DeleteFfElement(&b);
  253. DeleteFfElement(&rx);
  254. DeleteFfElement(&rf);
  255. DeleteFfElement(&ra);
  256. DeleteFfElement(&rb);
  257. return result;
  258. }