ecpoint_wrapper-testhelper.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 EcPoint C++ wrapper implementation.
  19. */
  20. #include "epid/common-testhelper/errors-testhelper.h"
  21. #include "epid/common-testhelper/ecpoint_wrapper-testhelper.h"
  22. #include "epid/common-testhelper/ecgroup_wrapper-testhelper.h"
  23. #include "epid/common/math/bignum.h"
  24. /// ecpoint deleter type
  25. struct EcPointDeleter {
  26. /// ecpoint deleter
  27. void operator()(EcPoint* ptr) {
  28. if (ptr) {
  29. DeleteEcPoint(&ptr);
  30. }
  31. }
  32. };
  33. /// ecpoint deleter singlton
  34. EcPointDeleter ecpoint_deleter;
  35. /// Internal state of the ecpoint wrapper
  36. struct EcPointObj::State {
  37. /// The containing field
  38. EcGroupObj group_;
  39. /// The stored EcPoint
  40. std::shared_ptr<EcPoint> point_;
  41. State() : group_(), point_() {}
  42. /// write a new value
  43. void write(EcGroupObj* group, unsigned char const* buf, size_t buflen) {
  44. group_ = *group;
  45. bool orig_has_data = (buf != nullptr) && (buflen > 0);
  46. std::shared_ptr<EcPoint> point;
  47. EcPoint* point_ptr;
  48. THROW_ON_EPIDERR(NewEcPoint(group_, &point_ptr));
  49. point.reset(point_ptr, ecpoint_deleter);
  50. if (orig_has_data) {
  51. THROW_ON_EPIDERR(ReadEcPoint(group_, buf, buflen, point.get()));
  52. }
  53. point_ = point;
  54. }
  55. };
  56. EcPointObj::EcPointObj() : state_(new State) {}
  57. EcPointObj::EcPointObj(EcPointObj const& other) : state_(new State) {
  58. std::vector<unsigned char> buf = other.data();
  59. state_->write(&other.state_->group_, &buf[0], buf.size());
  60. }
  61. EcPointObj& EcPointObj::operator=(EcPointObj const& other) {
  62. std::vector<unsigned char> buf = other.data();
  63. state_->write(&other.state_->group_, &buf[0], buf.size());
  64. return *this;
  65. }
  66. EcPointObj::EcPointObj(EcGroupObj* group) : state_(new State) {
  67. state_->write(group, nullptr, 0);
  68. }
  69. EcPointObj::EcPointObj(EcGroupObj* group, G1ElemStr const& bytes)
  70. : state_(new State) {
  71. init(group, (unsigned char*)&bytes, sizeof(bytes));
  72. }
  73. EcPointObj::EcPointObj(EcGroupObj* group, G2ElemStr const& bytes)
  74. : state_(new State) {
  75. init(group, (unsigned char*)&bytes, sizeof(bytes));
  76. }
  77. EcPointObj::EcPointObj(EcGroupObj* group, Epid11G2ElemStr const& bytes)
  78. : state_(new State) {
  79. init(group, (unsigned char*)&bytes, sizeof(bytes));
  80. }
  81. EcPointObj::EcPointObj(EcGroupObj* group,
  82. std::vector<unsigned char> const& bytes)
  83. : state_(new State) {
  84. init(group, &bytes[0], bytes.size());
  85. }
  86. EcPointObj::EcPointObj(EcGroupObj* group, void const* bytes, size_t size)
  87. : state_(new State) {
  88. init(group, (unsigned char const*)bytes, size);
  89. }
  90. void EcPointObj::init(EcGroupObj* group, unsigned char const* bytes,
  91. size_t size) {
  92. state_->write(group, bytes, size);
  93. }
  94. EcPointObj::~EcPointObj() {}
  95. EcPointObj::operator EcPoint*() { return state_->point_.get(); }
  96. EcPointObj::operator const EcPoint*() const { return state_->point_.get(); }
  97. EcPoint* EcPointObj::get() { return state_->point_.get(); }
  98. EcPoint const* EcPointObj::getc() const { return state_->point_.get(); }
  99. std::vector<unsigned char> EcPointObj::data() const {
  100. std::vector<unsigned char> buf;
  101. if (state_->point_.get() != nullptr) {
  102. buf.resize(state_->group_.GetElementMaxSize());
  103. THROW_ON_EPIDERR(WriteEcPoint(state_->group_, state_->point_.get(), &buf[0],
  104. buf.size()));
  105. }
  106. return buf;
  107. }