summaryrefslogtreecommitdiff
path: root/mpi.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'mpi.cpp')
-rw-r--r--mpi.cpp80
1 files changed, 59 insertions, 21 deletions
diff --git a/mpi.cpp b/mpi.cpp
index 18a3349..a2590fa 100644
--- a/mpi.cpp
+++ b/mpi.cpp
@@ -36,41 +36,71 @@
#include <stdexcept>
#include <gcrypt.h>
+#include <iostream>
-Mpi::Mpi()
+Mpi::Mpi() : val_(NULL)
{
- val_ = gcry_mpi_new(1);
+ val_ = gcry_mpi_set_ui(NULL, 0);
+ if(!val_)
+ throw std::bad_alloc();
}
-Mpi::Mpi(u_int8_t length)
+Mpi::Mpi(u_int8_t length) : val_(NULL)
{
val_ = gcry_mpi_new(length);
+ if(!val_)
+ throw std::bad_alloc();
}
-Mpi::Mpi(const Mpi &src)
+Mpi::Mpi(const Mpi &src) : val_(NULL)
{
val_ = gcry_mpi_copy(src.val_);
+ if(!val_)
+ throw std::bad_alloc();
}
-Mpi::Mpi(const u_int8_t * src, u_int32_t len)
+Mpi::Mpi(const u_int8_t* src, u_int32_t len) : val_(NULL)
{
- gcry_mpi_scan( &val_, GCRYMPI_FMT_STD, src, len, NULL );
+ u_int8_t* src_cpy = new u_int8_t[len+1];
+ if(!src_cpy)
+ throw std::bad_alloc();
+
+ u_int8_t* buf = src_cpy;
+ u_int32_t buf_len = len;
+ if(src[0] & 0x80) // this would be a negative number, scan can't handle this :(
+ {
+ src_cpy[0] = 0;
+ buf++;
+ buf_len++;
+ }
+ std::memcpy(buf, src, len);
+
+ gcry_mpi_scan( &val_, GCRYMPI_FMT_STD, src_cpy, buf_len, NULL );
+ delete[] src_cpy;
+ if(!val_)
+ throw std::bad_alloc();
}
Mpi::~Mpi()
{
- gcry_mpi_release( val_ );
+ gcry_mpi_release( val_ );
}
void Mpi::operator=(const Mpi &src)
{
+ gcry_mpi_release( val_ );
val_ = gcry_mpi_copy(src.val_);
+ if(!val_)
+ throw std::bad_alloc();
}
void Mpi::operator=(const u_int32_t src)
{
- gcry_mpi_set_ui(val_, src);
+ gcry_mpi_release( val_ );
+ val_ = gcry_mpi_set_ui(NULL, src);
+ if(!val_)
+ throw std::bad_alloc();
}
Mpi Mpi::operator+(const Mpi &b) const
@@ -125,22 +155,29 @@ Mpi Mpi::mul2exp(u_int32_t e) const
return res;
}
-u_int8_t* Mpi::getNewBuf(u_int32_t buf_len) const
+//TODO: problem, seems as gcry_mpi_(a)print doesn't work for mpi values of '0'
+u_int8_t* Mpi::getNewBuf(u_int32_t* written) const
{
- // u_int32_t len = 0;
- u_int32_t written = 0;
+ u_int8_t* res_cpy;
+ gcry_mpi_aprint( GCRYMPI_FMT_STD, &res_cpy, written, val_ );
+ if(!res_cpy)
+ throw std::bad_alloc();
+
+ u_int8_t* buf = res_cpy;
+ if(*written > 1 && ! (res_cpy[0])) // positive number with highestBit set
+ {
+ buf++;
+ (*written)--;
+ }
+
+ u_int8_t* res = new u_int8_t[*written];
+ if(!res)
+ throw std::bad_alloc();
- u_int8_t *res = NULL;
- res = new u_int8_t[buf_len];
- std::memset(res, 0, buf_len);
+ std::memcpy(res, buf, *written);
- // len = gcry_mpi_get_nbits( val_ );
- // if( len%8 == 0 )
- // len = len/8;
- // else
- // len = (len/8)+1;
+ gcry_free(res_cpy);
- gcry_mpi_print( GCRYMPI_FMT_STD, res, buf_len, &written, val_ );
return res;
}
@@ -151,7 +188,8 @@ std::string Mpi::getHexDump() const
// u_int32_t len;
// gcry_mpi_aprint( GCRYMPI_FMT_HEX, &buf, &len, val_ );
// std::string res(buf, len);
-
+// delete[] buf;
+
gcry_mpi_dump( val_ );
std::string res("\n");
return res;