aboutsummaryrefslogtreecommitdiffstats
path: root/src/math/bigint
diff options
context:
space:
mode:
authorlloyd <[email protected]>2012-05-18 20:32:36 +0000
committerlloyd <[email protected]>2012-05-18 20:32:36 +0000
commitc691561f3198f481c13457433efbccc1c9fcd898 (patch)
treea45ea2c5a30e0cb009fbcb68a61ef39332ff790c /src/math/bigint
parentd76700f01c7ecac5633edf75f8d7408b46c5dbac (diff)
Fairly huge update that replaces the old secmem types with std::vector
using a custom allocator. Currently our allocator just does new/delete with a memset before deletion, and the mmap and mlock allocators have been removed.
Diffstat (limited to 'src/math/bigint')
-rw-r--r--src/math/bigint/big_code.cpp34
-rw-r--r--src/math/bigint/big_io.cpp2
-rw-r--r--src/math/bigint/big_ops2.cpp40
-rw-r--r--src/math/bigint/big_ops3.cpp28
-rw-r--r--src/math/bigint/big_rand.cpp2
-rw-r--r--src/math/bigint/bigint.cpp8
-rw-r--r--src/math/bigint/bigint.h64
7 files changed, 105 insertions, 73 deletions
diff --git a/src/math/bigint/big_code.cpp b/src/math/bigint/big_code.cpp
index 28614c9f1..a55ec662e 100644
--- a/src/math/bigint/big_code.cpp
+++ b/src/math/bigint/big_code.cpp
@@ -21,7 +21,7 @@ void BigInt::encode(byte output[], const BigInt& n, Base base)
n.binary_encode(output);
else if(base == Hexadecimal)
{
- SecureVector<byte> binary(n.encoded_size(Binary));
+ secure_vector<byte> binary(n.encoded_size(Binary));
n.binary_encode(&binary[0]);
hex_encode(reinterpret_cast<char*>(output),
@@ -61,9 +61,23 @@ void BigInt::encode(byte output[], const BigInt& n, Base base)
/*
* Encode a BigInt
*/
-SecureVector<byte> BigInt::encode(const BigInt& n, Base base)
+std::vector<byte> BigInt::encode(const BigInt& n, Base base)
{
- SecureVector<byte> output(n.encoded_size(base));
+ std::vector<byte> output(n.encoded_size(base));
+ encode(&output[0], n, base);
+ if(base != Binary)
+ for(size_t j = 0; j != output.size(); ++j)
+ if(output[j] == 0)
+ output[j] = '0';
+ return output;
+ }
+
+/*
+* Encode a BigInt
+*/
+secure_vector<byte> BigInt::encode_locked(const BigInt& n, Base base)
+ {
+ secure_vector<byte> output(n.encoded_size(base));
encode(&output[0], n, base);
if(base != Binary)
for(size_t j = 0; j != output.size(); ++j)
@@ -75,7 +89,7 @@ SecureVector<byte> BigInt::encode(const BigInt& n, Base base)
/*
* Encode a BigInt, with leading 0s if needed
*/
-SecureVector<byte> BigInt::encode_1363(const BigInt& n, size_t bytes)
+secure_vector<byte> BigInt::encode_1363(const BigInt& n, size_t bytes)
{
const size_t n_bytes = n.bytes();
if(n_bytes > bytes)
@@ -83,7 +97,7 @@ SecureVector<byte> BigInt::encode_1363(const BigInt& n, size_t bytes)
const size_t leading_0s = bytes - n_bytes;
- SecureVector<byte> output(bytes);
+ secure_vector<byte> output(bytes);
encode(&output[leading_0s], n, Binary);
return output;
}
@@ -91,14 +105,6 @@ SecureVector<byte> BigInt::encode_1363(const BigInt& n, size_t bytes)
/*
* Decode a BigInt
*/
-BigInt BigInt::decode(const MemoryRegion<byte>& buf, Base base)
- {
- return BigInt::decode(&buf[0], buf.size(), base);
- }
-
-/*
-* Decode a BigInt
-*/
BigInt BigInt::decode(const byte buf[], size_t length, Base base)
{
BigInt r;
@@ -106,7 +112,7 @@ BigInt BigInt::decode(const byte buf[], size_t length, Base base)
r.binary_decode(buf, length);
else if(base == Hexadecimal)
{
- SecureVector<byte> binary;
+ secure_vector<byte> binary;
if(length % 2)
{
diff --git a/src/math/bigint/big_io.cpp b/src/math/bigint/big_io.cpp
index 70e4a464a..130a98a3b 100644
--- a/src/math/bigint/big_io.cpp
+++ b/src/math/bigint/big_io.cpp
@@ -27,7 +27,7 @@ std::ostream& operator<<(std::ostream& stream, const BigInt& n)
{
if(n < 0)
stream.write("-", 1);
- SecureVector<byte> buffer = BigInt::encode(n, base);
+ const std::vector<byte> buffer = BigInt::encode(n, base);
size_t skip = 0;
while(buffer[skip] == '0' && skip < buffer.size())
++skip;
diff --git a/src/math/bigint/big_ops2.cpp b/src/math/bigint/big_ops2.cpp
index ff5cc7922..ea2bdc961 100644
--- a/src/math/bigint/big_ops2.cpp
+++ b/src/math/bigint/big_ops2.cpp
@@ -23,15 +23,15 @@ BigInt& BigInt::operator+=(const BigInt& y)
grow_to(reg_size);
if(sign() == y.sign())
- bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw);
+ bigint_add2(data(), reg_size - 1, y.data(), y_sw);
else
{
s32bit relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw);
if(relative_size < 0)
{
- SecureVector<word> z(reg_size - 1);
- bigint_sub3(z, y.data(), reg_size - 1, data(), x_sw);
+ secure_vector<word> z(reg_size - 1);
+ bigint_sub3(&z[0], y.data(), reg_size - 1, data(), x_sw);
copy_mem(&reg[0], &z[0], z.size());
set_sign(y.sign());
}
@@ -41,7 +41,7 @@ BigInt& BigInt::operator+=(const BigInt& y)
set_sign(Positive);
}
else if(relative_size > 0)
- bigint_sub2(get_reg(), x_sw, y.data(), y_sw);
+ bigint_sub2(data(), x_sw, y.data(), y_sw);
}
return (*this);
@@ -62,9 +62,9 @@ BigInt& BigInt::operator-=(const BigInt& y)
if(relative_size < 0)
{
if(sign() == y.sign())
- bigint_sub2_rev(get_reg(), y.data(), y_sw);
+ bigint_sub2_rev(data(), y.data(), y_sw);
else
- bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw);
+ bigint_add2(data(), reg_size - 1, y.data(), y_sw);
set_sign(y.reverse_sign());
}
@@ -76,14 +76,14 @@ BigInt& BigInt::operator-=(const BigInt& y)
set_sign(Positive);
}
else
- bigint_shl1(get_reg(), x_sw, 0, 1);
+ bigint_shl1(data(), x_sw, 0, 1);
}
else if(relative_size > 0)
{
if(sign() == y.sign())
- bigint_sub2(get_reg(), x_sw, y.data(), y_sw);
+ bigint_sub2(data(), x_sw, y.data(), y_sw);
else
- bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw);
+ bigint_add2(data(), reg_size - 1, y.data(), y_sw);
}
return (*this);
@@ -105,22 +105,22 @@ BigInt& BigInt::operator*=(const BigInt& y)
else if(x_sw == 1 && y_sw)
{
grow_to(y_sw + 2);
- bigint_linmul3(get_reg(), y.data(), y_sw, word_at(0));
+ bigint_linmul3(data(), y.data(), y_sw, word_at(0));
}
else if(y_sw == 1 && x_sw)
{
grow_to(x_sw + 2);
- bigint_linmul2(get_reg(), x_sw, y.word_at(0));
+ bigint_linmul2(data(), x_sw, y.word_at(0));
}
else
{
grow_to(size() + y.size());
- SecureVector<word> z(data(), x_sw);
- SecureVector<word> workspace(size());
+ secure_vector<word> z(data(), data() + x_sw);
+ secure_vector<word> workspace(size());
- bigint_mul(get_reg(), size(), workspace,
- z, z.size(), x_sw,
+ bigint_mul(data(), size(), &workspace[0],
+ &z[0], z.size(), x_sw,
y.data(), y.size(), y_sw);
}
@@ -159,7 +159,7 @@ word BigInt::operator%=(word mod)
word result = (word_at(0) & (mod - 1));
clear();
grow_to(2);
- get_reg()[0] = result;
+ reg[0] = result;
return result;
}
@@ -171,9 +171,9 @@ word BigInt::operator%=(word mod)
grow_to(2);
if(remainder && sign() == BigInt::Negative)
- get_reg()[0] = mod - remainder;
+ reg[0] = mod - remainder;
else
- get_reg()[0] = remainder;
+ reg[0] = remainder;
set_sign(BigInt::Positive);
@@ -192,7 +192,7 @@ BigInt& BigInt::operator<<=(size_t shift)
words = sig_words();
grow_to(words + shift_words + (shift_bits ? 1 : 0));
- bigint_shl1(get_reg(), words, shift_words, shift_bits);
+ bigint_shl1(data(), words, shift_words, shift_bits);
}
return (*this);
@@ -208,7 +208,7 @@ BigInt& BigInt::operator>>=(size_t shift)
const size_t shift_words = shift / MP_WORD_BITS,
shift_bits = shift % MP_WORD_BITS;
- bigint_shr1(get_reg(), sig_words(), shift_words, shift_bits);
+ bigint_shr1(data(), sig_words(), shift_words, shift_bits);
if(is_zero())
set_sign(Positive);
diff --git a/src/math/bigint/big_ops3.cpp b/src/math/bigint/big_ops3.cpp
index 52472bc52..a33b32bb7 100644
--- a/src/math/bigint/big_ops3.cpp
+++ b/src/math/bigint/big_ops3.cpp
@@ -23,20 +23,20 @@ BigInt operator+(const BigInt& x, const BigInt& y)
BigInt z(x.sign(), std::max(x_sw, y_sw) + 1);
if((x.sign() == y.sign()))
- bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
+ bigint_add3(z.data(), x.data(), x_sw, y.data(), y_sw);
else
{
s32bit relative_size = bigint_cmp(x.data(), x_sw, y.data(), y_sw);
if(relative_size < 0)
{
- bigint_sub3(z.get_reg(), y.data(), y_sw, x.data(), x_sw);
+ bigint_sub3(z.data(), y.data(), y_sw, x.data(), x_sw);
z.set_sign(y.sign());
}
else if(relative_size == 0)
z.set_sign(BigInt::Positive);
else if(relative_size > 0)
- bigint_sub3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
+ bigint_sub3(z.data(), x.data(), x_sw, y.data(), y_sw);
}
return z;
@@ -56,22 +56,22 @@ BigInt operator-(const BigInt& x, const BigInt& y)
if(relative_size < 0)
{
if(x.sign() == y.sign())
- bigint_sub3(z.get_reg(), y.data(), y_sw, x.data(), x_sw);
+ bigint_sub3(z.data(), y.data(), y_sw, x.data(), x_sw);
else
- bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
+ bigint_add3(z.data(), x.data(), x_sw, y.data(), y_sw);
z.set_sign(y.reverse_sign());
}
else if(relative_size == 0)
{
if(x.sign() != y.sign())
- bigint_shl2(z.get_reg(), x.data(), x_sw, 0, 1);
+ bigint_shl2(z.data(), x.data(), x_sw, 0, 1);
}
else if(relative_size > 0)
{
if(x.sign() == y.sign())
- bigint_sub3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
+ bigint_sub3(z.data(), x.data(), x_sw, y.data(), y_sw);
else
- bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
+ bigint_add3(z.data(), x.data(), x_sw, y.data(), y_sw);
z.set_sign(x.sign());
}
return z;
@@ -87,13 +87,13 @@ BigInt operator*(const BigInt& x, const BigInt& y)
BigInt z(BigInt::Positive, x.size() + y.size());
if(x_sw == 1 && y_sw)
- bigint_linmul3(z.get_reg(), y.data(), y_sw, x.word_at(0));
+ bigint_linmul3(z.data(), y.data(), y_sw, x.word_at(0));
else if(y_sw == 1 && x_sw)
- bigint_linmul3(z.get_reg(), x.data(), x_sw, y.word_at(0));
+ bigint_linmul3(z.data(), x.data(), x_sw, y.word_at(0));
else if(x_sw && y_sw)
{
- SecureVector<word> workspace(z.size());
- bigint_mul(z.get_reg(), z.size(), workspace,
+ secure_vector<word> workspace(z.size());
+ bigint_mul(z.data(), z.size(), &workspace[0],
x.data(), x.size(), x_sw,
y.data(), y.size(), y_sw);
}
@@ -164,7 +164,7 @@ BigInt operator<<(const BigInt& x, size_t shift)
const size_t x_sw = x.sig_words();
BigInt y(x.sign(), x_sw + shift_words + (shift_bits ? 1 : 0));
- bigint_shl2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits);
+ bigint_shl2(y.data(), x.data(), x_sw, shift_words, shift_bits);
return y;
}
@@ -183,7 +183,7 @@ BigInt operator>>(const BigInt& x, size_t shift)
x_sw = x.sig_words();
BigInt y(x.sign(), x_sw - shift_words);
- bigint_shr2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits);
+ bigint_shr2(y.data(), x.data(), x_sw, shift_words, shift_bits);
return y;
}
diff --git a/src/math/bigint/big_rand.cpp b/src/math/bigint/big_rand.cpp
index 7cddb2de0..a6776a296 100644
--- a/src/math/bigint/big_rand.cpp
+++ b/src/math/bigint/big_rand.cpp
@@ -35,7 +35,7 @@ void BigInt::randomize(RandomNumberGenerator& rng,
clear();
else
{
- SecureVector<byte> array = rng.random_vec((bitsize + 7) / 8);
+ secure_vector<byte> array = rng.random_vec((bitsize + 7) / 8);
if(bitsize % 8)
array[0] &= 0xFF >> (8 - (bitsize % 8));
diff --git a/src/math/bigint/bigint.cpp b/src/math/bigint/bigint.cpp
index df4414aba..5029c01f8 100644
--- a/src/math/bigint/bigint.cpp
+++ b/src/math/bigint/bigint.cpp
@@ -345,12 +345,4 @@ void BigInt::binary_decode(const byte buf[], size_t length)
reg[length / WORD_BYTES] = (reg[length / WORD_BYTES] << 8) | buf[i];
}
-/*
-* Set this number to the value in buf
-*/
-void BigInt::binary_decode(const MemoryRegion<byte>& buf)
- {
- binary_decode(buf, buf.size());
- }
-
}
diff --git a/src/math/bigint/bigint.h b/src/math/bigint/bigint.h
index 57aa84528..98b98bd6f 100644
--- a/src/math/bigint/bigint.h
+++ b/src/math/bigint/bigint.h
@@ -314,22 +314,29 @@ class BOTAN_DLL BigInt
* @result a pointer to the start of the internal register of
* the integer value
*/
+ word* data() { return &reg[0]; }
+
+ /**
+ * Return a pointer to the big integer word register
+ * @result a pointer to the start of the internal register of
+ * the integer value
+ */
const word* data() const { return &reg[0]; }
/**
* return a reference to the internal register containing the value
- * @result a reference to the word-array (SecureVector<word>)
+ * @result a reference to the word-array (secure_vector<word>)
* with the internal register value (containing the integer
* value)
*/
- SecureVector<word>& get_reg() { return reg; }
+ secure_vector<word>& get_reg() { return reg; }
/**
* return a const reference to the internal register containing the value
- * @result a const reference to the word-array (SecureVector<word>)
+ * @result a const reference to the word-array (secure_vector<word>)
* with the internal register value (containing the integer value)
*/
- const SecureVector<word>& get_reg() const { return reg; }
+ const secure_vector<word>& get_reg() const { return reg; }
/**
* Assign using a plain word array
@@ -369,10 +376,13 @@ class BOTAN_DLL BigInt
void binary_decode(const byte buf[], size_t length);
/**
- * Read integer value from a byte array (MemoryRegion<byte>)
+ * Read integer value from a byte array (secure_vector<byte>)
* @param buf the array to load from
*/
- void binary_decode(const MemoryRegion<byte>& buf);
+ void binary_decode(const secure_vector<byte>& buf)
+ {
+ binary_decode(&buf[0], buf.size());
+ }
/**
* @param base the base to measure the size for
@@ -391,12 +401,21 @@ class BOTAN_DLL BigInt
const BigInt& max);
/**
- * Encode the integer value from a BigInt to a SecureVector of bytes
+ * Encode the integer value from a BigInt to a std::vector of bytes
+ * @param n the BigInt to use as integer source
+ * @param base number-base of resulting byte array representation
+ * @result secure_vector of bytes containing the integer with given base
+ */
+ static std::vector<byte> encode(const BigInt& n, Base base = Binary);
+
+ /**
+ * Encode the integer value from a BigInt to a secure_vector of bytes
* @param n the BigInt to use as integer source
* @param base number-base of resulting byte array representation
- * @result SecureVector of bytes containing the integer with given base
+ * @result secure_vector of bytes containing the integer with given base
*/
- static SecureVector<byte> encode(const BigInt& n, Base base = Binary);
+ static secure_vector<byte> encode_locked(const BigInt& n,
+ Base base = Binary);
/**
* Encode the integer value from a BigInt to a byte array
@@ -423,16 +442,31 @@ class BOTAN_DLL BigInt
* @param base number-base of the integer in buf
* @result BigInt representing the integer in the byte array
*/
- static BigInt decode(const MemoryRegion<byte>& buf,
- Base base = Binary);
+ static BigInt decode(const secure_vector<byte>& buf,
+ Base base = Binary)
+ {
+ return BigInt::decode(&buf[0], buf.size(), base);
+ }
+
+ /**
+ * Create a BigInt from an integer in a byte array
+ * @param buf the binary value to load
+ * @param base number-base of the integer in buf
+ * @result BigInt representing the integer in the byte array
+ */
+ static BigInt decode(const std::vector<byte>& buf,
+ Base base = Binary)
+ {
+ return BigInt::decode(&buf[0], buf.size(), base);
+ }
/**
* Encode a BigInt to a byte array according to IEEE 1363
* @param n the BigInt to encode
- * @param bytes the length of the resulting SecureVector<byte>
- * @result a SecureVector<byte> containing the encoded BigInt
+ * @param bytes the length of the resulting secure_vector<byte>
+ * @result a secure_vector<byte> containing the encoded BigInt
*/
- static SecureVector<byte> encode_1363(const BigInt& n, size_t bytes);
+ static secure_vector<byte> encode_1363(const BigInt& n, size_t bytes);
/**
* Swap this value with another
@@ -528,7 +562,7 @@ class BOTAN_DLL BigInt
*/
BigInt& operator=(const BigInt&) = default;
private:
- SecureVector<word> reg;
+ secure_vector<word> reg;
Sign signedness;
};