bignum_wrapper-testhelper.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 Bignum C++ wrapper implementation.
  19. */
  20. #include "epid/common-testhelper/errors-testhelper.h"
  21. #include "epid/common-testhelper/bignum_wrapper-testhelper.h"
  22. #include "epid/common/math/bignum.h"
  23. /// bignum deleter type
  24. struct BigNumDeleter {
  25. /// bignum deleter
  26. void operator()(BigNum* bn) {
  27. if (bn) {
  28. DeleteBigNum(&bn);
  29. }
  30. }
  31. };
  32. /// bignum deleter singlton
  33. BigNumDeleter bignum_deleter;
  34. /// Internal state of the bignum wrapper
  35. struct BigNumObj::State {
  36. /// size of the stored BigNum
  37. size_t size;
  38. /// The stored BigNum
  39. std::shared_ptr<BigNum> bn_;
  40. /// Default initializing constructor
  41. State() : size(0), bn_() {}
  42. /// write a new value
  43. void write(unsigned char const* buf, size_t buflen, size_t len) {
  44. bool orig_has_data = (buf != nullptr) && (buflen > 0);
  45. std::shared_ptr<BigNum> bn;
  46. BigNum* bn_ptr = nullptr;
  47. THROW_ON_EPIDERR(NewBigNum(len, &bn_ptr));
  48. bn.reset(bn_ptr, bignum_deleter);
  49. size = len;
  50. if (orig_has_data) {
  51. THROW_ON_EPIDERR(ReadBigNum(buf, buflen, bn.get()));
  52. }
  53. bn_ = bn;
  54. }
  55. };
  56. BigNumObj::BigNumObj() : state_(new State) {
  57. state_->write(nullptr, 0, sizeof(BigNumStr));
  58. }
  59. BigNumObj::BigNumObj(BigNumObj const& other) : state_(new State) {
  60. bool orig_has_data = other.state_->bn_.get() != nullptr;
  61. std::vector<unsigned char> buf;
  62. if (orig_has_data) {
  63. buf.resize(other.state_->size);
  64. THROW_ON_EPIDERR(WriteBigNum(other.state_->bn_.get(), buf.size(), &buf[0]));
  65. }
  66. state_->write(&buf[0], other.state_->size, buf.size());
  67. }
  68. BigNumObj& BigNumObj::operator=(BigNumObj const& other) {
  69. bool orig_has_data = other.state_->bn_.get() != nullptr;
  70. std::vector<unsigned char> buf;
  71. if (orig_has_data) {
  72. buf.resize(other.state_->size);
  73. THROW_ON_EPIDERR(WriteBigNum(other.state_->bn_.get(), buf.size(), &buf[0]));
  74. }
  75. state_->write(&buf[0], other.state_->size, buf.size());
  76. return *this;
  77. }
  78. BigNumObj::BigNumObj(size_t data_size_bytes) : state_(new State) {
  79. state_->write(nullptr, 0, data_size_bytes);
  80. }
  81. BigNumObj::BigNumObj(size_t data_size_bytes,
  82. std::vector<unsigned char> const& bytes)
  83. : state_(new State) {
  84. state_->write(&bytes[0], bytes.size(), data_size_bytes);
  85. }
  86. BigNumObj::BigNumObj(size_t data_size_bytes, BigNumStr const& bytes)
  87. : state_(new State) {
  88. state_->write((unsigned char const*)&bytes, sizeof(BigNumStr),
  89. data_size_bytes);
  90. }
  91. BigNumObj::BigNumObj(std::vector<unsigned char> const& bytes)
  92. : state_(new State) {
  93. state_->write(&bytes[0], bytes.size(), bytes.size());
  94. }
  95. BigNumObj::BigNumObj(BigNumStr const& bytes) : state_(new State) {
  96. state_->write((unsigned char const*)&bytes, sizeof(BigNumStr),
  97. sizeof(BigNumStr));
  98. }
  99. BigNumObj::~BigNumObj() {}
  100. BigNumObj::operator BigNum*() { return state_->bn_.get(); }
  101. BigNumObj::operator const BigNum*() const { return state_->bn_.get(); }
  102. BigNum* BigNumObj::get() { return state_->bn_.get(); }
  103. BigNum const* BigNumObj::getc() const { return state_->bn_.get(); }