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