nr_prove.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 EpidNrProve implementation.
  19. */
  20. #include "epid/common/src/memory.h"
  21. #include "epid/member/api.h"
  22. #include "epid/member/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. #pragma pack(1)
  31. /// Storage for values to create commitment in NrProve algorithm
  32. typedef struct NrVerifyCommitValues {
  33. BigNumStr p; //!< A large prime (256-bit)
  34. G1ElemStr g1; //!< Generator of G1 (512-bit)
  35. G1ElemStr b; //!< (element of G1): part of basic signature Sigma0
  36. G1ElemStr k; //!< (element of G1): part of basic signature Sigma0
  37. G1ElemStr bp; //!< (element of G1): one entry in SigRL
  38. G1ElemStr kp; //!< (element of G1): one entry in SigRL
  39. G1ElemStr t; //!< element of G1
  40. G1ElemStr r1; //!< element of G1
  41. G1ElemStr r2; //!< element of G1
  42. uint8_t msg[1]; //!< message
  43. } NrVerifyCommitValues;
  44. #pragma pack()
  45. EpidStatus EpidNrProve(MemberCtx const* ctx, void const* msg, size_t msg_len,
  46. BasicSignature const* sig, SigRlEntry const* sigrl_entry,
  47. NrProof* proof) {
  48. EpidStatus res = kEpidErr;
  49. NrVerifyCommitValues* commit_values = NULL;
  50. size_t const commit_len = sizeof(*commit_values) - 1 + msg_len;
  51. EcPoint* T = NULL;
  52. EcPoint* R1 = NULL;
  53. EcPoint* R2 = NULL;
  54. FfElement* mu = NULL;
  55. FfElement* nu = NULL;
  56. FfElement* rmu = NULL;
  57. FfElement* rnu = NULL;
  58. FfElement* c = NULL;
  59. FfElement* smu = NULL;
  60. FfElement* snu = NULL;
  61. EcPoint* B = NULL;
  62. EcPoint* K = NULL;
  63. EcPoint* rlB = NULL;
  64. EcPoint* rlK = NULL;
  65. FfElement const* f = NULL;
  66. if (!ctx || (0 != msg_len && !msg) || !sig || !sigrl_entry || !proof)
  67. return kEpidBadArgErr;
  68. if (msg_len > ((SIZE_MAX - sizeof(*commit_values)) + 1))
  69. return kEpidBadArgErr;
  70. if (!ctx->epid2_params || !ctx->priv_key) return kEpidBadArgErr;
  71. do {
  72. bool is_identity = false;
  73. BigNumStr mu_str = {0};
  74. BigNumStr nu_str = {0};
  75. BigNumStr rmu_str = {0};
  76. BigNumStr rnu_str = {0};
  77. BitSupplier rnd_func = ctx->rnd_func;
  78. void* rnd_param = ctx->rnd_param;
  79. FiniteField* Fp = ctx->epid2_params->Fp;
  80. EcGroup* G1 = ctx->epid2_params->G1;
  81. static const BigNumStr one = {
  82. {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}};
  83. // Check required parameters
  84. if (!ctx->priv_key->f || !rnd_func || !Fp || !G1) return kEpidBadArgErr;
  85. f = ctx->priv_key->f;
  86. commit_values = SAFE_ALLOC(commit_len);
  87. if (!commit_values) {
  88. res = kEpidMemAllocErr;
  89. break;
  90. }
  91. // The following variables T, R1, R2 (elements of G1), and mu, nu,
  92. // rmu, rnu, c, smu, snu (256-bit integers) are used.
  93. res = NewEcPoint(G1, &T);
  94. BREAK_ON_EPID_ERROR(res);
  95. res = NewEcPoint(G1, &R1);
  96. BREAK_ON_EPID_ERROR(res);
  97. res = NewEcPoint(G1, &R2);
  98. BREAK_ON_EPID_ERROR(res);
  99. res = NewFfElement(Fp, &mu);
  100. BREAK_ON_EPID_ERROR(res);
  101. res = NewFfElement(Fp, &nu);
  102. BREAK_ON_EPID_ERROR(res);
  103. res = NewFfElement(Fp, &rmu);
  104. BREAK_ON_EPID_ERROR(res);
  105. res = NewFfElement(Fp, &rnu);
  106. BREAK_ON_EPID_ERROR(res);
  107. res = NewFfElement(Fp, &c);
  108. BREAK_ON_EPID_ERROR(res);
  109. res = NewFfElement(Fp, &smu);
  110. BREAK_ON_EPID_ERROR(res);
  111. res = NewFfElement(Fp, &snu);
  112. BREAK_ON_EPID_ERROR(res);
  113. res = NewEcPoint(G1, &B);
  114. BREAK_ON_EPID_ERROR(res);
  115. res = NewEcPoint(G1, &K);
  116. BREAK_ON_EPID_ERROR(res);
  117. res = NewEcPoint(G1, &rlB);
  118. BREAK_ON_EPID_ERROR(res);
  119. res = NewEcPoint(G1, &rlK);
  120. BREAK_ON_EPID_ERROR(res);
  121. res = ReadEcPoint(G1, (const uint8_t*)&(sig->B), sizeof(sig->B), B);
  122. BREAK_ON_EPID_ERROR(res);
  123. res = ReadEcPoint(G1, (const uint8_t*)&(sig->K), sizeof(sig->K), K);
  124. BREAK_ON_EPID_ERROR(res);
  125. res = ReadEcPoint(G1, (const uint8_t*)&(sigrl_entry->b),
  126. sizeof(sigrl_entry->b), rlB);
  127. BREAK_ON_EPID_ERROR(res);
  128. res = ReadEcPoint(G1, (const uint8_t*)&(sigrl_entry->k),
  129. sizeof(sigrl_entry->k), rlK);
  130. BREAK_ON_EPID_ERROR(res);
  131. // 1. The member chooses random mu from [1, p-1].
  132. res = FfGetRandom(Fp, &one, rnd_func, rnd_param, mu);
  133. BREAK_ON_EPID_ERROR(res);
  134. // 2. The member computes nu = (- f * mu) mod p.
  135. res = FfMul(Fp, mu, f, nu);
  136. BREAK_ON_EPID_ERROR(res);
  137. res = FfNeg(Fp, nu, nu);
  138. BREAK_ON_EPID_ERROR(res);
  139. // 3. The member computes T = G1.sscmMultiExp(K', mu, B', nu).
  140. res = WriteFfElement(Fp, mu, (uint8_t*)&mu_str, sizeof(mu_str));
  141. BREAK_ON_EPID_ERROR(res);
  142. res = WriteFfElement(Fp, nu, (uint8_t*)&nu_str, sizeof(nu_str));
  143. BREAK_ON_EPID_ERROR(res);
  144. {
  145. EcPoint const* points[2];
  146. BigNumStr const* exponents[2];
  147. points[0] = rlK;
  148. points[1] = rlB;
  149. exponents[0] = &mu_str;
  150. exponents[1] = &nu_str;
  151. res = EcSscmMultiExp(G1, points, exponents, COUNT_OF(points), T);
  152. BREAK_ON_EPID_ERROR(res);
  153. }
  154. // 4. The member chooses rmu, rnu randomly from [1, p-1].
  155. res = FfGetRandom(Fp, &one, rnd_func, rnd_param, rmu);
  156. BREAK_ON_EPID_ERROR(res);
  157. res = FfGetRandom(Fp, &one, rnd_func, rnd_param, rnu);
  158. BREAK_ON_EPID_ERROR(res);
  159. // 5. The member computes R1 = G1.sscmMultiExp(K, rmu, B, rnu).
  160. res = WriteFfElement(Fp, rmu, (uint8_t*)&rmu_str, sizeof(rmu_str));
  161. BREAK_ON_EPID_ERROR(res);
  162. res = WriteFfElement(Fp, rnu, (uint8_t*)&rnu_str, sizeof(rnu_str));
  163. BREAK_ON_EPID_ERROR(res);
  164. {
  165. EcPoint const* points[2];
  166. BigNumStr const* exponents[2];
  167. points[0] = K;
  168. points[1] = B;
  169. exponents[0] = &rmu_str;
  170. exponents[1] = &rnu_str;
  171. res = EcSscmMultiExp(G1, points, exponents, COUNT_OF(points), R1);
  172. BREAK_ON_EPID_ERROR(res);
  173. }
  174. // 6. The member computes R2 = G1.sscmMultiExp(K', rmu, B', rnu).
  175. {
  176. EcPoint const* points[2];
  177. BigNumStr const* exponents[2];
  178. points[0] = rlK;
  179. points[1] = rlB;
  180. exponents[0] = &rmu_str;
  181. exponents[1] = &rnu_str;
  182. res = EcSscmMultiExp(G1, points, exponents, COUNT_OF(points), R2);
  183. BREAK_ON_EPID_ERROR(res);
  184. }
  185. // 7. The member computes c = Fp.hash(p || g1 || B || K || B' ||
  186. // K' || T || R1 || R2 || m). Refer to Section 7.1 for hash
  187. // operation over a prime field.
  188. // commit_values is allocated such that there are msg_len bytes available
  189. // starting at commit_values->msg
  190. if (msg) {
  191. // Memory copy is used to copy a message of variable length
  192. if (0 != memcpy_S(&commit_values->msg[0], msg_len, msg, msg_len)) {
  193. res = kEpidBadArgErr;
  194. break;
  195. }
  196. }
  197. commit_values->p = ctx->commit_values.p;
  198. commit_values->g1 = ctx->commit_values.g1;
  199. commit_values->b = sig->B;
  200. commit_values->k = sig->K;
  201. commit_values->bp = sigrl_entry->b;
  202. commit_values->kp = sigrl_entry->k;
  203. res = WriteEcPoint(G1, T, (uint8_t*)&commit_values->t,
  204. sizeof(commit_values->t));
  205. BREAK_ON_EPID_ERROR(res);
  206. res = WriteEcPoint(G1, R1, (uint8_t*)&commit_values->r1,
  207. sizeof(commit_values->r1));
  208. BREAK_ON_EPID_ERROR(res);
  209. res = WriteEcPoint(G1, R2, (uint8_t*)&commit_values->r2,
  210. sizeof(commit_values->r2));
  211. BREAK_ON_EPID_ERROR(res);
  212. res = FfHash(Fp, (uint8_t*)commit_values, commit_len, ctx->hash_alg, c);
  213. BREAK_ON_EPID_ERROR(res);
  214. // 8. The member computes smu = (rmu + c * mu) mod p.
  215. res = FfMul(Fp, c, mu, smu);
  216. BREAK_ON_EPID_ERROR(res);
  217. res = FfAdd(Fp, rmu, smu, smu);
  218. BREAK_ON_EPID_ERROR(res);
  219. // 9. The member computes snu = (rnu + c * nu) mod p.
  220. res = FfMul(Fp, c, nu, snu);
  221. BREAK_ON_EPID_ERROR(res);
  222. res = FfAdd(Fp, rnu, snu, snu);
  223. BREAK_ON_EPID_ERROR(res);
  224. // 10. The member outputs sigma = (T, c, smu, snu), a non-revoked
  225. // proof. If G1.is_identity(T) = true, the member also outputs
  226. // "failed".
  227. proof->T = commit_values->t;
  228. res = WriteFfElement(Fp, c, (uint8_t*)&proof->c, sizeof(proof->c));
  229. BREAK_ON_EPID_ERROR(res);
  230. res = WriteFfElement(Fp, smu, (uint8_t*)&proof->smu, sizeof(proof->smu));
  231. BREAK_ON_EPID_ERROR(res);
  232. res = WriteFfElement(Fp, snu, (uint8_t*)&proof->snu, sizeof(proof->snu));
  233. BREAK_ON_EPID_ERROR(res);
  234. res = EcIsIdentity(G1, T, &is_identity);
  235. BREAK_ON_EPID_ERROR(res);
  236. if (is_identity) {
  237. res = kEpidSigRevokedInSigRl;
  238. BREAK_ON_EPID_ERROR(res);
  239. }
  240. res = kEpidNoErr;
  241. } while (0);
  242. f = NULL;
  243. SAFE_FREE(commit_values)
  244. DeleteEcPoint(&T);
  245. DeleteEcPoint(&R1);
  246. DeleteEcPoint(&R2);
  247. DeleteFfElement(&mu);
  248. DeleteFfElement(&nu);
  249. DeleteFfElement(&rmu);
  250. DeleteFfElement(&rnu);
  251. DeleteFfElement(&c);
  252. DeleteFfElement(&smu);
  253. DeleteFfElement(&snu);
  254. DeleteEcPoint(&B);
  255. DeleteEcPoint(&K);
  256. DeleteEcPoint(&rlB);
  257. DeleteEcPoint(&rlK);
  258. return res;
  259. }