nrprove_commitment.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*############################################################################
  2. # Copyright 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. /// Host non-revoked proof helper implementation
  17. /*! \file */
  18. #include "epid/member/src/nrprove_commitment.h"
  19. #include <stdint.h>
  20. #include "epid/common/math/finitefield.h"
  21. #include "epid/common/src/memory.h"
  22. /// Handle SDK Error with Break
  23. #define BREAK_ON_EPID_ERROR(ret) \
  24. if (kEpidNoErr != (ret)) { \
  25. break; \
  26. }
  27. #pragma pack(1)
  28. /// Storage for values to create commitment in NrProve algorithm
  29. typedef struct NrProveCommitValues {
  30. BigNumStr p; //!< A large prime (256-bit)
  31. G1ElemStr g1; //!< Generator of G1 (512-bit)
  32. G1ElemStr B; //!< (element of G1): part of basic signature Sigma0
  33. G1ElemStr K; //!< (element of G1): part of basic signature Sigma0
  34. G1ElemStr rlB; //!< (element of G1): one entry in SigRL
  35. G1ElemStr rlK; //!< (element of G1): one entry in SigRL
  36. NrProveCommitOutput commit_out; //!< output of NrProveCommit
  37. uint8_t msg[1]; //!< message
  38. } NrProveCommitValues;
  39. #pragma pack()
  40. EpidStatus HashNrProveCommitment(FiniteField* Fp, HashAlg hash_alg,
  41. G1ElemStr const* B_str, G1ElemStr const* K_str,
  42. SigRlEntry const* sigrl_entry,
  43. NrProveCommitOutput const* commit_out,
  44. void const* msg, size_t msg_len,
  45. FpElemStr* c_str) {
  46. EpidStatus sts = kEpidErr;
  47. FfElement* c = NULL;
  48. NrProveCommitValues* commit_values = NULL;
  49. if (!Fp || !B_str || !K_str || !sigrl_entry || !commit_out ||
  50. (0 != msg_len && !msg) || !c_str) {
  51. return kEpidBadArgErr;
  52. }
  53. if (msg_len >
  54. ((SIZE_MAX - sizeof(*commit_values)) + sizeof(*commit_values->msg)))
  55. return kEpidBadArgErr;
  56. do {
  57. size_t const commit_len =
  58. sizeof(*commit_values) - sizeof(*commit_values->msg) + msg_len;
  59. Epid2Params params = {
  60. #include "epid/common/src/epid2params_ate.inc"
  61. };
  62. commit_values = SAFE_ALLOC(commit_len);
  63. if (!commit_values) {
  64. sts = kEpidMemAllocErr;
  65. BREAK_ON_EPID_ERROR(sts);
  66. }
  67. commit_values->p = params.p;
  68. commit_values->g1 = params.g1;
  69. commit_values->B = *B_str;
  70. commit_values->K = *K_str;
  71. commit_values->rlB = sigrl_entry->b;
  72. commit_values->rlK = sigrl_entry->k;
  73. commit_values->commit_out = *commit_out;
  74. // commit_values is allocated such that there are msg_len bytes available
  75. // starting at commit_values->msg
  76. if (msg) {
  77. // Memory copy is used to copy a message of variable length
  78. if (0 != memcpy_S(&commit_values->msg[0], msg_len, msg, msg_len)) {
  79. sts = kEpidBadArgErr;
  80. BREAK_ON_EPID_ERROR(sts);
  81. }
  82. }
  83. sts = NewFfElement(Fp, &c);
  84. BREAK_ON_EPID_ERROR(sts);
  85. // 7. The member computes c = Fp.hash(p || g1 || B || K || B' ||
  86. // K' || T || R1 || R2 || m).
  87. sts = FfHash(Fp, commit_values, commit_len, hash_alg, c);
  88. BREAK_ON_EPID_ERROR(sts);
  89. sts = WriteFfElement(Fp, c, c_str, sizeof(*c_str));
  90. BREAK_ON_EPID_ERROR(sts);
  91. sts = kEpidNoErr;
  92. } while (0);
  93. SAFE_FREE(commit_values);
  94. DeleteFfElement(&c);
  95. return sts;
  96. }