aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/math/bigint/bigint.cpp12
-rw-r--r--src/lib/math/bigint/bigint.h9
-rw-r--r--src/lib/pubkey/dsa/dsa.cpp4
-rw-r--r--src/lib/pubkey/ecdsa/ecdsa.cpp4
-rw-r--r--src/lib/pubkey/ecgdsa/ecgdsa.cpp4
5 files changed, 27 insertions, 6 deletions
diff --git a/src/lib/math/bigint/bigint.cpp b/src/lib/math/bigint/bigint.cpp
index 694c7afab..fd967e66e 100644
--- a/src/lib/math/bigint/bigint.cpp
+++ b/src/lib/math/bigint/bigint.cpp
@@ -93,6 +93,18 @@ BigInt::BigInt(const uint8_t input[], size_t length, Base base)
*this = decode(input, length, base);
}
+BigInt::BigInt(const uint8_t buf[], size_t length, size_t max_bits)
+ {
+ const size_t max_bytes = std::min(length, (max_bits + 7) / 8);
+ *this = decode(buf, max_bytes);
+
+ const size_t b = this->bits();
+ if(b > max_bits)
+ {
+ *this >>= (b - max_bits);
+ }
+ }
+
/*
* Construct a BigInt from an encoded BigInt
*/
diff --git a/src/lib/math/bigint/bigint.h b/src/lib/math/bigint/bigint.h
index 56b907d80..3d6626e4d 100644
--- a/src/lib/math/bigint/bigint.h
+++ b/src/lib/math/bigint/bigint.h
@@ -86,6 +86,15 @@ class BOTAN_PUBLIC_API(2,0) BigInt final
BigInt(const uint8_t buf[], size_t length, Base base);
/**
+ * Create a BigInt from an integer in a byte array
+ * @param buf the byte array holding the value
+ * @param length size of buf
+ * @param max_bits if the resulting integer is more than max_bits,
+ * it will be shifted so it is at most max_bits in length.
+ */
+ BigInt(const uint8_t buf[], size_t length, size_t max_bits);
+
+ /**
* Create a BigInt from an array of words
* @param words the words
* @param length number of words
diff --git a/src/lib/pubkey/dsa/dsa.cpp b/src/lib/pubkey/dsa/dsa.cpp
index 9249cd0d5..982e1b931 100644
--- a/src/lib/pubkey/dsa/dsa.cpp
+++ b/src/lib/pubkey/dsa/dsa.cpp
@@ -105,7 +105,7 @@ DSA_Signature_Operation::raw_sign(const uint8_t msg[], size_t msg_len,
{
const BigInt& q = m_group.get_q();
- BigInt i(msg, msg_len);
+ BigInt i(msg, msg_len, q.bits());
while(i >= q)
i -= q;
@@ -167,7 +167,7 @@ bool DSA_Verification_Operation::verify(const uint8_t msg[], size_t msg_len,
BigInt r(sig, q_bytes);
BigInt s(sig + q_bytes, q_bytes);
- BigInt i(msg, msg_len);
+ BigInt i(msg, msg_len, q.bits());
if(r <= 0 || r >= q || s <= 0 || s >= q)
return false;
diff --git a/src/lib/pubkey/ecdsa/ecdsa.cpp b/src/lib/pubkey/ecdsa/ecdsa.cpp
index 6ff02e8c9..f35693189 100644
--- a/src/lib/pubkey/ecdsa/ecdsa.cpp
+++ b/src/lib/pubkey/ecdsa/ecdsa.cpp
@@ -81,7 +81,7 @@ secure_vector<uint8_t>
ECDSA_Signature_Operation::raw_sign(const uint8_t msg[], size_t msg_len,
RandomNumberGenerator& rng)
{
- const BigInt m(msg, msg_len);
+ BigInt m(msg, msg_len, m_group.get_order_bits());
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
const BigInt k = generate_rfc6979_nonce(m_x, m_group.get_order(), m, m_rfc6979_hash);
@@ -134,7 +134,7 @@ bool ECDSA_Verification_Operation::verify(const uint8_t msg[], size_t msg_len,
if(sig_len != m_group.get_order_bytes() * 2)
return false;
- const BigInt e(msg, msg_len);
+ const BigInt e(msg, msg_len, m_group.get_order_bits());
const BigInt r(sig, sig_len / 2);
const BigInt s(sig + sig_len / 2, sig_len / 2);
diff --git a/src/lib/pubkey/ecgdsa/ecgdsa.cpp b/src/lib/pubkey/ecgdsa/ecgdsa.cpp
index db790b0d1..12962d18c 100644
--- a/src/lib/pubkey/ecgdsa/ecgdsa.cpp
+++ b/src/lib/pubkey/ecgdsa/ecgdsa.cpp
@@ -57,7 +57,7 @@ secure_vector<uint8_t>
ECGDSA_Signature_Operation::raw_sign(const uint8_t msg[], size_t msg_len,
RandomNumberGenerator& rng)
{
- const BigInt m(msg, msg_len);
+ const BigInt m(msg, msg_len, m_group.get_order_bits());
BigInt k = BigInt::random_integer(rng, 1, m_group.get_order());
@@ -107,7 +107,7 @@ bool ECGDSA_Verification_Operation::verify(const uint8_t msg[], size_t msg_len,
if(sig_len != m_group.get_order_bytes() * 2)
return false;
- const BigInt e(msg, msg_len);
+ const BigInt e(msg, msg_len, m_group.get_order_bits());
const BigInt r(sig, sig_len / 2);
const BigInt s(sig + sig_len / 2, sig_len / 2);