aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/math
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-03-20 12:15:35 -0400
committerJack Lloyd <[email protected]>2018-03-21 03:40:00 -0400
commitad66550111bac3f64fdf3eef4c630a9eb09be321 (patch)
tree824204d40fbdd77c5ac622a8ac022950ab792b3d /src/lib/math
parent230ec136952ce4077b988302e940518a8f5454f2 (diff)
Shift ECDSA inputs to match OpenSSL behavior
See also GH #986
Diffstat (limited to 'src/lib/math')
-rw-r--r--src/lib/math/bigint/bigint.cpp12
-rw-r--r--src/lib/math/bigint/bigint.h9
2 files changed, 21 insertions, 0 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