commitment.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 Commitment hash implementation.
  19. */
  20. #include "epid/common/src/commitment.h"
  21. #include <limits.h>
  22. #include "epid/common/math/ecgroup.h"
  23. #include "epid/common/src/memory.h"
  24. EpidStatus SetKeySpecificCommitValues(GroupPubKey const* pub_key,
  25. CommitValues* values) {
  26. static const Epid2Params params = {
  27. #include "epid/common/src/epid2params_ate.inc"
  28. };
  29. if (!pub_key || !values) return kEpidBadArgErr;
  30. values->p = params.p;
  31. values->g1 = params.g1;
  32. values->g2 = params.g2;
  33. values->h1 = pub_key->h1;
  34. values->h2 = pub_key->h2;
  35. values->w = pub_key->w;
  36. return kEpidNoErr;
  37. }
  38. EpidStatus SetCalculatedCommitValues(G1ElemStr const* B, G1ElemStr const* K,
  39. G1ElemStr const* T, EcPoint const* R1,
  40. EcGroup* G1, FfElement const* R2,
  41. FiniteField* GT, CommitValues* values) {
  42. EpidStatus sts;
  43. if (!B || !K || !T || !R1 || !G1 || !R2 || !GT || !values) {
  44. return kEpidBadArgErr;
  45. }
  46. values->B = *B;
  47. values->K = *K;
  48. values->T = *T;
  49. sts = WriteEcPoint(G1, R1, &values->R1, sizeof(values->R1));
  50. if (kEpidNoErr != sts) return sts;
  51. sts = WriteFfElement(GT, R2, &values->R2, sizeof(values->R2));
  52. if (kEpidNoErr != sts) return sts;
  53. return kEpidNoErr;
  54. }
  55. EpidStatus CalculateCommitmentHash(CommitValues const* values, FiniteField* Fp,
  56. HashAlg hash_alg, void const* msg,
  57. size_t msg_len, FfElement* c) {
  58. EpidStatus sts;
  59. FfElement* t3 = NULL;
  60. size_t t3mconcat_size = sizeof(FpElemStr) + msg_len;
  61. uint8_t* t3mconcat_buf = NULL;
  62. if (!values || !Fp || !c) return kEpidBadArgErr;
  63. if (!msg && (0 != msg_len)) {
  64. // if message is non-empty it must have both length and content
  65. return kEpidBadArgErr;
  66. }
  67. if (SIZE_MAX - sizeof(FpElemStr) < msg_len) {
  68. return kEpidBadArgErr;
  69. }
  70. do {
  71. sts = NewFfElement(Fp, &t3);
  72. if (kEpidNoErr != sts) break;
  73. // compute t3 = Fp.hash(p || g1 || g2 || h1 ||
  74. // h2 || w || B || K || T || R1 || R2).
  75. sts = FfHash(Fp, values, sizeof(*values), hash_alg, t3);
  76. if (kEpidNoErr != sts) break;
  77. // compute c = Fp.hash(t3 || m).
  78. t3mconcat_buf = SAFE_ALLOC(t3mconcat_size);
  79. if (!t3mconcat_buf) {
  80. sts = kEpidMemAllocErr;
  81. break;
  82. }
  83. // get t3 into buffer
  84. sts = WriteFfElement(Fp, t3, t3mconcat_buf, sizeof(FpElemStr));
  85. if (kEpidNoErr != sts) break;
  86. // get m into buffer
  87. if (msg) {
  88. // Memory copy is used to copy a message of variable length
  89. if (0 != memcpy_S(t3mconcat_buf + sizeof(FpElemStr),
  90. t3mconcat_size - sizeof(FpElemStr), msg, msg_len)) {
  91. sts = kEpidBadArgErr;
  92. break;
  93. }
  94. }
  95. sts = FfHash(Fp, t3mconcat_buf, t3mconcat_size, hash_alg, c);
  96. if (kEpidNoErr != sts) break;
  97. sts = kEpidNoErr;
  98. } while (0);
  99. SAFE_FREE(t3mconcat_buf);
  100. DeleteFfElement(&t3);
  101. return sts;
  102. }