aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/gnump
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/gnump')
-rw-r--r--src/engine/gnump/gmp_mem.cpp49
-rw-r--r--src/engine/gnump/gmp_wrap.cpp5
-rw-r--r--src/engine/gnump/gmp_wrap.h2
-rw-r--r--src/engine/gnump/gnump_pk.cpp8
4 files changed, 37 insertions, 27 deletions
diff --git a/src/engine/gnump/gmp_mem.cpp b/src/engine/gnump/gmp_mem.cpp
index 7cf11654d..b5a5a303e 100644
--- a/src/engine/gnump/gmp_mem.cpp
+++ b/src/engine/gnump/gmp_mem.cpp
@@ -7,6 +7,7 @@
#include <botan/internal/gnump_engine.h>
#include <cstring>
+#include <atomic>
#include <gmp.h>
namespace Botan {
@@ -14,36 +15,44 @@ namespace Botan {
namespace {
/*
-* Allocator used by GNU MP
+* For keeping track of existing GMP_Engines and only
+* resetting the memory when none are in use.
*/
-Allocator* gmp_alloc = 0;
-size_t gmp_alloc_refcnt = 0;
+std::atomic<size_t> gmp_alloc_refcnt(0);
/*
* Allocation Function for GNU MP
*/
void* gmp_malloc(size_t n)
{
- return gmp_alloc->allocate(n);
+ // Maintain alignment, mlock goes for sizeof(T) alignment
+ if(n % 8 == 0)
+ return secure_allocator<u64bit>().allocate(n / 8);
+ else if(n % 4 == 0)
+ return secure_allocator<u32bit>().allocate(n / 4);
+ else if(n % 2 == 0)
+ return secure_allocator<u16bit>().allocate(n / 2);
+
+ return secure_allocator<byte>().allocate(n);
}
/*
-* Reallocation Function for GNU MP
+* Deallocation Function for GNU MP
*/
-void* gmp_realloc(void* ptr, size_t old_n, size_t new_n)
+void gmp_free(void* ptr, size_t n)
{
- void* new_buf = gmp_alloc->allocate(new_n);
- std::memcpy(new_buf, ptr, std::min(old_n, new_n));
- gmp_alloc->deallocate(ptr, old_n);
- return new_buf;
+ secure_allocator<byte>().deallocate(static_cast<byte*>(ptr), n);
}
/*
-* Deallocation Function for GNU MP
+* Reallocation Function for GNU MP
*/
-void gmp_free(void* ptr, size_t n)
+void* gmp_realloc(void* ptr, size_t old_n, size_t new_n)
{
- gmp_alloc->deallocate(ptr, n);
+ void* new_buf = gmp_malloc(new_n);
+ std::memcpy(new_buf, ptr, std::min(old_n, new_n));
+ gmp_free(ptr, old_n);
+ return new_buf;
}
}
@@ -53,24 +62,22 @@ void gmp_free(void* ptr, size_t n)
*/
GMP_Engine::GMP_Engine()
{
- if(gmp_alloc == 0)
- {
- gmp_alloc = Allocator::get(true);
+ /*
+ if(gmp_alloc_refcnt == 0)
mp_set_memory_functions(gmp_malloc, gmp_realloc, gmp_free);
- }
- ++gmp_alloc_refcnt;
+ gmp_alloc_refcnt++;
+ */
}
GMP_Engine::~GMP_Engine()
{
+ /*
--gmp_alloc_refcnt;
if(gmp_alloc_refcnt == 0)
- {
mp_set_memory_functions(NULL, NULL, NULL);
- gmp_alloc = 0;
- }
+ */
}
}
diff --git a/src/engine/gnump/gmp_wrap.cpp b/src/engine/gnump/gmp_wrap.cpp
index 107823ab3..22c46c7ad 100644
--- a/src/engine/gnump/gmp_wrap.cpp
+++ b/src/engine/gnump/gmp_wrap.cpp
@@ -87,7 +87,10 @@ BigInt GMP_MPZ::to_bigint() const
{
BigInt out(BigInt::Positive, (bytes() + sizeof(word) - 1) / sizeof(word));
size_t dummy = 0;
- mpz_export(out.get_reg(), &dummy, -1, sizeof(word), 0, 0, value);
+
+ auto reg = out.get_reg();
+
+ mpz_export(&reg[0], &dummy, -1, sizeof(word), 0, 0, value);
if(mpz_sgn(value) < 0)
out.flip_sign();
diff --git a/src/engine/gnump/gmp_wrap.h b/src/engine/gnump/gmp_wrap.h
index 0a786f3ee..291d65a01 100644
--- a/src/engine/gnump/gmp_wrap.h
+++ b/src/engine/gnump/gmp_wrap.h
@@ -26,7 +26,7 @@ class GMP_MPZ
size_t bytes() const;
secure_vector<byte> to_bytes() const
- { return BigInt::encode(to_bigint()); }
+ { return BigInt::encode_locked(to_bigint()); }
GMP_MPZ& operator=(const GMP_MPZ&);
diff --git a/src/engine/gnump/gnump_pk.cpp b/src/engine/gnump/gnump_pk.cpp
index b2a2f9352..e9f5d29df 100644
--- a/src/engine/gnump/gnump_pk.cpp
+++ b/src/engine/gnump/gnump_pk.cpp
@@ -105,8 +105,8 @@ GMP_DSA_Signature_Operation::sign(const byte msg[], size_t msg_len,
throw Internal_Error("GMP_DSA_Op::sign: r or s was zero");
secure_vector<byte> output(2*q_bytes);
- r.encode(output, q_bytes);
- s.encode(output + q_bytes, q_bytes);
+ r.encode(&output[0], q_bytes);
+ s.encode(&output[q_bytes], q_bytes);
return output;
}
@@ -203,7 +203,7 @@ class GMP_RSA_Private_Operation : public PK_Ops::Signature,
secure_vector<byte> decrypt(const byte msg[], size_t msg_len)
{
BigInt m(msg, msg_len);
- return BigInt::encode(private_op(m));
+ return BigInt::encode_locked(private_op(m));
}
private:
@@ -248,7 +248,7 @@ class GMP_RSA_Public_Operation : public PK_Ops::Verification,
secure_vector<byte> verify_mr(const byte msg[], size_t msg_len)
{
BigInt m(msg, msg_len);
- return BigInt::encode(public_op(m));
+ return BigInt::encode_locked(public_op(m));
}
private: